Re dZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlmZddlmZddlmZdd lmZmZmZdd lmZmZmZmZmZmZ dd l mZn#e $r dZdd l!m"Z"YnwxYwd dgZ#dZ$ej$Z%ej&j'Z(e j)Z*e j+Z,dZ-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ej<ej=ej>ej?ej@ejAejBejCejDejEejFejGejHejIejJejKgZLejMejNejOfeejNejOfiZPeQe drejRejRfePe jS<eQe drejTejTfePe jU<eQe drejNejNfePe jV<eQe drejWejWfePe jX<eQe drejOejOfePe jY<dZZdZ[dZ\dZ]ej^e\Z_ej`e]ZaGddebZcerd"dZdnd#dZdedec_dGd d!ebZedS)$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 pip._vendor.urllib3.contrib.securetransport as securetransport 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)six)util)PROTOCOL_TLS_CLIENT)CoreFoundationSecurity SecurityConst)_assert_no_error_build_tls_unknown_ca_alert_cert_array_from_pem_create_cfstring_array_load_client_cert_chain_temporary_keychain) _fileobjectbackport_makefileinject_into_urllib3extract_from_urllib3Ti@PROTOCOL_SSLv2PROTOCOL_SSLv3PROTOCOL_TLSv1PROTOCOL_TLSv1_1PROTOCOL_TLSv1_2ctt_ttj_tt_ttj_dt_dtj_dS)zG Monkey-patch urllib3 with SecureTransport-backed SSL-support. TN)SecureTransportContextr SSLContextssl_HAS_SNIIS_SECURETRANSPORT/builddir/build/BUILDROOT/alt-python311-pip-21.3.1-3.el8.x86_64/opt/alt/python311/lib/python3.11/site-packages/pip/_vendor/urllib3/contrib/securetransport.pyrrs>-DO1DIDLDI"D#'DI   r#ctt_ttj_tt_ttj_dt_dtj_dS)z> Undo monkey-patching by :func:`inject_into_urllib3`. FN)orig_util_SSLContextrrrorig_util_HAS_SNIr r!r"r#r$rrs>+DO/DI$DL)DI#D#(DI   r#cHd} t|}| tjS|j}|d}|}d}d} ||kr||dkr4t j||st jtj d||z } tj | z ||z} || | } || z }| s|s tjSn||knl#tj$rZ} | j }|G|tj kr7||d<|tjks|tjkrtjcYd} ~ SYd} ~ nd} ~ wwxYw||d<||kr tjSdS#t($r} || |_tjcYd} ~ Sd} ~ wwxYw)zs SecureTransport read callback. This is called by ST to request that data be returned from the socket. Nr timed out)_connection_refsgetr errSSLInternalsocket gettimeoutr wait_for_readerrorerrnoEAGAINctypesc_char from_address recv_intoerrSSLClosedGraceful ECONNRESETEPIPEerrSSLClosedAborterrSSLWouldBlock Exception _exception) connection_id data_bufferdata_length_pointerwrapped_socket base_socketrequested_lengthtimeoutr0 read_count remainingbuffer chunk_sizees r$_read_callbackrJs N.,)--m<<  ! / /$+ .q1 ++--  ///?gll-k7CCF$l5<EEE,z9  -)3AA*,)2269EE j( !%B,AA///    GE Uel%:%:)3#A&E,,,0D0D(::::::: ",A ) ) ) 1 1q ,,,  %()N %+++++++,sf'E8'E8BC3+C32E83EA E E E8EE8EE88 F!FF!F!cd} t|}| tjS|j}|d}t j||}|}d}d} | |krf||dkr4tj ||st j tj d| |} | | z } || d}| |kfnl#tj $rZ} | j }|G|tj kr7| |d<|tjks|tjkrtjcYd} ~ SYd} ~ nd} ~ wwxYw| |d<| |kr tjSdS#t$$r} || |_tjcYd} ~ Sd} ~ wwxYw)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-r3 string_atr.rwait_for_writer0r1r2sendr8r9r:r;r<r=) r>r?r@rArBbytes_to_writedatarDr0sent chunk_sentrIs r$_write_callbackrS s N*,)--m<<  ! / /$+ ,Q/ ^<< ++-- ''?gll.{GDDF$l5<EEE(--d33  "JKK(''    GE Uel%:%:)-#A&E,,,0D0D(::::::: "&A > ! ! 1 1q ,,,  %()N %+++++++,s`'E>, ' ' (lD)) (Q vvxx  ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( $&& 5-l;;J .t|V\%=P=PQQF V $ $ $ El#CDDD;E:NNF V $ $ $?tLLF V $ $ $#688L.ufl<6P6PQQF V $ $ $ 0(///%(444  0(///%(4444&!!sAAA2C1F/F?c tjdtjtj|_tj|jtt} t| t5t|dz} | tvr| dzdz} | tv|t| <dddn #1swxYwYtj |j| } t| |rat|ts|d}tj|j|t%|} t| ||| tj|j|} t| tj|j|} t| |r|4tj|jtjd} t| |rdt3\|_|_t9|j|||_tj|j|j} t|  |5tj |j} | tj!krtEj#d| tj$kr#|%|| dddt|  ddddS#1swxYwY)z Actually performs the TLS handshake. This is run automatically by wrapped socket, and shouldn't be needed in user code. Nirzutf-8Tzhandshake timed out)&r SSLCreateContextr kSSLClientSidekSSLStreamTyperX SSLSetIOFuncs_read_callback_pointer_write_callback_pointerr _connection_ref_lockidr*SSLSetConnection isinstancebytesencodeSSLSetPeerDomainNamerhrmrsSSLSetProtocolVersionMinSSLSetProtocolVersionMaxSSLSetSessionOption"kSSLSessionOptionBreakOnServerAuthrr[r\rr]SSLSetCertificatere SSLHandshaker;r-rDerrSSLServerAuthCompletedr) r`server_hostnamerr min_version max_version client_cert client_keyclient_key_passphrasealpn_protocolsrlhandles r$ handshakezWrappedSocket.handshakes$ 0 -. 0L   ' L02I      " , ,XX *F,,, 1* 2,,,'+ V $  , , , , , , , , , , , , , , , *4<@@     %ou55 B"1"8"8"A"A2 os?/C/CF V $ $ $    00024<MM   24<MM    %11 mNPTF V $ $ $  %1D1F1F .DND.&= Z''D #/ d>UVVF V $ $ $ %%''  !.t|<<];;; .)>???}FFF))&,???       %V,,,                 s++7B..B25B23A%L$LLLc4|jSN)r-filenor`s r$rzWrappedSocket.fileno7s{!!###r#cv|jdkr|xjdzc_|jr|dSdS)Nrr)rYrZrcrs r$_decref_socketioszWrappedSocket._decref_socketios;sI   " "   1 $   <  JJLLLLL  r#cntj|}|||}|d|}|Sr)r3create_string_bufferr6)r`bufsizrG bytes_readrPs r$recvzWrappedSocket.recvAs8,V44^^FF33 kzk" r#Nc @|jrdS|t|}tj|z|}tjd}|5tj|j ||tj |}dddn #1swxYwY|tj kr |j dkrtjdn>|tjtjfvr|nt'||j S)Nrzrecv timed out)rZrhr3r4 from_bufferc_size_trer SSLReadrXrr r;rr-rDr7errSSLClosedNoNotifyrcr )r`rGnbytesprocessed_bytesrls r$r6zWrappedSocket.recv_intoGsX < 1 >[[F-&(55f== /!,,  ! ! # #  % fffl?.K.KF                ]3 3 3 $))n%5666*  .  .    JJLLLL V $ $ $$$s%/B  B$'B$c||_dSrr^)r`rDs r$r_zWrappedSocket.settimeoutrs  r#c|jSrrrs r$r.zWrappedSocket.gettimeoutus }r#c tjd}|5tj|j|t |tj|}dddn #1swxYwY|tj kr|j dkrtj dt||j S)Nrzsend timed out)r3rrer SSLWriterXrhrr r;rr-rDr )r`rPrrls r$rNzWrappedSocket.sendxs /!,,  ! ! # #  & dCIIv|O/L/LF                ]3 3 38MQR8R8R.!122 2 V $ $ $$$s:::(888 d01116::!3;$$&& &   1 $    r#Fc\|stdtj}d}d} tj|jt j|}t||s1 |rtj ||rtj |dSdStj |}|s1 |rtj ||rtj |dSdStj |d}|sJtj |}|sJtj |}tj|} t j| |}|rtj ||rtj |n2#|rtj ||rtj |wwxYw|S)Nz2SecureTransport only supports dumping binary certsr) ValueErrorr rrrXr3rr r rpSecTrustGetCertificateCountSecTrustGetCertificateAtIndexSecCertificateCopyDataCFDataGetLengthCFDataGetBytePtrrL) r` binary_formrcertdata der_bytesrl cert_countleaf data_lengthr?s r$ getpeercertzWrappedSocket.getpeercerts( SQRR R$&&  0.t|V\%=P=PQQF V $ $ $ & 3(222 0(///// 0 0'"=eDDJ  3(222 0(///// 0 09%CCDKKK 6t<]9 9 9,IJJ J ^}; ; ;9 ^}; ; ;9 ^}: : :7 ^}: : :7 ^}: : :7,88CDD Dr#c&|xjdz c_dSNr)rYrs r$_reusezWrappedSocket._reuses q r#ch|jdkr|dS|xjdzc_dSr)rYrcrs r$_dropzWrappedSocket._drops;   " " JJLLLLL   1 $    r#r)F)__name__ __module__ __qualname____doc__ra contextlibcontextmanagerrermrsrrxrrrrr6r_r.rNrzrrcrryrrr"r#r$rUrUFs"""$( ! ! ! 4 4 4!E!E!EF&"&"&"P[[[z$$$  )%)%)%)%V   %%%" ,,,%%%&8888tEEE*!!!%%%%%r#rUcH|xjdz c_t|||dS)NrT)rc)rYr)r`modebufsizes r$makefilers- q 4wd;;;;r#rc*d}t|||g|Ri|Srr)r`r bufferingargskwargss r$rrs+  tYHHHHHHHr#ceZdZdZdZedZejdZedZejdZedZ e jdZ d Z d Z d Z dd Z ddZdZ ddZd S)rz I am a wrapper class for the SecureTransport library, to translate the interface of the standard library ``SSLContext`` object to calls into SecureTransport. ct|\|_|_d|_d|_d|_d|_d|_d|_d|_ dSrW) _protocol_to_min_max _min_version _max_version_options_verify _trust_bundle _client_cert _client_key_client_key_passphrase_alpn_protocols)r`rs r$razSecureTransportContext.__init__sS/CH/M,4,  ! &*##r#cdS) SecureTransport cannot have its hostname checking disabled. For more, see the comment on getpeercert() in this file. Tr"rs r$check_hostnamez%SecureTransportContext.check_hostname#s tr#cdS)rNr"r`rs r$rz%SecureTransportContext.check_hostname+s r#c|jSrrrs r$optionszSecureTransportContext.options3s }r#c||_dSrrrs r$rzSecureTransportContext.options=s r#c@|jr tjn tjSr)rr CERT_REQUIRED CERT_NONErs r$ verify_modez"SecureTransportContext.verify_modeBs$(LCs  cmCr#c8|tjkrdnd|_dS)NTF)rr"rrs r$r$z"SecureTransportContext.verify_modeFs$(999ttu r#cdSrr"rs r$set_default_verify_pathsz/SecureTransportContext.set_default_verify_pathsJs  r#c*|Sr)r'rs r$load_default_certsz)SecureTransportContext.load_default_certsVs,,...r#cN|tjjkrtddS)Nz5SecureTransport doesn't support custom cipher strings)rrDEFAULT_CIPHERSr)r`rks r$ set_ciphersz"SecureTransportContext.set_ciphersYs) di/ / /TUU U 0 /r#Nc|td|(t|5 dddn #1swxYwY|p||_dS)Nz1SecureTransport does not support cert directories)rrr)r`cafilecapathcadatas r$load_verify_locationsz,SecureTransportContext.load_verify_locations^s  PQQ Q  f                 $-vs 155c0||_||_||_dSr)rr_client_cert_passphrase)r`certfilekeyfilepasswords r$load_cert_chainz&SecureTransportContext.load_cert_chainjs $"'/$$$r#cpttdstdd|D|_dS)z Sets the ALPN protocols that will later be set on the context. Raises a NotImplementedError if ALPN is not supported. roz2SecureTransport supports ALPN only in macOS 10.12+c6g|]}tj|Sr")r ensure_binary).0ps r$ z=SecureTransportContext.set_alpn_protocols..ys#HHH 1! 4 4HHHr#N)hasattrr NotImplementedErrorr)r`rqs r$set_alpn_protocolsz)SecureTransportContext.set_alpn_protocolsosK x!677 %D  IHiHHHr#FTc |rJ|sJ|sJt|}|||j|j|j|j|j|j|j|j |Sr) rUrrrrrrrrr)r`sock server_sidedo_handshake_on_connectsuppress_ragged_eofsrrAs r$ wrap_socketz"SecureTransportContext.wrap_socket{s&&&&####'t,,    L            '   r#)NNN)NN)FTTN)rrrrrapropertyrsetterrr$r'r)r,r1r7r@rFr"r#r$rrsk $$$X   X ^^DDXDEEE    ///VVV . . . .0000 I I I $! r#r)r)rN)fr __future__rrr3r1os.pathrrr-rr{ threadingweakref pip._vendorrr util.ssl_r_securetransport.bindingsr r r _securetransport.low_levelr r rrrrr ImportErrorpackages.backports.makefiler__all__r r'rrr&WeakValueDictionaryr*Lockrr'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256%TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384%TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256-TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256+TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256#TLS_DHE_RSA_WITH_AES_256_GCM_SHA384#TLS_DHE_RSA_WITH_AES_128_GCM_SHA256'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384$TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256$TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA%TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA%TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA#TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 TLS_DHE_RSA_WITH_AES_256_CBC_SHA#TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 TLS_DHE_RSA_WITH_AES_128_CBC_SHATLS_AES_256_GCM_SHA384TLS_AES_128_GCM_SHA256TLS_RSA_WITH_AES_256_GCM_SHA384TLS_RSA_WITH_AES_128_GCM_SHA256TLS_AES_128_CCM_8_SHA256TLS_AES_128_CCM_SHA256TLS_RSA_WITH_AES_256_CBC_SHA256TLS_RSA_WITH_AES_128_CBC_SHA256TLS_RSA_WITH_AES_256_CBC_SHATLS_RSA_WITH_AES_128_CBC_SHAri PROTOCOL_TLSrrrr>rrrrrrrrrrrJrS SSLReadFuncr SSLWriteFuncrobjectrUrrr"r#r$rys44j'&&&&&  ++++++NNNNNNNNNN@"""""""@@@K????????@ !"8 9 Ly+$/7.00%y~'' 9977?=55969674745252((11*(11..= J  3]5QR-5}7ST  73 !!##0+, 73 !!##0+, 73 !!##0+, 73"##$$2-. 73"##$$2-. ( ( ( ) ) )4,4,4,n0,0,0,l.-n==/(/@@v%v%v%v%v%Fv%v%v%r  I<<<<<IIII" HHHHHVHHHHHsA%% A54A5