Speed

A Flight Is Several Records

A flight is not one message and not one record: it is a run of records, each with a 5-byte header that says its type, a version and its length. Split the flights of a handshake made in this tab by the headers alone, and count the protected records.

The question

The last lesson counted flights. A flight is whatever one end hands to the other in one turn, and it is not one message and not one record. How is a flight built, and how can a program take it apart without reading anything but headers?

Every TLS record starts with 5 bytes: one byte of type, two bytes of version and two bytes of length (RFC 9846 section 5.1; the fields are the subject of TLS Records Carry a Length). The length says how many bytes follow, so the next record begins 5 plus that length further on.

Work it by hand

Take the first server flight of the handshake of the last lesson. Its first five bytes are 16 03 03 00 7a. The type 0x16 is 22, a handshake record. The version is 0303. The length 0x007a is 122, so that record is 5 plus 122 = 127 bytes long, and the next record starts at byte 127.

The types are 22 for handshake, 20 for change_cipher_spec and 23 for application_data (RFC 9846 section 5.1). Once the keys are in use every protected record has the outer type 23, whatever it carries inside (RFC 9846 section 5.2), so an observer sees only that type and a length.

Run it

The program steps the pair and splits every flight with two loops. Line 5 joins the two test endpoints for the name in host. The inner loop of lines 9 to 16 reads the length in line 10 and the version in line 11, adds one row, and jumps to the next header in line 15. Only header bytes are read, never the bytes behind them: those are different on every run.

A table of 11 records. Flight 1 from the client: type 22, version 0301, length 310. Flight 2 from the server: 22 with 122, 20 with 1, then four of type 23 with 23, 1806, 281 and 69. Flight 3 from the client: 20 with 1 and 23 with 69. Flight 4 from the server: two records of type 23 whose length is shown as varies. All later versions are 0303.
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("X25519");
let client = ssl.client_ctx(); client.add_ca(ssl.parse_cert(root_pem));
client.groups("X25519");
let pair = ssl.tls_pair(client, server, host);
let rows = str_list(); let turns = 0;
while (!pair.done() && pair.error() == "" && turns < 10) {
  let flight = str_hex_encode(pair.step()); turns = turns + 1; let at = 0;
  while (at + 5 <= hex_len(flight) && rows.len() < 16) {
    let size = hex_u16(flight, at + 3);
    let version = str_substr(flight, 2 * (at + 1), 4);
    let row = str(turns) + "|" + pair.from() + "|" + str(hex_u8(flight, at));
    let shown = str(size); if (pair.done()) { shown = "varies"; }
    rows.add(row + "|" + version + "|" + shown);
    at = at + 5 + size;
  }
}
lmcode_return_put("rows", rows);
let answer = rows.len();
{
  "answer": 11,
  "rows": [
    "1|client|22|0301|310",
    "2|server|22|0303|122",
    "2|server|20|0303|1",
    "2|server|23|0303|23",
    "2|server|23|0303|1806",
    "2|server|23|0303|281",
    "2|server|23|0303|69",
    "3|client|20|0303|1",
    "3|client|23|0303|69",
    "4|server|23|0303|varies",
    "4|server|23|0303|varies"
  ]
}

The picture is a table with one row per record, and line 19 returns the number of rows, 11. Only the first record carries the version 0301, which RFC 9846 section 5.1 allows for an initial ClientHello, and all later ones 0303. The length of the first record, 310 here, is from this build (lmssl 0.2.2, OpenSSL 4.0.3).

The lengths of the two records of the last flight are not shown: in some repeated runs the second was 16 bytes shorter, so the table says varies for the flight after which done() is true.

You can change host. A name the certificate does not cover, such as shop.example.test, still runs, but the client refuses the certificate: the table then has 8 records, and the third flight is one record of type 21, an alert, with length 2. example.test and an empty name give 11 records again; their first records are shorter (306 and 285 bytes, from this build) because the name travels in the ClientHello.

A name of one character (x), a name with a final dot, a name with a space and a name with a hyphen at the start or end of a label stop line 5 with "Line 5: Ssl.tls_pair: set host to verify". The four PEM texts are test material: leave them as they are.

Your turn

The program below is meant to return, for each flight that carried bytes, how many of its records have the outer type 23, as a list with one number per flight. It asks the server for server_group and leaves the client's groups at their default.

server_group takes X25519 or P-256; a name that no group matches, such as P-256x, stops line 2 with "Line 2: TlsCtx.groups: no usable group in 'P-256x'". Decide whether the program does what it should, and find the candidates that do. Check runs each candidate on the data shown and on four others.

The name example.test, the server group X25519 and the list 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));
let pair = ssl.tls_pair(client, server, host);
let answer = int_list(); let turns = 0; let records = 0;
while (!pair.done() && pair.error() == "" && turns < 10) {
  let flight = str_hex_encode(pair.step()); turns = turns + 1;
  let at = 0; let count = 0;
  while (at + 5 <= hex_len(flight)) {
    if (hex_u8(flight, at) == 22) { count = count + 1; }
    at = at + 5 + hex_u16(flight, at + 3); records = records + 1;
  }
  if (hex_len(flight) > 0) { answer.add(count); }
}
lmcode_return_put("records", records);
{
  "answer": [
    1,
    1,
    0,
    0
  ],
  "records": 11
}

The program builds a TLS client and a TLS server from the test chain, with the server asking for the group in server_group and the client keeping its default groups, joins them in memory and steps them. Return, as a list with one whole number for each flight that carried bytes, how many records of that flight have the outer type 23 (application_data). Each flight is a run of records with a 5-byte header: type, version, length. Check runs your program with other names, groups and test material.

Read the result carefully

The page splits only what step() returned whole, so the walk always lands on a header. The bodies of the type-23 records are encrypted and different on every run, which is why the page shows headers and lengths and nothing else. The certificates and the key are test material, and the two ends are test endpoints in this tab's memory.

The handshake of this page is not the recording of A TLS 1.3 Handshake, Message by Message, whose first lesson lists its thirteen records. 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 ends with a certificate error after three flights.

Recap

  • A flight is a run of records; each record has a 5-byte header.
  • The header's length field gives the offset of the next header.
  • Protected records all show the outer type 23.
  • The first record of a ClientHello may carry the version 0301; later ones 0303.