Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does OkHttp support accepting self-signed SSL certs?

I'm working for a customer who has a server with self-signed SSL cert.

I'm using Retrofit + CustomClient using wrapped OkHttp client:

RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Config.BASE_URL + Config.API_VERSION)     .setClient(new CustomClient(new OkClient(), context))     .build(); 

Does OkHttp support calling Self-Signed SSL cert server by default?

By the way. Which client is using Retrofit by default? I thought it was OkHttp but when I researched a bit more I realized I needed to import OkHttp dependencies

like image 424
cesards Avatar asked Apr 16 '14 07:04

cesards


People also ask

Can you use self-signed certificate with SSL?

When using the SSL for non-production applications or other experiments you can use a self-signed SSL certificate. Though the certificate implements full encryption, visitors to your site will see a browser warning indicating that the certificate should not be trusted.

Can I use a self-signed certificate for my website?

If you want to secure your website with an SSL/TLS certificate, you can use a free self-signed SSL/TLS certificate.

How do I get a browser to accept a self-signed certificate?

Go to your Settings in Chrome. Usually, this is done by clicking the 3 dots in the upper-right of the window, and select Settings. Scroll all the way down, click to view "Advanced", then select the Manage HTTPS/SSL Certificates link. You will see a window open like this: Click the Import button.


1 Answers

Yes, It does.

Retrofit allows you to set your custom HTTP client, that is configured to your needs.

As for self-signed SSL certs there is a discussion here. The link contains code samples to add self-signed SSL to Android's DefaultHttpClient and to load this client to Retrofit.

If you need OkHttpClient to accept self signed SSL, you need to pass it custom javax.net.ssl.SSLSocketFactory instance via setSslSocketFactory(SSLSocketFactory sslSocketFactory) method.

The easiest method to get a socket factory is to get one from javax.net.ssl.SSLContext as discussed here.

Here is a sample for configuring OkHttpClient:

OkHttpClient client = new OkHttpClient(); KeyStore keyStore = readKeyStore(); //your method to obtain KeyStore SSLContext sslContext = SSLContext.getInstance("SSL"); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, "keystore_pass".toCharArray()); sslContext.init(keyManagerFactory.getKeyManagers(),trustManagerFactory.getTrustManagers(), new SecureRandom()); client.setSslSocketFactory(sslContext.getSocketFactory()); 

Updated code for okhttp3 (using builder):

    OkHttpClient client = new OkHttpClient.Builder()             .sslSocketFactory(sslContext.getSocketFactory())             .build(); 

the client here is now configured to use certificates from your KeyStore. However it will only trust the certificates in your KeyStore and will not trust anything else, even if your system trust them by default. (If you have only self signed certs in your KeyStore and try to connect to Google main page via HTTPS you will get SSLHandshakeException).

You can obtain KeyStore instance from file as seen in docs:

KeyStore readKeyStore() {     KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());      // get user password and file input stream     char[] password = getPassword();      java.io.FileInputStream fis = null;     try {         fis = new java.io.FileInputStream("keyStoreName");         ks.load(fis, password);     } finally {         if (fis != null) {             fis.close();         }     }     return ks; } 

If you are on android you can put it in res/raw folder and get it from a Context instance using

fis = context.getResources().openRawResource(R.raw.your_keystore_filename); 

There are several discussions on how to create your keystore. For example here

like image 60
Andrey Makarov Avatar answered Oct 03 '22 05:10

Andrey Makarov