|
92 | 92 | import sys |
93 | 93 | import os |
94 | 94 | from collections import namedtuple |
| 95 | +from enum import Enum as _Enum |
95 | 96 |
|
96 | 97 | import _ssl # if we can't import it, let the error propagate |
97 | 98 |
|
@@ -298,11 +299,19 @@ def fromname(cls, name): |
298 | 299 | return super().__new__(cls, *_txt2obj(name, name=True)) |
299 | 300 |
|
300 | 301 |
|
| 302 | +class Purpose(_ASN1Object, _Enum): |
| 303 | + """SSLContext purpose flags with X509v3 Extended Key Usage objects |
| 304 | + """ |
| 305 | + SERVER_AUTH = '1.3.6.1.5.5.7.3.1' |
| 306 | + CLIENT_AUTH = '1.3.6.1.5.5.7.3.2' |
| 307 | + |
| 308 | + |
301 | 309 | class SSLContext(_SSLContext): |
302 | 310 | """An SSLContext holds various SSL-related configuration options and |
303 | 311 | data, such as certificates and possibly a private key.""" |
304 | 312 |
|
305 | 313 | __slots__ = ('protocol', '__weakref__') |
| 314 | + _windows_cert_stores = ("CA", "ROOT") |
306 | 315 |
|
307 | 316 | def __new__(cls, protocol, *args, **kwargs): |
308 | 317 | self = _SSLContext.__new__(cls, protocol) |
@@ -334,6 +343,25 @@ def set_npn_protocols(self, npn_protocols): |
334 | 343 |
|
335 | 344 | self._set_npn_protocols(protos) |
336 | 345 |
|
| 346 | + def _load_windows_store_certs(self, storename, purpose): |
| 347 | + certs = bytearray() |
| 348 | + for cert, encoding, trust in enum_certificates(storename): |
| 349 | + # CA certs are never PKCS#7 encoded |
| 350 | + if encoding == "x509_asn": |
| 351 | + if trust is True or purpose.oid in trust: |
| 352 | + certs.extend(cert) |
| 353 | + self.load_verify_locations(cadata=certs) |
| 354 | + return certs |
| 355 | + |
| 356 | + def load_default_certs(self, purpose=Purpose.SERVER_AUTH): |
| 357 | + if not isinstance(purpose, _ASN1Object): |
| 358 | + raise TypeError(purpose) |
| 359 | + if sys.platform == "win32": |
| 360 | + for storename in self._windows_cert_stores: |
| 361 | + self._load_windows_store_certs(storename, purpose) |
| 362 | + else: |
| 363 | + self.set_default_verify_paths() |
| 364 | + |
337 | 365 |
|
338 | 366 | class SSLSocket(socket): |
339 | 367 | """This class implements a subtype of socket.socket that wraps |
|
0 commit comments