Speed

A Second ClientHello

A client sends key shares for the groups it guesses; a server that supports none of them but another group the client listed answers with a HelloRetryRequest and the client sends a second ClientHello. Six flights instead of four, and still a finished handshake.

The question

The ClientHello carries key shares for the groups that the client guesses the server will accept. What happens when the server supports none of the guessed groups, but supports another group that the client also listed? The server could give up. It does not: it asks the client to try again.

This is a negotiation, not a failure, and the handshake still finishes. How many flights does it take, and how can a program tell that a second ClientHello was sent?

Work it by hand

RFC 9846 section 2.1 draws it in its Figure 2, and section 4.2.4 defines the HelloRetryRequest. The client sends a ClientHello with a key share. The server answers with a HelloRetryRequest that names the group it wants. The client sends a second ClientHello with a key share for that group, and the handshake goes on as in the first lesson.

Counted as flights for the demo below, where the server takes only P-256: client, server, client, server, client, server. That is six, instead of the four of the first lesson, because of the extra pair at the start. The lengths are from this build.

Run it

The program is the standard block with one change: line 2 gives the server only the group in server_group, and the client keeps its own default groups. The loop of lines 6 to 15 draws one arrow per flight, and line 18 returns the number of flights. The group that was negotiated, read in line 17, is secp256r1 in this build.

A message diagram with client and server: flight 1 from the client, 1555 bytes; flight 2 from the server, 99 bytes; flight 3 from the client, 374 bytes; flight 4 from the server, 2359 bytes; flight 5 from the client, 74 bytes; flight 6 from the server, with the note length varies.
Watch the steps (recorded video)

Public test material, generated for this lesson. Never use it for anything real.

let server = ssl.server_ctx(ssl.parse_cert(leaf_pem), ssl.parse_key(leaf_key));
server.chain(ssl.parse_cert(int_pem)); server.groups(server_group);
let client = ssl.client_ctx(); client.add_ca(ssl.parse_cert(root_pem));
let pair = ssl.tls_pair(client, server, host);
let messages = str_list(); let turns = 0; let flights = 0;
while (!pair.done() && pair.error() == "" && turns < 10) {
  let flight = pair.step(); turns = turns + 1;
  if (str_len(flight) > 0) {
    flights = flights + 1;
    let way = "1>0|"; if (pair.from() == "client") { way = "0>1|"; }
    let size = str(str_len(flight)) + " bytes";
    if (pair.done()) { size = "length varies"; }
    messages.add(way + "flight " + str(flights) + "|" + size);
  }
}
lmcode_return_put("messages", messages);
lmcode_return_put("group", pair.group());
let answer = flights;
{
  "answer": 6,
  "messages": [
    "0>1|flight 1|1555 bytes",
    "1>0|flight 2|99 bytes",
    "0>1|flight 3|374 bytes",
    "1>0|flight 4|2359 bytes",
    "0>1|flight 5|74 bytes",
    "1>0|flight 6|length varies"
  ],
  "group": "secp256r1"
}

The client's first flight is 1555 bytes in this build because it offers a post-quantum key share without being asked; lengths are what this build does. You can change server_group and host. The group X25519 gives the 4 flights of the first lesson, and X25519MLKEM768:X25519 also gives 4, with the group X25519MLKEM768 negotiated: the client's first guess is accepted.

The length of the last flight is not shown because it is not the same in every run (in some repeated runs it was 16 bytes shorter), so the picture says length varies.

A name that no group matches, such as P-256x, stops line 2 with "Line 2: TlsCtx.groups: no usable group in 'P-256x'". A name the certificate does not cover, such as shop.example.test, gives 5 flights, the last of 7 bytes: the retry happens first and the client then refuses the certificate. A name of one character, such as x, stops line 4 with "Line 4: Ssl.tls_pair: set host to verify".

Your turn

The program below is meant to return retry, direct or failed for the groups that the client and the server are given. client_groups takes X25519, P-256 or X25519:P-256, the client's default order, and server_group takes X25519 or P-256. A name that no group matches stops line 2 (the server) or line 4 (the client) with the message of Run it.

Decide whether it does what it should, and find the candidates that do. Check runs each candidate on the data shown and on eight others.

The server group P-256, the client groups X25519:P-256, the name example.test, and the word that the shown program returns.

Public test material, generated for this lesson. Never use it for anything real.

let server = ssl.server_ctx(ssl.parse_cert(leaf_pem), ssl.parse_key(leaf_key));
server.chain(ssl.parse_cert(int_pem)); server.groups(server_group);
let client = ssl.client_ctx(); client.add_ca(ssl.parse_cert(root_pem));
client.groups(client_groups);
let pair = ssl.tls_pair(client, server, host);
let hellos = 0; let turns = 0; let flights = 0;
while (!pair.done() && pair.error() == "" && turns < 10) {
  let flight = str_hex_encode(pair.step()); turns = turns + 1; let at = 0;
  if (hex_len(flight) > 0) { flights = flights + 1; }
  let mine = pair.from() == "client" && hex_len(flight) > 0;
  if (mine && hex_u8(flight, 0) == 22) { hellos = hellos + 1; }
}
let answer = "failed";
if (pair.done() && hellos == 1) { answer = "direct"; }
if (pair.done() && hellos == 2) { answer = "retry"; }
{
  "answer": "direct"
}

The program builds a TLS client and a TLS server from the test chain with the groups in client_groups (X25519, P-256 or X25519:P-256, the client's default order) and server_group, joins them in memory and steps them. Return a text: retry if the handshake finished after the client sent a second ClientHello, direct if it finished with one ClientHello, and failed if it did not finish. Check runs your program with other groups and names.

Read the result carefully

A server that takes only P-256 is slower, not broken: the handshake finishes, with one more pair of flights. A client that offers no group the server accepts gets a failure after two flights instead, the second a short alert. The lengths and the group names are what this build does, the two ends are test endpoints in this tab's memory, and the certificates are test material.

The recording of A TLS 1.3 Handshake, Message by Message has no HelloRetryRequest. The test chain is valid from 2026-01-01 to 2036-01-01 and the client checks the dates against the reader's clock; outside those years the handshake fails with a certificate error.

Recap

  • A server that supports another group than the ones guessed sends a HelloRetryRequest.
  • The client then sends a second ClientHello and the handshake takes six flights.
  • That is a negotiation, and done() is true at the end.
  • Groups and lengths are what this build does.