AlkantarClanX12

Your IP : 18.227.0.255


Current Path : /proc/self/root/opt/alt/python35/lib64/python3.5/asyncio/__pycache__/
Upload File :
Current File : //proc/self/root/opt/alt/python35/lib64/python3.5/asyncio/__pycache__/sslproto.cpython-35.pyc



��Yf]e�
@sddlZddlZyddlZWnek
rBdZYnXddlmZddlmZddlmZddlmZddl	m
Z
dd	�Zd
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_SSLv23ZoptionsZOP_NO_SSLv2ZOP_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
rrrrr�_is_sslproto_available%srZ	UNWRAPPEDZDO_HANDSHAKEZWRAPPEDZSHUTDOWNc@s�eZdZdZdZddd�Zedd��Zed	d
��Zedd��Z	ed
d��Z
ddd�Zddd�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�_staterr�	_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�\}}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 completedrr��only_handshakeTr)rr�RuntimeErrorrZwrap_biorrrrr�
_DO_HANDSHAKEr!�feed_ssldata�len�AssertionError)r#�callback�ssldata�appdatarrr�do_handshakevs					z_SSLPipe.do_handshakecCs�|jtkrtd��|jtkr6td��|jttfksQt�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-r0r"r.)r#r1r2r3rrr�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.r0)r#r2r3rrr�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-rr4r(r!�read�max_size�appendr5Zunwrapr"r�SSLError�CertificateError�getattr�SSL_ERROR_WANT_READ�SSL_ERROR_WANT_WRITE�SSL_ERROR_SYSCALLr8r�pending)r#�datar+r3r2�chunk�excrrrr.�sV
	
		



			

(z_SSLPipe.feed_ssldatarcCs�d|kot|�kns(t�|jtkru|t|�kr_||d�g}ng}|t|�fSg}t|�}xd|_y6|t|�kr�||jj||d��7}Wn|tj	k
rG}zY|j
dkr�tj|_|jtjtj
tjfkr �|jtjk|_WYdd}~XnX|jjrj|j|jj��|t|�ks�|jr�Pq�W||fS)aFeed 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/r0rr�
memoryviewr rr9rr=�reasonr@r8rArBrrCr<r:)r#rD�offsetr2ZviewrFrrr�feed_appdata�s4(	$(z_SSLPipe.feed_appdatai)�__name__�
__module__�__qualname__�__doc__r;r%�propertyr$r&r'r)r4r6r7r.rJrrrrr0s	
Jrc@s�eZdZdd�Zddd�Zdd�Zdd	�Zd
d�Zdd
�Ze	j
rldd�Zdd�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.)rR�_get_extra_info)r#�name�defaultrrr�get_extra_info0sz$_SSLProtocolTransport.get_extra_infocCs
||_dS)N)rS)r#�protocolrrr�set_protocol4sz"_SSLProtocolTransport.set_protocolcCs|jS)N)rS)r#rrr�get_protocol7sz"_SSLProtocolTransport.get_protocolcCs|jS)N)rT)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)rTrR�_start_shutdown)r#rrr�close=s	z_SSLProtocolTransport.closecCs+|js'tjd|t�|j�dS)Nzunclosed transport %r)rT�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)rR�
_transport�
pause_reading)r#rrrrfQsz#_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)rRre�resume_reading)r#rrrrgYsz$_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)rRre�set_write_buffer_limits)r#ZhighZlowrrrrhasz-_SSLProtocolTransport.set_write_buffer_limitscCs|jjj�S)z,Return the current size of the write buffer.)rRre�get_write_buffer_size)r#rrrrivsz+_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�	bytearrayrG�	TypeError�format�typerKrR�_write_appdata)r#rDrrrr9zs	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)rR�_abort)r#rrr�abort�sz_SSLProtocolTransport.abort)rKrLrMr%rZr\r]r^r`rZPY34rdrfrgrhrir9rqrsrrrrrP&s	
rPc@s�eZdZdZddddd�Zddd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zddd�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)rr,rrr�_sslcontext�dict�_extra�collections�deque�_write_backlog�_write_buffer_size�_waiterrQrSrP�_app_transport�_sslpipe�_session_established�
_in_handshake�_in_shutdownre�_call_connection_made)r#rUrVrZwaiterrrZcall_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#rFrrr�_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)rerrurrr~�_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)rrQ�	call_soonrS�connection_lostrer}r�)r#rFrrrr��s				zSSLProtocol.connection_lostcCs|jj�dS)z\Called when the low-level transport's buffer goes over
        the high-water mark.
        N)rS�
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)rS�resume_writing)r#rrrr��szSSLProtocol.resume_writingcCs�y|jj|�\}}Wnatjk
r}z>|jj�r_tjd||j|j	�|j
�dSWYdd}~XnXx|D]}|jj|�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.rr=rQ�	get_debugr�warningr8rHrrrer9rS�
data_receivedr_)r#rDr2r3�erErrrr��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)rQr�r�debugr��ConnectionResetErrorr�rS�eof_receivedr�rer`)r#Z	keep_openrrrr�s
	zSSLProtocol.eof_receivedcCsD||jkr|j|S|jdk	r<|jj||�S|SdS)N)rwrerZ)r#rXrYrrrrWs
