|
94 | 94 | import sys |
95 | 95 | import os |
96 | 96 | from collections import namedtuple |
97 | | -from enum import Enum as _Enum, IntEnum as _IntEnum |
| 97 | +from enum import Enum as _Enum, IntEnum as _IntEnum, IntFlag as _IntFlag |
98 | 98 |
|
99 | 99 | import _ssl # if we can't import it, let the error propagate |
100 | 100 |
|
|
104 | 104 | SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError, |
105 | 105 | SSLSyscallError, SSLEOFError, |
106 | 106 | ) |
107 | | -from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED |
108 | 107 | from _ssl import txt2obj as _txt2obj, nid2obj as _nid2obj |
109 | 108 | from _ssl import RAND_status, RAND_add, RAND_bytes, RAND_pseudo_bytes |
110 | 109 | try: |
|
113 | 112 | # LibreSSL does not provide RAND_egd |
114 | 113 | pass |
115 | 114 |
|
116 | | -def _import_symbols(prefix): |
117 | | - for n in dir(_ssl): |
118 | | - if n.startswith(prefix): |
119 | | - globals()[n] = getattr(_ssl, n) |
120 | | - |
121 | | -_import_symbols('OP_') |
122 | | -_import_symbols('ALERT_DESCRIPTION_') |
123 | | -_import_symbols('SSL_ERROR_') |
124 | | -_import_symbols('VERIFY_') |
125 | 115 |
|
126 | 116 | from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN |
127 | | - |
128 | 117 | from _ssl import _OPENSSL_API_VERSION |
129 | 118 |
|
| 119 | + |
| 120 | +_IntEnum._convert( |
| 121 | + '_SSLMethod', __name__, |
| 122 | + lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23', |
| 123 | + source=_ssl) |
| 124 | + |
| 125 | +_IntFlag._convert( |
| 126 | + 'Options', __name__, |
| 127 | + lambda name: name.startswith('OP_'), |
| 128 | + source=_ssl) |
| 129 | + |
130 | 130 | _IntEnum._convert( |
131 | | - '_SSLMethod', __name__, |
132 | | - lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23', |
133 | | - source=_ssl) |
| 131 | + 'AlertDescription', __name__, |
| 132 | + lambda name: name.startswith('ALERT_DESCRIPTION_'), |
| 133 | + source=_ssl) |
| 134 | + |
| 135 | +_IntEnum._convert( |
| 136 | + 'SSLErrorNumber', __name__, |
| 137 | + lambda name: name.startswith('SSL_ERROR_'), |
| 138 | + source=_ssl) |
| 139 | + |
| 140 | +_IntFlag._convert( |
| 141 | + 'VerifyFlags', __name__, |
| 142 | + lambda name: name.startswith('VERIFY_'), |
| 143 | + source=_ssl) |
| 144 | + |
| 145 | +_IntEnum._convert( |
| 146 | + 'VerifyMode', __name__, |
| 147 | + lambda name: name.startswith('CERT_'), |
| 148 | + source=_ssl) |
| 149 | + |
134 | 150 |
|
135 | 151 | PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_TLS |
136 | 152 | _PROTOCOL_NAMES = {value: name for name, value in _SSLMethod.__members__.items()} |
137 | 153 |
|
138 | | -try: |
139 | | - _SSLv2_IF_EXISTS = PROTOCOL_SSLv2 |
140 | | -except NameError: |
141 | | - _SSLv2_IF_EXISTS = None |
| 154 | +_SSLv2_IF_EXISTS = getattr(_SSLMethod, 'PROTOCOL_SSLv2', None) |
| 155 | + |
142 | 156 |
|
143 | 157 | if sys.platform == "win32": |
144 | 158 | from _ssl import enum_certificates, enum_crls |
@@ -434,6 +448,34 @@ def load_default_certs(self, purpose=Purpose.SERVER_AUTH): |
434 | 448 | self._load_windows_store_certs(storename, purpose) |
435 | 449 | self.set_default_verify_paths() |
436 | 450 |
|
| 451 | + @property |
| 452 | + def options(self): |
| 453 | + return Options(super().options) |
| 454 | + |
| 455 | + @options.setter |
| 456 | + def options(self, value): |
| 457 | + super(SSLContext, SSLContext).options.__set__(self, value) |
| 458 | + |
| 459 | + @property |
| 460 | + def verify_flags(self): |
| 461 | + return VerifyFlags(super().verify_flags) |
| 462 | + |
| 463 | + @verify_flags.setter |
| 464 | + def verify_flags(self, value): |
| 465 | + super(SSLContext, SSLContext).verify_flags.__set__(self, value) |
| 466 | + |
| 467 | + @property |
| 468 | + def verify_mode(self): |
| 469 | + value = super().verify_mode |
| 470 | + try: |
| 471 | + return VerifyMode(value) |
| 472 | + except ValueError: |
| 473 | + return value |
| 474 | + |
| 475 | + @verify_mode.setter |
| 476 | + def verify_mode(self, value): |
| 477 | + super(SSLContext, SSLContext).verify_mode.__set__(self, value) |
| 478 | + |
437 | 479 |
|
438 | 480 | def create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None, |
439 | 481 | capath=None, cadata=None): |
|
0 commit comments