39
39
stacklevel = 2 ,
40
40
)
41
41
try :
42
- import pymongo .ssl_context as _ssl
42
+ import pymongo .ssl_context as _stdssl
43
43
except ImportError :
44
44
HAVE_SSL = False
45
45
55
55
IPADDR_SAFE = True
56
56
57
57
if HAVE_PYSSL :
58
- HAS_SNI = _pyssl .HAS_SNI | _ssl .HAS_SNI
58
+ HAS_SNI = _pyssl .HAS_SNI | _stdssl .HAS_SNI
59
59
PYSSLError : Any = _pyssl .SSLError
60
- BLOCKING_IO_ERRORS : tuple = _pyssl .BLOCKING_IO_ERRORS + _ssl .BLOCKING_IO_ERRORS
61
- BLOCKING_IO_READ_ERROR : tuple = (_pyssl .BLOCKING_IO_READ_ERROR , _ssl .BLOCKING_IO_READ_ERROR )
60
+ BLOCKING_IO_ERRORS : tuple = _pyssl .BLOCKING_IO_ERRORS + _stdssl .BLOCKING_IO_ERRORS
61
+ BLOCKING_IO_READ_ERROR : tuple = (
62
+ _pyssl .BLOCKING_IO_READ_ERROR ,
63
+ _stdssl .BLOCKING_IO_READ_ERROR ,
64
+ )
62
65
BLOCKING_IO_WRITE_ERROR : tuple = (
63
66
_pyssl .BLOCKING_IO_WRITE_ERROR ,
64
- _ssl .BLOCKING_IO_WRITE_ERROR ,
67
+ _stdssl .BLOCKING_IO_WRITE_ERROR ,
65
68
)
66
69
else :
67
- HAS_SNI = _ssl .HAS_SNI
68
- PYSSLError = _ssl .SSLError
69
- BLOCKING_IO_ERRORS = _ssl .BLOCKING_IO_ERRORS
70
- BLOCKING_IO_READ_ERROR = (_ssl .BLOCKING_IO_READ_ERROR ,)
71
- BLOCKING_IO_WRITE_ERROR = (_ssl .BLOCKING_IO_WRITE_ERROR ,)
72
- SSLError = _ssl .SSLError
70
+ HAS_SNI = _stdssl .HAS_SNI
71
+ PYSSLError = _stdssl .SSLError
72
+ BLOCKING_IO_ERRORS = _stdssl .BLOCKING_IO_ERRORS
73
+ BLOCKING_IO_READ_ERROR = (_stdssl .BLOCKING_IO_READ_ERROR ,)
74
+ BLOCKING_IO_WRITE_ERROR = (_stdssl .BLOCKING_IO_WRITE_ERROR ,)
75
+ SSLError = _stdssl .SSLError
73
76
BLOCKING_IO_LOOKUP_ERROR = BLOCKING_IO_READ_ERROR
74
77
75
78
def get_ssl_context (
@@ -81,14 +84,14 @@ def get_ssl_context(
81
84
allow_invalid_hostnames : bool ,
82
85
disable_ocsp_endpoint_check : bool ,
83
86
is_sync : bool ,
84
- ) -> Union [_pyssl .SSLContext , _ssl .SSLContext ]: # type: ignore[name-defined]
87
+ ) -> Union [_pyssl .SSLContext , _stdssl .SSLContext ]: # type: ignore[name-defined]
85
88
"""Create and return an SSLContext object."""
86
89
if is_sync and HAVE_PYSSL :
87
- ssl_in_use : types .ModuleType = _pyssl
90
+ _ssl : types .ModuleType = _pyssl
88
91
else :
89
- ssl_in_use = _ssl
92
+ _ssl = _stdssl
90
93
verify_mode = CERT_NONE if allow_invalid_certificates else CERT_REQUIRED
91
- ctx = ssl_in_use .SSLContext (ssl_in_use .PROTOCOL_SSLv23 )
94
+ ctx = _ssl .SSLContext (_ssl .PROTOCOL_SSLv23 )
92
95
if verify_mode != CERT_NONE :
93
96
ctx .check_hostname = not allow_invalid_hostnames
94
97
else :
@@ -100,20 +103,20 @@ def get_ssl_context(
100
103
# up to date versions of MongoDB 2.4 and above already disable
101
104
# SSLv2 and SSLv3, python disables SSLv2 by default in >= 2.7.7
102
105
# and >= 3.3.4 and SSLv3 in >= 3.4.3.
103
- ctx .options |= ssl_in_use .OP_NO_SSLv2
104
- ctx .options |= ssl_in_use .OP_NO_SSLv3
105
- ctx .options |= ssl_in_use .OP_NO_COMPRESSION
106
- ctx .options |= ssl_in_use .OP_NO_RENEGOTIATION
106
+ ctx .options |= _ssl .OP_NO_SSLv2
107
+ ctx .options |= _ssl .OP_NO_SSLv3
108
+ ctx .options |= _ssl .OP_NO_COMPRESSION
109
+ ctx .options |= _ssl .OP_NO_RENEGOTIATION
107
110
if certfile is not None :
108
111
try :
109
112
ctx .load_cert_chain (certfile , None , passphrase )
110
- except ssl_in_use .SSLError as exc :
113
+ except _ssl .SSLError as exc :
111
114
raise ConfigurationError (f"Private key doesn't match certificate: { exc } " ) from None
112
115
if crlfile is not None :
113
- if ssl_in_use .IS_PYOPENSSL :
116
+ if _ssl .IS_PYOPENSSL :
114
117
raise ConfigurationError ("tlsCRLFile cannot be used with PyOpenSSL" )
115
118
# Match the server's behavior.
116
- ctx .verify_flags = getattr (ssl_in_use , "VERIFY_CRL_CHECK_LEAF" , 0 )
119
+ ctx .verify_flags = getattr (_ssl , "VERIFY_CRL_CHECK_LEAF" , 0 )
117
120
ctx .load_verify_locations (crlfile )
118
121
if ca_certs is not None :
119
122
ctx .load_verify_locations (ca_certs )
0 commit comments