o
     d/                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZmZmZmZ ddlmZmZmZmZ ddlmZmZ ddlmZmZmZmZ ed	Zeeeef d
f Z ee d
f Z!G dd deZ"eddG dd deZ#eddG dd dee# Z$dS )    N)	dataclass)wraps)	AnyCallableDictListMappingOptionalTupleTypeVarUnion   )BrokenResourceErrorEndOfStreamaclose_forcefullyget_cancelled_exc_class)TypedAttributeSettyped_attribute)AnyByteStream
ByteStreamListener	TaskGroupT_Retval.c                   @   s   e Zd ZU dZe Zee ed< e Z	e
ed< e Zeeeef ed< e Zeeeeeeef f  ed< e Zee
 ed< e Zeed< e Zeeeeef  ed< e Zejed	< e Zeed
< e Zeed< dS )TLSAttributez5Contains Transport Layer Security related attributes.alpn_protocolchannel_binding_tls_uniquecipherpeer_certificatepeer_certificate_binaryserver_sideshared_ciphers
ssl_objectstandard_compatibletls_versionN)__name__
__module____qualname____doc__r   r   r	   str__annotations__r   bytesr   r
   intr   r   r   _PCTRTTT_PCTRTTr   r   boolr    r   r!   ssl	SSLObjectr"   r#    r1   r1   </usr/local/lib/python3.10/dist-packages/anyio/streams/tls.pyr      s   
 r   F)eqc                   @   s  e Zd ZU dZeed< eed< ejed< ej	ed< ej	ed< e
ddddd	ded
ee dee deej dedd fddZdedef dedefddZdeeef fddZd$ddZd%dedefddZdeddfddZd$d d!Zedeeeg ef f fd"d#ZdS )&	TLSStreama  
    A stream wrapper that encrypts all sent data and decrypts received data.

    This class has no public initializer; use :meth:`wrap` instead.
    All extra attributes from :class:`~TLSAttribute` are supported.

    :var AnyByteStream transport_stream: the wrapped stream

    transport_streamr"   _ssl_object	_read_bio
_write_bioNT)r   hostnamessl_contextr"   r   r9   r:   returnc                   s   |du r| }|s&|rt jjnt jj}t |}tt dr&| jt jN  _t  }t  }|j	||||d}	| |||	||d}
|

|	jI dH  |
S )a  
        Wrap an existing stream with Transport Layer Security.

        This performs a TLS handshake with the peer.

        :param transport_stream: a bytes-transporting stream to wrap
        :param server_side: ``True`` if this is the server side of the connection, ``False`` if
            this is the client side (if omitted, will be set to ``False`` if ``hostname`` has been
            provided, ``False`` otherwise). Used only to create a default context when an explicit
            context has not been provided.
        :param hostname: host name of the peer (if host name checking is desired)
        :param ssl_context: the SSLContext object to use (if not provided, a secure default will be
            created)
        :param standard_compatible: if ``False``, skip the closing handshake when closing the
            connection, and don't raise an exception if the peer does the same
        :raises ~ssl.SSLError: if the TLS handshake fails

        NOP_IGNORE_UNEXPECTED_EOF)r   server_hostname)r5   r"   r6   r7   r8   )r/   PurposeCLIENT_AUTHSERVER_AUTHcreate_default_contexthasattroptionsr<   	MemoryBIOwrap_bio_call_sslobject_methoddo_handshake)clsr5   r   r9   r:   r"   purposebio_inbio_outr!   wrapperr1   r1   r2   wrapE   s.   

zTLSStream.wrapfunc.argsc                    s  	 z|| }W n t jyY   z| jjr!| j| j I d H  | j I d H }W n& ty8   | j	
  Y n tyP } z| j	
  | j
  t|d }~ww | j	| Y nl t jyn   | j| j I d H  Y nW t jy } z| j	
  | j
  t|d }~w t jy } z| j	
  | j
  t|t jsd|jv r| jrt|td  d }~ww | jjr| j| j I d H  |S q)NTUNEXPECTED_EOF_WHILE_READING)r/   SSLWantReadErrorr8   pendingr5   sendreadreceiver   r7   	write_eofOSErrorr   writeSSLWantWriteErrorSSLSyscallErrorSSLError
isinstanceSSLEOFErrorstrerrorr"   )selfrN   rO   resultdataexcr1   r1   r2   rF   }   sV   






z TLSStream._call_sslobject_methodc                    s:   |  | jjI dH  | j  | j  | j| j fS )z
        Does the TLS closing handshake.

        :return: a tuple of (wrapped byte stream, bytes left in the read buffer)

        N)rF   r6   unwrapr7   rV   r8   r5   rT   r_   r1   r1   r2   rc      s
   

