Solved

Getting SSL Certificate expiry date

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 72
Increment alphanumeric sequence 6 80
strCopies  challenge 17 73
Java - Why doesn't this JFrame work 3 18
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

20 Experts available now in Live!

Get 1:1 Help Now