Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 40 ยท Protocols and Practice

How TLS Works

Follow the TLS 1.3 handshake step by step: key shares, certificates, Finished messages, cipher suites and forward secrecy, with a runnable simulation in Python.

TLS (Transport Layer Security) is the protocol behind the padlock. It protects HTTPS, email transport, many database connections and almost every app that talks to a server. It is also the best place to see the whole course working together: key exchange to agree on a secret, certificates and signatures to know who you are talking to, HKDF to turn the secret into keys, and authenticated encryption to protect the actual data.

A TLS connection has two phases. The handshake lets the client and server agree on parameters, authenticate the server (and sometimes the client) and establish fresh keys. The record layer then uses those keys to encrypt and authenticate every message. This lesson concentrates on TLS 1.3, the current version, and builds a simplified but working simulation of its handshake.

What TLS promises

If everything is set up correctly, a TLS connection gives you:

  • Confidentiality. A network observer sees only ciphertext (plus the size and timing of messages, and usually the server name).
  • Integrity. Any modification of the data in transit is detected.
  • Server authentication. The server proves it owns the certificate for the name you asked for.
  • Forward secrecy. Recording the traffic today and stealing the server's key tomorrow does not reveal the contents.

TLS does not hide who you connect to at the IP level, does not protect data once it reaches the server, and does not make the server trustworthy; it only guarantees that you are talking, privately, to the holder of the right certificate.

The TLS 1.3 handshake

TLS 1.3 (RFC 8446, published in 2018) finishes its handshake in a single round trip. The client does not wait to learn the server's preferences; it guesses a key-exchange group, usually X25519, and sends a key share straight away.

sequenceDiagram
    participant C as Client
    participant S as Server
    C->>S: ClientHello (random, cipher suites, key share)
    S->>C: ServerHello (random, chosen suite, key share)
    Note over C,S: Both compute the shared secret and derive handshake keys
    S->>C: Certificate, CertificateVerify, Finished (encrypted)
    C->>S: Finished (encrypted)
    Note over C,S: Application keys are derived from the whole transcript
    C->>S: Application data (encrypted)
    S->>C: Application data (encrypted)

One round trip: after ServerHello, everything else is already encrypted.

Step by step:

  1. ClientHello. The client sends 32 random bytes, the cipher suites it supports, the groups it can use and a key share: a fresh ephemeral public key.
  2. ServerHello. The server picks a suite and answers with its own random bytes and its ephemeral public key. From here on both sides can compute the same shared secret, and they run it through HKDF, mixed with a hash of all messages so far, to derive handshake traffic keys.
  3. Certificate and CertificateVerify. Encrypted under the handshake keys, the server sends its certificate chain, then a signature over the hash of the whole transcript made with the certificate's private key. This proves that this server, right now, took part in this exact handshake. A copied certificate is useless without the private key.
  4. Finished. Each side sends a MAC (an HMAC keyed from the handshake secret) over the entire transcript. If an attacker had altered any earlier message, for example to remove a strong cipher suite from the list, the MACs would not match and the handshake would fail.
  5. Application keys. Both sides derive new keys for the data, and the encrypted records begin to flow.

Because the certificate is itself encrypted in TLS 1.3, passive observers no longer see which certificate the server presented. The requested host name in the ClientHello (SNI) is still visible unless Encrypted Client Hello is in use.

X25519: the key exchange behind most handshakes

The key share in most connections today is an X25519 public key: elliptic-curve Diffie-Hellman on Curve25519, with 32-byte keys. The function below is the Montgomery ladder from RFC 7748. It is written for clarity, not for security: it is not constant-time, so use a vetted library for real work. The example checks it against the test values published in the RFC.

Python X25519 checked against RFC 7748

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only the two 32-byte public values cross the network. An eavesdropper who sees both still cannot compute the shared secret without solving a discrete logarithm on the curve.

A simulated handshake

The next example wires the pieces together in one page of code. It is a simplification of TLS 1.3, not an implementation: messages are Python byte strings instead of the real wire format, the cipher is a small HMAC-based stream with a tag (like the toy cipher from the key derivation lesson), and the server's certificate is replaced by its bare public key with a toy RSA signature. What it keeps is the structure: ephemeral key shares, a transcript hash, key schedule, CertificateVerify, Finished and traffic keys that differ per direction.

Python Simulate a TLS 1.3-style handshake

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what the signature and Finished steps accomplish. The signature ties the server's identity to this handshake's transcript; the Finished MACs tie both parties to everything that was said. If an attacker had silently removed CHACHA20 from the client's list, the transcripts would differ and the check would fail.

Forward secrecy and why static RSA was dropped

Older TLS versions allowed the client to pick a secret and encrypt it directly under the server's long-term RSA public key. That is simple, but it means the server's private key can decrypt every past session that was ever recorded. A court order, a breach or a Heartbleed-style memory leak turns years of captured traffic into plaintext.

Forward secrecy (also called perfect forward secrecy) avoids this. In an ephemeral Diffie-Hellman exchange each side creates a fresh key pair for every connection and deletes it afterwards. The long-term key is used only to sign, which authenticates the handshake but never encrypts the secret, so stealing it later reveals nothing about earlier sessions. TLS 1.3 requires an ephemeral exchange; static RSA key exchange does not exist in it.

Python Why ephemeral keys protect past traffic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

(The toy group in this example is far too small to be secure; it only illustrates which secret protects what.)

Cipher suites and negotiation

