\d{PddlZddlZddlZ ddlZn #e$rdZYnwxYwddlmZddlmZddlmZddlm Z ddl m Z eej ej fZGdd ejZGd d ejZd Zd ZGdde je jZGddejZdS)N) constants) exceptions) protocols) transports)loggerc"eZdZdZdZdZdZdZdS)SSLProtocolState UNWRAPPED DO_HANDSHAKEWRAPPEDFLUSHINGSHUTDOWNN)__name__ __module__ __qualname__r r r rr!..\python\lib\asyncio\sslproto.pyr r s'I!LGHHHHrr ceZdZdZdZdZdZdS)AppProtocolState STATE_INITSTATE_CON_MADE STATE_EOFSTATE_CON_LOSTN)rrrrrrrrrrrrs$J%NI%NNNrrc`|rtdtj}|sd|_|S)Nz(Server side SSL needs a valid SSLContextF) ValueErrorsslcreate_default_contextcheck_hostname) server_sideserver_hostname sslcontexts r_create_transport_contextr$+s@ECDDD +--J *$) ! rc|||dz}n |}d|z}n|}||dz}n|}||cxkrdksntd|d|d||fS)Nirzhigh (z) must be >= low (z) must be >= 0)r)highlowkbhilos radd_flowcontrol_defaultsr,9s | ;dBBBRBB  { 1W  ====q====j""bbb"## # r6MrceZdZdZejjZdZddZ dZ dZ dZ dZ efd Zd Zd Zd Zdd ZdZdZddZdZdZedZdZdZdZdZdZdZ dZ!dS)_SSLProtocolTransportTc0||_||_d|_dS)NF)_loop _ssl_protocol_closed)selfloop ssl_protocols r__init__z_SSLProtocolTransport.__init__Ts ) rNc8|j||S)z#Get optional transport information.)r1_get_extra_infor3namedefaults rget_extra_infoz$_SSLProtocolTransport.get_extra_infoYs!11$@@@rc:|j|dSN)r1_set_app_protocol)r3protocols r set_protocolz"_SSLProtocolTransport.set_protocol]s ,,X66666rc|jjSr>)r1 _app_protocolr3s r get_protocolz"_SSLProtocolTransport.get_protocol`s!//rc|jSr>)r2rDs r is_closingz _SSLProtocolTransport.is_closingcs |rcf|js"d|_|jdSd|_dS)a Close the transport. Buffered data will be flushed asynchronously. No more data will be received. After all buffered data is flushed, the protocol's connection_lost() method will (eventually) called with None as its argument. TN)r2r1_start_shutdownrDs rclosez_SSLProtocolTransport.closefs>| &DL   . . 0 0 0 0 0!%D   rc\|js$d|_|dtdSdS)NTz9unclosed transport )r2warnResourceWarning)r3 _warningss r__del__z_SSLProtocolTransport.__del__tsE| ,DL NN* , , , , , , ,rc|jj Sr>)r1_app_reading_pausedrDs r is_readingz _SSLProtocolTransport.is_reading{s%999rc8|jdS)zPause the receiving end. No data will be passed to the protocol's data_received() method until resume_reading() is called. N)r1_pause_readingrDs r pause_readingz#_SSLProtocolTransport.pause_reading~s ))+++++rc8|jdS)zResume the receiving end. Data received will once again be passed to the protocol's data_received() method. N)r1_resume_readingrDs rresume_readingz$_SSLProtocolTransport.resume_readings **,,,,,rcn|j|||jdS)aSet the high- and low-water limits for write flow control. These two values control when to call the protocol's pause_writing() and resume_writing() methods. If specified, the low-water limit must be less than or equal to the high-water limit. Neither value can be negative. The defaults are implementation-specific. If only the high-water limit is given, the low-water limit defaults to an implementation-specific value less than or equal to the high-water limit. Setting high to zero forces low to zero as well, and causes pause_writing() to be called whenever the buffer becomes non-empty. Setting low to zero causes resume_writing() to be called only once the buffer is empty. Use of zero for either limit is generally sub-optimal as it reduces opportunities for doing I/O and computation concurrently. N)r1_set_write_buffer_limits_control_app_writingr3r'r(s rset_write_buffer_limitsz-_SSLProtocolTransport.set_write_buffer_limitss8& 33D#>>> //11111rc2|jj|jjfSr>)r1_outgoing_low_water_outgoing_high_waterrDs rget_write_buffer_limitsz-_SSLProtocolTransport.get_write_buffer_limits"6"79 9rc4|jS)z-Return the current size of the write buffers.)r1_get_write_buffer_sizerDs rget_write_buffer_sizez+_SSLProtocolTransport.get_write_buffer_sizes!88:::rcn|j|||jdS)aSet the high- and low-water limits for read flow control. These two values control when to call the upstream transport's pause_reading() and resume_reading() methods. If specified, the low-water limit must be less than or equal to the high-water limit. Neither value can be negative. The defaults are implementation-specific. If only the high-water limit is given, the low-water limit defaults to an implementation-specific value less than or equal to the high-water limit. Setting high to zero forces low to zero as well, and causes pause_reading() to be called whenever the buffer becomes non-empty. Setting low to zero causes resume_reading() to be called only once the buffer is empty. Use of zero for either limit is generally sub-optimal as it reduces opportunities for doing I/O and computation concurrently. N)r1_set_read_buffer_limits_control_ssl_readingr\s rset_read_buffer_limitsz,_SSLProtocolTransport.set_read_buffer_limitss8& 224=== //11111rc2|jj|jjfSr>)r1_incoming_low_water_incoming_high_waterrDs rget_read_buffer_limitsz,_SSLProtocolTransport.get_read_buffer_limitsrbrc4|jS)z+Return the current size of the read buffer.)r1_get_read_buffer_sizerDs rget_read_buffer_sizez*_SSLProtocolTransport.get_read_buffer_sizes!77999rc|jjSr>)r1_app_writing_pausedrDs r_protocol_pausedz&_SSLProtocolTransport._protocol_pauseds!55rct|tttfs$t dt |j|sdS|j|fdS)zWrite some data bytes to the transport. This does not block; it buffers the data and arranges for it to be sent out asynchronously. z+data: expecting a bytes-like instance, got N) isinstancebytes bytearray memoryview TypeErrortyperr1_write_appdatar3datas rwritez_SSLProtocolTransport.writesv $ : >?? :9#'::#699:: :  F ))4'22222rc:|j|dS)zWrite a list (or any iterable) of data bytes to the transport. The default implementation concatenates the arguments and calls write() on the result. N)r1r{)r3 list_of_datas r writelinesz _SSLProtocolTransport.writeliness! )),77777rct)zuClose the write end after flushing buffered data. This raises :exc:`NotImplementedError` right now. )NotImplementedErrorrDs r write_eofz_SSLProtocolTransport.write_eofs "!rcdS)zAReturn True if this transport supports write_eof(), False if not.FrrDs r can_write_eofz#_SSLProtocolTransport.can_write_eofsurcXd|_|j|jdSdS)zClose the transport immediately. Buffered data will be lost. No more data will be received. The protocol's connection_lost() method will (eventually) be called with None as its argument. TNr2r1_abortrDs rabortz_SSLProtocolTransport.aborts7   )   % % ' ' ' ' ' * )rcHd|_|j|dSNTrr3excs r _force_closez"_SSLProtocolTransport._force_closes&  !!#&&&&&rc|jj||jxjt |z c_dSr>)r1_write_backlogappend_write_buffer_sizelenr|s r_test__append_write_backlogz1_SSLProtocolTransport._test__append_write_backlogs? )00666 --T:----rr>NN)"rrr_start_tls_compatibler _SendfileModeFALLBACK_sendfile_compatibler6r<rArErGrJwarningsrOrRrUrXr]rarerirmrppropertyrsr~rrrrrrrrrr.r.Ns!$2; AAAA777000 & & &!),,,,:::,,,---2222,999;;;2222,999:::66X6 3 3 3888""" ( ( (''';;;;;rr.ceZdZdZdZdZdZ d-dZdZd.dZ dZ d Z d Z d Z d Zd Zd.dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#d"Z$d/d#Z%d$Z&d%Z'd&Z(d/d'Z)d(Z*d)Z+d*Z,d0d,Z-dS)1 SSLProtocoliNFTc ttdt|j|_t |j|_| tj}n|dkrtd|| tj } n| dkrtd| |st||}||_ |r |s||_ nd|_ ||_t||_t#j|_d|_||_||_||d|_d|_d|_||_| |_tj|_tj|_t@j!|_"d|_#|rtHj%|_&ntHj'|_&|j(|j|j|j |j |_)d|_*d|_+d|_,d|_-d|_.|/d|_0d|_1d|_2d|_3|4|5dS)Nzstdlib ssl module not availablerz7ssl_handshake_timeout should be a positive number, got z6ssl_shutdown_timeout should be a positive number, got )r#F)r!r")6r RuntimeErrorrwmax_size _ssl_bufferrx_ssl_buffer_viewrSSL_HANDSHAKE_TIMEOUTrSSL_SHUTDOWN_TIMEOUTr$ _server_side_server_hostname _sslcontextdict_extra collectionsdequerr_waiterr0r?_app_transport_app_transport_created _transport_ssl_handshake_timeout_ssl_shutdown_timeout MemoryBIO _incoming _outgoingr r _state _conn_lostrr _app_staterwrap_bio_sslobj_ssl_writing_pausedrQ_ssl_reading_pausedrlrkrg _eof_receivedrrr`r_rZ_get_app_transport) r3r4 app_protocolr#waiterr!r"call_connection_madessl_handshake_timeoutssl_shutdown_timeouts rr6zSSLProtocol.__init__ s ;@AA A$T]33 *4+; < < ($-$C ! ! "a ' '/,//00 0 '#,#A !Q & &.+..// / .2_..J(  ); )$3D ! !$(D !%j111 */11"#   |,,,"&+#&;#%9"&0   >.9DOO.=DO'00 NDN) 1133 $) #( #( $%!#$  $$&&&"#( $%!#$  %%''' !!!!!rc||_t|dr;t|tjr!|j|_|j|_d|_ dSd|_ dS)N get_bufferTF) rChasattrrurBufferedProtocolr_app_protocol_get_bufferbuffer_updated_app_protocol_buffer_updated_app_protocol_is_buffer)r3rs rr?zSSLProtocol._set_app_protocol^sc) L, / / 1<)CDD 1,8,CD )0<0KD -+/D ( ( (+0D ( ( (rc|jdS|js7||j|n|jdd|_dSr>)r cancelled set_exception set_resultrs r_wakeup_waiterzSSLProtocol._wakeup_waiterisd <  F|%%'' . **3//// ''--- rc|j7|jrtdt|j||_d|_|jS)Nz$Creating _SSLProtocolTransport twiceT)rrrr.r0rDs rrzSSLProtocol._get_app_transportssK   &* K"#IJJJ"7 D"I"ID *.D '""rc<||_|dS)zXCalled when the low-level connection is made. Start the SSL handshake. N)r_start_handshake)r3 transports rconnection_madezSSLProtocol.connection_made{s# $ rc|j|j|xjdz c_|j d|j_|jtj kr`|j tj ks|j tj kr6tj|_ |j|jj||tjd|_d|_d|_|||jr |jd|_|jr"|jd|_dSdS)zCalled when the low-level connection is lost or closed. The argument is an exception object or None (the latter meaning a regular EOF is received or the connection was aborted or closed). rNT)rclearrreadrrr2rr r rrrrrr0 call_soonrCconnection_lost _set_stater rr_shutdown_timeout_handlecancel_handshake_timeout_handlers rrzSSLProtocol.connection_lostsO !!###  1   **.D  ' ;*7 7 7#3#BBB#3#==="2"A $$T%7%GMMM (2333"! C    ( 1  ) 0 0 2 2 2,0D )  ) 2  * 1 1 3 3 3-1D * * * 2 2rc|}|dks ||jkr|j}t|j|kr-t||_t |j|_|jSNr)rrrrwrxr)r3nwants rrzSSLProtocol.get_buffersc 199t},,=D t 4 ' '(D $.t/?$@$@D !$$rc|j|jd||jtjkr|dS|jtjkr|dS|jtj kr| dS|jtj kr| dSdSr>) rr~rrr r _do_handshaker _do_readr _do_flushr _do_shutdown)r3nbytess rrzSSLProtocol.buffer_updateds T27F7;<<< ;*7 7 7    [,4 4 4 MMOOOOO [,5 5 5 NN      [,5 5 5        6 5rcd|_ |jrtjd||jt jkr|tdS|jt j kr>| t j |j rdS|dS|jt j krI|| t j|dS|jt jkr|dSdS#t$$r|jwxYw)aCalled when the other end of the low-level stream is half-closed. If this returns a false value (including None), the transport will close itself. If it returns a true value, closing the transport is up to the protocol. Tz%r received EOFN)rr0 get_debugrdebugrr r _on_handshake_completeConnectionResetErrorr rrrQr _do_writerr ExceptionrrJrDs r eof_receivedzSSLProtocol.eof_receivedsg" z##%% 6 .555{.;;;++,@AAAAA 0 888 0 9:::+%4NN$$$$$ 0 999    0 9:::!!##### 0 999!!#####:9    O ! ! # # #  s%AE(;E%E;AE)E%E+cv||jvr |j|S|j|j||S|Sr>)rrr<r9s rr8zSSLProtocol._get_extra_infosA 4;  ;t$ $ _ (?11$@@ @Nrcd}|tjkrd}n|jtjkr|tjkrd}nw|jtjkr|tjkrd}nO|jtjkr|tjkrd}n'|jtjkr|tjkrd}|r ||_dStd|j|)NFTz!cannot switch state from {} to {}) r r rr r rrrformat)r3 new_statealloweds rrzSSLProtocol._set_states (2 2 2GG K+5 5 5 )6 6 6GG K+8 8 8 )1 1 1GG K+3 3 3 )2 2 2GG K+4 4 4 )2 2 2G  -#DKKK3::K,,-- -rcfjr4tjdj_nd_tjj j fd_ dS)Nz%r starts SSL handshakec,Sr>)_check_handshake_timeoutrDsrz.SSLProtocol._start_handshake..s$*G*G*I*Ir) r0rrrtime_handshake_start_timerr r call_laterrrrrDs`rrzSSLProtocol._start_handshakes :   ! ! . L2D 9 9 9)-):):D & &)-D & (5666 J ! !$"="I"I"I"I K K & rc|jtjkr/d|jd}|t |dSdS)Nz$SSL handshake is taking longer than z! seconds: aborting the connection)rr r r _fatal_errorConnectionAbortedError)r3msgs rrz$SSLProtocol._check_handshake_timeout"s_ ;*7 7 7+.+++    4S99 : : : : : 8 7rc |j|ddS#t$r|YdSt j$r }||Yd}~dSd}~wwxYwr>)r do_handshakerSSLAgainErrors_process_outgoingrSSLErrorrs rrzSSLProtocol._do_handshake+s . L % % ' ' '  ' ' - - - - -  % % %  " " $ $ $ $ $ $| - - -  ' ' , , , , , , , , , -s2BB!A<<Bc|j |jd|_|j} | |tjn||}n#t$rt}|tjt|tj rd}nd}| ||| |Yd}~dSd}~wwxYw|jr:|j|jz }t%jd||dz|j|||||jt2jkr=t2j|_|j|| |dS)Nz1SSL handshake failed on verifying the certificatezSSL handshake failedz%r: SSL handshake took %.1f msg@@)peercertcipher compression ssl_object) rrrrr r getpeercertrr rurCertificateErrorrrr0rrrrrrupdaterr rrrrrCrrr)r3 handshake_excsslobjrrrdts rrz"SSLProtocol._on_handshake_complete5s  ) 5  * 1 1 3 3 3-1D * $ 0 89999##))++HH    OO,6 7 7 7#s344 -I,   c3 ' ' '    $ $ $ FFFFF  :   ! ! K""T%??B L94c J J J H"(--//'-'9'9';';&,  . . . ?.9 9 9.=DO   . .t/F/F/H/H I I I  s8A)) C'3A)C""C'cjtjtjtjfvrdSj dj_jtjkrdS tjj j fd_ dS)NTc,Sr>)_check_shutdown_timeoutrDsrrz-SSLProtocol._start_shutdown..ns4466r)rr rrr rr2r rrr0rrrrrDs`rrIzSSLProtocol._start_shutdown]s K ) ) *   F   **.D  ' ;*7 7 7 KKMMMMM OO,5 6 6 6,0J,A,A*6666--D ) NN     rc|jtjtjfvr.|jt jddSdS)NzSSL shutdown timed out)rr rrrrr TimeoutErrorrDs rrz#SSLProtocol._check_shutdown_timeoutrsf K ) )   O ( ('(@AA C C C C C   rc||tj|dSr>)rrr rrrDs rrzSSLProtocol._do_flush|s=  (1222 rcf |js|j|||ddS#t $r|YdStj$r }||Yd}~dSd}~wwxYwr>) rrunwrapr_call_eof_received_on_shutdown_completerrrrs rrzSSLProtocol._do_shutdowns -% & ##%%%  " " $ $ $  # # % % %  & &t , , , , , % % %  " " $ $ $ $ $ $| , , ,  & &s + + + + + + + + + ,s A!!B0B0B++B0c|j |jd|_|r||dS|j|jjdSr>)rrrr0rrrJ)r3 shutdown_excs rrz!SSLProtocol._on_shutdown_completesk  ( 4  ) 0 0 2 2 2,0D )  8   l + + + + + J !6 7 7 7 7 7rc|tj|j|jdSdSr>)rr r rrrDs rrzSSLProtocol._abortsB (2333 ? & O ! ! # # # # # ' &rc|jtjtjtjfvr;|jt jkrtj d|xjdz c_dS|D]9}|j ||xj t|z c_ : |jtjkr|dSdS#t $r!}||dYd}~dSd}~wwxYw)NzSSL connection is closedrFatal error on SSL protocol)rr rrr rr!LOG_THRESHOLD_FOR_CONNLOST_WRITESrwarningrrrrr rrr)r3rr}exs rr{zSSLProtocol._write_appdatas& K ) ) *   )"MMM9::: OOq OO F  1 1D   & &t , , ,  # #s4yy 0 # # # A{.666     76 A A A   b"? @ @ @ @ @ @ @ @ @ As#)C C;C66C;c\ |jr~|jd}|j|}t|}||kr#||d|jd<|xj|zc_n|jd=|xj|zc_|j~n#t $rYnwxYw|dSr)rrr~rrrr)r3r}countdata_lens rrzSSLProtocol._do_writes % 8*1- **400t998##-1%&&\D'*++u4++++A.++x7++% 8    D       sBB BBc|jsB|j}t|r|j||dSr>)rrrrrr~r[r|s rrzSSLProtocol._process_outgoings\' ,>&&((D4yy ,%%d+++ !!#####rc|jtjtjfvrdS |js`|jr|n||jr| n| | dS#t$r!}| |dYd}~dSd}~wwxYw)Nr)rr r rrQr_do_read__buffered_do_read__copiedrrrrhrr)r3r"s rrzSSLProtocol._do_reads K ( )    F A+ -/,++----))+++&-NN$$$$**,,,  % % ' ' ' ' ' A A A   b"? @ @ @ @ @ @ @ @ @ AsA;B C *CC c,d}d}}t|} j||}|dkr^|}||kr9j||z ||d}|dkr||z }nn#||k9jfdn#t$rYnwxYw|dkr||s*  dSdS)Nrrc,Sr>)rrDsrrz0SSLProtocol._do_read__buffered..sr) rrorrrr0rrrrrI)r3offsetr$bufwantss` rr(zSSLProtocol._do_read__bufferedsK++D,F,F,H,HIIC L%%eS11Eqyyunn L--efnc&''lKKEqyy% unnJ(()@)@)@)@AAA    D  A::  - -f 5 5 5 #  # # % % %  " " " " " # #sA?B== C  C cd}d}d} |j|j}|sn(|rd}d}|}n|rd}||g}n||Jn#t$rYnwxYw|r|j|n/|s-|jd||s*|| dSdS)N1TFr) rrrrrrC data_receivedjoinrrI)r3chunkzeroonefirstr}s rr)zSSLProtocol._do_read__copieds3  ' ))$-88' DC!EE'C!5>DDKK&&& '    D   =   , ,U 3 3 3 3 =   , ,SXXd^^ < < < #  # # % % %  " " " " " # #sA A A! A!c8 |jtjkrBtj|_|j}|rt jddSdSdS#ttf$rt$r!}| |dYd}~dSd}~wwxYw)Nz?returning true from eof_received() has no effect when using sslzError calling eof_received()) rrrrrCrrr!KeyboardInterrupt SystemExit BaseExceptionr)r3 keep_openr"s rrzSSLProtocol._call_eof_received!s B"2"AAA"2"< .;;== CN$BCCCCC BACC":.     B B B   b"@ A A A A A A A A A BsAAB8BBc:|}||jkrw|jspd|_ |jdS#t t f$rt$r/}|j d||j |dYd}~dSd}~wwxYw||j krw|jrrd|_ |j dS#t t f$rt$r/}|j d||j |dYd}~dSd}~wwxYwdSdS)NTzprotocol.pause_writing() failedmessage exceptionrr@Fz protocol.resume_writing() failed) rdr`rrrC pause_writingr8r9r:r0call_exception_handlerrr_resume_writing)r3sizers rr[z SSLProtocol._control_app_writing0s**,, 4, , ,T5M ,'+D $ "0022222%z2        11@!$!%!4 $ 33 T- - -$2J -',D $ "1133333%z2        11A!$!%!4 $ 33  . - - -s/A B%$BB1C D'$DDc*|jj|jzSr>)rpendingrrDs rrdz"SSLProtocol._get_write_buffer_sizeMs~%(???rc^t||tj\}}||_||_dSr>)r,r!FLOW_CONTROL_HIGH_WATER_SSL_WRITEr`r_r\s rrZz$SSLProtocol._set_write_buffer_limitsPs7, #yBDD c$(!#&   rcd|_dSr)rQrDs rrTzSSLProtocol._pause_readingXs#'   rcfjr(d_fd}j|dSdS)NFc jtjkrdSjtjkrdSjtjkrdSdSr>)rr r rrrrrrDsrresumez+SSLProtocol._resume_reading..resume_s};"2":::MMOOOOO[$4$===NN$$$$$[$4$===%%'''''>=r)rQr0r)r3rKs` rrWzSSLProtocol._resume_reading[sW  # )',D $ ( ( ( ( ( J  ( ( ( ( ( ) )rc|}||jkr)|js"d|_|jdS||jkr)|jr$d|_|jdSdSdS)NTF)rorlrrrUrkrX)r3rCs rrhz SSLProtocol._control_ssl_readingjs))++ 4, , ,T5M ,'+D $ O ) ) + + + + + T- - -$2J -',D $ O * * , , , , ,. - - -rc^t||tj\}}||_||_dSr>)r,r FLOW_CONTROL_HIGH_WATER_SSL_READrlrkr\s rrgz#SSLProtocol._set_read_buffer_limitsss7, #yACC c$(!#&   rc|jjSr>)rrErDs rroz!SSLProtocol._get_read_buffer_sizeys ~%%rc&|jrJd|_dS)z\Called when the low-level transport's buffer goes over the high-water mark. TN)rrDs rr@zSSLProtocol.pause_writing~s!++++#'   rcN|jsJd|_|dS)z^Called when the low-level transport's buffer drains below the low-water mark. FN)rrrDs rrBzSSLProtocol.resume_writings3''''#(       rFatal error on transportc\|jr|j|t|tr5|jrt jd||ddSdSt|tj s&|j |||j|ddSdS)Nz%r: %sT)exc_infor=) rrruOSErrorr0rrrrCancelledErrorrA)r3rr>s rrzSSLProtocol._fatal_errors ? . O ( ( - - - c7 # # z##%% E XtWtDDDDDD E EC!:;;  J - -" !_ //       r)FNTNNr>r)rR).rrrrrrrr6r?rrrrrrrr8rrrrrrIrrrrrr{rrrr(r)rr[rdrZrTrWrhrgror@rBrrrrrrsH  $#59&*'+&* Q"Q"Q"Q"f 1 1 1###   "2"2"2H%%%    !!!F$-$-$-P ;;;...$$$P*CCC - - -888$$$AAA0!!! $$$AAA,###:###< B B B:@@@''''((( ) ) )---'''' &&& (((!!!      rr)renumrr ImportErrorrrrrlogrSSLWantReadErrorSSLSyscallErrorrEnumr rr$r,_FlowControlMixin Transportr.rrrrrr`s JJJJ CCC?*C,?@Nty & & & & &ty & & &   *s;s;s;s;s;J8&0s;s;s;lV V V V V ),V V V V V s