TLS · Transport Layer Security

Privacy, integrity and identity for any connection.

07 · Application · Runs over TCP (usually 443)

TLS

Transport Layer Security

Privacy, integrity and identity for any connection.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a TLS exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System design›Protocols

Study with AI
ClaudeChatGPTGeminiPerplexityMore

Part 1 of 5

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.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. Why does TLS use both asymmetric and symmetric crypto?
  2. What does the certificate prove?

The answers are in part 4.

Part 3 of 5

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.

Client
Server
ClientHello + key share
ServerHello + key share
{Certificate, Verify, Finished}
{Finished}
Step 1 of 4

Supported versions, cipher suites, the server name (SNI) and an x25519 public key, in clear text.

← BackPlayNext →

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

ClientHelloServerHello

The first handshake message, sent in plain text. After the server’s reply everything else is encrypted. Random values here are illustrative.

0000160301008a0100008603030b30557a9f············0Uz·
0010c4e90e33587da2c7ec11365b80a5caef···3X}····6[····
002014395e83a8cdf2173c61860000061301·9^·····<a······
0030130213030100005700000010000e0000·······W········
00400b6578616d706c652e636f6d002b0003·example.com·+··
0050020304000a00040002001d000d000600················
00600404030804003300260024001d002007······3·&·$··· ·
00703c71a6db10457aafe4194e83b8ed2257<q···Ez···N···"W
00808cc1f62b6095caff34699ed3083d72···+`···4i···=r
FieldRecord type
Length1 byte
Decimal22

22 = Handshake. 23 = Application data, 21 = Alert.

FieldOffsetBytesHex
Record type0116
Record version1203 01
Record length3200 8a
Handshake type5101
Handshake length6300 00 86
client_version9203 03
random11320b 30 55 7a 9f c4 e9 0e …
session_id length43100
cipher_suites44800 06 13 01 13 02 13 03
compression52201 00
extensions length54200 57
server_name (SNI)562000 00 00 10 00 0e 00 00 …
supported_versions76700 2b 00 03 02 03 04
supported_groups83800 0a 00 04 00 02 00 1d
signature_algorithms911000 0d 00 06 00 04 04 03 08 04
key_share1014200 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.

tls.tsTypeScriptCopy
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}
26 linesUTF-8TypeScript
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.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why does TLS use both asymmetric and symmetric crypto?
Asymmetric crypto establishes a shared secret safely but is slow. Symmetric encryption then protects the data quickly.
What does the certificate prove?
That a trusted authority vouched that this public key belongs to this domain name.
Part 5 of 5

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.
← TCPMark as understoodHTTP →