跳到主要内容

openssl s_client: verify error:num=20 unable to get local issuer certificate

verify error:num=20:unable to get local issuer certificate is the most common chain-building failure you will hit when hand-testing a certificate with openssl s_client. It almost always means one of two things: the server is not sending the intermediate certificate, or your local OpenSSL is not reading the trust store you think it is. This guide shows how to tell which one it is in under a minute, how to fix each side, and how to prove the fix with Verify return code: 0 (ok).

1. What ‘unable to get local issuer certificate’ Actually Means

OpenSSL verifies a certificate by walking up the chain: leaf, then intermediate, then root. At each step it needs the certificate that issued the one it currently holds. It looks for that issuer in two places: the certificates the server sent, and the local trust store. num=20 means: I have a certificate at this depth, but I cannot find its issuer anywhere I am allowed to look.

depth=1 C = US, O = Let's Encrypt, CN = R3
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = example.com
verify return:1
---
Verify return code: 20 (unable to get local issuer certificate)

Read the depth= numbers bottom-up: depth=0 is the leaf (your domain certificate), depth=1 is the first intermediate, depth=2 is the next one up. The error is reported at the depth where the chain broke.

  • num=20 at depth=0 — OpenSSL has the leaf but nothing that issued it. The server sent one certificate and no intermediate.
  • num=20 at depth=1 — the intermediate was sent, but the root (or cross-signed root) that issued it is not in the local trust store.
  • num=20 at depth=2 — usually a private CA or an old root that modern distributions have removed.

Critically, this is a client-side verification result. It does not mean the certificate is invalid, and it does not mean the handshake failed — s_client still completes the connection and prints Verify return code: 20. Browsers often succeed against the same server because they fetch missing intermediates over AIA (Authority Information Access) and cache them; OpenSSL does not chase AIA URLs.

2. Step 1 — Reproduce It and Capture the Full Handshake

Never debug from the single error line. Capture everything first:

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>&1 | tee s_client.log

Then pull out the lines that matter:

grep -E 'depth=|verify error|verify return|Verify return code|subject=|issuer=' s_client.log

Two details change the result more often than people expect:

  • -servername example.com sends SNI. Without it you may be served the default virtual host’s certificate, which has a completely different chain.
  • </dev/null closes stdin so the command exits instead of waiting for input. Without it the session hangs after the handshake and scripts stall.

Note that s_client exits with status 0 even when verification fails. For scripts, force a real failure:

openssl s_client -connect example.com:443 -servername example.com -verify_return_error </dev/null >/dev/null 2>&1
echo "exit=$?"

3. Step 2 — Count the Certificates the Server Actually Sends

This is the single most useful command in this article. -showcerts prints every certificate in the chain the server presents:

openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | grep -c 'BEGIN CERTIFICATE'

Interpret the number:

  • 1 — leaf only. The chain is incomplete. Fix the server (Step 3).
  • 2 — leaf plus one intermediate. Normal and correct for most public CAs.
  • 3 — leaf, intermediate, plus a root or a cross-signed intermediate. Usually fine, occasionally the source of num=19.
  • 0 — no certificate presented at all: wrong port, a plaintext service, or a TLS listener that needs SNI you did not send.

Now inspect what you got, in order:

openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null > chain.txt
csplit -sz -f cert- chain.txt '/-----BEGIN CERTIFICATE-----/' '{*}'
for f in cert-*; do echo "== $f"; openssl x509 -in "$f" -noout -subject -issuer -dates; done

The order must be leaf first, then its issuer, then that issuer’s issuer. Check it mechanically: the issuer of cert-00 must equal the subject of cert-01, and so on. If it does not, the chain is misordered and some clients will fail even though every certificate is present.

openssl x509 -in cert-00 -noout -issuer
openssl x509 -in cert-01 -noout -subject

4. Step 3 — Fix the Server: Send the Full Chain

If Step 2 returned 1, no client-side flag will help a normal browser or library: the intermediate simply is not on the wire. Build a full-chain file, leaf first:

cat example.com.crt intermediate.crt > fullchain.pem
openssl x509 -in fullchain.pem -noout -subject   # confirms the leaf is first

nginx — point ssl_certificate at the bundle, not at the bare leaf:

ssl_certificate     /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/example.com.key;

Apache httpd 2.4.8+ — the chain goes in the same file as the leaf:

SSLCertificateFile    /etc/httpd/ssl/fullchain.pem
SSLCertificateKeyFile /etc/httpd/ssl/example.com.key

Apache httpd older, or explicit chain directive:

SSLCertificateFile      /etc/httpd/ssl/example.com.crt
SSLCertificateChainFile /etc/httpd/ssl/intermediate.crt
SSLCertificateKeyFile   /etc/httpd/ssl/example.com.key

HAProxy — one PEM containing key, leaf, then intermediates:

