You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssl_.py 14KB

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