Home/Learn/Computer Networks/Certificates & PKI

Certificates & PKI

Intermediate
Security

Public Key Infrastructure (PKI) is the trust hierarchy of Certificate Authorities, digital certificates, and revocation mechanisms that allows a client to verify it is talking to the intended server and not an impersonator.

Overview

When your browser connects to a website, how does it know the server is genuine and not an attacker performing a man-in-the-middle attack? The answer is PKI. An X.509 certificate binds a public key to an identity (domain name, organisation) and is signed by a Certificate Authority (CA) that both parties trust. Trust is hierarchical: a Root CA signs Intermediate CAs, which sign end-entity certificates. Browsers and OS ship with a list of trusted Root CAs. PKI underpins HTTPS, code signing, email encryption (S/MIME), and VPN authentication.

X.509 Certificate Structure

An X.509 certificate is a DER/PEM-encoded document containing the subject's identity, their public key, validity period, the issuing CA, and a digital signature from the CA over the certificate content.

X.509 certificate structure
// X.509 Certificate fields (simplified):
// ┌────────────────────────────────────────────────────────────────┐
// │ Version         : 3                                           │
// │ Serial Number   : 0x4A2B...  (unique per CA)                  │
// │ Signature Alg   : sha256WithRSAEncryption                     │
// │ Issuer          : CN=Let's Encrypt R3, O=Let's Encrypt        │
// │ Validity        : 2026-01-01 → 2026-04-01  (90-day cert)     │
// │ Subject         : CN=aicancode.org                            │
// │ Public Key      : RSA 2048-bit / EC P-256                     │
// │ Subject Alt Names (SAN): aicancode.org, www.aicancode.org    │
// │ Key Usage       : Digital Signature, Key Encipherment         │
// │ CA Signature    : <Let's Encrypt signs the above with its key>│
// └────────────────────────────────────────────────────────────────┘

// Inspect a certificate from command line:
openssl s_client -connect aicancode.org:443 -showcerts
openssl x509 -in cert.pem -text -noout

Certificate Chain & Chain of Trust

Trust is established through a chain: the end-entity certificate is signed by an Intermediate CA, which is signed by a Root CA. The Root CA is self-signed and its certificate is pre-installed in browsers/OS trust stores. During TLS, the server sends the full chain (end-entity + intermediates); the client verifies each signature up to a trusted root.

Certificate chain verification
// Certificate chain for aicancode.org:
//
//   Root CA (built into OS / browser trust store)
//   └─ DigiCert Global Root CA  [self-signed, 20-year validity]
//       └─ DigiCert TLS RSA SHA256 2020 CA1  [intermediate, 5-year]
//           └─ aicancode.org  [end-entity, 1-year]
//
// Verification steps (client):
// 1. Hash the end-entity cert content
// 2. Decrypt intermediate's signature with intermediate's public key → compare hash
// 3. Hash intermediate cert content
// 4. Decrypt root's signature with root's public key → compare hash
// 5. Check root is in trust store → TRUSTED

// If any step fails → certificate error (NET::ERR_CERT_AUTHORITY_INVALID)

Certificate Revocation: CRL & OCSP

Certificates have a validity period but can be revoked early (private key compromise, domain change). Two revocation mechanisms exist: Certificate Revocation Lists (CRL) — a periodically published list of revoked serial numbers — and Online Certificate Status Protocol (OCSP) — a real-time query to the CA. OCSP Stapling allows the server to embed a fresh CA-signed OCSP response in the TLS handshake, eliminating client round-trips.

Certificate revocation: CRL vs OCSP vs OCSP Stapling
// CRL (Certificate Revocation List):
// CA publishes a signed list of revoked serial numbers at a URL (CDP extension)
// Client downloads CRL and checks if cert serial is in the list
// Problem: CRL can be megabytes in size and is cached — not real-time

// OCSP (Online Certificate Status Protocol):
// Client sends: OCSP Request { serial: 0x4A2B... } → CA OCSP responder
// CA responds:  OCSP Response { status: good | revoked | unknown, thisUpdate, nextUpdate }
// Problem: extra round-trip per connection; privacy leak (CA sees every connection)

// OCSP Stapling (best practice):
// Server periodically fetches OCSP response and "staples" it to TLS handshake
// Client gets status without extra round-trip or privacy leak
// Nginx config:
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8;

Let's Encrypt & ACME Protocol

Let's Encrypt is a free, automated CA that issues 90-day DV (Domain Validation) certificates. The ACME protocol automates certificate issuance and renewal — the CA challenges the domain owner to prove control via HTTP-01 (serve a file at /.well-known/acme-challenge/) or DNS-01 (add a TXT record). Certbot automates this on Linux servers.

Let's Encrypt ACME certificate automation
// ACME HTTP-01 challenge flow:
// 1. Client (certbot) requests cert for aicancode.org from Let's Encrypt
// 2. LE sends challenge token
// 3. certbot writes file:
//    /.well-known/acme-challenge/Kxyz...abc
//    content: Kxyz...abc.fingerprint
// 4. LE fetches http://aicancode.org/.well-known/acme-challenge/Kxyz...abc
// 5. Content matches → domain ownership proved → cert issued

// Auto-renewal (cron):
0 3 * * * certbot renew --quiet --post-hook "systemctl reload nginx"

// Wildcard cert requires DNS-01 (add TXT record _acme-challenge.aicancode.org)
certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/.cloudflare.ini   -d "*.aicancode.org" -d "aicancode.org"

Key Points to Remember

  • 1An X.509 certificate binds a public key to an identity via a CA's digital signature — proving the server is who it claims to be.
  • 2Trust is hierarchical: Root CA → Intermediate CA → End-Entity cert. Roots are pre-installed in OS/browser trust stores.
  • 3OCSP Stapling eliminates the extra round-trip and privacy leak of traditional OCSP without sacrificing freshness.
  • 4Let's Encrypt issues free 90-day certificates; ACME protocol automates renewal — zero-cost HTTPS for every project.
  • 5A certificate with an invalid chain, expired validity, or revoked status causes browser warnings and failed API calls.

Interview Questions

Sign in to ask Aria
1

How does a browser verify a TLS certificate is legitimate?

MediumThoughtWorks
2

What is a certificate chain and why do intermediate CAs exist?

MediumAmazon
3

What is OCSP Stapling and why is it better than plain OCSP?

HardFlipkart
4

What happens when a certificate is revoked? How does the client find out?

MediumRazorpay
5

What is the difference between DV, OV, and EV certificates?

EasyCapgemini

Ask Aria about Certificates & PKI

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…