Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 40 ยท Protocols and Practice

Certificates and Public-Key Infrastructure

Learn how X.509 certificates bind keys to names, how chains of trust reach a root CA, and what revocation and pinning do. Includes a toy CA in Python.

Public-key cryptography has a gap. A signature proves that the holder of some private key wrote a message, and Diffie-Hellman lets two parties agree on a secret, but neither tells you whose key you are looking at. If you connect to shop.example and receive a public key, an attacker in the middle can hand you their own key instead and decrypt everything.

A certificate closes the gap. It is a small signed document that says "this public key belongs to this name". The signer is a certificate authority (CA) that your device already trusts. The whole system of CAs, certificates, trust decisions and revocation is the public-key infrastructure (PKI). It is what puts the padlock in your browser.

This lesson explains what is inside a certificate, how a chain of signatures leads back to a root you trust, what can go wrong, and how revocation and pinning try to limit the damage. The two examples build a toy CA so you can watch each check happen.

What a certificate contains

The standard format is X.509. A certificate has a few fields that matter most:

Field Meaning
Subject Who the certificate is for (a domain name, an organization)
Subject public key The key being vouched for
Issuer The CA that signed it
Validity A not before and not after date
Serial number Unique per issuer, used for revocation
Extensions Subject alternative names (the domains it covers), whether it may sign other certificates (CA:TRUE), key usage, links to revocation services
Signature The issuer's signature over all of the above

Modern browsers match the host name against the subject alternative names extension, not the old "common name" field. A certificate for example.com does not cover www.example.com unless that name is listed, although one certificate can list many names.

Everything is signed with the issuer's private key, which means that changing any field, for example swapping in a different public key, invalidates the signature.

Chains of trust

Your device does not hold a certificate for every website on the internet. It holds a few hundred root certificates, the trust store shipped with the operating system or browser. Roots are self-signed: the subject and issuer are the same, and nothing above them vouches for them. You trust them because the vendor decided to include them, after audits and policy requirements.

Roots rarely sign website certificates directly, because their private keys are kept offline in hardware. Instead a root signs an intermediate CA, and the intermediate signs the leaf (end-entity) certificates for sites. A server sends its leaf plus the intermediates; the client builds the path up to a root it already has.

flowchart TD
    R["Root CA<br/>(self-signed, in trust store)"] -->|"signs"| I["Intermediate CA<br/>(CA:TRUE)"]
    I -->|"signs"| L["Leaf certificate<br/>shop.example"]
    L --> S["Server key<br/>proves possession in handshake"]

A chain: the client trusts the root, the root vouches for the intermediate, and the intermediate vouches for the site.

To accept a chain, a client checks, for every certificate in it:

  1. The signature verifies under the public key of the next certificate up.
  2. The current date is inside the validity period.
  3. Every certificate above the leaf is allowed to sign certificates (CA:TRUE, and a key usage that permits it).
  4. The chain ends at a root in the trust store, with the very same key.
  5. The leaf covers the host name being visited.

Finally, during the TLS handshake the server must prove it holds the private key matching the leaf's public key; otherwise anyone could replay a copied certificate.

A toy CA in Python

The example below uses a small RSA signature scheme (128-bit primes, far too small for real use) and a simplified certificate: a dictionary signed over its serialized form. Real certificates use ASN.1 encoding, but the logic of chain checking is the same. Read the verify_chain function carefully; it follows the five checks above.

Python Build a three-level chain and verify it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Every failure is rejected for a different reason, and each rejection corresponds to a real incident type: expired certificates are the most common cause of outages, a missing CA flag is the check whose absence caused serious bugs in early TLS libraries, and signature checks are what make tampering visible.

Who decides what to trust?

The trust store is the weak point of the model: any root in it can issue a valid certificate for any domain. If one CA is careless or compromised, an attacker can obtain a certificate for a site they do not own. This has happened (DigiNotar, 2011, was removed from all trust stores after fraudulent certificates for Google domains appeared), and the ecosystem has built several defenses:

  • Strict audit and policy rules for CAs, enforced by the browser and OS vendors. A CA that breaks them is removed from the store.
  • Domain validation. Public CAs only issue a certificate after the requester proves control of the domain, usually by serving a token over HTTP or placing a DNS record. Protocols such as ACME automate this, which is how free certificates became routine.
  • Certificate Transparency (CT). Issued certificates are logged in public, append-only logs built on Merkle trees. Browsers require proof of logging, so a misissued certificate for your domain becomes visible, and you can watch the logs for your names.
  • CAA records. A DNS record in which a domain owner lists which CAs may issue for it.
  • Shorter lifetimes. The maximum lifetime for public certificates has been shrinking in agreed steps, with a published plan for only a few weeks by the end of the decade. Short lifetimes mean automation is mandatory, but they also limit how long a stolen key is useful.

Organizations often run a private CA for internal services. Its root is installed only on company devices, so it can never vouch for public sites, and the same chain logic applies.

Revocation

What if a private key leaks before the certificate expires? The CA must be able to take it back. Two mechanisms exist, and both are imperfect:

  • A certificate revocation list (CRL) is a signed list of revoked serial numbers published by the CA. Clients download it, but lists can grow large.
  • OCSP lets a client ask "is serial 4242 still valid?" in real time. It leaks which sites you visit to the CA and fails awkwardly when the responder is down. OCSP stapling has the server fetch a fresh signed answer and attach it to the handshake, which fixes the privacy problem. Several large CAs have started moving away from OCSP towards CRLs and shorter-lived certificates.

Browsers often soft-fail: if the revocation service cannot be reached they carry on, because blocking all traffic would be worse than the risk. That is why short lifetimes are considered the more reliable defense than revocation.

Certificate pinning

Pinning narrows trust further. Instead of accepting any certificate that chains to any root, a client remembers which key a particular service should use and rejects everything else. The usual pin is the SHA-256 hash of the certificate's public key information.

Pinning stops a rogue CA, but it has a sharp edge: if you lose or rotate the pinned key without planning, your own app locks everyone out until they update. Browsers dropped the HTTP-header version of pinning for exactly this reason; today it is used mostly inside mobile apps and internal tools, always with a backup pin for a key held in reserve.

Python Revocation lists and key pinning

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Seeing real certificates

You can inspect real certificates on your own computer. These commands are read-only here because the site's runner has no network, but they are worth trying in a terminal:

Terminal
# Show the certificate chain a server sends, with subjects, issuers and validity
openssl s_client -connect example.com:443 -servername example.com -showcerts

# Decode a certificate file and print its fields
openssl x509 -in cert.pem -noout -text

# Print the SHA-256 pin of a certificate's public key
openssl x509 -in cert.pem -pubkey -noout \
  | openssl pkey -pubin -outform der \
  | openssl dgst -sha256 -binary | openssl enc -base64

Your browser also shows this: click the padlock, then the certificate details, and you will see the same chain from leaf to root.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a certificate bind together?

    The certificate states that a particular public key belongs to a particular subject, and the issuer's signature makes the statement checkable.
  2. 2. Why do root CAs usually sign intermediates instead of website certificates directly?

    Keeping the root key in cold storage greatly reduces its exposure. Day-to-day issuing uses intermediates, which can be revoked and replaced without changing the trust store.
  3. 3. A client validates a chain whose last certificate is signed by a root it has never heard of. What happens?

    A valid signature only proves who signed. Whether that signer is trusted is decided by the trust store the user's device already holds.
  4. 4. What is the main risk of certificate pinning without a backup pin?

    Pins are hashes of public keys and are not secret. The danger is operational: with no reserve key pinned, a necessary key change locks out every client that has the old pin.
  5. 5. Why is Certificate Transparency useful?

    Public append-only logs record every certificate. Owners can monitor them for names they control, and browsers require proof of logging.

Summary

  • A certificate is a signed statement binding a public key to a name, with a validity period, serial number and extensions such as the covered domain names and the CA flag.
  • Clients trust a few hundred self-signed roots in their trust store; roots sign intermediates, intermediates sign leaves, and each link is checked for signature, dates and permission to sign.
  • Any trusted root can issue for any name, so the ecosystem relies on audits, domain validation, CAA records and public Certificate Transparency logs.
  • Revocation (CRLs, OCSP, stapling) is imperfect and often soft-fails, which is why short certificate lifetimes and automation matter.
  • Pinning restricts a client to chosen keys; always pin a backup key and plan rotation.

Next: How TLS Works, where certificates, key exchange and symmetric encryption come together in one handshake.