·7 min read

Under the Hood: What Actually Happens in a TLS Handshake?

A DevOps look at the TLS handshake: ClientHello, chain of trust, Diffie-Hellman, mTLS, TLS 1.3 0-RTT, and how to debug handshake failures in production.

The first time I had to debug a "Handshake Failure" in production, I realized I had no idea what was actually happening behind that green padlock. We treat TLS as "set and forget" in our Ingress controllers, but when things break (or when you're designing a Service Mesh), understanding the handshake is the only way to keep your sanity.

1. The Negotiation: "Who are you and what language do you speak?"

It all starts with the ClientHello. When your browser (or a K8s pod) reaches out, it throws a list of capabilities at the server: the TLS versions it supports, the cipher suites it knows, and the hostname it wants (SNI, which is how one Ingress IP serves a hundred certificates).

The server answers with a ServerHello, picking one version and one cipher suite from that list, based on its own config.

Field Note: If you've ever seen alert protocol version or curl's unsupported protocol, the negotiation died right here: the two sides have no TLS version in common. In a modern stack, lock your Load Balancers to TLS 1.2 and 1.3 only, and expect the old Java 8 client nobody remembers to be the first one to complain.

2. Identity & The "Chain of Trust"

The server then presents its Certificate (plus, if it's configured right, the intermediates). The client doesn't just "trust" the cert. It walks the chain up to a Root CA it already has in its local trust store.

What this means for you: This is why we inject ca-certificates into our Docker images. If your Go binary in a scratch image calls an external API and fails with x509: certificate signed by unknown authority, the chain of trust broke at this exact step. The image simply has no root CAs to check against.

The Fix: Add this to your Dockerfile so the minimal image can verify external certificates:

COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

3. The Math Magic: Diffie-Hellman & Forward Secrecy

How do we agree on an encryption key over an insecure line? Diffie-Hellman (today almost always the elliptic-curve version, ECDHE, usually X25519). Both sides generate a throwaway key pair, exchange the public halves, and each side computes the same shared secret without the secret ever crossing the wire.

The "So What?": Because those DH keys are ephemeral, you get Forward Secrecy. If someone steals your server's private key a year from now, they still can't decrypt traffic they recorded today. The certificate's private key is only used to sign the handshake (prove "it's really me"), not to encrypt the session key.

That's also the big difference from old-school TLS 1.2 with plain RSA key exchange (the TLS_RSA_WITH_... suites). There, the client encrypted the secret with the server's public key, so one leaked private key decrypts every recorded session. TLS 1.3 removed RSA key exchange completely, so every 1.3 connection has forward secrecy.

Short-lived certs in Istio or Linkerd (24h by default) are a different layer of defense: they don't protect old traffic (ECDHE already does that), they shrink the window in which a stolen key can be used to impersonate a workload.

4. TLS 1.2 vs 1.3: what actually changed

The headline is round trips. A full TLS 1.2 handshake takes 2 round trips before the client can send the first byte of HTTP. TLS 1.3 does it in 1.

The trick: in 1.3 the client doesn't wait to be told which DH group to use. It guesses (almost always X25519) and sends its key share right inside the ClientHello. The server answers with its own key share in the ServerHello, and from that moment everything is encrypted, including the server's certificate. If the guess was wrong, the server sends a HelloRetryRequest and you're back to 2 round trips.

Other things you'll notice in practice:

  • Cipher suites got short. A 1.3 suite like TLS_AES_128_GCM_SHA256 only names the encryption and hash. Key exchange and signature algorithm are negotiated separately. That's why in nginx ssl_ciphers doesn't affect TLS 1.3 at all.
  • The certificate is encrypted. In a Wireshark capture of a 1.3 handshake you won't see the cert. For 1.2 you will. If you need to look inside, set SSLKEYLOGFILE=/tmp/keys.log before running curl or a browser and load that file into Wireshark.
  • Gone in 1.3: RSA key exchange, CBC-mode ciphers, renegotiation, compression. A lot of the famous TLS CVEs of the 2010s lived in exactly those parts.

5. mTLS: The Service Mesh Standard

In a standard TLS handshake, only the server proves its identity. In a zero-trust setup that's not enough. In mTLS (Mutual TLS), the server also asks the client for a certificate.

Handshake Flow (TLS 1.3 with mTLS):

  1. Client → Server: ClientHello (versions, cipher suites, SNI, key share)
  2. Server → Client: ServerHello (its key share), then, already encrypted: EncryptedExtensions, CertificateRequest (this is the mTLS part), Certificate, CertificateVerify, Finished
  3. Client → Server: Certificate, CertificateVerify (proves it owns the client cert's private key), Finished
  4. Application data flows.

One round trip, and the server sends its Finished before the client does. In 1.2 it's the other way around and there's an extra round trip in the middle.

When mTLS fails it's usually on the client side: the client didn't send a cert at all, or sent one the server's CA bundle doesn't trust. Depending on the server, you'll see that as a certificate required or bad certificate alert right after the handshake.

6. TLS 1.3: Speed vs. Risk (0-RTT)

TLS 1.3 added 0-RTT (early data). If the client has connected to this server before and kept a resumption ticket, it can send encrypted application data in its very first flight, before the handshake finishes. Fast, but an attacker who captures that first flight can replay it, and the server can't tell the copy from the original.

The Reality Check: If you're behind a CDN that supports 0-RTT, it handles this for you, mostly by being picky about which requests it allows as early data. If you're exposing your origin directly, think twice before enabling it for non-idempotent requests (like POST or DELETE). In nginx it's off by default. If you turn on ssl_early_data on;, pass $ssl_early_data to the backend as an Early-Data header so the app can reject anything it doesn't want replayed (RFC 8470, the app should answer 425 Too Early).

For an internal API, the round trip you save is not worth reasoning about replays. Leave it off.


7. Hands-on: Seeing the Handshake

When a handshake fails, use openssl to see exactly where it died:

openssl s_client -connect api.myapp.com:443 -servername api.myapp.com -showcerts </dev/null

The </dev/null makes it exit right after the handshake instead of waiting for input. -showcerts dumps the full chain the server sent, which is how you catch a missing intermediate.

A few variations I keep coming back to:

# Force a version, to check what the LB actually allows
openssl s_client -connect api.myapp.com:443 -tls1_2 </dev/null
openssl s_client -connect api.myapp.com:443 -tls1_3 </dev/null
 
# Print every handshake message as it goes by (ClientHello, ServerHello, ...)
openssl s_client -connect api.myapp.com:443 -msg </dev/null
 
# Expiry, subject and issuer of the leaf cert, in one line each
openssl s_client -connect api.myapp.com:443 -servername api.myapp.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -dates
 
# Hit one specific backend IP but still send the right SNI / Host
curl -v --resolve api.myapp.com:443:10.0.0.12 https://api.myapp.com/healthz

The SNI gotcha: if you -connect to an IP address, openssl has no hostname to put in SNI. The Ingress then serves you its default certificate (with nginx ingress, the "Kubernetes Ingress Controller Fake Certificate") and you'll chase a cert problem that doesn't exist. Always pass -servername when testing by IP.

With curl -v, the lines starting with * are the TLS story: the negotiated version and cipher, the server cert's subject and expiry, and whether verification passed. Add --tlsv1.3 to require 1.3 as the minimum, or --tls-max 1.2 to cap it.

Common Output Signals (openssl):

  • Verify return code: 0 (ok): the chain of trust is solid.
  • Verify return code: 21 (unable to verify the first certificate): the server sent only its leaf cert. You're almost certainly missing the intermediate in your config (or in the Kubernetes TLS secret).
  • Verify return code: 20 (unable to get local issuer certificate): the chain points to a CA your machine doesn't trust. Common with internal CAs, or a missing intermediate on the client side.
  • alert handshake failure: the server refused the deal. Usually no shared cipher suite (your LB is too strict for this client), sometimes a missing client cert on an mTLS endpoint.
  • Cipher is (NONE): the handshake never completed. If you also see no peer certificate available and zero bytes read, the connection was cut before the server even answered. Look at firewalls, proxies, and whatever sits in front of the port, not at certificates.

8. Automate the cert lifecycle

Stop managing .crt files manually. It's 2026.

  1. Public: cert-manager + Let's Encrypt. It just works.
  2. Internal: HashiCorp Vault or AWS Private CA. Don't expose internal CAs to the public internet.
  3. Observability: Alert on expiry in Prometheus. cert-manager exports certmanager_certificate_expiration_timestamp_seconds for the certs it manages, and blackbox exporter's probe_ssl_earliest_cert_expiry covers everything else, including endpoints you don't own. An expired cert is the single most avoidable SEV-1 there is.

A note on how this was written: I used AI to help with phrasing and structure. The tone, the opinions, the war stories, the code, and everything else here are mine.