package net.cogindo.ssl; | |
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; | |
/** | |
* @author fkrauthan | |
*/ | |
public class TLSSocketFactory extends SSLSocketFactory { | |
private SSLSocketFactory internalSSLSocketFactory; | |
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException { | |
SSLContext context = SSLContext.getInstance("TLS"); | |
context.init(null, null, null); | |
internalSSLSocketFactory = context.getSocketFactory(); | |
} | |
@Override | |
public String[] getDefaultCipherSuites() { | |
return internalSSLSocketFactory.getDefaultCipherSuites(); | |
} | |
@Override | |
public String[] getSupportedCipherSuites() { | |
return internalSSLSocketFactory.getSupportedCipherSuites(); | |
} | |
@Override | |
public Socket createSocket() throws IOException { | |
return enableTLSOnSocket(internalSSLSocketFactory.createSocket()); | |
} | |
@Override | |
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { | |
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose)); | |
} | |
@Override | |
public Socket createSocket(String host, int port) throws IOException, UnknownHostException { | |
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port)); | |
} | |
@Override | |
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { | |
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort)); | |
} | |
@Override | |
public Socket createSocket(InetAddress host, int port) throws IOException { | |
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port)); | |
} | |
@Override | |
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { | |
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort)); | |
} | |
private Socket enableTLSOnSocket(Socket socket) { | |
if(socket != null && (socket instanceof SSLSocket)) { | |
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"}); | |
} | |
return socket; | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Prosanto
commented
May 13, 2016
Hi @fkrauthan, I face this issues(android enable tls).But i can't solve this .I am try to implement your example.But can't understand how i will add this code `public class SSLSocketFactoryEx extends SSLSocketFactory {
} |
This comment has been minimized.
This comment has been minimized.
mtcstle
commented
Jun 6, 2016
Prosanto, have you made any progress here. You're starting out way ahead of me. Can you send me or point me to any directions on how to add code to a phone. Can this sort of testing be done on a emulator? We're trying to restrict our web server to TLS v1.2 or better and it's breaking the mail client on Android 4 phones. |
This comment has been minimized.
This comment has been minimized.
seyoung-hyun
commented
Sep 9, 2016
Hi @fkrauthan, |
This comment has been minimized.
This comment has been minimized.
cantek41
commented
Apr 2, 2017
SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); |
This comment has been minimized.
This comment has been minimized.
SahilKashyap
commented
Apr 21, 2017
•
thanks, this code works for me but now Google Play Store rejected my application due to Malicious Behavior. Before adding this code my application uploaded successfully on Play Store. So, Please provide another solution. |
This comment has been minimized.
This comment has been minimized.
ishantsagar
commented
May 26, 2017
@SahilKashyap did you get another way of doing it, since your app was rejected by play store? I need to enable TLS 1.2 support for my apps having API 16+ |
This comment has been minimized.
This comment has been minimized.
tamilanmanikandan
commented
Jul 11, 2017
How to implement these SSLSocketFactory to webview in android? |
This comment has been minimized.
This comment has been minimized.
darkrider1287
commented
Jun 20, 2018
•
You maniac, this code solves my issue EXACTLY. Thank you!!! |
This comment has been minimized.
This comment has been minimized.
batica81
commented
Aug 4, 2018
Works on API 19! You are a genius! |
This comment has been minimized.
This comment has been minimized.
Kishanjvaghela
commented
Sep 30, 2018
for retrofit.. please check this https://gist.github.com/Kishanjvaghela/3eb249e6bd52ba6b2b858af674fc7c3d |
This comment has been minimized.
fkrauthan commentedAug 17, 2015
See the blog post to this code: http://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/