Skip to content

What HTTPS adds: the TLS handshake

Working knowledgeUnder the hood

The padlock means your bytes are private and really came from the site you typed, even though every router on the way sees them. Here is how the TLS handshake gets there: a certificate to prove the name, a key exchange done in public, and the session keys that come out of it.

01 What TCP leaves open

TCP gets your bytes to the other end, in order. It does nothing to stop the Wi-Fi hotspot, the ISP or any router on the path from reading those bytes or changing them. HTTPS is plain HTTP sent through a TLS connection, and TLS adds three things:

  • who you are talking to: a certificate ties the server’s public key to a name such as example.com;
  • a shared secret: a key exchange lets both ends agree on a secret that an eavesdropper cannot work out, even though every message is public;
  • the session keys: fast symmetric keys derived from that secret, which encrypt and check every byte after the handshake.

This page walks through TLS 1.3, the version most browsers and servers use today. TLS 1.2 is still around and works in the same spirit with one more round trip.

02 The wrong picture: the certificate encrypts

A common picture is that the server’s certificate holds a key and your traffic is encrypted with it. Older TLS (1.2 and before) did offer something close: with RSA key exchange, the client made up a secret, encrypted it with the server’s RSA public key and sent it over. Only the server could decrypt it.

The trouble is what happens later. Someone who records the traffic today and steals the server’s private key next year can decrypt the secret, and with it every recorded session. TLS 1.3 removed RSA key exchange. The certificate’s key now only signs; the secret comes from a fresh Diffie-Hellman exchange on every connection.

RSA key exchange (TLS 1.2)One stolen key opens allThe secret travels encrypted under the long-lived certificate key. Leak that key and recorded traffic can be read.
Ephemeral Diffie-Hellman (TLS 1.3)Fresh secret per connectionEach side throws its private value away after the handshake. A later key theft does not unlock old sessions. This is called forward secrecy.

Public-key maths is also slow compared with symmetric ciphers. So TLS uses it only during the handshake, and encrypts the actual data with a symmetric cipher such as AES-GCM or ChaCha20-Poly1305.

03 Agreeing on a secret in public

Diffie-Hellman lets two parties end up with the same number while sending only values that are safe to show. The trick is that raising a number to a power modulo a prime is easy, but undoing it (finding the exponent) is very hard for large numbers. Here it is with numbers small enough to check by hand.

Agreeing on a secret in public (toy numbers)
Step 1 of 7
On the wire (anyone can read)
ClientServerNothing sent yet
What each party knows
PrivatePublic seenShared secret
Client–––
Server–––
Eavesdropper–––
  • just chosen or learned
  • just sent
  • seen by the eavesdropper
  • shared secret
  • not known yet

Both sides already agree on two public numbers, a prime p = 23 and a base g = 5. Everyone, the eavesdropper included, knows them. Nobody has picked a secret yet.

p = 23, g = 5 public

Real TLS does the same thing on an elliptic curve, most often X25519, where the secrets are 32-byte values. Recent OpenSSL (3.5 and later) and current browsers typically go one step further and offer a hybrid group, X25519MLKEM768, which combines X25519 with a post-quantum key exchange. The idea is unchanged: each side contributes a public share, and only the two ends can compute the result.

04 The handshake, message by message

Now put the pieces in order. The client sends its key share in its very first message, guessing which group the server will accept. That guess is what lets TLS 1.3 finish in one round trip.

A TLS 1.3 handshake, one message at a time
Step 1 of 9
Messages, in order
ClientServerNothing sent yet
What each side has
Key shareHandshake keysServer provenApp keys
Client––––
Server––––
  • just sent
  • just changed
  • server proven
  • not yet

The TCP connection to port 443 is open. TLS has not sent anything, so neither side has a key.

The key exchange gives you a secret shared with someone. Only the certificate plus CertificateVerify tell you that someone is example.com. Without that check, an attacker in the middle could run one key exchange with you and another with the real server, and read everything.

05 How the client trusts a certificate

A certificate says “this public key belongs to example.com”, and it is signed by a certificate authority (CA). The server usually sends its own certificate plus one intermediate. The client already holds a set of root certificates, shipped with the operating system or browser.

