How-To Guide

How to Implement Client Certificate Authentication

Secure service-to-service and device authentication with mutual TLS: certificate issuance, verification, rotation and revocation.

On this page

  1. Introduction
  2. How mTLS Differs from Server-Only TLS
  3. Issuing Certificates
  4. Verifying on the Server
  5. Rotation and Short-Lived Certificates
  6. Revocation
  7. Authorisation, Not Just Authentication
  8. Common Pitfalls and Conclusion

Introduction

When two services or a fleet of devices talk to each other, passwords and API keys are awkward: they must be distributed, stored and rotated, and a single leaked key compromises a whole class of clients. Mutual TLS (mTLS) instead authenticates both ends with certificates. The server proves its identity to the client, and the client proves its identity to the server by presenting a certificate during the TLS handshake. Since the trust is anchored in a certificate authority, revocation and rotation have well-defined mechanics instead of ad-hoc key rotation.

How mTLS Differs from Server-Only TLS

In ordinary TLS only the server presents a certificate, so the client knows who it is talking to but the server does not know the client. In mTLS the server requests a client certificate during the handshake, and only accepts the connection if the client presents one that chains to a trusted CA. Authentication happens at the transport layer, before any application protocol runs.

This is attractive for service-to-service calls because the identity is established without an application-level credential, and it scales to many clients via a CA rather than distributing a shared secret to each. It is also common for device authentication, where each device gets a certificate at manufacture or first provisioning.

# handshake comparison
server-only TLS:  client -> [verify server cert] -> server
mTLS:             client -> [verify server cert] -> server
                  client <- [request client cert] <- server
                  client -> [present client cert] -> server verifies against CA
# identity established at the transport layer, per connection

Issuing Certificates

Operate a certificate authority that signs client certificates. In practice this is often a managed PKI or an internal CA tool, but the mechanics are that each client generates a key pair, sends a certificate signing request with its identity, and the CA issues a certificate binding that identity to the public key. The CA’s certificate is what servers trust.

Put the identity in the certificate in a form your services can authorise against: a SPIFFE id, a service name, or an email. The subject and subject alternative names are what you will read and check. Keep the CA hierarchy clear, with a root that is offline and intermediates that do the day-to-day signing, so a compromised intermediate can be replaced without changing the root of trust.

# Client generates a key and CSR, CA signs it.
openssl ecparam -genkey -name prime256v1 -out client.key
openssl req -new -key client.key -subj "/CN=orders-api/O=example" -out client.csr
openssl x509 -req -in client.csr -CA intermediate.crt -CAkey intermediate.key \
  -CAcreateserial -days 30 -out client.crt
# server trusts intermediate.crt; verifies the client chain on every handshake

Verifying on the Server

Configure the server to request and require a client certificate, and to verify it against the trusted CA. A certificate that does not chain to the CA is rejected at the handshake, so unauthenticated clients cannot even open a connection. The application then reads the verified identity from the connection and authorises it against its policy.

The identity must come from the verified certificate, not from a header the client sets. A reverse proxy that terminates mTLS should forward the verified identity in a header it controls, and the backend must trust that header only from the proxy, typically by network isolation or a shared secret. Trusting a client-supplied header is the classic mTLS bypass.

server {
  listen 443 ssl;
  ssl_certificate     /etc/tls/server.crt;
  ssl_certificate_key /etc/tls/server.key;
  ssl_client_certificate /etc/tls/intermediate.crt;  # trusted CA
  ssl_verify_client on;                               # require mTLS
  ssl_verify_depth 2;

  location / {
    proxy_set_header X-Client-Verify  $ssl_client_verify;  # proxy-controlled
    proxy_set_header X-Client-Subject $ssl_client_s_dn;    # verified identity
    proxy_pass http://backend;
  }
}

Rotation and Short-Lived Certificates

