o
     JAfCk                     @   s   d dl Z d dlZzd dlZW n ey   dZY nw 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.10/asyncio/sslproto.py_create_transport_context   s   r   Z	UNWRAPPEDZDO_HANDSHAKEZWRAPPEDZSHUTDOWNc                   @   s|   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   sJ   	 || _ || _|| _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      	 | j S N)r   r   r   r   r   r   M   s   z_SSLPipe.contextc                 C   r   r    )r   r!   r   r   r   
ssl_objectR   s   z_SSLPipe.ssl_objectc                 C   r   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   sT   	 | 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   sD   	 | 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}g |fS g }g |fS d| _|r | j| g }g }zZ| j tkrA| j  t| _ | j	r:| 	d  |rA||fW S | j tkrX	 | j
| j}|| |sVnqGn%| j tkrp| j  d | _t| _ | jro|   n| j tkr}|| j
  W n: tjtjfy } z*t|dd }|tjtjtjfvr| j tkr| j	r| 	|  |tjk| _W Y d }~nd }~ww | jjr|| j
  ||fS )NFTerrno)r   r   r   r   writer)   r   r/   r$   r   readmax_sizeappendr0   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*      sj   










z_SSLPipe.feed_ssldatar   c              
   C   s  	 | j tkr|t|k r||d  g}ng }|t|fS g }t|}	 d| _z|t|k r9|| j||d  7 }W n5 tjyo } z(t	|dd }|j
dkrTtj }|_|tjtjtjfvr_ |tjk| _W Y d }~nd }~ww | jjr||| j  |t|ks| jr	 ||fS q#)NTFr3   ZPROTOCOL_IS_SHUTDOWN)r   r   len
memoryviewr   r   r4   r   r9   r;   reasonr<   r3   r=   r>   r   r?   r7   r5   )r   r@   offsetr-   viewrB   rC   r   r   r   feed_appdata   s@   

z_SSLPipe.feed_appdatar    )F)r   )__name__
__module____qualname__r6   r   propertyr   r"   r#   r%   r/   r1   r2   r*   rI   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    )rP   _get_extra_infor   namedefaultr   r   r   get_extra_info&  s   z$_SSLProtocolTransport.get_extra_infoc                 C   s   | j | d S r    )rP   _set_app_protocol)r   protocolr   r   r   set_protocol*  s   z"_SSLProtocolTransport.set_protocolc                 C   s   | j jS r    )rP   _app_protocolr!   r   r   r   get_protocol-  s   z"_SSLProtocolTransport.get_protocolc                 C   s   | j S r    )rQ   r!   r   r   r   
is_closing0  s   z _SSLProtocolTransport.is_closingc                 C   s   	 d| _ | j  d S NT)rQ   rP   _start_shutdownr!   r   r   r   close3  s   z_SSLProtocolTransport.closec                 C   s*   | j s|d| t| d |   d S d S )Nzunclosed transport )source)rQ   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)rP   
_transportr(   
is_reading)r   trr   r   r   rf   C  s   z _SSLProtocolTransport.is_readingc                 C      	 | j j  d S r    )rP   re   pause_readingr!   r   r   r   ri   I     z#_SSLProtocolTransport.pause_readingc                 C   rh   r    )rP   re   resume_readingr!   r   r   r   rk   Q  rj   z$_SSLProtocolTransport.resume_readingc                 C   s   	 | j j|| d S r    )rP   re   set_write_buffer_limits)r   highlowr   r   r   rl   Y  s   z-_SSLProtocolTransport.set_write_buffer_limitsc                 C      	 | j j S r    )rP   re   get_write_buffer_sizer!   r   r   r   rp   n  s   z+_SSLProtocolTransport.get_write_buffer_sizec                 C   ro   r    )rP   re   get_write_buffer_limitsr!   r   r   r   rq   r  s   z-_SSLProtocolTransport.get_write_buffer_limitsc                 C   s
   | j jjS r    )rP   re   _protocol_pausedr!   r   r   r   rr   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	bytearrayrE   	TypeErrortyperJ   rP   _write_appdatar   r@   r   r   r   r4   }  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^   )rP   _abortrQ   r!   r   r   r   abort  s   

