TLStlshandshakeclient helloserver helloecdhe

The TLS Handshake

Before the first encrypted byte, client and server negotiate a version and cipher suite, the server proves its identity with a certificate and a signature, and both derive the same session key from an ephemeral key exchange — one round trip in TLS 1.3, two in TLS 1.2, on top of TCP’s own.

Conceptual
▶ InteractiveInterview question
Progress
Before this, know:The Three-Way Handshake

You have not marked these as done yet. Read on anyway if they feel familiar; come back to them if something here does not land.

The problem

Two machines that have never met must agree on a secret key over a channel where every byte can be read, and one of them must prove it is the real api.example.com — using as few round trips as possible, because each one costs a full RTT before the application can send anything.

Progressive depth

The same mechanism at different altitudes — start where you are.

Agree on a key, prove who you are

The client says what it can do, the server picks and proves its identity with a certificate, both sides derive the same secret, and everything after that is encrypted. One extra round trip before the first request.

The shape of the handshake

Conceptual

The handshake is a negotiation followed by a proof. The client opens with a Client Hello: the highest TLS version it supports, a list of cipher suites in preference order, the hostname it wants (SNI, so a server hosting many sites can pick the right certificate), the application protocols it can speak (ALPN: h2, http/1.1, h3), and — in TLS 1.3 — a key share: a fresh ephemeral ECDHE public value, sent speculatively before it knows what the server will pick.

The server answers with a Server Hello choosing the version and suite, plus its own key share. At that moment both sides can compute the same shared secret, and in TLS 1.3 everything after the Server Hello is already encrypted with keys derived from it. The server then sends its Certificate chain, a CertificateVerify — a signature over the handshake transcript made with the certificate’s private key, which is what proves the server holds that key — and Finished, a MAC over the transcript that proves the negotiation was not tampered with.

The client validates the chain against its trust store (see Certificates and the Chain of Trust), checks the hostname, verifies the signature, verifies Finished, and sends its own Finished. From then on both sides exchange application data in encrypted records. Client authentication (mTLS) adds a CertificateRequest and a client certificate in the same flight; it is common between services, rare between browsers and websites.

TLS 1.3 full handshake
→←okClient Hello: versions, suites, SNI, ALPN, key_shareServer Hello + key_share{Certificate, CertificateVerify, Finished} — encryptedClient: validate chain, hostname, signatureClient Finished (+ first request in the same flight)Application data both ways
UserLLMAgentToolDataDecisionHumanGuardrail

Versions and cipher suites change the details

Everything above is the TLS 1.3 flow; the exact messages depend on the version and the negotiated suite, and it is a mistake to describe "the" TLS handshake without saying which. In TLS 1.2 the client does not send a key share up front. It sends Client Hello, waits for Server Hello + Certificate + ServerKeyExchange (the server’s ECDHE value, signed) + ServerHelloDone, then sends ClientKeyExchange + ChangeCipherSpec + Finished, and only after the server’s ChangeCipherSpec + Finished can it send application data. That is two round trips before the first request byte, versus one for 1.3.

TLS 1.2 also allowed suites where the key exchange was RSA — the client encrypted a random premaster secret with the server’s public key. No ephemeral key means no forward secrecy: a stolen server key decrypts every recorded session. TLS 1.3 removed static RSA, removed every suite without AEAD (CBC-mode, RC4), removed renegotiation and compression, and cut the suite list to five names of the form TLS_AES_128_GCM_SHA256 — the key exchange and signature are negotiated separately via extensions rather than baked into the suite name.

Cipher suite strings therefore read differently by version. A 1.2 suite ECDHE-RSA-AES128-GCM-SHA256 names key exchange, authentication, cipher and hash; a 1.3 suite TLS_CHACHA20_POLY1305_SHA256 names only cipher and hash. When you see TLS_RSA_WITH_AES_256_CBC_SHA in a scan, that is a 1.2-era suite with no forward secrecy and a non-AEAD cipher — a finding, not a curiosity.

Handshake cost by version (full handshake, on an established TCP connection)
VersionRound trips before application dataForward secrecyEncrypted handshakeResumption
TLS 1.22 RTTOnly with (EC)DHE suites; RSA key exchange has noneNo — certificate sent in the clearSession IDs or tickets; 1-RTT abbreviated handshake, no 0-RTT
TLS 1.31 RTTAlways (ephemeral key share mandatory)Yes — everything after Server HelloPSK via NewSessionTicket; 1-RTT, optionally 0-RTT early data

Resumption, 0-RTT and its replay caveat

A full handshake costs an RTT and the asymmetric operations; a client returning to the same server should not pay again. After a 1.3 handshake the server sends one or more `NewSessionTicket` messages carrying an opaque, server-encrypted ticket. The client presents it as a pre-shared key in its next Client Hello and skips certificate transmission and verification; the handshake is still 1 RTT, and with psk_dhe_ke mode a fresh ECDHE exchange still happens so forward secrecy holds. TLS 1.2 had session IDs (server-side cache) and session tickets (RFC 5077) with an abbreviated 1-RTT handshake.

TLS 1.3 goes one step further: with a PSK the client may send 0-RTT early data — application bytes in the very first flight, encrypted under a key derived from the ticket, before any server response. The saving is an entire RTT for the first request. The cost is that early data is replayable: an attacker who captured the first flight can resend it and the server will decrypt and process it again, because nothing from the server has yet contributed freshness. 0-RTT is therefore safe only for requests that are idempotent by construction. Browsers and CDNs restrict it to GET without a body, and a well-configured server either rejects early data for non-idempotent paths or is engineered so that replaying them is harmless.

Tickets have a lifetime (hours to a week) and are bound to the server’s ticket-encryption key; rotating that key invalidates outstanding tickets, and a fleet behind a load balancer must share it or resumption silently fails and every connection pays a full handshake — visible as a jump in handshake CPU and latency after a deploy.

  • Full 1.3 handshake: 1 RTT + one signature verification + one ECDHE. Resumed: 1 RTT, no certificate, optional ECDHE. 0-RTT: request in the first flight, replayable.
  • HTTP/3 uses the same TLS 1.3 machinery and the same 0-RTT rules — see HTTP/3 and QUIC.
  • Early-Data: 1 is the header a proxy adds so an origin can refuse to act on a replayable request (HTTP 425 Too Early).

What it costs on top of TCP

Unix-style

TLS runs over an established TCP connection, so its RTTs add to the The Three-Way Handshake. On a cold connection to a server 100 ms away: 1 RTT for SYN/SYN-ACK, then 1 RTT for TLS 1.3 (2 for 1.2), then 1 RTT for request and first byte of response — roughly 300 ms before any application data arrives with 1.3, 400 ms with 1.2, before a single byte of the page has been transferred. Cross-continent RTTs of 150–250 ms turn that into over half a second, which is the whole motivation for Keep-Alive and Connection Reuse and Connection Pooling (do the handshake once) and for QUIC (fold the transport and TLS handshakes into one RTT).

The CPU cost lands almost entirely in the handshake: a P-256 ECDHE plus an RSA-2048 signature verification is on the order of 100–300 µs on a modern core; an ECDSA P-256 server certificate makes the server’s signing side roughly ten times cheaper than RSA, which is why large deployments prefer ECDSA certificates. Once keys are established, AES-GCM with hardware support encrypts at several gigabytes per second per core — bulk encryption is rarely the bottleneck; handshake rate under a connection storm frequently is.

A trimmed `openssl s_client -connect example.com:443 -servername example.com` transcript
CONNECTED(00000003)
depth=2 C = US, O = DigiCert Inc, CN = DigiCert Global Root G2
verify return:1
depth=1 C = US, O = DigiCert Inc, CN = DigiCert Global G2 TLS RSA SHA256 2020 CA1
verify return:1
depth=0 C = US, ST = California, O = Internet Corporation for Assigned Names and Numbers, CN = www.example.org
verify return:1
---
Certificate chain
 0 s:CN = www.example.org
   i:CN = DigiCert Global G2 TLS RSA SHA256 2020 CA1
 1 s:CN = DigiCert Global G2 TLS RSA SHA256 2020 CA1
   i:CN = DigiCert Global Root G2
---
SSL handshake has read 4512 bytes and written 393 bytes
Verification: OK
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 2048 bit
Protocol: TLSv1.3
Verify return code: 0 (ok)
---
Post-Handshake New Session Ticket arrived:
    Resumption PSK: ...
    TLS session ticket lifetime hint: 86400 (seconds)

Key points

  • Client Hello offers versions, suites, SNI, ALPN and (in 1.3) an ephemeral key share; Server Hello picks and replies with its own share, and from then on the handshake is encrypted.
  • The certificate proves the binding of hostname to public key; CertificateVerify proves the server holds the matching private key; Finished proves the transcript was not altered.
  • TLS 1.3 is 1 RTT, TLS 1.2 is 2 RTT; the exact messages depend on the version and cipher suite.
  • Forward secrecy comes from ephemeral ECDHE; TLS 1.3 makes it mandatory by removing static RSA key exchange.
  • Resumption via tickets/PSK skips certificate work; 0-RTT early data saves a full RTT but is replayable and must be limited to idempotent requests.
  • TLS RTTs stack on top of TCP’s: a cold HTTPS request is ≥ 3 RTTs to first byte with 1.3, which is why connection reuse matters so much.

Why does this exist?

Mechanisms are answers to constraints. Open each question before reading the answer.

▸Why does the client send a key share before it knows what the server supports?

To save a round trip. TLS 1.3 clients guess the server will accept X25519 or P-256 and send a share for it speculatively; if the server wants a different group it replies with HelloRetryRequest and the handshake costs one extra RTT — a rare path in practice.

▸Why is the certificate encrypted in TLS 1.3 but not in 1.2?

Because in 1.3 the key exchange completes at the Server Hello, so handshake keys exist before the certificate is sent. Hiding the certificate from passive observers reduces what the path learns about who you are talking to; SNI in the Client Hello still leaks the hostname unless ECH is used.

▸Why is 0-RTT data replayable when the rest of the handshake is not?

Anti-replay needs a value the attacker cannot predict contributed by the receiver — the server’s random or key share. In the first flight the server has contributed nothing yet, so the only defence is the application: idempotent requests only, or server-side single-use ticket tracking.

▸Why does resumption still do an ECDHE exchange?

A PSK alone means a leaked ticket key decrypts every resumed session. Mixing in a fresh ephemeral exchange (psk_dhe_ke) keeps forward secrecy at the cost of one cheap key agreement.

TLS handshake

TLS handshake
What each side sends, what is encrypted from which message on, and how many round trips stand between a SYN and the first byte of the response.
Version
Simulated
client (browser)server engineer-atlas.dev
  1. ClientHello versions[1.3,1.2] suites[AES_128_GCM_SHA256, CHACHA20_POLY1305] SNI=engineer-atlas.dev key_share(x25519)▶
  2. ◀ServerHello suite=TLS_AES_128_GCM_SHA256 key_share(x25519)
  3. ◀{EncryptedExtensions}
  4. ◀{Certificate} leaf + intermediate
  5. ◀{CertificateVerify} sig(transcript) with the leaf private key
  6. ◀{Finished} HMAC(transcript)
  7. client validates the chain (hostname, dates, signatures up to a trusted root), verifies CertificateVerify, derives application traffic keys
  8. {Finished}▶
  9. [GET / HTTP/1.1] application data▶
  10. ◀[200 OK] application data
Plaintext. The client guesses the key-exchange group and sends its public key share right away — that is what saves a round trip. SNI is visible to anyone on the path (Encrypted ClientHello is the fix).
Time to first byte = TCP + TLS + request
TLS 1.2 · 2-RTT4 RTT · 200 ms
TLS 1.3 · 1-RTT3 RTT · 150 ms
TLS 1.3 resumption · 0-RTT2 RTT · 100 ms
TCPTLSrequest
Encrypted from which message on
ClientHello, ServerHello
plaintext (SNI, suites, key shares visible)
EncryptedExtensions → Finished
handshake keys (ECDHE-derived)
Application data
application traffic keys
The exact messages depend on the version and the negotiated cipher suite (RSA vs ECDHE key exchange in 1.2, PSK-only vs PSK-ECDHE resumption in 1.3, client certificates add a round of messages). The structure — hello, key exchange, authenticate, Finished — is the same. TLS 1.3 removed RSA key exchange, static DH and renegotiation, which is why it can be both faster and simpler.
1/10 · TLS 1.3 · 1-RTT

How it fails

What the failure looks like from inside real software.
Reference · show
  • SSL_ERROR_HANDSHAKE_FAILURE_ALERT / handshake failure: no cipher suite or version in common — a client pinned to TLS 1.0 or a server configured with only 1.3 suites the client library does not know.
  • Every request pays a full handshake: keep-alive is disabled or ticket keys are not shared across the fleet, so resumption never succeeds; handshake CPU and p50 latency both climb.
  • 0-RTT replay: a POST accepted as early data is replayed by an attacker and processed twice; the fix is refusing non-idempotent early data (HTTP 425) or disabling 0-RTT.
  • Wrong certificate served because SNI was not sent — a client connecting by IP or an old library gets the server’s default certificate and fails hostname validation.
  • Handshake timeouts under load: the server’s accept queue fills with connections waiting for the CPU-bound signature step; symptoms are ETIMEDOUT during connect rather than during the request.
  • A middlebox that cannot parse a large Client Hello (post-quantum key share, ECH) drops it silently; the connection hangs at the first flight and works only with a smaller Hello.

