B ç0œ`‘~ã@s’dZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlmZmZmZddlmZmZmZmZydd 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;ejej?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\ernd#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 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úz/private/var/folders/sf/wxz_36012wxg_prv29r6623x5vlqx8/T/pip-target-tft40_m7/lib/python/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}y:t |¡}|dkr tjS|j}|d}| ¡}d}d}y|xv||kr¶|dksZ|dkrtt ||¡stt t j d¡‚||} t j |   ||¡} | | | ¡} || 7}| sB|s²tjSPqBWWnjtjk r$} zH| j }|dk r|t j kr||d<|t jks |t jkrtjS‚Wdd} ~ XYnX||d<||kr>tjSdStk rv} z|dk rd| |_tjSd} ~ XYnXdS)zs SecureTransport read callback. This is called by ST to request that data be returned from the socket. Nrz 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ÌsN         r=c Cs^d}yt |¡}|dkr tjS|j}|d}t ||¡}| ¡}d}d} yZxT| |kr |dksf|dkr€t  ||¡s€t  t j d¡‚|  |¡} | | 7} || d…}qNWWnbtj k r} z@| j }|dk rö|t j krö| |d<|t jksî|t jkrôtjS‚Wdd} ~ XYnX| |d<| |kr tjSdStk rX} z|dk rF| |_tjSd} ~ XYnXdS)zx SecureTransport write callback. This is called by ST to request that data actually be sent on the network. Nrz timed out)rr rr!r"r(Ú string_atr#rÚwait_for_writer%r&r'Úsendr-r.r/r0r1r2) r3r4r5r6r7Zbytes_to_writeÚdatar8r%ÚsentZ chunk_sentr<rrrÚ_write_callbacksD       rCc@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Ú_closedr2Ú _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)r2Úclose)rMÚ 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_SUITESZSSLSetEnabledCiphersrEr )rMÚciphersÚresultrrrÚ _set_cipherslszWrappedSocket._set_ciphersc Cs|sdStj |¡r2t|dƒ}| ¡}WdQRXd}t ¡}z€t|ƒ}t |j t   |¡¡}t |ƒ|srt  d¡‚t ||¡}t |ƒt |d¡}t |ƒt ¡}t |t   |¡¡}t |ƒWd|rÎt |¡|dk ràt |¡Xtjtjf}|j|kr 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 ÚSSLCopyPeerTrustrEr(Úbyrefr ÚsslÚSSLErrorZSecTrustSetAnchorCertificatesZ!SecTrustSetAnchorCertificatesOnlyZSecTrustResultTypeZSecTrustEvaluaterÚ CFReleaserZkSecTrustResultUnspecifiedZkSecTrustResultProceedÚvalue) rMÚverifyÚ trust_bundleÚfZ cert_arrayÚtrustrUZ trust_resultZ successesrrrÚ_custom_validateys:         zWrappedSocket._custom_validatec Cs¸t dtjtj¡|_t |jtt¡} t | ƒt 4t |ƒd} x| t krV| dd} q@W|t | <WdQRXt  |j| ¡} t | ƒ|r´t|tƒs˜| d¡}t |j|t|ƒ¡} t | ƒ| ¡t |j|¡} t | ƒt |j|¡} t | ƒ|rö|dk rt |jtjd¡} t | ƒ|rLtƒ\|_|_t|j||ƒ|_t |j|j¡} t | ƒxf| ¡Rt |j¡} | tj kr|t! "d¡‚n(| tj#krš| $||¡wNn t | ƒPWdQRXqNWdS)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)%rZSSLCreateContextrZkSSLClientSideZkSSLStreamTyperEZ SSLSetIOFuncsÚ_read_callback_pointerÚ_write_callback_pointerr Ú_connection_ref_lockÚidrZSSLSetConnectionÚ isinstanceÚbytesÚencodeZSSLSetPeerDomainNamerRrVZSSLSetProtocolVersionMinZSSLSetProtocolVersionMaxZSSLSetSessionOptionZ"kSSLSessionOptionBreakOnServerAuthr rHrIr rJZSSLSetCertificaterQZ SSLHandshaker0r"r8ZerrSSLServerAuthCompletedrh) rMÚserver_hostnamerdreÚ min_versionZ max_versionÚ client_certZ client_keyZclient_key_passphraserUÚhandlerrrÚ handshake²sV           zWrappedSocket.handshakecCs |j ¡S)N)r"Úfileno)rMrrrru szWrappedSocket.filenocCs*|jdkr|jd8_|jr&| ¡dS)Nrr)rFrGrO)rMrrrÚ_decref_socketioss zWrappedSocket._decref_socketioscCs&t |¡}| ||¡}|d|…}|S)N)r(Úcreate_string_bufferr+)rMZbufsizr:Ú bytes_readrArrrÚrecvs   zWrappedSocket.recvNc Cs¨|jr dS|dkrt|ƒ}tj| |¡}t d¡}| ¡t |j ||t  |¡¡}WdQRX|t j kr€|j dkr¢t d¡‚n"|t jt jfkrš| ¡nt|ƒ|j S)Nrzrecv timed out)rGrRr(r)Ú from_bufferÚc_size_trQrZSSLReadrEr_rr0rcr"r8r,ZerrSSLClosedNoNotifyrOr )rMr:ÚnbytesÚprocessed_bytesrUrrrr+s$       zWrappedSocket.recv_intocCs ||_dS)N)rK)rMr8rrrrLFszWrappedSocket.settimeoutcCs|jS)N)rK)rMrrrr#IszWrappedSocket.gettimeoutc Csht d¡}| ¡"t |j|t|ƒt |¡¡}WdQRX|tj krZ|j dkrZt   d¡‚nt |ƒ|j S)Nrzsend timed out)r(r{rQrZSSLWriterErRr_rr0rcr"r8r )rMrAr}rUrrrr@Ls  " zWrappedSocket.sendcCs8d}x.|t|ƒkr2| |||t…¡}||7}qWdS)Nr)rRr@ÚSSL_WRITE_BLOCKSIZE)rMrAÚ total_sentrBrrrÚsendall]szWrappedSocket.sendallc Cs$| ¡t |j¡WdQRXdS)N)rQrZSSLCloserE)rMrrrÚ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)rFrGrErrbrJrHrZSecKeychainDeleteÚshutilÚrmtreerIr"rO)rMrrrrOgs        zWrappedSocket.closeFc CsÀ|s tdƒ‚t ¡}d}d}z€t |jt |¡¡}t|ƒ|sBdSt |¡}|sTdSt  |d¡}|sht ‚t  |¡}|szt ‚t   |¡}t  |¡} t | |¡}Wd|r¬t  |¡|rºt  |¡X|S)Nz2SecureTransport only supports dumping binary certsr)Ú ValueErrorrr]r^rEr(r_r ZSecTrustGetCertificateCountZSecTrustGetCertificateAtIndexÚAssertionErrorZSecCertificateCopyDatarZCFDataGetLengthZCFDataGetBytePtrr>rb) rMÚ binary_formrgZcertdataZ der_bytesrUZ cert_countZleafZ data_lengthr4rrrÚ getpeercertzs2       zWrappedSocket.getpeercertcCsžt ¡}t |jt |¡¡}t|ƒ|jtj kr3s¬         76