22.8. TLS Certificates
Table of Contents
Why TLS Certificates Exist
When you visit https://example.com, your browser wants to be sure of two things:
- Your connection is encrypted, so others cannot read or modify the data.
- You are really talking to example.com, not an attacker.
TLS (Transport Layer Security) is the protocol that encrypts traffic.
A TLS certificate is the digital document that proves the server’s identity and helps set up encryption.
A website can only be considered secure if:
- It uses HTTPS (TLS encryption), and
- The TLS certificate is valid, not expired, issued by a trusted authority, and matches the domain.
Without a proper TLS certificate, users see browser warnings like “Your connection is not private.”
Basic Concepts and Terminology
Public Key, Private Key, and Certificates
TLS uses public key cryptography:
| Item | Kept Where | Purpose |
|---|---|---|
| Private key | On the server only | Decrypt data, prove identity |
| Public key | Inside the certificate | Encrypt data, verify signatures |
| Certificate | Sent to clients | Binds public key to a domain or organization |
The certificate is basically:
“The holder of this private key is allowed to use this domain name.”
signed by a Certificate Authority.
If an attacker steals the private key, they can impersonate your site.
So private key protection is critical.
Certificate Authorities (CAs) and Trust
Browsers and operating systems ship with a trust store, a list of trusted CAs.
A Certificate Authority:
- Verifies that you control a domain (and sometimes your organization).
- Issues a certificate signed with its own private key.
- Browsers trust your certificate because they already trust the CA.
Your TLS certificate is trusted only if:
- It is signed by a CA present in the client’s trust store, or
- It chains up to such a CA with valid intermediate certificates.
Types of TLS Certificates
By Identity Level
Domain Validated (DV)
- Validates: You control the domain, such as
example.com. - How: Email to
admin@example.com, DNS record, or HTTP file check. - Shows: Padlock and HTTPS. No visible company name.
- Use: Most APIs and websites. Lets Encrypt issues DV certificates.
Organization Validated (OV)
- Validates: Domain + legal organization details.
- CA checks company registration and ownership.
- Used mainly by corporations and institutions.
Extended Validation (EV)
- Validates: Domain + strict company checks, legal existence, physical presence.
- Historically showed company name in browser UI.
- Used for banks, financial services, etc.
- Modern browsers show less special UI than before, so EV is less common.
For backend APIs, DV is usually enough.
By Domain Coverage
| Type | Example domains covered |
|---|---|
| Single-domain | example.com or api.example.com |
| Wildcard | *.example.com (api.example.com, shop.example.com) |
| Multi-domain (SAN) | example.com, api.example.com, other-site.com |
A wildcard *.example.com does not cover the root example.com itself.
You must explicitly include example.com if you need it.
Certificate Chain and Root Certificates
What Is a Certificate Chain?
A browser does not directly trust your server certificate. It trusts root CAs.
Typical structure:
- Root CA certificate
- Self-signed. Stored in the OS or browser trust store.
- Intermediate CA certificate(s)
- Signed by the root. Used to sign server certificates.
- Server (leaf) certificate
- Your domain’s certificate, signed by an intermediate CA.
On TLS handshake, the server should send:
- The server certificate, and
- All intermediate certificates.
The browser uses these to build a chain up to a trusted root.
If an intermediate is missing, clients may show errors like:
- “Incomplete certificate chain”
- “Unable to get local issuer certificate”
Self-Signed Certificates
A self-signed certificate is signed by its own private key, not by a CA.
- Good for: Local development, internal testing.
- Not trusted by browsers by default.
- To avoid warnings, you would have to add it to the client’s trust store manually.
Use self-signed certs only for:
- Development environments.
- Internal systems where you control the clients and can install the root certificate.
How TLS Handshake Uses Certificates
Here is a simplified view of the TLS handshake with HTTPS:
- Client connects to
https://example.comon port 443. - Client sends supported protocol versions and cipher suites.
- Server responds with:
- Its certificate (with public key),
- Intermediate certificates,
- Chosen cipher suite.
- Client verifies the certificate:
- Signature validates up the chain to a trusted root.
- Not expired.
- Not revoked.
- Domain name matches (
example.com). - If all checks pass, client:
- Creates a random secret key.
- Encrypts it with the server’s public key.
- Sends it to the server.
- Server decrypts it using its private key.
- Both sides now share a session key for fast symmetric encryption.
From this point, the connection is encrypted.
If the certificate is invalid, expired, or does not match the domain, the browser must warn the user.
Users should not ignore these warnings in production environments.
Managing TLS Certificates
Key Files You Will See
On a Linux server with Nginx or similar you typically see:
| File | Contains |
|---|---|
privkey.pem | Private key for your domain |
cert.pem | Server certificate for your domain |
chain.pem | Intermediate certificate(s) |
fullchain.pem | cert.pem + chain.pem concatenated |
Some tools or servers can accept a single file that combines them.
Common Certificate Formats
| Extension | Encoding | Typical use |
|---|---|---|
.pem | Base64 with headers -----BEGIN CERTIFICATE----- | Linux, Nginx, Apache |
.crt | Often same as .pem | Generic cert file |
.key | Private key in PEM format | Server private key |
.pfx / .p12 | Binary PKCS#12 format | Windows, some load balancers, Java key stores |
Converting often uses openssl, for example:
# Convert PFX to PEM (cert + key)
openssl pkcs12 -in cert.pfx -out cert.pem -nodesGetting TLS Certificates
Free Certificates with Let’s Encrypt
Let’s Encrypt is a free, automated CA that issues DV certificates.
Typical flow on a Linux server:
- Install Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx- Request a certificate for your domain (Nginx example):
sudo certbot --nginx -d example.com -d www.example.comCertbot:
- Proves domain control by serving a special file over HTTP or using DNS.
- Requests a certificate from Let’s Encrypt.
- Configures Nginx to use the new certificate.
- Sets up automatic renewals via cron or systemd.
Check renewal status:
sudo certbot renew --dry-runPaid Certificates
Some organizations still buy certificates from commercial CAs, for example:
- To get OV or EV certificates.
- To meet specific compliance requirements.
- For special-use cases or support agreements.
The process is similar:
- Generate a CSR (Certificate Signing Request) on the server.
- Submit CSR to the CA.
- Complete domain and / or organization verification.
- Download the certificate and intermediate chain.
- Configure on the server.
CSR generation example:
# generate a private key
openssl genrsa -out example.com.key 2048
# generate a CSR
openssl req -new -key example.com.key -out example.com.csrCertificate Lifetimes, Renewal, and Revocation
Expiration and Renewal
Certificates have a validity period:
- Let’s Encrypt: typically 90 days.
- Many commercial CAs: up to 1 year.
If the expiration date passes, clients will reject the cert.
Always set up automatic renewal for TLS certificates.
An expired certificate is a very common and easily avoidable production outage.
For Let’s Encrypt, Certbot handles:
sudo certbot renewYou still must reload or restart your web server after renewal if the tool does not do it automatically.
For example, with Nginx:
sudo systemctl reload nginxRevocation
If a private key is leaked or a domain is no longer under your control, you must revoke the certificate.
- Revocation tells clients that a certificate should no longer be trusted before it expires.
- CAs publish revocation via:
- CRL (Certificate Revocation Lists),
- OCSP (Online Certificate Status Protocol).
Modern browsers mainly use OCSP or OCSP stapling.
As a backend developer, if you suspect key compromise:
- Generate a new key and CSR.
- Get a new certificate.
- Request revocation of the old certificate from the CA.
Common Certificate Problems and Errors
Common Browser Errors
| Error message (example) | Likely cause |
|---|---|
| “Your connection is not private” | Multiple possible issues |
| “Certificate has expired” | Certificate validity end date passed |
| “Certificate not yet valid” | System time incorrect or wrong notBefore date |
| “Certificate is not trusted” | Unknown CA or missing chain |
| “Hostname mismatch” or “SEC_ERROR_BAD_CERT_DOMAIN” | Cert’s CN/SAN does not match domain |
Check using tools like:
openssl s_client -connect example.com:443 -servername example.comor online services like SSL Labs’ SSL Server Test.
Hostname Mismatch Example
If your certificate is issued for api.example.com and you use it for example.com, browsers will show a domain mismatch error.
Fix: Request a certificate that covers all required hostnames, for example:
example.comwww.example.comapi.example.com
TLS Certificates in Reverse Proxies and Load Balancers
Termination at the Reverse Proxy
In many backend setups:
- Nginx or Traefik is the reverse proxy.
- Uvicorn or Gunicorn serves your Python app.
Typical pattern:
- Client connects to
https://api.example.com:443with TLS. - Nginx terminates TLS using the certificate.
- Nginx forwards traffic to your app over HTTP, for example
http://127.0.0.1:8000.
In this case:
- The TLS certificate and key exist only on the reverse proxy.
- The application server does not need a certificate.
This is called TLS termination or SSL termination at the reverse proxy.
End-to-End Encryption
Sometimes, especially in multi-server or multi-datacenter setups, you want encryption:
- Client to reverse proxy.
- Reverse proxy to backend services.
Then each hop uses its own certificate.
For internal traffic you might:
- Use an internal CA.
- Use self-signed certs, and configure services to trust that CA.
Security Best Practices for TLS Certificates
Strong Keys and Algorithms
- Use at least 2048-bit RSA keys or modern elliptic curve keys (like ECDSA).
- Disable obsolete protocols and ciphers:
- Disable SSLv2, SSLv3, TLS 1.0, TLS 1.1.
- Prefer TLS 1.2 and TLS 1.3.
Nginx example snippet:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;Private Key Protection
Never commit private keys to Git or store them in public places.
Limit file permissions to the web server user only.
On Linux:
chown root:root /etc/ssl/private/example.com.key
chmod 600 /etc/ssl/private/example.com.keyHTTP to HTTPS Redirect
Always redirect HTTP to HTTPS so all traffic is encrypted.
Nginx example:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}HSTS (HTTP Strict Transport Security)
HSTS tells browsers to always use HTTPS for your domain, which helps prevent downgrade attacks.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Use with care, especially includeSubDomains, because once enabled for a long max-age it is hard to roll back.
Example: Nginx Configuration Using a TLS Certificate
Assume you have:
- Certificate:
/etc/letsencrypt/live/example.com/fullchain.pem - Private key:
/etc/letsencrypt/live/example.com/privkey.pem - FastAPI app at
http://127.0.0.1:8000
Example Nginx server block:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
}
}This config shows how your certificate ties into real-world backend deployment.
Summary
- TLS certificates prove a server’s identity and enable encrypted connections.
- They are issued and signed by Certificate Authorities that browsers trust.
- A full certificate chain is required, from server certificate up to a trusted root.
- You can use free DV certificates from Let’s Encrypt, often managed automatically by Certbot.
- Certificates expire and must be renewed; set up automatic renewal.
- Protect private keys and configure your reverse proxy correctly.
- In a typical backend, TLS terminates at the reverse proxy such as Nginx or Traefik, which holds the certificate and forwards traffic to your application server.
Views: 5
KAHIBARO