Get trusted X509 certificate for Java SSLSocket


Jesse Wilson

I can establish a TLS connection to https://google.com/ . The remote service returns a chain of 3 certificates:

  • CN=www.google.com, O=Google Inc, L=Mountain View, ST=California, C=US
  • CN=Google Internet Authority G2, O=Google Inc, C=US
  • CN=GeoTrust Global CA, O=GeoTrust Inc., C=US

These certificates can be retrieved like this:

Certificate[] certificates = sslSocket.getSession().getPeerCertificates();

Unfortunately, this is not complete. A fourth certificate is installed in the TrustManager of my local SSL context. It looks like this:

  • OU=Equifax Secure Certificate Authority, O=Equifax, C=US

Retrieving its name is simple:

String equifax = ((X509Certificate) peerCertificates[2]).getIssuerDN().getName();

What I want is the X509Certificateinstance. How to get trusted CA certificate used during SSLSockethandshake ?

Note that I use the system TrustManagerby creating an SSL context like this:

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
Frantisek Hartman

You are almost there, just use the JVM default trust manager:

public static void main(String[] args) throws Exception{

    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket socket = (SSLSocket)factory.createSocket("www.google.com", 443);

    X509Certificate[] chain = socket.getSession().getPeerCertificateChain();

    String equifax = chain[2].getIssuerDN().getName();

    // JVM Default Trust Managers
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init((KeyStore) null);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

    X509TrustManager manager = (X509TrustManager) trustManagers[0];

    for (java.security.cert.X509Certificate x509Certificate : manager.getAcceptedIssuers()) {
        if (equifax.equals(x509Certificate.getSubjectDN().getName())) {
            System.out.println(x509Certificate);
        }
    }

}

Related


How to get BasicConstraints extension from Java X509 certificate

michalk: I want to read the extension BasicConstraints for Java X509Certificate (the certificate implementation comes from the default JCE, so yes sun.security.x509.X509CertImpl). I want to get the BasicConstraint extension value to check if it's a CA: X509Cer

How to get BasicConstraints extension from Java X509 certificate

michalk: I want to read the extension BasicConstraints for Java X509Certificate (the certificate implementation comes from the default JCE, so yes sun.security.x509.X509CertImpl). I want to get the BasicConstraint extension value to check if it is a CA: X509Ce

How to get BasicConstraints extension from Java X509 certificate

michalk: I want to read the extension BasicConstraints for Java X509Certificate (the certificate implementation comes from the default JCE, so yes sun.security.x509.X509CertImpl). I want to get the BasicConstraint extension value to check if it is a CA: X509Ce

Trust an x509 certificate already trusted on another host

on Monday I have two hosts, one with Debian Buster and the other with CentOS 7. I want to connect to the SMTP server from these two servers via SSL. However, I can only do this on CentOS: CentOS: $ openssl s_client -connect smtp.server.com:587 CONNECTED(000000

Java X509 certificate parsing and verification

Driss Amri I'm trying to handle X509 certificates in several steps and have run into two issues. I'm new to JCE so I'm not fully up to date yet. We want to be able to parse several different X509 certificates based on different encodings (PEM, DER and PCKS7).

Java X509 certificate parsing and verification

Driss Amri I'm trying to handle X509 certificates in several steps and have run into two issues. I'm new to JCE so I'm not fully up to date yet. We want to be able to parse several different X509 certificates based on different encodings (PEM, DER and PCKS7).

How to open X509 certificate in Java?

Jackson I am trying to open the crt certificate in Java, thereby getting some parameters from the crt. I use the following code: inStream = new FileInputStream("sbi.crt"); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert =

Java X509 certificate parsing and verification

Driss Amri I'm trying to handle X509 certificates in several steps and have run into two issues. I'm new to JCE so I'm not fully up to date yet. We want to be able to parse several different X509 certificates based on different encodings (PEM, DER and PCKS7).

How to get x509 certificate from http client in Go

mjb2kmn: I've gone through the documentation at https://golang.org/pkg/ but I can't make this connection. I am creating a client and requesting like this (error handling removed): client := http.Client{ CheckRedirect: func(req *http.Request, via []*http.Re

Get X509 certificate hash using OpenSSL library

Sn0wfreeze I'm currently developing an application that uses the openssl library (libcrypto) to generate certificates. Now, I have to get the hash of an already existing certificate. When I use the terminal, I can generate the hash by using openssl x509 -hash

How to get x509 certificate from http client in Go

mjb2kmn: I've gone through the documentation at https://golang.org/pkg/ but I can't make this connection. I am creating a client and requesting like this (error handling removed): client := http.Client{ CheckRedirect: func(req *http.Request, via []*http.Re

Get x509 certificate in Azure cloud service

Dan O'Leary I need to authenticate to Azure Key Vault using a certificate, but I can't access the uploaded key. I have taken the following steps: Upload the key (.pfx) to the Cloud Service through the portal. Add this to ServiceConfiguration <Certificates>

Get X509 certificate hash using OpenSSL library

Sn0wfreeze I'm currently developing an application that uses the openssl library (libcrypto) to generate certificates. Now, I have to get the hash of an already existing certificate. When I use the terminal, I can generate the hash by using openssl x509 -hash

How to get x509 certificate from http client in Go

mjb2kmn: I've gone through the documentation at https://golang.org/pkg/ but I can't make this connection. I am creating a client and requesting like this (error handling removed): client := http.Client{ CheckRedirect: func(req *http.Request, via []*http.Re