o
    ¯bÅ#  ã                   @   s|   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 G dd„ dƒZ
dd	„ ZG d
d„ dƒZG dd„ dƒZG dd„ dƒZdS )zÚ
This module implements memory BIO based TLS support.  It is the preferred
implementation and will be used whenever pyOpenSSL 0.10 or newer is installed
(whenever L{twisted.protocols.tls} is importable).

@since: 11.1
é    )ÚdirectlyProvides)ÚFileDescriptor)ÚISSLTransport)ÚTLSMemoryBIOFactoryÚTLSMemoryBIOProtocolc                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )Ú
_BypassTLSa  
    L{_BypassTLS} is used as the transport object for the TLS protocol object
    used to implement C{startTLS}.  Its methods skip any TLS logic which
    C{startTLS} enables.

    @ivar _base: A transport class L{_BypassTLS} has been mixed in with to which
        methods will be forwarded.  This class is only responsible for sending
        bytes over the connection, not doing TLS.

    @ivar _connection: A L{Connection} which TLS has been started on which will
        be proxied to by this object.  Any method which has its behavior
        altered after C{startTLS} will be skipped in favor of the base class's
        implementation.  This allows the TLS protocol object to have direct
        access to the transport, necessary to actually implement TLS.
    c                 C   s   || _ || _d S )N)Ú_baseÚ_connection)ÚselfÚbaseÚ
connection© r   ú:/usr/lib/python3/dist-packages/twisted/internet/_newtls.pyÚ__init__&   s   
z_BypassTLS.__init__c                 C   s   t | j|ƒS )zÈ
        Forward any extra attribute access to the original transport object.
        For example, this exposes C{getHost}, the behavior of which does not
        change after TLS is enabled.
        )Úgetattrr	   )r
   Únamer   r   r   Ú__getattr__*   s   z_BypassTLS.__getattr__c                 C   ó   | j  | j|¡S )z>
        Write some bytes directly to the connection.
        )r   Úwriter	   )r
   Údatar   r   r   r   2   ó   z_BypassTLS.writec                 C   r   )z@
        Write a some bytes directly to the connection.
        )r   ÚwriteSequencer	   ©r
   Úiovecr   r   r   r   8   r   z_BypassTLS.writeSequencec                 O   s   | j j| jg|¢R i |¤ŽS )z2
        Close the underlying connection.
        )r   ÚloseConnectionr	   )r
   ÚargsÚkwargsr   r   r   r   >   s   z_BypassTLS.loseConnectionc                 C   s   | j  | j||¡S )zE
        Register a producer with the underlying connection.
        )r   ÚregisterProducerr	   ©r
   ÚproducerÚ	streamingr   r   r   r   D   ó   z_BypassTLS.registerProducerc                 C   s   | j  | j¡S )zG
        Unregister a producer with the underlying connection.
        )r   ÚunregisterProducerr	   ©r
   r   r   r   r"   J   s   z_BypassTLS.unregisterProducerN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r"   r   r   r   r   r      s    r   c           	      C   s¦   |r| j }n| j  }d\}}| jdur| j| j}}|  ¡  t||dƒ}t|| jdƒ}|| _|j| _|j| _t	| t
ƒ d| _| j t|| ƒ¡ |rQ|  ||¡ dS dS )a`  
    Add a layer of SSL to a transport.

    @param transport: The transport which will be modified.  This can either by
        a L{FileDescriptor<twisted.internet.abstract.FileDescriptor>} or a
        L{FileHandle<twisted.internet.iocpreactor.abstract.FileHandle>}.  The
        actual requirements of this instance are that it have:

          - a C{_tlsClientDefault} attribute indicating whether the transport is
            a client (C{True}) or a server (C{False})
          - a settable C{TLS} attribute which can be used to mark the fact
            that SSL has been started
          - settable C{getHandle} and C{getPeerCertificate} attributes so
            these L{ISSLTransport} methods can be added to it
          - a C{protocol} attribute referring to the L{IProtocol} currently
            connected to the transport, which can also be set to a new
            L{IProtocol} for the transport to deliver data to

    @param contextFactory: An SSL context factory defining SSL parameters for
        the new SSL layer.
    @type contextFactory: L{twisted.internet.interfaces.IOpenSSLContextFactory}

    @param normal: A flag indicating whether SSL will go in the same direction
        as the underlying transport goes.  That is, if the SSL client will be
        the underlying client and the SSL server will be the underlying server.
        C{True} means it is the same, C{False} means they are switched.
    @type normal: L{bool}

    @param bypass: A transport base class to call methods on to bypass the new
        SSL layer (so that the SSL layer itself can send its bytes).
    @type bypass: L{type}
    )NNNFT)Ú_tlsClientDefaultr   ÚstreamingProducerr"   r   r   ÚprotocolÚ	getHandleÚgetPeerCertificater   r   ÚTLSÚmakeConnectionr   r   )	Ú	transportÚcontextFactoryÚnormalÚbypassÚclientr   r    Ú
tlsFactoryÚtlsProtocolr   r   r   ÚstartTLSQ   s$   %

ÿr6   c                   @   sF   e Zd ZdZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚConnectionMixinaN  
    A mixin for L{twisted.internet.abstract.FileDescriptor} which adds an
    L{ITLSTransport} implementation.

    @ivar TLS: A flag indicating whether TLS is currently in use on this
        transport.  This is not a good way for applications to check for TLS,
        instead use L{twisted.internet.interfaces.ISSLTransport}.
    FTc                 C   s   t | ||tƒ dS )z1
        @see: L{ITLSTransport.startTLS}
        N)r6   r   )r
   Úctxr1   r   r   r   r6   ¤   r!   zConnectionMixin.startTLSc                 C   ó0   | j r| jr| j |¡ dS dS t | |¡ dS )z©
        Write some bytes to this connection, passing them through a TLS layer if
        necessary, or discarding them if the connection has already been lost.
        N)r-   Ú	connectedr*   r   r   )r
   Úbytesr   r   r   r   ª   s
   ÿzConnectionMixin.writec                 C   r9   )zÇ
        Write some bytes to this connection, scatter/gather-style, passing them
        through a TLS layer if necessary, or discarding them if the connection
        has already been lost.
        N)r-   r:   r*   r   r   r   r   r   r   r   µ   s
   ÿzConnectionMixin.writeSequencec                 C   s6   | j r| jr| js| j ¡  dS dS dS t | ¡ dS )z„
        Close this connection after writing all pending data.

        If TLS has been negotiated, perform a TLS shutdown.
        N)r-   r:   Údisconnectingr*   r   r   r#   r   r   r   r   Á   s
   ÿzConnectionMixin.loseConnectionc                 C   s*   | j r| j ||¡ dS t | ||¡ dS )zc
        Register a producer.

        If TLS is enabled, the TLS connection handles this.
        N)r-   r*   r   r   r   r   r   r   r   Í   s   z ConnectionMixin.registerProducerc                 C   s"   | j r
| j ¡  dS t | ¡ dS )ze
        Unregister a producer.

        If TLS is enabled, the TLS connection handles this.
        N)r-   r*   r"   r   r#   r   r   r   r"   Ü   s   z"ConnectionMixin.unregisterProducerN)T)r$   r%   r&   r'   r-   r6   r   r   r   r   r"   r   r   r   r   r7   ˜   s    	
r7   c                   @   ó   e Zd ZdZdZdS )ÚClientMixina=  
    A mixin for L{twisted.internet.tcp.Client} which just marks it as a client
    for the purposes of the default TLS handshake.

    @ivar _tlsClientDefault: Always C{True}, indicating that this is a client
        connection, and by default when TLS is negotiated this class will act as
        a TLS client.
    TN©r$   r%   r&   r'   r(   r   r   r   r   r>   è   ó    	r>   c                   @   r=   )ÚServerMixina>  
    A mixin for L{twisted.internet.tcp.Server} which just marks it as a server
    for the purposes of the default TLS handshake.

    @ivar _tlsClientDefault: Always C{False}, indicating that this is a server
        connection, and by default when TLS is negotiated this class will act as
        a TLS server.
    FNr?   r   r   r   r   rA   õ   r@   rA   N)r'   Úzope.interfacer   Útwisted.internet.abstractr   Útwisted.internet.interfacesr   Útwisted.protocols.tlsr   r   r   r6   r7   r>   rA   r   r   r   r   Ú<module>   s   	<GP