CYhI FUdZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddl m ZddlmZddlmZmZdd lmZmZmZmZmZmZmZej<d ed e j@rdd l!m"Z"d dgZ#ejHjJZ&ejNZ(de)d<e jTZ+dZ,ejHjZej\ej^fejHj`ej\ej^fiZ1e2e dr%ejfejffe1e jh<e2e dr%ejjejjfe1e jl<e2e dr%ej\ej\fe1e jn<e2e dr%ejpejpfe1e jr<e2e dr%ej^ej^fe1e jt<e jvjxej\e jvjzej\e jvj|ejpe jvj~ej^e jvjej^iZAde)d<d"dZBd"dZC d#dZD d#dZEejeDZGejeEZIGddZJ d$ d%dZKeKeJ_KGd d!ZLy)&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. ) annotationsN)socket)util)CoreFoundationSecurity) SecurityConst_assert_no_error_build_tls_unknown_ca_alert_cert_array_from_pem_create_cfstring_array_load_client_cert_chain_temporary_keychainz'urllib3.contrib.securetransport' module is deprecated and will be removed in urllib3 v2.1.0. Read more in this issue: https://github.com/urllib3/urllib3/issues/2681)category stacklevel)Literalinject_into_urllib3extract_from_urllib3z/weakref.WeakValueDictionary[int, WrappedSocket]_connection_refsi@PROTOCOL_SSLv2PROTOCOL_SSLv3PROTOCOL_TLSv1PROTOCOL_TLSv1_1PROTOCOL_TLSv1_2zdict[int, int]_tls_version_to_stctt_ttj_dt_dtj_y)zG Monkey-patch urllib3 with SecureTransport-backed SSL-support. TN)SecureTransportContextr SSLContextssl_IS_SECURETRANSPORTA/usr/lib/python3/dist-packages/urllib3/contrib/securetransport.pyrrs+-DO1DII"D#'DII r#ctt_ttj_dt_dtj_y)z> Undo monkey-patching by :func:`inject_into_urllib3`. FN)orig_util_SSLContextrrr r!r"r#r$rrs++DO/DII#D#(DII r#cVd} tj|}|tjS|j}|d}|j }d}d} ||kr||dk\r0t j||sttjd||z } tj| zj||z} |j| | } || z }| s|stjSn||kr||d<||k7rtj&Sy#t$rl} | j}|T|tjk7rA||d<|tj k(s|tj"k(rtj$cYd} ~ SYd} ~ d} ~ wwxYw#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)rgetr errSSLInternalr gettimeoutr wait_for_readOSErrorerrnoEAGAINctypesc_char from_address recv_intoerrSSLClosedGraceful ECONNRESETEPIPEerrSSLClosedAborterrSSLWouldBlock Exception _exception) connection_id data_bufferdata_length_pointerwrapped_socket base_socketrequested_lengthtimeouterror read_count remainingbuffer chunk_sizees r$_read_callbackrHsN.,)--m<  ! // /$++ .q1 ++-  //?gl--k7C%ellK@@,z9  --)3AA*,)2269E j( !%,AAA//0",A ) ) 11 1 GGE Uell%:)3#A&E,,,0D(:::  ,  %()N %+++,sf&E<%E<BD"D)E< E9 AE4(E9)E<.E4/E<4E99E<< F(F#F(#F(c d} tj|}|tjS|j}|d}t j ||}|j}d}d} | |krX||dk\r0tj||sttjd|j|} | | z } || d}| |krX| |d<| |k7rtj"Sy#t$rl} | j}|T|tjk7rA| |d<|tjk(s|tjk(rtj cYd} ~ SYd} ~ d} ~ wwxYw#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()rr)r r*rr0 string_atr+rwait_for_writer-r.r/sendr5r6r7r8r9r:) r;r<r=r>r?bytes_to_writedatarArBsent chunk_sentrGs r$_write_callbackrQsN*,)--m<  ! // /$++ ,Q/ ^< ++- '?gl..{GD%ellK@@(--d3  "JK('&"&A > ! 11 1 GGE Uell%:)-#A&E,,,0D(:::  ,  %()N %+++,s`&E;E'ACE E(AEEE E EEE F E>8F>FceZdZdZddZej ddZddZddZ ddZ ddZ ddZ dd Z dd Z d d d Zd!d Zd"dZd#dZd$dZddZddZddZd%d&dZd'dZy )( WrappedSocketzO API-compatibility wrapper for Python's OpenSSL wrapped socket object. c||_d|_d|_d|_d|_d|_d|_d|_d|_|jj|_ |jjdyNrF) rcontext_io_refs_closed _real_closedr: _keychain _keychain_dir_client_cert_chainr+_timeout settimeout)selfrs r$__init__zWrappedSocket.__init__;sl    !,0)-"& ..0  q!r#c#Kd|_d|j&|jdc}|_|j|yw)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)r: _real_close)r_ exceptions r$_raise_on_errorzWrappedSocket._raise_on_errorNsF  ?? &)-$ &It    O 's?Ac|syt|} tj|j|}t |t j |y#t j |wxYw)z< Sets up the ALPN protocols on the context. N)rr SSLSetALPNProtocolsrVr r CFRelease)r_ protocols protocols_arrresults r$_set_alpn_protocolsz!WrappedSocket._set_alpn_protocolscsT .y9  411$,, NF V $  $ $] 3N $ $] 3s +AA(c.|r|ytjtjf} |j|}||vrydt |}d}t |j}|jj|tjddd} |jjtjtj| |jt!j"d||#t $r}d|}|}Yd}~d}~wwxYw)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. Nz error code: z exception: iirrzcertificate verify failed, )r kSecTrustResultUnspecifiedkSecTrustResultProceed_evaluate_trustintr9r versionrsendallstructpack setsockopt SOL_SOCKET SO_LINGERrbsslSSLError) r_verify trust_bundle successes trust_resultreasonexcrGrecoptss r$_custom_validatezWrappedSocket._custom_validateps-   4 4  0 0  // =Ly(#C $5#67FC*$,,.9 C {{4A& v00&2B2BDI ll8ABK "1%(FC sC:C:: DDDctjj|r%t|d5}|j }dddd}t j } t|}t j|jtj|}t||stjdt j||}t|t j |d}t|t j"}t j$|tj|}t||rt'j(||t'j(||j*S#1swYExYw#|rt'j(||t'j(|wwxYw)NrbzFailed to copy trust referenceT)ospathisfileopenreadr SecTrustRefr SSLCopyPeerTrustrVr0byrefr ryrzSecTrustSetAnchorCertificates!SecTrustSetAnchorCertificatesOnlySecTrustResultTypeSecTrustEvaluaterrgvalue)r_r|f cert_arraytrustrjr~s r$rpzWrappedSocket._evaluate_trustsb 77>>, 'lD) (Q vvx  ( $$& 5-l;J ..t||V\\%=PQF V $ll#CDD;;E:NF V $??tLF V $#668L..ufll<6PQF V $((/%((4!!!G ( (:((/%((4&sFC*F F  1F>c tjdtjtj|_tj |j tt} t| t5t|dz} | tvr| dzdz} | tvr|t| <dddtj|j  } t| |rVt|ts|j!d}tj"|j |t%|} t| |j'| tj(|j |} t| tj*|j |} t| |r|:tj,|j tj.d} t| |rht1\|_|_t7|j2|||_tj:|j |j8} t|  |j=5tj>|j } | tj@k(rtCjDd| tjFk(r|jI|| dddt|  dddy#1swY9xYw#1swYnxYw)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 kSSLClientSidekSSLStreamTyperV SSLSetIOFuncs_read_callback_pointer_write_callback_pointerr _connection_ref_lockidrSSLSetConnection isinstancebytesencodeSSLSetPeerDomainNamelenrkSSLSetProtocolVersionMinSSLSetProtocolVersionMaxSSLSetSessionOption"kSSLSessionOptionBreakOnServerAuthrrZr[rr\SSLSetCertificaterd SSLHandshaker8rrAerrSSLServerAuthCompletedr) r_server_hostnamer{r| min_version max_version client_cert client_keyclient_key_passphrasealpn_protocolsrjhandles r$ handshakezWrappedSocket.handshakes$ 00 -.. 0L0L  '' LL02I   " ,X *F,, 1* 2,,'+ V $  , **4<<@ ou5"1"8"8"A22 os?/CF V $   0224<<M 224<<M 111 mNNPTF V $ 1D1F .DND.&= Z'D #// d>U>UVF V $%%' !..t||<];;; ..)>??}FFF))&,?  %V,  g , ,f  s%3'K KA.K( K(K%(K1c6|jjSN)rfilenor_s r$rzWrappedSocket.filenos{{!!##r#c|jdkDr|xjdzc_|jr|jyy)Nrr)rWrXclosers r$_decref_socketioszWrappedSocket._decref_socketioss1 ==1  MMQ M << JJL r#ctj|}|j||}|d|}tjt |Sr)r0create_string_bufferr3typingcastr)r_bufsizrE bytes_readrNs r$recvzWrappedSocket.recv#s?,,V4^^FF3 kz"{{5$''r#Nc |jry| t|}tj|zj |}tj d}|j 5tj|j||tj|}dddtjk(r0|jdk(rtjd|jS|tj tj"fvr|j%|jSt'||jS#1swYxYw)Nrzrecv timed out)rYrr0r1 from_bufferc_size_trdr SSLReadrVrr r8rrrAr4errSSLClosedNoNotifyrbr )r_rEnbytesprocessed_bytesrjs r$r3zWrappedSocket.recv_into)s    >[F--&(55f= //!,  ! ! # %% fffll?.KF  ]33 3 $$)nn%566$$$  . .  . .       $$$ V $$$$=  s "6D88Ec||_yrr])r_rAs r$r^zWrappedSocket.settimeoutVs  r#c|jSrrrs r$r+zWrappedSocket.gettimeoutYs }}r#c tjd}|j5tj|j |t |tj|}dddtjk(r$|jdk(rtjdt||jS#1swYWxYw)Nrzsend timed out)r0rrdr SSLWriterVrrr r8rrrAr )r_rNrrjs r$rLzWrappedSocket.send\s //!,  ! ! # && dCIv||O/LF  ]33 38M8MQR8R..!12 2 V $$$$  s ?B;;Ccd}|t|kr0|j|||tz}||z }|t|kr/yyNr)rrLSSL_WRITE_BLOCKSIZE)r_rN total_sentrOs r$rszWrappedSocket.sendallmsF 3t9$99T*z>  & &t~~ 6  $ $T^^ 4 MM$,, -26 6DNT/{{  ""r#cn|s tdtj}d}d} tj|jt j |}t||s1 |rtj||rtj|yytj|}|s1 |rtj||rtj|yytj|d}|sJtj|}|sJtj|}tj|} t j| |}|rtj||rtj||S#|rtj||rtj|wwxYw)Nz2SecureTransport only supports dumping binary certsr) ValueErrorr rrrVr0rr rrgSecTrustGetCertificateCountSecTrustGetCertificateAtIndexSecCertificateCopyDataCFDataGetLengthCFDataGetBytePtrrJ) r_ binary_formrcertdata der_bytesrj cert_countleaf data_lengthr<s r$ getpeercertzWrappedSocket.getpeercerts(QR R$$&  0..t||V\\%=PQF V $&((2((/'"==eDJ((2((/99%CDK4 66t>]99 9,,IJ J ^^};; ; ^^};; ; ^^}:: : ^^}:: : ^^}:: :,,!6xlCD Dr#)r socket_clsreturnNone)rz"typing.Generator[None, None, None])rhlist[bytes] | Nonerr)r{boolr| bytes | Nonerr)r|rrrq)rbytes | str | Noner{rr|rrrqrrqr str | Nonerrr typing.Anyrrrrrrqrr)rrqrrr)rEzctypes.Array[ctypes.c_char]r int | Nonerrq)rAfloatrr)rz float | None)rNrrrq)rNrrr)F)rrrr)rstr)__name__ __module__ __qualname____doc__r` contextlibcontextmanagerrdrkrrprrrrr3r^r+rLrsrrrbrrrr"r#r$rSrS6s"&( 4#LJ&"PZ+ZZ# Z  Z  Z ZZ *Z+Z Zx$ (IM+%1+%;E+% +%Z %" , #8tEr#rSc<d}tj|||g|i|Sr)rmakefile)r_mode bufferingargskwargss r$r r s(I   tT9 Ft Fv FFr#ceZdZdZddZeddZejddZeddZejddZeddZ e jddZ dd Z dd Z dd Z d dd Z d ddZd dZ d! d"dZeddZejd#dZeddZejd$dZy )%rz I am a wrapper class for the SecureTransport library, to translate the interface of the standard library ``SSLContext`` object to calls into SecureTransport. cTtjj|_tjj|_|dtj tjfvrt|\|_ |_ d|_ d|_ d|_ d|_d|_d|_d|_yrU)ry TLSVersionMINIMUM_SUPPORTED_minimum_versionMAXIMUM_SUPPORTED_maximum_version PROTOCOL_TLSPROTOCOL_TLS_CLIENT_protocol_to_min_max _min_version _max_version_options_verify _trust_bundle _client_cert _client_key_client_key_passphrase_alpn_protocols)r_rs r$r`zSecureTransportContext.__init__s%(^^%E%E%(^^%E%E D#"2"2C4K4KL L3G3Q 0D t0  +/(,'+&*#37r#cy) SecureTransport cannot have its hostname checking disabled. For more, see the comment on getpeercert() in this file. Tr"rs r$check_hostnamez%SecureTransportContext.check_hostnames r#cy)r$Nr"r_rs r$r%z%SecureTransportContext.check_hostname sr#c|jSrrrs r$optionszSecureTransportContext.optionss}}r#c||_yrr)r's r$r*zSecureTransportContext.optionss  r#cZ|jrtjStjSr)rry CERT_REQUIRED CERT_NONErs r$ verify_modez"SecureTransportContext.verify_mode!s$(LLs  CcmmCr#c4|tjk(|_yr)ryr-rr's r$r/z"SecureTransportContext.verify_mode%s 1 11 r#cyrr"rs r$set_default_verify_pathsz/SecureTransportContext.set_default_verify_paths)s r#c"|jSr)r2rs r$load_default_certsz)SecureTransportContext.load_default_certs5s,,..r#ctd)Nz5SecureTransport doesn't support custom cipher strings)r)r_cipherss r$ set_ciphersz"SecureTransportContext.set_ciphers8sPQQr#Ncz| td|t|5 ddd|xs||_y#1swYxYw)Nz1SecureTransport does not support cert directories)rrr)r_cafilecapathcadatas r$load_verify_locationsz,SecureTransportContext.load_verify_locations;sL  PQ Q  f  $-v  s1:c.||_||_||_yr)rr _client_cert_passphrase)r_certfilekeyfilepasswords r$load_cert_chainz&SecureTransportContext.load_cert_chainLs %"'/$r#cttds td|Dcgc]"}tjj |d$c}|_ycc}w)z Sets the ALPN protocols that will later be set on the context. Raises a NotImplementedError if ALPN is not supported. rfz2SecureTransport supports ALPN only in macOS 10.12+asciiN)hasattrr NotImplementedErrorrto_bytesr")r_rhps r$set_alpn_protocolsz)SecureTransportContext.set_alpn_protocolsVsJ x!67%D IRR1 2 21g >RRs'Ac "|rJ|sJ|sJt|}|j||j|jt|j t|j |j|j|j|j |Sr) rSrrrrrrrr r!r")r_sock server_sidedo_handshake_on_connectsuppress_ragged_eofsrr>s r$ wrap_socketz"SecureTransportContext.wrap_socketbs&&&###'t,    LL    t44 5 t44 5        ' '   r#c|jSrrrs r$minimum_versionz&SecureTransportContext.minimum_version$$$r#c||_yrrQ)r_rRs r$rRz&SecureTransportContext.minimum_version /r#c|jSrrrs r$maximum_versionz&SecureTransportContext.maximum_versionrSr#c||_yrrW)r_rXs r$rXz&SecureTransportContext.maximum_versionrUr#)rrqrr)rz Literal[True])rrrrr)rrqrrr)r6rrr)NNN)r9rr:rr;rrr)NN)r?rr@rrArrr)rhzlist[str | bytes]rr)FTTN) rKrrLrrMrrNrrrrrS)rRrqrr)rXrqrr)rrrrr`propertyr%setterr*r/r2r4r7r<rBrIrOrRrXr"r#r$rrs 8   ^^DD22  /R "!# ... .  .(## 000 0  0 S"(,%).2 "&  #  ,  B%%00%%00r#rr)r;rqr<rqr= bytearrayrrq)rN) r_rr zILiteral['r'] | Literal['w'] | Literal['rw'] | Literal['wr'] | Literal['']r rrrrrrztyping.BinaryIO | typing.TextIO)Mr __future__rrr0r.os.pathrrrryrt threadingrwarningsweakrefrr_securetransport.bindingsrr _securetransport.low_levelr r r r rrrwarnDeprecationWarning TYPE_CHECKINGtyping_extensionsr__all__r rr&WeakValueDictionaryr__annotations__LockrrrrrrrrErrrrrrrrrrrTLSv1_1TLSv1_2rrrrrHrQ SSLReadFuncr SSLWriteFuncrrSr rr"r#r$rrs4l#   ' 5   ) "8 9yy++( G!"&y~~'  II]88-:V:VWII!!##$$$ 3 !####0++, 3 !####0++, 3 !####0++, 3"#$$$$2--. 3"#$$$$2--.NN$$m&A&ANN-55NNM88NNM88NN$$m&B&B &N()6,6,%(6,?H6,6,r2,2,%(2,?H2,2,p.--n=/(//@cEcET  G  G R G  G  G G% G" c0c0r#