Skip to content

TLS & PKI

Interactive Cryptography Visualizer

Explore symmetric encryption, asymmetric encryption, and the TLS handshake step by step. See how keys are exchanged and data is encrypted in real time.

Cryptography Visualizer

Explore symmetric & asymmetric encryption, TLS handshakes, and hashing

Same key encrypts and decrypts - like a shared padlock

Plaintext
Hello, World!
Key
shift(7)
AES-like
Ciphertext
???
Same Key
shift(7)
Decrypt
Decrypted
???
HEX REPRESENTATION
Plain: 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21
Cipher: 4F 6C 73 73 76 33 27 5E 76 79 73 6B 28
Key Concept: In symmetric encryption (e.g., AES), the same key is used for both encryption and decryption. The challenge is securely sharing this key between parties.

TLS (Transport Layer Security) is the protocol that secures virtually all internet communication. Every time you see the padlock icon in your browser, TLS is at work — encrypting data in transit, authenticating the server, and ensuring integrity of the connection. PKI (Public Key Infrastructure) is the trust framework that makes TLS possible by binding public keys to verified identities through digital certificates.


Why TLS Matters

Without TLS, all data sent between a client and server travels in plaintext — readable by anyone on the network path. This includes:

  • Usernames and passwords
  • Credit card numbers and banking details
  • Private messages and emails
  • API keys and session tokens
  • Personal health information
Without TLS:
┌────────┐ ┌────────┐
│ Client │──── GET /login?pass=secret ────────▶│ Server │
└────────┘ ▲ └────────┘
┌─────┴─────┐
│ Attacker │
│ reads: │
│ pass=secret│
└───────────┘
With TLS:
┌────────┐ ┌────────┐
│ Client │──── 7g$kL9!xmP#2bNq... ──────────▶│ Server │
└────────┘ ▲ └────────┘
┌─────┴─────┐
│ Attacker │
│ sees only │
│ encrypted │
│ gibberish │
└───────────┘

TLS Provides Three Guarantees

GuaranteeDescriptionCryptographic Mechanism
ConfidentialityData cannot be read by eavesdroppersSymmetric encryption (AES-GCM, ChaCha20)
IntegrityData cannot be modified in transitMAC (message authentication code)
AuthenticationServer (and optionally client) identity is verifiedDigital certificates + signatures

The TLS 1.3 Handshake

TLS 1.3 (finalized in 2018) is a major improvement over TLS 1.2. It is faster, simpler, and more secure — completing the handshake in just one round trip (1-RTT) compared to two round trips in TLS 1.2.

TLS 1.3 Handshake (1-RTT):
Client Server
────── ──────
│ │
│ ClientHello │
│ - Supported cipher suites │
│ - Key share (ECDHE public key) ──────────▶│
│ - Supported TLS versions │
│ │
│ │
│ ServerHello │
│ - Chosen cipher suite │
│ - Key share (ECDHE) │
│ ◀────────────────────── - Certificate │
│ - CertificateVerify │
│ - Finished │
│ │
│ ┌─────────────────────────────────────────┐ │
│ │ Client now has all it needs: │ │
│ │ 1. Server's ECDHE public key │ │
│ │ 2. Server's certificate (identity) │ │
│ │ 3. Server's signature (proof of identity│) │
│ │ 4. Derives shared symmetric keys │ │
│ └─────────────────────────────────────────┘ │
│ │
│ Finished │
│ (Client confirms handshake) ─────────▶│
│ │
│ ══════════ Encrypted application data ═══════════│
│ ◀════════════════════════════════════════════════▶│
│ │

TLS 1.3 vs TLS 1.2

FeatureTLS 1.2TLS 1.3
Handshake round trips2-RTT1-RTT (0-RTT for resumption)
Key exchangeRSA or ECDHEECDHE only (forward secrecy always)
Cipher suitesMany (including weak ones)Only 5 strong suites
CompressionSupported (CRIME attack vector)Removed
RenegotiationSupported (complex, attack-prone)Removed
0-RTT resumptionNoYes (with replay caveats)
Static RSASupported (no forward secrecy)Removed

