LESSON 31 · INTERMEDIATE8 min read

TLS, Certificates, and Public Key Infrastructure

Understand how TLS combines symmetric and asymmetric cryptography, how certificates and CAs establish trust, and how to diagnose real handshake failures with OpenSSL.

OWASP Top 10 · MITRE ATT&CK · NIST CSF 2.0Free to readModule 7 of 14
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Explain symmetric and asymmetric cryptography and how TLS combines them.
  • Read an X.509 certificate, including SANs, validity, key usage, and the certificate chain.
  • Create a lab CA, sign a server certificate, and verify it with OpenSSL.
  • Diagnose common TLS failures and describe pinning, mTLS, revocation, and ACME automation.

01Two Kinds of Keys, One Handshake

TLS protects data in transit, but it is easier to understand as three separate jobs: key exchange, authentication, and bulk encryption. Symmetric encryption uses one shared key and is fast. Asymmetric cryptography uses a key pair and solves the problem of agreeing on a key with someone you have never met. TLS combines them. The handshake authenticates the server and derives a shared session key; the session then uses fast authenticated encryption.

RSA and elliptic-curve cryptography are the common asymmetric families. RSA is older and widely supported; ECDSA and Ed25519 are smaller and faster for equivalent security. Modern TLS 1.3 removed static RSA key exchange and several legacy options. It uses ephemeral Diffie-Hellman key agreement, which gives forward secrecy: recording today's traffic does not let an attacker decrypt it later if the long-term key is stolen.

Symmetric algorithms such as AES-GCM and ChaCha20-Poly1305 provide confidentiality and integrity together. A cipher suite names the combination of key exchange, authentication, and record protection. You rarely choose one by hand today. Configure a modern TLS policy, let the library negotiate, and verify the result with a client. Understanding the pieces helps you diagnose failures and read a packet capture.

02What Is Inside a Certificate?

An X.509 certificate binds a public key to an identity. It contains a subject, an issuer, a validity period, a serial number, the public key, and extensions. The subject identifies the entity; the issuer identifies the CA that signed it. The certificate is not secret. The private key is, and losing it is a compromise, not a support ticket.

The Subject Alternative Name extension, or SAN, lists the DNS names and IP addresses for which the certificate is valid. Modern clients ignore the Common Name for hostname matching, so a certificate with only CN=app.example.com may fail for that name. Include every name users actually type, and avoid wildcards that are broader than necessary. A wildcard covers one label, not every subdomain.

Validity is a window, not a suggestion. Clients reject expired certificates and certificates that are not yet valid, which often means a clock or time-zone problem. Key Usage and Extended Key Usage restrict what the key may do, such as digital signature, server authentication, or client authentication. Basic Constraints marks whether the certificate is a CA. Read the extensions, not just the subject.

03CSRs, CAs, and the Chain of Trust

A certificate signing request, or CSR, contains the public key and the requested subject and SANs. The requester signs the CSR with the matching private key, proving possession. The CA validates the request, applies policy, and issues a certificate. The private key never goes to the CA in a properly designed flow.

A public CA is trusted because its root certificate is in the operating system, browser, or runtime trust store. The root signs an intermediate, and the intermediate signs the server certificate. The client builds a chain from the server certificate to a trusted root and verifies signatures, names, validity, and constraints at every step. An incomplete chain is a common outage: the server must send the intermediate certificates, not just the leaf.

Private PKI is useful for internal services, device identity, and mTLS. You run your own root and intermediate, distribute the root to clients deliberately, and document the trust decision. Protect the CA private key more carefully than any server key. If the root is compromised, every certificate it signed is suspect. Keep the root offline or in a managed service, and issue from an intermediate.

04The TLS Handshake in Plain Language

The TLS handshake starts after TCP. The client sends a ClientHello with supported versions, cipher suites, a random value, SNI, ALPN, and key shares. The server selects parameters, sends its own key share and certificate, and proves possession of the private key. Both sides derive the same session keys. The client verifies the certificate chain and hostname before sending application data.

In TLS 1.3 the handshake takes one round trip in the common case. Session resumption can reduce it further, and 0-RTT can send early data, but early data has replay risk and should be limited to idempotent operations. TLS 1.3 encrypts more of the handshake, including the certificate, which improves privacy but makes some middlebox troubleshooting harder. Use a packet capture and key logging only in a lab you control.

