How to get server certificate chain and then verify it is valid and trusted in Java


Mark H:

I need to create an Https connection to a remote server and then retrieve and verify the certificate.

I have established the connection:

try {  
    url = new URL(this.SERVER_URL);  
    HttpURLConnection con = (HttpURLConnection) url.openConnection();   
    HttpsURLConnection secured = (HttpsURLConnection) con;  
    secured.connect(); 
}  

But it seems the getServerCertificateChain()method is not defined by the type HttpsURLConnection.

So, how do I retrieve the server certificate chain? My understanding is that an array of objects getServerCertificateChain()should be returned , and the class has methods that can be used to ask for credentials.X509Certificate

I need to verify:

  1. the certificate is valid and trusted,
  2. Check the "Certificate Revocation List Distribution Point" against the certificate serial number
  3. make sure it's not expired,
  4. Check if the URL in the certificate matches another URL (which I have retrieved).

I'm lost, any help is greatly appreciated!

perception:

The method you want is getServerCertificates , it's not getServerCertificateChain. There is some good sample code here .


edit

Added some example code of my own. A good starting point for you. Don't forget to check out the Javadocs for HttpsURLConnection and X509Certificate .

import java.net.URL;
import java.security.cert.Certificate;
import java.security.cert.CertificateExpiredException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;

public class TestSecuredConnection {

    /**
     * @param args
     */
    public static void main(String[] args) {
        TestSecuredConnection tester = new TestSecuredConnection();
        try {
            tester.testConnectionTo("https://www.google.com");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public TestSecuredConnection() {
        super();
    }

    public void testConnectionTo(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) {
                try {
                    ( (X509Certificate) cert).checkValidity();
                    System.out.println("Certificate is active for current date");
                } catch(CertificateExpiredException cee) {
                    System.out.println("Certificate is expired");
                }
            }
        }
    }
}

Related


How to verify certificate chain with openssl

Zakcan I am trying to use OpenSSL to verify certificate files. Can you explain why s_clientthe connection succeeds but verifythe file with the same certificate chain fails? How to verify documents? Note that I compiled OpenSSL 1.0.1k myself, it shouldn't use a

How to verify certificate chain with openssl

Zakcan I am trying to use OpenSSL to verify certificate files. Can you explain why s_clientthe connection succeeds but verifythe file with the same certificate chain fails? How to verify documents? Note that I compiled OpenSSL 1.0.1k myself, it shouldn't use a

How to get a trusted SSL certificate?

substitute I want to secure my symfony2 application with https. I followed the instructions on how to create an SSL certificate and the SSL works fine, but Firefox says this page is not trusted. Same goes for Safari. How to get a trusted SSL certificate? Dextr

How to get a trusted SSL certificate?

substitute I want to secure my symfony2 application with https. I followed the instructions on how to create an SSL certificate and the SSL works fine, but Firefox says this page is not trusted. Same goes for Safari. How to get a trusted SSL certificate? Dextr

How to get a trusted SSL certificate?

substitute I want to secure my symfony2 application with https. I followed the instructions on how to create an SSL certificate and the SSL works fine, but Firefox says this page is not trusted. Same goes for Safari. How to get a trusted SSL certificate? Dextr

Verify certificate chain in Java from Truststore

vkx I have a certificate chain to validate as a der encoded byte[][] array. I also have a truststore file. After creating the X509Certificate[] from this byte array[][] and initializing the trustmanager, how do I tell the TrustManager to validate the X509Certi

Verify certificate chain in Java from Truststore

vkx I have a certificate chain to validate as a der encoded byte[][] array. I also have a truststore file. After creating the X509Certificate[] from this byte array[][] and initializing the trustmanager, how do I tell the TrustManager to validate the X509Certi

Verify certificate chain in Java from Truststore

vkx I have a certificate chain to validate as a der encoded byte[][] array. I also have a truststore file. After creating the X509Certificate[] from this byte array[][] and initializing the trustmanager, how do I tell the TrustManager to validate the X509Certi

Verify certificate chain in Java from Truststore

vkx I have a certificate chain to validate as a der encoded byte[][] array. I also have a truststore file. After creating the X509Certificate[] from this byte array[][] and initializing the trustmanager, how do I tell the TrustManager to validate the X509Certi

Verify certificate chain in Java from Truststore

vkx I have a certificate chain to validate as a der encoded byte[][] array. I also have a truststore file. After creating the X509Certificate[] from this byte array[][] and initializing the trustmanager, how do I tell the TrustManager to validate the X509Certi

Verify certificate chain in Java from Truststore

vkx I have a certificate chain to validate as a der encoded byte[][] array. I also have a truststore file. After creating the X509Certificate[] from this byte array[][] and initializing the trustmanager, how do I tell the TrustManager to validate the X509Certi

Verify certificate chain in Java from Truststore

vkx I have a certificate chain to validate as a der encoded byte[][] array. I also have a truststore file. After creating the X509Certificate[] from this byte array[][] and initializing the trustmanager, how do I tell the TrustManager to validate the X509Certi

How to verify a certificate against a distribution chain in Go?

Kurt Peek: I want to validate a PEM certificate against a release chain which is also a .pemfile containing multiple certificates separated by newlines, as shown in this gist https://gist.github.com/kurtpeek/8bf3282e344c781a20c5deadac75059f . I have tried as C

How to verify a certificate against a distribution chain in Go?

Kurt Peek: I want to validate a PEM certificate against a release chain which is also a .pemfile containing multiple certificates separated by newlines, as shown in this gist https://gist.github.com/kurtpeek/8bf3282e344c781a20c5deadac75059f . I have tried as C

How to verify certificate chain using CA certificate C#

Daigo Yamamoto I am trying to connect to a Mosquitto proxy. The agent will have a ca.crt and a server.crt. My application will only have ca.crt. Once connected, the proxy provides ca.crt and server.crt (certificate chain). How can I verify both against the ca.