Skip to content

Instantly share code, notes, and snippets.

@SvetlinZarev
Created April 25, 2018 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SvetlinZarev/8bd88885e1f93afaf2f379ba46a45da5 to your computer and use it in GitHub Desktop.
Save SvetlinZarev/8bd88885e1f93afaf2f379ba46a45da5 to your computer and use it in GitHub Desktop.
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public final class TLSChecker {
public static void main(String[] args) throws Exception {
final TracingSocketFactory socketFactory = new TracingSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);
final URL address = new URL(args[0]);
final HttpsURLConnection connection = (HttpsURLConnection) address.openConnection();
connection.connect();
final Collection<Socket> createdSockets = socketFactory.getCreatedSockets();
for (Socket socket : createdSockets) {
if (socket instanceof SSLSocket) {
final SSLSocket sslSocket = (SSLSocket) socket;
System.out.println("SSL Socket: " + sslSocket);
System.out.println("Selected Protocol: " + sslSocket.getSession().getProtocol());
System.out.println("Selected CipherSuite: " + sslSocket.getSession().getCipherSuite());
}
}
}
}
class TracingSocketFactory extends SSLSocketFactory {
private final SSLSocketFactory delegate;
private final Collection<Socket> createdSockets;
public TracingSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
delegate = (SSLSocketFactory) SSLSocketFactory.getDefault();
// SSLContext sslContext = SSLContext.getInstance("TLSv1");
// sslContext.init(null, null, null);
// delegate = sslContext.getSocketFactory();
createdSockets = new ArrayList<>();
System.out.println("Default enabled protocols: " + Arrays.toString(SSLContext.getDefault().getDefaultSSLParameters().getProtocols()));
}
@Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
final Socket socket = delegate.createSocket(s, host, port, autoClose);
createdSockets.add(socket);
return socket;
}
@Override
public Socket createSocket() throws IOException {
final Socket socket = delegate.createSocket();
createdSockets.add(socket);
return socket;
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
final Socket socket = delegate.createSocket(host, port);
createdSockets.add(socket);
return socket;
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
final Socket socket = delegate.createSocket(host, port, localHost, localPort);
createdSockets.add(socket);
return socket;
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
final Socket socket = delegate.createSocket(host, port);
createdSockets.add(socket);
return socket;
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
final Socket socket = delegate.createSocket(address, port, localAddress, localPort);
createdSockets.add(socket);
return socket;
}
public Collection<Socket> getCreatedSockets() {
return createdSockets;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment