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
: