HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //usr/local/lib/python3.7/site-packages/urllib3/contrib/__pycache__/securetransport.cpython-37.pyc
B

���gI��
@s�UdZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddl	m	ZddlmZddlmZmZdd	lmZmZmZmZmZmZmZejd
edd�e
j r�ddl!m"Z"d
dgZ#ej$j%Z&e�'�Z(de)d<e�*�Z+dZ,ej$j-ej.ej/fej$j0ej.ej/fiZ1e2e
d��rNej3ej3fe1e
j4<e2e
d��rlej5ej5fe1e
j6<e2e
d��r�ej.ej.fe1e
j7<e2e
d��r�ej8ej8fe1e
j9<e2e
d��r�ej/ej/fe1e
j:<e
j;j<ej.e
j;j=ej.e
j;j>ej8e
j;j?ej/e
j;j@ej/iZAde)d<dd�dd
�ZBdd�dd�ZCddddd�d d!�ZDddddd�d"d#�ZEe�FeD�ZGe�HeE�ZIGd$d%�d%�ZJd1d'd(d)d*d*d+d,�d-d.�ZKeKeJ_KGd/d0�d0�ZLdS)2a�	
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!

This code is a bastardised version of the code found in Will Bond's oscrypto
library. An enormous debt is owed to him for blazing this trail for us. For
that reason, this code should be considered to be covered both by urllib3's
license and by oscrypto's:

.. code-block::

    Copyright (c) 2015-2016 Will Bond <will@wbond.net>

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
�)�annotationsN)�socket�)�util�)�CoreFoundation�Security)�
SecurityConst�_assert_no_error�_build_tls_unknown_ca_alert�_cert_array_from_pem�_create_cfstring_array�_load_client_cert_chain�_temporary_keychainz�'urllib3.contrib.securetransport' module is deprecated and will be removed in urllib3 v2.1.0. Read more in this issue: https://github.com/urllib3/urllib3/issues/2681)�category�
stacklevel)�Literal�inject_into_urllib3�extract_from_urllib3z/weakref.WeakValueDictionary[int, WrappedSocket]�_connection_refsi@�PROTOCOL_SSLv2�PROTOCOL_SSLv3�PROTOCOL_TLSv1�PROTOCOL_TLSv1_1�PROTOCOL_TLSv1_2zdict[int, int]�_tls_version_to_st�None)�returncCs tt_ttj_dt_dtj_dS)zG
    Monkey-patch urllib3 with SecureTransport-backed SSL-support.
    TN)�SecureTransportContextr�
SSLContext�ssl_�IS_SECURETRANSPORT�r"r"�I/usr/local/lib/python3.7/site-packages/urllib3/contrib/securetransport.pyr�scCs tt_ttj_dt_dtj_dS)z>
    Undo monkey-patching by :func:`inject_into_urllib3`.
    FN)�orig_util_SSLContextrrr r!r"r"r"r#r�s�int�	bytearray)�
connection_id�data_buffer�data_length_pointerrc

Csxd}�y6t�|�}|dkr tjS|j}|d}|��}d}d}yzxt||kr�|dksZ|dkrrt�||�srtt	j
d��||}	tj|	�
||�}
|�|
|	�}||7}|sB|s�tjSPqBWWnhtk
�r }zH|j	}|dk	�r|t	j
k�r||d<|t	jk�s|t	jk�rtjS�Wdd}~XYnX||d<||k�r:tjSdStk
�rr}z|dk	�r`||_tjSd}~XYnXdS)zs
    SecureTransport read callback. This is called by ST to request that data
    be returned from the socket.
    Nrz	timed out)r�getr	�errSSLInternalr�
gettimeoutr�
wait_for_read�OSError�errno�EAGAIN�ctypes�c_charZfrom_address�	recv_into�errSSLClosedGraceful�
ECONNRESET�EPIPE�errSSLClosedAbort�errSSLWouldBlock�	Exception�
_exception)
r'r(r)�wrapped_socket�base_socketZrequested_length�timeout�errorZ
read_count�	remaining�buffer�
chunk_size�er"r"r#�_read_callback�sN






rCc
CsZd}�yt�|�}|dkr tjS|j}|d}t�||�}|��}d}d}	yXxR|	|kr�|dksf|dkr~t�	||�s~t
tjd��|�
|�}
|	|
7}	||
d�}qNWWn`t
k
�r}z@|j}|dk	r�|tjkr�|	|d<|tjks�|tjkr�tjS�Wdd}~XYnX|	|d<|	|k�rtjSdStk
�rT}z|dk	�rB||_tjSd}~XYnXdS)zx
    SecureTransport write callback. This is called by ST to request that data
    actually be sent on the network.
    Nrz	timed out)rr*r	r+rr1�	string_atr,r�wait_for_writer.r/r0�sendr5r6r7r8r9r:)r'r(r)r;r<Zbytes_to_write�datar=r>�sentZ
chunk_sentrBr"r"r#�_write_callback�sD




rIc@sNeZdZdZddd�dd�Zejdd�d	d
��Zddd�d
d�Zdddd�dd�Z	ddd�dd�Z
ddddddddddd�
dd�Zdd�dd �Zdd�d!d"�Z
ddd#�d$d%�ZdEd'd(dd)�d*d+�Zd,dd-�d.d/�Zd0d�d1d2�Zddd3�d4d5�Zddd3�d6d7�Zdd�d8d9�Zdd�d:d;�Zdd�d<d=�ZdFddd?�d@dA�ZdBd�dCdD�Zd&S)G�
WrappedSocketzO
    API-compatibility wrapper for Python's OpenSSL wrapped socket object.
    �
socket_clsr)rrcCsR||_d|_d|_d|_d|_d|_d|_d|_d|_|j�	�|_
|j�d�dS)NrF)r�context�_io_refs�_closed�_real_closedr:�	_keychain�
_keychain_dir�_client_cert_chainr,�_timeout�
settimeout)�selfrr"r"r#�__init__;szWrappedSocket.__init__z$typing.Generator[(None, None, None)])rccs4d|_dV|jdk	r0|jd}|_|��|�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)r:�_real_close)rU�	exceptionr"r"r#�_raise_on_errorNs

zWrappedSocket._raise_on_errorzlist[bytes] | None)�	protocolsrc	Cs<|sdSt|�}zt�|j|�}t|�Wdt�|�XdS)z<
        Sets up the ALPN protocols on the context.
        N)r
r�SSLSetALPNProtocolsrLr
r�	CFRelease)rUrZZ
protocols_arr�resultr"r"r#�_set_alpn_protocolscsz!WrappedSocket._set_alpn_protocols�boolzbytes | None)�verify�trust_bundlerc

Cs�|r|dkrdStjtjf}y,|�|�}||kr4dSdt|���}d}Wn0tk
rx}zd|��}|}Wdd}~XYnXt|���}|j�	|�t
�ddd�}	|j�tj
tj|	�|��t�d|���|�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.
        Raises an SSLError if the connection is not trusted.
        Nzerror code: zexception: �iirrzcertificate verify failed, )r	ZkSecTrustResultUnspecifiedZkSecTrustResultProceed�_evaluate_trustr%r9r�versionr�sendall�struct�pack�
setsockopt�
SOL_SOCKET�	SO_LINGERrW�ssl�SSLError)
rUr`raZ	successes�trust_result�reason�excrBZrec�optsr"r"r#�_custom_validateps&

zWrappedSocket._custom_validate�bytesr%)rarc	Cs�tj�|�r*t|d��}|��}WdQRXd}t��}z�t|�}t�|j	t
�|��}t|�|sjt
�d��t�||�}t|�t�|d�}t|�t��}t�|t
�|��}t|�Wd|r�t�|�|dk	r�t�|�X|jS)N�rbzFailed to copy trust referenceT)�os�path�isfile�open�readr�SecTrustRefr�SSLCopyPeerTrustrLr1�byrefr
rkrlZSecTrustSetAnchorCertificatesZ!SecTrustSetAnchorCertificatesOnlyZSecTrustResultTypeZSecTrustEvaluaterr\�value)rUra�fZ
cert_array�trustr]rmr"r"r#rc�s.

zWrappedSocket._evaluate_trustzbytes | str | Nonez
str | Nonez
typing.Any)
�server_hostnamer`ra�min_version�max_version�client_cert�
client_key�client_key_passphrase�alpn_protocolsrc
	Cs�t�dtjtj�|_t�|jtt�}
t	|
�t
�4t|�d}x|tkrV|dd}q@W|t|<WdQRXt�
|j|�}
t	|
�|r�t|t�s�|�d�}t�|j|t|��}
t	|
�|�|	�t�|j|�}
t	|
�t�|j|�}
t	|
�|r�|dk	�rt�|jtjd�}
t	|
�|�rNt�\|_|_t|j||�|_t�|j|j�}
t	|
�xf|���Rt�|j�}
|
tj k�r~t!�"d��n(|
tj#k�r�|�$||��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)%rZSSLCreateContextr	ZkSSLClientSideZkSSLStreamTyperLZ
SSLSetIOFuncs�_read_callback_pointer�_write_callback_pointerr
�_connection_ref_lock�idrZSSLSetConnection�
isinstancerr�encodeZSSLSetPeerDomainName�lenr^ZSSLSetProtocolVersionMinZSSLSetProtocolVersionMaxZSSLSetSessionOptionZ"kSSLSessionOptionBreakOnServerAuthrrPrQrrRZSSLSetCertificaterYZSSLHandshaker8rr=ZerrSSLServerAuthCompletedrq)rUrr`rar�r�r�r�r�r�r]�handler"r"r#�	handshake�sV




zWrappedSocket.handshakecCs
|j��S)N)r�fileno)rUr"r"r#r�szWrappedSocket.filenocCs*|jdkr|jd8_|jr&|��dS)Nrr)rMrN�close)rUr"r"r#�_decref_socketioss
zWrappedSocket._decref_socketios)�bufsizrcCs.t�|�}|�||�}|d|�}t�t|�S)N)r1�create_string_bufferr3�typing�castrr)rUr�r@Z
bytes_readrGr"r"r#�recv#s
zWrappedSocket.recvNzctypes.Array[ctypes.c_char]z
int | None)r@�nbytesrc	Cs�|jr
dS|dkrt|�}tj|�|�}t�d�}|���t�|j	||t�
|��}WdQRX|tjkr�|j
dkr�t�d��n"|tjtjfkr�|��nt|�|j
S)Nrzrecv timed out)rOr�r1r2Zfrom_buffer�c_size_trYrZSSLReadrLr{r	r8r|rr=r4ZerrSSLClosedNoNotifyrWr
)rUr@r��processed_bytesr]r"r"r#r3)s$





zWrappedSocket.recv_into�float)r=rcCs
||_dS)N)rS)rUr=r"r"r#rTVszWrappedSocket.settimeoutzfloat | NonecCs|jS)N)rS)rUr"r"r#r,YszWrappedSocket.gettimeout)rGrc	Csht�d�}|���"t�|j|t|�t�|��}WdQRX|tj	krZ|j
dkrZt�d��nt
|�|j
S)Nrzsend timed out)r1r�rYrZSSLWriterLr�r{r	r8r|rr=r
)rUrGr�r]r"r"r#rF\s

