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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
U
I=®d‘~ã@s’dZddlmZddlZddlZddlZddlZddlZddl    Z    ddl
Z
ddl Z ddl Z ddl mZddlmZmZmZddlmZmZmZmZzdd    l    mZWn$ek
rÄdZdd
lmZYnXd d gZd ZejZejj Z!e  "¡Z#e  $¡Z%dZ&ej'ej(ej)ej*ej+ej,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ej<ej=ej>ej?ej@ejAejBejCejDgZEejFejGejHfiZIeJe
dƒr ejKejKfeIe
jL<eJe
dƒr¾ejMejMfeIe
jN<eJe
dƒrÜejGejGfeIe
jO<eJe
dƒrúejPejPfeIe
jQ<eJe
dƒrejHejHfeIe
jR<dd „ZSdd „ZTdd„ZUdd„ZVe WeU¡ZXe YeV¡ZZGdd„de[ƒZ\ernd#dd„Z]n
d$d d„Z]e]e\_]Gd!d"„d"e[ƒZ^dS)%aÊ    
SecureTranport support for urllib3 via ctypes.
 
This makes platform-native TLS available to urllib3 users on macOS without the
use of a compiler. This is an important feature because the Python Package
Index is moving to become a TLSv1.2-or-higher server, and the default OpenSSL
that ships with macOS is not capable of doing TLSv1.2. The only way to resolve
this is to give macOS users an alternative solution to the problem, and that
solution is to use SecureTransport.
 
We use ctypes here because this solution must not require a compiler. That's
because pip is not allowed to require a compiler either.
 
This is not intended to be a seriously long-term solution to this problem.
The hope is that PEP 543 will eventually solve this issue for us, at which
point we can retire this contrib module. But in the short term, we need to
solve the impending tire fire that is Python on Mac without this kind of
contrib module. So...here we are.
 
To use this module, simply import and inject it::
 
    import urllib3.contrib.securetransport
    urllib3.contrib.securetransport.inject_into_urllib3()
 
Happy TLSing!
 
This code is a bastardised version of the code found in Will Bond's oscrypto
library. An enormous debt is owed to him for blazing this trail for us. For
that reason, this code should be considered to be covered both by urllib3's
license and by oscrypto's:
 
    Copyright (c) 2015-2016 Will Bond <will@wbond.net>
 
    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:
 
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
 
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
é)Úabsolute_importNé)Úutilé)ÚSecurityÚ SecurityConstÚCoreFoundation)Ú_assert_no_errorÚ_cert_array_from_pemÚ_temporary_keychainÚ_load_client_cert_chain)Ú _fileobject©Úbackport_makefileÚinject_into_urllib3Úextract_from_urllib3Ti@ÚPROTOCOL_SSLv2ÚPROTOCOL_SSLv3ÚPROTOCOL_TLSv1ÚPROTOCOL_TLSv1_1ÚPROTOCOL_TLSv1_2cCs.tt_ttj_tt_ttj_dt_dtj_dS)zG
    Monkey-patch urllib3 with SecureTransport-backed SSL-support.
    TN)ÚSecureTransportContextrÚ
SSLContextÚssl_ÚHAS_SNIÚIS_SECURETRANSPORT©rrúbD:\z\workplace\VsCode\pyvenv\venv\Lib\site-packages\pip/_vendor/urllib3/contrib/securetransport.pyr´s cCs.tt_ttj_tt_ttj_dt_dtj_dS)z>
    Undo monkey-patching by :func:`inject_into_urllib3`.
    FN)Úorig_util_SSLContextrrrÚorig_util_HAS_SNIrrrrrrrÀs c
Cs”d}zJt |¡}|dkr"tjWS|j}|d}| ¡}d}d}z|||krº|dksZ|dkrtt ||¡stt t    j
d¡‚||}    t j |      ||¡}
| |
|    ¡} || 7}| sB|sºtjWWSqºqBWnttjk
r0} zR| j    }|dk    r |t    j
kr ||d<|t    jks|t    jkrtjWY¢
WS‚W5d} ~ XYnX||d<||krLtjWSWdStk
rŽ} z|dk    rt| |_tjWY¢Sd} ~ XYnXdS)zs
    SecureTransport read callback. This is called by ST to request that data
    be returned from the socket.
    Nrú    timed out)Ú_connection_refsÚgetrÚerrSSLInternalÚsocketÚ
