4

I have a JEditorPane which loads a website over SSL/TLS. My goal is to get (with reflection) specific information of the SSL connection like the finished message of the SSL handshake. The only thing I get from JEditorPane is the URL and thus the HttpsURLConnection.

But how do I get the SSLSocket of the HttpsURLConnection? Any ideas?

Christian K
  • 41
  • 1
  • 2

1 Answers1

2

There is no direct way to get it. But you can control the SSL socket used by the HttpsURLConnection. You can set the socket factory in the httppsURLConnection.setSSLSocketFactory().

You can create a custom socket factory which creates your owns decorated sslsocket and return it. This will be the socket used by your HttpsURLConnection.

Note that u need to do this before the connection is established.

SSLSocketFactory sslSktFactory = SSLContext.getInstance("TLS").getSocketFactory();

httpsUrlConnection.setSSLSocketFactory(new CustomSSLSocketFactory(sslSktFactory ));

A sample custom SSL socket factory is below

class CustomSSLSocketFactory extends SSLSocketFactory {
    SSLSocketFactory factory = null;
    CustomSSLSocketFactory(SSLSocketFactory factory) {
        this.factory = factory;
    }

    @Override
    public Socket createSocket(Socket s, String host, int port,
            boolean autoClose) throws IOException {
        Socket skt = factory.createSocket(s, host, port, autoClose);
        return customizeSSLSocket(skt);
    }

    @Override
    public String[] getDefaultCipherSuites() {
        return factory.getDefaultCipherSuites();
    }

    @Override
    public String[] getSupportedCipherSuites() {
        return factory.getSupportedCipherSuites();
    }

    @Override
    public Socket createSocket(String host, int port) throws IOException,
            UnknownHostException {
        Socket skt = factory.createSocket(host, port);
        return customizeSSLSocket(skt);
    }

    @Override
    public Socket createSocket(InetAddress host, int port) throws IOException {
        Socket skt = factory.createSocket(host, port);
        return customizeSSLSocket(skt);
    }

    @Override
    public Socket createSocket(String host, int port, InetAddress localHost,
            int localPort) throws IOException, UnknownHostException {
        Socket skt = factory.createSocket(host, port, localHost, localPort);
        return customizeSSLSocket(skt); 
    }

    @Override
    public Socket createSocket(InetAddress address, int port,
            InetAddress localAddress, int localPort) throws IOException {
        Socket skt = factory.createSocket(address, port, localAddress, localPort);
        return customizeSSLSocket(skt); 
    }

    private Socket customizeSSLSocket(Socket skt) throws SocketException {
        ((SSLSocket)skt).addHandshakeCompletedListener(
                new HandshakeCompletedListener() {
                    public void handshakeCompleted(
                            HandshakeCompletedEvent event) {
                        System.out.println("Handshake finished!");
                        System.out.println(
                                "\t CipherSuite:" + event.getCipherSuite());
                        System.out.println(
                                "\t SessionId " + event.getSession());
                        System.out.println(
                                "\t PeerHost " + event.getSession().getPeerHost());
                        System.out.println(
                                "\t PeerHost " + event.getSession().getProtocol());

                    }
                }
                );      
        return skt;
    }
Rajesh Jose
  • 314
  • 2
  • 12