Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created October 22, 2018 14:08
Show Gist options
  • Save Audhil/2d82aed05e1ad4688df57b1ceb47d74c to your computer and use it in GitHub Desktop.
Save Audhil/2d82aed05e1ad4688df57b1ceb47d74c to your computer and use it in GitHub Desktop.
TLS v1.2 support in all Pre-lollipop devices
// TLS v1.2 support in pre-lollipop devices (if GooglePlayServices available in the device)
// only this lines can be added in App's application class file - this is enough
try {
ProviderInstaller.installIfNeeded(applicationContext)
val sslContext: SSLContext = SSLContext.getInstance("TLSv1.2")
sslContext.init(null, null, null)
sslContext.createSSLEngine()
} catch (e: GooglePlayServicesRepairableException) {
// Prompt the user to install/update/enable Google Play services.
GoogleApiAvailability.getInstance().showErrorNotification(this, e.connectionStatusCode)
} catch (e: GooglePlayServicesNotAvailableException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: KeyManagementException) {
e.printStackTrace()
}
// file 2
import java.io.IOException
import java.net.InetAddress
import java.net.Socket
import java.net.UnknownHostException
import java.security.KeyManagementException
import java.security.NoSuchAlgorithmException
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory
class CustomSSLFactory
@Throws(KeyManagementException::class, NoSuchAlgorithmException::class)
constructor() : SSLSocketFactory() {
private val internalSSLSocketFactory: SSLSocketFactory
init {
val context = SSLContext.getInstance("TLSv1.2")
context.init(null, null, null)
internalSSLSocketFactory = context.socketFactory
}
override fun getDefaultCipherSuites(): Array<String> {
return internalSSLSocketFactory.defaultCipherSuites
}
override fun getSupportedCipherSuites(): Array<String> {
return internalSSLSocketFactory.supportedCipherSuites
}
@Throws(IOException::class)
override fun createSocket(): Socket? {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket())
}
@Throws(IOException::class)
override fun createSocket(s: Socket, host: String, port: Int, autoClose: Boolean): Socket? {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose))
}
@Throws(IOException::class, UnknownHostException::class)
override fun createSocket(host: String, port: Int): Socket? {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port))
}
@Throws(IOException::class, UnknownHostException::class)
override fun createSocket(host: String, port: Int, localHost: InetAddress, localPort: Int): Socket? {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort))
}
@Throws(IOException::class)
override fun createSocket(host: InetAddress, port: Int): Socket? {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port))
}
@Throws(IOException::class)
override fun createSocket(address: InetAddress, port: Int, localAddress: InetAddress, localPort: Int): Socket? {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort))
}
private fun enableTLSOnSocket(socket: Socket?): Socket? {
if (socket != null && socket is SSLSocket) {
socket.enabledProtocols = arrayOf("TLSv1.2")
}
return socket
}
}
// TLS v1.2 support in pre-lollipop devices (if GooglePlayServices NOT available in the device)
// if we want to support China devices we need to add this content & file 2 in our project
// file 1
// adding sslSocketFactory in OkHttpClient
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
val trustManagerFactory: TrustManagerFactory?
var trustManager: X509TrustManager? = null
try {
trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
if (trustManagerFactory != null) {
trustManagerFactory.init(null as KeyStore?)
val trustManagers = trustManagerFactory.trustManagers
if (trustManagers.size != 1 || trustManagers[0] !is X509TrustManager) {
throw IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers))
}
trustManager = trustManagers[0] as X509TrustManager
}
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: KeyStoreException) {
e.printStackTrace()
}
trustManager?.let {
httpClient.sslSocketFactory(CustomSSLFactory(), it)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment