ñÕYf]eã @sddlZddlZyddlZWnek rBdZYnXddlmZddlmZddlmZddlmZddl m Z dd „Z d d „Z d Z d ZdZdZGdd„deƒZGdd„dejejƒZGdd„dejƒZdS)éNé)Ú base_events)Úcompat)Ú protocols)Ú transports)ÚloggercCs�|rtdƒ‚ttdƒr?tjƒ}|s‹d|_nLtjtjƒ}|jtjO_|jtj O_|j ƒtj |_ |S)Nz(Server side SSL needs a valid SSLContextÚcreate_default_contextF) Ú ValueErrorÚhasattrÚsslrÚcheck_hostnameZ SSLContextZPROTOCOL_SSLv23ZoptionsZ OP_NO_SSLv2Z OP_NO_SSLv3Zset_default_verify_pathsZ CERT_REQUIREDÚ verify_mode)Ú server_sideÚserver_hostnameÚ sslcontext©rú5/opt/alt/python35/lib64/python3.5/asyncio/sslproto.pyÚ_create_transport_contexts     rcCs ttdƒS)NÚ MemoryBIO)r r rrrrÚ_is_sslproto_available%srZ UNWRAPPEDZ DO_HANDSHAKEZWRAPPEDZSHUTDOWNc@s»eZdZdZdZddd„Zedd„ƒZed d „ƒZed d „ƒZ ed d„ƒZ ddd„Z ddd„Z dd„Z ddd„Zddd„ZdS)Ú_SSLPipeaAn SSL "Pipe". An SSL pipe allows you to communicate with an SSL/TLS protocol instance through memory buffers. It can be used to implement a security layer for an existing connection where you don't have access to the connection's file descriptor, or for some reason you don't want to use it. An SSL pipe can be in "wrapped" and "unwrapped" mode. In unwrapped mode, data is passed through untransformed. In wrapped mode, application level data is encrypted to SSL record level data and vice versa. The SSL record level is the lowest level in the SSL protocol suite and is what travels as-is over the wire. An SslPipe initially is in "unwrapped" mode. To start SSL, call do_handshake(). To shutdown SSL again, call unwrap(). éiNcCsj||_||_||_t|_tjƒ|_tjƒ|_d|_ d|_ d|_ d|_ dS)a• The *context* argument specifies the ssl.SSLContext to use. The *server_side* argument indicates whether this is a server side or client side transport. The optional *server_hostname* argument can be used to specify the hostname you are connecting to. You may only specify this parameter if the _ssl module supports Server Name Indication (SNI). NF) Ú_contextÚ _server_sideÚ_server_hostnameÚ _UNWRAPPEDÚ_stater rÚ _incomingÚ _outgoingÚ_sslobjÚ _need_ssldataÚ _handshake_cbÚ _shutdown_cb)ÚselfÚcontextrrrrrÚ__init__Ds       z_SSLPipe.__init__cCs|jS)z*The SSL context passed to the constructor.)r)r#rrrr$Zsz_SSLPipe.contextcCs|jS)z^The internal ssl.SSLObject instance. Return None if the pipe is not wrapped. )r)r#rrrÚ ssl_object_sz_SSLPipe.ssl_objectcCs|jS)zgWhether more record level data is needed to complete a handshake that is currently in progress.)r )r#rrrÚ need_ssldatagsz_SSLPipe.need_ssldatacCs |jtkS)zj Whether a security layer is currently in effect. Return False during handshake. )rÚ_WRAPPED)r#rrrÚwrappedmsz_SSLPipe.wrappedcCs||jtkrtdƒ‚|jj|j|jd|jd|jƒ|_ t |_||_ |j dddƒ\}}|S)aLStart the SSL handshake. Return a list of ssldata. A ssldata element is a list of buffers The optional *callback* argument can be used to install a callback that will be called when the handshake is complete. The callback will be called with None if successful, else an exception instance. z"handshake in progress or completedrróÚonly_handshakeT) rrÚ RuntimeErrorrZwrap_biorrrrrÚ _DO_HANDSHAKEr!Ú feed_ssldata)r#ÚcallbackÚssldataÚappdatarrrÚ do_handshakevs      z_SSLPipe.do_handshakecCsa|jtkrtdƒ‚|jtkr6tdƒ‚t|_||_|jdƒ\}}|S)a1Start the SSL shutdown sequence. Return a list of ssldata. A ssldata element is a list of buffers The optional *callback* argument can be used to install a callback that will be called when the shutdown is complete. The callback will be called without arguments. zno security layer presentzshutdown in progressr*)rrr,Ú _SHUTDOWNr"r.)r#r/r0r1rrrÚshutdown‹s     z_SSLPipe.shutdowncCs&|jjƒ|jdƒ\}}dS)z†Send a potentially "ragged" EOF. This method will raise an SSL_ERROR_EOF exception if the EOF is unexpected. r*N)rZ write_eofr.)r#r0r1rrrÚfeed_eofŸs z_SSLPipe.feed_eofFcCs|jtkr1|r!|g}ng}g|fSd|_|rP|jj|ƒg}g}yú|jtkrª|jjƒt|_|j rš|j dƒ|rª||fS|jtkrìx™|jj |j ƒ}|j |ƒ|s¼Pq¼Wni|jt kr0|jjƒd|_t|_|jrU|jƒn%|jtkrU|j |jj ƒƒWn˜tjtjfk rð}zlt|ddƒtjtjtjfkrÉ|jtkrÆ|j rÆ|j |ƒ‚|jtjk|_WYdd}~XnX|jjr|j |jj ƒƒ||fS)a�Feed SSL record level data into the pipe. The data must be a bytes instance. It is OK to send an empty bytes instance. This can be used to get ssldata for a handshake initiated by this endpoint. Return a (ssldata, appdata) tuple. The ssldata element is a list of buffers containing SSL data that needs to be sent to the remote SSL. The appdata element is a list of buffers containing plaintext data that needs to be forwarded to the application. The appdata list may contain an empty buffer indicating an SSL "close_notify" alert. This alert must be acknowledged by calling shutdown(). FNÚerrno)rrr rÚwriter-rr2r(r!ÚreadÚmax_sizeÚappendr3Zunwrapr"r ÚSSLErrorÚCertificateErrorÚgetattrÚSSL_ERROR_WANT_READÚSSL_ERROR_WANT_WRITEÚSSL_ERROR_SYSCALLr6rÚpending)r#Údatar+r1r0ÚchunkÚexcrrrr.©sV                ( z_SSLPipe.feed_ssldatarcCsl|jtkrM|t|ƒkr7||d…g}ng}|t|ƒfSg}t|ƒ}xd|_y6|t|ƒkr ||jj||d…ƒ7}Wn|tjk r}zY|j dkrÔtj |_ |j tj tj tj fkrø‚|j tj k|_WYdd}~XnX|jjrB|j|jjƒƒ|t|ƒks]|jrbPqbW||fS)a Feed plaintext data into the pipe. Return an (ssldata, offset) tuple. The ssldata element is a list of buffers containing record level data that needs to be sent to the remote SSL instance. The offset is the number of plaintext bytes that were processed, which may be less than the length of data. NOTE: In case of short writes, this call MUST be retried with the SAME buffer passed into the *data* argument (i.e. the id() must be the same). This is an OpenSSL requirement. A further particularity is that a short write will always have offset == 0, because the _ssl module does not enable partial writes. And even though the offset is zero, there will still be encrypted data in ssldata. NFZPROTOCOL_IS_SHUTDOWN)rrÚlenÚ memoryviewr rr7r r;Úreasonr>r6r?r@rrAr:r8)r#rBÚoffsetr0ZviewrDrrrÚ feed_appdataós2  $  ( z_SSLPipe.feed_appdatai)Ú__name__Ú __module__Ú __qualname__Ú__doc__r9r%Úpropertyr$r&r'r)r2r4r5r.rIrrrrr0s   Jrc@sÊeZdZdd„Zddd„Zdd„Zdd „Zd d „Zd d „Ze j rldd„Z dd„Z dd„Z dddd„Zdd„Zdd„Zdd„Zdd„ZdS)Ú_SSLProtocolTransportcCs(||_||_||_d|_dS)NF)Ú_loopÚ _ssl_protocolÚ _app_protocolÚ_closed)r#ÚloopZ ssl_protocolÚ app_protocolrrrr%)s   z_SSLProtocolTransport.__init__NcCs|jj||ƒS)z#Get optional transport information.)rQÚ_get_extra_info)r#ÚnameÚdefaultrrrÚget_extra_info0sz$_SSLProtocolTransport.get_extra_infocCs ||_dS)N)rR)r#ÚprotocolrrrÚ set_protocol4sz"_SSLProtocolTransport.set_protocolcCs|jS)N)rR)r#rrrÚ get_protocol7sz"_SSLProtocolTransport.get_protocolcCs|jS)N)rS)r#rrrÚ is_closing:sz _SSLProtocolTransport.is_closingcCsd|_|jjƒ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)rSrQÚ_start_shutdown)r#rrrÚclose=s z_SSLProtocolTransport.closecCs+|js'tjd|tƒ|jƒdS)Nzunclosed transport %r)rSÚwarningsÚwarnÚResourceWarningr_)r#rrrÚ__del__Ls z_SSLProtocolTransport.__del__cCs|jjjƒdS)z”Pause the receiving end. No data will be passed to the protocol's data_received() method until resume_reading() is called. N)rQÚ _transportÚ pause_reading)r#rrrreQsz#_SSLProtocolTransport.pause_readingcCs|jjjƒdS)z…Resume the receiving end. Data received will once again be passed to the protocol's data_received() method. N)rQrdÚresume_reading)r#rrrrfYsz$_SSLProtocolTransport.resume_readingcCs|jjj||ƒdS)a¦Set 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)rQrdÚset_write_buffer_limits)r#ZhighZlowrrrrgasz-_SSLProtocolTransport.set_write_buffer_limitscCs|jjjƒS)z,Return the current size of the write buffer.)rQrdÚget_write_buffer_size)r#rrrrhvsz+_SSLProtocolTransport.get_write_buffer_sizecCsTt|tttfƒs6tdjt|ƒjƒƒ‚|s@dS|jj |ƒdS)z�Write 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 {!r}N) Ú isinstanceÚbytesÚ bytearrayrFÚ TypeErrorÚformatÚtyperJrQÚ_write_appdata)r#rBrrrr7zs  z_SSLProtocolTransport.writecCsdS)zAReturn True if this transport supports write_eof(), False if not.Fr)r#rrrÚ can_write_eof‡sz#_SSLProtocolTransport.can_write_eofcCs|jjƒdS)zÝClose 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. N)rQÚ_abort)r#rrrÚabort‹sz_SSLProtocolTransport.abort)rJrKrLr%rYr[r\r]r_rZPY34rcrerfrgrhr7rprrrrrrrO&s            rOc@sôeZdZdZddddd„Zddd„Zd d „Zd d „Zd d„Zdd„Z dd„Z dd„Z ddd„Z dd„Z dd„Zdd„Zdd„Zdd „Zd!d"d#„Zd$d%„Zd&d'„ZdS)(Ú SSLProtocolz‰SSL protocol. Implementation of SSL on top of a socket using incoming and outgoing buffers which are ssl.MemoryBIO objects. FNTcCsûtdkrtdƒ‚|s-t||ƒ}||_|rO| rO||_n d|_||_td|ƒ|_tj ƒ|_ d|_ ||_ ||_ ||_t|j ||jƒ|_d|_d|_d|_d|_d|_||_dS)Nzstdlib ssl module not availablerrF)r r,rrrÚ _sslcontextÚdictÚ_extraÚ collectionsÚdequeÚ_write_backlogÚ_write_buffer_sizeÚ_waiterrPrRrOÚ_app_transportÚ_sslpipeÚ_session_establishedÚ _in_handshakeÚ _in_shutdownrdÚ_call_connection_made)r#rTrUrZwaiterrrZcall_connection_maderrrr%œs.                 zSSLProtocol.__init__cCs^|jdkrdS|jjƒsQ|dk rA|jj|ƒn|jjdƒd|_dS)N)r{Z cancelledZ set_exceptionZ set_result)r#rDrrrÚ_wakeup_waiterÁs zSSLProtocol._wakeup_waitercCs5||_t|j|j|jƒ|_|jƒdS)zXCalled when the low-level connection is made. Start the SSL handshake. N)rdrrtrrr}Ú_start_handshake)r#Ú transportrrrÚconnection_madeËs   zSSLProtocol.connection_madecCsN|jr+d|_|jj|jj|ƒd|_d|_|j|ƒdS)zÝCalled 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). FN)r~rPÚ call_soonrRÚconnection_lostrdr|r‚)r#rDrrrr‡Ös     zSSLProtocol.connection_lostcCs|jjƒdS)z\Called when the low-level transport's buffer goes over the high-water mark. N)rRÚ pause_writing)r#rrrrˆäszSSLProtocol.pause_writingcCs|jjƒdS)z^Called when the low-level transport's buffer drains below the low-water mark. N)rRÚresume_writing)r#rrrr‰êszSSLProtocol.resume_writingcCsÚy|jj|ƒ\}}Wnatjk r}z>|jjƒr_tjd||j|j ƒ|j ƒdSWYdd}~XnXx|D]}|j j |ƒq‡Wx2|D]*}|rÇ|j j|ƒq¨|jƒPq¨WdS)zXCalled when some SSL data is received. The argument is a bytes object. z%r: SSL error %s (reason %s)N)r}r.r r;rPÚ get_debugrÚwarningr6rGrqrdr7rRÚ data_receivedr^)r#rBr0r1ÚerCrrrrŒðs     zSSLProtocol.data_receivedc Cspz[|jjƒr"tjd|ƒ|jtƒ|jsZ|jjƒ}|rZtj dƒWd|j j ƒXdS)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. z%r received EOFz?returning true from eof_received() has no effect when using sslN) rPrŠrÚdebugr‚ÚConnectionResetErrorrrRÚ eof_receivedr‹rdr_)r#Z keep_openrrrr�s  zSSLProtocol.eof_receivedcCsD||jkr|j|S|jdk r<|jj||ƒS|SdS)N)rvrdrY)r#rWrXrrrrVs  zSSLProtocol._get_extra_infocCs=|jr dS|jr#|jƒnd|_|jdƒdS)NTr*)r€rrqro)r#rrrr^&s     zSSLProtocol._start_shutdowncCs9|jj|dfƒ|jt|ƒ7_|jƒdS)Nr)ryr:rzrEÚ_process_write_backlog)r#rBrrrro/szSSLProtocol._write_appdatacCsm|jjƒr4tjd|ƒ|jjƒ|_n d|_d|_|jjdƒ|jj |j ƒdS)Nz%r starts SSL handshakeTr*r)r*r) rPrŠrrŽÚtimeÚ_handshake_start_timerryr:r†r‘)r#rrrrƒ4s  zSSLProtocol._start_handshakecCsÕd|_|jj}ye|dk r*|‚|jƒ}t|jdƒsy|jry|jjtj krytj ||jƒWn¥t k r!}z…|j j ƒrßt|tjƒrÉtjd|ddƒntjd|ddƒ|jjƒt|tƒr |j|ƒdS‚WYdd}~XnX|j j ƒr^|j jƒ|j}tjd||dƒ|jjd |d |jƒd |jƒd |ƒ|jr«|jj|j ƒ|jƒd|_!|j j"|j#ƒdS) NFr z5%r: SSL handshake failed on verifying the certificateÚexc_infoTz%r: SSL handshake failedz%r: SSL handshake took %.1f msg@�@ÚpeercertÚcipherÚ compressionr&)$rr}r&Z getpeercertr rtrr r Z CERT_NONEZmatch_hostnameÚ BaseExceptionrPrŠrir<rr‹rdr_Ú Exceptionr‚r’r“rŽrvÚupdater–r—r�rRr…r|r~r†r‘)r#Z handshake_excZsslobjr•rDZdtrrrÚ_on_handshake_complete@sD               z"SSLProtocol._on_handshake_completecCs“|jdkrdSyxtt|jƒƒD]û}|jd\}}|ri|jj||ƒ\}}n?|r�|jj|jƒ}d}n|jj|j ƒ}d}x|D]}|jj |ƒq¯W|t|ƒkr||f|jd<|jj r|jj ƒP|jd=|j t|ƒ8_ q,WWn`tk rŽ}z@|jrZ|j|ƒn|j|dƒt|tƒs|‚WYdd}~XnXdS)NrrzFatal error on SSL transport)rdÚrangerEryr}rIr2r›r4Ú _finalizer7Z_pausedrfrzr˜rÚ _fatal_errorrir™)r#ÚirBrHr0rCrDrrrr‘ts8        z"SSLProtocol._process_write_backlogzFatal error on transportc Cs…t|tjƒr=|jjƒrhtjd||ddƒn+|jjd|d|d|jd|iƒ|jr�|jj |ƒdS)Nz%r: %sr”TÚmessageZ exceptionr„rZ) rirZ_FATAL_ERROR_IGNORErPrŠrrŽZcall_exception_handlerrdZ _force_close)r#rDr rrrrž s    zSSLProtocol._fatal_errorcCs)d|_|jdk r%|jjƒdS)N)r}rdr_)r#rrrr�¯s zSSLProtocol._finalizec Cs2z |jdk r|jjƒWd|jƒXdS)N)rdrrr�)r#rrrrqµszSSLProtocol._abort)rJrKrLrMr%r‚r…r‡rˆr‰rŒr�rVr^rorƒr›r‘ržr�rqrrrrrs•s& #       4 , rs)rwr`r Ú ImportErrorÚrrrrÚlogrrrrr-r(r3ÚobjectrZ_FlowControlMixinZ TransportrOZProtocolrsrrrrÚs(      ö n