Skip to main content
Back to blog

• Updated

mTLS (mutual TLS): How it works, with diagram

Mutual TLS (mTLS) is TLS where the client and the server each prove who they are with a certificate. In standard TLS, only the server proves itself. In mTLS, the client proves itself right back.

This mutual authentication is what separates mTLS from standard TLS. It’s built for machine-to-machine communication: service meshes like Istio and Linkerd turn it on by default, and API gateways, IoT devices, and zero trust architectures use it too.

What is TLS, and why isn’t it enough?

Before understanding mTLS, it helps to understand what standard TLS does, and what it leaves out.

TLS (Transport Layer Security) is the protocol that secures most internet traffic today. When your browser connects to https://ngrok.com, TLS is doing two things: authenticating the server (via its certificate) and encrypting the connection so nobody can read the data in transit.

That authentication is one-directional. The server proves its identity to you, but you don’t prove your identity to the server. For a public website, that’s fine: the server doesn’t need to know who you are just to serve a web page. Application-layer authentication (username and password) handles that.

For service-to-service communication, the situation is different. There’s no human logging in. TLS already tells Service A that it’s talking to the real Service B. It tells Service B nothing about who’s calling: the request could come from Service A, or from an attacker who has compromised the network. A shared secret or API key helps, but the caller sends it with every request, so a log or a proxy can leak it, and an attacker can replay it. A private key never leaves the service that owns it, and a certificate from a certificate authority that Service B trusts ties that key to Service A’s identity.

That’s the gap mTLS fills.

How does mTLS work?

The mTLS handshake extends the standard TLS handshake by adding a client certificate exchange step. Here’s the full sequence in TLS 1.3:

1. Hellos and key exchange

The client sends a ClientHello with the TLS versions and cipher suites it supports, plus a key share: a one-time public key made for this connection. The server answers with a ServerHello that picks the version and cipher suite and carries its own key share.

Each side combines its own one-time private key with the other’s key share, using a key agreement algorithm such as ECDHE, and arrives at the same secret without ever sending it. The session keys come from that secret. Everything after the ServerHello is encrypted, including both certificates. In TLS 1.2, encryption starts only after the ChangeCipherSpec message near the end of the handshake, so both certificates cross the network unencrypted.

2. The server asks for a certificate and proves itself

The server sends a CertificateRequest, which asks the client to present a certificate. This is the message a standard TLS handshake leaves out.

Then the server sends:

  • Its Certificate, which holds its public key and a signature from a certificate authority (CA).
  • A CertificateVerify, a signature over the handshake so far, made with the private key that matches the certificate.
  • A Finished message, which confirms that nobody tampered with the handshake.

A certificate is a name and a public key, signed by the CA’s private key:

3. The client checks the server

The client checks that:

  • The certificate chains to a CA it trusts, hasn’t expired, and names the host it meant to reach.
  • The signature in CertificateVerify matches the public key in the certificate.

The second check is the one that proves identity. A certificate isn’t a secret, so anyone can copy one. Only the holder of the matching private key can produce the signature. Standard TLS runs this same check, and it’s what stops an attacker from impersonating the server.

4. The client proves itself

The client sends its own Certificate, signed by a CA the server trusts, followed by its own CertificateVerify and Finished. In standard TLS, the client sends only Finished.

5. The server checks the client

The server runs the same checks on the client: the certificate must chain to a CA the server trusts, and the signature must match the certificate’s public key. If the client sent no certificate, a certificate from a CA the server doesn’t trust, or a signature made with another key, a server that requires mTLS sends a TLS alert and closes the connection.

This is the check mTLS adds. An attacker who copies a client’s certificate can’t produce the signature without the client’s private key, and an attacker with their own key pair can’t get a certificate from your private CA.

A TLS 1.3 client can start sending application data right after its Finished, so it may find out about the rejection only when it reads the alert. The server discards anything a rejected client sent, so none of it reaches your application.

If both checks pass, application data flows in both directions, encrypted with the session keys from step 1. Those keys came from one-time key shares, not from the keys in the certificates, so someone who records the traffic and later steals a certificate’s private key still can’t decrypt it.

The mTLS handshake diagram

Put the five steps together and you get the whole handshake. The numbers match the steps above, and the rows in bold blue are the ones mTLS adds to TLS.

Grab the diagram as a PNG, a light SVG, or a dark SVG.

