The TLS handshake

Introduction

Every time you open https:// anything, your browser and the server run a short conversation before a single byte of the page moves: the TLS handshake. In a fraction of a second they agree on keys nobody else knows, and the browser checks that it is really talking to the site in the address bar.

The handshake uses almost everything from the cryptography module at once: Diffie-Hellman to agree on a secret, certificates and signatures to prove identity, and a MAC to catch tampering. This lesson walks through TLS 1.3, the current version (RFC 8446, 2018), message by message.

How it works

TLS 1.3's full handshake takes one round trip: the client speaks, the server answers, and the client can send its request right after. Step through it below. Then tick Attacker's certificate to see where a man in the middle is caught.

The key ideas:

  • Key shares in the Hellos. The ClientHello already carries the client's Diffie-Hellman public value, guessing which group the server will accept; the ServerHello carries the server's. Both sides now compute the same shared secret, and nothing secret has crossed the network.
  • Everything after ServerHello is encrypted with keys derived from that secret, including the server's certificate.
  • Proof of identity. The certificate binds the server's public key to its name, signed by a CA the browser trusts. In CertificateVerify, the server signs the handshake so far with its private key, proving it really holds the key in the certificate.
  • Proof of an unchanged handshake. Each side's Finished message is a MAC over every handshake message, keyed from the shared secret. If an attacker changed anything on the way, the MACs would not match and the connection would close.

One detail is still visible to the network: the ClientHello usually carries the site's name in the clear (the SNI field), so a network can see which site you visit, though not what you do there. A newer extension, Encrypted Client Hello, hides it where both sides support it.

Example

The program runs the core of the handshake with the toy numbers from the Diffie-Hellman lesson and a toy hash for the MAC.

// The heart of a TLS 1.3 handshake in miniature: key shares in the Hellos, the same secret on both sides,
// and Finished messages that prove both saw the same handshake. Tiny numbers and a toy hash, for learning only.
function modPow(base, exp, m) {
  let result = 1;
  base = base % m;
  while (exp > 0) {
    if (exp % 2 === 1) result = (result * base) % m;
    base = (base * base) % m;
    exp = Math.floor(exp / 2);
  }
  return result;
}
function toyHash(text) {
  let h = 2166136261;
  for (let k = 0; k < text.length; k++) h = Math.imul(h ^ text.charCodeAt(k), 16777619) >>> 0;
  return h.toString(16).padStart(8, "0");
}

const p = 23, g = 5;
const clientSecret = 6, serverSecret = 15;
const clientHello = "ClientHello share=" + modPow(g, clientSecret, p);
const serverHello = "ServerHello share=" + modPow(g, serverSecret, p);
console.log(clientHello);
console.log(serverHello);

const clientShared = modPow(modPow(g, serverSecret, p), clientSecret, p);
const serverShared = modPow(modPow(g, clientSecret, p), serverSecret, p);
console.log("shared secret: client " + clientShared + ", server " + serverShared);

// Finished = a MAC over everything said so far, keyed by the shared secret.
const transcript = clientHello + "|" + serverHello;
const serverFinished = toyHash(serverShared + "|server finished|" + transcript);
const clientCheck = toyHash(clientShared + "|server finished|" + transcript);
console.log("server Finished " + serverFinished + (serverFinished === clientCheck ? ": matches, handshake complete" : ": MISMATCH"));

// If an attacker had changed a Hello on the way, the transcripts would differ and Finished would not match.
const tampered = clientHello + "|ServerHello share=4";
console.log("tampered transcript: " + (toyHash(clientShared + "|server finished|" + tampered) === serverFinished ? "matches" : "does not match, connection closed"));
Open in compiler

Line by line

  1. The ClientHello carries the client's share 5⁶ mod 23 = 8; the ServerHello carries 5¹⁵ mod 23 = 19.
  2. Each side raises the other's share to its own secret, and both get 2: the shared secret.
  3. The server's Finished value is a hash of the shared secret, a label and the transcript, the two Hellos joined together.
  4. The client computes the same value from its own copy of the transcript. It matches, so both saw the same messages.
  5. If an attacker had swapped the server's share for 4 on the way, the client's transcript would differ, its Finished check would fail, and the connection would close.

Expected result

ClientHello share=8
ServerHello share=19
shared secret: client 2, server 2
server Finished 19005da4: matches, handshake complete
tampered transcript: does not match, connection closed

In the real world

  • TLS 1.3 removed old, weak options that TLS 1.2 allowed, such as RSA key transport, which had no forward secrecy, and older ciphers.
  • A full TLS 1.2 handshake took two round trips; TLS 1.3 takes one, which makes pages start sooner on slow mobile networks.
  • TLS 1.3 also offers "0-RTT" resumption, which sends data in the very first message to a server visited before; that early data can be replayed by an attacker, so servers accept only safe requests that way.
  • When a browser shows "Your connection is not private", it is the certificate check in this handshake that failed.

Common mistakes

  • Thinking HTTPS hides which site you visit. The site's name and the IP address are still visible to the network, unless Encrypted Client Hello is used.
  • Believing the lock means the site is honest. It means the connection is private and the name is right; a phishing site can have a valid certificate for its own look-alike name.
  • Turning off certificate checks in code "to make it work". That removes the protection against a man in the middle.
  • Keeping TLS 1.0 and 1.1 enabled on a server for old clients; both are deprecated.

Best practices

  • Serve every page over HTTPS, and redirect plain HTTP to it.
  • Allow only TLS 1.2 and 1.3 on servers, with the library's recommended settings.
  • Never disable certificate verification, even in test code that may end up in production.
  • Renew certificates automatically, and add HSTS so browsers always use HTTPS for the site.

Summary

The TLS 1.3 handshake takes one round trip. The two Hellos carry Diffie-Hellman key shares, so both sides derive the same secret without sending it; everything after the ServerHello is encrypted. The server proves its identity with a certificate and a CertificateVerify signature over the handshake, and both sides' Finished MACs prove that nobody changed a message. A man in the middle fails at the certificate check, which is exactly when the browser shows a warning.

Quick check

1. Which two TLS 1.3 handshake messages are sent unencrypted?
2. What does CertificateVerify prove?
3. An attacker changes the server's key share on the way. Where does TLS notice?
4. Even with HTTPS, what can a network usually still see?

Key takeaways

  • TLS 1.3 needs one round trip: the Hellos carry the Diffie-Hellman shares.
  • Everything after ServerHello is encrypted, including the certificate.
  • The certificate, CertificateVerify and Finished together stop a man in the middle.
  • HTTPS hides content, not which server you talk to.

Practice problems for this lesson

Was this lesson clear?

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.