Skip to content
Advertisement

Android app client Mutual TLS with java server

I’m trying to send https requests to my server using mutual TLS. The server I got working successfully with TLS. But I can’t figure out how to do this on the client-side (Android app). I use spring on the java server. Requests from android app are made using HttpsUrlConnection().

I managed to be able to call HttpsUrlConnection() this how my code looks:

JavaScript

My server is configured to use TLSv1.2 protocol. Running test() throws this error:

JavaScript

Why do I see SSLV3 in the stacktrace? Is it not using TLSv1.2? Wireshark shows this https://ibb.co/27mpG4r

This code (from @Hakan54) makes the SSLContext:

JavaScript

Advertisement

Answer

What you are looking for is mutual authentication based on certificates. Both the server and the client needs to trust each other to communicate. And if the server just trust that specific client only it shouldn’t be possible for any other client to do a request.

The above example looks okay, but it will be easier to configure with the example below:

JavaScript

Here you need to provide the location of the keystore and truststore, and also the passwords. The public class will provide you the ssl context which you can load into your http client.

Make sure you have a client keystore with private and public key, and a truststore where you have the public key of the server. And make sure that the server has the public key of the client in its truststore. You also need to provide your server an additional properties in the application.yml file which enforces the server to validate the client. The property is: client-auth: need

See here a full example of setting up mutual authentication for server and client including example project spring-boot-mutual-tls-sll

Update 2022

I have made the above snippet and other utilities available in a library to make it easier and less verbose to setup ssl configuration. Next to that it also contains some validations. See here for the library GitHub – SSLContext Kickstart

The example which I provided at the first place can be replaced with:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement