î ±+ebcã @sddlZddlZyddlZWnek rBdZYnXddlmZddlmZddlmZddlm Z dd„Z d d „Z d Z d Z d ZdZGdd„deƒZGdd„dejejƒZGdd„dejƒZdS)éNé)Úcompat)Ú protocols)Ú transports)ÚloggercCs•|rtdƒ‚nttdƒrEtjƒ}|s‘d|_q‘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/python34/lib64/python3.4/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Úcontextr rrrrÚ__init__Cs       z_SSLPipe.__init__cCs|jS)z*The SSL context passed to the constructor.)r)r"rrrr#Ysz_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_ssldatafsz_SSLPipe.need_ssldatacCs |jtkS)zj Whether a security layer is currently in effect. Return False during handshake. )rÚ_WRAPPED)r"rrrÚwrappedlsz_SSLPipe.wrappedcCs—|jtkrtdƒ‚n|jj|j|jd|jd|jƒ|_ t |_||_ |j dddƒ\}}t |ƒdks“t‚|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 completedr róÚonly_handshakeTr)rrÚ RuntimeErrorrZwrap_biorrrrrÚ _DO_HANDSHAKEr Ú feed_ssldataÚlenÚAssertionError)r"ÚcallbackÚssldataÚappdatarrrÚ do_handshakeus     z_SSLPipe.do_handshakecCs£|jtkrtdƒ‚n|jtkr<tdƒ‚n|jttfksWt‚t|_||_|jdƒ\}}|gksŸ|dgksŸt‚|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!r-)r"r0r1r2rrrÚshutdownŠs   !z_SSLPipe.shutdowncCsG|jjƒ|jdƒ\}}|gksC|dgksCt‚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/)r"r1r2rrrÚfeed_eofžs z_SSLPipe.feed_eofFcCs8|jtkr1|r!|g}ng}g|fSd|_|rS|jj|ƒng}g}y |jtkr³|jjƒt|_|j r |j dƒn|r³||fSn|jtkrøx¢|jj |j ƒ}|j |ƒ|sÅPqÅqÅWno|jt kr?|jjƒd|_t|_|jrg|jƒqgn(|jtkrg|j |jj ƒƒnWnžtjtjfk r}zrt|ddƒtjtjtjfkrá|jtkrÛ|j rÛ|j |ƒn‚n|jtjk|_WYdd}~XnX|jjr.|j |jj ƒƒn||fS)aFeed 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)rrrrÚwriter,rr3r'r ÚreadÚmax_sizeÚappendr4Zunwrapr!r ÚSSLErrorÚCertificateErrorÚgetattrÚSSL_ERROR_WANT_READÚSSL_ERROR_WANT_WRITEÚSSL_ERROR_SYSCALLr7rÚpending)r"Údatar*r2r1ÚchunkÚexcrrrr-¨sV              ( z_SSLPipe.feed_ssldatarcCs£d|kot|ƒkns(t‚|jtkru|t|ƒkr_||d…g}ng}|t|ƒfSg}t|ƒ}xd|_y9|t|ƒkrË||jj||d…ƒ7}nWn‚tj k rP}z_|j dkrtj |_ n|j tj tj tjfkr)‚n|j tj k|_WYdd}~XnX|jjrv|j|jjƒƒn|t|ƒks‘|jrŠPqŠqŠW||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. rNFZPROTOCOL_IS_SHUTDOWN)r.r/rrÚ memoryviewrrr8r r<Úreasonr?r7r@rArrBr;r9)r"rCÚoffsetr1ZviewrErrrÚ feed_appdataòs4(  ' ( z_SSLPipe.feed_appdatai)Ú__name__Ú __module__Ú __qualname__Ú__doc__r:r$Úpropertyr#r%r&r(r3r5r6r-rIrrrrr/s   Jrc@sµeZdZdd„Zddd„Zdd„Zdd „ZejrWd d „Z nd d „Z dd„Z dddd„Z dd„Z dd„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_info/sz$_SSLProtocolTransport.get_extra_infocCs|jS)N)rS)r"rrrÚ is_closing3sz _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Úclose6s z_SSLProtocolTransport.closecCs.|js*tjd|tƒ|jƒndS)Nzunclosed transport %r)rSÚwarningsÚwarnÚResourceWarningr\)r"rrrÚ__del__Es 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"rrrrbJsz#_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)rQraÚresume_reading)r"rrrrcRsz$_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)rQraÚset_write_buffer_limits)r"ZhighZlowrrrrdZsz-_SSLProtocolTransport.set_write_buffer_limitscCs|jjjƒS)z,Return the current size of the write buffer.)rQraÚget_write_buffer_size)r"rrrreosz+_SSLProtocolTransport.get_write_buffer_sizecCsWt|tttfƒs9tdjt|ƒjƒƒ‚n|sCdS|jj |ƒdS)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 {!r}N) Ú isinstanceÚbytesÚ bytearrayrFÚ TypeErrorÚformatÚtyperJrQÚ_write_appdata)r"rCrrrr8ss  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$rYrZr\rZPY34r`rbrcrdrer8rmrorrrrrO%s         rOc@sñeZdZdZdddd„Zddd„Zdd „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. FNcCsøtdkrtdƒ‚n|s3t||ƒ}n||_|rU| rU||_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_shutdownra)r"rTrUrZwaiterr rrrrr$•s,               zSSLProtocol.__init__cCsa|jdkrdS|jjƒsT|dk rA|jj|ƒqT|jjdƒnd|_dS)N)rxZ cancelledZ set_exceptionZ set_result)r"rErrrÚ_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)rarrqrrrzÚ_start_handshake)r"Ú transportrrrÚconnection_madeÂs   zSSLProtocol.connection_madecCsD|jr.d|_|jj|jj|ƒnd|_d|_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_lostrary)r"rErrrrƒÍ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|ƒ\}}Wndtjk r‚}zA|jjƒrbtjd||j|j ƒn|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)rzr-r r<rPÚ get_debugrÚwarningr7rGrnrar8rRÚ data_receivedr[)r"rCr1r2ÚerDrrrrˆæs     zSSLProtocol.data_receivedc Csyzd|jjƒr%tjd|ƒn|jtƒ|jsc|jjƒ}|rctj dƒqcnWd|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~ÚConnectionResetErrorr|rRÚ eof_receivedr‡rar\)r"Z keep_openrrrrŒþs  zSSLProtocol.eof_receivedcCs1||jkr|j|S|jj||ƒSdS)N)rsrarY)r"rWrXrrrrVs zSSLProtocol._get_extra_infocCs'|jr dSd|_|jdƒdS)NTr))r}rl)r"rrrr[s  zSSLProtocol._start_shutdowncCs9|jj|dfƒ|jt|ƒ7_|jƒdS)Nr)rvr;rwr.Ú_process_write_backlog)r"rCrrrrl 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_timer|rvr;r‚r)r"rrrr%s  zSSLProtocol._start_handshakecCsÛd|_|jj}yn|dk r-|‚n|jƒ}t|jdƒs‚|jr‚|jjtj kr‚tj ||jƒq‚nWn¨t k r-}zˆ|j j ƒrët|tjƒrÒtjd|ddƒqëtjd|ddƒn|jjƒt|tƒr|j|ƒdS‚WYdd}~XnX|j j ƒrm|j jƒ|j}tjd||dƒn|jjd |d |jƒd |jƒd |ƒ|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%)#r|rzr%Z getpeercertr rqrr r Z CERT_NONEZmatch_hostnameÚ BaseExceptionrPr†rfr=rr‡rar\Ú Exceptionr~rŽrrŠrsÚupdater’r“rRrryr{r‚r)r"Z handshake_excZsslobjr‘rEZdtrrrÚ_on_handshake_complete1sB              z"SSLProtocol._on_handshake_completecCs®|jdkrdSy1x*tt|jƒƒD]}|jd\}}|ri|jj||ƒ\}}n?|r|jj|jƒ}d}n|jj|j ƒ}d}x|D]}|jj |ƒq¯W|t|ƒkr ||f|jd<|jj st ‚|jj r|jjƒnPn|jd=|jt|ƒ8_q,WWnctk r©}zC|jrr|j|ƒn|j|dƒt|tƒs—‚nWYdd}~XnXdS)NrrzFatal error on SSL transport)raÚranger.rvrzrIr3r—r5Ú _finalizer8r&r/Z_pausedrcrwr”r|Ú _fatal_errorrfr•)r"ÚirCrHr1rDrErrrrds:       z"SSLProtocol._process_write_backlogzFatal error on transportcCs’t|ttfƒrC|jjƒrrtjd||ddƒqrn/|jji|d6|d6|jd6|d6ƒ|jrŽ|jj |ƒndS)Nz%r: %srTÚmessageZ exceptionr€Zprotocol) rfÚBrokenPipeErrorr‹rPr†rrŠZcall_exception_handlerraZ _force_close)r"rErœrrrršs    zSSLProtocol._fatal_errorcCs#|jdk r|jjƒndS)N)rar\)r"rrrr™ŸszSSLProtocol._finalizec Cs5|jdk r1z|jjƒWd|jƒXndS)N)raror™)r"rrrrn£szSSLProtocol._abort)rJrKrLrMr$r~rrƒr„r…rˆrŒrVr[rlrr—rršr™rnrrrrrpŽs$ "       3 , rp)rtr]r Ú ImportErrorÚrrrÚlogrrrrr,r'r4ÚobjectrZ_FlowControlMixinZ TransportrOZProtocolrprrrrÚs&      ö h