Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Certificate Information in Java

I am trying to get and read a certificate only in Java. What code or examples should I look at to get the certificates of a website.

For examples the websites:

  1. https://google.com
  2. https://www.ssllabs.com/

Do I use the URL class?

URL url = new URL("https://google.com");
like image 260
Chase W. Avatar asked Mar 17 '14 20:03

Chase W.


People also ask

How do I find certificate details?

Visit an SSL-enabled website and double click on the padlock in the address bar of the website. A window will pop up. Now, click on Show Certificate and then click on Details to view the certificate details.

How do I open a certificate of information?

For most browsers, look to see if a site URL begins with “https,” which indicates it has an SSL certificate. Then click on the padlock icon in the address bar to view the certificate information.


1 Answers

Ok I just found out how I can get the information that I want.

public void certInformation(String aURL) throws Exception{
        URL destinationURL = new URL(aURL);
        HttpsURLConnection conn = (HttpsURLConnection) destinationURL.openConnection();
        conn.connect();
        Certificate[] certs = conn.getServerCertificates();
        for (Certificate cert : certs) {
            System.out.println("Certificate is: " + cert);
            if(cert instanceof X509Certificate) {
                    X509Certificate x = (X509Certificate ) cert;
                    System.out.println(x.getIssuerDN());
            }
        }
    }
like image 132
Chase W. Avatar answered Sep 21 '22 02:09

Chase W.