{"id":634,"date":"2016-05-17T10:42:26","date_gmt":"2016-05-17T08:42:26","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=634"},"modified":"2016-05-17T10:42:26","modified_gmt":"2016-05-17T08:42:26","slug":"certificate-pinning-with-okhttp","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2016\/05\/17\/certificate-pinning-with-okhttp\/","title":{"rendered":"Certificate pinning with OkHttp"},"content":{"rendered":"<p>A while back I wrote about &#8220;<a href=\"http:\/\/blog.ansuz.nl\/index.php\/2015\/07\/29\/using-ssl-right-on-android\/\" target=\"_blank\">Using SSL right on Android<\/a>&#8220;. If you are using <a href=\"https:\/\/github.com\/square\/okhttp\" target=\"_blank\">OkHttp<\/a> and\/or <a href=\"https:\/\/github.com\/square\/retrofit\" target=\"_blank\">Retrofit<\/a>, this should be quite simple to implement. You can use the <a href=\"https:\/\/square.github.io\/okhttp\/3.x\/okhttp\/okhttp3\/CertificatePinner.html\" target=\"_blank\">CertificatePinner<\/a> class when <a href=\"https:\/\/square.github.io\/okhttp\/3.x\/okhttp\/okhttp3\/OkHttpClient.Builder.html\" target=\"_blank\">building your OkHttpClient<\/a> instance.<\/p>\n<p>Example:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">java<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"java\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">CertificatePinner certificatePinner <span style=\"color: #339933\">=<\/span> <span style=\"color: #000000;font-weight: bold\">new<\/span> CertificatePinner.<span style=\"color: #006633\">Builder<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; .<span style=\"color: #006633\">add<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">&quot;example.com&quot;<\/span>, <span style=\"color: #0000ff\">&quot;your_pin&quot;<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; .<span style=\"color: #006633\">build<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">new<\/span> OkHttpClient.<span style=\"color: #006633\">Builder<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; .<span style=\"color: #006633\">certificatePinner<\/span><span style=\"color: #009900\">&#040;<\/span>certificatePinner<span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; .<span style=\"color: #006633\">build<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>More details about HTTPS and certificate pinning with OkHttp can be found on their <a href=\"https:\/\/github.com\/square\/okhttp\/wiki\/HTTPS#certificate-pinning\" target=\"_blank\">Wiki<\/a>, as well as a <a href=\"https:\/\/github.com\/square\/okhttp\/blob\/master\/samples\/guide\/src\/main\/java\/okhttp3\/recipes\/CertificatePinning.java\" target=\"_blank\">full example<\/a>.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>A while back I wrote about &#8220;Using SSL right on Android&#8220;. If you are using OkHttp and\/or Retrofit, this should be quite simple to implement. You can use the CertificatePinner class when building your OkHttpClient instance. Example: &lt; View plain &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2016\/05\/17\/certificate-pinning-with-okhttp\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[147,146,134],"class_list":["post-634","post","type-post","status-publish","format-standard","hentry","category-android","tag-okhttp","tag-retrofit","tag-ssl"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=634"}],"version-history":[{"count":4,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/634\/revisions"}],"predecessor-version":[{"id":638,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/634\/revisions\/638"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}