ssl_.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. from __future__ import absolute_import
  2. import errno
  3. import warnings
  4. import hmac
  5. import os
  6. import sys
  7. from binascii import hexlify, unhexlify
  8. from hashlib import md5, sha1, sha256
  9. from .url import IPV4_RE, BRACELESS_IPV6_ADDRZ_RE
  10. from ..exceptions import SSLError, InsecurePlatformWarning, SNIMissingWarning
  11. from ..packages import six
  12. SSLContext = None
  13. HAS_SNI = False
  14. IS_PYOPENSSL = False
  15. IS_SECURETRANSPORT = False
  16. # Maps the length of a digest to a possible hash function producing this digest
  17. HASHFUNC_MAP = {32: md5, 40: sha1, 64: sha256}
  18. def _const_compare_digest_backport(a, b):
  19. """
  20. Compare two digests of equal length in constant time.
  21. The digests must be of type str/bytes.
  22. Returns True if the digests match, and False otherwise.
  23. """
  24. result = abs(len(a) - len(b))
  25. for left, right in zip(bytearray(a), bytearray(b)):
  26. result |= left ^ right
  27. return result == 0
  28. _const_compare_digest = getattr(hmac, "compare_digest", _const_compare_digest_backport)
  29. try: # Test for SSL features
  30. import ssl
  31. from ssl import wrap_socket, CERT_REQUIRED
  32. from ssl import HAS_SNI # Has SNI?
  33. except ImportError:
  34. pass
  35. try: # Platform-specific: Python 3.6
  36. from ssl import PROTOCOL_TLS
  37. PROTOCOL_SSLv23 = PROTOCOL_TLS
  38. except ImportError:
  39. try:
  40. from ssl import PROTOCOL_SSLv23 as PROTOCOL_TLS
  41. PROTOCOL_SSLv23 = PROTOCOL_TLS
  42. except ImportError:
  43. PROTOCOL_SSLv23 = PROTOCOL_TLS = 2
  44. try:
  45. from ssl import OP_NO_SSLv2, OP_NO_SSLv3, OP_NO_COMPRESSION
  46. except ImportError:
  47. OP_NO_SSLv2, OP_NO_SSLv3 = 0x1000000, 0x2000000
  48. OP_NO_COMPRESSION = 0x20000
  49. # A secure default.
  50. # Sources for more information on TLS ciphers:
  51. #
  52. # - https://wiki.mozilla.org/Security/Server_Side_TLS
  53. # - https://www.ssllabs.com/projects/best-practices/index.html
  54. # - https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
  55. #
  56. # The general intent is:
  57. # - prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE),
  58. # - prefer ECDHE over DHE for better performance,
  59. # - prefer any AES-GCM and ChaCha20 over any AES-CBC for better performance and
  60. # security,
  61. # - prefer AES-GCM over ChaCha20 because hardware-accelerated AES is common,
  62. # - disable NULL authentication, MD5 MACs, DSS, and other
  63. # insecure ciphers for security reasons.
  64. # - NOTE: TLS 1.3 cipher suites are managed through a different interface
  65. # not exposed by CPython (yet!) and are enabled by default if they're available.
  66. DEFAULT_CIPHERS = ":".join(
  67. [
  68. "ECDHE+AESGCM",
  69. "ECDHE+CHACHA20",
  70. "DHE+AESGCM",
  71. "DHE+CHACHA20",
  72. "ECDH+AESGCM",
  73. "DH+AESGCM",
  74. "ECDH+AES",
  75. "DH+AES",
  76. "RSA+AESGCM",
  77. "RSA+AES",
  78. "!aNULL",
  79. "!eNULL",
  80. "!MD5",
  81. "!DSS",
  82. ]
  83. )
  84. try:
  85. from ssl import SSLContext # Modern SSL?
  86. except ImportError:
  87. class SSLContext(object): # Platform-specific: Python 2
  88. def __init__(self, protocol_version):
  89. self.protocol = protocol_version
  90. # Use default values from a real SSLContext
  91. self.check_hostname = False
  92. self.verify_mode = ssl.CERT_NONE
  93. self.ca_certs = None
  94. self.options = 0
  95. self.certfile = None
  96. self.keyfile = None
  97. self.ciphers = None
  98. def load_cert_chain(self, certfile, keyfile):
  99. self.certfile = certfile
  100. self.keyfile = keyfile
  101. def load_verify_locations(self, cafile=None, capath=None, cadata=None):
  102. self.ca_certs = cafile
  103. if capath is not None:
  104. raise SSLError("CA directories not supported in older Pythons")
  105. if cadata is not None:
  106. raise SSLError("CA data not supported in older Pythons")
  107. def set_ciphers(self, cipher_suite):
  108. self.ciphers = cipher_suite
  109. def wrap_socket(self, socket, server_hostname=None, server_side=False):
  110. warnings.warn(
  111. "A true SSLContext object is not available. This prevents "
  112. "urllib3 from configuring SSL appropriately and may cause "
  113. "certain SSL connections to fail. You can upgrade to a newer "
  114. "version of Python to solve this. For more information, see "
  115. "https://urllib3.readthedocs.io/en/latest/advanced-usage.html"
  116. "#ssl-warnings",
  117. InsecurePlatformWarning,
  118. )
  119. kwargs = {
  120. "keyfile": self.keyfile,
  121. "certfile": self.certfile,
  122. "ca_certs": self.ca_certs,
  123. "cert_reqs": self.verify_mode,
  124. "ssl_version": self.protocol,
  125. "server_side": server_side,
  126. }
  127. return wrap_socket(socket, ciphers=self.ciphers, **kwargs)
  128. def assert_fingerprint(cert, fingerprint):
  129. """
  130. Checks if given fingerprint matches the supplied certificate.
  131. :param cert:
  132. Certificate as bytes object.
  133. :param fingerprint:
  134. Fingerprint as string of hexdigits, can be interspersed by colons.
  135. """
  136. fingerprint = fingerprint.replace(":", "").lower()
  137. digest_length = len(fingerprint)
  138. hashfunc = HASHFUNC_MAP.get(digest_length)
  139. if not hashfunc:
  140. raise SSLError("Fingerprint of invalid length: {0}".format(fingerprint))
  141. # We need encode() here for py32; works on py2 and p33.
  142. fingerprint_bytes = unhexlify(fingerprint.encode())
  143. cert_digest = hashfunc(cert).digest()
  144. if not _const_compare_digest(cert_digest, fingerprint_bytes):
  145. raise SSLError(
  146. 'Fingerprints did not match. Expected "{0}", got "{1}".'.format(
  147. fingerprint, hexlify(cert_digest)
  148. )
  149. )
  150. def resolve_cert_reqs(candidate):
  151. """
  152. Resolves the argument to a numeric constant, which can be passed to
  153. the wrap_socket function/method from the ssl module.
  154. Defaults to :data:`ssl.CERT_REQUIRED`.
  155. If given a string it is assumed to be the name of the constant in the
  156. :mod:`ssl` module or its abbreviation.
  157. (So you can specify `REQUIRED` instead of `CERT_REQUIRED`.
  158. If it's neither `None` nor a string we assume it is already the numeric
  159. constant which can directly be passed to wrap_socket.
  160. """
  161. if candidate is None:
  162. return CERT_REQUIRED
  163. if isinstance(candidate, str):
  164. res = getattr(ssl, candidate, None)
  165. if res is None:
  166. res = getattr(ssl, "CERT_" + candidate)
  167. return res
  168. return candidate
  169. def resolve_ssl_version(candidate):
  170. """
  171. like resolve_cert_reqs
  172. """
  173. if candidate is None:
  174. return PROTOCOL_TLS
  175. if isinstance(candidate, str):
  176. res = getattr(ssl, candidate, None)
  177. if res is None:
  178. res = getattr(ssl, "PROTOCOL_" + candidate)
  179. return res
  180. return candidate
  181. def create_urllib3_context(
  182. ssl_version=None, cert_reqs=None, options=None, ciphers=None
  183. ):
  184. """All arguments have the same meaning as ``ssl_wrap_socket``.
  185. By default, this function does a lot of the same work that
  186. ``ssl.create_default_context`` does on Python 3.4+. It:
  187. - Disables SSLv2, SSLv3, and compression
  188. - Sets a restricted set of server ciphers
  189. If you wish to enable SSLv3, you can do::
  190. from urllib3.util import ssl_
  191. context = ssl_.create_urllib3_context()
  192. context.options &= ~ssl_.OP_NO_SSLv3
  193. You can do the same to enable compression (substituting ``COMPRESSION``
  194. for ``SSLv3`` in the last line above).
  195. :param ssl_version:
  196. The desired protocol version to use. This will default to
  197. PROTOCOL_SSLv23 which will negotiate the highest protocol that both
  198. the server and your installation of OpenSSL support.
  199. :param cert_reqs:
  200. Whether to require the certificate verification. This defaults to
  201. ``ssl.CERT_REQUIRED``.
  202. :param options:
  203. Specific OpenSSL options. These default to ``ssl.OP_NO_SSLv2``,
  204. ``ssl.OP_NO_SSLv3``, ``ssl.OP_NO_COMPRESSION``.
  205. :param ciphers:
  206. Which cipher suites to allow the server to select.
  207. :returns:
  208. Constructed SSLContext object with specified options
  209. :rtype: SSLContext
  210. """
  211. context = SSLContext(ssl_version or PROTOCOL_TLS)
  212. context.set_ciphers(ciphers or DEFAULT_CIPHERS)
  213. # Setting the default here, as we may have no ssl module on import
  214. cert_reqs = ssl.CERT_REQUIRED if cert_reqs is None else cert_reqs
  215. if options is None:
  216. options = 0
  217. # SSLv2 is easily broken and is considered harmful and dangerous
  218. options |= OP_NO_SSLv2
  219. # SSLv3 has several problems and is now dangerous
  220. options |= OP_NO_SSLv3
  221. # Disable compression to prevent CRIME attacks for OpenSSL 1.0+
  222. # (issue #309)
  223. options |= OP_NO_COMPRESSION
  224. context.options |= options
  225. # Enable post-handshake authentication for TLS 1.3, see GH #1634. PHA is
  226. # necessary for conditional client cert authentication with TLS 1.3.
  227. # The attribute is None for OpenSSL <= 1.1.0 or does not exist in older
  228. # versions of Python. We only enable on Python 3.7.4+ or if certificate
  229. # verification is enabled to work around Python issue #37428
  230. # See: https://bugs.python.org/issue37428
  231. if (cert_reqs == ssl.CERT_REQUIRED or sys.version_info >= (3, 7, 4)) and getattr(
  232. context, "post_handshake_auth", None
  233. ) is not None:
  234. context.post_handshake_auth = True
  235. context.verify_mode = cert_reqs
  236. if (
  237. getattr(context, "check_hostname", None) is not None
  238. ): # Platform-specific: Python 3.2
  239. # We do our own verification, including fingerprints and alternative
  240. # hostnames. So disable it here
  241. context.check_hostname = False
  242. # Enable logging of TLS session keys via defacto standard environment variable
  243. # 'SSLKEYLOGFILE', if the feature is available (Python 3.8+). Skip empty values.
  244. if hasattr(context, "keylog_filename"):
  245. sslkeylogfile = os.environ.get("SSLKEYLOGFILE")
  246. if sslkeylogfile:
  247. context.keylog_filename = sslkeylogfile
  248. return context
  249. def ssl_wrap_socket(
  250. sock,
  251. keyfile=None,
  252. certfile=None,
  253. cert_reqs=None,
  254. ca_certs=None,
  255. server_hostname=None,
  256. ssl_version=None,
  257. ciphers=None,
  258. ssl_context=None,
  259. ca_cert_dir=None,
  260. key_password=None,
  261. ca_cert_data=None,
  262. ):
  263. """
  264. All arguments except for server_hostname, ssl_context, and ca_cert_dir have
  265. the same meaning as they do when using :func:`ssl.wrap_socket`.
  266. :param server_hostname:
  267. When SNI is supported, the expected hostname of the certificate
  268. :param ssl_context:
  269. A pre-made :class:`SSLContext` object. If none is provided, one will
  270. be created using :func:`create_urllib3_context`.
  271. :param ciphers:
  272. A string of ciphers we wish the client to support.
  273. :param ca_cert_dir:
  274. A directory containing CA certificates in multiple separate files, as
  275. supported by OpenSSL's -CApath flag or the capath argument to
  276. SSLContext.load_verify_locations().
  277. :param key_password:
  278. Optional password if the keyfile is encrypted.
  279. :param ca_cert_data:
  280. Optional string containing CA certificates in PEM format suitable for
  281. passing as the cadata parameter to SSLContext.load_verify_locations()
  282. """
  283. context = ssl_context
  284. if context is None:
  285. # Note: This branch of code and all the variables in it are no longer
  286. # used by urllib3 itself. We should consider deprecating and removing
  287. # this code.
  288. context = create_urllib3_context(ssl_version, cert_reqs, ciphers=ciphers)
  289. if ca_certs or ca_cert_dir or ca_cert_data:
  290. try:
  291. context.load_verify_locations(ca_certs, ca_cert_dir, ca_cert_data)
  292. except IOError as e: # Platform-specific: Python 2.7
  293. raise SSLError(e)
  294. # Py33 raises FileNotFoundError which subclasses OSError
  295. # These are not equivalent unless we check the errno attribute
  296. except OSError as e: # Platform-specific: Python 3.3 and beyond
  297. if e.errno == errno.ENOENT:
  298. raise SSLError(e)
  299. raise
  300. elif ssl_context is None and hasattr(context, "load_default_certs"):
  301. # try to load OS default certs; works well on Windows (require Python3.4+)
  302. context.load_default_certs()
  303. # Attempt to detect if we get the goofy behavior of the
  304. # keyfile being encrypted and OpenSSL asking for the
  305. # passphrase via the terminal and instead error out.
  306. if keyfile and key_password is None and _is_key_file_encrypted(keyfile):
  307. raise SSLError("Client private key is encrypted, password is required")
  308. if certfile:
  309. if key_password is None:
  310. context.load_cert_chain(certfile, keyfile)
  311. else:
  312. context.load_cert_chain(certfile, keyfile, key_password)
  313. # If we detect server_hostname is an IP address then the SNI
  314. # extension should not be used according to RFC3546 Section 3.1
  315. # We shouldn't warn the user if SNI isn't available but we would
  316. # not be using SNI anyways due to IP address for server_hostname.
  317. if (
  318. server_hostname is not None and not is_ipaddress(server_hostname)
  319. ) or IS_SECURETRANSPORT:
  320. if HAS_SNI and server_hostname is not None:
  321. return context.wrap_socket(sock, server_hostname=server_hostname)
  322. warnings.warn(
  323. "An HTTPS request has been made, but the SNI (Server Name "
  324. "Indication) extension to TLS is not available on this platform. "
  325. "This may cause the server to present an incorrect TLS "
  326. "certificate, which can cause validation failures. You can upgrade to "
  327. "a newer version of Python to solve this. For more information, see "
  328. "https://urllib3.readthedocs.io/en/latest/advanced-usage.html"
  329. "#ssl-warnings",
  330. SNIMissingWarning,
  331. )
  332. return context.wrap_socket(sock)
  333. def is_ipaddress(hostname):
  334. """Detects whether the hostname given is an IPv4 or IPv6 address.
  335. Also detects IPv6 addresses with Zone IDs.
  336. :param str hostname: Hostname to examine.
  337. :return: True if the hostname is an IP address, False otherwise.
  338. """
  339. if not six.PY2 and isinstance(hostname, bytes):
  340. # IDN A-label bytes are ASCII compatible.
  341. hostname = hostname.decode("ascii")
  342. return bool(IPV4_RE.match(hostname) or BRACELESS_IPV6_ADDRZ_RE.match(hostname))
  343. def _is_key_file_encrypted(key_file):
  344. """Detects if a key file is encrypted or not."""
  345. with open(key_file, "r") as f:
  346. for line in f:
  347. # Look for Proc-Type: 4,ENCRYPTED
  348. if "ENCRYPTED" in line:
  349. return True
  350. return False