How to solve SSLHandshakeException in Android : SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version ?

Navneet Krishna
3 min readOct 29, 2018

--

Being android developers, we might have at least one time come across this issue >>

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb8df4b50: Failure in SSL library, usually a protocol error
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version (external/openssl/ssl/s23_clnt.c:741 0x8d92d990:0x00000000)

or sometimes a similar variant of the above issue. What this actually means is that >>

“The server you are talking to is supporting only a specific protocol version which your client(your android app) is unaware of. That is, your client does not support that particular version”

The issue is mostly encountered in devices below lollipop i.e, kitkat and below devices. Let us see why the issue occurs. Example Case :

Try getting some response from the host https://api.github.com/ like shown below

Create an interface class for the endpoints like below

pass your github username in the interface as shown so that you could fetch your repos

You could make the following observations while running this :

  1. This code would run successfully without any issues from android api levels 20+(from lollipop onwards) and will fetch us a successful response with list of repos.
  2. This will not return any successfull response in the case of android devices below 20(kitkat(19) and below). The onFailure method will be executed with an error like this

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb8df4b50: Failure in SSL library, usually a protocol errorerror:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version (external/openssl/ssl/s23_clnt.c:741 0x8d92d990:0x00000000)

The reason why this is working only in devices with api levels 20+ is that the host https://api.github.com/ supports only TLS 1.2 which is by default disabled in android devices below 20.

You can see the list of protocols supported by your web server by going here https://www.ssllabs.com/ssltest/ and typing in https://api.github.com/ inside hostname field.

Now hit submit. See the test results, in the section protocols inside configuration, you can see that TLS 1.2 is the only supported protocol.

But the protocol TLS 1.2 is supported in android from api level 16. So we just have to enable it for devices with api levels from 16 to 20.

So you can use the following TLSSocketFactory to enable TLS 1.2 for devices below 20

Using this TLSSocketFactory, the protocol will never fallback to ssl2 or ssl3 as we have enabled only TLS 1.2

Now apply this to your retrofit call using an OkHttpClient()

Thats it! you have now successfully enabled TLS 1.2 on devices from 16 to 20

You may also check similar issue in StackOverflow

check this post on StackOverflow

Subscribe to the below channel for android tutorials :

Originally published at www.freshbytelabs.com on October 29, 2018.

--

--