A browser that refuses a certificate usually gives one vague line. OpenSSL gives the reason, if it is asked the right question. The commands below cover the checks that come up most: when a certificate expires, which names it covers, whether its chain is complete, and whether a private key belongs to it. Every one was run against a small test setup built for this post, a private root CA, an intermediate CA, two server certificates and an nginx server, using OpenSSL 3.0.13. The output shown is what it printed.
Reading a certificate file
Subject, issuer and dates
openssl x509 reads a certificate and prints whatever parts are asked for. -noout suppresses the encoded certificate itself so only the answers appear:
$ openssl x509 -in srv.crt -noout -subject -issuer
subject=CN = test.example
issuer=CN = Test Root CA
$ openssl x509 -in srv.crt -noout -dates
notBefore=...
notAfter=...
The OpenSSL documentation describes -dates as printing the start and expiry dates of a certificate, and -subject and -issuer as printing the subject and issuer names. The dates appear in GMT. A certificate is valid only between the two; a notBefore in the future fails just as surely as a notAfter in the past, which catches servers whose clocks have drifted.
The names it actually covers
The common name in the subject is not what modern clients check. They check the Subject Alternative Name extension, which -ext prints:
$ openssl x509 -in srv.crt -noout -ext subjectAltName
X509v3 Subject Alternative Name:
DNS:test.example, DNS:www.test.example
A certificate whose subject says test.example but whose SAN list leaves out www.test.example will fail on the www name, and this is the command that shows it.
Fingerprint
-fingerprint -sha256 prints a hash of the whole certificate. Two copies with the same fingerprint are the same certificate, which settles the common question of whether a renewal actually reached a server.

Expiry and hostname checks
-checkend
For monitoring, reading dates by eye is the wrong tool. -checkend takes a number of seconds and, per the documentation, exits nonzero if yes it will expire within that window, or zero if not. The test certificate was issued for 30 days:
$ openssl x509 -in srv.crt -noout -checkend 604800 # 7 days
Certificate will not expire
$ echo $?
0
$ openssl x509 -in srv.crt -noout -checkend 5184000 # 60 days
Certificate will expire
$ echo $?
1
That exit code is the whole point. A cron job or monitoring check can run -checkend with 14 or 30 days and alert on a nonzero result, with no date parsing at all.
Checking a hostname with -checkhost
openssl x509 can also answer whether a certificate matches a name. The documentation describes -checkhost as checking that the certificate matches the specified host:
$ openssl x509 -in srv.crt -noout -checkhost www.test.example
Hostname www.test.example does match certificate
$ openssl x509 -in srv.crt -noout -checkhost mail.test.example
Hostname mail.test.example does NOT match certificate
That check reads the SAN list, the same list a browser uses. It is worth running on a new certificate before it goes anywhere near a server, because a missing name is cheaper to catch in a file than in a support ticket.
Everything at once with -text
-text prints the whole certificate in readable form: version, serial number, signature algorithm, validity, subject, public key and every extension. On the test leaf it showed sha256WithRSAEncryption as the signature algorithm and a Public-Key: (2048 bit) line. It is long, which is why the targeted options exist, but when something about a certificate looks wrong and the cause is not obvious, -text piped through less is the place to look before guessing.
Checking a live server
s_client and SNI
A certificate file on disk and the certificate a server actually sends are not always the same thing. openssl s_client connects to a server and shows what came back. The option that matters most is -servername, which, in the documentation’s words, sets the TLS SNI (Server Name Indication) extension in the ClientHello. A server hosting several sites on one address picks the certificate by that name, so leaving it out can return the wrong one. Since OpenSSL 1.1 the name given to -connect is used for SNI when it looks like a DNS name, but an IP address or a test against a different name still needs -servername spelled out.
-brief trims the output to a summary:
$ echo | openssl s_client -connect 127.0.0.1:8443 -servername test.example -CAfile ca.crt -brief
CONNECTION ESTABLISHED
Protocol version: TLSv1.3
Ciphersuite: TLS_AES_256_GCM_SHA384
Peer certificate: CN = test.example
Verification: OK
The echo | at the front closes the connection after the handshake, so the command returns instead of waiting for input.
Piping the server’s certificate into x509
s_client prints the server certificate in PEM form, so it can be piped straight into the commands from the first section:
$ echo | openssl s_client -connect 127.0.0.1:8443 -servername test.example 2>/dev/null \
| openssl x509 -noout -subject -issuer -ext subjectAltName
subject=CN = test.example
issuer=CN = Test Root CA
X509v3 Subject Alternative Name:
DNS:test.example, DNS:www.test.example
Adding -checkend 1209600 to the second command turns it into a 14-day expiry check against the live site.
Hostname mismatch from the client side
-verify_hostname makes s_client check the certificate against a name, as a browser would. Asked to accept other.example, it reported:
verify error:num=62:hostname mismatch
Verify return code: 62 (hostname mismatch)

