{"id":340,"date":"2016-06-07T08:25:34","date_gmt":"2016-06-07T13:25:34","guid":{"rendered":"http:\/\/jebware.com\/blog\/?p=340"},"modified":"2020-06-05T11:12:39","modified_gmt":"2020-06-05T16:12:39","slug":"self-signed-certificates-with-okhttp-the-right-way","status":"publish","type":"post","link":"https:\/\/jebware.com\/blog\/?p=340","title":{"rendered":"Self-Signed Certificates with OkHttp &#8211; the Right Way"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Your sysadmin comes to you and says \u201chey, let\u2019s quit using the <a href=\"http:\/\/thedailywtf.com\/articles\/The_Developmestuction_Environment\" target=\"_blank\" rel=\"noopener noreferrer\">developmestruction environment<\/a>, I set up this new test server we can test with instead.\u201d Awesome. &nbsp;This is definitely a good thing for your development.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So you switch the URL your app is calling, but now you&#8217;re getting see this:<\/span><\/p>\n<p><a href=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-05-at-7.02.02-PM.png\" rel=\"attachment wp-att-341\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-341\" src=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-05-at-7.02.02-PM.png\" alt=\"Screen Shot 2016-06-05 at 7.02.02 PM\" width=\"785\" height=\"540\" srcset=\"https:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-05-at-7.02.02-PM.png 785w, https:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-05-at-7.02.02-PM-300x206.png 300w, https:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-05-at-7.02.02-PM-768x528.png 768w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Why can&#8217;t you connect? &nbsp;Well,&nbsp;SSL certs cost time and money, so a lot of times in internal test and development environments you&#8217;ll see <a href=\"https:\/\/en.wikipedia.org\/wiki\/Self-signed_certificate\" target=\"_blank\" rel=\"noopener noreferrer\">self-signed certificates<\/a>. &nbsp;By default, OkHttp isn&#8217;t going to trust those, since they aren&#8217;t signed by a known, trusted Certificate Authority (CA).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">At this point, you may find some StackOverflow answers suggesting that you make a dummy TrustManager that just blindly accepts any SSL certificate. &nbsp;<\/span><b>Don\u2019t do that.<\/b><span style=\"font-weight: 400;\"> &nbsp;You may as well disable SSL at that point, because anybody can run a man-in-the-middle attack to read and\/or manipulate your traffic. &nbsp;Seriously, just don&#8217;t. &nbsp;Even for your test environment. &nbsp;<\/span><\/p>\n<p>The good news is, it&#8217;s just as easy to fix the&nbsp;<em>right<\/em> way by adding trust for your self-signed certificate. &nbsp;Here&#8217;s all it takes:<\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 1:<\/strong> Download the .cer file<\/span><\/p>\n<p><a href=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1-chrome-broken-lock.png\" rel=\"attachment wp-att-342\"><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-342\" src=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1-chrome-broken-lock-288x300.png\" alt=\"1-chrome-broken-lock\" width=\"288\" height=\"300\" srcset=\"https:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1-chrome-broken-lock-288x300.png 288w, https:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1-chrome-broken-lock-768x801.png 768w, https:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1-chrome-broken-lock-982x1024.png 982w, https:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1-chrome-broken-lock.png 1154w\" sizes=\"(max-width: 288px) 85vw, 288px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Open the URL in Chrome. &nbsp;You\u2019ll see the broken lock icon in the address bar; click it. Your goal is to drag the certificate to somplace you can work with it; Chrome will give you a .cer file. &nbsp;(screenshots are small, click to embiggen)<\/span><\/p>\n<p><a href=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1b-chrome-not-private.png\" rel=\"attachment wp-att-343\"><img loading=\"lazy\" class=\"wp-image-343 size-thumbnail aligncenter\" src=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1b-chrome-not-private-150x150.png\" alt=\"1b-chrome-not-private\" width=\"150\" height=\"150\"><\/a><a href=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1c-connection-detail.png\" rel=\"attachment wp-att-344\"><img loading=\"lazy\" class=\"wp-image-344 size-thumbnail aligncenter\" src=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1c-connection-detail-150x150.png\" alt=\"1c-connection-detail\" width=\"150\" height=\"150\"><\/a><\/p>\n<p><a href=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1d-cert.png\" rel=\"attachment wp-att-345\"><img loading=\"lazy\" class=\"wp-image-345 size-thumbnail aligncenter\" src=\"http:\/\/jebware.com\/blog\/wp-content\/uploads\/2016\/06\/1d-cert-150x150.png\" alt=\"1d-cert\" width=\"150\" height=\"150\"><\/a><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 2:<\/strong> convert to .pem, using this in your terminal (and maybe spend a minute with &#8220;man openssl&#8221; to see what&#8217;s up here &#8211; we&#8217;re converting from one certificate file format to another)<\/span><\/p>\n<blockquote>\n<pre><span style=\"font-weight: 400;\">openssl x509 -in server.cer -inform DER -out server.pem -outform PEM<\/span><\/pre>\n<\/blockquote>\n<p><span style=\"font-weight: 400;\"><strong>Step 3:<\/strong> Drop the .pem in your app&#8217;s assets folder<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 4a:<\/strong>&nbsp;Here&#8217;s how to add that custom cert to OkHttp3<\/span><\/p>\n<p><script src=\"https:\/\/gist.github.com\/jebware\/dc60fc90ee20fbb28ff7666d7f14c43c.js\"><\/script><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 4b:<\/strong> OkHttp2 version<\/span><\/p>\n<p><script src=\"https:\/\/gist.github.com\/jebware\/21ff20c36ca97a42fd70a0a0820b223c.js\"><\/script><\/p>\n<p><span style=\"font-weight: 400;\">Run it again, and you\u2019re good to go.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your sysadmin comes to you and says \u201chey, let\u2019s quit using the developmestruction environment, I set up this new test server we can test with instead.\u201d Awesome. &nbsp;This is definitely a good thing for your development. So you switch the URL your app is calling, but now you&#8217;re getting see this: Why can&#8217;t you connect? &hellip; <a href=\"https:\/\/jebware.com\/blog\/?p=340\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Self-Signed Certificates with OkHttp &#8211; the Right Way&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"twitterCardType":"","cardImageID":0,"cardImage":"","cardTitle":"","cardDesc":"","cardImageAlt":"","cardPlayer":"","cardPlayerWidth":0,"cardPlayerHeight":0,"cardPlayerStream":"","cardPlayerCodec":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/340"}],"collection":[{"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=340"}],"version-history":[{"count":7,"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/340\/revisions"}],"predecessor-version":[{"id":629,"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/340\/revisions\/629"}],"wp:attachment":[{"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jebware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}