a
t~®`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)Z
bytestring© r úN/usr/lib/python3.9/site-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 s | ]}|d V qdS )r Nr ©Ú.0Útr r r
Ú , ó z-_cf_dictionary_from_tuples..c s s | ]}|d V qdS )r Nr r r r r
r - r )r r Ú CFTypeRefZCFDictionaryCreater ZkCFTypeDictionaryKeyCallBacksZkCFTypeDictionaryValueCallBacks)ZtuplesZdictionary_sizeÚkeysÚvaluesZcf_keysZ 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.
)ÚctypesZc_char_pr ZCFStringCreateWithCStringr r ÚkCFStringEncodingUTF8)Zpy_bstrZc_strÚcf_strr r r
Ú_cfstr; s
ýr c
C sº d}znt t jdt t j¡¡}|s*tdƒ‚| D ]@}t|ƒ}|sFtdƒ‚zt ||¡ W t |¡ q.t |¡ 0 q.W nB t
y´ } z*|rt |¡ t d|f ¡‚W Y d}~n
d}~0 0 |S )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)ZlstZcf_arrÚitemr Úer r r
Ú_create_cfstring_arrayI s*
ý
&r% c C sn t | t t j¡¡}t |tj¡}|du rXt d¡}t ||dtj¡}|sRt
dƒ‚|j}|durj| 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 ÚcastZPOINTERZc_void_pr ZCFStringGetCStringPtrr r Zcreate_string_bufferZCFStringGetCStringÚOSErrorÚvalueÚdecode)r) Zvalue_as_void_pÚstringÚbufferÚresultr r r
Ú_cf_string_to_unicodeh s ÿ
ÿ
r. c C s\ | dkrdS t | d¡}t|ƒ}t |¡ |du s:|dkrBd| }|du rPtj}||ƒ‚dS )z[
Checks the return code and throws an exception if there is an error to
report
r NÚ zOSStatus %s)r ZSecCopyErrorMessageStringr. r r r! r" )ÚerrorZexception_classZcf_error_stringÚoutputr r r
Ú_assert_no_error s
r2 c C sÖ | dd¡} dd„ t | ¡D ƒ}|s.t d¡‚t tjdt tj
¡¡}|sTt d¡‚z`|D ]V}t|ƒ}|stt d¡‚t
tj|¡}t |¡ |sšt d¡‚t ||¡ t |¡ qZW n tyÐ t |¡ Y n0 |S ) 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 )Úbase64Z b64decodeÚgroup)r
Úmatchr r r
Ú
ž s z(_cert_array_from_pem..zNo root certificates specifiedr r zUnable to build cert object!)ÚreplaceÚ
_PEM_CERTS_REÚfinditerr! r" r r r r r r r r ZSecCertificateCreateWithDatar r Ú Exception)Z
pem_bundleZ der_certsZ
cert_arrayZ der_bytesZcertdataZcertr r r
Ú_cert_array_from_pem– s: ÿ
ý
ÿ
r<