a
    zeCk                     @   s   d dl Z d dlZzd dlZW n ey2   dZY n0 ddlmZ ddlmZ ddlmZ ddlm	Z	 dd Z
d	Zd
ZdZdZG dd deZG dd dejejZG dd dejZdS )    N   )	constants)	protocols)
transports)loggerc                 C   s"   | rt dt }|sd|_|S )Nz(Server side SSL needs a valid SSLContextF)
ValueErrorsslZcreate_default_contextZcheck_hostname)server_sideserver_hostname
sslcontext r   &/usr/lib/python3.9/asyncio/sslproto.py_create_transport_context   s    r   Z	UNWRAPPEDZDO_HANDSHAKEZWRAPPEDZSHUTDOWNc                   @   sz   e Zd ZdZdddZedd Zedd Zed	d
 Zedd Z	dddZ
dddZdd ZdddZdddZdS )_SSLPipei   Nc                 C   sH   || _ || _|| _t| _t | _t | _d | _	d| _
d | _d | _d S NF)_context_server_side_server_hostname
_UNWRAPPED_stater   Z	MemoryBIO	_incoming	_outgoing_sslobj_need_ssldata_handshake_cb_shutdown_cb)selfcontextr	   r
   r   r   r   __init__7   s    

z_SSLPipe.__init__c                 C   s   | j S N)r   r   r   r   r   r   M   s    z_SSLPipe.contextc                 C   s   | j S r   )r   r    r   r   r   
ssl_objectR   s    z_SSLPipe.ssl_objectc                 C   s   | j S r   )r   r    r   r   r   need_ssldataZ   s    z_SSLPipe.need_ssldatac                 C   s
   | j tkS r   )r   _WRAPPEDr    r   r   r   wrapped`   s    z_SSLPipe.wrappedc                 C   sR   | j tkrtd| jj| j| j| j| jd| _	t
| _ || _| jddd\}}|S )Nz"handshake in progress or completed)r	   r
       T)only_handshake)r   r   RuntimeErrorr   Zwrap_bior   r   r   r   r   _DO_HANDSHAKEr   feed_ssldatar   callbackssldataappdatar   r   r   do_handshakei   s    	
z_SSLPipe.do_handshakec                 C   sB   | j tkrtd| j tkr$tdt| _ || _| d\}}|S )Nzno security layer presentzshutdown in progressr%   )r   r   r'   	_SHUTDOWNr   r)   r*   r   r   r   shutdown~   s    	

z_SSLPipe.shutdownc                 C   s   | j   | d\}}d S )Nr%   )r   Z	write_eofr)   )r   r,   r-   r   r   r   feed_eof   s    
z_SSLPipe.feed_eofFc              
   C   s  | j tkr"|r|g}ng }g |fS d| _|r8| j| g }g }z| j tkrz| j  t| _ | j	rl| 	d  |rz||fW S | j tkr| j
| j}|| |sqqnJ| j tkr| j  d | _t| _ | jr|   n| j tkr|| j
  W n| tjtjfyn } zZt|dd }|tjtjtjfvrN| j tkrL| j	rL| 	|  |tjk| _W Y d }~n
d }~0 0 | jjr|| j
  ||fS )NFerrno)r   r   r   r   writer(   r   r.   r#   r   readmax_sizeappendr/   unwrapr   r   SSLErrorCertificateErrorgetattrSSL_ERROR_WANT_READSSL_ERROR_WANT_WRITESSL_ERROR_SYSCALLr   pending)r   datar&   r-   r,   chunkexc	exc_errnor   r   r   r)      sX    











"
z_SSLPipe.feed_ssldatar   c              
   C   s  | j tkr6|t|k r&||d  g}ng }|t|fS g }t|}d| _z(|t|k rn|| j||d  7 }W nj tjy } zPt	|dd }|j
dkrtj }|_|tjtjtjfvr |tjk| _W Y d }~n
d }~0 0 | jjr|| j  |t|ks| jrBqqB||fS )NFr2   ZPROTOCOL_IS_SHUTDOWN)r   r   len
memoryviewr   r   r3   r   r8   r:   reasonr;   r2   r<   r=   r   r>   r6   r4   )r   r?   offsetr,   viewrA   rB   r   r   r   feed_appdata   s4    

"z_SSLPipe.feed_appdata)N)N)N)F)r   )__name__
__module____qualname__r5   r   propertyr   r!   r"   r$   r.   r0   r1   r)   rH   r   r   r   r   r   #   s   








Kr   c                   @   s   e Zd ZejjZdd Zd$ddZdd Z	dd	 Z
d
d Zdd ZejfddZdd Zdd Zdd Zd%ddZdd Zdd Zedd Zdd Zd d! Zd"d# ZdS )&_SSLProtocolTransportc                 C   s   || _ || _d| _d S r   )_loop_ssl_protocol_closed)r   loopZssl_protocolr   r   r   r      s    z_SSLProtocolTransport.__init__Nc                 C   s   | j ||S r   )rO   _get_extra_infor   namedefaultr   r   r   get_extra_info&  s    z$_SSLProtocolTransport.get_extra_infoc                 C   s   | j | d S r   )rO   _set_app_protocol)r   protocolr   r   r   set_protocol*  s    z"_SSLProtocolTransport.set_protocolc                 C   s   | j jS r   )rO   _app_protocolr    r   r   r   get_protocol-  s    z"_SSLProtocolTransport.get_protocolc                 C   s   | j S r   )rP   r    r   r   r   
is_closing0  s    z _SSLProtocolTransport.is_closingc                 C   s   d| _ | j  d S NT)rP   rO   _start_shutdownr    r   r   r   close3  s    z_SSLProtocolTransport.closec                 C   s&   | j s"|d| t| d |   d S )Nzunclosed transport )source)rP   ResourceWarningr_   )r   _warnr   r   r   __del__>  s    z_SSLProtocolTransport.__del__c                 C   s    | j j}|d u rtd| S )Nz*SSL transport has not been initialized yet)rO   
_transportr'   
is_reading)r   trr   r   r   re   C  s    z _SSLProtocolTransport.is_readingc                 C   s   | j j  d S r   )rO   rd   pause_readingr    r   r   r   rg   I  s    z#_SSLProtocolTransport.pause_readingc                 C   s   | j j  d S r   )rO   rd   resume_readingr    r   r   r   rh   Q  s    z$_SSLProtocolTransport.resume_readingc                 C   s   | j j|| d S r   )rO   rd   set_write_buffer_limits)r   highlowr   r   r   ri   Y  s    z-_SSLProtocolTransport.set_write_buffer_limitsc                 C   s   | j j S r   )rO   rd   get_write_buffer_sizer    r   r   r   rl   n  s    z+_SSLProtocolTransport.get_write_buffer_sizec                 C   s   | j j S r   )rO   rd   get_write_buffer_limitsr    r   r   r   rm   r  s    z-_SSLProtocolTransport.get_write_buffer_limitsc                 C   s
   | j jjS r   )rO   rd   _protocol_pausedr    r   r   r   rn   x  s    z&_SSLProtocolTransport._protocol_pausedc                 C   s<   t |tttfs$tdt|j |s,d S | j| d S )Nz+data: expecting a bytes-like instance, got )	
isinstancebytes	bytearrayrD   	TypeErrortyperI   rO   _write_appdatar   r?   r   r   r   r3   }  s    z_SSLProtocolTransport.writec                 C   s   dS r   r   r    r   r   r   can_write_eof  s    z#_SSLProtocolTransport.can_write_eofc                 C   s   | j   d| _d S r]   )rO   _abortrP   r    r   r   r   abort  s    
z_SSLProtocolTransport.abort)N)NN)rI   rJ   rK   r   Z_SendfileModeZFALLBACKZ_sendfile_compatibler   rV   rY   r[   r\   r_   warningswarnrc   re   rg   rh   ri   rl   rm   rL   rn   r3   rv   rx   r   r   r   r   rM     s&   


rM   c                   @   s   e Zd Zd+ddZdd Zd,dd	Zd
d Zdd Zdd Zdd Z	dd Z
dd Zd-ddZdd Zdd Zdd Zdd Zd d! Zd"d# Zd.d%d&Zd'd( Zd)d* ZdS )/SSLProtocolFNTc	           	      C   s   t d u rtd|d u r tj}n|dkr6td| |sDt||}|| _|rZ|sZ|| _nd | _|| _t	|d| _
t | _d| _|| _|| _| | t| j| | _d | _d| _d| _d| _d | _|| _|| _d S )Nzstdlib ssl module not availabler   z7ssl_handshake_timeout should be a positive number, got )r   F)r   r'   r   ZSSL_HANDSHAKE_TIMEOUTr   r   r   r   _sslcontextdict_extracollectionsdeque_write_backlog_write_buffer_size_waiterrN   rW   rM   _app_transport_sslpipe_session_established_in_handshake_in_shutdownrd   _call_connection_made_ssl_handshake_timeout)	r   rQ   app_protocolr   waiterr	   r
   Zcall_connection_madeZssl_handshake_timeoutr   r   r   r     sB    

zSSLProtocol.__init__c                 C   s   || _ t|tj| _d S r   )rZ   ro   r   ZBufferedProtocol_app_protocol_is_buffer)r   r   r   r   r   rW     s    
zSSLProtocol._set_app_protocolc                 C   sD   | j d u rd S | j  s:|d ur.| j | n| j d  d | _ d S r   )r   	cancelledset_exception
set_resultr   rA   r   r   r   _wakeup_waiter  s    

zSSLProtocol._wakeup_waiterc                 C   s&   || _ t| j| j| j| _|   d S r   )rd   r   r|   r   r   r   _start_handshake)r   	transportr   r   r   connection_made  s    zSSLProtocol.connection_madec                 C   sn   | j r d| _ | j| jj| n| jd ur2d| j_d | _d | _t| dd rT| j	
  | | d | _d | _d S )NFT_handshake_timeout_handle)r   rN   	call_soonrZ   connection_lostr   rP   rd   r:   r   cancelr   r   r   r   r   r   r     s    


zSSLProtocol.connection_lostc                 C   s   | j   d S r   )rZ   pause_writingr    r   r   r   r      s    zSSLProtocol.pause_writingc                 C   s   | j   d S r   )rZ   resume_writingr    r   r   r   r     s    zSSLProtocol.resume_writingc                 C   s  | j d u rd S z| j |\}}W nH ttfy:    Y n2 tyj } z| |d W Y d }~d S d }~0 0 |D ]}| j| qp|D ]}|rz&| jrt	
| j| n| j| W nL ttfy    Y n6 ty } z| |d W Y d }~ d S d }~0 0 q|    qqd S )NzSSL error in data receivedz/application protocol failed to receive SSL data)r   r)   
SystemExitKeyboardInterruptBaseException_fatal_errorrd   r3   r   r   Z_feed_data_to_buffered_protorZ   data_receivedr^   )r   r?   r,   r-   er@   exr   r   r   r     s8    
zSSLProtocol.data_receivedc                 C   s^   zL| j  rtd|  | t | js@| j }|r@t	d W | j
  n| j
  0 d S )Nz%r received EOFz?returning true from eof_received() has no effect when using ssl)rN   	get_debugr   debugr   ConnectionResetErrorr   rZ   eof_receivedwarningrd   r_   )r   Z	keep_openr   r   r   r   2  s    


zSSLProtocol.eof_receivedc                 C   s4   || j v r| j | S | jd ur,| j||S |S d S r   )r~   rd   rV   rS   r   r   r   rR   H  s
    


zSSLProtocol._get_extra_infoc                 C   s.   | j r
d S | jr|   nd| _ | d d S )NTr%   )r   r   rw   rt   r    r   r   r   r^   P  s    
zSSLProtocol._start_shutdownc                 C   s.   | j |df |  jt|7  _|   d S )Nr   )r   r6   r   rC   _process_write_backlogru   r   r   r   rt   Y  s    zSSLProtocol._write_appdatac                 C   s\   | j  r$td|  | j  | _nd | _d| _| jd | j 	| j
| j| _|   d S )Nz%r starts SSL handshakeT)r%   r   )rN   r   r   r   time_handshake_start_timer   r   r6   Z
call_laterr   _check_handshake_timeoutr   r   r    r   r   r   r   ^  s    

zSSLProtocol._start_handshakec                 C   s*   | j du r&d| j d}| t| d S )NTz$SSL handshake is taking longer than z! seconds: aborting the connection)r   r   r   ConnectionAbortedError)r   msgr   r   r   r   m  s    
z$SSLProtocol._check_handshake_timeoutc              
   C   s  d| _ | j  | jj}z|d ur&|| }W n^ ttfyH    Y nH ty } z0t	|t
jrhd}nd}| || W Y d }~d S d }~0 0 | j r| j | j }td| |d  | jj|| | |d | jr| j| j |   d| _| j| j d S )NFz1SSL handshake failed on verifying the certificatezSSL handshake failedz%r: SSL handshake took %.1f msg     @@)peercertciphercompressionr!   T)r   r   r   r   r!   Zgetpeercertr   r   r   ro   r   r9   r   rN   r   r   r   r   r   r~   updater   r   r   rZ   r   r   r   r   r   r   )r   Zhandshake_excZsslobjr   rA   r   Zdtr   r   r   _on_handshake_completev  s8    

z"SSLProtocol._on_handshake_completec              
   C   sB  | j d u s| jd u rd S ztt| jD ]}| jd \}}|rR| j||\}}n*|rj| j| j}d}n| j| j	}d}|D ]}| j 
| q|t|k r||f| jd< | j jr| j    q| jd= |  jt|8  _q(W n\ ttfy    Y nF ty< } z,| jr| | n| |d W Y d }~n
d }~0 0 d S )Nr   r   zFatal error on SSL transport)rd   r   rangerC   r   rH   r.   r   r0   	_finalizer3   Z_pausedrh   r   r   r   r   r   r   )r   ir?   rF   r,   r@   rA   r   r   r   r     s:    
z"SSLProtocol._process_write_backlogFatal error on transportc                 C   sV   t |tr(| j r@tjd| |dd n| j||| j| d | jrR| j| d S )Nz%r: %sT)exc_info)message	exceptionr   rX   )	ro   OSErrorrN   r   r   r   Zcall_exception_handlerrd   Z_force_close)r   rA   r   r   r   r   r     s    

zSSLProtocol._fatal_errorc                 C   s   d | _ | jd ur| j  d S r   )r   rd   r_   r    r   r   r   r     s    
zSSLProtocol._finalizec                 C   s0   z | j d ur| j   W |   n
|   0 d S r   )rd   rx   r   r    r   r   r   rw     s    
zSSLProtocol._abort)FNTN)N)N)r   )rI   rJ   rK   r   rW   r   r   r   r   r   r   r   rR   r^   rt   r   r   r   r   r   r   rw   r   r   r   r   r{     s,      
.

&
		)+
r{   )r   ry   r   ImportError r   r   r   logr   r   r   r(   r#   r/   objectr   Z_FlowControlMixinZ	TransportrM   Protocolr{   r   r   r   r   <module>   s(   
 y~