A cipher suite names the algorithms a connection will use. In TLS 1.2 a name such as TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 packs several choices together: ECDHE key exchange, RSA signatures, AES-128 in GCM mode, and SHA-256 for the key derivation. TLS 1.3 shrank the list dramatically. A suite now names only the AEAD cipher and the hash:

  • TLS_AES_128_GCM_SHA256
  • TLS_AES_256_GCM_SHA384
  • TLS_CHACHA20_POLY1305_SHA256

Key exchange groups (X25519, P-256, and more recently hybrid groups that combine X25519 with a post-quantum scheme, see post-quantum cryptography) and signature algorithms are negotiated in separate extensions. TLS 1.3 removed everything that had caused trouble: RSA key exchange, static Diffie-Hellman, CBC-mode ciphers, RC4, SHA-1 signatures, compression and renegotiation.

The server normally chooses; the usual rule is to prefer the strongest suite both sides share. If the only common suite is weak, a well-configured server refuses rather than agrees.

Python Cipher suite negotiation and downgrade checking

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Extras you will meet

  • Session resumption and 0-RTT. After a first connection, the server can give the client a ticket so that a later handshake is cheaper. TLS 1.3 also allows "early data" sent in the very first flight, but that data can be replayed by an attacker, so it should only carry requests that are safe to repeat.
  • Mutual TLS (mTLS). The server also asks the client for a certificate. It is common between internal services.
  • ALPN. An extension in which the client lists application protocols (h2, http/1.1) so no extra round trip is needed to decide.
  • Library choices. Do not implement TLS yourself. Use the platform's library, enable TLS 1.2 and 1.3 only, keep it patched, and never disable certificate validation "just for testing".

VPNs and tunnelling protocols

A virtual private network (VPN) builds an encrypted tunnel across an untrusted network such as the internet: packets are wrapped (encapsulated) inside other packets, encrypted and authenticated, and unwrapped at the far end. Everything you have learned applies: a key exchange (with forward secrecy), certificates or pre-shared keys for authentication, and an authenticated cipher for the traffic. Typical uses are remote access (a laptop joining a company network), site-to-site links between offices, and privacy from a local network or ISP. A VPN does not make you anonymous, and it only moves your trust to the VPN provider, who now sees your traffic's destinations.

The main protocols:

Protocol Layer and idea Notes
IPsec (with IKEv2) Network layer. IKEv2 negotiates keys and authenticates; ESP encrypts packets Built into most operating systems; the standard for site-to-site links. L2TP/IPsec adds L2TP for the tunnel and IPsec for the encryption
OpenVPN Uses TLS for the handshake, then its own encrypted tunnel over UDP or TCP Open source and very flexible; needs a client app
WireGuard Modern, tiny codebase; Curve25519 key exchange, ChaCha20-Poly1305 Fast, with a very small attack surface; keys are exchanged out of band
SSTP Tunnels PPP through an HTTPS (TLS) connection Windows-centric; passes through firewalls on port 443
PPTP Point-to-Point Tunneling Protocol, one of the oldest Broken (weak MS-CHAPv2 authentication and RC4 encryption); never use it

VPN designs also differ by purpose: a remote-access VPN connects one user to a network, a site-to-site VPN joins two networks permanently (an intranet or extranet VPN when partners are included), and an MPLS VPN is a provider-managed private network that separates customers by routing, not by encryption, so it is private but not necessarily encrypted. A split tunnel sends only some traffic through the VPN; a full tunnel sends everything.

Rule of thumb for choosing: WireGuard or IKEv2/IPsec for new deployments, OpenVPN where its flexibility is needed, and nothing built on PPTP. A good provider also needs a no-logs policy you can verify, DNS leak protection and a kill switch that stops traffic if the tunnel drops.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the TLS 1.3 server's CertificateVerify message prove?

    The server signs a hash of the handshake transcript with its certificate's private key. A copied certificate alone cannot produce that signature.
  2. 2. Why does a client send a key share in its very first message in TLS 1.3?

    By including an X25519 (or similar) public key up front, the client lets the server complete the key exchange in its first reply. If the guess is wrong, one extra round trip is needed.
  3. 3. What is forward secrecy?

    With ephemeral key exchange the session secret depends on throwaway private values that are deleted after the handshake, while the long-term key only signs.
  4. 4. What do the Finished messages protect against?

    Each Finished is a MAC over the whole transcript. If an attacker changed any handshake message, the two sides would compute different MACs and the handshake would fail.
  5. 5. Which is true of TLS 1.3 compared with TLS 1.2?

    TLS 1.3 removed RSA key exchange, static Diffie-Hellman and legacy ciphers, and encrypts everything after the ServerHello, including the certificate.

Summary

  • TLS has a handshake (agree on parameters, authenticate, derive keys) and a record layer (encrypt and authenticate the data).
  • TLS 1.3 needs one round trip: ClientHello with a key share, ServerHello with a key share, then an encrypted Certificate, CertificateVerify and Finished.
  • Keys come from the ephemeral shared secret and a hash of the transcript through HKDF; separate keys protect each direction and each phase.
  • Signatures authenticate the server, Finished MACs bind everyone to the whole transcript, and ephemeral key exchange gives forward secrecy.
  • TLS 1.3 removed legacy features; cipher suites list only the AEAD and hash. Always use a maintained library with certificate validation enabled.

Next: Secret Sharing and Zero-Knowledge Basics, two protocols that do more than encrypt.