This class extends Sockets and provides secure socket using protocols such as the “Secure Sockets Layer” (SSL) or IETF “Transport Layer Security” (TLS) protocols.
TLS 1.1 and 1.2 are supported from API 16, but not enabled by default until API 20.
The first thing we realized was that despite documentation suggesting otherwise, not all devices on Android 4.1+ actually support TLS 1.2. Even though it is likely due to device manufacturers not fully following the official Android specs, we had to do what we could to ensure this would work for our users.
Luckily, Google Play Services provides a way to do this. The solution is to use ProviderInstaller from Google Play Services to try to update the device to support the latest and greatest security protocols.
1 2 3 4 5 6 7 8 9 10 11
fun Context.installTls12() { try { ProviderInstaller.installIfNeeded(this) } catch (e: GooglePlayServicesRepairableException) { // Prompt the user to install/update/enable Google Play services. GoogleApiAvailability.getInstance() .showErrorNotification(this, e.connectionStatusCode) } catch (e: GooglePlayServicesNotAvailableException) { // Indicates a non-recoverable error: let the user know. } }
Does not seem to work, as the root problem was that TLS was not enabled
Try normal HttpsUrlConnection
If we use any networking library and suspect it is the cause, then try using normal HttpsUrlConnection to check.
The Android documentation for SSLSocket says that TLS 1.1 and TLS 1.2 is supported within android starting API level 16+ (Android 4.1, Jelly Bean). But it is by default disabled but starting with API level 20+ (Android 4.4 for watch, Kitkat Watch and Android 5.0 for phone, Lollipop) they are enabled. But it is very hard to find any documentation about how to enable it for phones running 4.1 for example.
The first thing you need to do is to make sure that your minimum required API level is 16 to have the following code working in your project.
To enable TLS 1.1 and 1.2 you need to create a custom SSLSocketFactory that is going to proxy all calls to a default SSLSocketFactory implementation. In addition to that do we have to override all createSocket methods and callsetEnabledProtocols on the returned SSLSocket to enable TLS 1.1 and TLS 1.2. For an example implementation just follow the link below.