2TLS with SNI_-support for Python 2. Follow these instructions if you would
3like to verify TLS certificates in Python 2. Note, the default libraries do
4*not* do certificate checking; you need to do additional work to validate
7This needs the following packages installed:
9* `pyOpenSSL`_ (tested with 16.0.0)
10* `cryptography`_ (minimum 1.3.4, from pyopenssl)
11* `idna`_ (minimum 2.0, from cryptography)
13However, pyopenssl depends on cryptography, which depends on idna, so while we
14use all three directly here we end up having relatively few packages required.
16You can install them with the following command:
20 $ python -m pip install pyopenssl cryptography idna
22To activate certificate checking, call
23:func:`~urllib3.contrib.pyopenssl.inject_into_urllib3` from your Python code
24before you begin making HTTP requests. This can be done in a ``sitecustomize``
25module, or at any other time before your application begins using ``urllib3``,
31 import pip._vendor.urllib3.contrib.pyopenssl as pyopenssl
32 pyopenssl.inject_into_urllib3()
36Now you can use :mod:`urllib3` as you normally would, and it will support SNI
37when the required modules are installed.
39Activating this module also has the positive side effect of disabling SSL/TLS
40compression in Python 2 (see `CRIME attack`_).
42.. _sni: https://en.wikipedia.org/wiki/Server_Name_Indication
43.. _crime attack: https://en.wikipedia.org/wiki/CRIME_(security_exploit)
44.. _pyopenssl: https://www.pyopenssl.org
45.. _cryptography: https://cryptography.io
46.. _idna: https://github.com/kjd/idna
48from __future__
import absolute_import
52from cryptography
import x509
64from socket
import error
as SocketError
65from socket
import timeout
68 from socket
import _fileobject
79from ..packages
import six
80from ..
util.ssl_ import PROTOCOL_TLS_CLIENT
83 "'urllib3.contrib.pyopenssl' module is deprecated and will be removed "
84 "in a future release of urllib3 2.x. Read more in this issue: "
85 "https://github.com/urllib3/urllib3/issues/2680",
86 category=DeprecationWarning,
90__all__ = [
"inject_into_urllib3",
"extract_from_urllib3"]
112_stdlib_to_openssl_verify = {
121SSL_WRITE_BLOCKSIZE = 16384
131 "Monkey-patch urllib3 with PyOpenSSL-backed SSL-support."
144 "Undo monkey-patching by :func:`inject_into_urllib3`."
156 Verifies that PyOpenSSL's package-level dependencies have been met.
157 Throws `ImportError` if they are not met.
162 if getattr(Extensions,
"get_extension_for_class",
None)
is None:
164 "'cryptography' module missing required functionality. "
165 "Try upgrading to v1.3.4 or newer."
173 if getattr(x509,
"_x509",
None)
is None:
175 "'pyOpenSSL' module missing required functionality. "
176 "Try upgrading to v0.14 or newer."
182 Converts a dNSName SubjectAlternativeName field to the form used by the
183 standard library on the given Python version.
185 Cryptography produces a dNSName as a unicode string that was idna-decoded
186 from ASCII bytes. We need to idna-encode that string to get it back, and
187 then on Python 3 we also need to convert to unicode via UTF-8 (the stdlib
188 uses PyUnicode_FromStringAndSize on it, which decodes via UTF-8).
190 If the name cannot be idna-encoded then we return None signalling that
191 the name given should be skipped.
196 Borrowed wholesale from the Python Cryptography Project. It turns out
197 that we can't just safely call `idna.encode`: it can explode for
198 wildcard names. This avoids that problem.
203 for prefix
in [
u"*.",
u"."]:
205 name = name[
len(prefix) :]
225 Given an PyOpenSSL certificate, provides all the subject alternative names.
228 if hasattr(peer_cert,
"to_cryptography"):
243 UnsupportedExtension,
250 "A problem was encountered with the certificate that prevented "
251 "urllib3 from finding the SubjectAlternativeName field. This can "
252 "affect certificate validation. The error was %s",
277 """API-compatibility wrapper for Python OpenSSL's Connection-class.
279 Note: _makefile_refs, _drop() and _reuse() are needed for the garbage
283 def __init__(self, connection, socket, suppress_ragged_eofs=True):
300 def recv(self, *args, **kwargs):
315 raise timeout(
"The read operation timed out")
317 return self.
recv(*args, **kwargs)
340 raise timeout(
"The read operation timed out")
364 while total_sent <
len(data):
366 data[total_sent : total_sent + SSL_WRITE_BLOCKSIZE]
414 self._makefile_refs += 1
415 return _fileobject(self, mode, bufsize, close=
True)
418 makefile = backport_makefile
425 I am a wrapper class for the PyOpenSSL ``Context`` object. I am responsible
426 for translating the interface of the standard library ``SSLContext`` object
427 to calls into PyOpenSSL.
441 def options(self, value):
451 self.
_ctx.
set_verify(_stdlib_to_openssl_verify[value], _verify_callback)
462 if cafile
is not None:
464 if capath
is not None:
468 if cadata
is not None:
471 raise ssl.SSLError(
"unable to load trusted certificates: %r" % e)
475 if password
is not None:
489 do_handshake_on_connect=True,
490 suppress_ragged_eofs=True,
491 server_hostname=None,
498 if server_hostname
is not None:
508 raise timeout(
"select timed out")
set_ciphers(self, ciphers)
set_default_verify_paths(self)
load_cert_chain(self, certfile, keyfile=None, password=None)
load_verify_locations(self, cafile=None, capath=None, cadata=None)
set_alpn_protocols(self, protocols)
recv(self, *args, **kwargs)
__init__(self, connection, socket, suppress_ragged_eofs=True)
getpeercert(self, binary_form=False)
recv_into(self, *args, **kwargs)
_send_until_done(self, data)
settimeout(self, timeout)
_validate_dependencies_met()
get_subj_alt_name(peer_cert)
_verify_callback(cnx, x509, err_no, err_depth, return_code)