Check yourself

Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.

  1. Walk me through a TLS handshake. What does each side learn, how many round trips does it cost, and what changed between TLS 1.2 and TLS 1.3?

    Show a strong answer

    A handshake has to do three things before a byte of HTTP flows: authenticate the server (prove the public key in its certificate belongs to the name the client asked for — see Certificates and the Chain of Trust), agree on a shared secret that no observer can derive, and negotiate algorithms (key exchange, signature, symmetric cipher) plus extras such as the application protocol via ALPN (h2, http/1.1) and the hostname via SNI so one IP can serve many certificates. Everything before the shared secret exists is in the clear.

    TLS 1.2 takes two round trips. ClientHello (versions, cipher suites, random, SNI, ALPN) → ServerHello + Certificate + ServerKeyExchange (its ephemeral ECDHE public value, signed with the certificate’s key) + ServerHelloDone → ClientKeyExchange (the client’s ECDHE value) + ChangeCipherSpec + Finished → server ChangeCipherSpec + Finished. Both sides derive the same secret from the ECDHE exchange; the Finished messages are MACs over the whole transcript, so any tampering with the negotiation is detected. 1.2 also allowed RSA key exchange (client encrypts a secret to the server’s public key), which has no forward secrecy — compromise the key later, decrypt every recorded session.

    TLS 1.3 takes one round trip because the client *guesses*: its ClientHello already carries a key_share for a likely group (usually X25519). The server replies with ServerHello + its own key_share, and from that point everything — EncryptedExtensions, Certificate, CertificateVerify (a signature over the transcript proving possession of the private key), Finished — is already encrypted. The client sends Finished and application data in the same flight. If the guess was wrong the server sends HelloRetryRequest and it costs a second round trip. 1.3 also removed RSA key exchange, static DH, renegotiation, compression and every non-AEAD cipher, so the negotiation surface is small enough that downgrade tricks have little to attack. With a resumption PSK, 1.3 can send 0-RTT data in the first flight — replayable, so only for idempotent requests.

    The cost compounds with TCP: one RTT for the handshake (The Three-Way Handshake), plus one (1.3) or two (1.2), so a cold HTTPS request across a 150 ms path spends 300–450 ms before the first HTTP byte. That is why keep-alive, session resumption and TLS termination close to the user matter more than the cipher choice. Failures are legible: an alert such as handshake_failure (no common cipher/version) or protocol_version, a certificate alert after the Certificate message (client rejected it), a hostname mismatch when SNI was missing and the server presented a default certificate, or a silent hang when a middlebox drops something it does not understand. See The TLS Handshake and TLS Debugging: Why the Certificate Is "Invalid".

    Full interview question, with follow-ups and red flags →
  2. Explain the TCP three-way handshake. Why three packets, what state exists on each side, and what can go wrong before a byte of data is sent?

    Show a strong answer

    Each side needs to tell the other its initial sequence number and learn that the other heard it. The client sends SYN with ISN *x*; the server replies SYN-ACK — its own ISN *y*, and ack *x+1* to prove it received the SYN; the client sends ACK *y+1*. Three packets is the minimum for both sides to have their ISN acknowledged — two would leave the server unsure the client received its ISN. The SYN also carries options that shape the whole connection: MSS, window scaling, SACK permitted, timestamps. ISNs are randomised so old duplicates from a previous connection and off-path attackers cannot be confused with the live stream.

    State on the server (Linux): a SYN creates a mini-socket in the SYN queue (SYN_RECV); the final ACK moves a full socket into the accept queue, where accept() picks it up. If the accept queue is full because the application is slow to call accept(), the kernel drops the final ACK (or the SYN) and the client retransmits — a slow connect with a healthy-looking server, visible as overflows in ss -ltn (Recv-Q vs Send-Q on the listener) and nstat -az TcpExtListenOverflows. SYN floods exploit the SYN queue; SYN cookies avoid storing state until the ACK arrives.

    What can go wrong is a diagnostic ladder. No SYN-ACK at all → the client retransmits with exponential backoff (1, 2, 4, 8… s; ~2 minutes total by default on Linux) and reports "connection timed out": the SYN was dropped by a firewall, no route, host down, or the SYN-ACK was lost on an asymmetric return path. RST in reply to SYN → "connection refused", immediately: the host is reachable, no socket listens on that port (or a firewall *rejects* rather than drops). Handshake completes then RST → a stateful middlebox lost state, or the application closed instantly. Each is a different layer, and the client error names which.

    Full interview question, with follow-ups and red flags →
  3. Why can the server encrypt its certificate in TLS 1.3 but not in 1.2?

    Show a strong answer

    In 1.3 both key shares are exchanged in the first two messages, so a shared secret exists before the certificate is sent; in 1.2 the key exchange completes only after the certificate.

    Full interview question, with follow-ups and red flags →