Certificates and PKI
About this lesson
A public key alone does not say whose it is. A certificate binds a name to a public key and is signed by a certificate authority (CA), whose own certificate is signed by a root CA your computer already trusts. Follow example.com's certificate from its signing request to a six-line Python client that checks the chain, the name, the dates and revocation, and see why switching those checks off is the classic mistake.
Whose key is this?
Your computer wants a private connection to example.com. The server sends a public key, but a key alone carries no name. An impostor could send their own key instead. Your computer needs proof of whose key it is.
public key lets anyone lock data for, or check
signatures from, the key's owner
problem the key does not say who owns it
needed a trusted statement: this key
belongs to example.comRead the lesson step by step
Whose key is this?
Your computer wants a private connection to example.com. The server sends a public key, but a key alone carries no name. An impostor could send their own key instead. Your computer needs proof of whose key it is.
public key lets anyone lock data for, or check signatures from, the key's owner problem the key does not say who owns it needed a trusted statement: this key belongs to example.comA certificate binds name and key
A certificate is a signed statement that ties a name to a public key. The X.509 kind used on the web lists the subject name, its public key, who issued it, valid-from and valid-to dates, and the issuer's digital signature.
X.509 certificate (RFC 5280) subject example.com subjectAltName DNS:example.com, DNS:www.example.com public key the server's public key issuer Intermediate CA validity notBefore .. notAfter signature made with the issuer's private keyA CA signs it
The server makes a key pair and keeps the private key secret. It sends a certificate signing request (CSR) with its name and public key to a certificate authority (CA). The CA checks the server controls the domain, then signs.
1 server makes a key pair; the private key never leaves 2 server → CA CSR: name + public key 3 CA checks control of the domain 4 CA signs with ITS private key 5 CA → server the certificateThe chain of trust
Who vouches for the CA? A root CA signs the intermediate CA's certificate. The root's private key stays offline and is used rarely; the intermediate does the daily signing. Leaf, intermediate, root: a chain of trust.
root CA signs the intermediate's certificate private key offline, used rarely intermediate CA signs servers' certificates leaf example.com PKI the CAs, rules and systems that issue and manage certificatesYour trust store
Your computer already holds a trust store: root certificates it trusts, shipped with the operating system or browser. These are trust anchors. Line 3, ssl.create_default_context(), loads them and turns on certificate and hostname checks.
trust anchor a root certificate trusted in advance trust store the list of them on your computer line 3 ssl.create_default_context() verify_mode = CERT_REQUIRED check_hostname = True CA certs = the system defaultsThe server shows its chain
Lines 4 and 5 connect and start TLS. In the handshake the server sends its certificate plus the intermediate's. It need not send the root: your computer must already trust that. The checks run before any data flows.
line 4 TCP connection to port 443 line 5 TLS handshake, server_hostname=host server → your computer 1 leaf example.com 2 intermediate Intermediate CA (the root comes from your own trust store)Check every signature
Check the chain. The leaf's signature must verify with the intermediate's public key, and the intermediate's with the root's. That root must be in your trust store. Anyone can make a certificate; only a trusted CA's signature counts.
leaf signed by Intermediate CA? ✓ intermediate signed by Root CA? ✓ Root CA in my trust store? ✓ CHAIN OKCheck the name and dates
server_hostname must match a name in the certificate's subject alternative name list: example.com does. Today must fall between its valid-from and valid-to dates. An expired certificate fails, even when every signature is good.
asked for example.com certificate DNS:example.com, DNS:www.example.com NAME OK notBefore ≤ now ≤ notAfter DATES OKNot revoked
A CA can revoke a certificate early, for example if its private key is stolen. Clients learn this from a revocation list (CRL) or an OCSP responder. Short lifetimes also limit harm: the maximum is falling from 398 to 47 days by 2029.
revoked cancelled by the CA before notAfter CRL a signed list of revoked serial numbers OCSP ask the CA: is serial N still good? REVOKED? NO note Python's default context does not check revocation; browsers do, in their own waysTrusted, then encrypted
Every check passes, so the connection is trusted and encrypted. Line 6 prints the certificate's valid-to date. If any check fails, line 5 raises ssl.SSLCertVerificationError and no data is sent. That error is protection, not a nuisance.
all checks pass → encrypted connection line 6 s.getpeercert()["notAfter"] e.g. "Dec 25 22:56:35 2026 GMT" any check fails → ssl.SSLCertVerificationError raised in line 5The classic mistake
The classic mistake is silencing that error with check_hostname = False and verify_mode = ssl.CERT_NONE. Then any certificate, even an impostor's, is accepted. The fix: keep the checks and solve the real cause, like an expired certificate.
mistake (CWE-295, improper certificate validation) ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE → every check OFF; impostors accepted fix: keep the defaults and fix the cause expired renew it (automate renewal) missing chain serve the intermediate too private CA add its root: load_verify_locations()
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What does a certificate authority (CA) sign when it issues a website's certificate?
The certificate ties a subject name to a public key, with the issuer and valid-from / valid-to dates, and the CA signs all of it. The private key never leaves the server.
02In the TLS handshake, why does the server usually not need to send the root CA's certificate?
Trust has to start somewhere you already trust. A root sent by the server would prove nothing; it counts only because it is already in your trust store.
03A certificate's signatures all verify up to a trusted root, but its valid-to date was last week. What should the client do?
Every check must pass. The date check fails, so Python's wrap_socket raises ssl.SSLCertVerificationError. The fix is for the site to renew its certificate.
04A script fails with ssl.SSLCertVerificationError against an internal server that uses the company's own CA. Which fix is right?
Turning checks off accepts any certificate, including an impostor's (CWE-295). Trusting the right root keeps every check on and makes the real certificate verify.
05Why can a CA revoke a certificate before its valid-to date?
Revocation cancels a certificate early. Clients can learn about it from a CRL or an OCSP responder; short certificate lifetimes limit the damage too.
Glossary
- Public key infrastructure (PKI)
- The certificate authorities, rules and systems that issue, manage and revoke certificates, so that people can trust which public key belongs to whom.
- Certificate (X.509)
- A signed statement binding a name (the subject) to a public key, with the issuer's name, valid-from and valid-to dates, and the issuer's digital signature.
- Certificate authority (CA)
- An organisation that checks who controls a name and signs certificates. Root CAs sign intermediate CAs, which sign servers' certificates.
- Certificate signing request (CSR)
- What a server sends a CA to ask for a certificate: its name and public key, signed with its own private key. The private key itself is never sent.
- Chain of trust
- The line of signatures from a server's certificate, through one or more intermediate CAs, up to a root certificate the client already trusts.
- Trust anchor and trust store
- A trust anchor is a root certificate trusted in advance. The trust store is the list of them on a computer, shipped with the operating system or browser.
- Subject alternative name (SAN)
- The certificate field that lists the host names it is valid for. The name you connect to must match one of them.
- Revocation (CRL, OCSP)
- Cancelling a certificate before it expires. A CRL is a signed list of revoked serial numbers; OCSP lets a client ask about one certificate.
Notes and sources
- The CA names in the figure are generic. Run the code and print s.getpeercert()["issuer"] to see who really issued example.com's certificate; on 2026-11-06 it was a Cloudflare issuing CA and the certificate ran until 25 December 2026.
- Real chains are sometimes longer (several intermediates) or cross-signed, so a client may find more than one path to a trusted root. The idea is the same: each signature is checked by the next certificate up, ending at a trust anchor.
- Domain validation proves control of the domain name only. It says nothing about who runs the website or whether it is honest.
- Python's ssl module does not check CRLs or OCSP unless you configure it; browsers use their own revocation methods. Shorter certificate lifetimes (CA/Browser Forum ballot SC-081) limit how long a stolen key stays useful.
- CISA requires US federal websites to use HTTPS with HSTS, which also removes the option for users to click through certificate warnings.
- This course is about defence. It explains weaknesses at the level of the mistake and the fix, never how to attack a real system.
- NIST SP 800-32: Introduction to Public Key Technology and the Federal PKI Infrastructure
- NIST SP 800-52 Rev. 2: Guidelines for the Selection, Configuration, and Use of TLS Implementations
- NIST SP 1800-16: Securing Web Transactions: TLS Server Certificate Management
- NIST CSRC Glossary: trust anchor
- RFC 5280: Internet X.509 Public Key Infrastructure Certificate and CRL Profile
- RFC 6960: X.509 Internet PKI Online Certificate Status Protocol (OCSP)
- RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3
- Python documentation: ssl, TLS/SSL wrapper for socket objects
- OWASP Transport Layer Security Cheat Sheet
- MITRE CWE-295: Improper Certificate Validation
- CISA BOD 18-01: Enhance Email and Web Security
- CA/Browser Forum Ballot SC-081v3: schedule of reducing validity periods
Quiz Unlocks after the last step.