Speed

The Key Log Has Five Labels

keylog() is what both ends of a handshake would write to a key log file: one line for each secret, in the form LABEL client_random secret. Count the lines as the handshake goes and the labels at the end, without ever showing a secret.

The question

Both ends of the pair can tell what they would write to a key log file, the file that lets a tool decrypt a capture. pair.keylog() returns it as text. Each line has the form LABEL client_random secret: the key log format OpenSSL writes. This lesson counts its lines and its labels and shows how they appear as the handshake goes, and it never prints a value: the secrets of this handshake are test values, but a page that printed them would teach the habit.

How many lines are there when the handshake is done, and how many different labels do they carry?

Work it by hand

The lines appear as the handshake goes, because each end logs a secret once it has derived it. After the client's first flight there is nothing to log. The server derives its handshake secrets when it answers, so lines appear after the second flight, then more after the client's reply, and the last one after the server's final flight.

The demo below counts the lines of the key log that end with a line break after every flight, and at the end it collects the different labels, which are names and not secrets. The count for the whole handshake is what the program returns as its answer.

Run it

The function in lines 1 to 5 counts the lines of a text that end with a line break. The loop of lines 12 to 16 steps the pair and adds a row for every flight that carried bytes, with the number of key log lines at that moment. The loop of lines 18 to 22 splits each line at its spaces and keeps the first word, the label, if it is new. Line 23 returns the rows and the labels, and line 24 returns the final count.

A table with four rows: after flight 1, from the client, 0 key log lines; after flight 2, from the server, 4; after flight 3, from the client, 9; after flight 4, from the server, 10.
Watch the steps (recorded video)

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

fn n_lines(t: string) -> int {
  let k = str_split(t, "\n").len() - 1;
  if (str_len(t) == 0) { k = 0; }
  return k;
}
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 = pair.step(); turns = turns + 1; let n = n_lines(pair.keylog());
  let row = str(turns) + "|" + pair.from() + "|" + str(n);
  if (str_len(flight) > 0) { rows.add(row); }
}
let lines = str_split(pair.keylog(), "\n"); let seen = "|";
for (i in range(0, lines.len())) {
  let w = str_split(lines.at(i) + "  ", " ");
  let fresh = str_len(w.at(0)) > 0 && !str_contains(seen, "|" + w.at(0) + "|");
  if (fresh) { seen = seen + w.at(0) + "|"; }
}
lmcode_return_put("rows", rows); lmcode_return_put("labels", seen);
let answer = n_lines(pair.keylog());
{
  "answer": 10,
  "rows": [
    "1|client|0",
    "2|server|4",
    "3|client|9",
    "4|server|10"
  ],
  "labels": "|SERVER_HANDSHAKE_TRAFFIC_SECRET|CLIENT_HANDSHAKE_TRAFFIC_SECRET|EXPORTER_SECRET|SERVER_TRAFFIC_SECRET_0|CLIENT_TRAFFIC_SECRET_0|"
}

The picture shows the lines after each flight. The labels that the run collected are SERVER_HANDSHAKE_TRAFFIC_SECRET, CLIENT_HANDSHAKE_TRAFFIC_SECRET, EXPORTER_SECRET, SERVER_TRAFFIC_SECRET_0 and CLIENT_TRAFFIC_SECRET_0: names of secrets, not secrets. The three fields of a line are the label, the client's random and the secret; the page reads only the first and returns nothing else.

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 has 3 rows and fewer labels have been collected than in the original run. A name of one character, such as x, stops line 10 with "Line 10: 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 how many lines the key log has and how many different labels they carry, as a list of two numbers. It sets the client's highest version to client_max, which takes TLSv1.3 or TLSv1.2; a version name that OpenSSL does not know, such as TLSv1.4, stops line 4 with "Line 4: TlsCtx.max_version: unknown TLS version 'TLSv1.4' (use TLSv1.2 or TLSv1.3)"; TLSv1.1 is known and gives no key log line, so the answer is [0, 0].

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

The name example.test, the client's highest version TLSv1.3 and the pair of numbers 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("X25519");
let client = ssl.client_ctx(); client.add_ca(ssl.parse_cert(root_pem));
client.groups("X25519"); client.max_version(client_max);
let pair = ssl.tls_pair(client, server, host);
let turns = 0;
while (!pair.done() && pair.error() == "" && turns < 10) {
  let flight = pair.step(); turns = turns + 1;
}
let lines = str_split(pair.keylog(), "\n");
let count = 0; let labels = 0; let seen = "|";
for (i in range(0, lines.len())) {
  let label = str_split(lines.at(i), " ").at(0);
  let fresh = str_len(label) > 0 && !str_contains(seen, "|" + label + "|");
  if (fresh) { labels = labels + 1; seen = seen + label + "|"; }
}
count = lines.len();
let answer = int_list(); answer.add(count); answer.add(labels);
lmcode_return_put("turns", turns);
{
  "answer": [
    11,
    5
  ],
  "turns": 4
}

The program builds a TLS client and a TLS server from the test chain, with the client's highest version set to client_max (TLSv1.3 or TLSv1.2), joins them in memory and steps them to the end. keylog() then holds one line per logged secret, in the form LABEL client_random secret. Return, as a list of two whole numbers, the number of lines and the number of different labels. Return counts only. Check runs your program with other names and other test material.

Read the result carefully

A key log is what lets a tool decrypt a capture of a handshake. The secrets here belong to one handshake between two test endpoints in this tab's memory and protect nothing, and the page still prints none: they differ on every run, and a record of them would be stale at once. The log holds no private key of either end. The recording of A TLS 1.3 Handshake, Message by Message starts its key lesson from logged secrets.

The certificates are test material. 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 early with a certificate error and the log is shorter.

Recap

  • The key log has one line for each secret, LABEL client_random secret.
  • Lines appear as the handshake goes, not all at once.
  • Each end logs its own copy, so a label can occur twice.
  • Count lines and labels; never show a value.