How TLS works, step by step
What happens between typing an https address and seeing the page: the TLS 1.3 handshake step by step, with a toy key exchange you can run.
The padlock in your browser stands for a small conversation that happens before any web page arrives. Two computers that have never met agree on secret keys, one proves who it is, and both start talking in a way that nobody listening in can read or alter. This article follows that conversation, message by message, for TLS 1.3.
What TLS promises
Transport Layer Security gives a connection three properties:
- Confidentiality. Eavesdroppers cannot read the data.
- Integrity. Anyone tampering with the data is detected.
- Authentication. The client can check that it is talking to the server it asked for. (Authenticating the client is optional and less common on the open web.)
HTTPS is simply HTTP carried inside a TLS connection. TLS 1.0 and 1.1 were formally deprecated in 2021 (RFC 8996), and TLS 1.3, defined in RFC 8446, is the current version. TLS 1.3 also provides the security handshake used inside QUIC (RFC 9001), the transport underneath HTTP/3.
The central problem
Both sides need a shared secret key. But they can only talk over a network that anyone can watch. How do two strangers agree on a secret in public?
The answer is a key exchange. Here is the classic version with numbers small enough to follow by hand. This is only to show the shape of the idea, because real systems use enormous numbers or elliptic curves:
# Toy Diffie-Hellman. NEVER use numbers this small for real.
p, g = 23, 5 # public: a prime and a base, known to everyone
a = 6 # Alice's secret (never sent)
b = 15 # Bob's secret (never sent)
A = pow(g, a, p) # Alice sends A = g^a mod p -> 8
B = pow(g, b, p) # Bob sends B = g^b mod p -> 19
alice_secret = pow(B, a, p) # (g^b)^a mod p
bob_secret = pow(A, b, p) # (g^a)^b mod p
print(A, B, alice_secret, bob_secret) # 8 19 2 2Alice and Bob end up with the same number, 2, without either secret crossing the wire. An eavesdropper sees p, g, A and B. With big enough numbers, working out a or b from those is believed to be infeasible. TLS 1.3 does this with elliptic curves, most commonly X25519 (RFC 7748), and makes the secrets ephemeral: fresh for every connection.
The handshake, message by message
Client Server
ClientHello
+ key_share (client public value)
+ supported_groups, signature_algorithms
+ server_name -------->
ServerHello
+ key_share (server public value)
{EncryptedExtensions}
{Certificate}
{CertificateVerify}
<-------- {Finished}
{Finished} -------->
[Application Data] <-------> [Application Data]
{ } encrypted with handshake keys [ ] encrypted with application keys1. ClientHello
The client opens by sending its wish list: the TLS versions it speaks, the cipher suites it supports, and, importantly, a key share, which is its half of the key exchange, sent straight away. It also names the website it wants in the server_name extension (SNI, RFC 6066), which is how one server can host many sites.
2. ServerHello
The server picks a cipher suite and sends back its own key share. Now both sides hold a pair of public values and each can compute the same shared secret. From this point on, everything is encrypted.
3. Deriving keys
Nobody uses the raw shared secret as a key. TLS 1.3 feeds it through HKDF (RFC 5869), a key-derivation function, in stages: first to produce handshake keys, later to produce application keys. Each direction gets its own keys. The keys are derived, never sent.
4. EncryptedExtensions and Certificate
The server's remaining handshake messages are already protected. The Certificate message carries the server's certificate chain: a document, issued by a certificate authority, that binds a domain name to a public key (RFC 5280).
5. CertificateVerify
A certificate is public, so anyone could copy it. To prove it truly belongs to this server, the server signs a hash of the whole handshake so far with its private key. Only the real owner can make that signature.
6. Finished
Each side sends a final message: a MAC over the entire transcript of the handshake. If anyone altered any earlier message, perhaps to force weaker settings, the Finished values will not match and the connection fails.
7. Application data
The client's Finished is the last handshake message. Then real traffic flows, protected with authenticated encryption (AEAD) such as AES-GCM or ChaCha20-Poly1305. The whole exchange takes one round trip before data can flow, which is faster than the two round trips TLS 1.2 normally needed.
How the client decides to trust the server
When the certificate arrives, the client checks four things:
- Chain. Each certificate is signed by the one above it, ending in a root authority the client already trusts, from its operating-system or browser trust store.
- Validity. The certificate is within its dates and has not been revoked.
- Name. The domain you asked for matches a name listed in the certificate (RFC 9525).
- Possession. The CertificateVerify signature checks out, proving the server holds the matching private key.
Skip any one of these and the padlock means nothing.
What an eavesdropper still sees
A passive observer sees the IP addresses, the timing and size of the packets and, in the ClientHello, the server name. The certificate and everything after the ServerHello are encrypted in TLS 1.3. Work on Encrypted Client Hello is under way in the IETF to hide the server name as well; follow the TLS working group for its status.
Forward secrecy
Because the key exchange uses fresh, throwaway secrets, and they are deleted afterwards, stealing the server's long-term private key later does not let anyone decrypt recordings of old sessions. That property is called forward secrecy. TLS 1.3 also removed several older mechanisms that could weaken it, including static RSA key exchange, compression and renegotiation (see RFC 8446, section 1.2).
Resumption and 0-RTT, briefly
Once a client has talked to a server, TLS 1.3 lets it resume using a pre-shared key and skip some work. An optional 0-RTT mode lets the client send data in its very first flight, at the cost that this early data can be replayed by an attacker. RFC 8446 describes the risk, which is why 0-RTT should only carry requests that are safe to repeat.
Try it yourself
You can watch your own machine do this against a public site. This command asks for TLS 1.3 specifically and prints what was negotiated:
openssl s_client -connect example.com:443 -servername example.com -tls1_3 </dev/nullLook for the negotiated protocol and cipher suite, the certificate's subject and issuer, and a Verify return code: 0 (ok) line. Output varies between OpenSSL versions, so I have not pasted any here. For an even more detailed look, curl -v https://example.com/ narrates the handshake in readable form.
Common misunderstandings
- "HTTPS means the site is trustworthy." It means your connection is encrypted and goes to the domain named in the certificate. A harmful site can have a perfectly valid certificate.
- "Encryption hides everything." The destination server name and traffic patterns are still visible.
- "The padlock is magic." It is a protocol with specific steps, each of which can be checked, which is what you have just walked through.
Where to go next
For a byte-by-byte tour of a real TLS 1.3 connection, bookmark The Illustrated TLS 1.3 Connection. Then read the handshake chapter of RFC 8446 with this article beside it. The key-storage side of the story, how a server keeps passwords safe, is in How passwords are really stored.
Sources
- RFC 8446, The Transport Layer Security (TLS) Protocol Version 1.3 www.rfc-editor.org
- RFC 5869, HMAC-based Extract-and-Expand Key Derivation Function (HKDF) www.rfc-editor.org
- RFC 7748, Elliptic Curves for Security (X25519 and X448) www.rfc-editor.org
- RFC 5280, Internet X.509 Public Key Infrastructure Certificate and CRL Profile www.rfc-editor.org
- RFC 9525, Service Identity in TLS www.rfc-editor.org
- RFC 6066, TLS Extensions (Server Name Indication) www.rfc-editor.org
- RFC 8996, Deprecating TLS 1.0 and TLS 1.1 www.rfc-editor.org
- RFC 9001, Using TLS to Secure QUIC www.rfc-editor.org
- IETF TLS Working Group (Encrypted Client Hello work) datatracker.ietf.org
- The Illustrated TLS 1.3 Connection tls13.xargs.org