Key Usage and the CA Flag
Basic Constraints says whether a certificate's key may sign other certificates; Key Usage is a BIT STRING that lists what the key may do, with the first bit stored in the most significant bit of the first byte.
The question
Two extensions say what a certificate's key may do. The CA flag says whether the key may sign other certificates. Key Usage says which operations the key is meant for. How are they written in the bytes, and how does a program read a list of usages out of a bit string?
The certificates of this book are test material with 512-bit RSA keys, a toy size far too small to use. The lesson reads two of their extensions.
Work it by hand
RFC 5280, section 4.2.1 (the subsections Basic Constraints and Key Usage), says: "If the cA boolean is not asserted, then the keyCertSign bit in the key usage extension MUST NOT be asserted." A certificate that is not a CA does not claim the right to sign certificates.
The leaf test certificate has the extension Basic Constraints with the value 30 00: an empty SEQUENCE, so the cA boolean is absent and means false, and the extension is marked critical. Its Key Usage value is a BIT STRING, 03 02 07 80. The tag 03 is BIT STRING, the length is 2, the first content byte 07 says that 7 bits at the end of the last byte are unused, and the last byte is 80.
80 is 1000 0000. The bits are numbered from the most significant end: bit 0 is the first bit of the first byte. Bit 0 is digitalSignature in the list of RFC 5280, section 4.2.1, subsection Key Usage, so the leaf's key is for digital signatures only; bit 5 is keyCertSign and bit 6 is cRLSign.
Run it
The demo is one command (line 3): x509 -in in/cert.txt -noout -ext basicConstraints,keyUsage,extendedKeyUsage prints the three extensions of the leaf. It prints X509v3 Basic Constraints: critical and CA:FALSE, then X509v3 Key Usage: critical and Digital Signature, then the extended key usage TLS Web Server Authentication.
You can name other extensions or use another certificate. Measured: an extension that the certificate does not have is not printed, and the command still ends with status 0.

Public test material, generated for this lesson. Never use it for anything real.
# Prints the CA flag, the key usage and the extended key usage.
# Check runs it with other certificates in in/cert.txt.
x509 -in in/cert.txt -noout -ext basicConstraints,keyUsage,extendedKeyUsage
{
"answer": "X509v3 Basic Constraints: critical\n CA:FALSE\nX509v3 Key Usage: critical\n Digital Signature\nX509v3 Extended Key Usage: \n TLS Web Server Authentication"
}Your turn
The program below should read a Key Usage BIT STRING given as hex in ku, tag and length included, and return the names of the asserted bits in the order of their bit numbers: digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyAgreement, keyCertSign, cRLSign, encipherOnly, decipherOnly. 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 list = "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,";
list = list + "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly";
let names = str_split(list, ",");
let count = hex_u8(ku, 1) - 1;
let answer = str_list();
for (b in range(0, 1)) {
for (i in range(0, 8)) {
let bit = bits_of(hex_u8(ku, 3 + b), 7 - i, 1);
if (bit == 1 && b * 8 + i < 9) { answer.add(names.at(b * 8 + i)); }
}
}
{
"answer": [
"digitalSignature"
]
}ku is the hex of a DER BIT STRING for a Key Usage: the tag 03, the length, the byte that counts unused bits, and the data bytes (the length is below 128). Bit 0 is the most significant bit of the first data byte. Return the names of the asserted bits among bits 0 to 8, in the order of the bit numbers, as digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyAgreement, keyCertSign, cRLSign, encipherOnly, decipherOnly. Return an empty list if no bit is asserted.
Read the result carefully
The program reads a BIT STRING whose length byte is below 128, so up to 126 data bytes, and only bits 0 to 8 have names; further bits are ignored. It does not check that the unused bits are zero, which DER requires, and it does not read the extension wrapper (the object identifier, the critical flag and the OCTET STRING around the BIT STRING).
RFC 5280 names bit 1 nonRepudiation and notes that recent editions of X.509 renamed it contentCommitment; OpenSSL prints it as Non Repudiation. The page uses the RFC's ASN.1 name. It does not show extended key usage beyond the one line of the demo, or path length constraints; A Small Certificate Authority makes certificates with these extensions and measures what the verifier does with them.
Certificates of this kind are sent by a server in the Certificate message of a handshake; the book A TLS 1.3 Handshake, Message by Message reads one recorded handshake, made with other test certificates.
To see test certificates used by a live client and server, start with A TLS 1.3 Handshake You Can Step Through; for name, root and date failures, see TLS Failures You Can Trigger.
Recap
- Basic Constraints says whether the subject may sign certificates; a certificate that is not a CA must not assert keyCertSign (RFC 5280, section 4.2.1).
- Key Usage is a BIT STRING: after the length comes a byte that counts unused bits, then the bits.
- Bit 0 is the most significant bit of the first byte: 80 is digitalSignature.
- The names of bits 0 to 8 are digitalSignature to decipherOnly in the order of RFC 5280.