Viewing File: /usr/lib/python3.6/site-packages/pip/_vendor/urllib3/contrib/__pycache__/pyopenssl.cpython-36.pyc

3

aj;@s,dZddlmZddlZddlmZddlmZ	ddl
mZddlm
Z
mZddlmZydd	lmZWn$ek
rdZd
dlmZYnXddlZddlZd
dlmZddlZd
d
lmZddgZdZejej j!ej"ej j#iZ$e%edoe%ej drej j&e$ej'<e%edr0e%ej dr0ej j(e$ej)<ye$j*ej+ej j,iWne-k
r^YnXej.ej j/ej0ej j1ej2ej j1ej j3iZ4e5dde4j6DZ7dZ8ejZ9ej:j;Z<ej=e>Z?ddZ@ddZAddZBddZCddZDGd d!d!eEZFerd*d#d$ZGneZGeGeF_GGd%d&d&eEZHd'd(ZIdS)+ab
SSL with SNI_-support for Python 2. Follow these instructions if you would
like to verify SSL certificates in Python 2. Note, the default libraries do
*not* do certificate checking; you need to do additional work to validate
certificates yourself.

This needs the following packages installed:

* pyOpenSSL (tested with 16.0.0)
* cryptography (minimum 1.3.4, from pyopenssl)
* idna (minimum 2.0, from cryptography)

However, pyopenssl depends on cryptography, which depends on idna, so while we
use all three directly here we end up having relatively few packages required.

You can install them with the following command:

    pip install pyopenssl cryptography idna

To activate certificate checking, call
:func:`~urllib3.contrib.pyopenssl.inject_into_urllib3` from your Python code
before you begin making HTTP requests. This can be done in a ``sitecustomize``
module, or at any other time before your application begins using ``urllib3``,
like this::

    try:
        import urllib3.contrib.pyopenssl
        urllib3.contrib.pyopenssl.inject_into_urllib3()
    except ImportError:
        pass

Now you can use :mod:`urllib3` as you normally would, and it will support SNI
when the required modules are installed.

Activating this module also has the positive side effect of disabling SSL/TLS
compression in Python 2 (see `CRIME attack`_).

If you want to configure the default list of supported cipher suites, you can
set the ``urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST`` variable.

.. _sni: https://en.wikipedia.org/wiki/Server_Name_Indication
.. _crime attack: https://en.wikipedia.org/wiki/CRIME_(security_exploit)
)absolute_importN)x509)backend)_Certificate)timeouterror)BytesIO)_fileobject)backport_makefile)six)utilinject_into_urllib3extract_from_urllib3TPROTOCOL_TLSv1_1TLSv1_1_METHODPROTOCOL_TLSv1_2TLSv1_2_METHODccs|]\}}||fVqdS)N).0kvrr/usr/lib/python3.6/pyopenssl.py	<genexpr>`sri@cCs.tttj_tt_ttj_dt_dtj_dS)z7Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.TN)_validate_dependencies_metPyOpenSSLContextr
ssl_
SSLContextHAS_SNIIS_PYOPENSSLrrrrrmscCs(ttj_tt_ttj_dt_dtj_dS)z4Undo monkey-patching by :func:`inject_into_urllib3`.FN)orig_util_SSLContextr
rrorig_util_HAS_SNIrrrrrrrys
cCsRddlm}t|dddkr$tdddlm}|}t|dddkrNtddS)	z{
    Verifies that PyOpenSSL's package-level dependencies have been met.
    Throws `ImportError` if they are not met.
    r)
Extensionsget_extension_for_classNzX'cryptography' module missing required functionality.  Try upgrading to v1.3.4 or newer.)X509_x509zS'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer.)Zcryptography.x509.extensionsr"getattrImportErrorZOpenSSL.cryptor$)r"r$rrrrrsrcCs(dd}||}tjdkr$|jd}|S)a
    Converts a dNSName SubjectAlternativeName field to the form used by the
    standard library on the given Python version.

    Cryptography produces a dNSName as a unicode string that was idna-decoded
    from ASCII bytes. We need to idna-encode that string to get it back, and
    then on Python 3 we also need to convert to unicode via UTF-8 (the stdlib
    uses PyUnicode_FromStringAndSize on it, which decodes via UTF-8).
    cSsNddl}x:dD]2}|j|r|t|d}|jd|j|SqW|j|S)z
        Borrowed wholesale from the Python Cryptography Project. It turns out
        that we can't just safely call `idna.encode`: it can explode for
        wildcard names. This avoids that problem.
        rN*..ascii)r(r))idna
startswithlenencode)namer+prefixrrridna_encodes

z'_dnsname_to_stdlib.<locals>.idna_encoderzutf-8)r2r)sysversion_infodecode)r/r1rrr_dnsname_to_stdlibs



r6cCst|dr|j}ntt|j}y|jjtjj	}WnNtj
k
rJgStjtjtj
tfk
r}ztjd|gSd}~XnXdd|jtjD}|jdd|jtjD|S)zU
    Given an PyOpenSSL certificate, provides all the subject alternative names.
    to_cryptographyzA problem was encountered with the certificate that prevented urllib3 from finding the SubjectAlternativeName field. This can affect certificate validation. The error was %sNcSsg|]}dt|fqS)ZDNS)r6)rr/rrr
<listcomp>sz%get_subj_alt_name.<locals>.<listcomp>css|]}dt|fVqdS)z
IP AddressN)str)rr/rrrrsz$get_subj_alt_name.<locals>.<genexpr>)hasattrr7ropenssl_backendr%
extensionsr#rZSubjectAlternativeNamevalueZExtensionNotFoundZDuplicateExtensionZUnsupportedExtensionZUnsupportedGeneralNameTypeUnicodeErrorlogZwarningZget_values_for_typeZDNSNameextendZ	IPAddress)Z	peer_certZcertZextenamesrrrget_subj_alt_names(


	rCc@s|eZdZdZdddZddZddZd	d
ZddZd
dZ	ddZ
ddZddZddZ
d ddZddZddZdS)!
WrappedSocketzAPI-compatibility wrapper for Python OpenSSL's Connection-class.

    Note: _makefile_refs, _drop() and _reuse() are needed for the garbage
    collector of pypy.
    TcCs"||_||_||_d|_d|_dS)NrF)
connectionsocketsuppress_ragged_eofs_makefile_refs_closed)selfrErFrGrrr__init__s
zWrappedSocket.__init__cCs
|jjS)N)rFfileno)rJrrrrLszWrappedSocket.filenocCs*|jdkr|jd8_|jr&|jdS)Nr)rHrIclose)rJrrr_decref_socketioss
zWrappedSocket._decref_socketioscOsy|jj||}Wntjjk
rX}z&|jr<|jdkr<dStt|WYdd}~Xntjj	k
r}z|jj
tjjkrdSWYdd}~XnJtjjk
rt
j|j|jj}|stdn|j||SYnX|SdS)NrMUnexpected EOFzThe read operation timed out)rRrP)rErecvOpenSSLSSLSysCallErrorrGargsSocketErrorr9ZeroReturnErrorget_shutdownRECEIVED_SHUTDOWN
WantReadErrorr

wait_for_readrF
gettimeoutr)rJrWkwargsdatarArdrrrrSs 
zWrappedSocket.recvcOsy|jj||Stjjk
rT}z&|jr8|jdkr8dStt|WYdd}~Xntjj	k
r}z|jj
tjjkr~dSWYdd}~XnFtjjk
rt
j|j|jj}|stdn|j||SYnXdS)NrMUnexpected EOFrzThe read operation timed outrR)rRrb)rE	recv_intorTrUrVrGrWrXr9rYrZr[r\r
r]rFr^r)rJrWr_rArarrrrcs
zWrappedSocket.recv_intocCs|jj|S)N)rF
settimeout)rJrrrrrd*szWrappedSocket.settimeoutcCsxzy|jj|Stjjk
rFtj|j|jj}|s@t	wYqtjj
k
rv}ztt|WYdd}~XqXqWdS)N)
rEsendrTrUZWantWriteErrorr
Zwait_for_writerFr^rrVrXr9)rJr`wrrArrr_send_until_done-szWrappedSocket._send_until_donecCs8d}x.|t|kr2|j|||t}||7}qWdS)Nr)r-rgSSL_WRITE_BLOCKSIZE)rJr`Z
total_sentZsentrrrsendall9szWrappedSocket.sendallcCs|jjdS)N)rEshutdown)rJrrrrj?szWrappedSocket.shutdowncCsH|jdkr6yd|_|jjStjjk
r2dSXn|jd8_dS)NrMT)rHrIrErNrTrUError)rJrrrrNCs

zWrappedSocket.closeFcCsD|jj}|s|S|r(tjjtjj|Sd|jjffft|dS)NZ
commonName)ZsubjectZsubjectAltName)	rEZget_peer_certificaterTZcryptoZdump_certificateZ
FILETYPE_ASN1Zget_subjectZCNrC)rJZbinary_formrrrrgetpeercertMs
zWrappedSocket.getpeercertcCs|jd7_dS)NrM)rH)rJrrr_reuse_szWrappedSocket._reusecCs&|jdkr|jn|jd8_dS)NrM)rHrN)rJrrr_dropbs

zWrappedSocket._dropN)T)F)__name__
__module____qualname____doc__rKrLrOrSrcrdrgrirjrNrlrmrnrrrrrDs


rDrMcCs|jd7_t|||ddS)NrMT)rN)rHr	)rJmodebufsizerrrmakefilejsruc@szeZdZdZddZeddZejddZeddZejd	dZd
dZ	dd
Z
dddZdddZdddZ
dS)rz
    I am a wrapper class for the PyOpenSSL ``Context`` object. I am responsible
    for translating the interface of the standard library ``SSLContext`` object
    to calls into PyOpenSSL.
    cCs*t||_tjj|j|_d|_d|_dS)NrF)_openssl_versionsprotocolrTrUZContext_ctx_optionsZcheck_hostname)rJrwrrrrKys
zPyOpenSSLContext.__init__cCs|jS)N)ry)rJrrroptionsszPyOpenSSLContext.optionscCs||_|jj|dS)N)ryrxZset_options)rJr=rrrrzscCst|jjS)N)_openssl_to_stdlib_verifyrxZget_verify_mode)rJrrrverify_modeszPyOpenSSLContext.verify_modecCs|jjt|tdS)N)rxZ
set_verify_stdlib_to_openssl_verify_verify_callback)rJr=rrrr|scCs|jjdS)N)rxset_default_verify_paths)rJrrrrsz)PyOpenSSLContext.set_default_verify_pathscCs&t|tjr|jd}|jj|dS)Nzutf-8)
isinstancer	text_typer.rxZset_cipher_list)rJZciphersrrrset_cipherss
zPyOpenSSLContext.set_ciphersNcCsN|dk	r|jd}|dk	r$|jd}|jj|||dk	rJ|jjt|dS)Nzutf-8)r.rxload_verify_locationsr)rJZcafileZcapathZcadatarrrrs

z&PyOpenSSLContext.load_verify_locationscs<|jj|dk	r(|jjfdd|jj|p4|dS)NcsS)Nr)Z
max_lengthZprompt_twiceZuserdata)passwordrr<lambda>sz2PyOpenSSLContext.load_cert_chain.<locals>.<lambda>)rxZuse_certificate_fileZ
set_passwd_cbZuse_privatekey_file)rJZcertfileZkeyfilerr)rrload_cert_chainsz PyOpenSSLContext.load_cert_chainFTc	Cstjj|j|}t|tjr&|jd}|dk	r8|j||j	x|y|j
Wnhtjjk
rtj
||j}|s~tdwBYn4tjjk
r}ztjd|WYdd}~XnXPqBWt||S)Nzutf-8zselect timed outzbad handshake: %r)rTrUZ
Connectionrxrrrr.Zset_tlsext_host_nameZset_connect_stateZdo_handshaker\r
r]r^rrksslZSSLErrorrD)	rJZsockZserver_sideZdo_handshake_on_connectrGZserver_hostnamecnxrarArrrwrap_sockets$

 zPyOpenSSLContext.wrap_socket)NNN)NN)FTTN)rorprqrrrKpropertyrzsetterr|rrrrrrrrrrss
	
rcCs|dkS)Nrr)rrZerr_noZ	err_depthZreturn_coderrrr~sr~rR)rR)JrrZ
__future__rZOpenSSL.SSLrTZcryptographyrZ$cryptography.hazmat.backends.opensslrr;Z)cryptography.hazmat.backends.openssl.x509rrFrrrXiorr	r'Zpackages.backports.makefilerZloggingrZpackagesrr3r
__all__rZPROTOCOL_SSLv23rUZ
SSLv23_METHODZPROTOCOL_TLSv1ZTLSv1_METHODrvr:rrrrupdateZPROTOCOL_SSLv3ZSSLv3_METHODAttributeErrorZ	CERT_NONEZVERIFY_NONEZ
CERT_OPTIONALZVERIFY_PEERZ
CERT_REQUIREDZVERIFY_FAIL_IF_NO_PEER_CERTr}dictitemsr{rhr!rrr Z	getLoggerror?rrrr6rCobjectrDrurr~rrrr<module>+sh




3S
Back to Directory File Manager