A Certificate Has Three Parts
A certificate is a SEQUENCE of three parts, the part that is signed, the name of the signature algorithm and the signature, and the signature is a BIT STRING whose first content byte counts unused bits.
The question
The last lesson walked the top of the leaf certificate and found three children. What are they, and which one is the part the issuer signs? Which bytes of the third one are the signature, and how long is a signature of a toy RSA key?
RFC 5280, section 4.1.1, defines a certificate as a SEQUENCE of three fields: the tbsCertificate, the signatureAlgorithm and the signatureValue. "tbs" stands for "to be signed". The subsections of that section have these names as their titles.
Work it by hand
The leaf test certificate, test material with a 512-bit RSA key, a toy size far too small to use, has these children (lesson 2): a SEQUENCE at offset 4 with a header of 4 bytes and 410 bytes of value, a SEQUENCE of 13 bytes at 418 and a BIT STRING of 65 bytes at 433.
The first one is the tbsCertificate: 4 + 410 = 414 bytes with its header, and it is what the signature covers. The second holds the algorithm: the object identifier 06 09 2a 86 48 86 f7 0d 01 01 0b (OpenSSL's asn1parse prints it as sha256WithRSAEncryption) followed by 05 00, a NULL.
The third is the signatureValue. A BIT STRING value starts with one byte that says how many bits of the last byte are unused: here 00. The other 64 bytes are the signature, 512 bits, the size of the key.
Run it
The demo is one command (line 3): asn1parse -in in/cert.txt -strparse 4 -noout -out out.der parses the certificate again starting at offset 4, the first part, and writes its bytes to out.der without printing the structure. The picture shows the first 16 bytes and the length: 414 bytes for the leaf.
You can change the offset or the certificate. Measured on the leaf: offset 418 writes 15 bytes (the algorithm part), offset 433 writes 64 bytes (the signature without the unused-bits byte), offset 5, inside an element, writes the 3 bytes 82 01 9a, and offset 0 stops with asn1parse: Non-positive number "0" for option -strparse. Measured: offset 1000 prints '1000' is out of range and still ends with status 0, and out.der then holds all 500 bytes of the certificate.

Public test material, generated for this lesson. Never use it for anything real.
# Writes the first part of the certificate, the one that is signed, to out.der.
# Check runs it with other certificates in in/cert.txt.
openssl asn1parse -in in/cert.txt -strparse 4 -noout -out out.der
{
"answer": "3082019aa003020102020103300d06092a864886f70d01010b0500304231193017060355040a0c104578616d706c652054657374204c61623125302306035504030c1c4578616d706c65205465737420496e7465726d656469617465204341301e170d3236303130313030303030305a170d3336303130313030303030305a301b3119301706035504030c107777772e6578616d706c652e74657374305c300d06092a864886f70d0101010500034b003048024100ba5ca7906dfc2cb549cf8e7d35d727cf313a057ad7186b088dbb2fdc17ff855208eaeff4b6ee2ff00689be340c4a6b18371d5d1c41c690f5c153f0cdcfaccf030203010001a381a130819e300c0603551d130101ff04023000300e0603551d0f0101ff04040302078030130603551d25040c300a06082b0601050507030130290603551d110422302082107777772e6578616d706c652e74657374820c6578616d706c652e74657374301d0603551d0e04160414bc10b24aecaed03a1cbb88b9a8e5f7939d1e4e25301f0603551d2304183016801462d8f166c49655c2d84aae71641bcf0f9e8b2894",
"file": "out.der",
"bytes": 414
}Your turn
The program below takes the DER of a certificate as hex and should return a list of three texts: the length of the first part including its header, the bytes of the algorithm's object identifier as hex, and the number of bytes of the signature. Judge it, and find the candidates that are right for every input. Check runs each candidate on the data shown and on seven others. Two cases are constructed skeletons of three parts and not certificates.

Public test material, generated for this lesson. Never use it for anything real.
fn head(der: string, at: int) -> int {
let b = hex_u8(der, at + 1);
return 2 + bits_of(b, 0, 7) * (b / 128);
}
fn body(der: string, at: int) -> int {
let n = hex_u8(der, at + 1);
if (n == 129) { n = hex_u8(der, at + 2); }
if (n == 130) { n = hex_u16(der, at + 2); }
return n;
}
let tbs = head(der, 0);
let size = head(der, tbs) + body(der, tbs);
let alg = tbs + size;
let oid = alg + head(der, alg);
let sig = alg + head(der, alg) + body(der, alg);
let answer = str_list();
answer.add(str(size));
answer.add(hex_slice(der, oid, body(der, alg)));
answer.add(str(body(der, sig) - 1));
{
"answer": [
"271",
"06092a864886f70d01010b0500",
"64"
]
}der is the hex of a DER certificate: a SEQUENCE of three elements. Return a list of three texts: the length in bytes of the first element including its header, in decimal; the bytes of the object identifier inside the second element (the algorithm), as lowercase hex without its tag and length; the number of signature bytes in the third element, in decimal, not counting the byte that counts unused bits.
Read the result carefully
The program finds the three parts by their lengths and does not check what is inside them; it does not verify the signature, which is the subject of the last lesson of this book. How a chain of such certificates is checked is the subject of A Certificate Chain Trusts a Root. The two helper functions read a length in the short form or with one or two length bytes, as the lessons before.
The numbers 414, 13 and 64 belong to the leaf; the other certificates have other lengths for their first part, a header of 4 bytes and the same signature size of 64 bytes, a toy size. The constructed cases have a short header on their top element and an algorithm of another kind, the OID that asn1parse names ecdsa-with-SHA256, measured on those bytes; they are not certificates and no key made their signature bytes.
Recap
- A certificate is a SEQUENCE of the tbsCertificate, the signatureAlgorithm and the signatureValue (RFC 5280, section 4.1.1).
- The first part, with its header, is the part that is signed: 414 bytes for the leaf.
- The algorithm part holds an object identifier; for these certificates it is sha256WithRSAEncryption.
- The signature is a BIT STRING: one byte of unused-bit count, then the signature bytes, 64 for a 512-bit key.