1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
U
I=®d´.ã@s´dZddlZddlZddlZddlZddlZddlZddlZddlm    Z    m
Z
m Z e  dej ¡Zdd„Zdd    „Zd
d „Zdd d „Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„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é)ÚSecurityÚCoreFoundationÚCFConsts;-----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)Z
bytestring©r    úmD:\z\workplace\VsCode\pyvenv\venv\Lib\site-packages\pip/_vendor/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.
    css|]}|dVqdS)rNr    ©Ú.0Útr    r    r
Ú    <genexpr>,sz-_cf_dictionary_from_tuples.<locals>.<genexpr>css|]}|dVqdS)rNr    r r    r    r
r-s)rrÚ    CFTypeRefZCFDictionaryCreaterZkCFTypeDictionaryKeyCallBacksZkCFTypeDictionaryValueCallBacks)ZtuplesZdictionary_sizeÚkeysÚvaluesZcf_keysZ    cf_valuesr    r    r
Ú_cf_dictionary_from_tuples%súrcCsnt |t tj¡¡}t |tj¡}|dkrXt d¡}t     ||dtj¡}|sRt
dƒ‚|j }|dk    rj|  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) ÚctypesÚcastÚPOINTERÚc_void_prZCFStringGetCStringPtrrZkCFStringEncodingUTF8Úcreate_string_bufferZCFStringGetCStringÚOSErrorÚvalueÚdecode)rZvalue_as_void_pÚstringÚbufferÚresultr    r    r
Ú_cf_string_to_unicode;s&ÿ
ÿ
r cCs\|dkr dSt |d¡}t|ƒ}t |¡|dks:|dkrBd|}|dkrPtj}||ƒ‚dS)z[
    Checks the return code and throws an exception if there is an error to
    report
    rNÚz OSStatus %s)rZSecCopyErrorMessageStringr rÚ    CFReleaseÚsslÚSSLError)ÚerrorZexception_classZcf_error_stringÚoutputr    r    r
Ú_assert_no_errorTs 
r'cCsØ| 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 |¡qZWntk
rÒt |¡YnX|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
ó
cSsg|]}t | d¡¡‘qS)r)Úbase64Ú    b64decodeÚgroup)r Úmatchr    r    r
Ú
<listcomp>qsz(_cert_array_from_pem.<locals>.<listcomp>zNo root certificates specifiedrzUnable to allocate memory!zUnable to build cert object!)ÚreplaceÚ _PEM_CERTS_REÚfinditerr#r$rÚCFArrayCreateMutablerrÚbyrefÚkCFTypeArrayCallBacksr rZSecCertificateCreateWithDatar"ÚCFArrayAppendValueÚ    Exception)Z
pem_bundleZ    der_certsZ
cert_arrayZ    der_bytesZcertdataÚcertr    r    r
Ú_cert_array_from_pemis< ÿ
 
ý
 
ÿ
 
 r7cCst ¡}t |¡|kS)z=
    Returns True if a given CFTypeRef is a certificate.
    )rZSecCertificateGetTypeIDrÚ CFGetTypeID©ÚitemÚexpectedr    r    r
Ú_is_cert–sr<cCst ¡}t |¡|kS)z;
    Returns True if a given CFTypeRef is an identity.
    )rZSecIdentityGetTypeIDrr8r9r    r    r
Ú _is_identityžsr=c
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érF)ÚosÚurandomr)Ú    b16encoderÚtempfileÚmkdtempÚpathÚjoinÚencoderZSecKeychainRefZSecKeychainCreaterrr2r')Z random_bytesÚfilenameÚpasswordZ tempdirectoryZ keychain_pathÚkeychainÚstatusr    r    r
Ú_temporary_keychain¦s 
ÿrLc Csg}g}d}t|dƒ}| ¡}W5QRXz²t tj|t|ƒ¡}t ¡}t     |ddddd|t
  |¡¡}t |ƒt  |¡}    t|    ƒD]X}
t ||
¡} t
 | tj¡} t| ƒr¼t | ¡| | ¡q€t| ƒr€t | ¡| | ¡q€W5|rìt |¡t |¡X||fS)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Úreadrr"rrrZ
CFArrayRefrZ SecItemImportrr2r'ZCFArrayGetCountÚrangeZCFArrayGetValueAtIndexrrr<ZCFRetainÚappendr=) rJrEÚ certificatesÚ
identitiesZ result_arrayÚfZ raw_filedataZfiledatarZ result_countÚindexr:r    r    r
Ú_load_items_from_fileÉsJ ÿø
 
 
 
 
 rVc
Gsêg}g}dd„|Dƒ}z°|D]&}t||ƒ\}}| |¡| |¡q|sŠt ¡}t ||dt     
|¡¡}    t |    ƒ|  |¡t |  d¡¡t tjdt     
tj¡¡}
t ||¡D]} t |
| ¡q®|
W¢St ||¡D]}t |¡qÔXdS)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    )r rEr    r    r
r$sz*_load_client_cert_chain.<locals>.<genexpr>rN)Ú    itertoolsÚchainrr"rVÚextendrZSecIdentityRefZ SecIdentityCreateWithCertificaterr2r'rQÚpopr1rr3r4) rJÚpathsrRrSÚobjÚ    file_pathZnew_identitiesZ    new_certsZ new_identityrKZ trust_chainr:r    r    r
Ú_load_client_cert_chains8 
 ÿ
 
ýr^)N)Ú__doc__r)rrWÚrer@r#rCZbindingsrrrÚcompileÚDOTALLr/r rr r'r7r<r=rLrVr^r    r    r    r
Ú<module>s,    ÿ
 
-#7