ó õ«‰]c@@sŸdZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlmZmZmZddlmZmZmZmZydd l mZWn'ek r eZdd lmZnXd d gZeZejZ ej!j"Z#e j$ƒZ%e j&ƒZ'd Z(ej)ej*ej+ej,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ej<ej=ej>ej?ej@ejAejBejCejDejEejFgZGiejHejIfejJ6ZKeLe dƒrTejMejMfeKe jN 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(tutili(tSecurityt SecurityConsttCoreFoundation(t_assert_no_errort_cert_array_from_pemt_temporary_keychaint_load_client_cert_chain(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(((s@/tmp/pip-build-Q8jF4w/urllib3/urllib3/contrib/securetransport.pyR ·s      cC@sCtt_ttj_tt_ttj_tt_ttj_dS(s> Undo monkey-patching by :func:`inject_into_urllib3`. N(torig_util_SSLContextRRRtorig_util_HAS_SNIRtFalseR(((s@/tmp/pip-build-Q8jF4w/urllib3/urllib3/contrib/securetransport.pyR Ãs      c C@sÓd}y—tj|ƒ}|dkr+tjS|j}|d}|jƒ}d}d}y³x¬||kr|dks€|dkr­tj||ƒs­tj t j dƒ‚q­n||} t j | j||ƒ} |j| | ƒ} || 7}| s\|stjSPq\q\WWnotj k rz} | j }|dk r{|t j kr{||d<|t jksj|t jkrqtjS‚q{nX||d<||kr˜tjSdSWn/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((s@/tmp/pip-build-Q8jF4w/urllib3/urllib3/contrib/securetransport.pyt_read_callbackÏsN                c C@s±d}yutj|ƒ}|dkr+tjS|j}|d}tj||ƒ}|jƒ}d}d} yxx| |krå|dks’|dkr¿t j ||ƒs¿tj t j dƒ‚q¿n|j|ƒ} | | 7} || }qnWWnotj k rX} | j }|dk rY|t j krY| |d<|t jksH|t jkrOtjS‚qYnX| |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(RRRRRRR%t string_atR Rtwait_for_writeR"R#R$tsendR*R+R,R-R.R/( R0R1R2R3R4tbytes_to_writetdataR6R"tsentt chunk_sentR;((s@/tmp/pip-build-Q8jF4w/urllib3/urllib3/contrib/securetransport.pyt_write_callbacksD            t WrappedSocketcB@sÈeZdZd„Zejd„ƒZd„Zd„Zd„Z d„Z d„Z d„Z dd „Zd „Zd „Zd „Zd „Zd„Zd„Zed„Zd„Zd„Zd„ZRS(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 dƒdS(Ni( RRtcontextt_makefile_refsRt_closedR/t _keychaint _keychain_dirt_client_cert_chainR t_timeoutt settimeout(tselfR((s@/tmp/pip-build-Q8jF4w/urllib3/urllib3/contrib/securetransport.pyt__init__Hs        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(RR/tclose(RNt exception((s@/tmp/pip-build-Q8jF4w/urllib3/urllib3/contrib/securetransport.pyt_raise_on_errorZs  cC@sEtjttƒtŒ}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_SUITEStSSLSetEnabledCiphersRFR(RNtcipherstresult((s@/tmp/pip-build-Q8jF4w/urllib3/urllib3/contrib/securetransport.pyt _set_ciphersosc C@s‚|s dStjj|ƒrCt|dƒ}|jƒ}WdQXnd}tjƒ}z¹t|ƒ}tj |j t j |ƒƒ}t |ƒ|s¤tjdƒ‚ntj||ƒ}t |ƒtj|tƒ}t |ƒtjƒ}tj|t j |ƒƒ}t |ƒWd|r'tj|ƒn|dk rCtj|ƒnXtjtjf}|j|kr~tjd|jƒ‚ndS(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. NtrbsFailed to copy trust references)certificate verify failed, error code: %d(tostpathtisfiletopentreadRRt SecTrustRefRtSSLCopyPeerTrustRFR%tbyrefRtssltSSLErrortSecTrustSetAnchorCertificatest!SecTrustSetAnchorCertificatesOnlyRtSecTrustResultTypetSecTrustEvaluateRt CFReleaseRtkSecTrustResultUnspecifiedtkSecTrustResultProceedtvalue( RNtverifyt trust_bundletft cert_arrayttrustRXt trust_resultt successes((s@/tmp/pip-build-Q8jF4w/urllib3/urllib3/contrib/securetransport.pyt_custom_validate|s:        c C@s‘tjdtjtjƒ|_tj|jtt ƒ} t | ƒt @t |ƒd} x| t krw| dd} qZW|t | 3s¬         "        7 6ÿŸ