zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
U
 ý°d\?ã@sŽdZddlmZddlZddlZddlZddlZddlZddlZddl    Z    ddl
Z
ddl Z ddl m Z mZmZmZmZmZmZmZmZmZe dej¡Zddd    œd
d „Zd d dœdd„Zdddœdd„Zdddœdd„Zdddœdd„ZdGddd d!œd"d#„Zdd$d%œd&d'„Z d(d)d*œd+d,„Z!d(d)d*œd-d.„Z"d/d0œd1d2„Z#d3d4d5d6œd7d8„Z$d3dd$d9œd:d;„Z%d<d=d>d?d@dAœZ&d4ddBœdCdD„Z'GdEdF„dFƒZ(dS)Ha¦
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.
é)Ú annotationsNé)
ÚCFArrayÚCFConstÚCFDataÚ CFDictionaryÚCFMutableArrayÚCFStringÚ    CFTypeRefÚCoreFoundationÚSecKeychainRefÚSecuritys;-----BEGIN CERTIFICATE-----
(.*?)
-----END CERTIFICATE-----Úbytesr)Ú
bytestringÚreturncCst 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)r©rúad:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\urllib3/contrib/_securetransport/low_level.pyÚ_cf_data_from_bytes)s
ÿrz#list[tuple[typing.Any, typing.Any]]r)ÚtuplesrcCsZt|ƒ}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ÚtrrrÚ    <genexpr><sz-_cf_dictionary_from_tuples.<locals>.<genexpr>css|]}|dVqdS)rNrrrrrr=s)rr r
ZCFDictionaryCreaterZkCFTypeDictionaryKeyCallBacksZkCFTypeDictionaryValueCallBacks)rZdictionary_sizeÚkeysÚvaluesZcf_keysZ    cf_valuesrrrÚ_cf_dictionary_from_tuples3súrr    )Úpy_bstrrcCs 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 ZCFStringCreateWithCStringrrÚkCFStringEncodingUTF8)rZc_strÚcf_strrrrÚ_cfstrKs
ýr$z list[bytes]r)Úlstrc
Cs°d}zdt tjdt tj¡¡}|s*tdƒ‚|D]6}t|ƒ}|sFtdƒ‚zt     ||¡W5t |¡Xq.WnBt
k
rª}z$|rˆt |¡t   d|›¡d‚W5d}~XYnX|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: ) r ÚCFArrayCreateMutablerr ÚbyrefÚkCFTypeArrayCallBacksÚ MemoryErrorr$Ú    CFReleaseÚCFArrayAppendValueÚ BaseExceptionÚsslÚSSLError)r%Zcf_arrÚitemr#ÚerrrÚ_create_cfstring_arrayYs*
ý
$r2z
str | None)ÚvaluercCsnt |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) r ÚcastÚPOINTERÚc_void_pr ZCFStringGetCStringPtrrr"Úcreate_string_bufferZCFStringGetCStringÚOSErrorr3Údecode)r3Zvalue_as_void_pÚstringÚbufferÚresultrrrÚ_cf_string_to_unicodexs&ÿ
ÿ
r>Úintztype[BaseException] | NoneÚNone)ÚerrorÚexception_classrcCs^|dkr dSt |d¡}t|ƒ}t |¡|dks:|dkrDd|›}|dkrRtj}||ƒ‚dS)z[
    Checks the return code and throws an exception if there is an error to
    report
    rNÚz    OSStatus )r ZSecCopyErrorMessageStringr>r r+r.r/)rArBZcf_error_stringÚoutputrrrÚ_assert_no_error‘s 
 
rEr)Ú
pem_bundlercCsÚ| 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 |¡qZWn tk
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ÚmatchrrrÚ
<listcomp>°sz(_cert_array_from_pem.<locals>.<listcomp>zNo root certificates specifiedrr&zUnable to build cert object!)ÚreplaceÚ _PEM_CERTS_REÚfinditerr.r/r r'rr r(r)rr ZSecCertificateCreateWithDatar+r,Ú    Exception)rFZ    der_certsZ
cert_arrayZ    der_bytesZcertdataÚcertrrrÚ_cert_array_from_pem¨s> ÿ
 
ý
 
