Verify a certificate chain with openssl

· 4 min read

Everything looks fine in the browser, yet an app, a monitoring probe or a mail server reports a certificate error? An intermediate certificate is usually missing. Browsers quietly work around that; other software does not. This guide shows how to verify the chain with openssl and curl, what the verify return codes mean, and why the openssl that ships with macOS can mislead you.

Check chain, host name and expiry

Choose what you want to know and enter the host name the way visitors use it (with or without www). -servername makes the server present the certificate for exactly that name.

What to check
Tool

Only inserted into the commands in your browser.

Verify the chain · openssl · Linux, macOS

The chain as the server sends it: s: is the subject, i: the issuer. Each level's i: must be the next level's s:. The last line is the verdict; 0 (ok) means the chain leads without gaps to a trusted root.

Shell
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | grep -E "^ [0-9]+ s:|^   i:|Verify return code"
Sample output
 0 s:CN=example.com
   i:C=US, O=Example Trust, CN=Example Issuing CA 1
 1 s:C=US, O=Example Trust, CN=Example Issuing CA 1
   i:C=US, O=Example Trust, CN=Example Root CA
Verify return code: 0 (ok)

Verify the chain · curl · Linux, macOS

curl verifies chain and host name in one go and sums up: "SSL certificate verify ok." or "SSL certificate problem: …" with the reason. It does not print the chain itself.

Shell
curl -svI https://example.com 2>&1 | grep -E "subject:|issuer:|expire date|subjectAltName|SSL certificate"
Sample output
*  subject: CN=example.com
*  expire date: Dec 25 22:56:35 2026 GMT
*  subjectAltName: host "example.com" matched cert's "example.com"
*  issuer: C=US; O=Example Trust; CN=Example Issuing CA 1
*  SSL certificate verify ok.

Chain and host name · openssl · Linux, macOS

Without -verify_hostname s_client checks the chain only. With the option it also matters whether the certificate covers this name; a wrong name gives "62 (hostname mismatch)". OpenSSL only, not LibreSSL.

Shell
openssl s_client -connect example.com:443 -servername example.com -verify_hostname example.com </dev/null 2>/dev/null | grep Verif
Sample output
Verification: OK
Verified peername: example.com
Verify return code: 0 (ok)

Chain and host name · curl · Linux, macOS

curl only connects when chain and name are valid. "OK" therefore appears only for a valid certificate; otherwise curl exits with error code 60.

Shell
curl -sI https://example.com -o /dev/null && echo OK
Sample output
OK

Expiry date · openssl · Linux, macOS

-enddate prints the expiry date, -checkend tests whether it falls within the next 2592000 seconds (30 days) and sets the exit code for scripts. LibreSSL prints only the date; the exit code is still right.

Shell
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -enddate -checkend 2592000
Sample output
notAfter=Dec 25 22:56:35 2026 GMT
Certificate will not expire

Expiry date · curl · Linux, macOS

The server certificate's expiry date taken from curl's verbose output.

Shell
curl -svI https://example.com 2>&1 | grep "expire date"
Sample output
*  expire date: Dec 25 22:56:35 2026 GMT

A complete chain consists of the server certificate (level 0) and every intermediate. The server does not need to send the root; it lives in the client's trust store.

The verify return codes

The number on the last line says where verification failed. These are the ones you meet in practice:

  • 0 (ok): the chain is valid. Without -verify_hostname that says nothing about the host name.
  • 20 (unable to get local issuer certificate): the issuer of one certificate in the chain is missing. Nearly always the server does not send the intermediate.
  • 21 (unable to verify the first certificate): the consequence of 20 right at the server certificate. The server sends only its own certificate.
  • 10 (certificate has expired): a certificate in the chain is past its date. Usually the server certificate, occasionally a stale intermediate left in the configuration.
  • 18 (self-signed certificate): the server certificate is self-signed; there is no chain.
  • 19 (self-signed certificate in certificate chain): the chain ends at a root your system does not trust, such as a company CA or a TLS-inspecting proxy on the network.
  • 62 (hostname mismatch): only with -verify_hostname. The chain is fine but the certificate does not cover this name.

