🤔Adding SSL Certificates into your Android App with Retrofit

Kevin Gamboa
Yellowme
Published in
6 min readApr 16, 2020

--

When this task was assigned to me in the project I’m working, I thought: this will be easy and it was 🙂, but it wasn’t 🙃, because I didn’t find a lot of posts on how to add my certificate.

You can always read the Official Android SSL Documentation (you should), but the implementation wasn’t clear enough to me, so I decided to create this post to simplify it for anyone who is going through the same problem 👍.

Photo by Pathum Danthanarayana on Unsplash

🏁 Intro

In this post I’m going to explain how to add certificates to our Android app when we have a .pfx file, not only the .crt , and of course, it will include a brief explanation of what those files are.

So after reading this tutorial we’re going to be familiarized with:

  • 🤔 What the hell are the .pfx and .crt files.
  • 😎 How to add certificates to my app.

This tutorial is going to be based on a project using Retrofit to make calls to an API, but you can always adapt it for your specific case.

📄 Let’s talk about the certificates

📇 CAs

A certificate authority (CA) is a company or organization that acts to validate the identities of entities (such as websites, email addresses, companies, or individual persons) and bind them to cryptographic keys through the issuance of electronic documents known as digital certificates.

So basically the CAs give us a way to authenticate us by serving as credentials to validate our identity, encrypt our data for secure communication over the insecure networks such as the internet, and give us a way to be sure nothing has been altered by a third party in transit because of the signature of the certificate.

Typically, an applicant for a digital certificate will generate a key pair consisting of a private key and a public key, along with a certificate signing request (CSR). A CSR is an encoded text file that includes the public key and other information that will be included in the certificate (e.g. domain name, organization, email address, etc.). Key pair and CSR generation are usually done on the server or workstation.

The .crt and the .pfx files are CSR encoded.

🥺 What is the difference?

CER (or .CRT) files: CER file is used to store X.509 certificate. Normally used for SSL certification to verify and identify web servers security. The file contains information about certificate owner and public key.

PFX files: Personal Exchange Format, is a PKCS12 file. This contains a variety of cryptographic information, such as certificates, root authority certificates, certificate chains and private keys. It’s cryptographically protected with passwords to keep private keys private and preserve the integrity of the root certificates.

Basically, the PFX contains more information than the CRT.

😬 Give me the implementation!

Okay, if you already know something about the CAs you probably just want to know how to implement them. So, let’s get into it.

If you have tried to add your certificate with a .crt file and Retrofit, you may have found this class in some post or documentation. I’m actually going to use code of that class for the implementation, but I’m going to break it down for easier reading, and to show the small changes we need to add when we’re using a .pfx file so you can use whichever you need.

The source code is gonna be at the end of the post.

🏃‍♂️ Let’s get started!

In order to add our certificates we’re going to create a method that generates an OkHttpClient. We only need to follow these simple steps:

  1. Add our file into our project as a raw resource.

2. Create a method that returns an OkHttpClient.

3. Create a KeyStore containing our trusted CAs.

4. Create a KeyManagerFactory.

5. Create an SSL Context that contains our trusted CAs.

6. Add our socket factory to our builder.

7. Use the generated OkHttpClient as usual.

  1. Add our file into our project as a raw resource (in the raw folder).

We can just drag and drop the file into the raw folder or go to our project directory and create it inside the app/src/main/res and then paste our file.

2. Create a method that returns an OkHttpClient .

This method is where we’re going to add our certificate to our OkHttpClient so we can use it to make calls to an API. In this example, I’m going to simplify, but you can always add custom settings.

3. Create a KeyStore containing our trusted CAs.

The KeyStore class is going to help us to store our certificates, but the type of instance is very important, that’s going to make the difference between using a .crt file or a .pfx.

.CRT → the default type is gonna work for you. KeyStore.getInstance(KeyStore.getDefaultType())

.PFX → you need to use PKCS12, this is a special format to place the certificate (includes its “intermediate”) with the private key.
KeyStore.getInstance("PKCS12")

The password is the one you need to use when you want to read your file. In this code I’m setting it as a String, but you should always keep it secure, so you better use a file with it or as a Build Config Field.

4. Create a KeyManagerFactory so we can have keyManagers with the algorithm of our certificate.

We’re getting an instance of an X509 Factory because that’s the standard of my public key certificate and it’s the most used one.

5. Create an SSL Context that contains our trusted CAs.

In this case we’re using an instance of a TLS Context because our server required so, and as TLS is basically a newer version of SSL we can use it as default.

6. Finally, add our socket factory to our builder.

Just use the socketFactory of the sslContext that we created in the last step and set it to the builder.

7. Use the generated OkHttpClient as usual.

Now that we added our trusted CAs to an OkHttpClient we can proceed to use it with a Retrofit instance as usual.

And just like that we’ll be able to make some calls to our API 🎉.

💻 The Code

🏆 Conclusion

Adding our trusted CAs in our Android app is not complicated and can be very useful when we have multiple flavors or build types with different API environments and some of those are secured with a CA.

Remember to remove your Development certificate when you’re building a Release version of your app; you don’t wanna give it away 😅. I also suggest deleting the file from Git for security reasons.

This post was made to try to simplify the explanation of the implementation that you probably are gonna find on the internet.

📚 Useful resources

Official Google Documentation

Retrofit example class

--

--