A typical chain
CertificateNamesSigned byWhere the client gets it
Leafexample.com, www.example.comIntermediate CASent by the server
IntermediateExample Intermediate CARoot CASent by the server
RootExample Root CAItselfAlready in the trust store

The client checks each signature up the chain to a root it trusts, checks that the leaf lists the host name it asked for (in the Subject Alternative Name field), and checks that today falls between the certificate’s start and expiry dates. Revocation checks exist too, but clients apply them unevenly, which is one reason certificates have become short-lived (Let’s Encrypt’s are typically valid for 90 days).

06 From shared secret to session keys

The Diffie-Hellman result is not used as a key directly. TLS 1.3 feeds it, together with a hash of the handshake messages, through HKDF, a key derivation function, and gets a family of keys out.

The TLS 1.3 key schedule, simplified
Derived fromGivesUsed for
Shared secret + hash of ClientHello…ServerHelloClient and server handshake traffic secretsEncrypting the certificate, CertificateVerify and Finished
The same chain + hash of the handshake up to server FinishedClient and server application traffic secretsEncrypting HTTP requests and responses
Each traffic secretA key and an IVThe AEAD cipher, e.g. AES-128-GCM

Two details matter. Each direction gets its own key, so client-to-server and server-to-client traffic never share one. And because the transcript hash goes into the derivation, the keys depend on exactly what was said: if an attacker strips a cipher suite out of the ClientHello to force a weaker choice, the Finished check fails.

07 Watch a handshake with openssl

openssl s_client speaks TLS and prints what it negotiated. This run used OpenSSL 3.6.1 against a local openssl s_server with a self-signed certificate, which is why verification complains. Point it at a real site, for example -connect example.com:443, and the error goes away.

Shell
echo | openssl s_client -connect 127.0.0.1:18443 -servername localhost -brief
Text
CONNECTION ESTABLISHED
Protocol version: TLSv1.3
Ciphersuite: TLS_AES_256_GCM_SHA384
Peer certificate: CN=localhost
Hash used: SHA256
Signature type: ecdsa_secp256r1_sha256
Verification error: self-signed certificate
Negotiated TLS1.3 group: X25519MLKEM768
DONE

group is the key exchange, Signature type is how CertificateVerify was signed, and Ciphersuite is the symmetric cipher for the data. Swap -brief for -msg to list each handshake message. Trimmed to the message names, the same run printed:

Text
>>> Handshake, ClientHello
<<< Handshake, ServerHello
<<< ChangeCipherSpec
<<< Handshake, EncryptedExtensions
<<< Handshake, Certificate
<<< Handshake, CertificateVerify
<<< Handshake, Finished
>>> ChangeCipherSpec
>>> Handshake, Finished

That is the trace above, message for message. The ChangeCipherSpec records carry no meaning in TLS 1.3; they are sent so that old middleboxes, which expect TLS 1.2, let the connection through.

08 When it bites

certificate has expiredAn expired certificate takes the site downClients refuse the connection outright. Automate renewal (for example with an ACME client) and alert on expiry dates well before they arrive.
unable to get local issuer certificateThe server forgets the intermediateBrowsers often cope because they have cached or can fetch the intermediate, so the site looks fine. curl, Python and Java clients fail. Serve the full chain, leaf first.
-servernameNo SNI, wrong certificateOne IP address often hosts many sites. Without the server name in the ClientHello, the server cannot tell which certificate to send and returns a default one that does not match.
not yet validA wrong clock breaks TLSValidity dates are checked against the client’s clock. A device that boots with its clock years off rejects every certificate.
early_data0-RTT data can be replayedOn a resumed connection TLS 1.3 can send data with the very first message. An attacker can capture and resend it, so only allow it for requests that are safe to repeat, such as a plain GET.

curl -k, verify=False in Python requests and similar switches keep the encryption but skip the certificate check. You still get a shared secret, just possibly with an attacker. Fix the trust problem instead (add the right CA or the missing intermediate).

09 Check yourself

Question

In TLS 1.3, what is the certificate’s private key used for?

Question

Someone records your HTTPS session today and steals the server’s private key next year. With TLS 1.3, what can they read?

Question

A client skips certificate checks but still does the key exchange. What can an attacker on the path do?

10 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?