zTLSStream.unwrapc                    sP   | j rz	|  I d H  W n ty   t| jI d H   w | j I d H  d S N)r"   rc   BaseExceptionr   r5   acloserd   r1   r1   r2   rg      s   zTLSStream.aclose   	max_bytesc                    s$   |  | jj|I d H }|st|S re   )rF   r6   rT   r   )r_   ri   ra   r1   r1   r2   rU      s
   zTLSStream.receiveitemc                    s   |  | jj|I d H  d S re   )rF   r6   rX   )r_   rj   r1   r1   r2   rS      s   zTLSStream.sendc                    sb   |  tj}td|}|r-t|dt|dpd}}||fdk r-td| td)NzTLSv(\d+)(?:\.(\d+))?   r   r   )rk      z;send_eof() requires at least TLSv1.3; current session uses z7send_eof() has not yet been implemented for TLS streams)extrar   r#   rematchr+   groupNotImplementedError)r_   r#   ro   majorminorr1   r1   r2   send_eof   s   "zTLSStream.send_eofc                    s   i  j jtj jjtj jjtj jjtj	 fddtj
 fddtj fddtj fddtj fddtj fddtj jji
S )Nc                          j dS )NFr6   getpeercertr1   rd   r1   r2   <lambda>   s    z,TLSStream.extra_attributes.<locals>.<lambda>c                      ru   )NTrv   r1   rd   r1   r2   rx      s    c                      s    j jS re   )r6   r   r1   rd   r1   r2   rx      s    c                      s
    j  S re   )r6   r    r1   rd   r1   r2   rx      s   
 c                          j S re   r"   r1   rd   r1   r2   rx          c                      ry   re   )r6   r1   rd   r1   r2   rx      r{   )r5   extra_attributesr   r   r6   selected_alpn_protocolr   get_channel_bindingr   r   r   r   r    r"   r!   r#   versionrd   r1   rd   r2   r|      s   



zTLSStream.extra_attributesr;   N)rh   )r$   r%   r&   r'   r   r)   r.   r/   r0   rD   classmethodr	   r(   
SSLContextrM   r   r   objectrF   r
   r*   rc   rg   r+   rU   rS   rt   propertyr   r   r|   r1   r1   r1   r2   r4   3   sN   
 



7

/


$r4   c                   @   s   e Zd ZU dZee ed< ejed< dZ	e
ed< dZeed< eded	ed
dfddZ	ddeegef dee d
dfddZdddZed
eeeg ef f fddZdS )TLSListenera  
    A convenience listener that wraps another listener and auto-negotiates a TLS session on every
    accepted connection.

    If the TLS handshake times out or raises an exception, :meth:`handle_handshake_error` is
    called to do whatever post-mortem processing is deemed necessary.

    Supports only the :attr:`~TLSAttribute.standard_compatible` extra attribute.

    :param Listener listener: the listener to wrap
    :param ssl_context: the SSL context object
    :param standard_compatible: a flag passed through to :meth:`TLSStream.wrap`
    :param handshake_timeout: time limit for the TLS handshake
        (passed to :func:`~anyio.fail_after`)
    listenerr:   Tr"      handshake_timeoutrb   streamr;   Nc                    sT   dt  d t|I d H  t| t stt d t| tr't| t r( d S )Nz
        Handle an exception raised during the TLS handshake.

        This method does 3 things:

        #. Forcefully closes the original stream
        #. Logs the exception (unless it was a cancellation exception) using the ``z``
           logger
        #. Reraises the exception if it was a base exception or a cancellation exception

        :param exc: the exception
        :param stream: the original stream

        zError during TLS handshake)r$   r   r\   r   logging	getLogger	exception	Exception)rb   r   r1   r1   r2   handle_handshake_error  s   z"TLSListener.handle_handshake_errorhandler
task_groupc                    s:   t  dtdd f fdd}j||I d H  d S )Nr   r;   c              
      s   ddl m} z$|j tj| jjdI d H }W d    n1 s%w   Y  W n tyG } z|| I d H  W Y d }~d S d }~ww  |I d H  d S )Nr   )
fail_after)r:   r"   )	 r   r   r4   rM   r:   r"   rf   r   )r   r   wrapped_streamrb   r   r_   r1   r2   handler_wrapper$  s     z*TLSListener.serve.<locals>.handler_wrapper)r   r   r   serve)r_   r   r   r   r1   r   r2   r     s   zTLSListener.servec                    s   | j  I d H  d S re   )r   rg   rd   r1   r1   r2   rg   6  s   zTLSListener.aclosec                    s   t j fddiS )Nc                      ry   re   rz   r1   rd   r1   r2   rx   <  r{   z.TLSListener.extra_attributes.<locals>.<lambda>)r   r"   rd   r1   rd   r2   r|   9  s   zTLSListener.extra_attributesre   r   )r$   r%   r&   r'   r   r   r)   r/   r   r"   r.   r   floatstaticmethodrf   r   r   r   r4   r	   r   r   rg   r   r   r|   r1   r1   r1   r2   r      s&   
 


$r   )%r   rn   r/   dataclassesr   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   _core._typedattrr   r   abcr   r   r   r   r   r(   r-   r,   r   r4   r   r1   r1   r1   r2   <module>   s$    , ;