|
34 | 34 | from .route import Route
|
35 | 35 | from .status import BAD_REQUEST_400, UNAUTHORIZED_401, FORBIDDEN_403, NOT_FOUND_404
|
36 | 36 |
|
| 37 | +if implementation.name != "circuitpython": |
| 38 | + from ssl import Purpose, CERT_NONE, SSLError |
| 39 | + |
37 | 40 |
|
38 | 41 | NO_REQUEST = "no_request"
|
39 | 42 | CONNECTION_TIMED_OUT = "connection_timed_out"
|
@@ -62,13 +65,33 @@ def _validate_https_cert_provided(certfile: str, keyfile: str) -> None:
|
62 | 65 | raise ValueError("Both certfile and keyfile must be specified for HTTPS")
|
63 | 66 |
|
64 | 67 | @staticmethod
|
65 |
| - def _create_ssl_context(certfile: str, keyfile: str) -> SSLContext: |
| 68 | + def __create_circuitpython_ssl_context(certfile: str, keyfile: str) -> SSLContext: |
66 | 69 | ssl_context = create_default_context()
|
| 70 | + |
67 | 71 | ssl_context.load_verify_locations(cadata="")
|
68 | 72 | ssl_context.load_cert_chain(certfile, keyfile)
|
69 | 73 |
|
70 | 74 | return ssl_context
|
71 | 75 |
|
| 76 | + @staticmethod |
| 77 | + def __create_cpython_ssl_context(certfile: str, keyfile: str) -> SSLContext: |
| 78 | + ssl_context = create_default_context(purpose=Purpose.CLIENT_AUTH) |
| 79 | + |
| 80 | + ssl_context.load_cert_chain(certfile, keyfile) |
| 81 | + |
| 82 | + ssl_context.verify_mode = CERT_NONE |
| 83 | + ssl_context.check_hostname = False |
| 84 | + |
| 85 | + return ssl_context |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def _create_ssl_context(cls, certfile: str, keyfile: str) -> SSLContext: |
| 89 | + return ( |
| 90 | + cls.__create_circuitpython_ssl_context(certfile, keyfile) |
| 91 | + if implementation.name == "circuitpython" |
| 92 | + else cls.__create_cpython_ssl_context(certfile, keyfile) |
| 93 | + ) |
| 94 | + |
72 | 95 | def __init__(
|
73 | 96 | self,
|
74 | 97 | socket_source: _ISocketPool,
|
@@ -483,9 +506,16 @@ def poll(self) -> str:
|
483 | 506 | # Connection reset by peer, try again later.
|
484 | 507 | if error.errno == ECONNRESET:
|
485 | 508 | return NO_REQUEST
|
| 509 | + # Handshake failed, try again later. |
486 | 510 | if error.errno == MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
|
487 | 511 | return NO_REQUEST
|
488 | 512 |
|
| 513 | + # CPython specific SSL related errors |
| 514 | + if implementation.name != "circuitpython" and isinstance(error, SSLError): |
| 515 | + # Ignore unknown SSL certificate errors |
| 516 | + if getattr(error, "reason", None) == "SSLV3_ALERT_CERTIFICATE_UNKNOWN": |
| 517 | + return NO_REQUEST |
| 518 | + |
489 | 519 | if self.debug:
|
490 | 520 | _debug_exception_in_handler(error)
|
491 | 521 |
|
|
0 commit comments