I am undergoing a task to get the ssl expiration date for a domain website and one that is accessed via IP. I have written below code. When I give domain name it gives me correct ouput but when I give Ip address in URl i.e. 192.168.1.1 it throws javax.net.ssl.SSLException: Received fatal alert: protocol_version error. Can you let me know what I am doing wrong here
public static void main(String[] args) throws Exception {
URL url = new URL("https://google.co.in");
System.out.println("Expiration Date: "+getCertificateExpiration(url));
}
/**
* Returns the expiration (notAfter) date of the X.509 Certificate
* used to encrypt the HTTPS connection of the given URL. If the
* connection is not a secure HTTPS connection or the server is not
* signed with an X.509 certificate, this method returns null. You
* may want to modify the implementation to throw relevant
* exceptions if you need to handle those conditions separately.
* @param url the URL to connect to
* @return the expiration (notAfter) date of the server's X.509 Certificate or
* null if unable to connect, the connection is not secure, or the server is
* not signed with an X.509 certificate.
*/
public static Date getCertificateExpiration(URL url) {
try {
URLConnection conn = url.openConnection();
conn.connect();
if (conn instanceof HttpsURLConnection) {
/*retrieve the N-length signing chain for the server certificates.
certs[0] is the server's certificate.
certs[1] - certs[N-1] are the intermediate authorities that signed the cert.
certs[N] is the root certificate authority of the chain. */
Certificate[] certs = ((HttpsURLConnection)conn).getServerCertificates();
if (certs.length > 0 && certs[0] instanceof X509Certificate) {
// certs[0] is an X.509 certificate, return its "notAfter" date
return ((X509Certificate)certs[0]).getNotAfter();
}
}
// connection is not HTTPS or server is not signed with an X.509 certificate, return null
return null;
} catch (SSLPeerUnverifiedException spue) {
// connection to server is not verified, unable to get certificates
return null;
} catch (IllegalStateException ise) {
// shouldn't get here -- indicates attempt to get certificates before
// connection is established
return null;
} catch (IOException ioe) {
// error connecting to URL -- this must be caught last since
// other exceptions are subclasses of IOException
return null;
}
}
Error Stacktrace is as follows
javax.net.ssl.SSLException: Received fatal alert: protocol_version
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
at com.test.SSLTest.getCertificateExpiration(SSLTest.java:39)
at com.test.SSLTest.main(SSLTest.java:21)