gettimeoutrÚ wait_for_readÚerrorÚerrnoÚEAGAINÚctypesÚc_charÚ from_addressÚ    recv_intoÚerrSSLClosedGracefulÚ
ECONNRESETÚEPIPEÚerrSSLClosedAbortÚerrSSLWouldBlockÚ    ExceptionÚ
_exception) Ú connection_idÚ data_bufferÚdata_length_pointerÚwrapped_socketÚ base_socketZrequested_lengthÚtimeoutr'Z
read_countÚ    remainingÚbufferÚ
chunk_sizeÚerrrÚ_read_callbackÌsP
 
ÿ 
 
 
r?c
Csrd}z(t |¡}|dkr"tjWS|j}|d}t ||¡}| ¡}d}d}    zV|    |kr |dksf|dkr€t     ||¡s€t 
t j d¡‚|  |¡}
|    |
7}    ||
d…}qNWnltj
k
r} zJ| j }|dk    rþ|t j krþ|    |d<|t jksì|t jkrütjWY¢
WS‚W5d} ~ XYnX|    |d<|    |kr*tjWSWdStk
rl} z|dk    rR| |_tjWY¢Sd} ~ XYnXdS)zx
    SecureTransport write callback. This is called by ST to request that data
    actually be sent on the network.
    Nrr )r!r"rr#r$r*Ú    string_atr%rÚwait_for_writer'r(r)Úsendr/r0r1r2r3r4) r5r6r7r8r9Zbytes_to_writeÚdatar:r'ÚsentZ
chunk_sentr>rrrÚ_write_callbacksD
 
 
 
rEc@s²eZdZdZdd„Zejdd„ƒZdd„Zdd    „Z    d
d „Z
d d „Z dd„Z dd„Z d*dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd+d"d#„Zd$d%„Zd&d'„Zd(d)„ZdS),Ú WrappedSocketz²
    API-compatibility wrapper for Python's OpenSSL wrapped socket object.
 
    Note: _makefile_refs, _drop(), and _reuse() are needed for the garbage
    collector of PyPy.
    cCsL||_d|_d|_d|_d|_d|_d|_d|_|j ¡|_    |j 
d¡dS©NrF) r$ÚcontextÚ_makefile_refsÚ_closedr4Ú    _keychainÚ _keychain_dirÚ_client_cert_chainr%Ú_timeoutÚ
settimeout)Úselfr$rrrÚ__init__Es zWrappedSocket.__init__ccs4d|_dV|jdk    r0|jd}|_| ¡|‚dS)a]
        A context manager that can be used to wrap calls that do I/O from
        SecureTransport. If any of the I/O callbacks hit an exception, this
        context manager will correctly propagate the exception after the fact.
        This avoids silently swallowing those exceptions.
 
        It also correctly forces the socket closed.
        N)r4Úclose)rPÚ    exceptionrrrÚ_raise_on_errorWs
 
zWrappedSocket._raise_on_errorcCs2tjttƒtŽ}t |j|ttƒ¡}t|ƒdS)a4
        Sets up the allowed ciphers. By default this matches the set in
        util.ssl_.DEFAULT_CIPHERS, at least as supported by macOS. This is done
        custom and doesn't allow changing at this time, mostly because parsing
        OpenSSL cipher strings is going to be a freaking nightmare.
        N)rZSSLCipherSuiteÚlenÚ CIPHER_SUITESZSSLSetEnabledCiphersrHr    )rPÚciphersÚresultrrrÚ _set_cipherslsÿzWrappedSocket._set_ciphersc        Cs|sdStj |¡r2t|dƒ}| ¡}W5QRXd}t ¡}z€t    |ƒ}t 
|j t   |¡¡}t|ƒ|srt d¡‚t ||¡}t|ƒt |d¡}t|ƒt ¡}t |t   |¡¡}t|ƒW5|rÎt |¡|dk    ràt |¡Xtjtjf}|j|kr
t d|j¡‚dS)zÁ
        Called when we have set custom validation. We do this in two cases:
        first, when cert validation is entirely disabled; and second, when
        using a custom trust DB.
        NÚrbzFailed to copy trust referenceTz)certificate verify failed, error code: %d)ÚosÚpathÚisfileÚopenÚreadrÚ SecTrustRefrÚ    CFReleaser
