???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3

  \•e  ã               @   sÎ   d dl Z d dlZyd dlZW n ek
r4   dZY nX ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	„ 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é   )Úbase_events)Úcompat)Ú	protocols)Ú
transports)Úloggerc             C   sj   | rt dƒ‚ttdƒr*tjƒ }|sfd|_n<tjtjƒ}| jtjO  _| jtj	O  _|j
ƒ  tj|_|S )Nz(Server side SSL needs a valid SSLContextÚcreate_default_contextF)Ú
ValueErrorÚhasattrÚsslr   Úcheck_hostnameZ
SSLContextZPROTOCOL_SSLv23ZoptionsZOP_NO_SSLv2ZOP_NO_SSLv3Zset_default_verify_pathsZCERT_REQUIREDÚverify_mode)Úserver_sideÚserver_hostnameÚ
sslcontext© r   ú(/usr/lib64/python3.6/asyncio/sslproto.pyÚ_create_transport_context   s    
r   c               C   s
   t tdƒS )NÚ	MemoryBIO)r
   r   r   r   r   r   Ú_is_sslproto_available%   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 ) Ú_SSLPipeé   i   Nc             C   sH   || _ || _|| _t| _tjƒ | _tjƒ | _d | _	d| _
d | _d | _d S )NF)Ú_contextÚ_server_sideÚ_server_hostnameÚ
_UNWRAPPEDÚ_stater   r   Ú	_incomingÚ	_outgoingÚ_sslobjÚ_need_ssldataÚ_handshake_cbÚ_shutdown_cb)ÚselfÚcontextr   r   r   r   r   Ú__init__D   s    

z_SSLPipe.__init__c             C   s   | j S )N)r   )r#   r   r   r   r$   Z   s    z_SSLPipe.contextc             C   s   | j S )N)r   )r#   r   r   r   Ú
ssl_object_   s    z_SSLPipe.ssl_objectc             C   s   | j S )N)r    )r#   r   r   r   Úneed_ssldatag   s    z_SSLPipe.need_ssldatac             C   s
   | j tkS )N)r   Ú_WRAPPED)r#   r   r   r   Úwrappedm   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_handshakev   s    	
z_SSLPipe.do_handshakec             C   sB   | j tkrtdƒ‚| j tkr$tdƒ‚t| _ || _| jdƒ\}}|S )Nzno security layer presentzshutdown in progressr*   )r   r   r,   Ú	_SHUTDOWNr"   r.   )r#   r/   r0   r1   r   r   r   Úshutdown‹   s    	

z_SSLPipe.shutdownc             C   s   | j jƒ  | jdƒ\}}d S )Nr*   )r   Z	write_eofr.   )r#   r0   r1   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j|ƒ g }g }y´| j tkrx| jjƒ  t| _ | j	rl| j	d ƒ |rx||fS | j tkr¨xn| jj
| jƒ}|j|ƒ |s„P q„W nJ| j tkrØ| jjƒ  d | _t| _ | jrò| jƒ  n| j tkrò|j| jj
ƒ ƒ W nx tjtjfk
rl } zRt|dd ƒtjtjtjfkrN| j tkrL| j	rL| j	|ƒ ‚ |jtjk| _W Y d d }~X nX | jjrˆ|j| jj
ƒ ƒ ||fS )NFÚerrno)r   r   r    r   Úwriter-   r   r2   r(   r!   ÚreadÚmax_sizeÚappendr3   Zunwrapr"   r   ÚSSLErrorÚCertificateErrorÚgetattrÚSSL_ERROR_WANT_READÚSSL_ERROR_WANT_WRITEÚSSL_ERROR_SYSCALLr6   r   Úpending)r#   Údatar+   r1   r0   ÚchunkÚexcr   r   r   r.   ©   sV    











 
z_SSLPipe.feed_ssldatar   c             C   s  | j tkr6|t|ƒk r&||d … g}ng }|t|ƒfS g }t|ƒ}x¼d| _y(|t|ƒk rp|| jj||d … ƒ7 }W n\ tjk
rÎ } z>|j	dkr˜tj
|_|jtj
tjtjfkr°‚ |jtj
k| _W Y d d }~X nX | jjrè|j| jjƒ ƒ |t|ƒksú| jrDP qDW ||fS )NFZPROTOCOL_IS_SHUTDOWN)r   r   ÚlenÚ
memoryviewr    r   r7   r   r;   Úreasonr>   r6   r?   r@   r   rA   r:   r8   )r#   rB   Úoffsetr0   ZviewrD   r   r   r   Úfeed_appdataó   s2    


 z_SSLPipe.feed_appdatai   )N)N)N)F)r   )Ú__name__Ú
