Certificates and PKI
A certificate is a server's digital ID card: it binds a public key to a domain name and is signed by a trusted Certificate Authority. PKI (Public Key Infrastructure) is the whole system of Root CAs, Intermediates, leaf certificates, revocation and trust stores that lets a browser know it is talking to the real shop.lab.
133.1 Why is it used?
- Encryption alone is not enough: an attacker on the same cafe Wi-Fi can claim to be shop.lab and encrypt with their own key, so your password goes to them.
- The certificate proves the server's identity: it was issued by a CA the browser already trusts.
- A certificate carries only the public key, never the private key.
- mTLS extends this so the server can also verify the client (service mesh, zero trust).
133.2 How it works: the CA chain
- Root CA: self-signed, private key kept offline, signs only Intermediate CAs; it lives in the browser/OS trust store.
- Intermediate (Issuing) CA: signs server (leaf) certificates like shop.lab.
- In the handshake the server sends its leaf + the Intermediate; the Root is already on the client.
- The browser checks each signature with the issuer's public key until it reaches a trusted Root, then checks dates, hostname and revocation.
- Step 1
1.Server sends Certificate (CN=shop.lab) + Intermediate (Issuing CA 1)
- Step 2
2.shop.lab <- Issuing CA 1: signature OK
- Step 3
3.Issuing CA 1 <- Root CA: signature OK
- Step 4
4.Root CA found in the trust store: chain complete
- Step 5
5.Check Not Before/Not After, hostname in SAN, revoked? (CRL/OCSP)
- Step 6
6.All pass -> lock icon; any fail -> red warning page
133.3 Important certificate fields
| Field | Meaning | shop.lab example |
|---|---|---|
| Subject | Whose certificate it is | CN=shop.lab |
| Issuer | Who signed it | Shop Lab Issuing CA 1 |
| Validity | Not Before / Not After | Nov 9 2026 - Feb 7 2027 (90 days) |
| SAN | All valid names; browsers check SAN, not CN | DNS:shop.lab, DNS:www.shop.lab |
| Key Usage / EKU | What the key is for | Digital Signature, TLS Web Server Authentication |
| Basic Constraints | Is it a CA? | CA:FALSE |
| Authority Information Access | Where to ask OCSP | OCSP - URI:http://192.168.153.20:8888 |
133.4 Building a lab PKI with openssl
# 1. Root CA: self-signed, 10 yearsopenssl req -x509 $EC -days 3650 \ -keyout root.key -out root.crt \ -subj "/C=IN/O=Shop Lab/CN=Shop Lab Root CA"# 2. Intermediate CA, signed by root, 5 yearsopenssl x509 -req -in inter.csr \ -CA root.crt -CAkey root.key -CAcreateserial \ -days 1825 -extfile inter.ext \ -out inter.crt 2>/dev/null# 3. shop.lab server cert (openssl ca), 90 daysopenssl ca -batch -config ca.cnf \ -extensions server_ext $D -notext \ -in $1.csr -out $1.crt 2>/dev/null# verifyopenssl verify -show_chain -CAfile root.crt \ -untrusted inter.crt shop.crtshop.crt: OKChain:depth=0: CN=shop.lab (untrusted)depth=1: C=IN, O=Shop Lab, CN=Shop Lab Issuing CA 1 (untrusted)depth=2: C=IN, O=Shop Lab, CN=Shop Lab Root CAopenssl x509 -in shop.crt -noout -text \ -certopt no_pubkey,no_sigdumpSerial Number: 4096 (0x1000)Issuer: C=IN, O=Shop Lab, CN=Shop Lab Issuing CA 1Validity Not Before: Nov 9 03:48:54 2026 GMT Not After : Feb 7 03:48:54 2027 GMTSubject: CN=shop.labX509v3 extensions: X509v3 Basic Constraints: critical CA:FALSE X509v3 Key Usage: critical Digital Signature X509v3 Extended Key Usage: TLS Web Server Authentication X509v3 Subject Alternative Name: DNS:shop.lab, DNS:www.shop.lab Authority Information Access: OCSP - URI:http://192.168.153.20:8888133.5 Seeing the chain with openssl s_client
openssl s_client -connect shop.lab:443 \ -servername shop.lab -CAfile root.crt \ -showcertsCONNECTED(00000003)depth=2 C=IN, O=Shop Lab, CN=Shop Lab Root CAverify return:1depth=1 C=IN, O=Shop Lab, CN=Shop Lab Issuing CA 1verify return:1depth=0 CN=shop.labverify return:1Certificate chain 0 s:CN=shop.lab i:C=IN, O=Shop Lab, CN=Shop Lab Issuing CA 1 1 s:C=IN, O=Shop Lab, CN=Shop Lab Issuing CA 1 i:C=IN, O=Shop Lab, CN=Shop Lab Root CANew, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384Verify return code: 0 (ok)- -servername sets SNI; -CAfile root.crt trusts only the lab Root.
- s: = subject, i: = issuer; each cert's issuer should be the next cert's subject.
133.6 When verification fails
| Error | Cause | Lab port |
|---|---|---|
| 18 self-signed certificate | Subject = issuer = shop.lab, no trusted CA signed it | 8441 |
| 20 unable to get local issuer certificate | Client does not have the lab Root: chain reaches no trusted Root | 443 without -CAfile |
| 21 unable to verify the first certificate | Server sends only the leaf, not the Intermediate | 8442 |
| 10 certificate has expired | notAfter=Jan 1 00:00:00 2025 GMT has passed | 8443 |
| 62 hostname mismatch | Connecting to wrong.lab; SAN has only shop.lab, www.shop.lab | 443 as wrong.lab |
| 23 certificate revoked | Serial 1001 is in the CRL (Key Compromise) | 8444 |
openssl s_client -connect shop.lab:8441 \ -servername shop.lab -CAfile root.crtopenssl s_client -connect shop.lab:8443 \ -servername shop.lab -CAfile root.crtopenssl s_client -connect wrong.lab:443 \ -servername wrong.lab -CAfile root.crt \ -verify_hostname wrong.labdepth=0 CN=shop.labverify error:num=18:self-signed certificate 0 s:CN=shop.lab i:CN=shop.labVerify return code: 18 (self-signed certificate) depth=0 CN=shop.labverify error:num=10:certificate has expirednotAfter=Jan 1 00:00:00 2025 GMTVerify return code: 10 (certificate has expired) depth=0 CN=shop.labverify error:num=62:hostname mismatchVerify return code: 62 (hostname mismatch)133.7 Revocation: CRL vs OCSP
- Step 1
1.shop.lab private key leaks while the cert (serial 1001) is still valid
- Step 2
2.Issuing CA 1 revokes it and publishes it in the CRL
- Step 3
3.CRL way: client downloads the whole list and finds 1001 revoked
- Step 4
4.OCSP way: client asks the responder 192.168.153.20:8888 about serial 1001 only
- Step 5
5.Answer: good / revoked / unknown -> revoked: keyCompromise
| Point | CRL | OCSP |
|---|---|---|
| What the client gets | Whole list of revoked serials | Status of one serial |
| Freshness | Until Next Update (7 days in lab) | Live answer from the responder |
| Answer | Listed or not | good, revoked or unknown |
| openssl | -crl_check -CRL issuing.crl | openssl ocsp -issuer -cert -url |
openssl crl -in issuing.crl -noout -textopenssl s_client -connect shop.lab:8444 \ -servername shop.lab -CAfile root.crt \ -crl_check -CRL issuing.crlopenssl x509 -in shop.crt -noout -ocsp_uriopenssl ocsp -issuer inter.crt -cert old.crt \ -url http://192.168.153.20:8888 -CAfile root.crtRevoked Certificates: Serial Number: 1001 Revocation Date: Nov 9 03:48:55 2026 GMT X509v3 CRL Reason Code: Key Compromiseverify error:num=23:certificate revokedVerify return code: 23 (certificate revoked) http://192.168.153.20:8888Response verify OKshop.crt: goodResponse verify OKold.crt: revoked Reason: keyCompromise Revocation Time: Nov 9 03:48:55 2026 GMT133.8 mTLS: certificates on both sides
- Step 1
1.billing-service 192.168.153.10 connects to shop.lab API 192.168.153.20:8445
- Step 2
2.Server sends its cert (shop.lab) and a CertificateRequest
- Step 3
3.Client sends its cert CN=billing-service, signed by the lab CA
- Step 4
4.Both verify the other's chain against the same Root
- Step 5
5.No client cert -> alert certificate required, connection refused
openssl s_client -connect shop.lab:8445 \ -servername shop.lab -CAfile root.crt \ -cert client.crt -key client.key \ -cert_chain inter.crt# without a client cert:ssl3_read_bytes:tlsv13 alert certificate required# server log: peer did not return a certificate # with the cert (server log):depth=0 CN=billing-serviceverify return:1Client certificatesubject=CN=billing-serviceissuer=C=IN, O=Shop Lab, CN=Shop Lab Issuing CA 1CIPHER is TLS_AES_256_GCM_SHA384133.9 Common mistakes
- Server not sending the Intermediate: browsers cache/download it so the site looks fine, but openssl, curl and apps fail with error 21.
- Lock icon = honest site: it only proves encryption and domain control; phishing sites get valid certs for free.
- Thinking self-signed is always wrong: every Root CA is self-signed; the issue is only whether it is trusted.
- Putting names only in CN: browsers check the SAN.
- Forgetting to renew: 90-day certs expire fast (error 10).
133.10 Interview tips
- Chain verification: verify each signature with the issuer's public key up to a Root in the trust store; check dates and Basic Constraints; hostname must match SAN; check revocation via CRL/OCSP.
- Root keys stay offline; Intermediates do the daily signing, so a compromised Intermediate can be revoked without replacing the Root.
- Know the openssl error numbers: 18 self-signed, 20 unknown issuer, 21 missing Intermediate, 10 expired, 62 hostname, 23 revoked.
- mTLS = server also sends CertificateRequest and verifies the client cert: used in service meshes and zero trust.