Show contents
Chapter 5 of 6 · 25 min
Project: your own certificate authority
By the end of this chapter, you'll be able to
- Issue certificates signed by a root authority and an intermediate authority
- Verify a certificate chain up to a trusted root
- Check validity dates, the name and the revocation list
- Refuse that an ordinary certificate be used to sign other certificates
The pieces of the PKI
The verifier checks each link, from the leaf up to the root.
Text description of the animation
- The root rarely signs and its private key is kept offline, in a vault.
- The root delegates: it signs the school's certificate, allowing it to sign in turn.
- The school signs the students' certificates. A leaf cannot sign anything.
- If a key is stolen or a certificate is fraudulent, its number is published in the list.
- The verifier checks each link, from the leaf up to the root.
We reuse the RSA signature from the previous chapter. Each authority has its key pair; for the example, we build the keys from well-known Mersenne primes (never for real!).
import hashlib
import json
E = 65537
def h(data):
return int.from_bytes(hashlib.sha256(data).digest(), "big")
def key_pair(p, q):
return {"n": p * q, "d": pow(E, -1, (p - 1) * (q - 1))}
def canonical(content):
return json.dumps(content, sort_keys=True).encode()
def issue(serial, subject, public_key, issuer, issuer_key, start, end, ca):
content = {"serial": serial, "subject": subject, "key": public_key, "issuer": issuer,
"start": start, "end": end, "ca": ca}
return {"content": content, "signature": pow(h(canonical(content)), issuer_key["d"], issuer_key["n"])}
def signed_by(cert, issuer_n):
return pow(cert["signature"], E, issuer_n) == h(canonical(cert["content"]))
k_ministry = key_pair(2**521 - 1, 2**607 - 1)
k_school = key_pair(2**127 - 1, 2**521 - 1)
k_student = key_pair(2**89 - 1, 2**607 - 1)
c_school = issue(1001, "Bamako-East School CA", k_school["n"], "Ministry CA", k_ministry, "2026-01-01", "2029-12-31", True)
c_student = issue(1002, "Aminata Coulibaly", k_student["n"], "Bamako-East School CA", k_school, "2026-07-01", "2031-06-30", False)
print("school signed by the ministry :", signed_by(c_school, k_ministry["n"]))
print("student signed by the school :", signed_by(c_student, k_school["n"]))
print("student signed by the ministry:", signed_by(c_student, k_ministry["n"]))
school signed by the ministry : True
student signed by the school : True
student signed by the ministry: False
The verifier only trusts the ministry's public key, stored in its trust store. To accept the student's certificate, it must go up the chain: the student is signed by the school, whose certificate is signed by the ministry.
Dates are written in ISO format YYYY-MM-DD: comparing two strings in this format gives the right chronological order, which simplifies the checks.
Mini-lab: the canonical form mattersPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Project: the certificate verifier
Write a function verify(chain, expected_subject, date, trusted, revoked) where chain is the list [leaf, intermediate], trusted a dictionary {root name: n} and revoked a set of serial numbers. It returns "valid" or the first reason for refusal among: "invalid signature", "unknown issuer", "not allowed to sign", "outside validity period", "revoked", "wrong subject". Test six cases: a valid certificate, an expired one, a revoked one, one for another person, a chain whose root is unknown, and the fake from the box above.
Hint 1
content["issuer"] in trusted.Hint 2
ca == True if it is not the leaf, dates (start <= date <= end), revocation. At the end, compare the leaf's subject.Show the solution and common mistakes
import hashlib
import json
E = 65537
h = lambda d: int.from_bytes(hashlib.sha256(d).digest(), "big")
canonical = lambda c: json.dumps(c, sort_keys=True).encode()
def key_pair(p, q): return {"n": p * q, "d": pow(E, -1, (p - 1) * (q - 1))}
def issue(serial, subject, key, issuer, k, start, end, ca):
c = {"serial": serial, "subject": subject, "key": key, "issuer": issuer, "start": start, "end": end, "ca": ca}
return {"content": c, "signature": pow(h(canonical(c)), k["d"], k["n"])}
def verify(chain, expected_subject, date, trusted, revoked):
for i, cert in enumerate(chain):
c = cert["content"]
if i + 1 < len(chain):
issuer_n = chain[i + 1]["content"]["key"]
elif c["issuer"] in trusted:
issuer_n = trusted[c["issuer"]]
else:
return "unknown issuer"
if pow(cert["signature"], E, issuer_n) != h(canonical(c)):
return "invalid signature"
if i > 0 and not c["ca"]:
return "not allowed to sign"
if not c["start"] <= date <= c["end"]:
return "outside validity period"
if c["serial"] in revoked:
return "revoked"
return "valid" if chain[0]["content"]["subject"] == expected_subject else "wrong subject"
k_min, k_sch = key_pair(2**521 - 1, 2**607 - 1), key_pair(2**127 - 1, 2**521 - 1)
k_a, k_b, k_att = key_pair(2**89 - 1, 2**607 - 1), key_pair(2**107 - 1, 2**607 - 1), key_pair(2**107 - 1, 2**1279 - 1)
trusted = {"Ministry CA": k_min["n"]}
school = issue(1001, "School CA", k_sch["n"], "Ministry CA", k_min, "2026-01-01", "2029-12-31", True)
aminata = issue(1002, "Aminata Coulibaly", k_a["n"], "School CA", k_sch, "2026-07-01", "2031-06-30", False)
former = issue(1003, "Ibrahim Sow", k_b["n"], "School CA", k_sch, "2026-01-01", "2026-06-30", False)
fraud = issue(1007, "Fatou Ndiaye", k_b["n"], "School CA", k_sch, "2026-07-01", "2031-06-30", False)
fake_school = issue(1001, "School CA", k_att["n"], "Attacker CA", k_att, "2026-01-01", "2029-12-31", True)
fake_aminata = issue(1002, "Aminata Coulibaly", k_b["n"], "School CA", k_att, "2026-07-01", "2031-06-30", False)
fake_student = issue(1666, "Fake graduate", k_b["n"], "Aminata Coulibaly", k_a, "2026-07-01", "2031-06-30", False)
cases = [
("valid certificate", [aminata, school], "Aminata Coulibaly"),
("expired certificate", [former, school], "Ibrahim Sow"),
("revoked certificate", [fraud, school], "Fatou Ndiaye"),
("another person", [aminata, school], "Moussa Traoré"),
("unknown root", [fake_aminata, fake_school], "Aminata Coulibaly"),
("signed by a leaf", [fake_student, aminata, school], "Fake graduate"),
]
for name, chain, subject in cases:
print(f"{name:<20} → {verify(chain, subject, '2026-11-17', trusted, revoked={1007})}")
valid certificate → valid
expired certificate → outside validity period
revoked certificate → revoked
another person → wrong subject
unknown root → unknown issuer
signed by a leaf → not allowed to sign
Each refusal has its precise reason. For the unknown root, the attacker built a whole chain: the fake certificate is properly signed by the "fake school", but the latter is signed by an authority missing from the trust store. For the fake graduate, Aminata's certificate, which is not an authority, was used to sign: refused.
Common mistakes:
- Taking the issuer's key from the certificate itself instead of the next link: each certificate then validates itself.
- Comparing dates in the
DD/MM/YYYYform: alphabetical order no longer matches chronological order. - Checking revocation only for the leaf: a compromised intermediate authority must be revocable too.
Summary
- 1A PKI chains a trusted root, intermediate authorities and leaf certificates.
- 2The verifier checks each link: signature, right to sign, dates and revocation, then the subject.
- 3A canonical form of the certificate is signed, so that issuer and verifier hash the same bytes.
- 4Forgetting the right-to-sign check made it possible to forge certificates in real browsers.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.