TLS handshakes
Security has a price in time
Section titled “Security has a price in time”A connection over the open internet needs three things from TLS: that nobody on the path can read the data, that nobody can change it unnoticed, and that the client is talking to the server it meant to, which the server proves with a certificate. To provide them, the two ends have to agree on keys, and that takes messages. Those messages are the handshake, and they have to cross the network before the first byte of the request can be sent safely.
The cost this lesson follows is the round trips. Each time one side has to wait for the other’s reply before it can send the next message, the whole path’s round-trip time is added.
Mental model. A handshake is a number of round trips. Multiply it by the round-trip time, and that is how long the client waits before the request can go. Everything the protocols do to be faster is a way of having fewer round trips.
Counting the round trips
Section titled “Counting the round trips”TLS 1.2 does a full handshake in two round trips. RFC 5246’s message flow has four flights: the client’s hello; the server’s hello with its certificate; the client’s key exchange and its Finished message; and the server’s Finished message. Application data starts only after the last. Add TCP’s one round trip first, and the request goes in the fourth round trip: 1 for TCP, 2 for TLS, 1 for the request and its answer.
TLS 1.3 needs one. RFC 8446 requires that application data is not sent before the Finished message, except for the 0-RTT case described below, and the client’s Finished message can travel in the same flight as the request. The count becomes 1 for TCP, 1 for TLS, 1 for the request and its answer: three.
QUIC combines the transport handshake and TLS 1.3 into one. RFC 9001 says that most new connections can be established and secured within a single round trip. The count is 1 for the combined handshake and 1 for the request: two.
Try it: round trips on the wire
Section titled “Try it: round trips on the wire”The lab below shows the messages of a new connection under five variants. Step through the first one and count how many times a message goes to the server and back before the HTTP request. Then compare the bars.
What a new connection costs
A new connection over TCP with TLS 1.2, as HTTP/1.1 and HTTP/2 can use it: one round trip for TCP, two for TLS, one for the request.
- TCP handshake40 msClient to Server: SYN. Arrives at 40 ms.SYN
- 80 msServer to Client: SYN-ACK. Arrives at 80 ms.SYN-ACK
- TLS handshake120 msClient to Server: ACK + ClientHello. Arrives at 120 ms.ACK + ClientHello
- 160 msServer to Client: ServerHello, Certificate, ServerHelloDone. Arrives at 160 ms.ServerHello, Certificate, ServerHelloDone
- 200 msClient to Server: ClientKeyExchange, ChangeCipherSpec, Finished. Arrives at 200 ms.ClientKeyExchange, ChangeCipherSpec, Finished
- 240 msServer to Client: ChangeCipherSpec, Finished. Arrives at 240 ms.ChangeCipherSpec, Finished
- HTTP request and response280 msClient to Server: HTTP request. Arrives at 280 ms.Processing: 20 msHTTP request
- 340 msServer to Client: HTTP response. Arrives at 340 ms.HTTP response
Time until the response arrives: 340 ms4 round trips on the wire · 20 ms of processing
Model notes
Assumptions
- Each message takes half the round-trip time of the link it crosses. Messages are never lost, never queue and never overlap, and each leaves when the one before it has arrived, once its receiver has finished any processing.
Simplifications
- Only the messages that carry the handshakes and the request are drawn. TCP's slow start, the size of the certificate chain, packet loss and the time to compute the cryptography are left out, and each of them adds time to a real connection.
- Resuming a session and 0-RTT need an earlier connection to the same server. Data sent as 0-RTT can be replayed by an attacker, so it is meant for requests that are safe to repeat.
- The response is a single message. A large response is several messages and takes longer.
Parameter values are illustrative. They are not benchmarks of any real product.
At a round-trip time of 80 ms and a server that needs 20 ms, the response arrives after 340 ms over TCP with TLS 1.2, 260 ms with TLS 1.3, and 180 ms with QUIC. The difference between them is only the number of round trips: 4, 3 and 2 times 80 ms, plus the same 20 ms.
Now raise Round-trip time to 300 ms, which is a client on the far side of the world, and the totals become 1,220 ms, 920 ms and 620 ms. The same handshakes, the same server, and the user waits more than a second for the first response with TLS 1.2. The server’s 20 ms did not change, and it does not matter. When the round-trip time is long, the number of round trips is the cost. At 20 ms, which is a client in the same region, the same three variants take 100 ms, 80 ms and 60 ms.
Resumption and 0-RTT
Section titled “Resumption and 0-RTT”A client that has talked to the server before can remember something from the last time. TLS 1.2 can resume a session with an abbreviated handshake of one round trip instead of two, as RFC 5246’s second figure shows. TLS 1.3 goes further: a resumed connection can send application data in the very first flight, called 0-RTT or early data, and QUIC does the same with its own first packet.
The last two variants in the lab are these. Over QUIC the response arrives after 100 ms, one round trip plus the server’s time, and over TCP with TLS 1.3 after 180 ms, because TCP’s handshake still has to finish first.
There is a price. RFC 8446 warns that early data is not forward secret, and that there are no guarantees of non-replay between connections. An attacker who records the first flight can send it again, and the server may act on it a second time. So 0-RTT is suitable for requests that are safe to repeat, such as reading a page, and not for a request that changes something, such as placing an order, unless the application protects against repeats.
What happens if your users are far from your servers?
Section titled “What happens if your users are far from your servers?”A service has a single data centre, and a user in another continent connects with TLS 1.2 over a round trip of 300 ms. The page needs the first response, and the user waits 1,220 ms for it, before the browser has received any HTML. Every extra connection the page opens pays the same again.
There are three ways to bring that down, and the course follows them in turn. Fewer round trips: TLS 1.3 and QUIC, as above. Reusing the connection, so that the handshake is paid once: the lesson on connection pooling. A shorter distance: a server near the user that ends the connection, so that every round trip is 20 ms instead of 300. At 20 ms, TLS 1.2 takes 100 ms, a twelfth of the time, and the CDN lesson shows how that server gets its content.
- A secure connection needs a handshake, and the cost this lesson counts is round trips. Multiply the count by the round-trip time.
- TLS 1.2 needs two round trips for a full handshake, TLS 1.3 needs one, and QUIC folds the transport handshake into TLS 1.3, so a new connection needs one.
- With TCP, add one for the TCP handshake and one for the request. At 80 ms and a 20 ms server: 340 ms for TLS 1.2, 260 ms for TLS 1.3, 180 ms for QUIC.
- At 300 ms, the same variants take 1,220 ms, 920 ms and 620 ms. At 20 ms, 100, 80 and 60. The longer the distance, the more each round trip costs.
- Resuming a session saves a round trip, and 0-RTT sends the request in the first flight, so QUIC with 0-RTT answers in 100 ms. Early data can be replayed, so it is for requests that are safe to repeat.
- To pay less: fewer round trips, a connection that is reused, and a server that is closer.
Next: HTTP versions, and what each does with a connection once it is open.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.