A CLI Method to Check SSL Certificate Expiration Date
I know that browser does this automatically, but it might come in handy if you need to check the expiration date of a SSL certificate through CLI. The key is openssl
, OpenSSL command line tool.
|
|
The command is consisted of two parts:
- Retrieve SSL certificate from the server
- Extract the expiration date data
The openssl program is a command line tool for using the various cryptography functions of OpenSSL’s crypto library from the shell. It can be used for[^1]
- Creation and management of private keys, public keys and parameters
- Public key cryptographic operations
- Creation of X.509 certificates, CSRs and CRLs
- Calculation of Message Digests
- Encryption and Decryption with Ciphers
- SSL/TLS Client and Server Tests
- Handling of S/MIME signed or encrypted mail
- Time Stamp requests, generation and verification
What we need here is to perform SSL/TLS Client and Server Tests.
s_client
is one of the standard commands of openssl
command line tool:
This implements a generic SSL/TLS client which can establish a transparent connection to a remote server speaking SSL/TLS. It’s intended for testing purposes only and provides only rudimentary interface functionality but internally uses mostly all functionality of the OpenSSL ssl library.[^1]
Dig deeper into s_client
command:
The s_client command implements a generic SSL/TLS client which connects to a remote host using SSL/TLS. It is a very useful diagnostic tool for SSL servers.[^2]
Option -connect host:port
:
This specifies the host and optional port to connect to. If not specified then an attempt is made to connect to the local host on port 4433.[^2]
And the format is:
|
|
If a connection is established, openssl
enters interactive mode:
If a connection is established with an SSL server then any data received from the server is displayed and any key presses will be sent to the server. When used interactively (which means neither
-quiet
nor-ign_eof
have been given), the session will be renegotiated if the line begins with an R, and if the line begins with a Q or if end of file is reached, the connection will be closed down.[^2]
To quit, type Q
or <ctr>+d
(EOF).
|
|
Dump the session data:
|
|
To avoid the interactive mode, we can pipe an empty string into the command:
|
|
Now we have retrieved the SSL certificate from the server. Next, extract the expiration date. This is done by using the standard command x509
:
|
|
Standard command x509
is used for X.509 certificate data management.
The x509 command is a multi purpose certificate utility. It can be used to display certificate information, convert certificates to various forms, sign certificate requests like a “mini CA” or edit certificate trust settings.[^3]
What we need is to display certificate information, in particular, the expiration date. That’s the option -enddate
:
prints out the expiry date of the certificate, that is the notAfter date.[^3]
Command:
|
|
However, this will also print the certificate data. To avoid that, add -noout
option.
this option prevents output of the encoded version of the request.[^3]
In fact, this option should be used in combination with others when displaying certificate information.
|
|
Putting everything together:
|
|
Let’s take it a step further. Strip others and leave just the date:
|
|
Or:
|
|
Handling multiple domains?
|
|
Verbosely:
|
|
The entire command chain inside the sub shell was executed for every domain. Because it’s not simple to use openssl x509
command to handle multiple session documents generated from the output of openssl s_client
. Therefore, for each domain, we run the entire retrieval and extraction steps under a sub shell. Added a little formatting with xargs
and echo
.
☺
References:
[^1]: $ man openssl
[^2]: $ man s_client
[^3]: $ man x509