Long-lived client certificates are a liability: a stolen key works for years. Modern practice is short-lived certificates, often hours or days, renewed automatically via an agent that has a bootstrap credential or a workload identity. Short lifetimes mean revocation is less critical, because a leaked certificate expires soon.

Automate renewal well before expiry, and make the client able to hold two certificates during the overlap so a renewal never causes downtime. Monitor certificate expiry as a first-class alert, because an expired client certificate fails every connection and is an outage that is entirely predictable and entirely avoidable.

# rotation timeline
t0        issue cert A (24h)
t0+22h    request cert B (overlap begins)
t0+22h    client can present A or B
t0+24h    A expires; client presents B
t0+46h    request cert C ...
# alert if time-to-expiry < renewal_window for any active cert

Revocation

Revocation is the hard part of any PKI. The two mechanisms are CRL (a signed list of revoked serials) and OCSP (an online check per certificate). Both add latency or infrastructure. With short-lived certificates, revocation becomes less important because the natural expiry limits the exposure; many internal systems rely on short lifetimes instead of OCSP.

Where you do need revocation, prefer OCSP stapling or a cached CRL so verification does not depend on an online responder for every handshake. Treat a failed revocation check deliberately: fail closed for high-value access, which risks availability if the responder is down, or fail open with a short cache, which risks accepting a revoked certificate briefly.

# revocation options
CRL        signed list; needs distribution + caching; can be stale
OCSP       per-cert online check; adds a round trip; responder SPOF
stapling   server attaches a signed OCSP response; no client round trip
short-lived certs  expiry is the revocation; simplest at scale
# pick per threat model; document the fail-open/closed behaviour

Authorisation, Not Just Authentication

mTLS proves identity; it does not grant permission. A certificate that proves “orders-api” must still be authorised for the endpoint it is calling. Map the certificate identity to a policy that says which operations it may perform, and enforce that per request, not per connection. This prevents a valid but low-privilege service from calling an admin endpoint.

Distinguish authentication (who) from authorisation (what) explicitly, and log both. An mTLS connection that succeeds with a certificate your policy does not recognise should be rejected by authorisation, with a log entry naming the subject, rather than silently allowed because the handshake succeeded.

function authorise(req: Request, peerSubject: string, policy: Policy) {
  if (!policy.isKnownService(peerSubject)) {
    throw new Error('unknown_service: ' + peerSubject);   // authenticated but not authorised
  }
  if (!policy.canCall(peerSubject, req.method, req.path)) {
    throw new Error('forbidden: ' + peerSubject + ' -> ' + req.path);
  }
  return true;
}

Common Pitfalls and Conclusion

Do not trust a client-supplied identity header; use the verified certificate only. Do not issue long-lived certificates without a rotation plan. Do not ignore expiry alerting. Do not confuse authentication with authorisation. Do not leave the CA key online. And do not assume mTLS protects against a compromised host that legitimately holds a key; it protects the channel and the fleet identity, not a fully compromised node.

mTLS moves service and device authentication to the transport layer, anchored in a CA. Issue certificates with clear identities, require and verify the client certificate, read the identity only from the verified chain, use short-lived certificates with automatic renewal, and decide revocation policy deliberately. Combined with a real authorisation policy, it gives strong, rotatable, fleet-wide identity without shared secrets.

Sources: RFC 8705: OAuth 2.0 Mutual-TLS, NGINX: Client certificate verification, SPIFFE: Workload identity.

FAQ

How do I know the client identity on the backend?
From the verified certificate chain, forwarded by the mTLS-terminating proxy in a header the proxy controls, not a header the client sets. The backend must trust that header only from the proxy, enforced by network isolation or a shared secret.
Do I need OCSP if I use short-lived certificates?
Usually not. Short-lived certificates expire quickly, so the exposure window is small and natural expiry acts as revocation. If you do need immediate revocation, prefer OCSP stapling or a cached CRL over a per-handshake online check.

Related Examples