Four Flights
Two test endpoints in this tab's memory take turns; each turn hands the other end one flight of bytes. Count the flights of a TLS 1.3 handshake and see which end sends each, and how a refused certificate shortens it.
The question
A TLS handshake is usually drawn as a few messages going back and forth. In this lesson your program builds a TLS client and a TLS server from the test chain, joins them in memory with ssl.tls_pair, and lets them take turns. Nothing leaves the tab: both ends are test endpoints that the program itself made, and the certificates and the key are test material.
Each call of step() lets one end take its turn and returns the bytes that it hands to the other end. How many such flights make a whole handshake, and who sends each?
Work it by hand
RFC 9846 section 2 draws the full handshake in its Figure 1. The client sends a ClientHello. The server answers with a ServerHello and then its encrypted messages, up to its Finished. The client sends its own Finished, and application data can follow in both directions.
Counted as turns, that is the client, then the server, then the client: three flights. The pair of this page also lets the server take a last turn: RFC 9846 section 4.7.1 says that a server may send a NewSessionTicket message after it has received the client's Finished, if the ClientHello offered psk_key_exchange_modes, and this page counts that turn as a flight (stated here, not run). So a handshake of this pair should take four turns that carry bytes, alternating client, server, client, server.
Run it
The program is the standard block of this book and a loop. Line 5 joins the two ends for the name in host. The loop of lines 7 to 16 takes a turn with step() in line 8 and ends when the pair is done, when an error is set, or after 10 turns. Line 9 counts a flight only if it carried bytes, and pair.from() names the sender.

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 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);
let answer = flights;
{
"answer": 4,
"messages": [
"0>1|flight 1|315 bytes",
"1>0|flight 2|2332 bytes",
"0>1|flight 3|80 bytes",
"1>0|flight 4|length varies"
]
}The picture draws one arrow per flight with its length, and line 18 returns the number of flights. The lengths are from this build (lmssl 0.2.2, OpenSSL 4.0.3): another client offers other groups and its hello has another length.
The length of the last flight is not shown: in some repeated runs of this program it was 16 bytes shorter, so the picture says length varies for the flight after which done() is true. The program asks for the group X25519 on both ends, which keeps the lengths small.
You can change host, the name that the client asks for and checks. A name the certificate does not cover, such as shop.example.test or www.example.testx, still runs: the client refuses the certificate and there are 3 flights, the last of 7 bytes. WWW.EXAMPLE.TEST gives the same 4 flights as the original, and an empty name sends no name and checks none: 4 flights. An IP address such as 192.0.2.7 sends no name and is refused: 3 flights.
Some names stop the program at line 5: a name of one character (x), a name with a final dot, with a space or with a hyphen at the start or end of a label stops it with "Line 5: Ssl.tls_pair: set host to verify", and a name of 260 characters with "Line 5: Ssl.tls_pair: set SNI: error:0A00013F:SSL routines::tls ext invalid servername". The four PEM texts are test material: leave them as they are.
Your turn
The program below is meant to return how many flights the client sent and how many the server sent, as a list of two numbers, for the name in host. A turn that returns no bytes is not a flight. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on four others.

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 by_client = 0; let by_server = 0; let turns = 0;
while (!pair.done() && pair.error() == "" && turns < 10) {
let sender = pair.from();
let flight = pair.step(); turns = turns + 1;
if (str_len(flight) > 0 && sender == "client") { by_client = by_client + 1; }
if (str_len(flight) > 0 && sender == "server") { by_server = by_server + 1; }
}
let answer = int_list(); answer.add(by_client); answer.add(by_server);
lmcode_return_put("turns", turns);
{
"answer": [
2,
1
],
"turns": 4
}The program builds a TLS client and a TLS server from the test chain, joins them in memory and lets them take turns with step(). A turn that returns no bytes is not a flight. Return, as a list of two whole numbers, the number of flights the client sent and the number of flights the server sent, for the name host that the client asks for and checks. Check runs your program with other names and other test material.
Read the result carefully
Both ends are programs in this tab, and the reader's page set the policy of both: the test root was handed to the client by the page. The handshake shown is not the recording of the book A TLS 1.3 Handshake, Message by Message: the randoms and key shares are new on every run, so the page returns only counts and lengths.
The last server flight is the session tickets of RFC 9846 section 4.7.1 (stated here, not run): the client has not read them when done() turns true. 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 after three flights with a certificate error.
See TLS assembled for the earlier overview, and failures you can trigger for runs that stop short.
Recap
- One call of
step()is one turn of one end, and a turn can carry bytes or none. - A flight is a run of records, not a message.
- This pair sends client, server, client, server when everything is accepted.
- A refused certificate shortens the handshake, and the last flight is then a short alert.