ÚSSLCopyPeerTrustrHr*Úbyrefr    ÚsslÚSSLErrorZSecTrustSetAnchorCertificatesZ!SecTrustSetAnchorCertificatesOnlyZSecTrustResultTypeZSecTrustEvaluaterZkSecTrustResultUnspecifiedZkSecTrustResultProceedÚvalue)    rPÚverifyÚ trust_bundleÚfZ
cert_arrayÚtrustrXZ trust_resultZ    successesrrrÚ_custom_validateys>  
 
 þ ÿzWrappedSocket._custom_validatec         CsÆt dtjtj¡|_t |jtt¡}    t    |    ƒt
0t |ƒd}
|
t krT|
dd}
q>|t |
<W5QRXt  |j|
¡}    t    |    ƒ|r°t|tƒs”| d¡}t |j|t|ƒ¡}    t    |    ƒ| ¡t |j|¡}    t    |    ƒt |j|¡}    t    |    ƒ|rò|dk    r t |jtjd¡}    t    |    ƒ|rHtƒ\|_|_t|j||ƒ|_t |j|j¡}    t    |    ƒ| ¡ht |j¡}    |    tj krvt! "d¡‚n>|    tj#krž| $||¡W5QR£qHnt    |    ƒW5QR£qÂW5QRXqHdS)z‘
        Actually performs the TLS handshake. This is run automatically by
        wrapped socket, and shouldn't be needed in user code.
        Niÿÿÿrzutf-8Tzhandshake timed out)%rZSSLCreateContextrZkSSLClientSideZkSSLStreamTyperHZ SSLSetIOFuncsÚ_read_callback_pointerÚ_write_callback_pointerr    Ú_connection_ref_lockÚidr!ZSSLSetConnectionÚ
isinstanceÚbytesÚencodeZSSLSetPeerDomainNamerUrYZSSLSetProtocolVersionMinZSSLSetProtocolVersionMaxZSSLSetSessionOptionZ"kSSLSessionOptionBreakOnServerAuthr rKrLr rMZSSLSetCertificaterTZ SSLHandshaker2r$r:ZerrSSLServerAuthCompletedrk) rPÚserver_hostnamergrhÚ min_versionZ max_versionÚ client_certZ
client_keyZclient_key_passphraserXÚhandlerrrÚ    handshake²srÿÿ 
 
ÿÿÿ
     zWrappedSocket.handshakecCs
|j ¡S©N)r$Úfileno©rPrrrry szWrappedSocket.filenocCs*|jdkr|jd8_|jr&| ¡dS)Nrr)rIrJrRrzrrrÚ_decref_socketioss
zWrappedSocket._decref_socketioscCs&t |¡}| ||¡}|d|…}|Srx)r*Úcreate_string_bufferr-)rPZbufsizr<Ú
bytes_readrCrrrÚrecvs
  zWrappedSocket.recvNc    Cs¨|jr
dS|dkrt|ƒ}tj| |¡}t d¡}| ¡t |j    ||t 
|¡¡}W5QRX|t j kr€|j dkr¢t d¡‚n"|t jt jfkrš| ¡nt|ƒ|j S)Nrzrecv timed out)rJrUr*r+Ú from_bufferÚc_size_trTrZSSLReadrHrcrr2rfr$r:r.ZerrSSLClosedNoNotifyrRr    )rPr<ÚnbytesÚprocessed_bytesrXrrrr-s.
 
ÿ
 
 þ
zWrappedSocket.recv_intocCs
||_dSrx©rN)rPr:rrrrOFszWrappedSocket.settimeoutcCs|jSrxrƒrzrrrr%IszWrappedSocket.gettimeoutc    Csht d¡}| ¡"t |j|t|ƒt |¡¡}W5QRX|tj    krZ|j
dkrZt   d¡‚nt |ƒ|j
S)Nrzsend timed out)r*r€rTrZSSLWriterHrUrcrr2rfr$r:r    )rPrCr‚rXrrrrBLs
 
ÿ zWrappedSocket.sendcCs4d}|t|ƒkr0| |||t…¡}||7}qdS©Nr)rUrBÚSSL_WRITE_BLOCKSIZE)rPrCÚ
total_sentrDrrrÚsendall]s zWrappedSocket.sendallc    Cs$| ¡t |j¡W5QRXdSrx)rTrZSSLCloserHrzrrrÚshutdowncs
zWrappedSocket.shutdowncCs’|jdkr€d|_|jr(t |j¡d|_|jr@t |j¡d|_|jrvt |j¡t |j¡t     
|j ¡d|_|_ |j   ¡S|jd8_dS)NrT)rIrJrHrrarMrKrZSecKeychainDeleteÚshutilÚrmtreerLr$rRrzrrrrRgs
 
zWrappedSocket.closeFc
    CsÈ|s tdƒ‚t ¡}d}d}zˆt |jt |¡¡}t    |ƒ|sFW¢ddSt 
|¡}|s\W¢NdSt  |d¡}|spt ‚t  |¡}|s‚t ‚t |¡}t |¡}    t |    |¡}W5|r´t |¡|rÂt |¡X|S)Nz2SecureTransport only supports dumping binary certsr)Ú
ValueErrorrr`rrarbrHr*rcr    ZSecTrustGetCertificateCountZSecTrustGetCertificateAtIndexÚAssertionErrorZSecCertificateCopyDataZCFDataGetLengthZCFDataGetBytePtrr@)
rPÚ binary_formrjZcertdataZ    der_bytesrXZ
cert_countZleafZ data_lengthr6rrrÚ getpeercertzs2
 
 
 
 
 zWrappedSocket.getpeercertcCsžt ¡}t |jt |¡¡}t|ƒ|jtj    kr<t
  d¡‚n^|jtj krLdS|jtj kr\dS|jtjkrldS|jtjkr|dS|jtjkrŒdSt
  d|¡‚dS)Nz(SecureTransport does not support TLS 1.3zTLSv1.2zTLSv1.1ÚTLSv1ÚSSLv3ZSSLv2zUnknown TLS version: %r)rZ SSLProtocolZSSLGetNegotiatedProtocolVersionrHr*rcr    rfrZkTLSProtocol13rdreÚkTLSProtocol12ÚkTLSProtocol11Ú kTLSProtocol1Ú kSSLProtocol3Ú kSSLProtocol2)rPÚprotocolrXrrrÚversion´s&ÿ       zWrappedSocket.versioncCs|jd7_dS©Nr)rIrzrrrÚ_reuseÉszWrappedSocket._reusecCs&|jdkr| ¡n|jd8_dSr˜)rIrRrzrrrÚ_dropÌs
 
zWrappedSocket._drop)N)F)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rQÚ
contextlibÚcontextmanagerrTrYrkrwryr{r~r-rOr%rBr‡rˆrRrŽr—r™ršrrrrrF=s*
 9Y
+
:rFéÿÿÿÿcCs|jd7_t|||ddS)NrT)rR)rIr )rPÚmodeÚbufsizerrrÚmakefileÕsr¤ÚrcOsd}t|||f|ž|ŽSr„r)rPr¢Ú    bufferingÚargsÚkwargsrrrr¤Üsc@sœeZdZdZdd„Zedd„ƒZejdd„ƒZedd„ƒZejd    d„ƒZed
d „ƒZ    e    jd d „ƒZ    d d„Z
dd„Z dd„Z ddd„Z ddd„Zddd„ZdS)rz¯
    I am a wrapper class for the SecureTransport library, to translate the
    interface of the standard library ``SSLContext`` object to calls into
    SecureTransport.
    cCs8t|\|_|_d|_d|_d|_d|_d|_d|_dSrG)    Ú_protocol_to_min_maxÚ _min_versionÚ _max_versionÚ_optionsÚ_verifyÚ _trust_bundleÚ _client_certÚ _client_keyÚ_client_key_passphrase)rPr–rrrrQíszSecureTransportContext.__init__cCsdS)úŽ
        SecureTransport cannot have its hostname checking disabled. For more,
        see the comment on getpeercert() in this file.
        TrrzrrrÚcheck_hostnameösz%SecureTransportContext.check_hostnamecCsdS)r²Nr©rPrfrrrr³þscCs|jSrx©r¬rzrrrÚoptionsszSecureTransportContext.optionscCs
||_dSrxrµr´rrrr¶scCs|jr tjStjSrx)r­rdÚ CERT_REQUIREDÚ    CERT_NONErzrrrÚ verify_modesz"SecureTransportContext.verify_modecCs|tjkrdnd|_dS)NTF)rdr·r­r´rrrr¹scCsdSrxrrzrrrÚset_default_verify_pathss
z/SecureTransportContext.set_default_verify_pathscCs| ¡Srx)rºrzrrrÚload_default_certs)sz)SecureTransportContext.load_default_certscCs|tjjkrtdƒ‚dS)Nz5SecureTransport doesn't support custom cipher strings)rrÚDEFAULT_CIPHERSr‹)rPrWrrrÚ set_ciphers,s z"SecureTransportContext.set_ciphersNc    Cs:|dk    rtdƒ‚|dk    r,t|ƒW5QRX|p2||_dS)Nz1SecureTransport does not support cert directories)r‹r^r®)rPÚcafileÚcapathÚcadatarrrÚload_verify_locations1s 
 
z,SecureTransportContext.load_verify_locationscCs||_||_||_dSrx)r¯r°Z_client_cert_passphrase)rPÚcertfileÚkeyfileÚpasswordrrrÚload_cert_chain=sz&SecureTransportContext.load_cert_chainFTc
CsJ|rt‚|st‚|st‚t|ƒ}| ||j|j|j|j|j|j|j    ¡|Srx)
rŒrFrwr­r®rªr«r¯r°r±)rPÚsockÚ server_sideÚdo_handshake_on_connectÚsuppress_ragged_eofsrsr8rrrÚ wrap_socketBs ø
z"SecureTransportContext.wrap_socket)NNN)NN)FTTN)r›rœrržrQÚpropertyr³Úsetterr¶r¹rºr»r½rÁrÅrÊrrrrræs0    
 
 
    
 
 
 
 
úr)r¡)r¥N)_ržÚ
__future__rrŸr*r(Úos.pathr[r‰r$rdÚ    threadingÚweakrefÚrZ_securetransport.bindingsrrrZ_securetransport.low_levelr    r
r r r Ú ImportErrorZpackages.backports.makefilerÚ__all__rrrrrÚWeakValueDictionaryr!ÚLockrnr…Z'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384Z'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256Z%TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384Z%TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256Z-TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256Z+TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256Z#TLS_DHE_RSA_WITH_AES_256_GCM_SHA384Z#TLS_DHE_RSA_WITH_AES_128_GCM_SHA256Z'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384Z$TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHAZ'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256Z$TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHAZ%TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384Z"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHAZ%TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256Z"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHAZ#TLS_DHE_RSA_WITH_AES_256_CBC_SHA256Z TLS_DHE_RSA_WITH_AES_256_CBC_SHAZ#TLS_DHE_RSA_WITH_AES_128_CBC_SHA256Z TLS_DHE_RSA_WITH_AES_128_CBC_SHAZTLS_AES_256_GCM_SHA384ZTLS_AES_128_GCM_SHA256ZTLS_RSA_WITH_AES_256_GCM_SHA384ZTLS_RSA_WITH_AES_128_GCM_SHA256ZTLS_AES_128_CCM_8_SHA256ZTLS_AES_128_CCM_SHA256ZTLS_RSA_WITH_AES_256_CBC_SHA256ZTLS_RSA_WITH_AES_128_CBC_SHA256ZTLS_RSA_WITH_AES_256_CBC_SHAZTLS_RSA_WITH_AES_128_CBC_SHArVÚ PROTOCOL_TLSr“r‘r©Úhasattrr•rr”rrr’rrrrr?rEZ SSLReadFuncrlZ SSLWriteFuncrmÚobjectrFr¤rrrrrÚ<module>s¼3  â%
ÿ þ
 þ
 þ
 þ
 þ
  76