Solved

Getting SSL Certificate expiry date

Posted on 2012-04-10
1
751 Views
Last Modified: 2012-11-13
Hi

I will get a url as an input. I have to get the expiry date of the ssl certificate using that url. I searched various sites but i am not able to find using url to get the certificate. all are saying using host and port number

i tried using HttpsURLConnection API but it is refusing the connection

Thanks
Pavan
0
Comment
Question by:Gangadhara Melukote
1 Comment
 
LVL 10

Accepted Solution

by:
gordon_vt02 earned 500 total points
ID: 37832660
Once you get the URL, you should be able to call URL.openConnection(), returning you some subclass of URLConnection, or throwing an error if it cannot connect.  If the instance you get back is an HttpsURLConnection, you can retrieve the server certificate(s), verify that it is an X509Certificate and get the expiration date from that:

/**
 * 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 Date getCertificateExpiration(URL url) {
    try {
        URLConnection conn = url.openConnection();
        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;
    }
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question