The missing intermediate
Why a chain fails only in some clients
The most common certificate fault on a working-looking site is an incomplete chain. Public certificates are rarely signed directly by a root; they are signed by an intermediate CA, and the server is supposed to send that intermediate along with its own certificate. Browsers often paper over a missing intermediate. Command-line tools, API clients and older devices often do not.
The test built a leaf certificate signed by an intermediate, then served it two ways from nginx: first the leaf alone, then a fullchain file with the leaf followed by the intermediate. -showcerts lists what the server sent; the documentation notes it shows the server certificate list as sent by the server, and that it is not a verified chain.
| Server sends | Certificates listed | Verify return code |
|---|---|---|
| Leaf only | 0 s:CN = test.example |
21 (unable to verify the first certificate) |
| Leaf + intermediate | 0 s:CN = test.example, 1 s:CN = Test Intermediate CA |
0 (ok) |
In both runs the client trusted the same root. Only the file on the server changed. Error 21 from s_client, with only one certificate in the -showcerts list, points straight at the server’s certificate file.
The same fault in a file
openssl verify builds and checks a chain from files. The documentation describes it simply: it verifies certificate chains. Given only the root, the leaf failed with error 20 at 0 depth lookup: unable to get local issuer certificate. Adding the intermediate with -untrusted, an option the documentation describes as a source of untrusted certificates to use for chain building, made it pass:
$ openssl verify -CAfile ca.crt -untrusted int.crt leaf.crt
leaf.crt: OK
“Untrusted” there does not mean suspicious. It means the intermediate is used to build the path, while trust still has to end at the root given in -CAfile.
A small expiry check for several sites
The script
The pieces above combine into a monitoring check short enough to read in one glance. This version takes host names, connects to each with SNI, pipes the certificate into -checkend with a 14-day window, and exits with 1 if anything needs attention:
#!/bin/sh
# usage: chk.sh host[:port] ... exits 1 if any cert expires within 14 days
rc=0
for h in "$@"; do
hp=$h; case $h in *:*) ;; *) hp=$h:443;; esac
if echo | openssl s_client -connect "$hp" -servername "${h%%:*}" 2>/dev/null \
| openssl x509 -noout -checkend 1209600 >/dev/null; then echo "ok $h"
else echo "WARN $h"; rc=1; fi
done
exit $rcPointed at the test server, it printed ok test.example and exited 0 with the 14-day window. With the window widened to 40 days, longer than the test certificate’s 30-day life, it printed WARN test.example and exited 1.
A failure that counts as a warning
The first run of the script was pointed at port 443, where nothing was listening. s_client could not connect, x509 received nothing and printed Could not read certificate from <stdin>, and the script reported WARN. That behavior is deliberate in a check like this. A site that cannot be reached at all is at least as urgent as one whose certificate is about to lapse, and a check that went quiet whenever a connection failed would hide exactly the outages it exists to catch.

Extensions, keys and signing requests
CA or leaf
The -ext option takes several names at once. Asked for basicConstraints,keyUsage, the test intermediate printed CA:TRUE, marked critical, and a key usage of Certificate Sign, CRL Sign: the marks of a certificate allowed to sign other certificates. The same command on the leaf printed No extensions in certificate, because the leaf was issued with only a SAN extension. A server certificate that claims CA:TRUE is a misissue worth noticing, and a CA certificate missing it will not be accepted as an issuer by a validating client.
Does the key match the certificate?
After a renewal, the fastest way to break a server is to pair a new certificate with the old key. The certificate contains the public key; the private key file can produce its own public key. If the two hash to the same value, they belong together:
$ openssl x509 -in srv.crt -noout -pubkey | sha256sum
a6078b4976dd669d...
$ openssl pkey -in srv.key -pubout | sha256sum
a6078b4976dd669d...
$ openssl pkey -in ca.key -pubout | sha256sum
1df21b683e5a8162...
The server key matched the server certificate; the CA’s key, as expected, did not. This method works for RSA and elliptic-curve keys alike, unlike the older habit of comparing -modulus output, which only applies to RSA.
Checking a CSR before sending it
A certificate signing request can be checked the same way before it goes to a CA. openssl req -in srv.csr -noout -subject -verify printed Certificate request self-signature verify OK and the subject, confirming the request was signed by the key it claims and carries the intended name. The DNS records that point a name at the server are a separate check, covered in the earlier post on A, CNAME, MX and TXT records.
Sources
- openssl-x509, OpenSSL 3.0 documentation: https://docs.openssl.org/3.0/man1/openssl-x509/
- openssl-s_client, OpenSSL 3.0 documentation: https://docs.openssl.org/3.0/man1/openssl-s_client/
- openssl-verify, OpenSSL 3.0 documentation: https://docs.openssl.org/3.0/man1/openssl-verify/
