Common certificate errors and how to check them with openssl: expiry, missing intermediates, wrong hostname

  1. Goal
  2. Preparation
  3. Core tools
  4. Error 1: the certificate has expired
  5. Error 2: the intermediate certificate is missing
  6. Error 3: wrong hostname
  7. Checking in order
  8. Common mistakes when using `openssl` itself
  9. Summary
  10. References

Goal

This article helps you recognise three common certificate errors, read openssl's messages and know where to fix them.

You should already know the certificate-chain concept and how certificates are issued and renewed (the two previous articles in the series).

Preparation

Core tools

Two commands you will use constantly:

# Connect and see the verification result. -servername sends SNI, required when one IP serves many names.
echo | openssl s_client -connect example.com:443 -servername example.com -verify_hostname example.com

# Read the details of a certificate in a PEM file
openssl x509 -in cert.pem -noout -subject -issuer -dates -ext subjectAltName

echo | closes the input so s_client exits at once instead of waiting for you to type.

At the end of s_client's output, the line Verify return code: 0 (ok) means the chain is valid; a non-zero number with an explanation is an error.

With -verify_hostname you also see a line Verification: OK or Verification error: ....

To fetch the server certificate and read it straight away:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates

Error 1: the certificate has expired

Symptoms: the browser says "certificate has expired"; s_client reports an error code related to the validity period (in OpenSSL it reads certificate has expired).

Check:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates

Look at the notAfter line. To find out whether the certificate expires within the next N seconds, use -checkend:

openssl x509 -in cert.pem -noout -checkend 1209600   # 14 days

The command prints Certificate will not expire and exits with code 0 if it is still valid, or Certificate will expire and code 1 if it is about to expire.

This is very handy in a monitoring script.

Fix: renew the certificate, then reload the web service. A common cause is that the renewal timer is not running, or the new certificate is on disk but nginx has not been reloaded.

Note: a skewed system clock causes similar errors. If the client's clock is wrong, a valid certificate can still be treated as not yet valid or as expired.

Error 2: the intermediate certificate is missing

Symptoms: some clients (especially curl, libraries inside applications and the Java virtual machine) refuse the connection,

while a few browsers still open the site because they fetch the missing intermediate certificate themselves. openssl s_client reports:

verify error:num=20:unable to get local issuer certificate
verify error:num=21:unable to verify the first certificate
Verify return code: 21 (unable to verify the first certificate)

Check: list the certificates the server actually sends:

echo | openssl s_client -connect example.com:443 -servername example.com -showcerts 2>/dev/null | grep -E "^ *[0-9]+ s:|^ *i:"

Each certificate is printed with an s: (subject) line and an i: (issuer) line.

If there is only one certificate (entry 0) and its issuer is not a trusted root, the server forgot to send the intermediate certificate.

The error codes 20 and 21 above also appear when the client simply does not trust the issuer (for example a self-made CA that has not been added to the trust store), so compare with -showcerts before drawing a conclusion.

Fix: configure nginx to use the file that contains both the server certificate and the intermediate certificate. With Let's Encrypt, that is fullchain.pem, not cert.pem.

Then reload nginx and check again.

Error 3: wrong hostname

Symptoms: the browser says the name in the certificate does not match. With OpenSSL and -verify_hostname, you see:

verify error:num=62:hostname mismatch
Verification error: hostname mismatch
Verify return code: 62 (hostname mismatch)

Check: read the list of names the certificate covers:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -ext subjectAltName

The name you visit must appear in the DNS: list (or match a wildcard entry such as *.example.com; that entry covers only one level of subdomain, so it does not cover example.com itself).

Two common causes:

Fix: issue the certificate again with all the names you need (one -d per name with certbot), or fix the configuration so that the right server block serves the right name.

Checking in order

When you receive a report that "the web shows a certificate error", you can follow a fixed order:

  1. Run s_client with -servername and -verify_hostname, and read Verify return code.

  2. If the error is about validity: read notAfter and check the system clock.

  3. If the error is that the issuer cannot be found: use -showcerts to count how many certificates the server sends.

  1. If the error is about the name: read the SAN and compare it with the name being visited.

Only fix the server once you know clearly which type of cause it is, and always check again with the same command after fixing.

Common mistakes when using `openssl` itself

Summary

The three errors, expiry, a missing intermediate certificate and a wrong hostname, account for most certificate incidents.

With openssl s_client to see the verification code and openssl x509 to read the expiry date and the SAN, you can tell them apart in a few commands.

Adding -checkend to your monitoring lets you learn that a certificate is about to expire before your users do.

References

Common certificate errors and how to check them with openssl: expiry, missing intermediates, wrong hostname