For 20 and 21 the fix is on the server: configure the full chain instead of the single certificate, with Let's Encrypt the file fullchain.pem rather than cert.pem. The order is server certificate first, then the intermediates.

Verify certificate files before deploying them

When certificate and chain arrive as files, check them before they go onto the server. -untrusted passes the intermediates, -show_chain prints the chain that was built.

Shell
$ openssl verify -show_chain -untrusted chain.pem cert.pem
cert.pem: OK
Chain:
depth=0: CN=example.com (untrusted)
depth=1: C=US, O=Example Trust, CN=Example Issuing CA 1 (untrusted)
depth=2: C=US, O=Example Trust, CN=Example Root CA

$ openssl verify cert.pem
CN=example.com
error 20 at 0 depth lookup: unable to get local issuer certificate
error cert.pem: verification failed

"(untrusted)" only means the certificate came from the file you passed rather than the trust store. The second call, without the chain, shows exactly the error clients see when the server omits the intermediate. grep -c "BEGIN CERTIFICATE" chain.pem counts the certificates in a file.

Careful with the openssl on macOS

/usr/bin/openssl on macOS is LibreSSL. Tested against a server with a missing intermediate, LibreSSL 3.3.6 did print "verify error:num=20" and "num=21", but finished with "Verify return code: 0 (ok)". Read only the last line and you will think the chain is valid. OpenSSL 3 correctly reported 21 for the same server.

curl disagrees across systems too: the curl on macOS said "SSL certificate verify ok." for that server, curl on Debian said "unable to get local issuer certificate". For chain checks rely on a current OpenSSL (via Homebrew: brew install openssl) or on a test from Linux, and watch for "verify error" lines further up in the output.

  • Names: OpenSSL 3.2 and later prints "s:CN=example.com", older versions such as 1.1.1 and 3.0 "s:CN = example.com" with spaces, LibreSSL "s:/CN=example.com".
  • LibreSSL does not know -verify_hostname or -brief ("unknown option").
  • With OpenSSL, -verify_return_error aborts the handshake with an error exit code as soon as verification fails, so a script can read the result from the exit status.
  • openssl x509 -ext subjectAltName exists only in OpenSSL; on LibreSSL use -text.

Checking without a terminal

The SSL checker connects with SNI, verifies the chain against the common root stores and shows issuer, validity, alternative names and the negotiated protocol version, for mail server ports as well if you like. More openssl commands for reading certificates are in the guide "Check an SSL certificate with openssl".

Frequently asked questions

How do I check with openssl whether a certificate chain is valid?
Run openssl s_client -connect example.com:443 -servername example.com < /dev/null and look at the "Verify return code" line. 0 (ok) means valid, 20 or 21 a missing intermediate. For files: openssl verify -untrusted chain.pem cert.pem.
What does "Verify return code: 21 (unable to verify the first certificate)" mean?
The server sends only its own certificate without the intermediate. Clients that have no copy of it cannot build the chain. Configure the full chain on the server (fullchain).
Why does the site work in the browser despite an incomplete chain?
Browsers remember intermediates from earlier connections or fetch them from the address named in the certificate. Command line tools, apps, mail servers and many libraries do not.
What does "Verify return code: 19" mean?
The chain ends at a root certificate your system does not know. The cause is either a private CA or a device on the network that intercepts TLS connections and re-signs them.
Check now

Free SSL certificate check: expiry date, issuer, chain, host name and TLS version, mail servers too. Detects expired, self-signed and mismatched certificates.

DomainWarn checks the certificates of all client domains on a schedule, mail ports included, and reports expiry, wrong host names and incomplete chains before visitors see a warning.

Monitor this domain continuously14-day free trial, no credit card.

More guides