The Wrong Host Name
A TLS client asks for a name and checks that the certificate lists it. With a name that is not listed it refuses, and what the server hears is a 7-byte alert record, here number 42, bad_certificate.
The question
A TLS client is told which name it is talking to, and the server answers with a certificate. What does the client do if the certificate does not list that name, and what does the server hear about it?
In this lesson both ends are test endpoints in this tab's memory, made with ssl.tls_pair: a real OpenSSL client and a real OpenSSL server that hand each other their flights, with no network in between. The certificate chain and the key are public test material. You change the name the client asks for and read what each end says.
Work it by hand
The test leaf certificate lists two names, www.example.test and example.test (stated here from the notes of the test material, not read by the program). Ask for shop.example.test: it is not on the list.
The server cannot know that. It has one certificate and sends it to everyone, so its flight is the same as ever. The client reads the certificate, compares the name it asked for with the names it lists, finds no match and refuses.
Before it stops it sends one last record, an alert, which has seven bytes: 15 for the record type alert, 03 03, a length of 00 02, then 02 for the level fatal and 2a, which is 42, the description. RFC 9846 section 6.2 defines bad_certificate as "A certificate was corrupt, contained signatures that did not verify correctly, etc."; which alert a client sends for a name that does not match is the implementation's choice, and this build sends 42.
So there are three flights: the hello, the server's flight and the 7-byte alert.
Run it
Lines 1 to 5 build the server with the test certificate, the chain and the key, build the client with one trusted root, and make the pair with the name host. The loop of lines 7 to 11 lets the two ends take turns, counts the flights that carry bytes and keeps the last one as hex.
Lines 12 to 14 read the alert number from the last flight, but only if it is exactly 7 bytes long and starts with 21 (hex 15): the bytes of a longer flight are different on every run. Lines 15 to 18 return the alert bytes, the number of flights, verify_result() and error(). Line 19 returns the name the server saw and the alert number.

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 turns = 0; let flights = 0; let last = "";
while (!pair.done() && pair.error() == "" && turns < 10) {
let flight = pair.step(); turns = turns + 1;
if (str_len(flight) > 0) { flights = flights + 1; }
if (str_len(flight) > 0) { last = str_hex_encode(flight); }
}
let code = 0 - 1; let alert = "";
let plain = hex_len(last) == 7 && hex_u8(last, 0) == 21;
if (plain) { code = hex_u8(last, 6); alert = last; }
lmcode_return_put("alert", alert);
lmcode_return_put("flights", flights);
lmcode_return_put("verify_result", pair.verify_result());
lmcode_return_put("error", pair.error());
let answer = pair.server_name() + " " + str(code);
{
"answer": "shop.example.test 42",
"alert": "1503030002022a",
"flights": 3,
"verify_result": "hostname mismatch",
"error": "client handshake: error:0A000086:SSL routines::certificate verify failed (certificate verify: hostname mismatch)"
}With shop.example.test the answer is shop.example.test 42: the server saw the name in the first flight and the client's alert is 42, bytes 1503030002022a, in 3 flights. The picture cuts the alert into its five fields.
What this OpenSSL build says: verify_result() is hostname mismatch and error() is client handshake: error:0A000086:SSL routines::certificate verify failed (certificate verify: hostname mismatch).
You can change host. A name that the certificate lists ends in a finished handshake, an empty alert and -1, so the picture is empty; an empty name runs too and checks no name. Adding a letter to a good name still runs and is refused.
A name such as x, www.example.test. or *.example.test, or one with a space, stops line 5 with "Line 5: Ssl.tls_pair: set host to verify". A name of 260 characters stops it with "Line 5: Ssl.tls_pair: set SNI: error:0A00013F:SSL routines::tls ext invalid servername".
Your turn
The program below should return the number of flights that carry bytes and the number of the plaintext alert in the last flight, or -1 if the last flight is not a 7-byte alert, as the list [flights, alert]. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on six 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 turns = 0; let flights = 0; let last = "";
while (!pair.done() && pair.error() == "" && turns < 10) {
let flight = pair.step(); turns = turns + 1;
if (str_len(flight) > 0) { flights = flights + 1; }
if (str_len(flight) > 0) { last = str_hex_encode(flight); }
}
let code = 0 - 1;
if (hex_len(last) == 7 && hex_u8(last, 0) == 21) { code = hex_u8(last, 5); }
let answer = int_list(); answer.add(flights); answer.add(code);
lmcode_return_put("last_bytes", hex_len(last));
{
"answer": [
3,
2
],
"last_bytes": 7
}A client asks for the name host and trusts root_pem; the server holds leaf_pem, int_pem and leaf_key. Both ends are two test endpoints in this tab's memory. Run the handshake, taking turns up to ten times, and return the list [flights, alert]: flights is the number of steps that returned bytes; alert is the description number of the last flight when that flight is exactly 7 bytes long and starts with 21 (a plaintext alert), otherwise -1. Check runs each candidate on the data shown and on six others.
Read the result carefully
The server learns nothing about the reason: it hears a number, 42, and in this build the same number comes for other certificate problems too. The name the client asked for travelled in the first flight in the clear, which is why the answer can show it; the extension that carries it is in tls-13-handshake-trace, and the certificate's name fields are in x509-certificates-field-by-field.
The texts above are what this OpenSSL build says; a real server may answer differently or not at all. The certificate is valid from 2026-01-01 to 2036-01-01 and the client checks it against your clock, so the results hold only while the clock is between those years. The certificates and the key are test material.
Compare a live successful handshake and how a certificate chain trusts a root.
Recap
- The client compares the name it asked for with the names in the certificate and refuses when none matches.
- The refusal is a 7-byte alert record: 15 03 03 00 02, the level 02 and the description, here 42
bad_certificate(RFC 9846 section 6.2). - The server's flight does not depend on the name; the client alone decides.
- The alert number is read from a 7-byte flight only; longer flights are random beyond their headers.