SNI lets one IP address host many certificates. The client sends the requested hostname in the clear in TLS 1.2, and in TLS 1.3 it is protected once a shared key exists but still visible in some deployments. If a client connects by IP without SNI, the server may return the wrong certificate. ALPN negotiates the application protocol, such as HTTP/2. These details explain many 'it works in the browser but not in curl' failures.

05OpenSSL in the Lab

OpenSSL is the Swiss army knife of TLS troubleshooting. Use req to create keys and CSRs, x509 to inspect and sign, verify to check a chain, and s_client to make a TLS connection and print the handshake. The commands below are for a lab. Do not paste production private keys into a shared terminal, a ticket, or a chat window.

Generate the CA key and certificate once, then create a server key and CSR. Sign the CSR with the CA and a SAN configuration. Verify the chain with the CA file, inspect the extensions, and connect with s_client using the correct server name. The -servername flag matters when one IP hosts multiple sites. A successful verify return code of 0 means the chain and trust anchor checked out.

Store private keys with restrictive permissions, preferably 0600 and owned by the service account. Better still, keep them in a hardware module, operating-system keystore, or cloud key service and never write them to disk in plaintext. Rotate keys on a schedule and after any suspected exposure. Backups of keys need the same protection as the keys themselves.

san.cnfPlain text
[req]distinguished_name = dnreq_extensions = v3_reqprompt = no[dn]CN = app.lab.local[v3_req]subjectAltName = @alt_nameskeyUsage = critical, digitalSignature, keyEnciphermentextendedKeyUsage = serverAuthbasicConstraints = critical, CA:FALSE[alt_names]DNS.1 = app.lab.localDNS.2 = api.lab.localIP.1 = 10.10.10.20

06Pinning, mTLS, Revocation, and Common Failures

Certificate pinning hardens a client against a compromised or mis-issued CA by requiring a specific certificate or public key. Pin the SPKI hash rather than the whole certificate so a renewal with the same key does not break clients, and always ship a backup pin. Pinning is powerful and operationally expensive: a lost key or a rushed rotation can lock every client out. Mobile apps and high-value service-to-service links use it; general browsers rely on the CA system and expect rotation.

Mutual TLS authenticates both ends. The server requests a client certificate, verifies it against a trusted CA, and maps the identity to an authorization decision. mTLS is common for service meshes, APIs, and device fleets. It is not a substitute for authorization: a valid certificate proves an identity, not permission to call every endpoint. Protect client keys, prefer short-lived certificates, and plan revocation.

Revocation is the hard part of PKI. A CA can publish a certificate revocation list, or clients can check OCSP. Both can fail open if the client cannot reach the responder, and privacy concerns led to OCSP stapling and shorter certificate lifetimes. TLS 1.3 and modern practice increasingly rely on short-lived certificates. Let's Encrypt and the ACME protocol automate issuance with HTTP-01, DNS-01, or TLS-ALPN-01 challenges. Automation is only safe when key storage and renewal monitoring are part of the design.

Common failures have recognizable signatures. A hostname mismatch means the SAN does not include the name the client used. An expired certificate means the validity window closed. An incomplete chain means the server did not send an intermediate. An unknown CA means the client does not trust the root. A protocol or cipher mismatch means the two sides share no common policy. Clock skew can make a valid certificate look expired. Diagnose with s_client, openssl verify, and the server's certificate chain before changing anything.

