pyopenssl.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. """
  2. Module for using pyOpenSSL as a TLS backend. This module was relevant before
  3. the standard library ``ssl`` module supported SNI, but now that we've dropped
  4. support for Python 2.7 all relevant Python versions support SNI so
  5. **this module is no longer recommended**.
  6. This needs the following packages installed:
  7. * `pyOpenSSL`_ (tested with 16.0.0)
  8. * `cryptography`_ (minimum 1.3.4, from pyopenssl)
  9. * `idna`_ (minimum 2.0)
  10. However, pyOpenSSL depends on cryptography, so while we use all three directly here we
  11. end up having relatively few packages required.
  12. You can install them with the following command:
  13. .. code-block:: bash
  14. $ python -m pip install pyopenssl cryptography idna
  15. To activate certificate checking, call
  16. :func:`~urllib3.contrib.pyopenssl.inject_into_urllib3` from your Python code
  17. before you begin making HTTP requests. This can be done in a ``sitecustomize``
  18. module, or at any other time before your application begins using ``urllib3``,
  19. like this:
  20. .. code-block:: python
  21. try:
  22. import urllib3.contrib.pyopenssl
  23. urllib3.contrib.pyopenssl.inject_into_urllib3()
  24. except ImportError:
  25. pass
  26. .. _pyopenssl: https://www.pyopenssl.org
  27. .. _cryptography: https://cryptography.io
  28. .. _idna: https://github.com/kjd/idna
  29. """
  30. from __future__ import annotations
  31. import OpenSSL.SSL # type: ignore[import]
  32. from cryptography import x509
  33. try:
  34. from cryptography.x509 import UnsupportedExtension # type: ignore[attr-defined]
  35. except ImportError:
  36. # UnsupportedExtension is gone in cryptography >= 2.1.0
  37. class UnsupportedExtension(Exception): # type: ignore[no-redef]
  38. pass
  39. import logging
  40. import ssl
  41. import typing
  42. from io import BytesIO
  43. from socket import socket as socket_cls
  44. from socket import timeout
  45. from .. import util
  46. if typing.TYPE_CHECKING:
  47. from OpenSSL.crypto import X509 # type: ignore[import]
  48. __all__ = ["inject_into_urllib3", "extract_from_urllib3"]
  49. # Map from urllib3 to PyOpenSSL compatible parameter-values.
  50. _openssl_versions = {
  51. util.ssl_.PROTOCOL_TLS: OpenSSL.SSL.SSLv23_METHOD, # type: ignore[attr-defined]
  52. util.ssl_.PROTOCOL_TLS_CLIENT: OpenSSL.SSL.SSLv23_METHOD, # type: ignore[attr-defined]
  53. ssl.PROTOCOL_TLSv1: OpenSSL.SSL.TLSv1_METHOD,
  54. }
  55. if hasattr(ssl, "PROTOCOL_TLSv1_1") and hasattr(OpenSSL.SSL, "TLSv1_1_METHOD"):
  56. _openssl_versions[ssl.PROTOCOL_TLSv1_1] = OpenSSL.SSL.TLSv1_1_METHOD
  57. if hasattr(ssl, "PROTOCOL_TLSv1_2") and hasattr(OpenSSL.SSL, "TLSv1_2_METHOD"):
  58. _openssl_versions[ssl.PROTOCOL_TLSv1_2] = OpenSSL.SSL.TLSv1_2_METHOD
  59. _stdlib_to_openssl_verify = {
  60. ssl.CERT_NONE: OpenSSL.SSL.VERIFY_NONE,
  61. ssl.CERT_OPTIONAL: OpenSSL.SSL.VERIFY_PEER,
  62. ssl.CERT_REQUIRED: OpenSSL.SSL.VERIFY_PEER
  63. + OpenSSL.SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
  64. }
  65. _openssl_to_stdlib_verify = {v: k for k, v in _stdlib_to_openssl_verify.items()}
  66. # The SSLvX values are the most likely to be missing in the future
  67. # but we check them all just to be sure.
  68. _OP_NO_SSLv2_OR_SSLv3: int = getattr(OpenSSL.SSL, "OP_NO_SSLv2", 0) | getattr(
  69. OpenSSL.SSL, "OP_NO_SSLv3", 0
  70. )
  71. _OP_NO_TLSv1: int = getattr(OpenSSL.SSL, "OP_NO_TLSv1", 0)
  72. _OP_NO_TLSv1_1: int = getattr(OpenSSL.SSL, "OP_NO_TLSv1_1", 0)
  73. _OP_NO_TLSv1_2: int = getattr(OpenSSL.SSL, "OP_NO_TLSv1_2", 0)
  74. _OP_NO_TLSv1_3: int = getattr(OpenSSL.SSL, "OP_NO_TLSv1_3", 0)
  75. _openssl_to_ssl_minimum_version: dict[int, int] = {
  76. ssl.TLSVersion.MINIMUM_SUPPORTED: _OP_NO_SSLv2_OR_SSLv3,
  77. ssl.TLSVersion.TLSv1: _OP_NO_SSLv2_OR_SSLv3,
  78. ssl.TLSVersion.TLSv1_1: _OP_NO_SSLv2_OR_SSLv3 | _OP_NO_TLSv1,
  79. ssl.TLSVersion.TLSv1_2: _OP_NO_SSLv2_OR_SSLv3 | _OP_NO_TLSv1 | _OP_NO_TLSv1_1,
  80. ssl.TLSVersion.TLSv1_3: (
  81. _OP_NO_SSLv2_OR_SSLv3 | _OP_NO_TLSv1 | _OP_NO_TLSv1_1 | _OP_NO_TLSv1_2
  82. ),
  83. ssl.TLSVersion.MAXIMUM_SUPPORTED: (
  84. _OP_NO_SSLv2_OR_SSLv3 | _OP_NO_TLSv1 | _OP_NO_TLSv1_1 | _OP_NO_TLSv1_2
  85. ),
  86. }
  87. _openssl_to_ssl_maximum_version: dict[int, int] = {
  88. ssl.TLSVersion.MINIMUM_SUPPORTED: (
  89. _OP_NO_SSLv2_OR_SSLv3
  90. | _OP_NO_TLSv1
  91. | _OP_NO_TLSv1_1
  92. | _OP_NO_TLSv1_2
  93. | _OP_NO_TLSv1_3
  94. ),
  95. ssl.TLSVersion.TLSv1: (
  96. _OP_NO_SSLv2_OR_SSLv3 | _OP_NO_TLSv1_1 | _OP_NO_TLSv1_2 | _OP_NO_TLSv1_3
  97. ),
  98. ssl.TLSVersion.TLSv1_1: _OP_NO_SSLv2_OR_SSLv3 | _OP_NO_TLSv1_2 | _OP_NO_TLSv1_3,
  99. ssl.TLSVersion.TLSv1_2: _OP_NO_SSLv2_OR_SSLv3 | _OP_NO_TLSv1_3,
  100. ssl.TLSVersion.TLSv1_3: _OP_NO_SSLv2_OR_SSLv3,
  101. ssl.TLSVersion.MAXIMUM_SUPPORTED: _OP_NO_SSLv2_OR_SSLv3,
  102. }
  103. # OpenSSL will only write 16K at a time
  104. SSL_WRITE_BLOCKSIZE = 16384
  105. orig_util_SSLContext = util.ssl_.SSLContext
  106. log = logging.getLogger(__name__)
  107. def inject_into_urllib3() -> None:
  108. "Monkey-patch urllib3 with PyOpenSSL-backed SSL-support."
  109. _validate_dependencies_met()
  110. util.SSLContext = PyOpenSSLContext # type: ignore[assignment]
  111. util.ssl_.SSLContext = PyOpenSSLContext # type: ignore[assignment]
  112. util.IS_PYOPENSSL = True
  113. util.ssl_.IS_PYOPENSSL = True
  114. def extract_from_urllib3() -> None:
  115. "Undo monkey-patching by :func:`inject_into_urllib3`."
  116. util.SSLContext = orig_util_SSLContext
  117. util.ssl_.SSLContext = orig_util_SSLContext
  118. util.IS_PYOPENSSL = False
  119. util.ssl_.IS_PYOPENSSL = False
  120. def _validate_dependencies_met() -> None:
  121. """
  122. Verifies that PyOpenSSL's package-level dependencies have been met.
  123. Throws `ImportError` if they are not met.
  124. """
  125. # Method added in `cryptography==1.1`; not available in older versions
  126. from cryptography.x509.extensions import Extensions
  127. if getattr(Extensions, "get_extension_for_class", None) is None:
  128. raise ImportError(
  129. "'cryptography' module missing required functionality. "
  130. "Try upgrading to v1.3.4 or newer."
  131. )
  132. # pyOpenSSL 0.14 and above use cryptography for OpenSSL bindings. The _x509
  133. # attribute is only present on those versions.
  134. from OpenSSL.crypto import X509
  135. x509 = X509()
  136. if getattr(x509, "_x509", None) is None:
  137. raise ImportError(
  138. "'pyOpenSSL' module missing required functionality. "
  139. "Try upgrading to v0.14 or newer."
  140. )
  141. def _dnsname_to_stdlib(name: str) -> str | None:
  142. """
  143. Converts a dNSName SubjectAlternativeName field to the form used by the
  144. standard library on the given Python version.
  145. Cryptography produces a dNSName as a unicode string that was idna-decoded
  146. from ASCII bytes. We need to idna-encode that string to get it back, and
  147. then on Python 3 we also need to convert to unicode via UTF-8 (the stdlib
  148. uses PyUnicode_FromStringAndSize on it, which decodes via UTF-8).
  149. If the name cannot be idna-encoded then we return None signalling that
  150. the name given should be skipped.
  151. """
  152. def idna_encode(name: str) -> bytes | None:
  153. """
  154. Borrowed wholesale from the Python Cryptography Project. It turns out
  155. that we can't just safely call `idna.encode`: it can explode for
  156. wildcard names. This avoids that problem.
  157. """
  158. import idna
  159. try:
  160. for prefix in ["*.", "."]:
  161. if name.startswith(prefix):
  162. name = name[len(prefix) :]
  163. return prefix.encode("ascii") + idna.encode(name)
  164. return idna.encode(name)
  165. except idna.core.IDNAError:
  166. return None
  167. # Don't send IPv6 addresses through the IDNA encoder.
  168. if ":" in name:
  169. return name
  170. encoded_name = idna_encode(name)
  171. if encoded_name is None:
  172. return None
  173. return encoded_name.decode("utf-8")
  174. def get_subj_alt_name(peer_cert: X509) -> list[tuple[str, str]]:
  175. """
  176. Given an PyOpenSSL certificate, provides all the subject alternative names.
  177. """
  178. cert = peer_cert.to_cryptography()
  179. # We want to find the SAN extension. Ask Cryptography to locate it (it's
  180. # faster than looping in Python)
  181. try:
  182. ext = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value
  183. except x509.ExtensionNotFound:
  184. # No such extension, return the empty list.
  185. return []
  186. except (
  187. x509.DuplicateExtension,
  188. UnsupportedExtension,
  189. x509.UnsupportedGeneralNameType,
  190. UnicodeError,
  191. ) as e:
  192. # A problem has been found with the quality of the certificate. Assume
  193. # no SAN field is present.
  194. log.warning(
  195. "A problem was encountered with the certificate that prevented "
  196. "urllib3 from finding the SubjectAlternativeName field. This can "
  197. "affect certificate validation. The error was %s",
  198. e,
  199. )
  200. return []
  201. # We want to return dNSName and iPAddress fields. We need to cast the IPs
  202. # back to strings because the match_hostname function wants them as
  203. # strings.
  204. # Sadly the DNS names need to be idna encoded and then, on Python 3, UTF-8
  205. # decoded. This is pretty frustrating, but that's what the standard library
  206. # does with certificates, and so we need to attempt to do the same.
  207. # We also want to skip over names which cannot be idna encoded.
  208. names = [
  209. ("DNS", name)
  210. for name in map(_dnsname_to_stdlib, ext.get_values_for_type(x509.DNSName))
  211. if name is not None
  212. ]
  213. names.extend(
  214. ("IP Address", str(name)) for name in ext.get_values_for_type(x509.IPAddress)
  215. )
  216. return names
  217. class WrappedSocket:
  218. """API-compatibility wrapper for Python OpenSSL's Connection-class."""
  219. def __init__(
  220. self,
  221. connection: OpenSSL.SSL.Connection,
  222. socket: socket_cls,
  223. suppress_ragged_eofs: bool = True,
  224. ) -> None:
  225. self.connection = connection
  226. self.socket = socket
  227. self.suppress_ragged_eofs = suppress_ragged_eofs
  228. self._io_refs = 0
  229. self._closed = False
  230. def fileno(self) -> int:
  231. return self.socket.fileno()
  232. # Copy-pasted from Python 3.5 source code
  233. def _decref_socketios(self) -> None:
  234. if self._io_refs > 0:
  235. self._io_refs -= 1
  236. if self._closed:
  237. self.close()
  238. def recv(self, *args: typing.Any, **kwargs: typing.Any) -> bytes:
  239. try:
  240. data = self.connection.recv(*args, **kwargs)
  241. except OpenSSL.SSL.SysCallError as e:
  242. if self.suppress_ragged_eofs and e.args == (-1, "Unexpected EOF"):
  243. return b""
  244. else:
  245. raise OSError(e.args[0], str(e)) from e
  246. except OpenSSL.SSL.ZeroReturnError:
  247. if self.connection.get_shutdown() == OpenSSL.SSL.RECEIVED_SHUTDOWN:
  248. return b""
  249. else:
  250. raise
  251. except OpenSSL.SSL.WantReadError as e:
  252. if not util.wait_for_read(self.socket, self.socket.gettimeout()):
  253. raise timeout("The read operation timed out") from e
  254. else:
  255. return self.recv(*args, **kwargs)
  256. # TLS 1.3 post-handshake authentication
  257. except OpenSSL.SSL.Error as e:
  258. raise ssl.SSLError(f"read error: {e!r}") from e
  259. else:
  260. return data # type: ignore[no-any-return]
  261. def recv_into(self, *args: typing.Any, **kwargs: typing.Any) -> int:
  262. try:
  263. return self.connection.recv_into(*args, **kwargs) # type: ignore[no-any-return]
  264. except OpenSSL.SSL.SysCallError as e:
  265. if self.suppress_ragged_eofs and e.args == (-1, "Unexpected EOF"):
  266. return 0
  267. else:
  268. raise OSError(e.args[0], str(e)) from e
  269. except OpenSSL.SSL.ZeroReturnError:
  270. if self.connection.get_shutdown() == OpenSSL.SSL.RECEIVED_SHUTDOWN:
  271. return 0
  272. else:
  273. raise
  274. except OpenSSL.SSL.WantReadError as e:
  275. if not util.wait_for_read(self.socket, self.socket.gettimeout()):
  276. raise timeout("The read operation timed out") from e
  277. else:
  278. return self.recv_into(*args, **kwargs)
  279. # TLS 1.3 post-handshake authentication
  280. except OpenSSL.SSL.Error as e:
  281. raise ssl.SSLError(f"read error: {e!r}") from e
  282. def settimeout(self, timeout: float) -> None:
  283. return self.socket.settimeout(timeout)
  284. def _send_until_done(self, data: bytes) -> int:
  285. while True:
  286. try:
  287. return self.connection.send(data) # type: ignore[no-any-return]
  288. except OpenSSL.SSL.WantWriteError as e:
  289. if not util.wait_for_write(self.socket, self.socket.gettimeout()):
  290. raise timeout() from e
  291. continue
  292. except OpenSSL.SSL.SysCallError as e:
  293. raise OSError(e.args[0], str(e)) from e
  294. def sendall(self, data: bytes) -> None:
  295. total_sent = 0
  296. while total_sent < len(data):
  297. sent = self._send_until_done(
  298. data[total_sent : total_sent + SSL_WRITE_BLOCKSIZE]
  299. )
  300. total_sent += sent
  301. def shutdown(self) -> None:
  302. # FIXME rethrow compatible exceptions should we ever use this
  303. self.connection.shutdown()
  304. def close(self) -> None:
  305. self._closed = True
  306. if self._io_refs <= 0:
  307. self._real_close()
  308. def _real_close(self) -> None:
  309. try:
  310. return self.connection.close() # type: ignore[no-any-return]
  311. except OpenSSL.SSL.Error:
  312. return
  313. def getpeercert(
  314. self, binary_form: bool = False
  315. ) -> dict[str, list[typing.Any]] | None:
  316. x509 = self.connection.get_peer_certificate()
  317. if not x509:
  318. return x509 # type: ignore[no-any-return]
  319. if binary_form:
  320. return OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_ASN1, x509) # type: ignore[no-any-return]
  321. return {
  322. "subject": ((("commonName", x509.get_subject().CN),),), # type: ignore[dict-item]
  323. "subjectAltName": get_subj_alt_name(x509),
  324. }
  325. def version(self) -> str:
  326. return self.connection.get_protocol_version_name() # type: ignore[no-any-return]
  327. WrappedSocket.makefile = socket_cls.makefile # type: ignore[attr-defined]
  328. class PyOpenSSLContext:
  329. """
  330. I am a wrapper class for the PyOpenSSL ``Context`` object. I am responsible
  331. for translating the interface of the standard library ``SSLContext`` object
  332. to calls into PyOpenSSL.
  333. """
  334. def __init__(self, protocol: int) -> None:
  335. self.protocol = _openssl_versions[protocol]
  336. self._ctx = OpenSSL.SSL.Context(self.protocol)
  337. self._options = 0
  338. self.check_hostname = False
  339. self._minimum_version: int = ssl.TLSVersion.MINIMUM_SUPPORTED
  340. self._maximum_version: int = ssl.TLSVersion.MAXIMUM_SUPPORTED
  341. @property
  342. def options(self) -> int:
  343. return self._options
  344. @options.setter
  345. def options(self, value: int) -> None:
  346. self._options = value
  347. self._set_ctx_options()
  348. @property
  349. def verify_mode(self) -> int:
  350. return _openssl_to_stdlib_verify[self._ctx.get_verify_mode()]
  351. @verify_mode.setter
  352. def verify_mode(self, value: ssl.VerifyMode) -> None:
  353. self._ctx.set_verify(_stdlib_to_openssl_verify[value], _verify_callback)
  354. def set_default_verify_paths(self) -> None:
  355. self._ctx.set_default_verify_paths()
  356. def set_ciphers(self, ciphers: bytes | str) -> None:
  357. if isinstance(ciphers, str):
  358. ciphers = ciphers.encode("utf-8")
  359. self._ctx.set_cipher_list(ciphers)
  360. def load_verify_locations(
  361. self,
  362. cafile: str | None = None,
  363. capath: str | None = None,
  364. cadata: bytes | None = None,
  365. ) -> None:
  366. if cafile is not None:
  367. cafile = cafile.encode("utf-8") # type: ignore[assignment]
  368. if capath is not None:
  369. capath = capath.encode("utf-8") # type: ignore[assignment]
  370. try:
  371. self._ctx.load_verify_locations(cafile, capath)
  372. if cadata is not None:
  373. self._ctx.load_verify_locations(BytesIO(cadata))
  374. except OpenSSL.SSL.Error as e:
  375. raise ssl.SSLError(f"unable to load trusted certificates: {e!r}") from e
  376. def load_cert_chain(
  377. self,
  378. certfile: str,
  379. keyfile: str | None = None,
  380. password: str | None = None,
  381. ) -> None:
  382. try:
  383. self._ctx.use_certificate_chain_file(certfile)
  384. if password is not None:
  385. if not isinstance(password, bytes):
  386. password = password.encode("utf-8") # type: ignore[assignment]
  387. self._ctx.set_passwd_cb(lambda *_: password)
  388. self._ctx.use_privatekey_file(keyfile or certfile)
  389. except OpenSSL.SSL.Error as e:
  390. raise ssl.SSLError(f"Unable to load certificate chain: {e!r}") from e
  391. def set_alpn_protocols(self, protocols: list[bytes | str]) -> None:
  392. protocols = [util.util.to_bytes(p, "ascii") for p in protocols]
  393. return self._ctx.set_alpn_protos(protocols) # type: ignore[no-any-return]
  394. def wrap_socket(
  395. self,
  396. sock: socket_cls,
  397. server_side: bool = False,
  398. do_handshake_on_connect: bool = True,
  399. suppress_ragged_eofs: bool = True,
  400. server_hostname: bytes | str | None = None,
  401. ) -> WrappedSocket:
  402. cnx = OpenSSL.SSL.Connection(self._ctx, sock)
  403. # If server_hostname is an IP, don't use it for SNI, per RFC6066 Section 3
  404. if server_hostname and not util.ssl_.is_ipaddress(server_hostname):
  405. if isinstance(server_hostname, str):
  406. server_hostname = server_hostname.encode("utf-8")
  407. cnx.set_tlsext_host_name(server_hostname)
  408. cnx.set_connect_state()
  409. while True:
  410. try:
  411. cnx.do_handshake()
  412. except OpenSSL.SSL.WantReadError as e:
  413. if not util.wait_for_read(sock, sock.gettimeout()):
  414. raise timeout("select timed out") from e
  415. continue
  416. except OpenSSL.SSL.Error as e:
  417. raise ssl.SSLError(f"bad handshake: {e!r}") from e
  418. break
  419. return WrappedSocket(cnx, sock)
  420. def _set_ctx_options(self) -> None:
  421. self._ctx.set_options(
  422. self._options
  423. | _openssl_to_ssl_minimum_version[self._minimum_version]
  424. | _openssl_to_ssl_maximum_version[self._maximum_version]
  425. )
  426. @property
  427. def minimum_version(self) -> int:
  428. return self._minimum_version
  429. @minimum_version.setter
  430. def minimum_version(self, minimum_version: int) -> None:
  431. self._minimum_version = minimum_version
  432. self._set_ctx_options()
  433. @property
  434. def maximum_version(self) -> int:
  435. return self._maximum_version
  436. @maximum_version.setter
  437. def maximum_version(self, maximum_version: int) -> None:
  438. self._maximum_version = maximum_version
  439. self._set_ctx_options()
  440. def _verify_callback(
  441. cnx: OpenSSL.SSL.Connection,
  442. x509: X509,
  443. err_no: int,
  444. err_depth: int,
  445. return_code: int,
  446. ) -> bool:
  447. return err_no == 0