Skip to content

Instantly share code, notes, and snippets.

@smourachov
Created July 4, 2013 23:30
Show Gist options
  • Save smourachov/5930820 to your computer and use it in GitHub Desktop.
Save smourachov/5930820 to your computer and use it in GitHub Desktop.
force using TLSv1.2
public static SocketFactory getSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
return new SocketFactory() {
Socket getSoscket() {
try {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, sTrustManagers, null);
SSLSocketFactory f = context.getSocketFactory();
Socket res = f.createSocket();
Set<String> protocols = new HashSet<String>(Arrays.asList(context.getSupportedSSLParameters().getProtocols()));
Log.i("WebSocketClient", "protocols=" + protocols);
if (protocols.contains("TLSv1.2")) {
((SSLSocket) res).setEnabledProtocols(new String[]{"TLSv1.2"});
} else if (protocols.contains("TLSv1.1")) {
((SSLSocket) res).setEnabledProtocols(new String[]{"TLSv1.1"});
}
return res;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public Socket createSocket(String s, int i) throws IOException, UnknownHostException {
Socket res = getSoscket();
res.connect(new InetSocketAddress(s, i));
return res;
}
@Override
public Socket createSocket(String s, int i, InetAddress inetAddress, int i2) throws IOException, UnknownHostException {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Socket createSocket(InetAddress inetAddress, int i) throws IOException {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress2, int i2) throws IOException {
throw new UnsupportedOperationException("Not implemented");
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment