Skills101

AdvancedComputer Networks · Lesson 14 of 15 · Updated ·

How HTTPS Works — TLS Handshake, Key Exchange, Certificates and Chain of Trust Explained

Lesson 5 said that HTTPS is "HTTP over TLS" and gives encryption, integrity and authentication. This lesson opens the box. By the end you should be able to answer the common interview question "What happens in a TLS handshake?" and the follow-up "How do two computers agree on a secret key when everyone can see their messages?"

1. The problem TLS solves

You connect to your college's results website over public Wi-Fi. Without TLS, three things can go wrong:

ThreatExampleTLS protection
Eavesdroppingsomeone on the Wi-Fi reads your passwordencryption
Tamperinga router changes "pay ₹500" to "pay ₹900"integrity checks
Impersonationa fake server pretends to be the real sitecertificates (authentication)

2. Two kinds of encryption

  • Symmetric encryption — one shared key encrypts and decrypts. Examples: AES, ChaCha20. It is very fast, so it is used for all the actual data. The problem: how do both sides get the same key without sending it in the open?
  • Asymmetric (public-key) cryptography — a key pair: a public key that anyone may see and a private key that the owner keeps secret. Used for key exchange and digital signatures. It is much slower, so it is used only during the handshake.

TLS uses both: asymmetric methods to agree on a key and prove identity, then symmetric encryption for everything else.

3. Agreeing on a key in public: Diffie-Hellman

The Diffie-Hellman (DH) key exchange lets two sides build the same secret even though an eavesdropper sees every message. A small-number version shows the idea:

p, g = 23, 5            # public: a prime and a base (real TLS uses huge numbers or elliptic curves)
a, b = 6, 15            # private: browser's secret a, server's secret b

A = pow(g, a, p)        # browser sends A in its key share
B = pow(g, b, p)        # server sends B in its key share
print("sent over the network:", A, B)

browser_key = pow(B, a, p)
server_key = pow(A, b, p)
print("browser computes:", browser_key)
print("server computes: ", server_key)
sent over the network: 8 19
browser computes: 2
server computes:  2

How does it work?

  1. Everyone knows p = 23 and g = 5.
  2. The browser picks a secret a = 6 and sends A = 5⁶ mod 23 = 8.
  3. The server picks a secret b = 15 and sends B = 5¹⁵ mod 23 = 19.
  4. The browser computes B^a mod p; the server computes A^b mod p. Both equal g^(ab) mod p = 2.

An eavesdropper sees 23, 5, 8 and 19, but to find the key it must recover a or b from them (the discrete logarithm problem). With tiny numbers that is easy; with the very large numbers or elliptic curves used in real TLS, it is not practical with today's computers.

TLS 1.3 creates fresh DH secrets for every connection (ephemeral DH). So even if the server's long-term private key is stolen later, old recorded traffic cannot be decrypted. This property is called forward secrecy.

4. The TLS 1.3 handshake

After the TCP handshake, the browser and server run TLS:

TLS 1.3 handshake as a ladder: the browser sends ClientHello with its key share, the server replies with ServerHello and its key share plus its certificate, signature and Finished message, the browser sends Finished with the first HTTP request, and the server sends the encrypted HTTP response — one round trip before data flows

  1. ClientHello — the TLS versions and cipher suites the browser supports, a random number, its DH key share, and the site name it wants (SNI, server name indication — one IP can host many sites).
  2. ServerHello — the server's choices and its own key share. Now both sides can compute the shared secret, so everything after this is encrypted.
  3. Certificate + CertificateVerify — the server's certificate, plus a signature over the handshake made with its private key. This proves it owns the key in the certificate.
  4. Finished (both sides) — a check value over the whole handshake, so any tampering with earlier messages is detected.
  5. The browser can send its HTTP request with its Finished message.

Round trips. TLS 1.3 needs 1 RTT. The older TLS 1.2 needed 2 RTTs. When reconnecting to a known server, TLS 1.3 can even send data in the first message (0-RTT resumption), at the cost of some replay risk, so it should be used only for safe requests.

5. Certificates and the chain of trust

The key share alone does not prove who is on the other end — an attacker could send its own key share. The certificate links a public key to a domain name. It contains:

  • the domain name(s) it is valid for,
  • the server's public key,
  • the issuer (the certificate authority, or CA) and the issuer's signature,
  • validity dates.

Chain of trust: the root CA, already built into the operating system or browser, signs an intermediate CA, which signs the certificate for skills101.in; the browser checks each signature, the name, and the expiry and revocation status

Browsers and operating systems ship with a list of trusted root CAs. Roots usually sign intermediate CAs, and intermediates sign website certificates. The browser checks:

  1. Every signature in the chain is valid, up to a trusted root.
  2. The domain in the certificate matches the site.
  3. The certificate is within its validity dates and has not been revoked.

If any check fails, you see a full-page warning. Never train users to click past it.

6. Integrity: detecting changes

Even one changed character completely changes a cryptographic hash:

import hashlib

msg1 = b"pay 500 to Ravi"
msg2 = b"pay 900 to Ravi"
print(hashlib.sha256(msg1).hexdigest()[:16])
print(hashlib.sha256(msg2).hexdigest()[:16])
dfdd5367eb18acc1
e790823b49bdd211

TLS uses authenticated encryption (for example AES-GCM): every record carries a tag computed with the secret key. A changed byte makes the tag check fail and the record is rejected. A plain hash alone is not enough, because an attacker could change the message and recompute the hash — the tag needs the key.

7. Cost of HTTPS: a numerical

RTT between you and the server = 50 ms. Ignore DNS and server processing. Time until the first byte of the response arrives:

StepTLS 1.2TLS 1.3
TCP handshake1 RTT1 RTT
TLS handshake2 RTT1 RTT
HTTP request → response1 RTT1 RTT
Total4 RTT = 200 ms3 RTT = 150 ms

This is why TLS 1.3 and connection reuse (keeping one connection open for many requests) matter so much on slow mobile networks. HTTP/3 goes further by running over QUIC, which combines the transport and TLS handshakes.

8. What HTTPS hides — and what it does not

Hidden from the networkStill visible
the full URL path and query (/results?roll=21BCA045)the server's IP address
headers, cookies, passwordsthe site name, through DNS and usually the SNI field
page contenthow much data is sent, and when

A server sends the HSTS header (Strict-Transport-Security) to tell the browser "always use HTTPS for this site". That blocks attacks that try to keep you on plain HTTP.

Common mistakes

  • Saying the data is encrypted with the server's public key — the data uses a symmetric key agreed through (EC)DH.
  • Saying the certificate encrypts the traffic — it authenticates the server.
  • Thinking HTTPS hides which website you visit — the domain usually leaks via DNS and SNI.
  • Claiming TLS 1.3 takes 2 round trips — that was TLS 1.2.
  • Calling the padlock a sign that a site is honest — it only means the connection is private and the domain matches the certificate.

Quick recap

  • TLS gives encryption, integrity and authentication.
  • Asymmetric crypto for key exchange and signatures; fast symmetric crypto for the data.
  • Diffie-Hellman: both sides compute g^(ab) mod p; ephemeral keys give forward secrecy.
  • TLS 1.3: ClientHello + key share → ServerHello + key share, certificate, signature, Finished → data. 1 RTT (TLS 1.2: 2).
  • Certificate chain: site → intermediate → trusted root; check signatures, name, dates, revocation.
  • Time to first byte over a new HTTPS connection with TLS 1.3 = 3 RTT.

Practice what you learned

📝 Quiz: How HTTPS Works5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →