Current Dir: /snap/core22/2955/usr/lib/python3/dist-packages/urllib3/contrib/_securetransport/__pycache__/

[FILE] bindings.cpython-310.pyc [ edit | delete | rename ]
[FILE] low_level.cpython-310.pyc [ edit | delete | rename ]
[FILE] __init__.cpython-310.pyc [ edit | delete | rename ]

Viewing: /snap/core22/2955/usr/lib/python3/dist-packages/urllib3/contrib/_securetransport/__pycache__/low_level.cpython-310.pyc

o

�~�`T6�@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZe�
dej�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Zd&dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zddd d!d"d#�Zd$d%�ZdS)'a�
Low-level helpers for the SecureTransport bindings.

These are Python functions that are not directly related to the high-level APIs
but are necessary to get them to work. They include a whole bunch of low-level
CoreFoundation messing about and memory management. The concerns in this module
are almost entirely about trying to avoid memory leaks and providing
appropriate and useful assistance to the higher-level code.
�N�)�CFConst�CoreFoundation�Securitys;-----BEGIN CERTIFICATE-----
(.*?)
-----END CERTIFICATE-----cCst�tj|t|��S)zv
    Given a bytestring, create a CFData object from it. This CFData object must
    be CFReleased by the caller.
    )r�CFDataCreate�kCFAllocatorDefault�len)�
bytestring�r
�L/usr/lib/python3/dist-packages/urllib3/contrib/_securetransport/low_level.py�_cf_data_from_bytess�rcCsZt|�}dd�|D�}dd�|D�}tj||�}tj||�}t�tj|||tjtj�S)zK
    Given a list of Python tuples, create an associated CFDictionary.
    cs��|]}|dVqdS)rNr
��.0�tr
r
r�	<genexpr>,��z-_cf_dictionary_from_tuples.<locals>.<genexpr>csr
)rNr
rr
r
rr-r)rr�	CFTypeRef�CFDictionaryCreater�kCFTypeDictionaryKeyCallBacks�kCFTypeDictionaryValueCallBacks)�tuples�dictionary_size�keys�values�cf_keys�	cf_valuesr
r
r�_cf_dictionary_from_tuples%s�rcCs t�|�}t�tj|tj�}|S)zi
    Given a Python binary data, create a CFString.
    The string must be CFReleased by the caller.
    )�ctypes�c_char_pr�CFStringCreateWithCStringrr�kCFStringEncodingUTF8)�py_bstr�c_str�cf_strr
r
r�_cfstr;s
�r%c
Cs�d}z7t�tjdt�tj��}|std��|D]}t|�}|s#td��z
t�||�Wt�	|�qt�	|�wW|St
yT}z|rHt�	|�t�d|f��d}~ww)z�
    Given a list of Python binary data, create an associated CFMutableArray.
    The array must be CFReleased by the caller.

    Raises an ssl.SSLError on failure.
    Nr�Unable to allocate memory!zUnable to allocate array: %s)
r�CFArrayCreateMutablerr�byref�kCFTypeArrayCallBacks�MemoryErrorr%�CFArrayAppendValue�	CFRelease�
BaseException�ssl�SSLError)�lst�cf_arr�itemr$�er
r
r�_create_cfstring_arrayIs0
���
��r4cCsnt�|t�tj��}t�|tj�}|dur,t�d�}t�	||dtj�}|s)t
d��|j}|dur5|�d�}|S)z�
    Creates a Unicode string from a CFString object. Used entirely for error
    reporting.

    Yes, it annoys me quite a lot that this function is this complex.
    Niz'Error copying C string from CFStringRef�utf-8)
r�cast�POINTER�c_void_pr�CFStringGetCStringPtrrr!�create_string_buffer�CFStringGetCString�OSError�value�decode)r=�value_as_void_p�string�buffer�resultr
r
r�_cf_string_to_unicodehs�

�
rCcCsX|dkrdSt�|d�}t|�}t�|�|dus|dkr!d|}|dur(tj}||��)z[
    Checks the return code and throws an exception if there is an error to
    report
    rN�zOSStatus %s)r�SecCopyErrorMessageStringrCrr,r.r/)�error�exception_class�cf_error_string�outputr
r
r�_assert_no_error�s
rJcCs�|�dd�}dd�t�|�D�}|st�d��t�tjdt�	tj
��}|s*t�d��z1|D]+}t|�}|s:t�d��t�
tj|�}t�|�|sMt�d��t�||�t�|�q-W|Styjt�|�Y|Sw)	z�
    Given a bundle of certs in PEM format, turns them into a CFArray of certs
    that can be used to validate a cert chain.
    s
�
cSsg|]
}t�|�d���qS)r)�base64�	b64decode�group)r�matchr
r
r�
<listcomp>�s�z(_cert_array_from_pem.<locals>.<listcomp>zNo root certificates specifiedrr&zUnable to build cert object!)�replace�
_PEM_CERTS_RE�finditerr.r/rr'rrr(r)rr�SecCertificateCreateWithDatar,r+�	Exception)�
pem_bundle�	der_certs�
cert_array�	der_bytes�certdata�certr
r
r�_cert_array_from_pem�s@�

�

�

���r\cC�t��}t�|�|kS)z=
    Returns True if a given CFTypeRef is a certificate.
    )r�SecCertificateGetTypeIDr�CFGetTypeID�r2�expectedr
r
r�_is_cert��rbcCr])z;
    Returns True if a given CFTypeRef is an identity.
    )r�SecIdentityGetTypeIDrr_r`r
r
r�_is_identity�rcrec
Cs�t�d�}t�|dd���d�}t�|dd��}t��}tj�||��	d�}t
��}t
�|t
|�|ddt�|��}t|�||fS)a�
    This function creates a temporary Mac keychain that we can use to work with
    credentials. This keychain uses a one-time password and a temporary file to
    store the data. We expect to have one keychain per socket. The returned
    SecKeychainRef must be freed by the caller, including calling
    SecKeychainDelete.

    Returns a tuple of the SecKeychainRef and the path to the temporary
    directory that contains it.
    �(N�r5F)�os�urandomrL�	b16encoder>�tempfile�mkdtemp�path�join�encoder�SecKeychainRef�SecKeychainCreaterrr(rJ)�random_bytes�filename�password�
tempdirectory�
keychain_path�keychain�statusr
r
r�_temporary_keychain�s
�rycCs*g}g}d}t|d��}|��}Wd�n1swYzht�tj|t|��}t��}t�|ddddd|t	�
|��}t|�t�|�}	t
|	�D],}
t�||
�}t	�|tj�}t|�rht�|�|�|�qJt|�rvt�|�|�|�qJW|rt�|�t�|�||fS|r�t�|�t�|�w)z�
    Given a single file, loads all the trust objects from it into arrays and
    the keychain.
    Returns a tuple of lists: the first list is a list of identities, the
    second a list of certs.
    N�rbr)�open�readrrrr�
CFArrayRefr�
SecItemImportrr(rJ�CFArrayGetCount�range�CFArrayGetValueAtIndexr6rrb�CFRetain�appendrer,)rwrm�certificates�
identities�result_array�f�raw_filedata�filedatarB�result_count�indexr2r
r
r�_load_items_from_file�sR
���




��

�
r�c
Gsg}g}dd�|D�}ze|D]}t||�\}}|�|�|�|�q|sEt��}t�||dt�|��}t|�|�|�t	�
|�d��t	�t	j
dt�t	j��}	t�||�D]}
t	�|	|
�qW|	Wt�||�D]}t	�
|�qhSt�||�D]}t	�
|�qww)z�
    Load certificates and maybe keys from a number of files. Has the end goal
    of returning a CFArray containing one SecIdentityRef, and then zero or more
    SecCertificateRef objects, suitable for use as a client certificate trust
    chain.
    css�|]}|r|VqdS�Nr
)rrmr
r
rrQrz*_load_client_cert_chain.<locals>.<genexpr>r)r��extendr�SecIdentityRef� SecIdentityCreateWithCertificaterr(rJr�rr,�popr'rr)�	itertools�chainr+)rw�pathsr�r��	file_path�new_identities�	new_certs�new_identityrx�trust_chainr2�objr
r
r�_load_client_cert_chain-s: 
�

���r�)r�)�r)r�r)r�r�)r�r�)�SSLv2�SSLv3�TLSv1zTLSv1.1zTLSv1.2c	CsHt|\}}d}d}t�d||�}t|�}d}t�d||||�|}|S)z6
    Builds a TLS alert record for an unknown CA.
    r��0z>BB�z>BBBH)�TLS_PROTOCOL_VERSIONS�struct�packr)	�version�ver_maj�ver_min�severity_fatal�description_unknown_ca�msg�msg_len�record_type_alert�recordr
r
r�_build_tls_unknown_ca_alert�sr�r�)�__doc__rLrr�rh�rer.r�rk�bindingsrrr�compile�DOTALLrRrrr%r4rCrJr\rbreryr�r�r�r�r
r
r
r�<module>s@	�

-#7L�	
Upload File: