Test certificate conditions before adding network effects
Certificate path validation and matching the intended hostname are distinct checks in TLS. This experiment uses Python and OpenSSL 3 to create an ephemeral root, intermediate CA and server certificate locally. It contacts no external site and does not modify the system-wide trust store. Compare a successful validation, a wrong-hostname validation and a validation missing the intermediate. Changing one condition at a time shows why a mathematically valid certificate signature alone does not identify the intended service. This offline experiment isolates certificate checks from network failure, protocol negotiation and application behavior that would otherwise complicate diagnosis.
Keep the trust decision local
The experimental root is supplied as CAfile only for these checks. It need not be installed in a normal browser or operating-system trust store. The intermediate receives CA constraints, while the leaf receives server-authentication usage and a subjectAltName for lab.example. Generated private keys remain in a temporary directory removed when the program exits. These are throwaway test keys, not replacements for a real service or certification authority. Keeping trust scope explicit avoids turning a teaching experiment into an unintended persistent security change. The names use an example domain and do not require that any corresponding network endpoint exists.
Assert three exit-status outcomes
The program requires exit status zero for the complete chain and correct name. It then keeps the same certificates but changes the requested hostname to wrong.example and requires failure. Finally it restores the correct name while withholding the intermediate and again requires failure. Use process exit codes rather than relying solely on a short diagnostic phrase. Certificate-generation failures stop the program before validation proceeds. OpenSSL versions and configurations can vary in message wording, so no one error sentence is treated as a universal expected transcript. The assertions encode the tested distinctions while preserving the actual diagnostic output for a failed setup.
import pathlib, subprocess, tempfile
with tempfile.TemporaryDirectory(prefix="tls-chain-lab-") as folder:
work = pathlib.Path(folder)
def openssl(*args, ok=True):
result = subprocess.run(["openssl", *args], cwd=work, capture_output=True, text=True)
if ok and result.returncode:
raise RuntimeError(result.stderr)
return result
openssl("req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "1",
"-subj", "/CN=Local Lab Root", "-keyout", "root.key", "-out", "root.pem",
"-addext", "basicConstraints=critical,CA:TRUE",
"-addext", "keyUsage=critical,keyCertSign,cRLSign")
openssl("req", "-new", "-newkey", "rsa:2048", "-nodes", "-subj", "/CN=Local Lab Intermediate",
"-keyout", "intermediate.key", "-out", "intermediate.csr")
(work/"ca.ext").write_text("basicConstraints=critical,CA:TRUE,pathlen:0\nkeyUsage=critical,keyCertSign,cRLSign\n")
openssl("x509", "-req", "-in", "intermediate.csr", "-CA", "root.pem", "-CAkey", "root.key",
"-CAcreateserial", "-days", "1", "-extfile", "ca.ext", "-out", "intermediate.pem")
openssl("req", "-new", "-newkey", "rsa:2048", "-nodes", "-subj", "/CN=lab.example",
"-keyout", "leaf.key", "-out", "leaf.csr")
(work/"leaf.ext").write_text("basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth\nsubjectAltName=DNS:lab.example\n")
openssl("x509", "-req", "-in", "leaf.csr", "-CA", "intermediate.pem", "-CAkey", "intermediate.key",
"-CAcreateserial", "-days", "1", "-extfile", "leaf.ext", "-out", "leaf.pem")
common = ["verify", "-purpose", "sslserver", "-CAfile", "root.pem"]
assert openssl(*common, "-untrusted", "intermediate.pem", "-verify_hostname", "lab.example", "leaf.pem").returncode == 0
assert openssl(*common, "-untrusted", "intermediate.pem", "-verify_hostname", "wrong.example", "leaf.pem", ok=False).returncode != 0
assert openssl(*common, "-verify_hostname", "lab.example", "leaf.pem", ok=False).returncode != 0
print("chain and hostname assertions passed")
An intermediate is not a new trust anchor
The certificate supplied through untrusted is a candidate used to construct a path, not automatically a newly trusted anchor. The root named in CAfile is the trust starting point for this experiment. Likewise, when a real server supplies intermediates, the client’s configuration and policy still determine trust. Some implementations can obtain missing intermediates through other mechanisms, but this generated example does not configure such a distribution path. A successful validation by one client therefore does not prove that every client can build the same chain. Certificate availability and the decision to trust an anchor are different requirements.
Distinguish identity, time and usage
The same certificate can pass or fail according to the intended name being checked. The wrong-name failure does not require changing the signature. Validity dates, usage and CA constraints also participate in validation; a badly incorrect local clock can cause another failure. Adding a blanket ignore-verification option conceals which guarantee was lost. Instead, identify the failed condition and supply an appropriate name and chain. This procedure teaches separation of checks, not a workaround that accepts an unauthenticated peer. Preserve the verification boundary when transferring the lesson to a client library with different option names.
State what the offline test does not establish
This experiment checks path and hostname validation for locally generated certificates. It does not exercise a real TLS handshake, handshake-time proof of private-key possession, operational revocation handling, network reachability or application authorization. Acceptance of a service certificate also does not establish that its returned information is true. Record OpenSSL version, the three exit outcomes, selected checks and absence of external traffic. Do not promote the generated material into normal trust configuration, and verify that temporary files are gone afterward. These limits let the experiment explain a precise mechanism without overstating it as a complete end-to-end security test.