An SSLContext holds various SSL-related configuration options and data, such as certificates and possibly a private key.
| 420 | |
| 421 | |
| 422 | class SSLContext(_SSLContext): |
| 423 | """An SSLContext holds various SSL-related configuration options and |
| 424 | data, such as certificates and possibly a private key.""" |
| 425 | _windows_cert_stores = ("CA", "ROOT") |
| 426 | |
| 427 | sslsocket_class = None # SSLSocket is assigned later. |
| 428 | sslobject_class = None # SSLObject is assigned later. |
| 429 | |
| 430 | def __new__(cls, protocol=None, *args, **kwargs): |
| 431 | if protocol is None: |
| 432 | warnings.warn( |
| 433 | "ssl.SSLContext() without protocol argument is deprecated.", |
| 434 | category=DeprecationWarning, |
| 435 | stacklevel=2 |
| 436 | ) |
| 437 | protocol = PROTOCOL_TLS |
| 438 | self = _SSLContext.__new__(cls, protocol) |
| 439 | return self |
| 440 | |
| 441 | def _encode_hostname(self, hostname): |
| 442 | if hostname is None: |
| 443 | return None |
| 444 | elif isinstance(hostname, str): |
| 445 | return hostname.encode('idna').decode('ascii') |
| 446 | else: |
| 447 | return hostname.decode('ascii') |
| 448 | |
| 449 | def wrap_socket(self, sock, server_side=False, |
| 450 | do_handshake_on_connect=True, |
| 451 | suppress_ragged_eofs=True, |
| 452 | server_hostname=None, session=None): |
| 453 | # SSLSocket class handles server_hostname encoding before it calls |
| 454 | # ctx._wrap_socket() |
| 455 | return self.sslsocket_class._create( |
| 456 | sock=sock, |
| 457 | server_side=server_side, |
| 458 | do_handshake_on_connect=do_handshake_on_connect, |
| 459 | suppress_ragged_eofs=suppress_ragged_eofs, |
| 460 | server_hostname=server_hostname, |
| 461 | context=self, |
| 462 | session=session |
| 463 | ) |
| 464 | |
| 465 | def wrap_bio(self, incoming, outgoing, server_side=False, |
| 466 | server_hostname=None, session=None): |
| 467 | # Need to encode server_hostname here because _wrap_bio() can only |
| 468 | # handle ASCII str. |
| 469 | return self.sslobject_class._create( |
| 470 | incoming, outgoing, server_side=server_side, |
| 471 | server_hostname=self._encode_hostname(server_hostname), |
| 472 | session=session, context=self, |
| 473 | ) |
| 474 | |
| 475 | def set_npn_protocols(self, npn_protocols): |
| 476 | warnings.warn( |
| 477 | "ssl NPN is deprecated, use ALPN instead", |
| 478 | DeprecationWarning, |
| 479 | stacklevel=2 |
no test coverage detected