Development of an internal social media platform with personalised dashboards for students
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

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