How HTTPS keeps a connection private
IntermediateThe TLS handshake step by step: how two strangers agree on a secret key in the open, and how a certificate proves the server really is example.com.Builds on What happens when you type a URL
$ curl -v https://example.com/* Trying 203.0.113.10:443...* Connected to example.com (203.0.113.10) port 443* TLSv1.3 (OUT), TLS handshake, Client hello (1):* TLSv1.3 (IN), TLS handshake, Server hello (2):* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):* TLSv1.3 (IN), TLS handshake, Certificate (11):* TLSv1.3 (IN), TLS handshake, CERT verify (15):* TLSv1.3 (IN), TLS handshake, Finished (20):* TLSv1.3 (OUT), TLS handshake, Finished (20):* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256* ALPN: server accepted h2* Server certificate:* subject: CN=example.com* start date: Oct 1 00:00:00 2026 GMT* expire date: Dec 30 23:59:59 2026 GMT* subjectAltName: host "example.com" matched cert's "example.com"* issuer: C=US; O=Example CA; CN=Example CA R1* SSL certificate verify ok.> GET / HTTP/2Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
3 min read
On this page
What is HTTPS and what does TLS do?
HTTPS is ordinary HTTP sent inside a TLS connection. TLS (Transport Layer Security) gives you three things that plain HTTP does not:
- Privacy. Everything after the handshake is encrypted, so the Wi-Fi, your internet provider and anyone else on the path see only scrambled bytes.
- Integrity. Every encrypted record carries a check, so a change of even one byte is detected and the connection is dropped.
- Authentication. A proves the server really owns the name in the address bar.
Before any of that, the two sides need a shared secret key, and they have to agree on it over a network anyone can read. That is the job of the handshake.
How does the TLS handshake work?
- Key exchange. The browser and the server each make a private secret and send only a public key share derived from it. Mixing your own secret with the other's share gives both sides the same key, while someone who saw both shares still cannot compute it (a Diffie-Hellman exchange). A fresh key is made for every connection, so recording traffic today and stealing the server's key later does not unlock it.
- The certificate. It says "this public key belongs to example.com", signed by a certificate authority (CA). The server also signs the handshake with the matching private key, to prove it is not just showing a copied certificate.
- The chain of trust. Usually a CA's intermediate certificate signs the site's, and a root certificate signs the intermediate. Root certificates come built into your operating system or browser. If the chain does not end at one of them, or the name or the dates are wrong, the browser stops with a warning.
- One round trip. TLS 1.3 needs a single round trip before the request can go, and a returning visitor can often resume a previous session even faster.
What do people get wrong about HTTPS?
Mini example
openssl and curl show the handshake from the terminal. The site badssl.com keeps broken
certificates on purpose, so you can see what a failed check looks like:
# The certificates the server sends, from the site's up to the CA's$ openssl s_client -connect example.com:443 -servername example.com -showcerts # An expired certificate: curl refuses, like the browser would$ curl https://expired.badssl.com/curl: (60) SSL certificate problem: certificate has expiredQuick check
Someone on the café Wi-Fi watches your whole TLS handshake. Why can't they decrypt what comes next?
Further reading
All steps as text
- Step 1:
Weeks earlier, the owner of example.com asked a certificate authority (CA) for a certificate, sending the server’s public key. The CA first checks they really control the name.
- Step 2:
The CA signs a certificate: “this public key belongs to example.com”. Browsers ship with a list of CAs they trust, Example CA among them.
- Step 3:
Today you open https://example.com. The browser connects to port 443. Anyone on the path, like the café’s Wi-Fi, can see this traffic, and so far nothing is hidden.
Lines 1–3
- Step 4:
ClientHello: the browser lists the TLS versions and ciphers it supports, names the site it wants, and sends its half of a key exchange, a public key share.
Line 4
- Step 5:
ServerHello: the server picks TLS 1.3 and a cipher, and sends its own key share. Each side mixes its secret with the other’s share, and both get the same key.
Line 5
- Step 6:
The network saw both key shares, yet it can’t work out the key: that takes one side’s private secret. This trick is called a Diffie-Hellman key exchange.
- Step 7:
From here on everything is encrypted with that key. The server sends its certificate, which the network now sees only as scrambled bytes.
Lines 6–7
- Step 8:
A certificate is public, so anyone could copy it. CertificateVerify proves more: the server signs the handshake with the private key that matches the certificate.
Line 8
- Step 9:
The browser checks the certificate: the name matches example.com, today is between its dates, and its signature leads to a CA it trusts. All good.
Lines 13–19
- Step 10:
Both sides send Finished, a check over everything said so far. If anyone had changed a single byte of the handshake, it wouldn’t match.
Lines 9–10
- Step 11:
The handshake took one round trip. Data will be encrypted with AES-128-GCM, which also detects tampering, and both sides agreed on HTTP/2 on the way.
Lines 11–12
- Step 12:
Only now does the HTTP request leave. The path, the headers and any cookies are inside the encryption; the network sees only sizes and timing.
Line 20
- Step 13:
The response comes back encrypted the same way. The browser shows the page with the padlock: private, and really from example.com.
- Step 14:
What if the network had answered instead, pretending to be example.com? No trusted CA would sign it a certificate for that name, so the browser would show a full-page warning.
- Step 15:
HTTPS is HTTP inside TLS. The key exchange makes the connection private, the certificate proves who is on the other end, and Finished catches tampering.