Speed

The Signature Covers the First Part

The issuer signs the DER bytes of the first part of the certificate, header included; change one byte of that part and the signature no longer verifies, and the certificate's fingerprint is a different hash.

The question

A certificate carries a signature. Which bytes does it cover, and what happens to it when one byte of the certificate changes? Three different hashes of the same certificate are easy to confuse, so it matters to know which one the issuer's signature is made over.

The certificates of this book are test material with 512-bit RSA keys, a toy size far too small to use. Their signatures show the rule, not a protection you could rely on.

Work it by hand

The first part of the certificate, the tbsCertificate, is at offset 4 of the leaf and takes 414 bytes with its 4-byte header (lesson 3). The signature value is the BIT STRING at offset 433: a header of 2 bytes, the byte 00 that counts unused bits, and 64 bytes of signature, which start at offset 436.

The issuer here is the intermediate test certificate. It signs the SHA-256 hash of the 414 bytes, header included, with its private key, and a verifier checks that signature with the issuer's public key. The signature is made over the first part only.

So there are three hashes. The SHA-256 of the whole certificate, 500 bytes, is its fingerprint; it is not what was signed. The SHA-256 of the first part with its header is what was signed. The SHA-256 of the first part without its header is a third value that nobody signed.

Run it

The demo is one command (line 3): dgst -sha256 -verify in/pub.txt -signature in/sig.bin in/tbs.bin takes the intermediate's public key as text, the 64 signature bytes and the 414 bytes of the first part of the leaf, and prints Verified OK. All three come from the leaf and the intermediate test certificates.

You can change the bytes. Measured: with the last byte of the first part changed, the command prints Verification failure on standard output and ends with exit status 1; a run that fails is shown as a failure by this page.

The first lines of the intermediate's public key in PEM, the first 16 of 64 signature bytes, the first 16 of 414 bytes of the first part, 30 82 01 9a a0 03 02 01 02 02 01 03 30 0d 06 09, and the output text Verified OK.

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

# Verifies the signature of the first part of the leaf certificate.
# Check runs it with other bytes in in/tbs.bin.
dgst -sha256 -verify in/pub.txt -signature in/sig.bin in/tbs.bin
{
  "answer": "Verified OK"
}

Your turn

The program below should return the SHA-256 hash, as lowercase hex, of the bytes that the issuer signed, from the DER of a certificate given as hex in der. Judge it, and find the candidates that are right for every input. Check runs each candidate on the data shown and on five others.

The first 16 bytes of the certificate dates2050, 30 82 01 5f 30 82 01 09 a0 03 02 01 02 02 01 07, of 355 bytes, and the 32-byte hash that the shown program returns.

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;
size = size + head(der, alg) + body(der, alg);
let answer = ssl.digest("SHA256", ssl.unhex(hex_slice(der, tbs, size)));
{
  "answer": "a696b13400c9f30040071906aa3f66a70a20c9ef3913e4d557276f8820c9427a"
}

der is the hex of a DER certificate. Return the SHA-256 hash, as 64 lowercase hex characters, of the bytes that the issuer signed: the first element inside the outer SEQUENCE, the tbsCertificate, including its tag and length bytes.

Read the result carefully

The command only checks that the signature is correct for the key and the bytes it is given: it does not check the dates, the names, whether the key belongs to a CA or whether the issuer is trusted. How a chain is checked is the subject of A Certificate Chain Trusts a Root, and A Small Certificate Authority makes a chain and checks it.

The exercise finds the first part by its length and returns a hash; it does not verify a signature. The page does not show how the signature is made or what padding it uses, and a 512-bit RSA key is a toy size far too small to use.

The book as a whole does not show revocation, name constraints, certificate transparency, the policy of a certificate authority, forms of ASN.1 other than DER, extensions other than the four it reads or key algorithms other than RSA.

Recap

  • The issuer signs the DER of the tbsCertificate, with its header: 414 bytes for the leaf.
  • Changing one byte of that part makes the signature fail to verify.
  • The fingerprint of a certificate is the SHA-256 of the whole certificate, a different hash.
  • A verifier needs the issuer's public key; the toy key here is far too small to use.