Skip to main content
apiguide.dev

mTLS & Client Certificates

How mutual TLS authenticates both sides of an API connection, when to use it over bearer tokens, and how to manage certificate rotation and validation.

Topic:API Security

Introduction to Mutual TLS

Standard TLS authenticates the server to the client. Mutual TLS (mTLS) goes further: both parties present and verify X.509 certificates during the handshake, so each side cryptographically proves its identity before any application data flows. This makes mTLS a foundation for service-to-service communication and zero-trust architectures, where every service must authenticate regardless of network location.

This guide covers how the mTLS handshake works, common implementation patterns, certificate rotation and validation, and how mTLS compares with — and complements — bearer tokens. For token-based approaches, see authentication and OAuth & API keys; for the wider picture, see API security.


1. What mTLS Adds Over Standard TLS

In ordinary TLS the client verifies the server, but the server has no cryptographic assurance of the client — it relies on application-layer mechanisms like API keys or bearer tokens for that. mTLS shifts authentication from “prove you know a secret” to “prove you possess a private key.”

  • Bidirectional authentication: both client and server present X.509 certificates.
  • Strong machine identity: certificates are bound to infrastructure, unlike static keys that can be copied or leaked.
  • Non-repudiation and audit: certificate attributes identify the calling workload.

Relevant standards include TLS 1.2 (RFC 5246) and TLS 1.3 (RFC 8446), the X.509 certificate profile (RFC 5280), and RFC 8705 for OAuth mTLS client authentication and certificate-bound tokens.


2. The mTLS Handshake

mTLS extends the standard TLS handshake with client certificate exchange.

  1. The server presents its certificate, then sends a CertificateRequest message.
  2. The client responds with its own X.509 certificate.
  3. The client sends a CertificateVerify message — a digital signature proving it holds the private key for that certificate.
  4. The server validates the client certificate against its trusted CA store, checking the chain, expiry, and optionally revocation.

This adds roughly 1-2 milliseconds of latency over standard TLS. That overhead can be amortized with connection pooling, keep-alives, and TLS session resumption.


3. Implementation Patterns

Service-to-service communication

mTLS is the predominant mechanism for securing internal service-to-service traffic in microservices and zero-trust environments. Service meshes such as Istio, Linkerd, and Consul Connect automate certificate issuance, rotation, and encryption transparently at the infrastructure layer, with no application code changes.

API gateway integration

Gateways such as AWS API Gateway and Azure API Management can enforce mTLS at the edge. They validate a client certificate against a configured truststore (a collection of trusted CA certificates) and can forward certificate details to backend services or authorizers for granular decisions.

GET /v1/orders HTTP/1.1
Host: api.apiguide.dev
X-Client-Cert-Subject: CN=payments-service,O=Example

A frequent pitfall: a reverse proxy or load balancer terminates TLS and does not forward the client certificate to the backend. The usual workaround is to pass the certificate (or its verified attributes) in a custom HTTP header — but the backend must then trust that only the proxy can set it.

PKI management

mTLS depends on a Public Key Infrastructure that issues, distributes, and revokes certificates. Tools like cert-manager (Kubernetes), HashiCorp Vault, and cloud KMS/Key Vault services automate the certificate lifecycle, which is what makes large deployments tractable.


4. Certificate Rotation

Automated, short-lived certificates are a critical best practice. Long-lived certificates reduce operational churn but increase the blast radius if a private key is compromised.

  • Workload identities can use very short lifetimes — SPIFFE/SPIRE recommends around 1 hour, with rotation handled by an agent.
  • External partner certificates are typically longer (days to months) but still rotated frequently.
  • Public TLS certificate maximum lifetimes are shrinking under CA/Browser Forum timelines, reinforcing the move to automation.

Use overlapping validity periods: issue and deploy the new certificate while the old one is still valid so connections transition without interruption. Gateways and meshes that validate against multiple certificates concurrently allow rotation on live APIs without downtime. Generate a fresh private key on each renewal, and keep a centralized inventory to avoid unmanaged “ghost” certificates. Without automation, expiry-driven outages are common.


5. Certificate Validation

Thorough validation is essential — a certificate that merely parses is not a trusted one.

Check Purpose
Chain validation Verify the chain up to a trusted root CA
Expiry Ensure the certificate is within its validity period
Revocation Check OCSP or CRLs; use soft-fail if the responder is unreachable
Identity Match the subject (CN or SAN) to the expected identity

Common pitfalls

  • Improper certificate extraction: some libraries return an array of certificates while the TLS layer only verifies the first. Iterating and trusting the wrong entry has led to real authentication bypasses (for example CVE-2023-2422 in Keycloak). Authenticate using the certificate the TLS layer actually verified.
  • Injection from certificate fields: unescaped subject or SAN values placed into LDAP or SQL queries can enable injection (for example CVE-2023-33201 in Bouncy Castle).
  • Untrusted revocation endpoints: preferring a CRL/OCSP URL taken from the certificate’s own extensions can leak credentials or enable SSRF.

Because handshake failures surface as opaque errors like “TLS handshake failed,” instrument metrics for handshake success and failure and log the specific validation error to make debugging tractable.


6. mTLS vs Bearer Tokens

The two are not mutually exclusive; they solve different problems.

Bearer tokens (such as OAuth 2.0 access tokens) are simple, scalable, and carry fine-grained scopes, but they are vulnerable to theft and replay — anyone holding a valid token can use it, because the resource server cannot verify the sender.

mTLS provides strong transport-layer machine identity but offers coarse, certificate-level access control and carries more operational complexity.

mTLS Bearer tokens
Identity Certificate possession Token possession
Granularity Coarse (certificate) Fine (scopes, claims)
Replay resistance High (key possession) Low unless sender-constrained
Best fit Internal, high-assurance, regulated Dynamic, multi-tenant, edge

Certificate-bound tokens (holder-of-key)

RFC 8705 combines the strengths of both. The authorization server binds an access token to the client’s mTLS certificate by embedding a SHA-256 thumbprint of the certificate in the token, using the x5t#S256 member of the JWT cnf claim (or the introspection response for opaque tokens).

{
"sub": "payments-service",
"scope": "orders:read",
"cnf": { "x5t#S256": "bwcK0esc3ACC3DB2Y5_lESsXE8o9ltc05O89jdN-dg2" }
}

The resource server compares the certificate thumbprint from the live mTLS session against the value in the token; a mismatch is rejected. This makes the token useless to anyone who does not also hold the private key, defeating token replay and man-in-the-middle attacks. RFC 8705 also defines two client authentication methods: tls_client_auth for CA-issued certificates and self_signed_tls_client_auth for self-signed certificates registered with the server.

The optimal pattern for many systems: mTLS for internal service-to-service identity, OAuth at the edge for user-aware authorization, and certificate-bound tokens to tie them together. Open Banking and Financial-grade API (FAPI) profiles frequently mandate exactly this model.


Summary

mTLS delivers strong, bidirectional authentication at the transport layer and is the natural fit for service-to-service traffic and zero-trust designs. Its power comes with operational cost, so lean heavily on automation, short-lived certificates with overlapping validity, rigorous validation, and observability. For granular, user-aware access control, pair mTLS with OAuth and certificate-bound tokens rather than choosing one over the other.