Extensions Say What a Key May Do
Basic Constraints holds the CA flag, false when absent, and an optional path length that limits how many intermediate CAs may follow; a program reads both from a SEQUENCE in which the first field may be missing.
The question
A signature proves that a key made it, not that the key may sign certificates. The extensions say what a key may do. In the bytes, how does Basic Constraints write the CA flag and the path length, and what must a reader do when a field 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
The value of Basic Constraints is a SEQUENCE of two optional fields: a BOOLEAN, the CA flag, and an INTEGER, the path length. A CA flag that is false is left out, so the empty SEQUENCE 30 00 of an ordinary leaf means CA:FALSE and no limit.
The intermediate certificate of the example chain has 30 06 01 01 ff 02 01 00. After the header 30 06 come the BOOLEAN 01 01 ff (true) and the INTEGER 02 01 00 (zero). RFC 5280, section 4.2.1, subsection Basic Constraints, says the path length is "meaningful only if the cA boolean is asserted and the key usage extension, if present, asserts the keyCertSign bit", gives the maximum number of non-self-issued intermediate certificates that may follow, and says that where it does not appear, "no limit is imposed".
A reader cannot take the second field from a fixed place. Without the BOOLEAN the INTEGER comes first. The program reads one field at a time and looks at its tag.
Run it
The demo is one command (line 3): x509 -in in/cert.txt -noout -ext basicConstraints,keyUsage prints the two extensions of the intermediate: X509v3 Basic Constraints: critical with CA:TRUE, pathlen:0, and X509v3 Key Usage: critical with Certificate Sign, CRL Sign.
You can put another certificate in in/cert.txt. Measured: for the root the command prints CA:TRUE and no path length; for the leaf of the example chain it prints CA:FALSE.

Public test material, generated for this lesson. Never use it for anything real.
# Prints the CA flag, the path length and the key usage.
# Check runs it with other certificates in in/cert.txt.
x509 -in in/cert.txt -noout -ext basicConstraints,keyUsage
{
"answer": "X509v3 Basic Constraints: critical\n CA:TRUE, pathlen:0\nX509v3 Key Usage: critical\n Certificate Sign, CRL Sign"
}Your turn
The program below should read the value of a Basic Constraints extension given as hex in bc, header included, and return two numbers: 1 if the CA flag is true and 0 if not, then the path length, or -1 if there is none. Judge it, and find the candidates that are right for every input. Check runs each candidate on the data shown and on nine others.

Public test material, generated for this lesson. Never use it for anything real.
let n = hex_u8(bc, 1);
let end = 2 + n;
let at = 2;
let ca = 0;
let pathlen = 0;
if (at < end && hex_u8(bc, at) == 1) {
if (hex_u8(bc, at + 2) != 0) { ca = 1; }
at = at + 3;
}
if (at < end && hex_u8(bc, at) == 2) {
pathlen = hex_u8(bc, at + 2);
}
let answer = int_list();
answer.add(ca);
answer.add(pathlen);
{
"answer": [
1,
0
]
}bc is the hex of the value of a Basic Constraints extension: a SEQUENCE (tag 30, length below 128) that may hold a BOOLEAN (tag 01), the CA flag, and an INTEGER (tag 02) of one byte, the path length, in that order, either of them missing. Return a list of two numbers: 1 if the CA flag is present and true, otherwise 0, and the path length, or -1 if there is none.
Read the result carefully
The program reads lengths below 128 and a path length of one byte, and it does not check that the SEQUENCE holds nothing else. Three test inputs are BER-style forms that DER does not use, as a reader may meet them: 30 03 01 01 00 and 30 06 01 01 00 02 01 01 spell out a FALSE that DER leaves out, and 30 06 01 01 01 02 01 00 writes TRUE as 01 instead of ff.
The program only reports the bytes of 30 03 02 01 05; RFC 5280 says a CA must not issue a path length without the CA flag, so a certificate with these bytes breaks that rule. How a verifier uses the limit is measured in a later lesson; the same bytes are read field by field in A Certificate, Field by Field.
Recap
- Basic Constraints is a SEQUENCE with an optional BOOLEAN (the CA flag, false when absent) and an optional INTEGER (the path length).
- An empty SEQUENCE means not a CA and no limit.
- The path length counts the intermediate CAs that may follow; absent means no limit.
- A reader checks the tag of each field, because the first one may be missing.