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� d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl m
Z
mZmZ e�
dej�Zdd� Zdd � Zd
d� Zdd
� 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-----c C s t �t j| 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_bytes s �r c C sZ t | �}dd� | D �}dd� | D �}tj| |� }tj| |� }t�tj|||tjtj�S )zK
Given a list of Python tuples, create an associated CFDictionary.
c s � � | ]}|d V qdS )r Nr
��.0�tr
r
r � <genexpr>, � � z-_cf_dictionary_from_tuples.<locals>.<genexpr>c s r
)r Nr
r r
r
r r - r )r r � CFTypeRef�CFDictionaryCreater �kCFTypeDictionaryKeyCallBacks�kCFTypeDictionaryValueCallBacks)�tuples�dictionary_size�keys�values�cf_keys� cf_valuesr
r
r �_cf_dictionary_from_tuples% s �r c C s 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 �CFStringCreateWithCStringr r �kCFStringEncodingUTF8)�py_bstr�c_str�cf_strr
r
r �_cfstr; s
�r% c
C s� d}z7t �t jdt�t j��}|std��| D ]}t|�}|s#td��z
t �||� W t � |� qt � |� w W |S t
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 �CFArrayCreateMutabler r �byref�kCFTypeArrayCallBacks�MemoryErrorr% �CFArrayAppendValue� CFRelease�
BaseException�ssl�SSLError)�lst�cf_arr�itemr$ �er
r
r �_create_cfstring_arrayI s0
���
��r4 c C sn t �| t �t j��}t�|tj�}|du r,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.
Ni z'Error copying C string from CFStringRef�utf-8)
r �cast�POINTER�c_void_pr �CFStringGetCStringPtrr r! �create_string_buffer�CFStringGetCString�OSError�value�decode)r= �value_as_void_p�string�buffer�resultr
r
r �_cf_string_to_unicodeh s �
�
rC c C sX | dkrdS t �| d�}t|�}t�|� |du s|dkr!d| }|du r(tj}||��)z[
Checks the return code and throws an exception if there is an error to
report
r N� zOSStatus %s)r �SecCopyErrorMessageStringrC r r, r. r/ )�error�exception_class�cf_error_string�outputr
r
r �_assert_no_error� s
rJ c C s� | � 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 |S tyj t�|� Y |S w ) 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
�
c S s g | ]
}t �|�d ���qS )r )�base64� b64decode�group)r �matchr
r
r �
<listcomp>� s �z(_cert_array_from_pem.<locals>.<listcomp>zNo root certificates specifiedr r&