TLS 1.3 Cipher Suites

TLS 1.3 supports only five cipher suites, all of which provide authenticated encryption:

Cipher SuiteKey ExchangeEncryptionHash
TLS_AES_256_GCM_SHA384ECDHEAES-256-GCMSHA-384
TLS_AES_128_GCM_SHA256ECDHEAES-128-GCMSHA-256
TLS_CHACHA20_POLY1305_SHA256ECDHEChaCha20-Poly1305SHA-256
TLS_AES_128_CCM_SHA256ECDHEAES-128-CCMSHA-256
TLS_AES_128_CCM_8_SHA256ECDHEAES-128-CCM-8SHA-256

X.509 Certificates

A certificate is a digitally signed document that binds a public key to an identity (a domain name, organization, or person). Certificates are the foundation of trust in TLS.

┌─────────────────────────────────────────────────────────┐
│ X.509 Certificate │
│ │
│ Subject: CN=www.example.com │
│ Issuer: CN=Let's Encrypt Authority X3 │
│ Valid From: 2024-01-01 00:00:00 UTC │
│ Valid To: 2024-03-31 23:59:59 UTC │
│ Public Key: EC (P-256) 04:a1:b2:c3:d4:... │
│ Serial Number: 03:f7:a1:... │
│ │
│ Extensions: │
│ Subject Alternative Names (SAN): │
│ DNS: www.example.com │
│ DNS: example.com │
│ DNS: api.example.com │
│ │
│ Signature Algorithm: ECDSA with SHA-256 │
│ Signature: 30:45:02:21:00:a1:... │
│ (signed by the issuer's private key) │
└─────────────────────────────────────────────────────────┘

Certificate Fields

FieldDescription
SubjectThe entity the certificate identifies (e.g., CN=example.com)
IssuerThe Certificate Authority that signed the certificate
Validity periodStart and end dates for the certificate
Public keyThe subject’s public key
Subject Alternative Names (SAN)Additional domain names covered by the certificate
Serial numberUnique identifier assigned by the CA
SignatureThe CA’s digital signature proving the certificate is authentic

Certificate Authorities and the Chain of Trust

A Certificate Authority (CA) is a trusted third party that verifies identities and issues certificates. Trust is established through a chain of trust from the server certificate up to a trusted root CA.

┌─────────────────────────────────────────────────────────┐
│ Chain of Trust │
│ │
│ ┌────────────────┐ │
│ │ Root CA │ Pre-installed in OS/browser │
│ │ (Self-signed) │ (e.g., DigiCert, ISRG Root X1) │
│ └───────┬────────┘ │
│ │ Signs │
│ ▼ │
│ ┌────────────────┐ │
│ │ Intermediate CA │ Signed by the Root CA │
│ │ │ (e.g., Let's Encrypt R3) │
│ └───────┬────────┘ │
│ │ Signs │
│ ▼ │
│ ┌────────────────┐ │
│ │ Server Cert │ Signed by the Intermediate CA │
│ │ (Leaf cert) │ (e.g., www.example.com) │
│ └────────────────┘ │
│ │
│ Verification: Browser walks the chain from leaf to root│
│ and verifies each signature. If the root is in the │
│ browser's trust store, the entire chain is trusted. │
└─────────────────────────────────────────────────────────┘

How Certificate Verification Works

  1. Server presents its certificate chain during the TLS handshake
  2. Client verifies the leaf certificate’s signature using the intermediate CA’s public key
  3. Client verifies the intermediate CA’s signature using the root CA’s public key
  4. Client checks that the root CA is in its trust store (pre-installed list of trusted roots)
  5. Client checks that the certificate is not expired and has not been revoked
  6. Client checks that the domain name matches the certificate’s Subject or SAN

Certificate Types

TypeValidationWhat CA VerifiesUse Case
DV (Domain Validation)Domain ownership onlyAutomated DNS/HTTP challengeBlogs, personal sites, APIs
OV (Organization Validation)Domain + organizationLegal entity verificationBusiness websites
EV (Extended Validation)Domain + org + thorough vettingPhysical address, legal standingBanks, e-commerce (less common now)
WildcardDomain + all subdomainsSame as DV/OV*.example.com

Let’s Encrypt

Let’s Encrypt is a free, automated, and open Certificate Authority that has revolutionized HTTPS adoption. Before Let’s Encrypt (launched 2015), obtaining a certificate required manual processes and annual fees. Now, certificates can be issued and renewed automatically in seconds.

How ACME Protocol Works

The ACME (Automatic Certificate Management Environment) protocol automates certificate issuance:

┌──────────┐ ┌───────────────┐
│ Client │ │ Let's Encrypt │
│ (certbot) │ │ ACME Server │
└────┬─────┘ └───────┬───────┘
│ │
│ 1. Request certificate │
│ for example.com ─────────▶│
│ │
│ 2. Challenge: prove you │
│ control example.com ◀────────│
│ │
│ 3. Place challenge token at │
│ http://example.com/ │
│ .well-known/acme-challenge/ │
│ (or DNS TXT record) │
│ ─────────▶│
│ │
│ 4. CA verifies the challenge │
│ │
│ 5. Certificate issued ◀────────│
│ │

Setting Up Let’s Encrypt

Terminal window
# Install certbot
sudo apt-get install certbot python3-certbot-nginx
# Obtain and install certificate (Nginx)
sudo certbot --nginx -d example.com -d www.example.com
# Obtain certificate only (manual)
sudo certbot certonly --standalone -d example.com
# Auto-renewal (certbot sets up a systemd timer)
sudo certbot renew --dry-run
# Certificate files are stored at:
# /etc/letsencrypt/live/example.com/fullchain.pem (cert + chain)
# /etc/letsencrypt/live/example.com/privkey.pem (private key)

mTLS (Mutual TLS)

Standard TLS authenticates only the server — the client verifies the server’s certificate, but the server does not verify the client. mTLS (mutual TLS) adds client certificate authentication, where both sides present and verify certificates.

Standard TLS:
Client ──────────── verifies server cert ──────────▶ Server
(anonymous) (authenticated)
mTLS (Mutual TLS):
Client ◀─────────── verifies client cert ────────── Server
(authenticated) verifies server cert (authenticated)
Both sides present certificates. Both sides are verified.

When to Use mTLS

Use CaseWhy mTLS
Service-to-service communicationAuthenticate microservices without API keys
Zero-trust networksEvery connection is authenticated, even internal ones
IoT device authenticationDevices present client certs instead of passwords
API access for partnersMore secure than API keys for B2B integrations
Kubernetes pod communicationService mesh (Istio, Linkerd) uses mTLS by default

mTLS Configuration

import requests
# mTLS: client presents its own certificate to the server
response = requests.get(
"https://api.internal.example.com/data",
cert=(
"/path/to/client.crt", # Client certificate
"/path/to/client.key", # Client private key
),
verify="/path/to/ca-bundle.crt", # CA to verify server cert
timeout=30,
)
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")

Certificate Pinning

Certificate pinning restricts which certificates a client will accept for a particular server, even if the certificate is technically valid and signed by a trusted CA. This protects against compromised CAs and man-in-the-middle attacks using fraudulent certificates.

Without pinning:
Any certificate signed by ANY trusted CA is accepted.
If attacker compromises ANY CA, they can forge a certificate.
With pinning:
Client only accepts certificates matching a specific pin.
Even a valid certificate from a different CA is rejected.
┌────────┐ Connects to server ┌────────┐
│ Client │─────────────────────────▶│ Server │
│ │ │ │
│ Pinned:│ Cert presented: │ │
│ Pin=X │ Pin=X → ACCEPT │ Cert X │
│ │ Pin=Y → REJECT │ │
└────────┘ (even if Y is valid) └────────┘

Pinning Strategies

StrategyWhat Is PinnedProsCons
Pin the leaf certThe exact server certificateMost restrictiveMust update pin on every cert rotation
Pin the intermediate CAThe issuing CA’s certificateSurvives cert rotationTied to one CA
Pin the public keyThe server’s public key (SPKI hash)Survives cert reissuance if key is reusedKey rotation requires pin update

HPKP vs Modern Alternatives

MechanismStatusUse Case
HPKPDeprecated (removed from browsers)N/A
Certificate TransparencyActivePublicly log all issued certificates for audit
CAA DNS recordsActiveRestrict which CAs can issue certificates for your domain
App-level pinningActiveMobile apps pin server certificates in code
DANE/TLSALimited adoptionDNS-based certificate association

CAA DNS Record Example

; Only Let's Encrypt and DigiCert can issue certs for example.com
example.com. CAA 0 issue "letsencrypt.org"
example.com. CAA 0 issue "digicert.com"
example.com. CAA 0 iodef "mailto:security@example.com"

Certificate Revocation

When a private key is compromised or a certificate needs to be invalidated before its expiry date, it must be revoked. There are two main mechanisms:

MechanismHow It WorksProsCons
CRL (Certificate Revocation List)CA publishes a list of revoked serial numbersSimpleLists can grow very large; clients must download periodically
OCSP (Online Certificate Status Protocol)Client queries CA in real time for certificate statusCurrent statusExtra latency; privacy concern (CA sees which sites you visit)
OCSP StaplingServer periodically fetches its own OCSP response and attaches it to the TLS handshakeNo extra latency; no privacy leakServer must be configured correctly

TLS Configuration Best Practices

PracticeRecommendation
Minimum TLS versionTLS 1.2 (prefer TLS 1.3)
DisableSSLv2, SSLv3, TLS 1.0, TLS 1.1
Forward secrecyUse ECDHE key exchange exclusively
Certificate typeECDSA P-256 (faster) or RSA-2048+
HSTSEnable with max-age=63072000 (2 years)
OCSP staplingEnable for faster certificate verification
CAA recordsRestrict which CAs can issue for your domain
Certificate TransparencyEnsure your CA logs all certificates
Automated renewalUse certbot or ACME client to prevent expiration
Test your configurationUse SSL Labs (ssllabs.com) to grade your setup

SSL Labs Grading

GradeMeaning
A+Excellent — TLS 1.2+, strong ciphers, HSTS, no vulnerabilities
AGood — strong configuration with minor improvements possible
BAcceptable — some weak ciphers or missing HSTS
C-FPoor — weak protocols, vulnerable to known attacks

Inspecting Certificates

Terminal window
# View a server's certificate
openssl s_client -connect example.com:443 -servername example.com \
< /dev/null 2>/dev/null | openssl x509 -noout -text
# Check certificate expiry date
openssl s_client -connect example.com:443 -servername example.com \
< /dev/null 2>/dev/null | openssl x509 -noout -dates
# View the full certificate chain
openssl s_client -connect example.com:443 -servername example.com \
-showcerts < /dev/null
# Verify a certificate against a CA bundle
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt \
server.crt
# Generate a self-signed certificate (for development only)
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
-keyout key.pem -out cert.pem -days 365 -nodes \
-subj "/CN=localhost"

Quick Reference

ConceptDescription
TLSProtocol for encrypted, authenticated communication
TLS 1.3Latest version — 1-RTT handshake, mandatory forward secrecy
X.509Standard format for digital certificates
CATrusted entity that issues and signs certificates
Chain of trustLeaf cert signed by intermediate, signed by root
ACMEProtocol for automated certificate issuance (Let’s Encrypt)
mTLSBoth client and server present certificates
HSTSHTTP header forcing browsers to use HTTPS
OCSP staplingServer provides certificate revocation status in handshake
CAADNS record restricting which CAs can issue for a domain
Certificate pinningClient restricts accepted certificates beyond CA trust
Forward secrecyEphemeral keys ensure past sessions stay secure if long-term key leaks

Next Steps