Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

nginx answers a client certificate with 400 The SSL certificate error

Your error

400 Bad Request3 more lines it can show

Fix it fast

Most common cause, 1 of 4

The chain has more intermediate CAs than ssl_verify_depth permits

Set the depth to the number of intermediates the chain really has, and keep those intermediates with the root in the CA file.

Fix
ssl_client_certificate /etc/nginx/client-ca.pem;
ssl_verify_client on;
ssl_verify_depth 2;
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

400 The SSL certificate error is nginx's internal code 495: OpenSSL refused the client certificate during verification. The page hides the reason, so log $ssl_client_verify and set error_log to info. Then fix what the number says: a chain deeper than ssl_verify_depth, a missing CA, expired dates or a stale CRL. Covers nginx 1.30 and 1.31.

  • Applies to nginx 1.30, nginx 1.31, OpenSSL 3.0 to 4.1, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Make nginx tell you the reason. $ssl_client_verify holds SUCCESS, FAILED:reason or NONE, and the matching error log line is written at info level, which the default error level hides. Add both, reload, repeat the request and read the number in brackets.

Run this first
# http block
log_format mtls '$remote_addr $ssl_client_verify "$ssl_client_s_dn" "$ssl_client_i_dn"';
access_log /var/log/nginx/mtls.log mtls;
error_log /var/log/nginx/error.log info;

sudo nginx -t && sudo systemctl reload nginx
sudo grep "client SSL certificate verify error" /var/log/nginx/error.log | tail -n 3

Then find the line you see in this list and open its fix:

  1. 1The chain has more intermediate CAs than ssl_verify_depth permitsclient SSL certificate verify error: (22:certificate chain too long)
  2. 2The CA file nginx loads lacks the client certificate's issuerclient SSL certificate verify error: (20:unable to get local issuer certificate)
  3. 3The certificate is outside its validity dates, or the server clock is offclient SSL certificate verify error: (10:certificate has expired) or (9:certificate is not yet valid)
  4. 4The CRL file is out of date, incomplete or revokes the certificateclient SSL certificate verify error: (12:CRL has expired), (3:unable to get certificate CRL) or (23:certificate revoked), with ssl_crl configured

What does the error look like?

  • 400 Bad Request
  • 400 The SSL certificate error
  • client SSL certificate verify error: (20:unable to get local issuer certificate)
  • client SSL certificate verify error: (22:certificate chain too long)

Cause 1 of 4

The chain has more intermediate CAs than ssl_verify_depth permits

You seeclient SSL certificate verify error: (22:certificate chain too long)

Check

Error 22 behind the 400 page means the client chain holds more intermediate CAs than ssl_verify_depth allows. The directive defaults to 1, and OpenSSL's verify depth counts only intermediates, not the client certificate or the trust anchor, so a certificate two intermediates below the root breaks the default. List the issuers to count them.

Check
openssl x509 -in client.crt -noout -subject -issuer
grep -c "BEGIN CERTIFICATE" /etc/nginx/client-ca.pem

Fix

Set the depth to the number of intermediates the chain really has, and keep those intermediates with the root in the CA file.

Fix
ssl_client_certificate /etc/nginx/client-ca.pem;
ssl_verify_client on;
ssl_verify_depth 2;

Verify

After a reload, the next request logs SUCCESS in mtls.log.

Verify
sudo nginx -t && sudo systemctl reload nginx
tail -n 3 /var/log/nginx/mtls.log

Confirmed by docs.openssl.org

Did this fix it?

Cause 2 of 4

The CA file nginx loads lacks the client certificate's issuer

You seeclient SSL certificate verify error: (20:unable to get local issuer certificate)

Check

Error 20 behind the 400 page means nginx's trusted CA file does not contain the CA that issued the client certificate. Client certificates are checked against the PEM file in ssl_client_certificate or ssl_trusted_certificate, so a missing intermediate or root there leaves the issuer unknown. Run the same check from the shell against the same file.

Check
openssl verify -CAfile /etc/nginx/client-ca.pem client.crt

Fix

Add the missing CA certificates in PEM form. If you would rather not send the CA names to every client during the handshake, load them with ssl_trusted_certificate: nginx sends the list from ssl_client_certificate but not that one.

Fix
cat intermediate.pem root.pem >> /etc/nginx/client-ca.pem

# nginx
ssl_trusted_certificate /etc/nginx/client-ca.pem;
ssl_verify_client on;

Verify

openssl verify ends with OK, and the reloaded nginx logs SUCCESS.

Verify
openssl verify -CAfile /etc/nginx/client-ca.pem client.crt
sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

Cause 3 of 4

The certificate is outside its validity dates, or the server clock is off

You seeclient SSL certificate verify error: (10:certificate has expired) or (9:certificate is not yet valid)

Check

Errors 10 and 9 behind the 400 page mean the client certificate's validity dates do not include the nginx host's current time. OpenSSL refuses a certificate whose notAfter has passed or whose notBefore is still ahead, so print both dates next to the host clock.

Check
openssl x509 -in client.crt -noout -dates
date -u

Fix

An expired certificate has to be reissued by its CA. If the dates are fine and the clock is wrong, turn network time synchronisation on.

Fix
timedatectl status
sudo timedatectl set-ntp true

Verify

-checkend 0 reports that the certificate will not expire, and the request reaches the app.

Verify
openssl x509 -in client.crt -noout -checkend 0

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

The CRL file is out of date, incomplete or revokes the certificate

You seeclient SSL certificate verify error: (12:CRL has expired), (3:unable to get certificate CRL) or (23:certificate revoked), with ssl_crl configured

Check

Errors 12, 3 and 23 behind the 400 page come from the revocation list in ssl_crl. nginx reads every CRL from that one PEM file, and with intermediate CAs their lists belong there too, so a list past its nextUpdate date, or a missing list for one intermediate, rejects every certificate below it.

Check
grep -R -n "ssl_crl" /etc/nginx/
openssl crl -in /etc/nginx/client.crl -noout -lastupdate -nextupdate

Fix

Have each CA publish a fresh CRL and combine the root's and every intermediate's list into the one file. A certificate that is listed as revoked needs a replacement, not a config change.

Fix
cat root.crl.pem intermediate.crl.pem > /etc/nginx/client.crl

ssl_crl /etc/nginx/client.crl;

Verify

nextUpdate shows a future date, and requests pass after a reload.

Verify
openssl crl -in /etc/nginx/client.crl -noout -nextupdate
sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Put the CRL's nextUpdate date in your monitoring; a lapsed CRL locks out every client at once.
  • Run openssl verify against the exact CA file nginx loads before giving out a new client certificate.
  • Keep $ssl_client_verify and the certificate subject in the access log on every site that uses client certificates.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, stegnor.com.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.