Want to see this handshake succeed and fail for real? Skip to the hands-on walkthrough: you’ll generate a CA and client certificate, put an mTLS-protected endpoint in front of a local service, and watch curl get rejected without a certificate.

mTLS vs TLS: key differences

TLSmTLS
Who authenticatesServer onlyClient and server
Client certificate requiredNoYes
Protects against impersonationServer impersonation onlyBoth sides
Typical use caseBrowser to websiteService to service
Operational complexityLowHigher (certificate lifecycle)
Zero trust compatiblePartialYes

The core trade-off is complexity vs. assurance. Standard TLS is simpler to operate because only server certificates need managing. mTLS requires issuing, rotating, and revoking client certificates too, but in return you get cryptographic proof that both parties are who they claim to be.

mTLS authentication: how certificates work

The foundation of mTLS is X.509 digital certificates. Understanding these explains why mTLS is stronger than shared secrets or API keys.

Certificate authorities (CAs) are the root of trust. A CA is an entity that issues certificates, signing each one with its private key to vouch for the certificate holder’s identity. In an mTLS setup, you run your own private CA, because you don’t want arbitrary clients on the internet to be able to obtain a certificate. Public CAs have also stopped issuing client certificates: Let’s Encrypt, for example, removed the client authentication EKU from its certificates in 2026.

Certificate issuance works like this: a service generates a key pair (public + private), creates a Certificate Signing Request (CSR) containing its public key and identity information, and submits it to the CA. The CA verifies the identity and signs the certificate. The private key never leaves the service.

Certificate verification at handshake time asks: does this certificate chain back to a CA I trust? Is it expired? Has it been revoked?

Revocation is the Achilles heel of certificate-based auth. If a service’s private key is compromised, you need to revoke its certificate quickly. Two mechanisms handle revocation:

  • OCSP (Online Certificate Status Protocol) lets the verifier query the CA in real time for a certificate’s current status.
  • CRL (Certificate Revocation List) is a list of revoked certificates that the CA publishes and verifiers download periodically.

For production mTLS, rotate short-lived certificates that expire every few hours or days automatically. Rotation limits the blast radius of a compromised key without depending on revocation infrastructure being available, or on the verifier checking revocation at all.

What is an mTLS client certificate?

An mTLS client certificate is the certificate a client presents in step 4 of the handshake. It’s an ordinary X.509 certificate: nothing in its format is specific to mTLS. The server checks it in step 5 the same way the client checked the server’s certificate in step 3, except there’s no hostname to match.

What makes it a client certificate is what’s inside it:

  • The subject, or a subject alternative name (SAN), names the client, such as a service, a device, or a customer.
  • The issuer is the CA the server trusts for clients, usually your private CA.
  • The public key matches a private key that stays on the client.
  • The extended key usage (EKU) extension usually lists clientAuth, which marks the certificate for client authentication. A server certificate lists serverAuth instead.

To try one, generate a CA and a client certificate with openssl, then pass it to curl with --cert and --key.

When to use mTLS

mTLS shines in environments where all participants are machines and you can control certificate issuance:

Microservices and service meshes. Service meshes like Istio and Linkerd use mTLS by default for all intra-cluster traffic. Every service gets a certificate, and mutual authentication happens transparently at the sidecar proxy layer.

API gateways. When your API gateway receives requests from partner services or internal clients, mTLS gives you cryptographic proof of which client is connecting. That’s stronger than API keys, which can be shared or leaked. See how ngrok works as an API gateway with mTLS for exactly this pattern.

Zero trust networks. Zero trust security assumes no implicit trust based on network location: every connection must be authenticated regardless of whether it’s internal or external. mTLS is the standard mechanism for enforcing this for machine identities.

IoT devices. Devices in the field can hold a certificate provisioned at manufacture time. When they connect home, mTLS verifies the device is genuine, not an attacker who has taken over the device’s IP address.

Webhooks and callbacks. If your service sends webhooks to customer endpoints, your service is the client. TLS already verifies the customer’s endpoint for you. mTLS adds the other direction: the customer’s endpoint verifies that each webhook came from your service, and rejects anyone else who finds the URL.

mTLS is generally not used for browser-to-server connections, because distributing client certificates to end users at scale is operationally difficult. For human-facing authentication, OAuth 2.0 and OpenID Connect are the standard choice.

