Speed

A Missing Link Breaks a Chain

A chain fails when an issuer on it may not sign: no CA flag, a key whose usage lacks signing, or a path length exceeded. Each has its own error; with -purpose, a leaf whose extended key usage does not name the purpose fails too.

The question

A chain whose signatures all fit can still fail. The verifier also asks of each issuer whether it was allowed to issue. What does it check, which error does each failure give, and at which depth is the blame put?

The certificates of this book are test material, 512-bit RSA keys, a toy size far too small to use.

Work it by hand

Three things are checked on an issuer. Its basic constraints must say it is a CA. Its key usage, if present, must include signing certificates. Its path length, if present, must not be smaller than the number of non-self-issued CA certificates that follow it before the leaf (the model has no self-issued ones). The depth counts from the leaf, which is depth 0.

For the example chain, root, then the intermediate with path length 0, then the leaf: no CA follows the intermediate, so the limit holds. Put a second CA under the intermediate and one CA follows it; the limit 0 is exceeded and the verifier blames the intermediate, which sits at depth 2 in a chain of four certificates.

The extended key usage of a leaf says what it may be used for. The example leaf names TLS server authentication, so, when the command is given a purpose, it passes as a server certificate and not as a client certificate.

Run it

The demo is one command (lines 3 and 4): verify -attime 1790000000 -purpose sslserver -CAfile in/root.txt -untrusted in/int.txt in/leaf.txt prints in/leaf.txt: OK.

One line printed by the command: in/leaf.txt: OK.

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

# Verifies the leaf for use as a TLS server certificate.
# Check runs it with other chains in the three files in/.
openssl verify -attime 1790000000 -purpose sslserver -CAfile in/root.txt \
  -untrusted in/int.txt in/leaf.txt
{
  "answer": "in/leaf.txt: OK"
}

You can change the purpose. Measured: -purpose sslclient for this leaf stops with error 26 at 0 depth lookup: unsuitable certificate purpose.

Your turn

The program below is a model of these checks. chain has one line for each CA certificate, from the root down, as ca|pathlen|keycertsign: yes or no for the CA flag, a number or - for the path length, and yes or no for whether the key usage includes signing certificates. The leaf follows the last line. The first line is the trusted root, whose CA flag and key usage the model does not check. Return [error number, depth] of the problem found from the leaf upward, or [0, -1].

Judge it, and find the candidates that are right for every input. Check runs each candidate on the data shown and on twelve others.

The two lines of the chain, yes|-|yes for the root and yes|0|yes for the intermediate, and the pair of numbers that the shown program returns.

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

let rows = str_split(chain, "\n");
let k = rows.len();
let err = 0;
let depth = -1;
for (i in range(0, k)) {
  let j = k - 1 - i;
  let f = str_split(rows.at(j), "|");
  let below = k - j;
  let bad = 0;
  if (j > 0 && f.at(0) == "no") { bad = 1; }
  if (j > 0 && f.at(2) == "no") { bad = 1; }
  if (err == 0 && bad == 1) { err = 79; depth = k - j; }
  if (err == 0 && f.at(1) != "-" && below > str_to_int(f.at(1))) {
    err = 25; depth = k - j;
  }
}
let answer = int_list();
answer.add(err);
answer.add(depth);
{
  "answer": [
    25,
    1
  ]
}

chain has one line for each CA certificate, from the root down, as ca|pathlen|keycertsign; the leaf follows the last line. ca is yes or no, pathlen is a number or -, keycertsign is yes or no. Depth counts from the leaf, which is depth 0. Return [79, depth] for the first CA certificate from the leaf up, that is not the first line, with ca no or keycertsign no; [25, depth] for the first certificate, root included, whose pathlen is smaller than the number of CA lines after it; otherwise [0, -1]. The first line needs no CA flag or key usage. Check runs your program on chains with one problem each.

Read the result carefully

The model's cases were built as real certificates from the committed keys and run with the verify command. Measured: a CA flag of false gives error 79 at 1 depth lookup: invalid CA certificate; a CA without the signing bit gives that line and then error 32 at 1 depth lookup: key usage does not include certificate signing; a path length exceeded below an intermediate gives error 25 at 2 depth lookup: path length constraint exceeded; the same limit on the root gives error 25 at 3.

The model returns the first line only, and its cases hold one problem each; what the command reports first when two problems occur was not measured. The path length is a one-byte limit here, and a chain has at most four CA lines. Whether a verifier checks the root's own flags is not modelled; measured, the verify command does: a root made with CA:FALSE gives error 79 at depth 2 and a root without the signing bit gives error 79 and then error 32 at depth 2.

Recap

  • A verifier asks of each issuer: is it a CA, may its key sign certificates, and is its path length respected.
  • A failed CA flag gives error 79; a missing signing bit gives 79 and then 32; a path length exceeded gives 25.
  • The blame goes to the issuer that breaks a rule, at its depth counted from the leaf.
  • When a purpose is given with -purpose, the leaf's extended key usage is checked against it; the wrong purpose gives error 26 at depth 0.