Your IP : 3.17.181.181


Current Path : /usr/lib/python3.6/site-packages/urllib3/contrib/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/urllib3/contrib/__pycache__/securetransport.cpython-36.opt-1.pyc

3

nf�\ev�)@s�dZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
mZddlmZmZmZddlmZmZmZmZydd	l	mZWn$ek
r�dZdd
lmZYnXddgZd
ZejZejj Z!ej"�Z#ej$�Z%dZ&ej'ej(ej)ej*ej+ej,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ej<ej=ej>ej?ej@ejAejBejCejDejEejFejGg!ZHe
jIejJejKfiZLeMe
d��r�ejNejNfeLe
jO<eMe
d��r�ejPejPfeLe
jQ<eMe
d��r�ejJejJfeLe
jR<eMe
d��rejSejSfeLe
jT<eMe
d��r$ejKejKfeLe
jU<eMe
d��r@eLe
jIeLe
jV<dd�ZWdd�ZXdd�ZYdd�ZZej[eY�Z\ej]eZ�Z^Gdd�de_�Z`e�r�d$dd�Zan
d%d d�Zaeae`_aGd!d"�d"e_�ZbdS)&aU
SecureTranport support for urllib3 via ctypes.

This makes platform-native TLS available to urllib3 users on macOS without the
use of a compiler. This is an important feature because the Python Package
Index is moving to become a TLSv1.2-or-higher server, and the default OpenSSL
that ships with macOS is not capable of doing TLSv1.2. The only way to resolve
this is to give macOS users an alternative solution to the problem, and that
solution is to use SecureTransport.

We use ctypes here because this solution must not require a compiler. That's
because pip is not allowed to require a compiler either.

This is not intended to be a seriously long-term solution to this problem.
The hope is that PEP 543 will eventually solve this issue for us, at which
point we can retire this contrib module. But in the short term, we need to
solve the impending tire fire that is Python on Mac without this kind of
contrib module. So...here we are.

To use this module, simply import and inject it::

    import urllib3.contrib.securetransport
    urllib3.contrib.securetransport.inject_into_urllib3()

Happy TLSing!
�)�absolute_importN�)�util�)�Security�
SecurityConst�CoreFoundation)�_assert_no_error�_cert_array_from_pem�_temporary_keychain�_load_client_cert_chain)�_fileobject)�backport_makefile�inject_into_urllib3�extract_from_urllib3Ti@�PROTOCOL_SSLv2�PROTOCOL_SSLv3�PROTOCOL_TLSv1�PROTOCOL_TLSv1_1�PROTOCOL_TLSv1_2�PROTOCOL_TLScCs(ttj_tt_ttj_dt_dtj_dS)zG
    Monkey-patch urllib3 with SecureTransport-backed SSL-support.
    TN)�SecureTransportContextr�ssl_�
SSLContext�HAS_SNI�IS_SECURETRANSPORT�rr�%/usr/lib/python3.6/securetransport.pyr�s
cCs(ttj_tt_ttj_dt_dtj_dS)z>
    Undo monkey-patching by :func:`inject_into_urllib3`.
    FN)�orig_util_SSLContextrrr�orig_util_HAS_SNIrrrrrrr�s
c
Csxd}�y8tj|�}|dkr tjS|j}|d}|j�}d}d}y|xv||kr�|dksZ|dkrttj||�sttjt	j
d��||}	tj|	j
||�}
|j|
|	�}||7}|sB|s�tjSPqBWWnhtjk
�r"}zH|j	}|dk	o�|t	j
k�r||d<|t	jk�s
|t	jk�rtjS�WYdd}~XnX||d<||k�r<tjSdStk
�rr}z|dk	�rb||_tjSd}~XnXdS)zs
    SecureTransport read callback. This is called by ST to request that data
    be returned from the socket.
    Nrz	timed out)�_connection_refs�getr�errSSLInternal�socket�
gettimeoutrZ
wait_for_read�error�errno�EAGAIN�ctypes�c_charZfrom_address�	recv_into�errSSLClosedGraceful�
ECONNRESET�EPIPE�errSSLClosedAbort�errSSLWouldBlock�	Exception�
_exception)
�
connection_id�data_buffer�data_length_pointer�wrapped_socket�base_socketZrequested_length�timeoutr%Z
read_countZ	remaining�bufferZ
chunk_size�errr�_read_callback�sN






r:cCs\d}�ytj|�}|dkr tjS|j}|d}tj||�}|j�}d}d}	yZxT|	|kr�|dksf|dkr�tj	||�s�tj
tjd��|j
|�}
|	|
7}	||
d�}qNWWnbtj
k
�r}zB|j}|dk	r�|tjkr�|	|d<|tjks�|tjkr�tjS�WYdd}~XnX|	|d<|	|k�r tjSdStk
�rV}z|dk	�rF||_tjSd}~XnXdS)zx
    SecureTransport write callback. This is called by ST to request that data
    actually be sent on the network.
    Nrz	timed out)r r!rr"r#r(�	string_atr$rZwait_for_writer%r&r'�sendr,r-r.r/r0r1)r2r3r4r5r6Zbytes_to_write�datar7r%�sentZ
chunk_sentr9rrr�_write_callback�sD




r?c@s�eZdZdZdd�Zejdd��Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
d(dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd)d"d#�Zd$d%�Zd&d'�ZdS)*�
WrappedSocketz�
    API-compatibility wrapper for Python's OpenSSL wrapped socket object.

    Note: _makefile_refs, _drop(), and _reuse() are needed for the garbage
    collector of PyPy.
    cCsL||_d|_d|_d|_d|_d|_d|_d|_|jj�|_	|jj
d�dS)NrF)r#�context�_makefile_refs�_closedr1�	_keychain�
_keychain_dir�_client_cert_chainr$�_timeout�
settimeout)�selfr#rrr�__init__(szWrappedSocket.__init__ccs4d|_dV|jdk	r0|jd}|_|j�|�dS)a]
        A context manager that can be used to wrap calls that do I/O from
        SecureTransport. If any of the I/O callbacks hit an exception, this
        context manager will correctly propagate the exception after the fact.
        This avoids silently swallowing those exceptions.

        It also correctly forces the socket closed.
        N)r1�close)rIZ	exceptionrrr�_raise_on_error:s

zWrappedSocket._raise_on_errorcCs2tjtt�t�}tj|j|tt��}t|�dS)a4
        Sets up the allowed ciphers. By default this matches the set in
        util.ssl_.DEFAULT_CIPHERS, at least as supported by macOS. This is done
        custom and doesn't allow changing at this time, mostly because parsing
        OpenSSL cipher strings is going to be a freaking nightmare.
        N)rZSSLCipherSuite�len�
CIPHER_SUITESZSSLSetEnabledCiphersrAr	)rI�ciphers�resultrrr�_set_ciphersOszWrappedSocket._set_ciphersc	Cs|sdStjj|�r2t|d��}|j�}WdQRXd}tj�}z�t|�}tj|j	t
j|��}t|�|srt
jd��tj||�}t|�tj|d�}t|�tj�}tj|t
j|��}t|�Wd|r�tj|�|dk	r�tj|�Xtjtjf}|j|k�r
t
jd|j��dS)z�
        Called when we have set custom validation. We do this in two cases:
        first, when cert validation is entirely disabled; and second, when
        using a custom trust DB.
        N�rbzFailed to copy trust referenceTz)certificate verify failed, error code: %d)�os�path�isfile�open�readr�SecTrustRefr
�SSLCopyPeerTrustrAr(�byrefr	�sslZSSLErrorZSecTrustSetAnchorCertificatesZ!SecTrustSetAnchorCertificatesOnlyZSecTrustResultTypeZSecTrustEvaluater�	CFReleaserZkSecTrustResultUnspecifiedZkSecTrustResultProceed�value)	rI�verify�trust_bundle�fZ
cert_array�trustrPZtrust_resultZ	successesrrr�_custom_validate\s@

zWrappedSocket._custom_validatec	Cs�tjdtjtj�|_tj|jtt�}	t	|	�t
�4t|�d}
x|
tkrV|
dd}
q@W|t|
<WdQRXtj
|j|
�}	t	|	�|r�t|t�s�|jd�}tj|j|t|��}	t	|	�|j�tj|j|�}	t	|	�tj|j|�}	t	|	�|s�|dk	�rtj|jtjd�}	t	|	�|�rNt�\|_|_t|j||�|_tj|j|j�}	t	|	�xf|j��Rtj|j�}	|	tj k�r~t!j"d��n(|	tj#k�r�|j$||��wPn
t	|	�PWdQRX�qPWdS)z�
        Actually performs the TLS handshake. This is run automatically by
        wrapped socket, and shouldn't be needed in user code.
        Ni���rzutf-8Tzhandshake timed out)%rZSSLCreateContextrZkSSLClientSideZkSSLStreamTyperAZ
SSLSetIOFuncs�_read_callback_pointer�_write_callback_pointerr	�_connection_ref_lock�idr ZSSLSetConnection�
isinstance�bytes�encodeZSSLSetPeerDomainNamerMrQZSSLSetProtocolVersionMinZSSLSetProtocolVersionMaxZSSLSetSessionOptionZ"kSSLSessionOptionBreakOnServerAuthrrDrErrFZSSLSetCertificaterLZSSLHandshaker/r#r7ZerrSSLServerAuthCompletedrb)rI�server_hostnamer^r_Zmin_versionZmax_versionZclient_certZ
client_keyZclient_key_passphraserPZhandlerrr�	handshake�s\



zWrappedSocket.handshakecCs
|jj�S)N)r#�fileno)rIrrrrl�szWrappedSocket.filenocCs*|jdkr|jd8_|jr&|j�dS)Nrr)rBrCrK)rIrrr�_decref_socketios�s
zWrappedSocket._decref_socketioscCs&tj|�}|j||�}|d|�}|S)N)r(Zcreate_string_bufferr*)rIZbufsizr8Z
bytes_readr=rrr�recv�s
zWrappedSocket.recvNc
Cs�|jr
dS|dkrt|�}tj|j|�}tjd�}|j��tj|j	||tj
|��}WdQRX|tjkr�|j
dkr�tjd��n"|tjtjfkr�|j�nt|�|j
S)Nrzrecv timed out)rCrMr(r)Zfrom_buffer�c_size_trLrZSSLReadrArZrr/r]r#r7r+ZerrSSLClosedNoNotifyrKr	)rIr8�nbytes�processed_bytesrPrrrr*s 




zWrappedSocket.recv_intocCs
||_dS)N)rG)rIr7rrrrH,szWrappedSocket.settimeoutcCs|jS)N)rG)rIrrrr$/szWrappedSocket.gettimeoutc
Cshtjd�}|j��"tj|j|t|�tj|��}WdQRX|tj	krZ|j
dkrZtjd��nt
|�|j
S)Nrzsend timed out)r(rorLrZSSLWriterArMrZrr/r]r#r7r	)rIr=rqrPrrrr<2s

"zWrappedSocket.sendcCs8d}x.|t|�kr2|j|||t��}||7}qWdS)Nr)rMr<�SSL_WRITE_BLOCKSIZE)rIr=Z
total_sentr>rrr�sendallCszWrappedSocket.sendallc	Cs$|j��tj|j�WdQRXdS)N)rLrZSSLCloserA)rIrrr�shutdownIs
zWrappedSocket.shutdowncCs�|jdkr�d|_|jr(tj|j�d|_|jr@tj|j�d|_|jrvtj|j�tj|j�t	j
|j�d|_|_|jj
�S|jd8_dS)NrT)rBrCrArr\rFrDrZSecKeychainDelete�shutilZrmtreerEr#rK)rIrrrrKMs