__module__Ú__qualname__r9   r%   Úpropertyr$   r&   r'   r)   r2   r4   r5   r.   rI   r   r   r   r   r   0   s   
	



Jr   c               @   s†   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Ze	j
rHd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S ) Ú_SSLProtocolTransportc             C   s   || _ || _d| _d S )NF)Ú_loopÚ_ssl_protocolÚ_closed)r#   ÚloopZssl_protocolr   r   r   r%   )  s    z_SSLProtocolTransport.__init__Nc             C   s   | j j||ƒS )N)rP   Ú_get_extra_info)r#   ÚnameÚdefaultr   r   r   Úget_extra_info/  s    z$_SSLProtocolTransport.get_extra_infoc             C   s   || j _d S )N)rP   Ú_app_protocol)r#   Úprotocolr   r   r   Úset_protocol3  s    z"_SSLProtocolTransport.set_protocolc             C   s   | j jS )N)rP   rW   )r#   r   r   r   Úget_protocol6  s    z"_SSLProtocolTransport.get_protocolc             C   s   | j S )N)rQ   )r#   r   r   r   Ú
is_closing9  s    z _SSLProtocolTransport.is_closingc             C   s   d| _ | jjƒ  d S )NT)rQ   rP   Ú_start_shutdown)r#   r   r   r   Úclose<  s    z_SSLProtocolTransport.closec             C   s&   | j s"tjd|  t| d | jƒ  d S )Nzunclosed transport %r)Úsource)rQ   ÚwarningsÚwarnÚResourceWarningr]   )r#   r   r   r   Ú__del__K  s    z_SSLProtocolTransport.__del__c             C   s   | j jjƒ  d S )N)rP   Ú
_transportÚpause_reading)r#   r   r   r   rd   Q  s    z#_SSLProtocolTransport.pause_readingc             C   s   | j jjƒ  d S )N)rP   rc   Úresume_reading)r#   r   r   r   re   Y  s    z$_SSLProtocolTransport.resume_readingc             C   s   | j jj||ƒ d S )N)rP   rc   Úset_write_buffer_limits)r#   ZhighZlowr   r   r   rf   a  s    z-_SSLProtocolTransport.set_write_buffer_limitsc             C   s   | j jjƒ S )N)rP   rc   Úget_write_buffer_size)r#   r   r   r   rg   v  s    z+_SSLProtocolTransport.get_write_buffer_sizec             C   s<   t |tttfƒs$tdjt|ƒjƒƒ‚|s,d S | jj	|ƒ d S )Nz/data: expecting a bytes-like instance, got {!r})
Ú
isinstanceÚbytesÚ	bytearrayrF   Ú	TypeErrorÚformatÚtyperJ   rP   Ú_write_appdata)r#   rB   r   r   r   r7   z  s    z_SSLProtocolTransport.writec             C   s   dS )NFr   )r#   r   r   r   Úcan_write_eof‡  s    z#_SSLProtocolTransport.can_write_eofc             C   s   | j jƒ  d S )N)rP   Ú_abort)r#   r   r   r   Úabort‹  s    z_SSLProtocolTransport.abort)N)NN)rJ   rK   rL   r%   rV   rY   rZ   r[   r]   r   ZPY34rb   rd   re   rf   rg   r7   ro   rq   r   r   r   r   rN   &  s   

