CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
_openssl.py69 linesDownload Raw Back to truststore
1import contextlib2import os3import re4import ssl5import typing6 7# candidates based on https://github.com/tiran/certifi-system-store by Christian Heimes8_CA_FILE_CANDIDATES = [9    # Alpine, Arch, Fedora 34-42, OpenWRT, RHEL 9-10, BSD10    "/etc/ssl/cert.pem",11    # Fedora 43+, RHEL 11+12    "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",13    # Fedora <= 34, RHEL <= 9, CentOS <= 914    "/etc/pki/tls/cert.pem",15    # Debian, Ubuntu (requires ca-certificates)16    "/etc/ssl/certs/ca-certificates.crt",17    # SUSE18    "/etc/ssl/ca-bundle.pem",19]20 21_HASHED_CERT_FILENAME_RE = re.compile(r"^[0-9a-fA-F]{8}\.[0-9]$")22 23 24@contextlib.contextmanager25def _configure_context(ctx: ssl.SSLContext) -> typing.Iterator[None]:26    # First, check whether the default locations from OpenSSL27    # seem like they will give us a usable set of CA certs.28    # ssl.get_default_verify_paths already takes care of:29    # - getting cafile from either the SSL_CERT_FILE env var30    #   or the path configured when OpenSSL was compiled,31    #   and verifying that that path exists32    # - getting capath from either the SSL_CERT_DIR env var33    #   or the path configured when OpenSSL was compiled,34    #   and verifying that that path exists35    # In addition we'll check whether capath appears to contain certs.36    defaults = ssl.get_default_verify_paths()37    if defaults.cafile or (defaults.capath and _capath_contains_certs(defaults.capath)):38        ctx.set_default_verify_paths()39    else:40        # cafile from OpenSSL doesn't exist41        # and capath from OpenSSL doesn't contain certs.42        # Let's search other common locations instead.43        for cafile in _CA_FILE_CANDIDATES:44            if os.path.isfile(cafile):45                ctx.load_verify_locations(cafile=cafile)46                break47 48    yield49 50 51def _capath_contains_certs(capath: str) -> bool:52    """Check whether capath exists and contains certs in the expected format."""53    if not os.path.isdir(capath):54        return False55    for name in os.listdir(capath):56        if _HASHED_CERT_FILENAME_RE.match(name):57            return True58    return False59 60 61def _verify_peercerts_impl(62    ssl_context: ssl.SSLContext,63    cert_chain: list[bytes],64    server_hostname: str | None = None,65) -> None:66    # This is a no-op because we've enabled SSLContext's built-in67    # verification via verify_mode=CERT_REQUIRED, and don't need to repeat it.68    pass69