Your IP : 3.136.19.136


Current Path : /opt/alt/python37/lib/python3.7/site-packages/urllib3/contrib/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/urllib3/contrib/__pycache__/pyopenssl.cpython-37.pyc

B

��`�A�@sxdZddlmZddlZddlmZddlmZ	ddl
mZyddlm
Z
Wn$ek
rpGdd	�d	e�Z
YnXdd
lmZddlmZddlmZydd
lmZWn$ek
r�dZddlmZYnXddlZddlZddlZddlmZddlmZddl m!Z!ddgZ"dZ#ej$ej%j&e!ej%j&ej'ej%j(iZ)e*ed��r\e*ej%d��r\ej%j+e)ej,<e*ed��r�e*ej%d��r�ej%j-e)ej.<e*ed��r�e*ej%d��r�ej%j/e)ej0<ej1ej%j2ej3ej%j4ej5ej%j4ej%j6iZ7e8dd�e7�9�D��Z:dZ;ej#Z<ej=j>Z?e�@eA�ZBdd�ZCd d�ZDd!d"�ZEd#d$�ZFd%d&�ZGGd'd(�d(eH�ZIe�rRd0d*d+�ZJneZJeJeI_JGd,d-�d-eH�ZKd.d/�ZLdS)1a�
TLS with SNI_-support for Python 2. Follow these instructions if you would
like to verify TLS 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:

.. code-block:: bash

    $ python -m 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:

.. code-block:: python

    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`_).

.. _sni: https://en.wikipedia.org/wiki/Server_Name_Indication
.. _crime attack: https://en.wikipedia.org/wiki/CRIME_(security_exploit)
.. _pyopenssl: https://www.pyopenssl.org
.. _cryptography: https://cryptography.io
.. _idna: https://github.com/kjd/idna
�)�absolute_importN)�x509)�backend)�_Certificate)�UnsupportedExtensionc@seZdZdS)rN)�__name__�
__module__�__qualname__�r
r
�J/opt/alt/python37/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.pyr;sr)�BytesIO)�error)�timeout)�_fileobject�)�backport_makefile)�util)�six)�PROTOCOL_TLS_CLIENT�inject_into_urllib3�extract_from_urllib3T�PROTOCOL_SSLv3�SSLv3_METHOD�PROTOCOL_TLSv1_1�TLSv1_1_METHOD�PROTOCOL_TLSv1_2�TLSv1_2_METHODccs|]\}}||fVqdS)Nr
)�.0�k�vr
r
r�	<genexpr>msr i@cCs4t�tt_ttj_tt_ttj_dt_dtj_dS)z7Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.TN)�_validate_dependencies_met�PyOpenSSLContextr�
SSLContext�ssl_�HAS_SNI�IS_PYOPENSSLr
r
r
rryscCs.tt_ttj_tt_ttj_dt_dtj_dS)z4Undo monkey-patching by :func:`inject_into_urllib3`.FN)�orig_util_SSLContextrr#r$�orig_util_HAS_SNIr%r&r
r
r
rr�scCsRddlm}t|dd�dkr$td��ddlm}|�}t|dd�dkrNtd��dS)	z{
    Verifies that PyOpenSSL's package-level dependencies have been met.
    Throws `ImportError` if they are not met.
    r)�
Extensions�get_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)�getattr�ImportErrorZOpenSSL.cryptor+)r)r+rr
r
rr!�sr!cCs@dd�}d|kr|S||�}|dkr(dStjdkr<|�d�}|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).

    If the name cannot be idna-encoded then we return None signalling that
    the name given should be skipped.
    cSslddl}yFx:dD]2}|�|�r|t|�d�}|�d�|�|�SqW|�|�S|jjk
rfdSXdS)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)z*.�.�ascii)�idna�
startswith�len�encode�coreZ	IDNAError)�namer1�prefixr
r
r�idna_encode�s


z'_dnsname_to_stdlib.<locals>.idna_encode�:N)�rzutf-8)�sys�version_info�decode)r6r8r
r
r�_dnsname_to_stdlib�s

r>cCs�t|d�r|��}ntt|j�}y|j�tj�j	}WnNtj
k
rJgStjttj
tfk
r�}zt�d|�gSd}~XYnXdd�tt|�tj��D�}|�dd�|�tj�D��|S)zU
    Given an PyOpenSSL certificate, provides all the subject alternative names.
    �to_cryptographyz�A problem was encountered with the certificate that prevented urllib3 from finding the SubjectAlternativeName field. This can affect certificate validation. The error was %sNcSsg|]}|dk	rd|f�qS)NZDNSr
)rr6r
r
r�
<listcomp>sz%get_subj_alt_name.<locals>.<listcomp>css|]}dt|�fVqdS)z
IP AddressN)�str)rr6r
r
rr sz$get_subj_alt_name.<locals>.<genexpr>)�hasattrr?r�openssl_backendr,�
extensionsr*rZSubjectAlternativeName�valueZExtensionNotFoundZDuplicateExtensionrZUnsupportedGeneralNameType�UnicodeError�log�warning�mapr>Zget_values_for_typeZDNSName�extendZ	IPAddress)Z	peer_certZcert�ext�e�namesr
r
r�get_subj_alt_name�s*


rNc@s�eZdZdZd!dd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
d"dd�Zdd�Zdd�Zdd�Zd S)#�
WrappedSocketz�API-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)�
connection�socket�suppress_ragged_eofs�_makefile_refs�_closed)�selfrPrQrRr
r
r�__init__s
zWrappedSocket.__init__cCs
|j��S)N)rQ�fileno)rUr
r
rrWszWrappedSocket.filenocCs*|jdkr|jd8_|jr&|��dS)Nr�)rSrT�close)rUr
r
r�_decref_socketioss
zWrappedSocket._decref_socketiosc
Osy|jj||�}Wn�tjjk
rX}z$|jr<|jdkr<dStt|���Wdd}~XYn�tjj	k
r�|j�
�tjjkr�dS�Ynxtjjk
r�t
�|j|j���s�td��n|j||�SYn8tjjk
r�}zt�d|��Wdd}~XYnX|SdS)N)���zUnexpected EOF�zThe read operation timed outzread error: %r)rP�recv�OpenSSL�SSL�SysCallErrorrR�args�SocketErrorrA�ZeroReturnError�get_shutdown�RECEIVED_SHUTDOWN�
WantReadErrorr�
wait_for_readrQ�
gettimeoutr�Error�ssl�SSLError)rUra�kwargs�datarLr
r
rr]$s"
 zWrappedSocket.recvc
Os�y|jj||�Stjjk
rT}z$|jr8|jdkr8dStt|���Wdd}~XYn�tjj	k
r�|j�
�tjjkr|dS�Ynttjjk
r�t
�|j|j���s�td��n|j||�SYn4tjjk
r�}zt�d|��Wdd}~XYnXdS)N)r[zUnexpected EOFrzThe read operation timed outzread error: %r)rP�	recv_intor^r_r`rRrarbrArcrdrerfrrgrQrhrrirjrk)rUrarlrLr
r
rrn=s 
zWrappedSocket.recv_intocCs|j�|�S)N)rQ�
settimeout)rUrr
r
rroTszWrappedSocket.settimeoutc
Cs|xvy|j�|�Stjjk
rBt�|j|j���s<t	��wYqtjj
k
rr}ztt|���Wdd}~XYqXqWdS)N)
rP�sendr^r_ZWantWriteErrorrZwait_for_writerQrhrr`rbrA)rUrmrLr
r
r�_send_until_doneWszWrappedSocket._send_until_donecCs8d}x.|t|�kr2|�|||t��}||7}qWdS)Nr)r3rq�SSL_WRITE_BLOCKSIZE)rUrm�
total_sent�sentr
r
r�sendallbs
zWrappedSocket.sendallcCs|j��dS)N)rP�shutdown)rUr
r
rrvjszWrappedSocket.shutdowncCsH|jdkr6yd|_|j��Stjjk
r2dSXn|jd8_dS)NrXT)rSrTrPrYr^r_ri)rUr
r
rrYns

zWrappedSocket.closeFcCsD|j��}|s|S|r(tj�tjj|�Sd|��jffft|�d�S)NZ
commonName)ZsubjectZsubjectAltName)	rPZget_peer_certificater^ZcryptoZdump_certificateZ
FILETYPE_ASN1Zget_subjectZCNrN)rUZbinary_formrr
r
r�getpeercertxs
zWrappedSocket.getpeercertcCs
|j��S)N)rPZget_protocol_version_name)rUr
r
r�version�szWrappedSocket.versioncCs|jd7_dS)NrX)rS)rUr
r
r�_reuse�szWrappedSocket._reusecCs&|jdkr|��n|jd8_dS)NrX)rSrY)rUr
r
r�_drop�s

zWrappedSocket._dropN)T)F)rrr	�__doc__rVrWrZr]rnrorqrurvrYrwrxryrzr
r
r
rrOs