rN   c               @   sœ   e Zd Zd'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!d"„Zd#d$„ Zd%d&„ ZdS )+ÚSSLProtocolFNTc             C   s¦   t d krtdƒ‚|st||ƒ}|| _|r6| r6|| _nd | _|| _t|d| _tj	ƒ | _
d| _|| _|| _|| _t| j| ƒ| _d | _d| _d| _d| _d | _|| _d S )Nzstdlib ssl module not available)r   r   F)r   r,   r   r   r   Ú_sslcontextÚdictÚ_extraÚcollectionsÚdequeÚ_write_backlogÚ_write_buffer_sizeÚ_waiterrO   rW   rN   Ú_app_transportÚ_sslpipeÚ_session_establishedÚ_in_handshakeÚ_in_shutdownrc   Ú_call_connection_made)r#   rR   Zapp_protocolr   Zwaiterr   r   Zcall_connection_mader   r   r   r%   œ  s,    


zSSLProtocol.__init__c             C   sD   | j d krd S | j jƒ s:|d k	r.| j j|ƒ n| j jd ƒ d | _ d S )N)rz   Z	cancelledZset_exceptionZ
set_result)r#   rD   r   r   r   Ú_wakeup_waiterÀ  s    

zSSLProtocol._wakeup_waiterc             C   s&   || _ t| j| j| jƒ| _| jƒ  d S )N)rc   r   rs   r   r   r|   Ú_start_handshake)r#   Ú	transportr   r   r   Úconnection_madeÊ  s
    
zSSLProtocol.connection_madec             C   s8   | j rd| _ | jj| jj|ƒ d | _d | _| j|ƒ d S )NF)r}   rO   Ú	call_soonrW   Úconnection_lostrc   r{   r   )r#   rD   r   r   r   r†   Õ  s    zSSLProtocol.connection_lostc             C   s   | j jƒ  d S )N)rW   Úpause_writing)r#   r   r   r   r‡   ã  s    zSSLProtocol.pause_writingc             C   s   | j jƒ  d S )N)rW   Úresume_writing)r#   r   r   r   rˆ   é  s    zSSLProtocol.resume_writingc             C   s´   | j d krd S y| j j|ƒ\}}W nH tjk
rj } z*| jjƒ rTtjd| |j|j	ƒ | j
ƒ  d S d }~X nX x|D ]}| jj|ƒ qrW x(|D ] }|r¢| jj|ƒ qŒ| jƒ  P qŒW d S )Nz%r: SSL error %s (reason %s))r|   r.   r   r;   rO   Ú	get_debugr   Úwarningr6   rG   rp   rc   r7   rW   Údata_receivedr\   )r#   rB   r0   r1   ÚerC   r   r   r   r‹   ï  s"    



zSSLProtocol.data_receivedc          
   C   sT   zB| j jƒ rtjd| ƒ | jtƒ | js@| jjƒ }|r@tj	dƒ W d | j
jƒ  X d S )Nz%r received EOFz?returning true from eof_received() has no effect when using ssl)rO   r‰   r   Údebugr   ÚConnectionResetErrorr~   rW   Úeof_receivedrŠ   rc   r]   )r#   Z	keep_openr   r   r   r     s    


zSSLProtocol.eof_receivedc             C   s4   || j kr| j | S | jd k	r,| jj||ƒS |S d S )N)ru   rc   rV   )r#   rT   rU   r   r   r   rS   !  s
    


