Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/truststore/_openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import ssl
import typing
import weakref

# candidates based on https://github.com/tiran/certifi-system-store by Christian Heimes
_CA_FILE_CANDIDATES = [
Expand All @@ -24,9 +25,15 @@

_HASHED_CERT_FILENAME_RE = re.compile(r"^[0-9a-fA-F]{8}\.[0-9]$")

_configured_contexts: weakref.WeakSet[ssl.SSLContext] = weakref.WeakSet()


@contextlib.contextmanager
def _configure_context(ctx: ssl.SSLContext) -> typing.Iterator[None]:
if ctx in _configured_contexts:
yield
return

# First, check whether the default locations from OpenSSL
# seem like they will give us a usable set of CA certs.
# ssl.get_default_verify_paths already takes care of:
Expand Down Expand Up @@ -54,6 +61,7 @@ def _configure_context(ctx: ssl.SSLContext) -> typing.Iterator[None]:
ctx.load_verify_locations(cafile=cafile_or_cadir)
break

_configured_contexts.add(ctx)
yield


Expand Down
Loading