How HTTPS Works: TLS Explained
On this page
What HTTPS protects against
On café Wi-Fi, anyone on the network can see and modify unencrypted traffic: read passwords, inject scripts, redirect you to a fake site. TLS (Transport Layer Security; SSL is its retired predecessor) prevents all three:
- Confidentiality: traffic is encrypted.
- Integrity: any modification is detected.
- Authentication: the client verifies it's talking to the real server, using a certificate (Day 17).
What TLS doesn't hide: the server's IP address, and usually the hostname, which travels in the clear in the SNI field so the server knows which certificate to present (Encrypted Client Hello, ECH, is rolling out to fix that).
Symmetric vs asymmetric encryption
- Asymmetric (public and private keys): used in the handshake to agree on secrets and prove identity. Slow.
- Symmetric (one shared key, such as AES-GCM or ChaCha20-Poly1305): used for the data itself. Fast.
The handshake uses asymmetric cryptography to set up a symmetric key, then everything else uses that key.
How two strangers agree on a secret: Diffie-Hellman
The puzzle at the heart of TLS: your browser and a server have never met, and everything they send can be read by whoever is on the café network. How can they end up sharing a secret key that the eavesdropper doesn't know?
Diffie-Hellman key exchange solves it. Each side picks a private number and never sends it. Each sends a public value computed from its private number. Then each combines the other side's public value with its own private number, and the math guarantees they both get the same result. Here it is with numbers small enough to follow:
# dh.py: Diffie-Hellman with toy numbers. Real TLS uses X25519, but the idea is the same.
p, g = 23, 5 # public: everyone, including an eavesdropper, knows these
a = 6 # the client's secret, never sent (random in real use)
b = 15 # the server's secret, never sent (random in real use)
A = pow(g, a, p) # the client's key share: sent in the ClientHello
B = pow(g, b, p) # the server's key share: sent in the ServerHello
client_secret = pow(B, a, p) # the client combines the server's share with its own secret
server_secret = pow(A, b, p) # the server does the same with the client's share
print(f"on the wire: p={p} g={g} A={A} B={B}")
print(f"client computes {client_secret}, server computes {server_secret}, same: {client_secret == server_secret}")
# Why the toy numbers are toys: with p = 23 an eavesdropper can simply try every value.
eve = next(x for x in range(1, p) if pow(g, x, p) == A)
print(f"an eavesdropper who tries every value finds a = {eve}")on the wire: p=23 g=5 A=8 B=19
client computes 2, server computes 2, same: True
an eavesdropper who tries every value finds a = 6The eavesdropper saw p, g, A and B, but never a or b. Both sides reached the same secret because (g^b)^a and (g^a)^b are both g^(ab). With a modulus of 23, though, an eavesdropper can simply try every possible a, as the last line shows. Real TLS uses X25519, an elliptic-curve version of the same idea with numbers around 2^255, where trying every value would take far longer than the age of the universe. That's the "key share" you'll see in the handshake below.
One thing Diffie-Hellman doesn't do is prove who you're talking to. An attacker in the middle could run one exchange with you and another with the server. That's why the server also signs the handshake with its certificate's private key, the step that Day 17's certificates make trustworthy.
The TLS 1.3 handshake
- ClientHello: supported versions and ciphers, a random value, and a key share (the client's half of an ephemeral Diffie-Hellman exchange, usually X25519).
- ServerHello: the chosen cipher and the server's key share. Both sides can now compute the same secret; everything after this is encrypted.
- The server sends its certificate and a signature over the handshake with its private key, proving it holds the key matching the certificate, then Finished.
- The client verifies the certificate chain and signature, sends Finished, and can send the HTTP request right away.
One round trip, compared with two in TLS 1.2. Resumed connections can send data immediately (0-RTT), with the replay caveat from Day 14.
Forward secrecy explained
The session key comes from ephemeral key shares that are discarded after the handshake. The server's long-term private key only signs; it never encrypts the session key. So if that private key leaks next year, recorded traffic from today still can't be decrypted. TLS 1.3 requires this property, called forward secrecy; older RSA key exchange didn't have it.
Inspecting a TLS handshake with openssl
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| grep -E 'Protocol|Cipher|Server Temp|Verify return'Server Temp Key: ECDH, X25519, 253 bits
Protocol : TLSv1.3
Cipher : AEAD-CHACHA20-POLY1305-SHA256
Verify return code: 0 (ok)TLS 1.3, an X25519 ephemeral key exchange, ChaCha20-Poly1305 for the data, and a certificate chain that verified. -servername sets SNI; without it, servers that host many sites may present the wrong certificate.
TLS configuration best practices
- Allow TLS 1.2 and 1.3 only. Disable SSL 3, TLS 1.0 and 1.1.
- Prefer ephemeral key exchange and AEAD ciphers; TLS 1.3 only offers good ones.
- Redirect HTTP to HTTPS and send HSTS (
Strict-Transport-Security), so browsers never try plain HTTP again. - Test your public endpoints with a scanner such as SSL Labs.
Check yourself
Confidentiality, because data is encrypted; integrity, because authenticated encryption detects any tampering; and server authentication, because the certificate proves the server owns the domain. Client authentication is optional, with mutual TLS.
It's far slower than symmetric encryption. TLS uses it to agree on and authenticate a shared symmetric key, then protects all application data with fast symmetric ciphers like AES-GCM or ChaCha20.
No. Session keys came from ephemeral key exchanges that were discarded, so the long-term key can't recover them (forward secrecy). The attacker could impersonate the server going forward, until the certificate is revoked and replaced.
It would need one side's private number, and recovering that from the public value is computationally infeasible at real key sizes such as X25519. With tiny toy numbers it could just try every value.
TLS 1.2 and 1.3, preferring 1.3 for its faster handshake and simpler, safer cipher suites. TLS 1.0 and 1.1 are formally deprecated and should be disabled; modern browsers no longer support them anyway.