Skip to content

Commit

Permalink
Add support for macOS verification disabling too
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson committed Jan 26, 2023
1 parent 0d7f8a2 commit c26b914
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/truststore/_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ class CFConst:

kCFStringEncodingUTF8 = CFStringEncoding(0x08000100)

errSecIncompleteCertRevocationCheck = -67635
errSecHostNameMismatch = -67602
errSecCertificateExpired = -67818
errSecNotTrusted = -67843


def _bytes_to_cf_data_ref(value: bytes) -> CFDataRef: # type: ignore[valid-type]
return CoreFoundation.CFDataCreate( # type: ignore[no-any-return]
Expand Down Expand Up @@ -439,8 +444,27 @@ def _verify_peercerts_impl(
f"Unknown result from Security.SecTrustEvaluateWithError: {sec_trust_eval_result!r}"
)

cf_error_code = 0
if not is_trusted:
cf_error_code = CoreFoundation.CFErrorGetCode(cf_error)

# If the error is a known failure that we're
# explicitly okay with from SSLContext configuration
# we can set is_trusted accordingly.
if ssl_context.verify_mode != ssl.CERT_REQUIRED and (
cf_error_code == CFConst.errSecNotTrusted
or cf_error_code == CFConst.errSecCertificateExpired
):
is_trusted = True
elif (
not ssl_context.check_hostname
and cf_error_code == CFConst.errSecHostNameMismatch
):
is_trusted = True

# If we're still not trusted then we start to
# construct and raise the SSLCertVerificationError.
if not is_trusted:
cf_error_string_ref = None
try:
cf_error_string_ref = CoreFoundation.CFErrorCopyDescription(cf_error)
Expand Down

0 comments on commit c26b914

Please sign in to comment.