Created
June 18, 2015 08:37
-
-
Save yankeppey/877b6584bc51e9bd18dd to your computer and use it in GitHub Desktop.
TLS 1.2 on Android
This file contains 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.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; | |
import javax.net.ssl.TrustManager; | |
public class TLS12SocketFactory extends SSLSocketFactory { | |
private SSLContext mSSLContext; | |
private static final String TLS_1_2 = "TLSv1.2"; | |
public TLS12SocketFactory() throws NoSuchAlgorithmException, KeyManagementException { | |
mSSLContext = SSLContext.getInstance(TLS_1_2); | |
TrustManager[] trustManagers = new TrustManager[] { new TrustEverythingSSLTrustManager() }; | |
mSSLContext.init(null, trustManagers, null); | |
} | |
@Override | |
public String[] getDefaultCipherSuites() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Override | |
public String[] getSupportedCipherSuites() { | |
return null; | |
} | |
@Override | |
public Socket createSocket(String host, int port) throws IOException, | |
UnknownHostException { | |
SSLSocket s = (SSLSocket)mSSLContext.getSocketFactory().createSocket(host, port); | |
s.setEnabledProtocols(new String[] {TLS_1_2} ); | |
return s; | |
} | |
@Override | |
public Socket createSocket(InetAddress host, int port) throws IOException { | |
SSLSocket s = (SSLSocket)mSSLContext.getSocketFactory().createSocket(host, port); | |
s.setEnabledProtocols(new String[] {TLS_1_2} ); | |
return s; | |
} | |
@Override | |
public Socket createSocket(String host, int port, InetAddress localHost, | |
int localPort) throws IOException, UnknownHostException { | |
SSLSocket s = (SSLSocket)mSSLContext.getSocketFactory().createSocket(host, port, localHost, localPort); | |
s.setEnabledProtocols(new String[] {TLS_1_2} ); | |
return s; | |
} | |
@Override | |
public Socket createSocket(InetAddress address, int port, | |
InetAddress localAddress, int localPort) throws IOException { | |
SSLSocket s = (SSLSocket)mSSLContext.getSocketFactory().createSocket(address, port, localAddress, localPort); | |
s.setEnabledProtocols(new String[] {TLS_1_2} ); | |
return s; | |
} | |
@Override | |
public Socket createSocket(Socket socket, String host, int port, | |
boolean autoClose) throws IOException { | |
SSLSocket s = (SSLSocket)mSSLContext.getSocketFactory().createSocket(socket, host, port, autoClose); | |
s.setEnabledProtocols(new String[] {TLS_1_2} ); | |
return s; | |
} | |
} |
This file contains 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.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
import javax.net.ssl.X509TrustManager; | |
public class TrustEverythingSSLTrustManager implements X509TrustManager { | |
public void checkClientTrusted(X509Certificate[] chain, String authType) | |
throws CertificateException { | |
} | |
public void checkServerTrusted(X509Certificate[] chain, String authType) | |
throws CertificateException { | |
} | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment