TExcelTAD

Client Certificates (mTLS)

In ordinary TLS only the server proves who it is. In mutual TLS (mTLS) the client presents a certificate too, and the server accepts only clients whose certificate was issued by a CA it trusts. It is the standard way to authenticate machines: a payment provider's webhooks, internal service-to-service calls, IoT devices, admin tools reachable only from company laptops. A service mesh (Service Mesh) does the same thing automatically between every pair of pods.

The pieces

Directive Meaning
ssl_client_certificate ca.crt; CA(s) whose client certificates are accepted (their names are also sent to the client as hints)
ssl_verify_client on; a valid client certificate is required; without one, Nginx answers 400
ssl_verify_client optional; ask for one, accept connections without; decide per location with $ssl_client_verify
ssl_verify_depth 2; allow an intermediate CA between the client certificate and the trusted CA
ssl_crl revoked.crl; refuse revoked client certificates

Useful variables: $ssl_client_verify (SUCCESS, NONE or FAILED:reason), $ssl_client_s_dn (the subject), $ssl_client_serial, $ssl_client_fingerprint, and $ssl_client_escaped_cert (the PEM, URL-encoded) to pass the identity to the app.

A session with two kinds of client

The session creates one CA for servers and a separate CA for clients (keep them apart: a client CA that can also issue server certificates is a needless risk), issues a client certificate to billing-service, and protects /internal/ while leaving / public:

bash · try it yourself
ec="-newkey ec -pkeyopt ec_paramgen_curve:P-256"
# server side
openssl req -x509 -new -noenc $ec -keyout server-ca.key -out server-ca.crt -days 365 -subj "/CN=Lab Server CA" 2>/dev/null
openssl req -new -noenc $ec -keyout api.key -out api.csr -subj "/CN=api.example.test" 2>/dev/null
openssl x509 -req -in api.csr -CA server-ca.crt -CAkey server-ca.key -set_serial 1 -days 90 -out api.crt -extfile <(printf 'subjectAltName=DNS:api.example.test\n') 2>/dev/null
# client side: its own CA, and a certificate with clientAuth usage
openssl req -x509 -new -noenc $ec -keyout client-ca.key -out client-ca.crt -days 365 -subj "/CN=Lab Client CA" 2>/dev/null
openssl req -new -noenc $ec -keyout billing.key -out billing.csr -subj "/O=Shop/CN=billing-service" 2>/dev/null
openssl x509 -req -in billing.csr -CA client-ca.crt -CAkey client-ca.key -set_serial 42 -days 30 -out billing.crt -extfile <(printf 'extendedKeyUsage=clientAuth\n') 2>/dev/null
# a certificate from a CA the server doesn't trust
openssl req -x509 -new -noenc $ec -keyout rogue.key -out rogue.crt -days 30 -subj "/CN=billing-service" 2>/dev/null
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log error.log;
events {}
http {
  access_log off;
  server {
    listen 127.0.0.1:8443 ssl;
    server_name api.example.test;
    ssl_certificate api.crt;
    ssl_certificate_key api.key;
    ssl_client_certificate client-ca.crt;
    ssl_verify_client optional;
    location /internal/ {
      if ($ssl_client_verify != SUCCESS) { return 403 "client certificate required ($ssl_client_verify)\n"; }
      return 200 "hello $ssl_client_s_dn (serial $ssl_client_serial)\n";
    }
    location / { return 200 "public page\n"; }
  }
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
c() { curl -s --resolve api.example.test:8443:127.0.0.1 --cacert server-ca.crt "$@"; }
echo "no certificate, /:          $(c https://api.example.test:8443/)"
echo "no certificate, /internal/: $(c https://api.example.test:8443/internal/)"
echo "billing cert, /internal/:   $(c --cert billing.crt --key billing.key https://api.example.test:8443/internal/)"
echo "rogue cert, /internal/:     $(c --cert rogue.crt --key rogue.key https://api.example.test:8443/internal/ | grep -o '<title>.*</title>')"
sed -i 's/ssl_verify_client optional;/ssl_verify_client optional_no_ca;/' nginx.conf
nginx -p "$PWD" -c nginx.conf -e stderr -s reload 2>/dev/null; sleep 0.5
echo "--- with optional_no_ca:"
echo "rogue cert, /internal/:     $(c --cert rogue.crt --key rogue.key https://api.example.test:8443/internal/)"
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null
no certificate, /:          public page
no certificate, /internal/: client certificate required (NONE)
billing cert, /internal/:   hello CN=billing-service,O=Shop (serial 2A)
rogue cert, /internal/:     <title>400 The SSL certificate error</title>
--- with optional_no_ca:
rogue cert, /internal/:     client certificate required (FAILED:self-signed certificate)

Three behaviours to tell apart:

  • No certificate with optional: the request goes through with $ssl_client_verify = NONE, and the location decides (here: 403).
  • An invalid certificate with optional: Nginx rejects the request itself with 400 "The SSL certificate error", before any location runs. optional means "a certificate is optional", not "verification is optional". With on, a missing certificate also gets a 400 ("No required SSL certificate was sent").
  • With optional_no_ca, Nginx accepts any certificate and only reports the result: $ssl_client_verify said FAILED: plus OpenSSL's reason, and the location refused it. Use this mode only when something after Nginx really checks the certificate (for example the app compares fingerprints), since an if you forget lets everyone in.

Note the serial: 2A is 42 in hex. Logging the serial and subject of each client certificate (add them to the access log format) tells you which machine made which call.

Passing the identity to the app

The app behind the proxy should not re-check certificates, but it needs to know who called. Pass it in headers, and make sure clients can't set those headers themselves (the proxy overwrites them on every request):

nginx
location /internal/ {
    proxy_set_header X-Client-Verify $ssl_client_verify;
    proxy_set_header X-Client-DN     $ssl_client_s_dn;
    proxy_set_header X-Client-Cert   $ssl_client_escaped_cert;
    proxy_pass http://app;
}

The app then authorizes by subject or serial ("only CN=billing-service may call /internal/refunds"). Never trust these headers on a port the client can reach directly, bypassing the proxy.

mTLS towards the backends

The reverse direction is just as common: Nginx itself presents a client certificate to a backend that requires mTLS, and verifies the backend's certificate:

nginx
location /payments/ {
    proxy_pass https://payments.internal:8443;
    proxy_ssl_certificate         /etc/nginx/mtls/edge.crt;
    proxy_ssl_certificate_key     /etc/nginx/mtls/edge.key;
    proxy_ssl_trusted_certificate /etc/nginx/mtls/internal-ca.crt;
    proxy_ssl_verify              on;              # off by default: Nginx does NOT check backend certs unless told
    proxy_ssl_name                payments.internal;
    proxy_ssl_server_name         on;              # send SNI
}

proxy_ssl_verify is off by default, so a plain proxy_pass https://… encrypts the traffic but accepts any certificate the backend presents. Turn it on whenever the network between proxy and backend isn't fully trusted.

Operating client certificates

  • Short lifetimes (days to weeks) issued automatically beat long-lived certificates plus revocation lists. Internal CAs such as step-ca, HashiCorp Vault's PKI engine or cert-manager in Kubernetes automate this.
  • If you do revoke, ssl_crl must be refreshed and Nginx reloaded whenever the CRL changes; a stale CRL is an expired one, and Nginx then rejects every client.
  • Browsers can use client certificates too, but the user experience (certificate pickers, installing .p12 files) makes them a poor fit for people; use them for machines.

Test yourself

With ssl_verify_client optional;, a client presents a certificate from an untrusted CA. What happens?

  1. The request reaches the location with $ssl_client_verify set to FAILED:…
  2. Nginx rejects the request with 400 "The SSL certificate error"; only optional_no_ca would let it through to the location
  3. The certificate is accepted
  4. Nginx crashes

Nginx proxies to https://backend with no other settings. Does it verify the backend's certificate?

  1. Yes, always
  2. No: proxy_ssl_verify is off by default
  3. Only for public CAs
  4. Only with HTTP/2

Why pass $ssl_client_s_dn to the backend in a header that the proxy always sets?

  1. For compression
  2. So the app knows which client called and can authorize it, while clients can't forge the header because the proxy overwrites it
  3. To enable caching
  4. Browsers need it