zSSLProtocol._get_extra_infocCs=|jr
dS|jr#|j�nd|_|jd�dS)NTr*)r�r�rrrp)r#rrrr_&s		
	zSSLProtocol._start_shutdowncCs9|jj|df�|jt|�7_|j�dS)Nr)rzr<r{r/�_process_write_backlog)r#rDrrrrp/szSSLProtocol._write_appdatacCsm|jj�r4tjd|�|jj�|_n	d|_d|_|jjd�|jj	|j
�dS)Nz%r starts SSL handshakeTr*r)r*r)rQr�rr��time�_handshake_start_timer�rzr<r�r�)r#rrrr�4s		zSSLProtocol._start_handshakecCs�d|_|jj}ye|dk	r*|�|j�}t|jd�sy|jry|jjtj	krytj
||j�Wn�tk
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&)$r�r~r&Zgetpeercertr
rurr
rZ	CERT_NONEZmatch_hostname�
BaseExceptionrQr�rjr>rr�rer`�	Exceptionr�r�r�r�rw�updater�r�r�rSr�r}rr�r�)r#Z
handshake_excZsslobjr�rFZdtrrr�_on_handshake_complete@sD				


	
	z"SSLProtocol._on_handshake_completecCs�|jdkrdSy+x$tt|j��D]
}|jd\}}|ri|jj||�\}}n?|r�|jj|j�}d}n|jj|j	�}d}x|D]}|jj
|�q�W|t|�kr||f|jd<|jjst�|jj
r|jj�P|jd=|jt|�8_q,WWn`tk
r�}z@|jrl|j|�n|j|d�t|t�s��WYdd}~XnXdS)NrrzFatal error on SSL transport)re�ranger/rzr~rJr4r�r6�	_finalizer9r'r0Z_pausedrgr{r�r��_fatal_errorrjr�)r#�irDrIr2rErFrrrr�ts:		


	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�r[)
rjrZ_FATAL_ERROR_IGNORErQr�rr�Zcall_exception_handlerreZ_force_close)r#rFr�rrrr��s		
	zSSLProtocol._fatal_errorcCs)d|_|jdk	r%|jj�dS)N)r~rer`)r#rrrr��s	zSSLProtocol._finalizec
Cs2z |jdk	r|jj�Wd|j�XdS)N)rersr�)r#rrrrr�szSSLProtocol._abort)rKrLrMrNr%r�r�r�r�r�r�r�rWr_rpr�r�r�r�r�rrrrrrrt�s&#
	4,rt)rxrar�ImportError�rrrr�logrrrrr-r(r5�objectrZ_FlowControlMixinZ	TransportrPZProtocolrtrrrr�<module>s(
�n