<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Javier Muñoz on Medium]]></title>
        <description><![CDATA[Stories by Javier Muñoz on Medium]]></description>
        <link>https://medium.com/@jaedmuva?source=rss-8ef59565941f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SaSdlpDMymOIU4TH.</url>
            <title>Stories by Javier Muñoz on Medium</title>
            <link>https://medium.com/@jaedmuva?source=rss-8ef59565941f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 23:04:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jaedmuva/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React Native SSL Pinning is back! — iOS version]]></title>
            <link>https://medium.com/@jaedmuva/react-native-ssl-pinning-is-back-ios-version-814dfce2400c?source=rss-8ef59565941f------2</link>
            <guid isPermaLink="false">https://medium.com/p/814dfce2400c</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[trustkit]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[ssl-pinning]]></category>
            <dc:creator><![CDATA[Javier Muñoz]]></dc:creator>
            <pubDate>Fri, 27 Apr 2018 20:10:50 GMT</pubDate>
            <atom:updated>2018-04-27T20:10:50.516Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Actually, it never like “went away” in iOS, but we’ll keep the title honoring the first story with the Android version.</em></p><p>Finally! Got some time to write this for you guys, hope it isn’t too late. Please forgive me for I’m not an iOS developer myself, not a very experience now 😉. So. Let’s get to it.</p><p>There is very little about SSL pinning out there with React Native. I checked out some very good options, including <a href="https://github.com/localz/react-native-pinch">react-native-pinch</a> and <a href="https://github.com/Statoil/react-native-trustkit">react-native-trustkit-wrapper</a>, claps for you guys 👏🏼👏🏼! Sadly, none of them convinced me to take their way, so I made the tough call: implement pinning the old-native way.</p><p>Some of you might say: “<em>but the title says React Native! why would you do that!?</em>” Here are my motivations:</p><ul><li>react-native-pinch: which is like the most taken option - I believe. It provides you with a nice network API, just like RN’s fetch. But the thing is that I was not using fetch in my app, which would’ve made it way easier to take this option btw. And I was not willing to change the current library, I really like it — btw, it’s axios (pls, don’t yell at me). Additionally, this module wouldn’t let you specify PubliKey info, only the actual certificates, which leads to rebuild the app in case the certificate (for any reason) is compromised or just expires.</li><li>react-native-trustkit-wrapper: is a very nice approach for making a module implementing <a href="https://github.com/datatheorem/TrustKit">TrutKit</a> (I used it in my Android implementation so I think I’m biased to it XD), but! — there’s always a but- it was not very straightforward as I read somewhere, and you would have to restart the app after first launch or so. Then I was not so convinced of using this module.</li></ul><p>That said. I went with TrustKit for iOS and the results are very convincing, so this is it:</p><p>First of all, you’ll have to get TrustKit in your app, and you can do so using Pods or Carthage. I prefer Carthage so I used it. Setup is very easy, you can go through the <a href="https://github.com/datatheorem/TrustKit/blob/master/docs/getting-started.md">Getting Started</a> documentation to get a better understanding of what we’re doing here.</p><p>In your XCode project, add TrustKit Framework as an embedded binary to your app’s target. Then add the configuration needed to initialize TrustKit. The latter can be done in two ways: programmatically or using Info.plist file. I used an altered or extended version of the Info.plist file because I have more build configurations other that the default Debug and Release RN projects come with.</p><p>In the background, TrustKit reads the Info.plist file in order to create a dictionary to initialize itself, so when you add your configuration to Info.plist, it reads the file, creates a dictionary and gets initialized automatically without you having to write any code. Based on that, I added a few lines of code to initialize TrustKit programmatically with all the configuration needed located in different .plist files, and TrustKit will be configured depending on which build configuration (environment) you want to deploy for.</p><p>Firs step, we’re going to create those configuration files. Go to your project folder, right click, <em>New file…</em> Select <em>Property list</em> file type and name it properly. Do this for as many build configuration or environments as you have or need.</p><p>Now, go to your target’s <em>Build settings</em> and add a <em>User defined</em> setting. We’re going to call it TK_CONFIG. You will be able to specify a value for every build configuration you have. In my case, I put the file name of the corresponding .plist file without the extension. This is because I placed those files in the project’s folder root. If you want to place them elsewhere, just make sure you specify the right path to the file.</p><p>We’re almost done. Now we’ll add the code I mentioned before in our <em>AppDelegate.m</em> file and we’re ready to go. Open the <em>AppDelegate.m </em>file and put this inside <em>didFinishLaunchingWithOptions</em> method before anything else:</p><pre>NSString *fileName = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), (__bridge CFStringRef)@&quot;TK_CONFIG_FILE&quot;);</pre><pre>NSString *thePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@&quot;plist&quot;];</pre><pre>NSDictionary *properties = [NSDictionary dictionaryWithContentsOfFile:thePath];</pre><pre>[TrustKit initSharedInstanceWithConfiguration:properties];</pre><p>And that’s it! TrustKit will be ready to work and make sure SSL Pinning rises your app’s security. There are only a couple things you have to make sure:</p><ul><li>Put the code above as the first thing in the method, otherwise TrustKit won’t work properly in your app.</li><li>Add TSKSwizzleNetworkDelegates as true in your configuration file. This will instruct TrustKit to modify your app’s network delegates. That’s right, the ones that React Native uses for networking.</li></ul><p>That’d be all! It’s more easier to do than reading this story 😉. Please leave a comment if I missed something and a big THANKS for taking the time of reading this, really hope it helped!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=814dfce2400c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Native SSL Pinning is back! — Android version]]></title>
            <link>https://medium.com/@jaedmuva/react-native-ssl-pinning-is-back-e317e6682642?source=rss-8ef59565941f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e317e6682642</guid>
            <category><![CDATA[network-security]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[trutkit]]></category>
            <category><![CDATA[ssl-pinning]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Javier Muñoz]]></dc:creator>
            <pubDate>Wed, 21 Mar 2018 18:19:39 GMT</pubDate>
            <atom:updated>2018-04-04T15:50:20.055Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Disclaimer: First story here, not much of a writter and not native speaker… That said, this is it:</em></p><p><em>Update: I separeted Andriod and iOS implementations for each one has different approaches and found two of them for Android which is what you are reading here.</em></p><p>After a few hours searching the web for a solution for this rather common topic for those of us who are starting with React Native, I came across with a few possible ways of doing this, here I will mention two of them.</p><h4><strong>OkHttpClient using CertificatePinner</strong></h4><p>Thanks to <a href="https://www.madebymany.com/stories/a-year-of-react-native-ssl-pinning">this very comprehensive article</a> and the proposal mentioned there, I thought it is a very straightforward way to nail SSL Pinning. Apparently it was pretty easy, but wait! React Native, somewhere in the whole commit storm it constantly has, removed the ability to replace Android’s OkHttpClient, at least that was what I read and didn’t have the time to verify. What a mess! well, not so much. Version 0.54 added it back! Thank God I just updated the RN version in my project. But the code is still not working!</p><p>The thing is, customizing Android’s network client is possible again, but not in the old fashion. Checking out RN’s source code, I realized this time they are using the factory pattern and it’s great! And as simple as that, we just have to pass our custom Factory class and that’s it. Let’s take a look at that code:</p><pre>@Override<br>protected void onCreate(Bundle savedInstanceState) {</pre><pre>super.onCreate(savedInstanceState);</pre><pre>OkHttpClientProvider.setOkHttpClientFactory(new CustomClientFactory());</pre><pre>}</pre><p>Easy right? Just add that piece of code to your MainActivity’s onCreate method and that’s it! The only thing left is to write our CustomClientFactory and we’re ready to go:</p><pre>import com.facebook.react.modules.network.OkHttpClientFactory;</pre><pre>import com.facebook.react.modules.network.OkHttpClientProvider;</pre><pre>import com.facebook.react.modules.network.ReactCookieJarContainer;</pre><pre>import java.util.concurrent.TimeUnit;</pre><pre>import okhttp3.CertificatePinner;</pre><pre>import okhttp3.OkHttpClient;</pre><pre>public class CustomClientFactory implements OkHttpClientFactory {</pre><pre>private static String hostname = &quot;*.your.service.com&quot;;</pre><pre>@Override</pre><pre>public OkHttpClient createNewNetworkModuleClient() {</pre><pre>CertificatePinner certificatePinner = new CertificatePinner.Builder()</pre><pre>.add(hostname, &quot;sha256/YOUR_PUBLIC_KEY_HASH&quot;)</pre><pre>.add(hostname, &quot;sha256/YOUR_PUBLIC_KEY_HASH_BACKUP1&quot;)</pre><pre>.add(hostname, &quot;sha256/YOUR_PUBLIC_KEY_HASH_BACKUP2&quot;)</pre><pre>.build();</pre><pre>OkHttpClient.Builder client = new OkHttpClient.Builder()</pre><pre>.connectTimeout(0, TimeUnit.MILLISECONDS)</pre><pre>.readTimeout(0, TimeUnit.MILLISECONDS)</pre><pre>.writeTimeout(0, TimeUnit.MILLISECONDS)</pre><pre>.cookieJar(new ReactCookieJarContainer())</pre><pre>.certificatePinner(certificatePinner);</pre><pre>return OkHttpClientProvider.enableTls12OnPreLollipop(client).build();</pre><pre>}</pre><pre>}</pre><p>That’s it 🎉. Where did all that code come from? I used the same lines from OkHttpClientProvider so there are no differences with the default client provided by React Native. Of course, you can add/remove anything that meets your needs foryour custom client, and we’ll be sure it will work for all of your network requests made through React Native’s Networking module in Android. At least for RN 0.54.2, the version used when this was written.</p><h4><strong>TrustKit for Android</strong></h4><p>There is another approach in Android and we’ll use the previous one to make use of this great tool and React Native. Instead of adding a CertificatePinner to OkHttpClient, we are going to use <a href="https://github.com/datatheorem/TrustKit-Android">TrustKit for Android</a> which I found to be a very useful library as it:</p><ul><li>Sticks to <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning">HKPK </a>specification, and</li><li>Makes use of Android’s official <a href="https://developer.android.com/training/articles/security-config.html">Network Security Configuration</a></li></ul><p>There is gonna be a little bit more configuration to be done but it is worth it. As mentioned before, TrustKit for Android uses Network Security Configuration which is very useful if you are targeting Android Nougat or later, but what if your target is lower? TrustKit for Android to the rescue! It makes security configuration available for targets below API 24, using basically the same xml file and tags you’ll use with official support. Enough talking, let’s get to it.</p><p>First of all, we are going to add TrustKit’s dependency to our android project implementation ‘com.datatheorem.android.trustkit:trustkit:+’ . If you need a specific version of TrustKit you should specify it instead of + .</p><p>TrustKit for Android, as stated in it’s <a href="https://datatheorem.github.io/TrustKit-Android/documentation/">documentation</a>, only supports the following XML tags, so take that in account when using it along the official API:</p><ul><li>&lt;domain-config&gt;.</li><li>&lt;domain&gt; and the includeSubdomains attribute.</li><li>&lt;pin-set&gt; and the expiration attribute.</li><li>&lt;pin&gt; and the digest attribute.</li><li>&lt;debug-overrides&gt;.</li><li>&lt;trust-anchors&gt;, but only within a &lt;debug-overrides&gt; tag. Hence, custom trust anchors for specific domains cannot be set.</li><li>&lt;certificates&gt; and the overridePins and src attributes. Only raw certificate files are supported for the src attribute (user and system values will be ignored).</li></ul><p>Our final configuration file should look like this:</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;network-security-config&gt;<br>    &lt;domain-config&gt;<br>        &lt;domain includeSubdomains=&quot;true&quot;&gt;example.com&lt;/domain&gt;<br>        &lt;pin-set&gt;<br>            &lt;pin digest=&quot;SHA-256&quot;&gt;AAAAAAAAAAAAAAAAAAAAAAAAAA=&lt;/pin&gt;<br>        &lt;trustkit-config enforcePinning=&quot;true&quot;&gt;<br>            &lt;report-uri&gt;http://example.com/log_report&lt;/report-uri&gt;<br>        &lt;/trustkit-config&gt;<br>    &lt;/domain-config&gt;<br>&lt;/network-security-config&gt;</pre><p>There are some more options you could use according to your needs, so please go through the documentation and the <a href="https://github.com/datatheorem/TrustKit-Android">Github repository</a> to have a clearer idea on how to configure TrustKit. For example, you’ll find that it is strongly recommended to add more than one PIN for every domain you configure, so you don’t have any problems whenever your domain’s certificate or any of the certificates in the chain expires.</p><p>One more thing I had to went through was how to obtain the Public key digest to add it to the configuration! <a href="https://report-uri.com/home/pkp_hash">This great web tool</a> came in handy to obtain the hashes for all of the certificates in the chain. Just enter your site’s URL and it’ll have it done for you.</p><p>Now initialize TrustKit in the onCreate method of your main activity, or your main application. It’s up to you. Just make sure it’s not initialized more than once through the app’s life-cycle or you’ll get a killer exception. I recommend wrap it in a try-catch block:</p><pre>@Override<br>protected void onCreate(Bundle savedInstanceState) {    <br>    try {<br>        TrustKit.<em>initializeWithNetworkSecurityConfiguration</em>(this);<br>    } catch (Exception e) {<br>        e.printStackTrace();<br>    }<br>    OkHttpClientProvider.<em>setOkHttpClientFactory</em>(new CustomClientFactory());<br>    super.onCreate(savedInstanceState);<br>}</pre><p>Since this will work for Android N or later, we’ll set the corresponding configuration in AndroidManifest.xml inside our application tag:</p><pre>&lt;application<br>    android:name=&quot;your-app&quot;<br>    android:networkSecurityConfig=&quot;@xml/network_security_config&quot;<br>...</pre><p>This indicates us that the configuration file should be placed inside the <em>res/xml </em>directory. You can always change the file’s location and, if you do so, specify where you’ve put it in the manifest and when initializing TrustKit as we just did.</p><p>Finally, time to make this work with React Native. Remember how we added the CertificatePinner in the previous option? It’s quite the same here. TrustKit states that in order to work in devices with Android before N (&lt; API 24), we need to use it with TrustKit SSLSocketFactoryor X509TrustManager. So, based on previous option, just add this configuration to your <strong><em>custom client factory</em></strong> builder’s chain with the hostname (which obviously is within the domain you configured before) and you’re done:</p><pre>@Override<br>public OkHttpClient createNewNetworkModuleClient() {<br>        String hostname = null;<br>        try {<br>            hostname = new URL(<em>YOURHOSTNAME</em>).getHost();<br>        } catch (MalformedURLException e) {<br>            e.printStackTrace();<br>        }<br><br>        OkHttpClient.Builder client = new OkHttpClient.Builder()<br>                .connectTimeout(0, TimeUnit.<em>MILLISECONDS</em>)<br>                .readTimeout(0, TimeUnit.<em>MILLISECONDS</em>)<br>                .writeTimeout(0, TimeUnit.<em>MILLISECONDS</em>)<br>                .cookieJar(new ReactCookieJarContainer())<br>                .sslSocketFactory(TrustKit.<em>getInstance</em>().getSSLSocketFactory(hostname),TrustKit.<em>getInstance</em>().getTrustManager(hostname));<br>        return OkHttpClientProvider.<em>enableTls12OnPreLollipop</em>(client).build();<br>    }</pre><p>Almost forgot to mention this: TrutKit offers a reporting feature which I believe is very useful when it comes to add awareness to security break attempts to your app. Place your own service URL in report-uri and you’ll receive a report every time a pin validation occurs in the app. Also, TrusKit offers a dashboard for your app with all these reports which are automatically generated and sent to a default URL, just e-mail them and ask for the dashboard, or, disable the default reporting URL.</p><p>So this was Android implementation. I will write another story with how I approached this in iOS with all the tricky things I had to went through, and kinda suffer ’cause I’m not an iOS developer (Objective C or Swift).</p><p>Thanks for taking the time for reading this. If you have any idea or suggestion please leave a comment bellow, they will be very appreciated. Greetings and keep coding…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e317e6682642" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>