#!/usr/bin/env python3 """pqprobe: does a TLS server accept post-quantum key exchange? Sends one TLS 1.3 ClientHello that lists ONLY post-quantum groups and an EMPTY key_share. A server that supports one of the groups must answer with a HelloRetryRequest naming it. A server that supports none answers with an alert. No keys are generated, so the probe needs nothing beyond the standard library. A host that refuses is checked again with an ordinary ClientHello, so that "classical only" is reported only when normal TLS works for that name. usage: pqprobe.py [--json] host [host ...] """ import json, os, socket, struct, sys HRR_RANDOM = bytes.fromhex("CF21AD74E59A6111BE1D8C021E65B891C2A211167ABB8C5E079E09E2C8A8339C") PQ_GROUPS = { 0x11EC: "X25519MLKEM768", 0x11EB: "SecP256r1MLKEM768", 0x11ED: "SecP384r1MLKEM1024", 0x0201: "MLKEM768", 0x0202: "MLKEM1024", } CLASSICAL_GROUPS = (0x001D, 0x0017, 0x0018) # x25519, secp256r1, secp384r1 TLS13_SUITES = bytes.fromhex("130113021303") TLS12_SUITES = bytes.fromhex("c02bc02fc02cc030cca9cca8c013c014009c009d002f0035") SIG_ALGS = bytes.fromhex("0403080404010503080505010806060108070201") def _ext(ext_type, body): return struct.pack(">HH", ext_type, len(body)) + body def client_hello(host, groups, allow_tls12=False): name = host.encode() sni = struct.pack(">H", len(name) + 3) + b"\x00" + struct.pack(">H", len(name)) + name group_list = b"".join(struct.pack(">H", g) for g in groups) versions = b"\x04\x03\x04\x03\x03" if allow_tls12 else b"\x02\x03\x04" suites = TLS13_SUITES + (TLS12_SUITES if allow_tls12 else b"") exts = ( _ext(0, sni) + _ext(43, versions) + _ext(10, struct.pack(">H", len(group_list)) + group_list) + _ext(13, struct.pack(">H", len(SIG_ALGS)) + SIG_ALGS) + _ext(51, b"\x00\x00") # key_share with no entries ) if allow_tls12: exts += _ext(11, b"\x01\x00") + _ext(23, b"") + _ext(0xFF01, b"\x00") body = ( b"\x03\x03" + os.urandom(32) + b"\x20" + os.urandom(32) + struct.pack(">H", len(suites)) + suites + b"\x01\x00" + struct.pack(">H", len(exts)) + exts ) handshake = b"\x01" + struct.pack(">I", len(body))[1:] + body return b"\x16\x03\x01" + struct.pack(">H", len(handshake)) + handshake def _first_record(host, hello, timeout): with socket.create_connection((host, 443), timeout=timeout) as s: s.sendall(hello) buf = b"" while len(buf) < 5: chunk = s.recv(4096) if not chunk: return buf buf += chunk need = 5 + struct.unpack(">H", buf[3:5])[0] while len(buf) < need: chunk = s.recv(4096) if not chunk: break buf += chunk return buf def _server_hello(buf): """Return (is_tls13, random, selected_group) or None if this is not a ServerHello.""" if len(buf) < 44 or buf[0] != 0x16 or buf[5] != 2: return None rnd = buf[11:43] p = 43 p += 1 + buf[p] # session id p += 3 # cipher suite + compression tls13, group = False, None if p + 2 <= len(buf): end = p + 2 + struct.unpack(">H", buf[p:p + 2])[0] p += 2 while p + 4 <= min(end, len(buf)): t, l = struct.unpack(">HH", buf[p:p + 4]) v = buf[p + 4:p + 4 + l] p += 4 + l if t == 43: tls13 = True if t == 51 and l >= 2: group = struct.unpack(">H", v[:2])[0] return tls13, rnd, group def probe(host, timeout=8.0): """Return a dict: host, result (pq | classical | no-tls | unreachable), plus detail.""" out = {"host": host} try: buf = _first_record(host, client_hello(host, list(PQ_GROUPS)), timeout) except OSError as e: return {**out, "result": "unreachable", "detail": type(e).__name__} sh = _server_hello(buf) if sh and sh[0] and sh[1] == HRR_RANDOM and sh[2] in PQ_GROUPS: return {**out, "result": "pq", "group": PQ_GROUPS[sh[2]]} # Refused. Check that ordinary TLS works for this name before calling it classical. try: buf = _first_record(host, client_hello(host, CLASSICAL_GROUPS, allow_tls12=True), timeout) except OSError as e: return {**out, "result": "unreachable", "detail": type(e).__name__} sh = _server_hello(buf) if sh is None: return {**out, "result": "no-tls", "detail": "alert %d" % buf[6] if len(buf) > 6 and buf[0] == 0x15 else "no ServerHello"} return {**out, "result": "classical", "tls": "1.3" if sh[0] else "1.2"} def main(argv): as_json = "--json" in argv hosts = [a for a in argv if not a.startswith("-")] if not hosts: print(__doc__.strip()) return 2 for h in hosts: r = probe(h.strip().lower()) if as_json: print(json.dumps(r)) elif r["result"] == "pq": print(f"{h}: accepts post-quantum key exchange ({r['group']})") elif r["result"] == "classical": print(f"{h}: classical only (TLS {r['tls']})") else: print(f"{h}: {r['result']} ({r.get('detail', '')})") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))