zWrappedSocket.closeFc
Cs�|std��tj�}d}d}zptj|jtj|��}t|�|sBdStj|�}|sTdStj	|d�}tj
|�}tj|�}tj
|�}	tj|	|�}Wd|r�tj|�|r�tj|�X|S)Nz2SecureTransport only supports dumping binary certsr)�
ValueErrorrrXrYrAr(rZr	ZSecTrustGetCertificateCountZSecTrustGetCertificateAtIndexZSecCertificateCopyDatarZCFDataGetLengthZCFDataGetBytePtrr;r\)
rIZbinary_formraZcertdataZ	der_bytesrPZ
cert_countZleafZdata_lengthr3rrr�getpeercert`s2




zWrappedSocket.getpeercertcCs|jd7_dS)Nr)rB)rIrrr�_reuse�szWrappedSocket._reusecCs&|jdkr|j�n|jd8_dS)Nr)rBrK)rIrrr�_drop�s

zWrappedSocket._drop)N)F)�__name__�
__module__�__qualname__�__doc__rJ�
contextlib�contextmanagerrLrQrbrkrlrmrnr*rHr$r<rsrtrKrwrxryrrrrr@!s&
>Z
(
>r@cCs|jd7_t|||dd�S)NrT)rK)rBr
)rI�mode�bufsizerrr�makefile�sr��rcOsd}t|||f|�|�S)Nr)r)rIr��	buffering�args�kwargsrrrr��sc@s�eZdZdZdd�Zedd��Zejdd��Zedd��Zejd	d��Zed
d��Z	e	jdd��Z	d
d�Z
dd�Zdd�Zddd�Z
ddd�Zddd�ZdS)rz�
    I am a wrapper class for the SecureTransport library, to translate the
    interface of the standard library ``SSLContext`` object to calls into
    SecureTransport.
    cCs8t|\|_|_d|_d|_d|_d|_d|_d|_dS)NrF)	�_protocol_to_min_max�_min_version�_max_version�_options�_verify�
_trust_bundle�_client_cert�_client_key�_client_key_passphrase)rIZprotocolrrrrJ�szSecureTransportContext.__init__cCsdS)z�
        SecureTransport cannot have its hostname checking disabled. For more,
        see the comment on getpeercert() in this file.
        Tr)rIrrr�check_hostname�sz%SecureTransportContext.check_hostnamecCsdS)z�
        SecureTransport cannot have its hostname checking disabled. For more,
        see the comment on getpeercert() in this file.
        Nr)rIr]rrrr��scCs|jS)N)r�)rIrrr�options�szSecureTransportContext.optionscCs
||_dS)N)r�)rIr]rrrr��scCs|jrtjStjS)N)r�r[�
CERT_REQUIREDZ	CERT_NONE)rIrrr�verify_mode�sz"SecureTransportContext.verify_modecCs|tjkrdnd|_dS)NTF)r[r�r�)rIr]rrrr��scCsdS)Nr)rIrrr�set_default_verify_paths�s
z/SecureTransportContext.set_default_verify_pathscCs|j�S)N)r�)rIrrr�load_default_certs�sz)SecureTransportContext.load_default_certscCs|tjjkrtd��dS)Nz5SecureTransport doesn't support custom cipher strings)rrZDEFAULT_CIPHERSrv)rIrOrrr�set_ciphers�sz"SecureTransportContext.set_ciphersNcCs|dk	rtd��|p||_dS)Nz1SecureTransport does not support cert directories)rvr�)rIZcafileZcapathZcadatarrr�load_verify_locationssz,SecureTransportContext.load_verify_locationscCs||_||_||_dS)N)r�r�Z_client_cert_passphrase)rIZcertfileZkeyfileZpasswordrrr�load_cert_chainsz&SecureTransportContext.load_cert_chainFTc	Cs2t|�}|j||j|j|j|j|j|j|j�|S)N)	r@rkr�r�r�r�r�r�r�)rIZsockZserver_sideZdo_handshake_on_connectZsuppress_ragged_eofsrjr5rrr�wrap_sockets
z"SecureTransportContext.wrap_socket)NNN)NN)FTTN)rzr{r|r}rJ�propertyr��setterr�r�r�r�r�r�r�r�rrrrr�s 	

	
r���)r�)r�N)cr}Z
__future__rr~r(r&Zos.pathrSrur#r[Z	threading�weakref�rZ_securetransport.bindingsrrrZ_securetransport.low_levelr	r
rrr
�ImportErrorZpackages.backports.makefiler�__all__rrrrr�WeakValueDictionaryr ZLockrerrZTLS_AES_256_GCM_SHA384ZTLS_CHACHA20_POLY1305_SHA256ZTLS_AES_128_GCM_SHA256Z'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384Z%TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384Z'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256Z%TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256Z#TLS_DHE_DSS_WITH_AES_256_GCM_SHA384Z#TLS_DHE_RSA_WITH_AES_256_GCM_SHA384Z#TLS_DHE_DSS_WITH_AES_128_GCM_SHA256Z#TLS_DHE_RSA_WITH_AES_128_GCM_SHA256Z'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384Z%TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384Z$TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHAZ"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHAZ#TLS_DHE_RSA_WITH_AES_256_CBC_SHA256Z#TLS_DHE_DSS_WITH_AES_256_CBC_SHA256Z TLS_DHE_RSA_WITH_AES_256_CBC_SHAZ TLS_DHE_DSS_WITH_AES_256_CBC_SHAZ'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256Z%TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256Z$TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHAZ"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHAZ#TLS_DHE_RSA_WITH_AES_128_CBC_SHA256Z#TLS_DHE_DSS_WITH_AES_128_CBC_SHA256Z TLS_DHE_RSA_WITH_AES_128_CBC_SHAZ TLS_DHE_DSS_WITH_AES_128_CBC_SHAZTLS_RSA_WITH_AES_256_GCM_SHA384ZTLS_RSA_WITH_AES_128_GCM_SHA256ZTLS_RSA_WITH_AES_256_CBC_SHA256ZTLS_RSA_WITH_AES_128_CBC_SHA256ZTLS_RSA_WITH_AES_256_CBC_SHAZTLS_RSA_WITH_AES_128_CBC_SHArNZPROTOCOL_SSLv23Z
kTLSProtocol1ZkTLSProtocol12r��hasattrZ
kSSLProtocol2rZ
kSSLProtocol3rrZkTLSProtocol11rrrrrr:r?ZSSLReadFuncrcZSSLWriteFuncrd�objectr@r�rrrrr�<module>s�76





?>