Files
mtusi2026lab7/src/tls_context.py
T

25 lines
751 B
Python

"""Require a trusted certificate and an exact DNS SAN from the allowlist."""
import ssl
def listener(cert, key, ca, mutual):
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.minimum_version = ssl.TLSVersion.TLSv1_2
context.options |= ssl.OP_NO_TICKET
context.load_cert_chain(cert, key)
if mutual:
context.load_verify_locations(ca)
context.verify_mode = ssl.CERT_REQUIRED
return context
def allowed(ssl_socket, identities):
if not ssl_socket:
return False
certificate = ssl_socket.getpeercert()
if not certificate:
return False
sans = {value for kind, value in certificate.get("subjectAltName", ()) if kind == "DNS"}
return len(sans) == 1 and sans <= identities