zSSLProtocol._get_extra_infoc             C   s.   | j r
d S | jr| jƒ  nd| _ | jdƒ d S )NTr*   )r   r~   rp   rn   )r#   r   r   r   r\   )  s    
zSSLProtocol._start_shutdownc             C   s.   | j j|dfƒ |  jt|ƒ7  _| jƒ  d S )Nr   )rx   r:   ry   rE   Ú_process_write_backlog)r#   rB   r   r   r   rn   2  s    zSSLProtocol._write_appdatac             C   sH   | j jƒ r$tjd| ƒ | j jƒ | _nd | _d| _| jjdƒ | j	ƒ  d S )Nz%r starts SSL handshakeTr*   r   )r*   r   )
rO   r‰   r   r   ÚtimeÚ_handshake_start_timer~   rx   r:   r   )r#   r   r   r   r‚   7  s    
zSSLProtocol._start_handshakec             C   sT  d| _ | jj}yF|d k	r|‚|jƒ }t| jdƒsR| jrR| jjtj	krRtj
|| jƒ W n~ tk
rÒ } zb| jjƒ ržt|tjƒrŽtjd| dd ntjd| dd | jjƒ  t|tƒrÀ| j|ƒ d S ‚ W Y d d }~X nX | jjƒ r| jjƒ | j }tjd| |d ƒ | jj||jƒ |jƒ |d	 | jr4| jj| j ƒ | jƒ  d| _!| jj"| j#ƒ d S )
NFr   z5%r: SSL handshake failed on verifying the certificateT)Úexc_infoz%r: SSL handshake failedz%r: SSL handshake took %.1f msg     @@)ÚpeercertÚcipherÚcompressionr&   )$r~   r|   r&   Zgetpeercertr
   rs   r   r   r   Z	CERT_NONEZmatch_hostnameÚBaseExceptionrO   r‰   rh   r<   r   rŠ   rc   r]   Ú	Exceptionr   r‘   r’   r   ru   Úupdater•   r–   r€   rW   r„   r{   r}   r…   r   )r#   Zhandshake_excZsslobjr”   rD   Zdtr   r   r   Ú_on_handshake_completeC  sD    




z"SSLProtocol._on_handshake_completec             C   s>  | j d ks| jd krd S yÎxÈtt| jƒƒD ]¶}| jd \}}|rT| jj||ƒ\}}n*|rl| jj| jƒ}d}n| jj| j	ƒ}d}x|D ]}| j j
|ƒ q„W |t|ƒk rÆ||f| jd< | j jrÄ| j jƒ  P | jd= |  jt|ƒ8  _q*W W nR tk
r8 } z4| jr| j|ƒ n| j|dƒ t|tƒs(‚ W Y d d }~X nX d S )Nr   r   zFatal error on SSL transport)rc   r|   ÚrangerE   rx   rI   r2   rš   r4   Ú	_finalizer7   Z_pausedre   ry   r—   r~   Ú_fatal_errorrh   r˜   )r#   ÚirB   rH   r0   rC   rD   r   r   r   r   w  s8    

z"SSLProtocol._process_write_backlogúFatal error on transportc             C   sX   t |tjƒr*| jjƒ rBtjd| |dd n| jj||| j| dœƒ | jrT| jj	|ƒ d S )Nz%r: %sT)r“   )ÚmessageZ	exceptionrƒ   rX   )
rh   r   Z_FATAL_ERROR_IGNORErO   r‰   r   r   Zcall_exception_handlerrc   Z_force_close)r#   rD   r    r   r   r   r   £  s    

zSSLProtocol._fatal_errorc             C   s   d | _ | jd k	r| jjƒ  d S )N)r|   rc   r]   )r#   r   r   r   rœ   ²  s    
zSSLProtocol._finalizec          
   C   s(   z| j d k	r| j jƒ  W d | jƒ  X d S )N)rc   rq   rœ   )r#   r   r   r   rp   ¸  s    
zSSLProtocol._abort)FNT)N)N)rŸ   )rJ   rK   rL   r%   r   r„   r†   r‡   rˆ   r‹   r   rS   r\   rn   r‚   rš   r   r   rœ   rp   r   r   r   r   rr   •  s$    
"


	4,
rr   )rv   r_   r   ÚImportErrorÚ r   r   r   r   Úlogr   r   r   r   r-   r(   r3   Úobjectr   Z_FlowControlMixinZ	TransportrN   ZProtocolrr   r   r   r   r   Ú<module>   s*   
 wn