Eu\dc@@sdZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlmZddlmZddlmZmZmZdd lmZmZmZmZmZmZydd l mZWn'ek rAe Zdd l!m"Z"nXd d gZ#e$Z%ej%Z&ej'j(Z)e j*Z+e j,Z-dZ.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ej<ej=ej>ej?ej@ejAejBejCejDejEejFejGejHejIejJejKejLgZMiejNejOfejP6ejNejOfe6ZQeRe drejSejSfeQe jT 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. i(tabsolute_importNi(tutil(tPROTOCOL_TLS_CLIENTi(tCoreFoundationtSecurityt SecurityConst(t_assert_no_errort_build_tls_unknown_ca_alertt_cert_array_from_pemt_create_cfstring_arrayt_load_client_cert_chaint_temporary_keychain(t _fileobject(tbackport_makefiletinject_into_urllib3textract_from_urllib3i@tPROTOCOL_SSLv2tPROTOCOL_SSLv3tPROTOCOL_TLSv1tPROTOCOL_TLSv1_1tPROTOCOL_TLSv1_2cC@sCtt_ttj_tt_ttj_tt_ttj_dS(sG Monkey-patch urllib3 with SecureTransport-backed SSL-support. N(tSecureTransportContextRt SSLContexttssl_tHAS_SNItTruetIS_SECURETRANSPORT(((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyRs      cC@sCtt_ttj_tt_ttj_tt_ttj_dS(s> Undo monkey-patching by :func:`inject_into_urllib3`. N(torig_util_SSLContextRRRtorig_util_HAS_SNIRtFalseR(((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyRs      c C@sd}ytj|}|dkr+tjS|j}|d}|j}d}d}yx||kr|dks|dkrtj||stj t j dqn||} t j | j||} |j| | } || 7}| s\|stjSPq\q\WWnotj k rz} | j }|dk r{|t j kr{||d<|t jksj|t jkrqtjSq{nX||d<||krtjSdSWn/tk r} |dk r| |_ntjSXdS(ss SecureTransport read callback. This is called by ST to request that data be returned from the socket. is timed outN(tNonet_connection_refstgetRterrSSLInternaltsockett gettimeoutRt wait_for_readterrorterrnotEAGAINtctypestc_chart from_addresst recv_intoterrSSLClosedGracefult ECONNRESETtEPIPEterrSSLClosedAbortterrSSLWouldBlockt Exceptiont _exception( t connection_idt data_buffertdata_length_pointertwrapped_sockett base_sockettrequested_lengthttimeoutR%t read_countt remainingtbuffert chunk_sizete((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyt_read_callbacksN                c C@sd}yutj|}|dkr+tjS|j}|d}tj||}|j}d}d} yxx| |kr|dks|dkrt j ||stj t j dqn|j|} | | 7} || }qnWWnotj k rX} | j }|dk rY|t j krY| |d<|t jksH|t jkrOtjSqYnX| |d<| |krvtjSdSWn/tk r} |dk r| |_ntjSXdS(sx SecureTransport write callback. This is called by ST to request that data actually be sent on the network. is timed outN(RRR RR!R"R(t string_atR#Rtwait_for_writeR%R&R'tsendR-R.R/R0R1R2( R3R4R5R6R7tbytes_to_writetdataR9R%tsentt chunk_sentR>((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyt_write_callback sD            t WrappedSocketcB@seZdZdZejdZdZdZdZ dZ dZ dZ d Z d Zdd Zd Zd ZdZdZdZdZedZdZdZdZRS(s API-compatibility wrapper for Python's OpenSSL wrapped socket object. Note: _makefile_refs, _drop(), and _reuse() are needed for the garbage collector of PyPy. cC@sn||_d|_d|_t|_d|_d|_d|_d|_ |jj |_ |jj ddS(Ni( R"Rtcontextt_makefile_refsRt_closedR2t _keychaint _keychain_dirt_client_cert_chainR#t_timeoutt settimeout(tselfR"((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyt__init__Ns        cc@sGd|_dV|jdk rC|jd}|_|j|ndS(s] 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(RR2tclose(RQt exception((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyt_raise_on_error`s  cC@sEtjttt}tj|j|tt}t|dS(s4 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(RtSSLCipherSuitetlent CIPHER_SUITEStSSLSetEnabledCiphersRIR(RQtcipherstresult((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyt _set_ciphersuscC@sN|s dSt|}z#tj|j|}t|Wdtj|XdS(s< Sets up the ALPN protocols on the context. N(R RtSSLSetALPNProtocolsRIRRt CFRelease(RQt protocolst protocols_arrR[((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyt_set_alpn_protocolss c C@s|s dStjtjf}y0|j|}||kr>dSd|f}Wn tk rn}d|f}nXt|j}|jj|t j ddd}|jj tj tj ||jtjd|dS(s 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. Raises an SSLError if the connection is not trusted. Nserror code: %ds exception: %rtiiiiscertificate verify failed, %s(RtkSecTrustResultUnspecifiedtkSecTrustResultProceedt_evaluate_trustR1RtversionR"tsendalltstructtpackt setsockoptt SOL_SOCKETt SO_LINGERRStssltSSLError( RQtverifyt trust_bundlet successest trust_resulttreasonR>trectopts((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyt_custom_validates"   cC@sAtjj|r9t|d}|j}WdQXnd}tj}zt|}tj |j t j |}t ||stjdntj||}t |tj|t}t |tj}tj|t j |}t |Wd|rtj|n|dk r9tj|nX|jS(NtrbsFailed to copy trust reference(tostpathtisfiletopentreadRRt SecTrustRefRtSSLCopyPeerTrustRIR(tbyrefRRmRntSecTrustSetAnchorCertificatest!SecTrustSetAnchorCertificatesOnlyRtSecTrustResultTypetSecTrustEvaluateRR^tvalue(RQRptft cert_arrayttrustR[Rr((sx/private/var/folders/49/mc35vj0j30g7w0ryjvyy2vz80000gs/T/pip-target-MP7kiJ/lib/python/urllib3/contrib/securetransport.pyRes.       c C@shtjdtjtj|_tj|jtt } t | t @t |d} x| t krw| dd} qZW|t | 5s           .         7 6