Why certificates
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.
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.
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.
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.
Python checks the chain
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.
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.
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.
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.
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.
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.
The 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.
What happens
;; step 1/11 · whose-key
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.com
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. 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.