All articles

Inspect a TLS certificate with OpenSSL

5 minutes read


Linux publication

Share this article

𝕏✉

Illustration for Inspect a TLS certificate with OpenSSL

A TLS failure can come from several places: DNS, the TCP connection, the handshake, certificate trust, or hostname verification. A browser compresses those stages into one warning. OpenSSL lets you inspect them separately.

Connect with the right server name

Start with a bounded connection and pass the hostname twice:

timeout 15 openssl s_client \
  -connect example.com:443 \
  -servername example.com \
  -verify_hostname example.com \
  -verify_return_error \
  -brief </dev/null

-connect chooses the address and port. -servername sends Server Name Indication, which matters when several sites share one IP address. -verify_hostname checks that the certificate is valid for the name you intended to reach. -verify_return_error stops on a verification failure instead of continuing the diagnostic session.12

Do not omit SNI when testing a normal HTTPS virtual host. Without it, the server may return its default certificate and send you toward the wrong diagnosis.

Read the certificate itself

Save the leaf certificate, then ask openssl x509 for fields that are useful during an incident:

timeout 15 openssl s_client \
  -connect example.com:443 \
  -servername example.com \
  -showcerts </dev/null 2>/dev/null \
  | awk '/BEGIN CERTIFICATE/{on=1} on{print} /END CERTIFICATE/{exit}' \
  > leaf.pem

openssl x509 -in leaf.pem -noout \
  -subject -issuer -serial -dates -fingerprint -sha256

-showcerts prints the certificates the server sent. That list is not the same thing as a verified chain. Chain construction and trust decisions happen during verification.12

The subject identifies the certificate. The issuer tells you who signed it. The validity dates show its permitted time window, and the SHA-256 fingerprint gives you a compact value to compare through another trusted channel.3

Check an expiry window

To ask whether the certificate remains valid for at least seven days:

openssl x509 -in leaf.pem -checkend 604800 -noout

A zero exit status means the certificate will not expire within that interval. A nonzero status means it will expire, or is already expired. This makes -checkend useful in monitoring scripts, where parsing a formatted date is unnecessary.3

What this test does not prove

A successful handshake from your shell proves what that host returned on that route at that moment. It does not prove that every load balancer node serves the same chain, that clients use the same trust store, or that an application sends the same SNI value.

Repeat the test from the affected network and with the exact hostname the application uses. If a proxy terminates TLS, test both sides of that proxy separately.

[!CAUTION] Treat saved certificates and diagnostic output as operational data. Public certificates are not secret, but hostnames, internal issuers, and network paths may reveal details you should not publish.

Sources and further reading
  1. openssl-s_client
  2. OpenSSL certificate verification options
  3. openssl-x509