z]c@@sdZddlmZddlZddlZddlZddlZddlZddl 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 drTejMejMfeKe 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-kBFYxq/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-kBFYxq/urllib3/urllib3/contrib/securetransport.pyR s      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((s@/tmp/pip-build-kBFYxq/urllib3/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(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-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt_write_callbacksD            t WrappedSocketcB@seZdZdZejdZdZdZdZ dZ dZ dZ dd Zd Zd Zd Zd ZdZdZedZdZdZdZRS(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( RRtcontextt_makefile_refsRt_closedR/t _keychaint _keychain_dirt_client_cert_chainR t_timeoutt settimeout(tselfR((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt__init__?s        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-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt_raise_on_errorQs  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_SUITEStSSLSetEnabledCiphersRFR(RNtcipherstresult((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt _set_ciphersfsc C@s|s dStjj|rCt|d}|j}WdQXnd}tj}zt|}tj |j t j |}t ||stjdntj||}t |tj|t}t |tj}tj|t j |}t |Wd|r'tj|n|dk rCtj|nXtjtjf}|j|kr~tjd|jndS(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-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt_custom_validatess@        c C@stjdtjtj|_tj|jtt } t | t @t |d} x| t krw| dd} qZW|t | a    (       >  icC@s%|jd7_t|||dtS(NiRP(RGR R(RNtmodetbufsize((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pytmakefilestrcO@sd}t|||||S(Ni(R (RNRt bufferingtargstkwargs((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyRsRcB@seZdZdZedZejdZedZejdZedZejdZdZ d Z d Z dddd Z ddd Zeeedd ZRS(s I am a wrapper class for the SecureTransport library, to translate the interface of the standard library ``SSLContext`` object to calls into SecureTransport. cC@sPt|\|_|_d|_t|_d|_d|_d|_ d|_ dS(Ni( t_protocol_to_min_maxt _min_versiont _max_versiont_optionsRt_verifyRt _trust_bundlet _client_certt _client_keyt_client_key_passphrase(RNR((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyROs     cC@stS(s SecureTransport cannot have its hostname checking disabled. For more, see the comment on getpeercert() in this file. (R(RN((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pytcheck_hostnamescC@sdS(s SecureTransport cannot have its hostname checking disabled. For more, see the comment on getpeercert() in this file. N((RNRl((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyRscC@s|jS(N(R(RN((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pytoptionsscC@s ||_dS(N(R(RNRl((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyRscC@s|jrtjStjS(N(RRct CERT_REQUIREDt CERT_NONE(RN((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt verify_modescC@s"|tjkrtnt|_dS(N(RcRRRR(RNRl((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyRscC@sdS(N((RN((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pytset_default_verify_pathss cC@s |jS(N(R(RN((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pytload_default_certs)scC@s%|tjjkr!tdndS(Ns5SecureTransport doesn't support custom cipher strings(RRtDEFAULT_CIPHERSR(RNRW((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt set_ciphers,scC@s.|dk rtdn|p$||_dS(Ns1SecureTransport does not support cert directories(RRR(RNtcafiletcapathtcadata((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pytload_verify_locations3s  cC@s||_||_||_dS(N(RRt_client_cert_passphrase(RNtcertfiletkeyfiletpassword((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pytload_cert_chain<s  c C@sl| s t|st|s%tt|}|j||j|j|j|j|j|j|j |S(N( RRERRRRRRRR(RNtsockt server_sidetdo_handshake_on_connecttsuppress_ragged_eofsRR3((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt wrap_socketAs    N(RRRROtpropertyRtsetterRRRRRRRRRRR(((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyRs      (bRt __future__RRR%R#tos.pathR[RRRct threadingtweakreftRt_securetransport.bindingsRRRt_securetransport.low_levelRRRRR t ImportErrorRtpackages.backports.makefileR t__all__RRRRRRtWeakValueDictionaryRtLockR{Rt'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384t'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256t%TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384t%TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256t-TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256t+TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256t#TLS_DHE_RSA_WITH_AES_256_GCM_SHA384t#TLS_DHE_RSA_WITH_AES_128_GCM_SHA256t'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384t$TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHAt'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256t$TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHAt%TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384t"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHAt%TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256t"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHAt#TLS_DHE_RSA_WITH_AES_256_CBC_SHA256t TLS_DHE_RSA_WITH_AES_256_CBC_SHAt#TLS_DHE_RSA_WITH_AES_128_CBC_SHA256t TLS_DHE_RSA_WITH_AES_128_CBC_SHAtTLS_AES_256_GCM_SHA384tTLS_AES_128_GCM_SHA256tTLS_RSA_WITH_AES_256_GCM_SHA384tTLS_RSA_WITH_AES_128_GCM_SHA256tTLS_AES_128_CCM_8_SHA256tTLS_AES_128_CCM_SHA256tTLS_RSA_WITH_AES_256_CBC_SHA256tTLS_RSA_WITH_AES_128_CBC_SHA256tTLS_RSA_WITH_AES_256_CBC_SHAtTLS_RSA_WITH_AES_128_CBC_SHARURRt PROTOCOL_TLSRthasattrRR RRRRRRRR R R<RDt SSLReadFuncRyt SSLWriteFuncRztobjectRERR(((s@/tmp/pip-build-kBFYxq/urllib3/urllib3/contrib/securetransport.pyt3s         "        7 6