"zWrappedSocket.sendcCs8d}x.|t|�kr2|�|||t��}||7}qWdS)Nr)r�rF�SSL_WRITE_BLOCKSIZE)rUrG�
total_sentrHr"r"r#remszWrappedSocket.sendallc	Cs$|���t�|j�WdQRXdS)N)rYrZSSLCloserL)rUr"r"r#�shutdownss
zWrappedSocket.shutdowncCsd|_|jdkr|��dS)NTr)rNrMrW)rUr"r"r#r�ws
zWrappedSocket.closecCsvd|_|jrt�|j�d|_|jr6t�|j�d|_|jrlt�|j�t�|j�t�	|j
�d|_|_
|j��S)NT)
rOrLrr\rRrPrZSecKeychainDelete�shutil�rmtreerQrr�)rUr"r"r#rW}szWrappedSocket._real_closeF)�binary_formrc
	Cs�|std��t��}d}d}z�t�|jt�|��}t|�|sBdSt�|�}|sTdSt�	|d�}|sht
�t�|�}|szt
�t�
|�}t�|�}	t�|	|�}Wd|r�t�|�|r�t�|�X|S)Nz2SecureTransport only supports dumping binary certsr)�
ValueErrorrryrzrLr1r{r
ZSecTrustGetCertificateCountZSecTrustGetCertificateAtIndex�AssertionErrorZSecCertificateCopyDatarZCFDataGetLengthZCFDataGetBytePtrrDr\)
rUr�r~ZcertdataZ	der_bytesr]Z
cert_count�leafZdata_lengthr(r"r"r#�getpeercert�s2




zWrappedSocket.getpeercert�strcCs�t��}t�|jt�|��}t|�|jtj	kr<t
�d��n`|jtjkrLdS|jtj
kr\dS|jtjkrldS|jtjkr|dS|jtjkr�dSt
�d|����dS)Nz(SecureTransport does not support TLS 1.3zTLSv1.2zTLSv1.1�TLSv1�SSLv3ZSSLv2zUnknown TLS version: )r�SSLProtocolZSSLGetNegotiatedProtocolVersionrLr1r{r
r|r	ZkTLSProtocol13rkrl�kTLSProtocol12�kTLSProtocol11�
kTLSProtocol1�
kSSLProtocol3�
kSSLProtocol2)rU�protocolr]r"r"r#rd�s"zWrappedSocket.version)N)F)�__name__�
__module__�__qualname__�__doc__rV�
contextlib�contextmanagerrYr^rqrcr�r�r�r�r3rTr,rFrer�r�rWr�rdr"r"r"r#rJ6s(
%( \,:rJ�rrKzILiteral['r'] | Literal['w'] | Literal['rw'] | Literal['wr'] | Literal['']z
int | Nonez
typing.Anyztyping.BinaryIO | typing.TextIO)rU�mode�	buffering�args�kwargsrcOsd}tj|||f|�|�S)Nr)rK�makefile)rUr�r�r�r�r"r"r#r��sr�c@sjeZdZdZddd�dd�Zedd�d	d
��Zejddd�d
d
��Zedd�dd��Zejddd�dd��Zedd�dd��Z	e	jddd�dd��Z	dd�dd�Z
dd�dd�Zddd�dd�Zd:ddddd�dd �Z
d;d!dddd"�d#d$�Zd%dd&�d'd(�Zd<d+d,d,d,d-d.d/�d0d1�Zedd�d2d3��Zejddd4�d5d3��Zedd�d6d7��Zejddd8�d9d7��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.
    r%r)r�rcCsdtjj|_tjj|_|dtjtjfkr6t|\|_	|_
d|_d|_d|_
d|_d|_d|_d|_dS)NrF)rk�
TLSVersion�MINIMUM_SUPPORTED�_minimum_version�MAXIMUM_SUPPORTED�_maximum_version�PROTOCOL_TLS�PROTOCOL_TLS_CLIENT�_protocol_to_min_maxZ_min_versionZ_max_version�_options�_verify�
_trust_bundle�_client_cert�_client_key�_client_key_passphrase�_alpn_protocols)rUr�r"r"r#rV�s

zSecureTransportContext.__init__z
Literal[True])rcCsdS)z�
        SecureTransport cannot have its hostname checking disabled. For more,
        see the comment on getpeercert() in this file.
        Tr")rUr"r"r#�check_hostnamesz%SecureTransportContext.check_hostnamez
typing.Any)r|rcCsdS)z�
        SecureTransport cannot have its hostname checking disabled. For more,
        see the comment on getpeercert() in this file.
        Nr")rUr|r"r"r#r�scCs|jS)N)r�)rUr"r"r#�optionsszSecureTransportContext.optionscCs
||_dS)N)r�)rUr|r"r"r#r�scCs|jrtjStjS)N)r�rk�
CERT_REQUIRED�	CERT_NONE)rUr"r"r#�verify_mode!sz"SecureTransportContext.verify_modecCs|tjk|_dS)N)rkr�r�)rUr|r"r"r#r�%scCsdS)Nr")rUr"r"r#�set_default_verify_paths)s
z/SecureTransportContext.set_default_verify_pathscCs|��S)N)r�)rUr"r"r#�load_default_certs5sz)SecureTransportContext.load_default_certs)�ciphersrcCstd��dS)Nz5SecureTransport doesn't support custom cipher strings)r�)rUr�r"r"r#�set_ciphers8sz"SecureTransportContext.set_ciphersNz
str | Nonezbytes | None)�cafile�capath�cadatarc	Cs:|dk	rtd��|dk	r,t|��WdQRX|p2||_dS)Nz1SecureTransport does not support cert directories)r�rwr�)rUr�r�r�r"r"r#�load_verify_locations;s

z,SecureTransportContext.load_verify_locationsr�)�certfile�keyfile�passwordrcCs||_||_||_dS)N)r�r�Z_client_cert_passphrase)rUr�r�r�r"r"r#�load_cert_chainLsz&SecureTransportContext.load_cert_chainzlist[str | bytes])rZrcCs&ttd�std��dd�|D�|_dS)z�
        Sets the ALPN protocols that will later be set on the context.

        Raises a NotImplementedError if ALPN is not supported.
        r[z2SecureTransport supports ALPN only in macOS 10.12+cSsg|]}tj�|d��qS)�ascii)r�to_bytes)�.0�pr"r"r#�
<listcomp>`sz=SecureTransportContext.set_alpn_protocols.<locals>.<listcomp>N)�hasattrr�NotImplementedErrorr�)rUrZr"r"r#�set_alpn_protocolsVs
z)SecureTransportContext.set_alpn_protocolsFTrKr_zbytes | str | NonerJ)�sock�server_side�do_handshake_on_connect�suppress_ragged_eofsrrcCsV|rt�|st�|st�t|�}|�||j|jt|jt|j|j|j	|j
|j�	|S)N)r�rJr�r�r�rr�r�r�r�r�r�)rUr�r�r�r�rr;r"r"r#�wrap_socketbsz"SecureTransportContext.wrap_socketcCs|jS)N)r�)rUr"r"r#�minimum_version�sz&SecureTransportContext.minimum_version)r�rcCs
||_dS)N)r�)rUr�r"r"r#r��scCs|jS)N)r�)rUr"r"r#�maximum_version�sz&SecureTransportContext.maximum_version)r�rcCs
||_dS)N)r�)rUr�r"r"r#r��s)NNN)NN)FTTN)r�r�r�r�rV�propertyr��setterr�r�r�r�r�r�r�r�r�r�r�r"r"r"r#r�s2
r)r�N)Mr��
__future__rr�r1r/�os.pathrtr�rrkrf�	threadingr��warnings�weakrefrK�rZ_securetransport.bindingsrrZ_securetransport.low_levelr	r
rrr
rr�warn�DeprecationWarning�
TYPE_CHECKING�typing_extensionsr�__all__r rr$�WeakValueDictionaryr�__annotations__�Lockr�r�r�r�r�r�r�r�r�rr�rrr�rrr�r�r��TLSv1_1�TLSv1_2r�rrrrCrIZSSLReadFuncr�ZSSLWriteFuncr�rJr�rr"r"r"r#�<module>5s�$







98

-