Mobile system design, from a packet to an app · Puzzle 4 · 5 of 11

What HTTPS adds

Every request your app makes is encrypted. That adds three promises on top of TCP, and one more step before the first byte. Here's what you get and what it costs, without the cryptography.

The problem

Puzzle 0 said TLS "encrypts the stream" and moved on. On the café's Wi-Fi, every router between the phone and your server sees your packets. What exactly stops them from reading or changing a request, or from pretending to be your server? And what does that protection cost?

See it

Between the phone and the server, routers see only scrambled bytes; a changed byte is detected; the server shows a certificate signed by an authority the phone trusts; setting this up is one handshake round trip phone api.example.com routers see: x7#Qe… a changed byte: rejected certificatesigned by an authority the phone trusts handshake: one round trip
  1. HTTPS is HTTP through TLS. Routers on the way see only scrambled bytes: encryption.

    Step 1 of 4
  2. If one changes a byte, TLS notices and rejects the data: integrity.

    Step 2 of 4
  3. The server shows a certificate, signed by an authority your phone trusts: authentication. Predict: what does setting this up cost?

    Step 3 of 4
  4. A handshake when the connection opens: one round trip in TLS 1.3, on top of TCP's.

    Step 4 of 4

The building block

HTTPS is ordinary HTTP sent through TLS, Transport Layer Security, which sits between HTTP and TCP. TLS gives three promises:

  1. Encryption: anyone on the way sees only scrambled bytes.
  2. Integrity: a changed byte is detected, and the data is rejected.
  3. Authentication: you're talking to the server you named, not an impostor. The server proves it with a certificate: a document, signed by an authority your phone already trusts, that says "this key belongs to api.example.com".

To set this up, client and server run a handshake when the connection opens: they agree on keys and the server shows its certificate. In TLS 1.3, the current version, that takes one round trip, on top of TCP's own.

You don't write any of it: OkHttp and Android do it for you. What you need to know is that it happens once per new connection, and that it costs a round trip, which is why the next puzzle cares so much about reusing connections. (How the keys are agreed in public, and how a certificate can be trusted, is the Encryption course.)

The analogy

A sealed envelope (encryption), with a wax seal that breaks if anyone opens it (integrity), sent to an office that shows an ID card issued by an authority you trust (authentication). Agreeing on the envelope and checking the ID takes one exchange before the first real letter.

Check yourself

A router on the café Wi-Fi changes one byte of your HTTPS response. What happens?

Not quite. Integrity means a change is detected, not passed on.

Right. Integrity: changes are caught.

Not quite. They can tamper; TLS makes tampering detectable.

What does the TLS handshake cost on a new connection?

Not quite. Agreeing on keys needs an exchange with the server: a round trip.

Right. Once per connection, one round trip.

Not quite. It happens once, when the connection opens.

What's the difference between encryption and authentication in TLS?

Not quite. That's the two swapped. Re-read that part ↑

Right. Secret vs identity.

Sources

What you now know

HTTPS is HTTP sent through TLS. TLS gives three promises: encryption (nobody on the way can read it), integrity (nobody can change it unnoticed) and authentication (you're really talking to the server you named, proven by its certificate). To set that up, client and server run a TLS handshake when the connection opens; in TLS 1.3 it costs one round trip, on top of TCP's.