Solved

Getting SSL Certificate expiry date

Posted on 2012-04-10
1
669 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now