z_SSLProtocolTransport.abortr    )NN)rJ   rK   rL   r   Z_SendfileModeZFALLBACKZ_sendfile_compatibler   rW   rZ   r\   r]   r`   warningswarnrd   rf   ri   rk   rl   rp   rq   rM   rr   r4   rz   r|   r   r   r   r   rN     s(    


rN   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rtd| |s"t||}|| _|r-|s-|| _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_waiterrO   rX   rN   _app_transport_sslpipe_session_established_in_handshake_in_shutdownre   _call_connection_made_ssl_handshake_timeout)	r   rR   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    )r[   rs   r   ZBufferedProtocol_app_protocol_is_buffer)r   r   r   r   r   rX     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   rB   r   r   r   _wakeup_waiter  s   


zSSLProtocol._wakeup_waiterc                 C   s(   	 || _ t| j| j| j| _|   d S r    )re   r   r   r   r   r   _start_handshake)r   	transportr   r   r   connection_made  s   zSSLProtocol.connection_madec                 C   sp   	 | j rd| _ | j| jj| n	| jd urd| j_d | _d | _t| dd r+| j	
  | | d | _d | _d S )NFT_handshake_timeout_handle)r   rO   	call_soonr[   connection_lostr   rQ   re   r;   r   cancelr   r   r   r   r   r   r     s   



zSSLProtocol.connection_lostc                 C      	 | j   d S r    )r[   pause_writingr!   r   r   r   r         zSSLProtocol.pause_writingc                 C   r   r    )r[   resume_writingr!   r   r   r   r     r   zSSLProtocol.resume_writingc                 C   s  	 | j d u rd S z
| j |\}}W n" ttfy     ty4 } z| |d W Y d }~d S d }~ww |D ]}| j| q7|D ]A}|r}z| jrRt	
| j| n| j| W qB ttfyc     ty| } z| |d W Y d }~ d S d }~ww |    d S d S )NzSSL error in data receivedz/application protocol failed to receive SSL data)r   r*   
SystemExitKeyboardInterruptBaseException_fatal_errorre   r4   r   r   Z_feed_data_to_buffered_protor[   data_receivedr_   )r   r@   r-   r.   erA   exr   r   r   r     sF   
zSSLProtocol.data_receivedc                 C   s~   	 z7| j  rtd|  | t | js)| j }|r1t	d W | j
  d S W | j
  d S W | j
  d S | j
  w )Nz%r received EOFz?returning true from eof_received() has no effect when using ssl)rO   	get_debugr   debugr   ConnectionResetErrorr   r[   eof_receivedwarningre   r`   )r   Z	keep_openr   r   r   r   2  s   


zSSLProtocol.eof_receivedc                 C   s0   || j v r
| j | S | jd ur| j||S |S r    )r   re   rW   rT   r   r   r   rS   H  s
   


zSSLProtocol._get_extra_infoc                 C   s0   | j rd S | jr|   d S d| _ | d d S )NTr&   )r   r   r{   rx   r!   r   r   r   r_   P  s   zSSLProtocol._start_shutdownc                 C   s.   | j |df |  jt|7  _|   d S )Nr   )r   r7   r   rD   _process_write_backlogry   r   r   r   rx   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   )rO   r   r   r   time_handshake_start_timer   r   r7   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 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y"     tyE } zt	|t
jr2d}nd}| || W Y d }~d S d }~ww | j r\| j | j }td| |d  | jj|| | |d | jrt| 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   rs   r   r:   r   rO   r   r   r   r   r   r   updater   r   r   r[   r   r   r   r   r   r   )r   Zhandshake_excZsslobjr   rB   r   Zdtr   r   r   _on_handshake_completev  s<   

z"SSLProtocol._on_handshake_completec              
   C   sL  | j d u s
| jd u rd S zgtt| jD ]\}| jd \}}|r)| j||\}}n|r5| j| j}d}n	| j| j	}d}|D ]}| j 
| q@|t|k rc||f| jd< | j jr_| j    W d S | jd= |  jt|8  _qW d S  ttfy}     ty } z| jr| | n| |d W Y d }~d S W Y d }~d S d }~ww )Nr   r   zFatal error on SSL transport)re   r   rangerD   r   rI   r/   r   r1   	_finalizer4   Z_pausedrk   r   r   r   r   r   r   )r   ir@   rG   r-   rA   rB   r   r   r   r     sB   
z"SSLProtocol._process_write_backlogFatal error on transportc                 C   sZ   t |tr| j rtjd| |dd n| j||| j| d | jr+| j| d S d S )Nz%r: %sT)exc_info)message	exceptionr   rY   )	rs   OSErrorrO   r   r   r   Zcall_exception_handlerre   Z_force_close)r   rB   r   r   r   r   r     s   

zSSLProtocol._fatal_errorc                 C   s"   d | _ | jd ur| j  d S d S r    )r   re   r`   r!   r   r   r   r     s   
zSSLProtocol._finalizec                 C   s<   z| j d ur| j   W |   d S W |   d S |   w r    )re   r|   r   r!   r   r   r   r{     s   
zSSLProtocol._abort)FNTNr    )r   )rJ   rK   rL   r   rX   r   r   r   r   r   r   r   rS   r_   rx   r   r   r   r   r   r   r{   r   r   r   r   r     s0    
.

&
		)
+r   )r   r}   r   ImportError r   r   r   logr   r   r   r)   r$   r0   objectr   Z_FlowControlMixinZ	TransportrN   ZProtocolr   r   r   r   r   <module>   s,     y~