mTLS challenges and limitations

Weigh these costs before you roll out mTLS:

Certificate lifecycle management. Every service needs a certificate, and certificates expire. Automating issuance and rotation is critical, because manual certificate management at scale is a source of outages, not security.

Debugging. Encrypted connections with mutual auth are harder to inspect than plain HTTP. Tools like ngrok’s Traffic Inspector help here by capturing the decrypted request/response at the gateway layer before forwarding to your upstream.

CA infrastructure. Running a private CA requires operational care. If an attacker gets the CA’s private key, you can’t trust any certificate it has signed. Keep CA keys in a hardware security module (HSM) or a secrets manager with tight access controls.

Performance. The additional certificate verification during the handshake adds a few milliseconds of latency compared to standard TLS. For long-lived connections this is negligible; for high-frequency short-lived connections it can accumulate.

How to implement mTLS with ngrok

ngrok’s terminate-tls Traffic Policy action puts mTLS in front of any service without changing your application code. ngrok verifies the client certificate in the ngrok cloud, rejects anything not signed by your CA, and forwards only authenticated traffic to your upstream. It’s the same building block behind ngrok’s secure customer connectivity, where mutual TLS authenticates both ends of a private link.

The whole thing takes about five minutes. You’ll need openssl, curl, and an ngrok account on the Pay-as-you-go plan, which is where mTLS is available. Its monthly fee counts as a credit against usage, so trying this costs effectively nothing.

1. Generate a CA and a client certificate

If you already run a private CA, skip this and use your existing CA certificate.

# CA private key and self-signed CA certificate
openssl genpkey -algorithm RSA -out ca.key -pkeyopt rsa_keygen_bits:2048
openssl req -x509 -new -nodes -key ca.key -sha256 -days 365 -out ca.crt -subj "/CN=ExampleCA"
 
# Client private key, signing request, and CA-signed client certificate
openssl genpkey -algorithm RSA -out client.key -pkeyopt rsa_keygen_bits:2048
openssl req -new -key client.key -out client.csr -subj "/CN=ExampleClient"
printf 'extendedKeyUsage = clientAuth\n' > client.ext
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
  -out client.crt -days 365 -sha256 -extfile client.ext

2. Write the Traffic Policy

Paste the contents of ca.crt into policy.yml. The min_version line accepts only TLS 1.3, the handshake this post describes. The second rule is optional: it passes the verified client’s identity to your upstream as a header, which ngrok doesn’t do on its own.

on_tcp_connect:
  - actions:
      - type: terminate-tls
        config:
          min_version: "1.3"
          mutual_tls_certificate_authorities:
            - |-
              -----BEGIN CERTIFICATE-----
              <CONTENTS_OF_CA_CRT>
              -----END CERTIFICATE-----
on_http_request:
  - actions:
      - type: add-headers
        config:
          headers:
            x-client-cn: "${conn.tls.client.subject.common_name}"

3. Start an mTLS-protected endpoint

Point ngrok at any local service, for example one on port 8080:

ngrok http 8080 --traffic-policy-file policy.yml

4. Test it

Use the URL ngrok prints. Without a client certificate, the handshake fails before any request reaches your service:

curl https://<YOUR_NGROK_URL>

With the certificate signed by your CA, the request goes through, and your service sees x-client-cn: ExampleClient:

curl --cert client.crt --key client.key https://<YOUR_NGROK_URL>

That’s mutual TLS end to end: your CA decides who gets in, and the ngrok network enforces it.

ngrok doesn’t check CRLs or OCSP, so revoking a client certificate at your CA doesn’t block it at ngrok. To cut off a leaked certificate, deny its serial number with a Traffic Policy rule on conn.tls.client.serial_number, or replace the CA and reissue every client certificate. Short-lived client certificates limit how long a leaked one stays useful.

For uploading your CA to the dashboard instead of inlining it, matching on other certificate fields, and terminating mTLS at the agent instead of in the ngrok cloud, see the mTLS termination docs. To put mTLS in front of an API, see mTLS for API gateways.

Ready to try it? Create an ngrok account and you’ll have an mTLS-protected endpoint running in minutes.

Frequently asked questions

Answers come from ngrok.com and the ngrok docs, and can be wrong. Check the linked pages before you rely on one.

Avatar for Team ngrok

Team ngrok

This author has not provided a bio yet.

Share this post