cat example.com.key example.com.crt intermediate.crt > /etc/haproxy/certs/example.com.pem
bind *:443 ssl crt /etc/haproxy/certs/example.com.pem

Reload the service, then re-run the count from Step 2. It should now print 2 (or 3). Do not append the root certificate to the chain: clients are supposed to already trust it, and sending it adds bytes to every handshake and can trigger num=19:self-signed certificate in certificate chain on stricter verifiers.

5. Step 4 — Fix the Client: Point OpenSSL at the Right Trust Store

If Step 2 returned 2 or 3 and you still see num=20, the chain is fine and the local trust store is the problem. First find out which store this OpenSSL build uses by default:

openssl version -d
openssl version -a

Then test explicitly against a known bundle:

openssl s_client -connect example.com:443 -servername example.com 
  -CAfile /etc/ssl/certs/ca-certificates.crt </dev/null

Typical bundle locations: Debian/Ubuntu /etc/ssl/certs/ca-certificates.crt, RHEL/Fedora /etc/pki/tls/certs/ca-bundle.crt, Alpine /etc/ssl/cert.pem, Homebrew OpenSSL on macOS /opt/homebrew/etc/openssl@3/cert.pem. Homebrew’s OpenSSL does not read the macOS Keychain, which is why a chain that Safari accepts can fail here.

The directory form needs hash-named symlinks, so it fails quietly if the directory has never been processed with c_rehash:

openssl s_client -connect example.com:443 -servername example.com -CApath /etc/ssl/certs </dev/null
c_rehash /etc/ssl/certs        # or: update-ca-certificates

OpenSSL 3.x also accepts a store URI:

openssl s_client -connect example.com:443 -servername example.com -CAstore 'file:/etc/ssl/certs' </dev/null

To change the default for every command in a shell, use the environment variables OpenSSL reads at startup:

export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
export SSL_CERT_DIR=/etc/ssl/certs

For a private CA, either pass the root directly with -CAfile root-ca.pem or install it into the system store (update-ca-certificates on Debian, update-ca-trust on RHEL) so every tool agrees.

6. Step 5 — Complete Chain, Still Failing: Order, Cross-Signs, and Expiry

Verify offline, without any network involved. This isolates chain building from trust-store discovery:

openssl verify -CAfile root-ca.pem -untrusted intermediate.pem example.com.crt
openssl verify -show_chain -CAfile root-ca.pem -untrusted intermediate.pem example.com.crt   # OpenSSL 3.x

If that passes but s_client fails, the issue is the local store. If it fails, the message tells you exactly which certificate is the problem. The usual suspects:

  • Wrong order in fullchain.pem — intermediate before leaf. Some clients tolerate it, many do not.
  • Duplicate or unnecessary root in the bundle — produces num=19.
  • Expired intermediate — the leaf can be perfectly valid while the intermediate it chains to expired months ago. Check with -dates.
  • Cross-signed ambiguity — two possible paths to different roots. Try -trusted_first, or -partial_chain when you intentionally trust an intermediate.
  • Intermediate issued by a root that is not in the store — common with private CAs and with roots removed from modern distributions.
  • Broken extensions — a non-CA intermediate (basicConstraints CA:FALSE), a name-constraints violation, or a key-usage mismatch. Add -x509_strict to surface these.
openssl x509 -in intermediate.pem -noout -text | grep -A1 'Basic Constraints'
openssl x509 -in intermediate.pem -noout -dates

7. Comparison Table: OpenSSL Verify Error Codes

Code Message Typical cause First thing to run
num=20 unable to get local issuer certificate Issuer missing locally: server sent only the leaf, or the root is absent from the trust store -showcerts plus a certificate count
num=21 unable to verify the first certificate Leaf alone with no intermediate, and none available locally -showcerts count
num=19 self-signed certificate in certificate chain Root included in the served chain, or a self-signed intermediate Remove the root from fullchain.pem
num=18 self-signed certificate The leaf itself is self-signed (dev and test setups) -CAfile pointing at that certificate
num=2 unable to get issuer certificate Issuer missing higher up the chain, often after -partial_chain Check which depth= line precedes it
num=10 certificate has expired Expired leaf or intermediate openssl x509 -noout -dates on each cert
num=9 certificate is not yet valid Clock skew, or a certificate with a future notBefore date -u and -dates
0 ok Chain built and verified successfully Nothing — this is the goal

8. Troubleshooting: Real Messages and What They Mean

1. ‘verify error:num=20:unable to get local issuer certificate’ followed by ‘Verify return code: 20’

depth=1 C = US, O = Let's Encrypt, CN = R3
verify error:num=20:unable to get local issuer certificate
Verify return code: 20 (unable to get local issuer certificate)

Run the certificate count from Step 2. One certificate means a server fix; two or three means a trust-store fix with -CAfile.

