"""Loopback-only TLS profile checks. Requires OpenSSL 3.6.3 and Python 3.""" import argparse from contextlib import contextmanager from pathlib import Path import re import socket import subprocess import tempfile import time HYBRID = "X25519MLKEM768" CLASSICAL = "X25519" def run(*args, check=True): return subprocess.run( [OPENSSL, *args], input="", text=True, capture_output=True, timeout=8, check=check, ) @contextmanager def endpoint(directory, name, groups): cert = str(directory / f"{name}.crt") key = str(directory / f"{name}.key") run("req", "-x509", "-newkey", "rsa:2048", "-noenc", "-days", "1", "-subj", f"/CN={name}", "-addext", f"subjectAltName=DNS:{name}", "-keyout", key, "-out", cert) with socket.socket() as reservation: reservation.bind(("127.0.0.1", 0)) port = reservation.getsockname()[1] server = subprocess.Popen( [OPENSSL, "s_server", "-accept", f"127.0.0.1:{port}", "-tls1_3", "-cert", cert, "-key", key, "-groups", groups, "-www", "-quiet"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) try: until = time.monotonic() + 5 while True: if server.poll() is not None or time.monotonic() >= until: raise RuntimeError(f"{name}: TLS server did not start") try: with socket.create_connection(("127.0.0.1", port), timeout=0.1): break except OSError: time.sleep(0.05) yield port, name, cert finally: if server.poll() is None: server.terminate() try: server.wait(timeout=2) except subprocess.TimeoutExpired: server.kill() server.wait() def probe(endpoint, offered, hostname=None): port, name, cert = endpoint result = run("s_client", "-connect", f"127.0.0.1:{port}", "-servername", name, "-verify_hostname", hostname or name, "-CAfile", cert, "-verify_return_error", "-tls1_3", "-groups", offered, "-brief", check=False) return result.returncode, result.stdout + result.stderr def expect_group(label, endpoint, offered, expected): status, transcript = probe(endpoint, offered) group = re.search(r"(?:Negotiated TLS1\.3 group|Peer Temp Key):\s*([^\s,]+)", transcript) if status or not group or group[1] != expected or "Verification: OK" not in transcript: raise RuntimeError(f"{label}: expected verified {expected}\n{transcript}") if not re.search(r"^Signature type: rsa_pss_rsae_sha256\s*$", transcript, re.MULTILINE): raise RuntimeError(f"{label}: expected RSA-PSS peer handshake signature\n{transcript}") print(f"{label}: {expected}, RSA-PSS peer signature") def expect_failure(label, target, groups, reason, *, hostname=None): status, transcript = probe(target, groups, hostname) if not status or reason not in transcript.lower(): raise RuntimeError(f"{label}: expected {reason}\n{transcript}") print(f"{label}: handshake rejected") def main(): with tempfile.TemporaryDirectory(prefix="tls-legs-") as directory: path = Path(directory) with endpoint(path, "edge.test", f"{HYBRID}:{CLASSICAL}") as edge: expect_group("edge hybrid", edge, HYBRID, HYBRID) expect_group("edge classical client", edge, CLASSICAL, CLASSICAL) expect_failure("wrong origin identity", edge, HYBRID, "hostname mismatch", hostname="origin.test") with endpoint(path, "origin.test", CLASSICAL) as origin: expect_group("origin fallback", origin, f"{HYBRID}:{CLASSICAL}", CLASSICAL) expect_failure("origin hybrid only", origin, HYBRID, "handshake failure") with endpoint(path, "origin.test", f"{HYBRID}:{CLASSICAL}") as origin: expect_group("origin after profile change", origin, HYBRID, HYBRID) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--openssl", default="openssl", help="OpenSSL executable") OPENSSL = parser.parse_args().openssl print(run("version").stdout.split(" (Library:", 1)[0].strip()) main()