How HTTPS keeps a connection private

· 3 min read

#how-it-works#security

HTTPS is HTTP carried over TLS, a protocol that gives two computers a private channel across a network they do not trust. The one idea to keep: before the first byte of the page is sent, browser and server agree on a secret key that nobody on the path can see.

The short version

The browser and the server exchange a few messages called the handshake. Each side sends a public piece of a key, and from the two pieces both compute the same secret without ever sending it. The server also shows a certificate and signs the conversation so far, which proves it owns the name in the address bar. From then on, every request and response is encrypted with that secret.

The TLS 1.3 handshake between a browser and a server The browser sends a hello with its key share. The server replies with its own key share, then its certificate, a signature and a Finished message, all encrypted. The browser sends Finished. After that HTTP flows encrypted in both directions. encrypted from here on Browser Server 1 hello, my key share 2 hello, my key share 3 certificate, signature, Finished 4 Finished 5 HTTP requests and responses
Numbers show the order. The shaded area is what an observer on the network cannot read.

Step by step

  1. The browser sends a ClientHello: a random value, the cipher suites it supports, and its share of a fresh key.
  2. The server answers with a ServerHello and its own key share. Each side combines its secret with the other's share and gets the same key. Everything after this point is encrypted.
  3. The server sends its certificate and a signature over the handshake, made with the private key that belongs to the certificate. The browser checks the certificate against the name it asked for.
  4. Both sides send a Finished message, a checksum over the whole handshake, so a tampered message is caught.
  5. Only now does HTTP start. Headers and bodies travel encrypted in both directions.

Why it matters

This is why a shared Wi-Fi can see which server you talk to, but not the pages or the form data. It is also why an expired or mismatched certificate stops the page: step 3 failed, so the browser refuses to continue.

Sources