tls-lab.shShell
# Create a lab root CA. Use -nodes instead of -noenc on older OpenSSL.openssl req -x509 -newkey rsa:3072 -sha256 -days 3650 -noenc \  -keyout ca.key -out ca.crt -subj "/CN=Lab Root CA"# Create a server key and CSR using the SAN configuration.openssl req -new -newkey rsa:3072 -sha256 -noenc \  -keyout server.key -out server.csr -config san.cnf# Sign the CSR as an intermediate would in a real PKI.openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key \  -CAcreateserial -out server.crt -days 397 -sha256 \  -extfile san.cnf -extensions v3_req# Inspect the certificate and verify the chain.openssl x509 -in server.crt -noout -subject -issuer -dates -ext subjectAltNameopenssl verify -CAfile ca.crt server.crt# Connect with SNI and print the negotiated protocol and chain.openssl s_client -connect 10.10.10.20:443 -servername app.lab.local \  -CAfile ca.crt -showcerts </dev/null# Test TLS 1.3 against a public site and inspect its certificate.openssl s_client -connect example.com:443 -servername example.com -tls1_3 </dev/null \  | grep -E "Protocol|Cipher|Verify return code"# Compute a public-key pin (SPKI SHA-256, base64) for an app client.openssl x509 -in server.crt -pubkey -noout \  | openssl pkey -pubin -outform der \  | openssl dgst -sha256 -binary | openssl base64# Private keys should not be readable by other users.chmod 600 ca.key server.keystat -c "%a %U:%G %n" ca.key server.key
Outputstdout
subject=CN = app.lab.local
issuer=CN = Lab Root CA
notBefore=Nov  1 10:00:00 2026 GMT
notAfter=Dec  3 10:00:00 2027 GMT
X509v3 Subject Alternative Name:
    DNS:app.lab.local, DNS:api.lab.local, IP Address:10.10.10.20
server.crt: OK
Protocol: TLSv1.3
Cipher: TLS_AES_256_GCM_SHA384
Verify return code: 0 (ok)
600 root:root ca.key
600 root:root server.key
LET IT CLICK

The ideas worth keeping.

  • TLS combines asymmetric authentication and key agreement with fast symmetric authenticated encryption.
  • A certificate is trusted only when the chain, hostname SAN, validity, extensions, and intended use all check out.
  • OpenSSL req, x509, verify, and s_client are the core tools for building and diagnosing PKI.
  • Pinning, mTLS, revocation, and ACME automation are operational commitments, not just configuration switches.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Build a lab CA and server certificate

Create a private root CA and a server certificate for app.lab.local and 10.10.10.20. Sign it with a SAN, verify the chain, inspect the key usage, and connect with s_client. List the checks a client performs before trusting the connection.

Explore a worked solution
one_possible_solution.mdCybersecurity
Generate ca.key and ca.crt with openssl req -x509. Create server.key and server.csr with a configuration that includes subjectAltName DNS:app.lab.local, IP:10.10.10.20, keyUsage digitalSignature and keyEncipherment, and extendedKeyUsage serverAuth. Sign with openssl x509 -req using ca.crt, ca.key, -CAcreateserial, -extfile san.cnf, and -extensions v3_req. Run openssl verify -CAfile ca.crt server.crt and inspect with openssl x509 -text. Start a local TLS listener or use a lab web server, then connect with openssl s_client -connect 10.10.10.20:443 -servername app.lab.local -CAfile ca.crt. The client checks the chain signatures, the trust anchor, the validity dates, the SAN for the requested name, the key usage and extended key usage, and the handshake signature. It also checks revocation according to policy and may enforce certificate transparency or pinning.

A certificate is only trusted when the chain, name, validity, extensions, and intended use all line up. Verification is a set of checks, not a single file comparison.

02

Exercise 2: Diagnose three TLS failures

A client reports three failures: (A) 'certificate has expired', (B) 'hostname mismatch' for api.example.com, and (C) 'unable to get local issuer certificate' only on some networks. Describe how you would confirm each cause and the correct fix.

Explore a worked solution
one_possible_solution.mdCybersecurity
A: inspect notBefore and notAfter with openssl x509 -noout -dates, check the client clock, and compare with the server certificate actually served using s_client. Fix by renewing and deploying the certificate, not by disabling verification. B: inspect the SAN with openssl x509 -noout -ext subjectAltName; confirm the exact name the client requests and whether SNI is sent. Add the missing name to a new certificate or correct the client URL. C: use s_client -showcerts to see whether the server sends the intermediate. If it sends only the leaf, install the intermediate chain on the server. If the client trust store is missing the root, distribute the root through managed configuration. The 'only some networks' detail suggests different trust stores or a TLS-inspecting proxy, so compare the chain from each network.

Each message points to a different layer: time, name, or chain. Confirm with the certificate actually served and avoid the tempting but unsafe fix of disabling verification.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01What is the Subject Alternative Name extension used for?
02What does signing a CSR prove?
03Why is certificate pinning operationally risky?

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Completion is saved in this browser. It records reading and hands-on practice progress.