Verify Walks the Chain
The verify command builds a path from the leaf through the untrusted certificates to a trusted root, matching each issuer by name and key identifier before it checks the signature; a model of the path building shows why the name alone is not enough.
The question
A verifier is given a leaf, a heap of intermediates it does not trust and a list of roots it does. It has to find the path from the leaf to a root. How does it decide which certificate comes next, and what does it do when the right one is missing?
The certificates of this book are test material, 512-bit RSA keys, a toy size far too small to use.
Work it by hand
Each certificate names its issuer by name and, in the authority key identifier extension, by the identifier of the issuer's key. The next link is the certificate whose subject name is the issuer name and whose subject key identifier is that identifier. Only then does the verifier check the signature with the key of the link.
The example chain is the leaf, the intermediate and the root. The leaf's issuer is the intermediate; the intermediate's authority key identifier is the identifier of the root's key. The verifier takes untrusted certificates from the files named with -untrusted and stops when it reaches a certificate that is in the trusted file.
A different root with the same name does not fit: its key identifier is another one. This was measured on the look-alike root of the committed example files; the result is below.
Run it
The demo is one command (lines 3 and 4): verify -attime 1790000000 -CAfile in/root.txt -untrusted in/int.txt -show_chain in/leaf.txt. The time is fixed so that the run is repeatable. It prints in/leaf.txt: OK and then the chain, depth 0 for the leaf, depth 1 for the intermediate and depth 2 for the root; the first two are marked (untrusted) (the picture cuts the second line before its mark).

Public test material, generated for this lesson. Never use it for anything real.
# Verifies the leaf and prints the chain that it found.
# Check runs it with other certificates in the three files in/.
openssl verify -attime 1790000000 -CAfile in/root.txt -untrusted in/int.txt \
-show_chain in/leaf.txt
{
"answer": "in/leaf.txt: OK\nChain:\ndepth=0: CN=www.example.test (untrusted)\ndepth=1: O=Example Test Lab, CN=Example Test Intermediate CA (untrusted)\ndepth=2: O=Example Test Lab, CN=Example Test Root CA"
}You can change the files. Measured: without -untrusted in/int.txt the command stops with error 20 at 0 depth lookup: unable to get local issuer certificate; with the intermediate as the trusted file and -partial_chain it prints OK and a chain of two lines.
Your turn
The program below is a model of the path building, not the command: it checks names and key identifiers and not signatures. certs has one line for each untrusted certificate, name|skid|issuer|akid, the leaf first; root is name|skid of the trusted root. Return the names from the leaf up to the root, or an empty list if there is no path. Judge it, and find the candidates that are right for every input. Check runs each candidate on the data shown and on eleven others.

Public test material, generated for this lesson. Never use it for anything real.
let rows = str_split(certs, "\n");
let top = str_split(root, "|");
let answer = str_list();
let at = 0;
let state = 0;
for (step in range(0, 6)) {
let f = str_split(rows.at(at), "|");
if (state == 0) { answer.add(f.at(0)); }
let up = -1;
for (r in range(0, rows.len())) {
let g = str_split(rows.at(r), "|");
if (g.at(0) == f.at(2) && g.at(1) == f.at(3)) { up = r; }
}
let hit = 0;
if (f.at(2) == top.at(0)) { hit = 1; }
if (hit == 1 && state == 0) { answer.add(top.at(0)); }
if (hit == 1) { state = 1; }
if (hit == 0 && up < 0) { state = 2; }
if (hit == 0 && up >= 0) { at = up; }
}
if (state != 1) { answer = str_list(); }
{
"answer": [
"www",
"ICA",
"Root"
]
}certs has one line for each untrusted certificate, name|skid|issuer|akid, with the leaf on the first line, in any order after it. root is name|skid of the one trusted root. Return the list of names from the leaf up to and including the root: each step goes to the line whose name is the issuer and whose skid is the akid; the walk ends when the issuer and akid are those of the root. Return an empty list if there is no such path. Treat the model as names and key identifiers only.
Read the result carefully
Measured with the look-alike root as the trusted file and the real intermediate: the command does not complain about a signature but prints error 20 at 1 depth lookup: unable to get local issuer certificate, the same message as for a missing intermediate. The model gives an empty list for both, as it should.
The model stops after six steps, treats the first line as the leaf and does not look at dates, purposes or signatures; the command looks at dates and signatures, and at the purpose when one is given. Real verifiers differ in how they search, and this page describes one build. Why a root is trusted at all is the subject of A Certificate Chain Trusts a Root.
Where a leaf and an intermediate travel together on the wire is shown in A TLS 1.3 Handshake, Message by Message: the Certificate message of its recorded handshake carries such a pair, made from other test keys.
For a live client and server, start with A TLS 1.3 Handshake You Can Step Through; then try a client that trusts the wrong root and read the failure at each end.
Recap
- A link is found by the issuer name and the key identifier, not by the name alone.
- The verifier walks up through untrusted certificates and stops at a trusted root.
- A missing intermediate and a look-alike root give the same error 20, at different depths.
- The model checks names and key identifiers; the signature check comes after the link is found.