2. Only depth=0 appears before the error

depth=0 CN = example.com
verify error:num=20:unable to get local issuer certificate

OpenSSL never received an intermediate, so it could not even attempt depth=1. The server is sending the leaf alone. Fix ssl_certificate or SSLCertificateChainFile.

3. ‘verify error:num=21:unable to verify the first certificate’

Same root cause as above — a bare leaf with no chain — reported differently depending on OpenSSL version and on whether any issuer was found at all. Treat it as a server-side chain problem first.

4. ‘verify error:num=19:self-signed certificate in certificate chain’

depth=2 C = US, O = DigiCert Inc, CN = DigiCert Global Root CA
verify error:num=19:self-signed certificate in certificate chain

The server is sending its root. Remove the root from fullchain.pem and reload; leaf plus intermediates is the correct payload.

5. ‘verify error:num=18:self-signed certificate’

The leaf itself is self-signed — an internal service or a development box. Either trust it explicitly with -CAfile server.crt or install the internal root in the system store.

6. ‘verify error:num=10:certificate has expired’

depth=1 C = US, O = Let's Encrypt, CN = R3
verify error:num=10:certificate has expired
notAfter=Sep 30 15:01:15 2021 GMT

Not a chain problem. Reissue the certificate so you get a current intermediate, or fix your automation — the leaf may have renewed while the intermediate in your bundle did not.

7. Verification passes with -CAfile but fails by default

This is definitive proof that the chain is fine and the default store is wrong. Compare openssl version -d with the bundle you passed. On macOS with Homebrew OpenSSL, or inside slim Docker images without ca-certificates, this is the norm rather than the exception.

apt-get install -y ca-certificates && update-ca-certificates
# or
dnf install -y ca-certificates && update-ca-trust

8. ‘no peer certificate available’

openssl s_client -connect example.com:443 </dev/null
...
no peer certificate available
No client certificate CA names sent

You reached something that is not presenting a certificate: a plaintext HTTP port, a proxy, or a TLS listener that needs SNI. Re-test with -servername and confirm the port with ss -ltnp | grep 443.

9. The command hangs after printing the certificate

s_client keeps the connection open and reads stdin. Add </dev/null, or use -brief for a compact one-shot summary. In CI, always redirect stdin.

10. -CApath is given but num=20 persists

-CApath only works with hash-named symlinks. If the directory holds plain .pem files with no xxxxxxxx.0 links, OpenSSL finds nothing. Run c_rehash /etc/ssl/certs or switch to -CAfile with the bundle.

11. Fails for one hostname on a shared IP

Missing SNI: without -servername the server returns the default vhost certificate and its chain, which may legitimately not match your target. Add -servername and confirm the name with -verify_hostname example.com.

12. curl works, openssl does not

They may be reading different bundles, or curl may be linked against a different TLS library. Check with curl -V and curl -vI https://example.com; if curl succeeds while s_client fails, compare CA paths before assuming the certificate is broken.

9. FAQ

Is verify error:num=20 a server problem or a client problem?
Both are possible, and the certificate count decides. One certificate from -showcerts is a server problem. Two or three certificates plus num=20 is a client trust-store problem.

Why do browsers load the site fine while openssl fails?
Browsers fetch missing intermediates via the AIA extension in the leaf certificate and cache them. OpenSSL performs no AIA fetching, so it only sees what the server sent plus what is in its local store.

Does s_client return a non-zero exit code when verification fails?
No. It prints Verify return code: 20 and exits 0. Add -verify_return_error to abort the handshake and get a non-zero status.

What is the exact success criterion?
Verify return code: 0 (ok) in the output, ideally with no verify error lines above it. Grep for it: openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | grep 'Verify return code'.

Should the server send the root certificate?
No. The client must already trust the root; sending it wastes handshake bytes and can cause num=19. Serve the leaf followed by the intermediates.

What is the difference between -CAfile, -CApath, and -CAstore?
-CAfile is a single PEM bundle of trusted roots. -CApath is a directory of individual certificates named by subject hash (created by c_rehash). -CAstore is the OpenSSL 3.x store URI form, for example file:/etc/ssl/certs.

Does -showcerts fix the error?
No, it only prints the certificates the server sends. It is a diagnostic, not a fix — but it is the diagnostic that tells you which side to fix.

How do I test a chain without touching the network?
openssl verify -CAfile root.pem -untrusted intermediate.pem leaf.pem. Add -show_chain on OpenSSL 3.x to see the path it built.

What does -partial_chain do?
It lets verification terminate at a trusted intermediate instead of requiring a path all the way to a self-signed root. Useful when you deliberately trust an intermediate, for example in pinned internal deployments.

Is hostname verification covered by this error?
No. num=20 is purely about chain building. Hostname mismatches appear separately as verify error:num=62:hostname mismatch, or via the -verify_hostname option.