Verify Certificate With OpenSSL

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: January 31, 2024

The short answer

To check the expiration date of a PEM certificate and thus verify that it is still valid, you can use the following openssl x509 command:

Bash
$ openssl x509 -in <cert>  -noout -enddate

Which will write to the standard output the notAfter field of the certificate.

For example:

Bash
$ openssl x509 -in mycert.cer -noout -enddate
notAfter=Sep 19 23:59:59 2023 GMT

You can learn more about generating self-signed certificates with our article on how to generate a certificate signing request.

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering check certificate expiration openssl in the AI Command Search will prompt an openssl command that can then quickly be inserted into your shell by doing CMD+ENTER.

Verifying a file certificate

To decode and verify an entire certificate, you can use the following command:

Bash
$ openssl x509 -in <cert>  -noout -text

Where:

  • cert is the path to the file certificate.
  • The -noout flag is used to prevent the output of the encoded version of the request.
  • The -text flag is used to output the certificate in text form, including its public key, signature algorithms, etc.

For example:

Bash
$ openssl x509 -in /etc/nginx/ssl/cert.pem -noout -text

Verifying a website’s certificate

To verify the certificate of a website, you can use the following openssl s\_client command:

Bash
$ openssl s_client -connect <domain>:443

Which will retrieve the website's certificate identified by domain (e.g. example.com) and output its details in the terminal window, including its chain, issuer, and other information.

For example:

Bash
$ openssl s_client -connect google.com:443

Once downloaded, you can close the client connection by pressing CTRL + c.

Alternatively, you can use the pipe operator combined with the openssl x509 command to directly decode and verify the certificate as follows:

Bash
$ openssl s_client -connect <domain>:443 | openssl x509 -noout -text

Note that to save the certificate into a file on your local machine for future processing, you can use the output redirection operator as follows:

Bash
$ openssl s_client -connect <domain>:443 > cert.pem

Verifying a certificate and a private key match

To verify that a certificate and a private key match, you can compare their modulus by first extracting the modulus of the certificate using the following command:

Bash
$ openssl x509 -noout -modulus -in <certificate>> cert_mod

Then, by extracting the modulus of the private key using the following command:

Bash
$ openssl rsa -noout -modulus -in <private_key> > pkey_mod

Finally, by comparing these two files using the diff command:

Bash
$ diff cert_mod pkey_mod

Which will result in no output if the files are identical.

Verifying a certificate chain

A certificate chain is a series of certificates that are linked together to establish trust and verify the authenticity of a digital certificate.

To verify a certificate chain, you can use the openssl verify command as follows:

Bash
$ openssl verify -untrusted <intermediary-certificate> <certificate>

Where:

  • The -untrusted flag is used to specify the file path of the intermediate certificate.
Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.