rOr[cCs|jd7_t|||dd�S)NrXT)rY)rSr)rU�mode�bufsizer
r
r�makefile�sr~c@s�eZdZdZdd�Zedd��Zejdd��Zedd��Zejd	d��Zd
d�Z	dd
�Z
ddd�Zddd�Zdd�Z
ddd�ZdS)r"z�
    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||_tj�|j�|_d|_d|_dS)NrF)�_openssl_versions�protocolr^r_ZContext�_ctx�_optionsZcheck_hostname)rUr�r
r
rrV�s
zPyOpenSSLContext.__init__cCs|jS)N)r�)rUr
r
r�options�szPyOpenSSLContext.optionscCs||_|j�|�dS)N)r�r��set_options)rUrEr
r
rr��scCst|j��S)N)�_openssl_to_stdlib_verifyr�Zget_verify_mode)rUr
r
r�verify_mode�szPyOpenSSLContext.verify_modecCs|j�t|t�dS)N)r�Z
set_verify�_stdlib_to_openssl_verify�_verify_callback)rUrEr
r
rr��scCs|j��dS)N)r��set_default_verify_paths)rUr
r
rr��sz)PyOpenSSLContext.set_default_verify_pathscCs&t|tj�r|�d�}|j�|�dS)Nzutf-8)�
isinstancer�	text_typer4r�Zset_cipher_list)rUZciphersr
r
r�set_ciphers�s
zPyOpenSSLContext.set_ciphersNc
Cs�|dk	r|�d�}|dk	r$|�d�}y*|j�||�|dk	rL|j�t|��Wn4tjjk
r�}zt�d|��Wdd}~XYnXdS)Nzutf-8z'unable to load trusted certificates: %r)	r4r��load_verify_locationsrr^r_rirjrk)rUZcafileZcapathZcadatarLr
r
rr��s

z&PyOpenSSLContext.load_verify_locationscsR|j�|��dk	r>t�tj�s*��d��|j��fdd��|j�|pJ|�dS)Nzutf-8cs�S)Nr
)�_)�passwordr
r�<lambda>�r\z2PyOpenSSLContext.load_cert_chain.<locals>.<lambda>)r�Zuse_certificate_chain_filer�rZbinary_typer4Z
set_passwd_cbZuse_privatekey_file)rUZcertfileZkeyfiler�r
)r�r�load_cert_chain�s
z PyOpenSSLContext.load_cert_chaincCsdd�|D�}|j�|�S)NcSsg|]}t�|��qSr
)rZ
ensure_binary)r�pr
r
rr@�sz7PyOpenSSLContext.set_alpn_protocols.<locals>.<listcomp>)r�Zset_alpn_protos)rUZ	protocolsr
r
r�set_alpn_protocols�sz#PyOpenSSLContext.set_alpn_protocolsFTc
Cs�tj�|j|�}t|tj�r&|�d�}|dk	r8|�|�|�	�xxy|�
�Wndtjjk
r�t�
||���sztd��wBYn4tjjk
r�}zt�d|��Wdd}~XYnXPqBWt||�S)Nzutf-8zselect timed outzbad handshake: %r)r^r_Z
Connectionr�r�rr�r4Zset_tlsext_host_nameZset_connect_stateZdo_handshakerfrrgrhrrirjrkrO)rU�sockZserver_sideZdo_handshake_on_connectrRZserver_hostname�cnxrLr
r
r�wrap_socket�s"

 zPyOpenSSLContext.wrap_socket)NNN)NN)FTTN)rrr	r{rV�propertyr��setterr�r�r�r�r�r�r�r
r
r
rr"�s

r"cCs|dkS)Nrr
)r�rZerr_noZ	err_depthZreturn_coder
r
rr��sr�)r[)Mr{�
__future__rZOpenSSL.SSLr^ZcryptographyrZ$cryptography.hazmat.backends.opensslrrCZ)cryptography.hazmat.backends.openssl.x509rZcryptography.x509rr.�	Exception�iorrQr
rbrrZpackages.backports.makefiler�loggingrjr;�r�packagesrZ	util.ssl_r�__all__r%ZPROTOCOL_TLSr_Z
SSLv23_METHODZPROTOCOL_TLSv1ZTLSv1_METHODrrBrrrrrrZ	CERT_NONEZVERIFY_NONEZ
CERT_OPTIONALZVERIFY_PEERZ
CERT_REQUIREDZVERIFY_FAIL_IF_NO_PEER_CERTr��dict�itemsr�rrr(r$r#r'�	getLoggerrrGrrr!r>rN�objectrOr~r"r�r
r
r
r�<module>/sp





+6^

?>