ÿ
 
 
rRr
Úbool)r0rcCst ¡}t |¡|kS)z=
    Returns True if a given CFTypeRef is a certificate.
    )r ZSecCertificateGetTypeIDr Ú CFGetTypeID©r0ÚexpectedrrrÚ_is_certÖsrWcCst ¡}t |¡|kS)z;
    Returns True if a given CFTypeRef is an identity.
    )r ZSecIdentityGetTypeIDr rTrUrrrÚ _is_identityÞsrXztuple[SecKeychainRef, str])rc
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ér4F)ÚosÚurandomrHÚ    b16encoder:ÚtempfileÚmkdtempÚpathÚjoinÚencoder r ZSecKeychainCreaterr r(rE)Z random_bytesÚfilenameÚpasswordZ tempdirectoryZ keychain_pathÚkeychainÚstatusrrrÚ_temporary_keychainæs 
ÿrgr Ústrz'tuple[list[CFTypeRef], list[CFTypeRef]])rer`rc 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Úreadr r+rrrZ
CFArrayRefr Z SecItemImportr r(rEZCFArrayGetCountÚrangeZCFArrayGetValueAtIndexr5r
rWZCFRetainÚappendrX) rer`Ú certificatesÚ
identitiesZ result_arrayÚfZ raw_filedataZfiledatar=Z result_countÚindexr0rrrÚ_load_items_from_file    sJ     ÿø
 
 
 
 
 rr)reÚpathsrc
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)rr`rrrrfsz*_load_client_cert_chain.<locals>.<genexpr>rN)Ú    itertoolsÚchainr r+rrÚextendr ZSecIdentityRefZ SecIdentityCreateWithCertificater r(rErmÚpopr'rr)r,) rersrnroZfiltered_pathsÚobjÚ    file_pathZnew_identitiesZ    new_certsZ new_identityrfZ trust_chainr0rrrÚ_load_client_cert_chainBs8 
 ÿ
 
ýrz)ré)ér)r|r)r|r{)r|r|)ZSSLv2ÚSSLv3ÚTLSv1zTLSv1.1zTLSv1.2)Úversionrc    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)    rZver_majZver_minZseverity_fatalZdescription_unknown_caÚmsgZmsg_lenZrecord_type_alertÚrecordrrrÚ_build_tls_unknown_ca_alert–s r‡c@s°eZdZdZdZdZdZdZdZdZ    dZ
d    Z dZ dZ dZdZdZd
ZdZd Zd ZdZd ZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(d Z)d!Z*d"Z+d#S)$Ú SecurityConstzU
    A class object that acts as essentially a namespace for Security constants.
    rrr{éérZé
içr|ééi¸ÙÿÿiµÙÿÿi³Ùÿÿi¨Ùÿÿi²Ùÿÿi±Ùÿÿi¯Ùÿÿi®ÙÿÿiªÙÿÿi©Ùÿÿi¬Ùÿÿi«ÙÿÿiÙÿÿi Ùÿÿi‘Ùÿÿi†ÙÿÿiÙÿÿi‰Ùÿÿi ÷þÿiQÿÿi,ÿÿiRÿÿN),Ú__name__Ú
__module__Ú __qualname__Ú__doc__Z"kSSLSessionOptionBreakOnServerAuthZ kSSLProtocol2Z kSSLProtocol3Z kTLSProtocol1ZkTLSProtocol11ZkTLSProtocol12ZkTLSProtocol13ZkTLSProtocolMaxSupportedZkSSLClientSideZkSSLStreamTypeZkSecFormatPEMSequenceZkSecTrustResultInvalidZkSecTrustResultProceedZkSecTrustResultDenyZkSecTrustResultUnspecifiedZ&kSecTrustResultRecoverableTrustFailureZ kSecTrustResultFatalTrustFailureZkSecTrustResultOtherErrorZerrSSLProtocolZerrSSLWouldBlockZerrSSLClosedGracefulZerrSSLClosedNoNotifyZerrSSLClosedAbortZerrSSLXCertChainInvalidZ errSSLCryptoZerrSSLInternalZerrSSLCertExpiredZerrSSLCertNotYetValidZerrSSLUnknownRootCertZerrSSLNoRootCertZerrSSLHostNameMismatchZerrSSLPeerHandshakeFailZerrSSLPeerUserCancelledZerrSSLWeakPeerEphemeralDHKeyZerrSSLServerAuthCompletedZerrSSLRecordOverflowZerrSecVerifyFailedZerrSecNoTrustSettingsZerrSecItemNotFoundZerrSecInvalidTrustSettingsrrrrrˆ¤sRrˆ)N))r‘Ú
__future__rrHr rtr[Úrer.rƒr^ÚtypingZbindingsrrrrrr    r
r r r ÚcompileÚDOTALLrNrrr$r2r>rErRrWrXrgrrrzr‚r‡rˆrrrrÚ<module>sH     0ÿ
ÿ.#9Lû