#280 How it works, by example 6.9 min
How HTTPS Works: Watch a TLS 1.3 Handshake in Python
In a TLS 1.3 handshake, every handshake message after the two hellos is encrypted, but the site name is not.
Predict the three trust results
Copy the code folder first. This needs the cryptography package that the video already requires, so these are prediction tasks. Read part 2 of run.py and predict, for each of the three clients, whether it will be accepted or rejected and the reason. Then run python run.py and compare your predictions with the output.
A hint
Check which certificates each client context was built to trust, and which host name it asks for.
Predict a different wrong name
In run.py, change the host in the third client from example.com to example.org. Predict what the line for that client will say, and whether the server's certificate or the name is the problem. Then run it and compare. The video says a valid certificate for another name is still the wrong server.
A hint
The certificate names localhost only.
Read the five-byte record header
Open watch.py and read the records function. Using the comment about the record, work out which byte tells the type. Then predict the name it gives for a record of type 22 whose sixth byte is 2, for type 20, and for type 23. Check your answers against the TYPES and HANDSHAKE dictionaries.
A hint
The names are in the TYPES and HANDSHAKE dictionaries.
Find where the name hides
In watch.py, the server_name function walks past fixed fields and then looks through the extensions for number 0. Write down in order the fields it skips (headers, version, random bytes, session id, cipher suites, compression methods). Then explain, in one sentence, why the video says HTTPS does not always hide the site name.
A hint
Only Encrypted Client Hello, where both sides support it, hides it.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
One handshake in memory on one machine, with a throwaway self-signed certificate; it names record types only and does not show certificate chains, session resumption or Encrypted Client Hello.
Up next in How it works, by example
More in How it works, by example
- #274 How File Compression Works: Huffman Coding in 40 Lines
- #275 Bloom Filters Explained: Build One and Measure Errors
- #276 How Database Indexes Work: B-tree vs Full Scan in SQLite
- #277 How REST APIs Work: Build One with the Standard Library
- #278 Why Redis Is Fast: Build a Cache with Expiry and Eviction
