PKI and Digital Certificates: Core Concepts
PKI and Digital Certificates: Core Concepts
Asymmetric encryption lets you encrypt data with a public key, but how do you know that a public key actually belongs to the person or server you think it does? If an attacker can substitute their own public key for the target's, they can intercept and decrypt all your communications. Public Key Infrastructure (PKI) solves this trust problem by using digital certificates to bind public keys to identities.
What a Certificate Contains
An X.509 certificate (the standard format) contains:
- Subject — The entity the certificate identifies (domain name, organization, or individual)
- Issuer — The Certificate Authority (CA) that signed the certificate
- Public key — The subject's public key
- Validity period — Not Before and Not After dates
- Serial number — Unique identifier assigned by the CA
- Signature algorithm — The algorithm used by the CA to sign (e.g., SHA256withRSA)
- Signature — The CA's digital signature over all the above fields
The certificate is essentially a statement by the CA: "We have verified that this public key belongs to this subject."
Certificate Authorities and the Chain of Trust
A Certificate Authority (CA) is a trusted entity that issues and signs certificates. The CA uses its own private key to sign certificates, and anyone with the CA's public key can verify those signatures.
The trust model is hierarchical:
Root CA (self-signed, offline)
└── Intermediate CA (signed by Root)
└── End-entity certificate (signed by Intermediate)
Root CAs are self-signed and kept offline (air-gapped) to protect their private keys. If a root CA's private key is compromised, every certificate in the hierarchy is untrustworthy.
Intermediate CAs are signed by the root and issue end-entity certificates. This separation means the root can stay offline while intermediates handle daily operations. If an intermediate is compromised, only its certificates need to be revoked — the root remains trusted.
End-entity certificates are the certificates presented by servers, clients, or individuals. They cannot sign other certificates.
Certificate Validation
When a client receives a certificate, it validates the entire chain:
- Verify the signature — Check that each certificate's signature is valid using the issuer's public key
- Check the chain — Follow the chain from the end-entity certificate up to a trusted root CA
- Check validity dates — Ensure the current date is within the certificate's validity period
- Check revocation — Verify the certificate has not been revoked (via CRL or OCSP)
- Check the hostname — Verify the certificate's Subject Alternative Name (SAN) matches the hostname you are connecting to
# View a certificate's details
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -text -noout
# Verify a certificate chain
openssl verify -CAfile root-ca.pem -untrusted intermediate.pem server-cert.pem
Where It Fits
PKI is the trust infrastructure that makes TLS, code signing, email encryption (S/MIME), and document signing possible. Study Asymmetric Encryption first to understand the key pairs that certificates bind to identities, then move to the TLS Handshake to see how certificates are used in practice.
PKI and Digital Certificates: Practice
PKI and Digital Certificates: Practice
PKI is best understood by building a small certificate authority and issuing certificates. The exercises below walk through creating a root CA, an intermediate CA, and end-entity certificates.
Exercise 1: Create a Root CA
# Generate the root CA private key (keep this offline!)
openssl genrsa -out root-ca.key 4096
# Create the root CA self-signed certificate
openssl req -new -x509 -key root-ca.key -sha256 -days 3650 \
-out root-ca.pem \
-subj "/C=US/ST=State/L=City/O=MyOrg/CN=My Root CA"
# View the certificate
openssl x509 -in root-ca.pem -text -noout | head -20
The root CA certificate is self-signed — the issuer and subject are the same. This is the trust anchor.
Exercise 2: Create an Intermediate CA
# Generate the intermediate CA private key
openssl genrsa -out intermediate.key 4096
# Create a Certificate Signing Request (CSR)
openssl req -new -key intermediate.key \
-out intermediate.csr \
-subj "/C=US/ST=State/L=City/O=MyOrg/CN=My Intermediate CA"
# Sign the CSR with the root CA to create the intermediate certificate
openssl x509 -req -in intermediate.csr -CA root-ca.pem \
-CAkey root-ca.key -CAcreateserial -days 1825 \
-sha256 -out intermediate.pem \
-extfile <(echo "basicConstraints=CA:TRUE")
The basicConstraints=CA:TRUE extension marks this certificate as a CA certificate, allowing it to sign other certificates.
Exercise 3: Issue a Server Certificate
# Generate the server private key
openssl genrsa -out server.key 2048
# Create a CSR with the server's hostname
openssl req -new -key server.key \
-out server.csr \
-subj "/C=US/ST=State/L=City/O=MyOrg/CN=www.example.com"
# Sign with the intermediate CA, including SAN extension
openssl x509 -req -in server.csr -CA intermediate.pem \
-CAkey intermediate.key -CAcreateserial -days 365 \
-sha256 -out server.pem \
-extfile <(echo "subjectAltName=DNS:www.example.com,DNS:example.com")
# Verify the full chain
openssl verify -CAfile root-ca.pem -untrusted intermediate.pem server.pem
Exercise 4: Check Certificate Revocation with OCSP
# Query an OCSP responder
openssl ocsp -issuer intermediate.pem -cert server.pem \
-url http://ocsp.example.com -resp_text
OCSP (Online Certificate Status Protocol) provides real-time revocation status. An alternative is CRL (Certificate Revocation List), which is a periodically updated list of revoked certificate serial numbers.
Check Your Understanding
- Why are root CAs kept offline?
- What is the purpose of an intermediate CA?
- What happens if a certificate's private key is compromised?
PKI and Digital Certificates: Trade-offs and Interviews
PKI and Digital Certificates: Trade-offs and Interviews
PKI is the foundation of trust on the internet, but it is also one of the most complex and failure-prone security systems. Understanding its limitations is essential for both operating and securing PKI.
When PKI Is the Wrong Tool
- Internal-only services: For services that are never exposed to the internet, a private PKI adds complexity without proportional benefit. Use shared secrets or SSH key-based authentication instead.
- Short-lived credentials: For ephemeral workloads (containers, serverless functions), the overhead of certificate issuance and renewal may exceed the security benefit. Use short-lived tokens (OAuth, SPIFFE) instead.
- Small teams: Operating a private PKI requires expertise and ongoing maintenance. For small teams, Let's Encrypt (free, automated certificates) or cloud provider certificate managers are better options.
Common PKI Failures
- Expired certificates: The most common PKI failure. An expired certificate causes service outages and security warnings. Implement automated renewal (ACME protocol, cert-manager).
- Weak key algorithms: Certificates signed with RSA-1024 or SHA-1 are no longer trusted. Ensure all certificates use RSA-2048+ or ECC-256+ and SHA-256+.
- Misconfigured SANs: If the certificate's Subject Alternative Name does not match the hostname, clients will reject it. Always include all hostnames the certificate will be used for.
- Compromised CA: If a CA's private key is compromised, every certificate it issued must be revoked. This is why root CAs are kept offline and intermediates are used for daily operations.
Let's Encrypt and Automated PKI
Let's Encrypt provides free, automated domain-validated certificates using the ACME protocol. This has dramatically reduced the cost and complexity of TLS:
# Obtain a certificate with certbot
certbot certonly --standalone -d www.example.com -d example.com
# Certbot automatically renews via a cron job or systemd timer
certbot renew --dry-run
Let's Encrypt certificates are valid for 90 days, which encourages automation. The short validity period limits the damage from a compromised key.
How This Comes Up in Interviews
- "How does a client verify a server's certificate?" The client checks the certificate chain from the end-entity certificate up to a trusted root CA, verifies each signature, checks validity dates, checks revocation status (CRL/OCSP), and verifies the hostname matches the SAN.
- "What happens if a CA's private key is compromised?" Every certificate issued by that CA (and its intermediates) must be revoked. This is why root CAs are kept offline and why intermediate CAs limit the blast radius.
- "How do you handle certificate renewal in production?" Use automated tools like certbot with the ACME protocol, or a certificate manager like cert-manager in Kubernetes. Never rely on manual renewal — expired certificates cause outages.
Practice Problems
Solution
```bash
# Root CA
openssl genrsa -out root-ca.key 4096
openssl req -new -x509 -key root-ca.key -sha256 -days 3650 \
-out root-ca.pem -subj "/CN=My Root CA"
# Intermediate CA
openssl genrsa -out intermediate.key 4096
openssl req -new -key intermediate.key -out intermediate.csr \
-subj "/CN=My Intermediate CA"
openssl x509 -req -in intermediate.csr -CA root-ca.pem \
-CAkey root-ca.key -CAcreateserial -days 1825 -sha256 \
-out intermediate.pem -extfile <(echo "basicConstraints=CA:TRUE")
# Server certificate
openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr \
-subj "/CN=www.example.com"
openssl x509 -req -in server.csr -CA intermediate.pem \
-CAkey intermediate.key -CAcreateserial -days 365 -sha256 \
-out server.pem -extfile <(echo "subjectAltName=DNS:www.example.com")
# Verify
openssl verify -CAfile root-ca.pem -untrusted intermediate.pem server.pem
``` Solution
1) The certificate's Subject Alternative Name (SAN) does not match the hostname the user is accessing. 2) The intermediate CA certificate is not being sent by the server, so the client cannot build the chain to a trusted root. 3) The root CA is not in the client's trust store (common for private PKI). 4) The certificate has been revoked and the client is checking revocation status. Quiz
1. What does a digital certificate bind together?
2. Why are root CAs typically kept offline?
3. What is the purpose of an intermediate CA?
4. What is the difference between CRL and OCSP?
Flashcards
Question
What does an X.509 certificate contain?
Click to reveal answer
Answer
Subject, issuer, public key, validity period, serial number, signature algorithm, and the CA's digital signature.
Question
What is the chain of trust in PKI?
Click to reveal answer
Answer
Root CA (self-signed, offline) → Intermediate CA (signed by root) → End-entity certificate (signed by intermediate).
Question
What is the purpose of the Subject Alternative Name (SAN) extension?
Click to reveal answer
Answer
Lists the hostnames the certificate is valid for. Clients verify that the hostname they are connecting to matches one of the SAN entries.
Question
What is Let's Encrypt?
Click to reveal answer
Answer
A free, automated certificate authority that issues domain-validated certificates using the ACME protocol. Certificates are valid for 90 days.
Question
What happens if an intermediate CA is compromised?
Click to reveal answer
Answer
Only the certificates issued by that intermediate need to be revoked. The root CA remains trusted, limiting the blast radius.
Revision Notes
Key Takeaways
- 1. Certificates bind a public key to a subject name signed by a certificate authority.
- 2. Certificate chains establish trust from a root CA through intermediates to end-entity certificates.
- 3. Root CAs are kept offline to protect the trust anchor.
- 4. Let's Encrypt provides free, automated certificates via the ACME protocol.
- 5. Certificate validation includes signature verification, chain building, validity dates, revocation checks, and hostname matching.
Interview Tips
- • Explain the chain of trust and why root CAs are kept offline.
- • Describe the certificate validation process step by step.
- • Discuss common PKI failures (expired certs, misconfigured SANs, compromised CAs) and their mitigations.
- • Connect PKI to real-world applications: TLS, code signing, S/MIME, and automated certificate management.
Cheat Sheet
PKI and Digital Certificates Cheat Sheet
Phase: Cryptography Foundations | Level: intermediate
Certificate Contents:
- Subject, issuer, public key, validity period, serial number, signature
Chain of Trust:
Root CA (self-signed, offline)
→ Intermediate CA (signed by root)
→ End-entity certificate (signed by intermediate)
Validation Steps:
- Verify signature using issuer's public key
- Follow chain to trusted root CA
- Check validity dates
- Check revocation (CRL/OCSP)
- Verify hostname matches SAN
OpenSSL Commands:
openssl req -new -x509— Create self-signed certopenssl req -new— Create CSRopenssl x509 -req— Sign a CSRopenssl verify -CAfile root.pem -untrusted intermediate.pem server.pem— Verify chainopenssl x509 -in cert.pem -text -noout— View certificate
Tags: pki, cryptography
Continue Learning
Before You Start
- No prerequisites listed.