TLS
Transport Layer Security
Privacy, integrity and identity for any connection.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
TLS wraps a connection so that nobody on the path can read or change it, and so that the client knows it is talking to the real server. It does this by agreeing a shared secret using public-key cryptography, then encrypting everything with fast symmetric ciphers.
TLS 1.3 needs one round trip. The client guesses the key exchange group and sends its public key in the first message, so the server can finish key setup in its first reply.
1.2Key ideas
The handful of ideas everything else follows from.
Forward secrecy
Each connection uses fresh ephemeral keys. Recording traffic today and stealing the server key tomorrow does not reveal it.
Certificates
A chain from the server certificate to a trusted root proves the name. The client checks signature, validity dates, name and revocation status.
AEAD
TLS encrypts and authenticates each record together. The receiver detects tampering.
Resumption and 0-RTT
Returning clients can reuse a session ticket to start faster. 0-RTT data can be replayed, so only use it for safe requests.
SNI and ECH
The server name is visible in ClientHello unless Encrypted Client Hello is used.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why does TLS use both asymmetric and symmetric crypto?
- What does the certificate prove?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
Supported versions, cipher suites, the server name (SNI) and an x25519 public key, in clear text.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
The first handshake message, sent in plain text. After the server’s reply everything else is encrypted. Random values here are illustrative.
22 = Handshake. 23 = Application data, 21 = Alert.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Record type | 0 | 1 | 16 | |
| Record version | 1 | 2 | 03 01 | |
| Record length | 3 | 2 | 00 8a | |
| Handshake type | 5 | 1 | 01 | |
| Handshake length | 6 | 3 | 00 00 86 | |
| client_version | 9 | 2 | 03 03 | |
| random | 11 | 32 | 0b 30 55 7a 9f c4 e9 0e … | |
| session_id length | 43 | 1 | 00 | |
| cipher_suites | 44 | 8 | 00 06 13 01 13 02 13 03 | |
| compression | 52 | 2 | 01 00 | |
| extensions length | 54 | 2 | 00 57 | |
| server_name (SNI) | 56 | 20 | 00 00 00 10 00 0e 00 00 … | |
| supported_versions | 76 | 7 | 00 2b 00 03 02 03 04 | |
| supported_groups | 83 | 8 | 00 0a 00 04 00 02 00 1d | |
| signature_algorithms | 91 | 10 | 00 0d 00 06 00 04 04 03 08 04 | |
| key_share | 101 | 42 | 00 33 00 26 00 24 00 1d … |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Reading the server name from a ClientHello
Load balancers route on this without decrypting anything. It's also why the name is visible on the network.
1/** Walks a ClientHello far enough to read the server name, as a load balancer does. */2export function parseClientHello(b: Uint8Array) {3 const view = new DataView(b.buffer, b.byteOffset, b.byteLength);4 if (b[0] !== 22 || b[5] !== 1) throw new Error("not a ClientHello"); // record type 22, handshake type 15 6 let i = 5 + 4 + 2 + 32; // record header, handshake header, version, random7 i += 1 + b[i]; // session id8 const suitesLength = view.getUint16(i);9 const suites: number[] = [];10 for (let s = 0; s < suitesLength; s += 2) suites.push(view.getUint16(i + 2 + s));11 i += 2 + suitesLength;12 i += 1 + b[i]; // compression methods13 14 const end = i + 2 + view.getUint16(i);15 i += 2;16 let serverName: string | null = null;17 const extensions: number[] = [];18 while (i < end && i + 4 <= b.length) {19 const type = view.getUint16(i);20 const length = view.getUint16(i + 2);21 extensions.push(type);22 if (type === 0) serverName = new TextDecoder().decode(b.subarray(i + 9, i + 4 + length));23 i += 4 + length;24 }25 return { recordLength: view.getUint16(3), suites, extensions, serverName, offersTls13: extensions.includes(43) };26}5 + 4 + 2 + 32- Skips the record header, handshake header, version and random.
i += 1 + b[i]- Variable-length fields start with their own length, so you walk them one by one.
type === 0- Extension 0 is the server name, and 43 lists supported versions.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why does TLS use both asymmetric and symmetric crypto?
What does the certificate prove?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- An expired or mismatched certificate is the most common TLS failure.
- TLS terminating at a load balancer means traffic behind it is plain unless re-encrypted.
- Pinning certificates in apps can lock out users when certificates rotate.