Created
March 29, 2018 02:23
-
-
Save YusukeIwaki/a276cbfe99b3beba31f6af745033989f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.InetAddress | |
import java.net.Socket | |
import javax.net.ssl.SSLSocket | |
import javax.net.ssl.SSLSocketFactory | |
/** | |
* Enables TLS v1.2 when creating SSLSockets. | |
* | |
* For some reason, android supports TLS v1.2 from API 16, but enables it by | |
* default only from API 20. | |
* @link https://developer.android.com/reference/javax/net/ssl/SSLSocket.html | |
* @see SSLSocketFactory | |
*/ | |
class Tls12SocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { | |
companion object { | |
private val TLS_V12_ONLY = arrayOf("TLSv1.2") | |
} | |
private fun patch(sock: Socket): Socket { | |
if (sock is SSLSocket) { | |
sock.enabledProtocols = TLS_V12_ONLY | |
} | |
return sock | |
} | |
override fun getDefaultCipherSuites(): Array<out String> = delegate.defaultCipherSuites | |
override fun getSupportedCipherSuites(): Array<out String> = delegate.supportedCipherSuites | |
override fun createSocket(socket: Socket, host: String, port: Int, autoClose: Boolean) | |
= patch(delegate.createSocket(socket, host, port, autoClose)) | |
override fun createSocket(host: InetAddress, port: Int) | |
= patch(delegate.createSocket(host, port)) | |
override fun createSocket(address: InetAddress, port: Int, localAddress: InetAddress, localPort: Int) | |
= patch(delegate.createSocket(address, port, localAddress, localPort)) | |
override fun createSocket(host: String, port: Int) | |
= patch(delegate.createSocket(host, port)) | |
override fun createSocket(host: String, port: Int, localAddress: InetAddress, localPort: Int) | |
= patch(delegate.createSocket(host, port, localAddress, localPort)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment