Digital Certificates and PKI
X.509, certificate authorities, trust chains, and Let's Encrypt
Skills taught: crypto.certificates
X.509 Digital Certificates
A digital certificate binds a public key to an identity. When you visit https://bank.com, the server presents a certificate saying: 'This public key belongs to bank.com, and I (a trusted CA) vouch for it.' Your browser verifies the CA's signature and trusts the connection.
- Certificate Authority (CA)
- A trusted third party that verifies domain ownership and signs certificates. Browsers ship with ~150 trusted root CAs. If a CA is compromised, it is removed from browser trust stores.
- Certificate Chain
- Root CA → Intermediate CA → Leaf (your) certificate. Browsers verify by walking up the chain to a trusted root. The root CA's private key is kept offline; intermediate CAs are online for day-to-day signing.
- CSR (Certificate Signing Request)
- A file containing your public key and domain information, sent to a CA for signing. The CA verifies you own the domain, then returns a signed certificate.
Certificate Operations with OpenSSL
# --- View a certificate ---
openssl x509 -in cert.pem -text -noout
# --- Generate a self-signed certificate (development only!) ---
openssl req -x509 -newkey rsa:4096 \
-keyout key.pem -out cert.pem \
-days 365 -nodes \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
# --- Generate a CSR (send to CA to get a signed certificate) ---
openssl genrsa -out key.pem 2048
openssl req -new -key key.pem -out request.csr \
-subj "/CN=example.com/O=My Company/C=US"
# --- Check certificate expiry ---
openssl x509 -in cert.pem -noout -dates
# --- Check which CA signed a certificate ---
openssl x509 -in cert.pem -noout -issuer
# --- Verify a certificate chain ---
openssl verify -CAfile ca-bundle.pem cert.pem
Let's Encrypt: Free Automated Certificates
Let's Encrypt is a free, automated CA that issues DV (Domain Validation) certificates in seconds using the ACME protocol. Certbot automates issuance and renewal. No manual CSR submission needed.
# Install certbot
sudo apt install certbot python3-certbot-nginx
# Obtain and configure certificate for Nginx
sudo certbot --nginx -d example.com -d www.example.com
# Obtain certificate only (manual configuration)
sudo certbot certonly --standalone -d example.com
# Auto-renew (certificates expire in 90 days)
# Certbot adds a cron job or systemd timer automatically
sudo certbot renew --dry-run
# Check renewal timer
systemctl status certbot.timer
# Certificate files are in:
# /etc/letsencrypt/live/example.com/fullchain.pem (cert + chain)
# /etc/letsencrypt/live/example.com/privkey.pem (private key)
Using Certificates in Python
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa
import datetime
# --- Generate private key ---
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
# --- Build a self-signed certificate ---
subject = issuer = x509.Name([
x509.NameAttribute(NameOID.COMMON_NAME, "localhost"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "My Dev Server"),
])
cert = (
x509.CertificateBuilder()
.subject_name(subject)
.issuer_name(issuer)
.public_key(key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(datetime.datetime.now(datetime.timezone.utc))
.not_valid_after(datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=365))
.add_extension(x509.SubjectAlternativeName([x509.DNSName("localhost")]), critical=False)
.sign(key, hashes.SHA256())
)
# Save certificate
with open("cert.pem", "wb") as f:
f.write(cert.public_bytes(serialization.Encoding.PEM))
# Save private key (no password for dev; always password-protect in prod)
with open("key.pem", "wb") as f:
f.write(key.private_bytes(
serialization.Encoding.PEM,
serialization.PrivateFormat.TraditionalOpenSSL,
serialization.NoEncryption()
))
DV vs OV vs EV Certificates
- DV (Domain Validation): CA verifies only that you control the domain
- OV/EV (Organization/Extended Validation): CA verifies legal organization identity
Warning: Never use a self-signed certificate in production — browsers will show a scary warning and users will be trained to click through warnings (which is dangerous). Self-signed certs are only acceptable for internal dev/test environments.
Tip: Add a CAA DNS record to restrict which CAs can issue certificates for your domain: example.com. CAA 0 issue "letsencrypt.org". This prevents rogue CAs from issuing certs for your domain.
Frequently Asked Questions
What is certificate pinning?
Hardcoding the expected certificate or public key hash in an app. This prevents MitM attacks even with a rogue CA cert. Used in mobile apps. HPKP (HTTP header version) was deprecated because misconfigurations could brick a domain — use certificate transparency monitoring instead.
How often should I renew my certificate?
Let's Encrypt certificates expire in 90 days. Certbot renews automatically at ~60 days. For manually obtained certificates, renew 30+ days before expiry. Certificate expiry has caused major outages at Microsoft, Google, and AWS.
Should I pay for a certificate?
No, for DV certificates. Let's Encrypt provides free, automated DV certificates trusted by all major browsers. Only pay for OV/EV certificates if your organization's legal identity in the certificate is a business requirement.