a t~®`q†ã@sÀdZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlmZddlmZddlmZmZmZdd lmZmZmZmZmZmZzdd l mZWn"eyædZdd l m!Z!Yn0d d gZ"dZ#ej#Z$ej%j&Z'e  (¡Z)e  *¡Z+dZ,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ejej?ej@ejAejBejCejDejEejFejGejHejIejJgZKejLejMejNfeejMejNfiZOePe dƒrÎejQejQfeOe jR<ePe dƒrìejSejSfeOe jT<ePe dƒr ejMejMfeOe jU<ePe dƒr(ejVejVfeOe jW<ePe dƒrFejNejNfeOe jX<dd „ZYdd „ZZdd„Z[dd„Z\e ]e[¡Z^e _e\¡Z`Gdd„deaƒZberœd$dd„Zcn d%d!d„Zceceb_cGd"d#„d#eaƒZddS)&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: .. code-block:: Copyright (c) 2015-2016 Will Bond 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)ÚPROTOCOL_TLS_CLIENTé)ÚCoreFoundationÚSecurityÚ SecurityConst)Ú_assert_no_errorÚ_build_tls_unknown_ca_alertÚ_cert_array_from_pemÚ_create_cfstring_arrayÚ_load_client_cert_chainÚ_temporary_keychain)Ú _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úC/usr/lib/python3.9/site-packages/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_SNIrrrrrr rÉs c Cs˜d}zPt |¡}|dur"tjWS|j}|d}| ¡}d}d}z|||krº|dusZ|dkrtt ||¡stt t j d¡‚||} t j |   ||¡} | | | ¡} || 7}| sB|sºtjWWSqºqBWnztjy6} z^| j }|dur"|t j kr"||d<|t jks |t jkr tjWYd} ~ WS‚WYd} ~ n d} ~ 00||d<||krRtjWSWdSty’} z"|durx| |_tjWYd} ~ Sd} ~ 00dS)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Ú gettimeoutrZ wait_for_readÚerrorÚerrnoÚEAGAINÚctypesÚc_charZ 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_countZ remainingÚbufferZ chunk_sizeÚerrr Ú_read_callbackÕsP   ÿ    r>c Cs|d}z4t |¡}|dur"tjWS|j}|d}t ||¡}| ¡}d}d} zV| |kr |dusf|dkr€t  ||¡s€t  t j d¡‚|  |¡} | | 7} || d…}qNWnxtj y} z\| j }|dur|t j kr| |d<|t jksð|t jkrtjWYd} ~ WS‚WYd} ~ n d} ~ 00| |d<| |kr6tjWSWdStyv} z"|dur\| |_tjWYd} ~ Sd} ~ 00dS)zx SecureTransport write callback. This is called by ST to request that data actually be sent on the network. Nrr#)r$r%r r&r'r,Ú string_atr(rZwait_for_writer)r*r+Úsendr0r1r2r3r4r5) r6r7r8r9r:Zbytes_to_writeÚdatar;r)ÚsentZ chunk_sentr=rrr Ú_write_callback sD      rCc@sÂeZdZdZdd„Zejdd„ƒZdd„Zdd „Z d d „Z d d „Z dd„Z dd„Z dd„Zdd„Zd.dd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd/d&d'„Zd(d)„Zd*d+„Zd,d-„ZdS)0Ú 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Ú_closedr5Ú _keychainÚ _keychain_dirÚ_client_cert_chainr(Ú_timeoutÚ settimeout)Úselfr'rrr Ú__init__Ns zWrappedSocket.__init__ccs4d|_dV|jdur0|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)r5Úclose)rNÚ exceptionrrr Ú_raise_on_error`s  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_SUITESZSSLSetEnabledCiphersrFr )rNÚciphersÚresultrrr Ú _set_ciphersus  ÿzWrappedSocket._set_ciphersc CsF|sdSt|ƒ}z$t |j|¡}t|ƒWt |¡n t |¡0dS)z< Sets up the ALPN protocols on the context. N)r rÚSSLSetALPNProtocolsrFr rÚ CFRelease)rNÚ protocolsZ protocols_arrrVrrr Ú_set_alpn_protocols‚s z!WrappedSocket._set_alpn_protocolsc Cs¾|sdStjtjf}z&| |¡}||vr.WdSd|f}Wn.tyh}zd|f}WYd}~n d}~00t| ¡ƒ}|j |¡t   ddd¡}|j  tj tj |¡| ¡t d|¡‚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. Raises an SSLError if the connection is not trusted. Nzerror code: %dz exception: %rÚiirrzcertificate verify failed, %s)r ZkSecTrustResultUnspecifiedZkSecTrustResultProceedÚ_evaluate_trustr4r Úversionr'ÚsendallÚstructÚpackZ setsockoptZ SOL_SOCKETZ SO_LINGERrPÚsslÚSSLError) rNÚverifyÚ trust_bundleZ successesÚ trust_resultÚreasonr=ZrecZoptsrrr Ú_custom_validates$þ    zWrappedSocket._custom_validatec Cstj |¡r>t|dƒ}| ¡}Wdƒn1s40Yd}t ¡}z t|ƒ}t |j t   |¡¡}t |ƒ|s~t  d¡‚t ||¡}t |ƒt |d¡}t |ƒt ¡}t |t   |¡¡}t |ƒW|rØt |¡|durêt |¡n$|rút |¡|durt |¡0|jS)NÚrbzFailed to copy trust referenceT)ÚosÚpathÚisfileÚopenÚreadrÚ SecTrustRefr ÚSSLCopyPeerTrustrFr,Úbyrefr rbrcZSecTrustSetAnchorCertificatesZ!SecTrustSetAnchorCertificatesOnlyZSecTrustResultTypeZSecTrustEvaluaterrYÚvalue)rNreÚfZ cert_arrayÚtrustrVrfrrr r]²s6  &      ü   zWrappedSocket._evaluate_trustc Cst dtjtj¡|_t |jtt¡} t | ƒt :t |ƒd} | t vrT| dd} q>|t | <Wdƒn1sp0Yt  |j| ¡} t | ƒ|rÄt|tƒs¨| d¡}t |j|t|ƒ¡} t | ƒ| ¡| | ¡t |j|¡} t | ƒt |j|¡} t | ƒ|r|dur,t |jtjd¡} t | ƒ|rhtƒ\|_|_t|j||ƒ|_t |j|j¡} t | ƒ| ¡vt  |j¡} | tj!kr–t" #d¡‚nB| tj$krÀ| %||¡Wdƒqhnt | ƒWdƒqüWdƒn1sî0YqhdS)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)&rZSSLCreateContextr ZkSSLClientSideZkSSLStreamTyperFZ SSLSetIOFuncsÚ_read_callback_pointerÚ_write_callback_pointerr Ú_connection_ref_lockÚidr$ZSSLSetConnectionÚ isinstanceÚbytesÚencodeZSSLSetPeerDomainNamerSrWr[ZSSLSetProtocolVersionMinZSSLSetProtocolVersionMaxZSSLSetSessionOptionZ"kSSLSessionOptionBreakOnServerAuthrrIrJrrKZSSLSetCertificaterRZ SSLHandshaker3r'r;ZerrSSLServerAuthCompletedrh) rNÚserver_hostnamerdreZ min_versionZ max_versionZ client_certZ client_keyZclient_key_passphraseZalpn_protocolsrVZhandlerrr Ú handshakeÚs` ÿÿ &   ÿ  ÿÿ      zWrappedSocket.handshakecCs |j ¡S©N)r'Úfileno©rNrrr r7szWrappedSocket.filenocCs*|jdkr|jd8_|jr&| ¡dS)Nrr)rGrHrPr€rrr Ú_decref_socketios;s zWrappedSocket._decref_socketioscCs&t |¡}| ||¡}|d|…}|Sr~)r,Zcreate_string_bufferr.)rNZbufsizr<Z bytes_readrArrr ÚrecvAs   zWrappedSocket.recvNc Cs¼|jr dS|durt|ƒ}tj| |¡}t d¡}| ¡(t |j ||t  |¡¡}Wdƒn1sj0Y|t j kr”|j dkr¶t d¡‚n"|t jt jfvr®| ¡nt|ƒ|j S)Nrzrecv timed out)rHrSr,r-Z from_bufferÚc_size_trRrZSSLReadrFrqr r3rrr'r;r/ZerrSSLClosedNoNotifyrPr )rNr<ÚnbytesÚprocessed_bytesrVrrr r.Gs(  ÿ"   þ zWrappedSocket.recv_intocCs ||_dSr~©rL)rNr;rrr rMrszWrappedSocket.settimeoutcCs|jSr~r†r€rrr r(uszWrappedSocket.gettimeoutc Cs|t d¡}| ¡,t |j|t|ƒt |¡¡}Wdƒn1sD0Y|tj krn|j dkrnt   d¡‚nt |ƒ|j S)Nrzsend timed out)r,rƒrRrZSSLWriterFrSrqr r3rrr'r;r )rNrAr…rVrrr r@xs  ÿ" zWrappedSocket.sendcCs4d}|t|ƒkr0| |||t…¡}||7}qdS©Nr)rSr@ÚSSL_WRITE_BLOCKSIZE)rNrAZ total_sentrBrrr r_‰s zWrappedSocket.sendallcCs8| ¡t |j¡Wdƒn1s*0YdSr~)rRrZSSLCloserFr€rrr Úshutdowns zWrappedSocket.shutdowncCs’|jdkr€d|_|jr(t |j¡d|_|jr@t |j¡d|_|jrvt |j¡t |j¡t   |j ¡d|_|_ |j   ¡S|jd8_dS)NrT)rGrHrFrrYrKrIrZSecKeychainDeleteÚshutilÚrmtreerJr'rPr€rrr rP“s        zWrappedSocket.closeFc Cs|s tdƒ‚t ¡}d}d}zØt |jt |¡¡}t|ƒ|s`W|rNt  |¡|r\t  |¡dSt  |¡}|sW|r~t  |¡|rŒt  |¡dSt  |d¡}|s¤J‚t  |¡}|s¶J‚t  |¡}t |¡} t | |¡}W|ræt  |¡|rôt  |¡n"|rt  |¡|rt  |¡0|S)Nz2SecureTransport only supports dumping binary certsr)Ú ValueErrorrrorprFr,rqr rrYZSecTrustGetCertificateCountZSecTrustGetCertificateAtIndexZSecCertificateCopyDataZCFDataGetLengthZCFDataGetBytePtrr?) rNZ binary_formrtZcertdataZ der_bytesrVZ cert_countZleafZ data_lengthr7rrr Ú getpeercert¦sN  ê    ð      ý  zWrappedSocket.getpeercertcCsžt ¡}t |jt |¡¡}t|ƒ|jtj krt|\|_|_d|_d|_d|_d|_d|_d|_d|_ dSrE) Ú_protocol_to_min_maxÚ _min_versionÚ _max_versionÚ_optionsÚ_verifyÚ _trust_bundleÚ _client_certÚ _client_keyÚ_client_key_passphraseÚ_alpn_protocols)rNr“rrr rOszSecureTransportContext.__init__cCsdS)úŽ SecureTransport cannot have its hostname checking disabled. For more, see the comment on getpeercert() in this file. Trr€rrr Úcheck_hostname#sz%SecureTransportContext.check_hostnamecCsdS)r¯Nr©rNrrrrr r°+scCs|jSr~©r¨r€rrr Úoptions3szSecureTransportContext.optionscCs ||_dSr~r²r±rrr r³=scCs|jr tjStjSr~)r©rbÚ CERT_REQUIREDZ CERT_NONEr€rrr Ú verify_modeBsz"SecureTransportContext.verify_modecCs|tjkrdnd|_dS)NTF)rbr´r©r±rrr rµFscCsdSr~rr€rrr Úset_default_verify_pathsJs z/SecureTransportContext.set_default_verify_pathscCs| ¡Sr~)r¶r€rrr Úload_default_certsVsz)SecureTransportContext.load_default_certscCs|tjjkrtdƒ‚dS)Nz5SecureTransport doesn't support custom cipher strings)rrZDEFAULT_CIPHERSrŒ)rNrUrrr Ú set_ciphersYs z"SecureTransportContext.set_ciphersNcCsN|durtdƒ‚|dur@t|ƒWdƒn1s60Y|pF||_dS)Nz1SecureTransport does not support cert directories)rŒrmrª)rNZcafileZcapathZcadatarrr Úload_verify_locations^s  z,SecureTransportContext.load_verify_locationscCs||_||_||_dSr~)r«r¬Z_client_cert_passphrase)rNZcertfileZkeyfileÚpasswordrrr Úload_cert_chainjsz&SecureTransportContext.load_cert_chaincCs&ttdƒstdƒ‚dd„|Dƒ|_dS)z Sets the ALPN protocols that will later be set on the context. Raises a NotImplementedError if ALPN is not supported. rXz2SecureTransport supports ALPN only in macOS 10.12+cSsg|]}t |¡‘qSr)ÚsixZ ensure_binary)Ú.0Úprrr Ú yóz=SecureTransportContext.set_alpn_protocols..N)ÚhasattrrÚNotImplementedErrorr®)rNrZrrr Úset_alpn_protocolsos  ÿz)SecureTransportContext.set_alpn_protocolsFTc CsN|rJ‚|sJ‚|sJ‚t|ƒ}| ||j|j|j|j|j|j|j|j ¡ |Sr~) rDr}r©rªr¦r§r«r¬r­r®)rNZsockZ server_sideZdo_handshake_on_connectZsuppress_ragged_eofsr|r9rrr Ú wrap_socket{s ÷ z"SecureTransportContext.wrap_socket)NNN)NN)FTTN)r—r˜r™ršrOÚpropertyr°Úsetterr³rµr¶r·r¸r¹r»rÃrÄrrrr rs2         úr)r)r¡N)eršZ __future__rr›r,r*Zos.pathrjrŠr'rbr`Ú threadingÚweakrefr¼ÚrZ util.ssl_rZ_securetransport.bindingsrrr Z_securetransport.low_levelr r r r rrrÚ ImportErrorZpackages.backports.makefilerÚ__all__rr"rrr!ÚWeakValueDictionaryr$ÚLockrwrˆ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_SHArTZ PROTOCOL_TLSrrŽr¥rÁr’rr‘rrrrrrrr>rCZ SSLReadFuncruZ SSLWriteFuncrvÚobjectrDr rrrrr ÚsÂ5     â% þ þ  þ  þ  þ  þ   76  <