• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Mister PKI

SSL Certificates * SSL Tools * Certificate Decoder

  • Buy SSL Certificates
  • Blog
  • OpenSSL
  • Keytool
  • SSL Tools
  • Donate

openssl verify – Verify a certificate and certificate chain

January 10, 2023 by Mister PKI Leave a Comment

Use the openssl verify function to verify a certificate chain. To verify a certificate chain you must first get the certificate chain to verify against.

openssl verify certificate chain

To verify a certificate and its chain for a given website with OpenSSL, run the following command:

openssl verify -CAfile chain.pem www.example.org.pem

Where -CAfile chain.pem is the downloaded certificate chain installed at the site and www.example.org.pem is the downloaded end entity server cert. You can download the certificate chain by visiting the website and using your browser tools to export the chain. Alternatively, you can use the examples at openssl_s_client to get the chain from the command line.

You can also add the -x509_strict flag for strict compliance.

To verify the intermediates and root separately, use the -untrusted flag. Note that -untrusted can be used once for a certificate chain bundle of intermediates, or can be used more than once for each intermediate in a separate file.

openssl verify -CAfile root.pem -untrusted intermediate.pem www.example.org.pem

Add the -show_chain flag to output the certificate chain and corresponding depth of each certificate in the chain.

openssl verify -show_chain -CAfile chain.pem www.example.org.pem

openssl verify certificate and CRL

To verify a certificate with it’s CRL, download the certificate and get its CRL Distribution Point. The following commands will demonstrate how to use openssl to check a certificate against its CRL.

openssl x509 -noout -text -in www.example.org.pem | grep -A 4 'X509v3 CRL Distribution Points'

In the output you should see the CRL url.

Next, download the CRL with the wget function. It will be in der format, so we will be converting it to pem format for the openssl verify function to work.

wget -O crl.der http://crl3.digicert.com/DigiCertTLSRSASHA2562020CA1.crl

Next, convert the crl to pem format with the openssl crl function:

openssl crl -inform DER -in crl.der -outform PEM -out crl.pem

Next, concatenate the the chain and the crl into one file:

cat chain.pem crl.pem > crl_chain.pem

Finally, use openssl to verify the ssl certificate with its CRL:

openssl verify -crl_check -CAfile crl_chain.pem www.example.org.pem

You should see an OK message. If the certificate has been revoked, you will see a lookup:certificate revoked message.

openssl verify certificate and key

To use openssl to verify an ssl certificate is the matching certificate for a private key, we will need to break away from using the openssl verify command and switch to checking the modulus of each key. This example will demonstrate the openssl command to check a certificate with its private key.

First, use the openssl rsa command to check that the private key is valid:

openssl rsa -check -noout -in key.pem
openssl check private key

The result should be: RSA key ok. If not, you will need to determine why your key may be corrupt.

After verifying that the private key is valid, determine its modulus with this command:

openssl rsa -modulus -noout -in key.pem | openssl sha256
openssl modulus private key

Then also determine the modulus of the public key, running the same command except with the openssl x509 command instead of rsa.

openssl x509 -modulus -noout -in cert.pem | openssl sha256
openssl modulus certificate

Verify that the modulus of both the public key and private key are the same. If they are, then you have a matching public key and certificate with your private key.

Additionally, you can verify that the modulus of the key in the CSR is also the same by running the following command against the CSR:

openssl req -noout -modulus -in example.csr | openssl sha256

Java verify certificate

You may find it useful to programmatically verify a certificate, a certificate chain, or a certificate path with Java. For this, you can use the java security package.

Here is a Java code sample for verifying a certificate chain with java security. For the complete example with the ability to build a jar artifact to run locally for the certificate validation, view the GitHub repository.

public static boolean validateCertificateChain(final List<X509Certificate> certificates) {
    for (int i = 0; i < certificates.size(); i++) {
      try {
        if (i == certificates.size() - 1) {
          if (isSelfSigned(certificates.get(i))) {
            certificates.get(i).verify(certificates.get(i).getPublicKey());
          }
        } else {
          certificates.get(i).verify(certificates.get(i + 1).getPublicKey());
        }
      } catch (Exception e) {
        return false;
      }
    }
    return true;
  }

To follow along in this piece of code:

  1. pass in an ordered list of certificates as a parameter, starting from the leaf and ending with the root, to be validated.
  2. Loop through the list of certificates.
  3. If the certificate is the last in the list, check if it is self-signed. If it is not self signed, do nothing and stop validation (this will pass even though it’s an incomplete chain). If it is, verify the signature of the certificate with its own public key.
  4. If the certificate was not last in the list, verify the signature of the certificate with the public key of the next certificate in the list.
  5. If at any point verification fails an exception will be caught and the method will return false. Else, the method will return true.

To verify a certificate path these steps can be followed programmatically with code or by hand using the openssl commands above.

Read more of our content.

java,  openssl

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular Posts

PKCS12

openssl s_client

Keytool

Keytool list

ECDSA vs RSA

OpenSSL

PKCS7

Certificate Decoder

Training Courses

Top online courses in IT & Software

Cyber Security Training

Udemy - The Complete Internet Security Privacy Course icon

Buy SSL Certificates

The SSL Store

Comodo Store

Sectigo Store

RapidSSL

Recent Posts

  • Ubuntu unattended upgrades
  • openssl verify – Verify a certificate and certificate chain
  • NetScaler ADC configuration history
  • Remove CA from Domain in Active Directory
  • pfx password

Footer

  • Twitter
  • YouTube

Pages

  • About Mister PKI
  • Blog
  • Compare and Buy Affordable PKI Certificates
  • Contact Us
  • Full Disclosure
  • Privacy Policy
  • SSL Tools – Certificate Decoder and Certificate Checker

Copyright © 2023