0
votes

How do I get the Cipher used by an HTTPS Connection in Java? I need to find the TLS version used whether it's TLS 1.1 or 1.2 after hitting the URLusing the below code:

url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

Screenshot of the firefox browser is give below. Can any one help me in getting this? TLS Version

I have tried

SSLSocket socket = (SSLSocket) con.getSSLSocketFactory().createSocket();
String[] test = socket.getSSLParameters().getProtocols();

But this gives the list of protocols that are enabled for the connection. Where as I need the the protocol that was used in the response from the server.

The screenshot attached shows what I expecting to read(TLS 1.2)

3

3 Answers

0
votes

I'think con.getCipherSuite(); should do the trick for algorithm...

https://docs.oracle.com/javase/7/docs/api/javax/net/ssl/HttpsURLConnection.html

This could be an odd solution for tls but if you manage to open a new SSL socket(maybe then you can close with socket.close();) SSLSocket socket = (SSLSocket) con.getSSLSocketFactory().createSocket(); String[] test = socket.getSSLParameters().getProtocols(); In test value Notice that : You can get usable TLS versions not just only one.

0
votes
  1. Install a custom SSLSocketFactory on the HttpsURLConnection before you connect it, or do any I/O on connection.
  2. Arrange that your SSLSocketFactory can return the most recently created SSLSocket, or indeed all of them.
  3. After you've connected, or done any I/O on the connection, retrieve the SSLSocket concerned from the SSLSocketFactory, and get the current cipher from the current SSLSession. Note that associating SSLSockets with HttpsURLConnections is non-trivial, because of connection pooling and HTTP keep-alive.
-1
votes

You can print the protocol (TLSv1.2) and suite (TLS_ECDHE_RSA_WITH_AES) with following:

SSLSocket ss = (SSLSocket) SSLSocketFactory.getDefault().createSocket("host", 443);
System.out.println("protocol: "+ss.getSession().getProtocol());
System.out.println("suite:    "+ss.getSession().ss.getSession().getCipherSuite());