%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/twisted/protocols/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/twisted/protocols/__pycache__/tls.cpython-312.pyc

�

Ϫ�f����dZddlmZddlmZmZmZmZddlm	Z	m
Z
mZddlm
Z
mZmZmZmZddlmZddlmZddlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"dd	l#m$Z$dd
l%m&Z&ddl'm(Z(m)Z)ddl*m+Z+e
e�Gd
�d��Z,dd�Z-e
e!ee"�Gd�de(��Z.e
ee�Gd�d��Z/Gd�d�Z0dd�Z1Gd�de.�Z2Gd�de)�Z3y)a�
Implementation of a TLS transport (L{ISSLTransport}) as an
L{IProtocol<twisted.internet.interfaces.IProtocol>} layered on top of any
L{ITransport<twisted.internet.interfaces.ITransport>} implementation, based on
U{OpenSSL<http://www.openssl.org>}'s memory BIO features.

L{TLSMemoryBIOFactory} is a L{WrappingFactory} which wraps protocols created by
the factory it wraps with L{TLSMemoryBIOProtocol}.  L{TLSMemoryBIOProtocol}
intercedes between the underlying transport and the wrapped protocol to
implement SSL and TLS.  Typical usage of this module looks like this::

    from twisted.protocols.tls import TLSMemoryBIOFactory
    from twisted.internet.protocol import ServerFactory
    from twisted.internet.ssl import PrivateCertificate
    from twisted.internet import reactor

    from someapplication import ApplicationProtocol

    serverFactory = ServerFactory()
    serverFactory.protocol = ApplicationProtocol
    certificate = PrivateCertificate.loadPEM(certPEMData)
    contextFactory = certificate.options()
    tlsFactory = TLSMemoryBIOFactory(contextFactory, False, serverFactory)
    reactor.listenTCP(12345, tlsFactory)
    reactor.run()

This API offers somewhat more flexibility than
L{twisted.internet.interfaces.IReactorSSL}; for example, a
L{TLSMemoryBIOProtocol} instance can use another instance of
L{TLSMemoryBIOProtocol} as its transport, yielding TLS over TLS - useful to
implement onion routing.  It can also be used to run TLS over unusual
transports, such as UNIX sockets and stdio.
�)�annotations)�Callable�Iterable�Optional�cast)�directlyProvides�implementer�
providedBy)�
Connection�Error�SysCallError�
WantReadError�ZeroReturnError)�_PullToPush)�_setAcceptableProtocols)�IDelayedCall�IHandshakeListener�ILoggingContext�INegotiated�IOpenSSLClientConnectionCreator�IOpenSSLServerConnectionCreator�	IProtocol�IProtocolNegotiationFactory�
IPushProducer�IReactorTime�
ISystemHandle�
ITransport)�CONNECTION_LOST)�Protocol)�ProtocolWrapper�WrappingFactory)�Failurec�,�eZdZdZdZd�Zd�Zd�Zd�Zy)�_ProducerMembranea
    Stand-in for producer registered with a L{TLSMemoryBIOProtocol} transport.

    Ensures that producer pause/resume events from the undelying transport are
    coordinated with pause/resume events from the TLS layer.

    @ivar _producer: The application-layer producer.
    Fc��||_y�N)�	_producer)�self�producers  �7/usr/lib/python3/dist-packages/twisted/protocols/tls.py�__init__z_ProducerMembrane.__init__Rs	��!���c�`�|jryd|_|jj�y)zP
        C{pauseProducing} the underlying producer, if it's not paused.
        NT)�_producerPausedr'�pauseProducing�r(s r*r/z _ProducerMembrane.pauseProducingUs(������#������%�%�'r,c�`�|jsyd|_|jj�y)zM
        C{resumeProducing} the underlying producer, if it's paused.
        NF)r.r'�resumeProducingr0s r*r2z!_ProducerMembrane.resumeProducing^s(���#�#��$������&�&�(r,c�8�|jj�y)z�
        C{stopProducing} the underlying producer.

        There is only a single source for this event, so it's simply passed
        on.
        N)r'�
stopProducingr0s r*r4z_ProducerMembrane.stopProducinggs��	
���$�$�&r,N)	�__name__�
__module__�__qualname__�__doc__r.r+r/r2r4�r,r*r$r$Es"����O�"�(�)�'r,r$c��t|t�r|j\}}n|jd}|d\}}}|j�j	d�S)zD
    Does the given OpenSSL.SSL.Error represent an end-of-file?
    r���zunexpected eof)�
isinstancer
�args�casefold�
startswith)�exceptionObject�_�reasonString�
errorQueues    r*�_representsEOFrDqsX��
�/�<�0�)�.�.���<�$�)�)�!�,�
�'��^���1�l�� � �"�-�-�.>�?�?r,c��eZdZdZdZdZdZdZdZdd�Z	d�Z
d�Zd�Zd�Z
d	�Zd
�Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zed��Zd�Zd�Zy)�TLSMemoryBIOProtocola�
    L{TLSMemoryBIOProtocol} is a protocol wrapper which uses OpenSSL via a
    memory BIO to encrypt bytes written to it before sending them on to the
    underlying transport and decrypts bytes received from the underlying
    transport before delivering them to the wrapped protocol.

    In addition to producer events from the underlying transport, the need to
    wait for reads before a write can proceed means the L{TLSMemoryBIOProtocol}
    may also want to pause a producer.  Pause/resume events are therefore
    merged using the L{_ProducerMembrane} wrapper.  Non-streaming (pull)
    producers are supported by wrapping them with L{_PullToPush}.

    Because TLS may need to wait for reads before writing, some writes may be
    buffered until a read occurs.

    @ivar _tlsConnection: The L{OpenSSL.SSL.Connection} instance which is
        encrypted and decrypting this connection.

    @ivar _lostTLSConnection: A flag indicating whether connection loss has
        already been dealt with (C{True}) or not (C{False}).  TLS disconnection
        is distinct from the underlying connection being lost.

    @ivar _appSendBuffer: application-level (cleartext) data that is waiting to
        be transferred to the TLS buffer, but can't be because the TLS
        connection is handshaking.
    @type _appSendBuffer: L{list} of L{bytes}

    @ivar _connectWrapped: A flag indicating whether or not to call
        C{makeConnection} on the wrapped protocol.  This is for the reactor's
        L{twisted.internet.interfaces.ITLSTransport.startTLS} implementation,
        since it has a protocol which it has already called C{makeConnection}
        on, and which has no interest in a new transport.  See #3821.

    @ivar _handshakeDone: A flag indicating whether or not the handshake is
        known to have completed successfully (C{True}) or not (C{False}).  This
        is used to control error reporting behavior.  If the handshake has not
        completed, the underlying L{OpenSSL.SSL.Error} will be passed to the
        application's C{connectionLost} method.  If it has completed, any
        unexpected L{OpenSSL.SSL.Error} will be turned into a
        L{ConnectionLost}.  This is weird; however, it is simply an attempt at
        a faithful re-implementation of the behavior provided by
        L{twisted.internet.ssl}.

    @ivar _reason: If an unexpected L{OpenSSL.SSL.Error} occurs which causes
        the connection to be lost, it is saved here.  If appropriate, this may
        be used as the reason passed to the application protocol's
        C{connectionLost} method.

    @ivar _producer: The current producer registered via C{registerProducer},
        or L{None} if no producer has been registered or a previous one was
        unregistered.

    @ivar _aborted: C{abortConnection} has been called.  No further data will
        be received to the wrapped protocol's C{dataReceived}.
    @type _aborted: L{bool}
    NFc�@�tj|||�||_yr&)r r+�_connectWrapped)r(�factory�wrappedProtocolrHs    r*r+zTLSMemoryBIOProtocol.__init__�s��� � ��w��@�.��r,c��|jS)ai
        Return the L{OpenSSL.SSL.Connection} object being used to encrypt and
        decrypt this connection.

        This is done for the benefit of L{twisted.internet.ssl.Certificate}'s
        C{peerFromTransport} and C{hostFromTransport} methods only.  A
        different system handle may be returned by future versions of this
        method.
        )�_tlsConnectionr0s r*�	getHandlezTLSMemoryBIOProtocol.getHandle�s���"�"�"r,c�P�|jj|�|_g|_t	|�D]}t||��t
j||�|jj|�|jrtj||�|j�y)z�
        Connect this wrapper to the given transport and initialize the
        necessary L{OpenSSL.SSL.Connection} with a memory BIO.
        N)rI�_createConnectionrL�_appSendBufferr
rr�makeConnection�registerProtocolrHr �_checkHandshakeStatus)r(�	transport�	interfaces   r*rQz#TLSMemoryBIOProtocol.makeConnection�s���
#�l�l�<�<�T�B��� ���$�I�.�	.�I��T�9�-�	.�
	����i�0����%�%�d�+����
�*�*�4��;�
	
�"�"�$r,c�Z�|jry	|jj�d|_t	j
|j�r|jj�yy#t$r|j�Yyt$r|jt��YywxYw)a(
        Ask OpenSSL to proceed with a handshake in progress.

        Initially, this just sends the ClientHello; after some bytes have been
        stuffed in to the C{Connection} object by C{dataReceived}, it will then
        respond to any C{Certificate} or C{KeyExchange} messages.
        NT)
�_abortedrL�do_handshake�_handshakeDonerr
rJ�handshakeCompletedr�
_flushSendBIOr�_tlsShutdownFinishedr"r0s r*rSz*TLSMemoryBIOProtocol._checkHandshakeStatus�s����=�=��		:����,�,�.�#'�D��!�,�,�T�-A�-A�B��$�$�7�7�9�C��
�	!���� ��	1��%�%�g�i�0�	1�s�A+�+B*�!B*�)B*c��	|jjd�}|jj|�y#t$rYywxYw)zh
        Read any bytes out of the send BIO and write them to the underlying
        transport.
        ��N)rL�bio_readrT�writer�r(�bytess  r*r[z"TLSMemoryBIOProtocol._flushSendBIOsE��
	(��'�'�0�0��7�E�

�N�N� � ��'��	�	��	�s�9�	A�Ac��|jsL	|jjd�}|jst	j
||�	|js�L|j�y#t$rY�t$r$|j�|jd�Y�Tt$rt�}|j|�Y�ywxYw)ae
        Try to receive any application-level bytes which are now available
        because of a previous write into the receive BIO.  This will take
        care of delivering any application-level bytes which are received to
        the protocol, as well as handling of the various exceptions which
        can come from trying to get such bytes.
        r^N)
�_lostTLSConnectionrL�recvrWr �dataReceivedrr�_shutdownTLSr\rr"r[)r(rb�failures   r*�_flushReceiveBIOz%TLSMemoryBIOProtocol._flushReceiveBIOs����)�)�
>��+�+�0�0��7��(�}�}�#�0�0��u�=�/�)�)�8	
�����3!�
��"�
0��!�!�#��)�)�$�/��
3�
"�)���)�)�'�2�

3�s�A*�*	C�5)C� #C�Cc���|jj|�|js|j�|jsy|jr|j�|j
�y)z�
        Deliver any received bytes to the receive BIO and then read and deliver
        to the application any application-level data which becomes available
        as a result of this.
        N)rL�	bio_writerYrSrP�_unbufferPendingWritesriras  r*rfz!TLSMemoryBIOProtocol.dataReceived8sb��	
���%�%�e�,��"�"��&�&�(��&�&������'�'�)�
	
���r,c��	|jj�}|j�|r|jj�yy#t$rd}Y�;wxYw)zQ
        Initiate, or reply to, the shutdown handshake of the TLS layer.
        FN)rL�shutdownrr[rT�loseConnection)r(�shutdownSuccesss  r*rgz!TLSMemoryBIOProtocol._shutdownTLSUs[��	$�"�1�1�:�:�<�O�	
�����

�N�N�)�)�+����	$�
$�O�	$�s�A
�
A�Ac���|�$t|j�rtt�}|j�||_d|_|j
�|jj�y)a�
        Called when TLS connection has gone away; tell underlying transport to
        disconnect.

        @param reason: a L{Failure} whose value is an L{Exception} if we want to
            report that failure through to the wrapped protocol's
            C{connectionLost}, or L{None} if the C{reason} that
            C{connectionLost} should receive should be coming from the
            underlying transport.
        @type reason: L{Failure} or L{None}
        NT)	rD�valuer"r�_reasonrdr[rTro�r(�reasons  r*r\z)TLSMemoryBIOProtocol._tlsShutdownFinishedis[�����f�l�l�+� ��1���<�<��!�D�L�"&���	
����	
���%�%�'r,c���|js1|jj�|j�d|_|jxs|}d|_d|_t
j||�d|_y)z�
        Handle the possible repetition of calls to this method (due to either
        the underlying transport going away or due to an error at the TLS
        layer) and make sure the base implementation only gets invoked once.
        TNF)rdrL�bio_shutdownrirs�	connectedr �connectionLostrts  r*ryz#TLSMemoryBIOProtocol.connectionLost�sk���&�&�
���,�,�.��!�!�#�&*�D�#����'����������&�&�t�V�4�#��r,c���|js|jsy|js|js|j	�d|_|js|j
�|j
�yyy)zM
        Send a TLS close alert and close the underlying connection.
        NT)�
disconnectingrxrYrP�abortConnectionr'rgr0s r*roz#TLSMemoryBIOProtocol.loseConnection�sc�����T�^�^���"�"�4�+>�+>�� � �"�!����"�"�t�~�~�'=�����(>�"r,c�t�d|_d|_|j�|jj	�y)z�
        Tear down TLS state so that if the connection is aborted mid-handshake
        we don't deliver any further data from the application.
        TN)rWr{rgrTr|r0s r*r|z$TLSMemoryBIOProtocol.abortConnection�s/��
��
�!����������&�&�(r,c�2�||_|j�y)a,
        Abort the connection during connection setup, giving a reason that
        certificate verification failed.

        @param reason: The reason that the verification failed; reported to the
            application protocol's C{connectionLost} method.
        @type reason: L{Failure}
        N)rsr|rts  r*�failVerificationz%TLSMemoryBIOProtocol.failVerification�s��������r,c�X�|jr
|j�y|j|�y)z�
        Process the given application bytes and send any resulting TLS traffic
        which arrives in the send BIO.

        If C{loseConnection} was called, subsequent calls to C{write} will
        drop the bytes on the floor.
        N)r{r'�_writeras  r*r`zTLSMemoryBIOProtocol.write�s%�����$�.�.�"8�����E�r,c��|jj|�|j�|jj�yy)z�
        Put the given octets into L{TLSMemoryBIOProtocol._appSendBuffer}, and
        tell any listening producer that it should pause because we are now
        buffering.
        N)rP�appendr'r/)r(�octetss  r*�_bufferedWritez#TLSMemoryBIOProtocol._bufferedWrite�s7��	
���"�"�6�*��>�>�%��N�N�)�)�+�&r,c��|jgc}|_|D]}|j|��|jry|j�|jj�y|jr|j�yy)zY
        Un-buffer all waiting writes in L{TLSMemoryBIOProtocol._appSendBuffer}.
        N)rPr�r'r2r{rg)r(�
pendingWrites�	eachWrites   r*rlz+TLSMemoryBIOProtocol._unbufferPendingWrites�s��.2�-@�-@�"�*�
�t�*�&�	#�I��K�K�	�"�	#����
��>�>�%�
�N�N�*�*�,�����
����	r,c�`�|jryd}d}|t|�krI||||z}	|jj|�}||z
}|j	�|t|�kr�Hyy#t
$r|j
||d�Yyt$r|jt��YywxYw)a^
        Process the given application bytes and send any resulting TLS traffic
        which arrives in the send BIO.

        This may be called by C{dataReceived} with bytes that were buffered
        before C{loseConnection} was called, which is why this function
        doesn't check for disconnection but accepts the bytes regardless.
        Ni@r)
rd�lenrL�sendr[rr�rr\r")r(rb�
bufferSize�alreadySent�toSend�sents      r*r�zTLSMemoryBIOProtocol._write�s����"�"���
����C��J�&��;��z�)A�B�F�
%��*�*�/�/��7���t�#���"�"�$�+�C��J�&��!�
��#�#�E�+�,�$7�8���
��)�)�'�)�4��
�s�A*�*B-�	!B-�,B-c�D�|jdj|��y)z}
        Write a sequence of application bytes by joining them into one string
        and passing them to L{write}.
        r,N)r`�join)r(�iovecs  r*�
writeSequencez"TLSMemoryBIOProtocol.writeSequence"s��
	
�
�
�3�8�8�E�?�#r,c�6�|jj�Sr&)rL�get_peer_certificater0s r*�getPeerCertificatez'TLSMemoryBIOProtocol.getPeerCertificate)s���"�"�7�7�9�9r,c���d}	|jj�}|dvr|S	|jj	�}|dk7r|Sy#ttf$rY�:wxYw#ttf$rY�.wxYw)z9
        @see: L{INegotiated.negotiatedProtocol}
        N)r,Nr,)rL�get_alpn_proto_negotiated�NotImplementedError�AttributeError�get_next_proto_negotiated)r(�protocolNames  r*�negotiatedProtocolz'TLSMemoryBIOProtocol.negotiatedProtocol,s���
��	��.�.�H�H�J�L��{�*���	��.�.�H�H�J�L��3������$�^�4�	��	��$�^�4�	��	�s"�A�A�A�A�A.�-A.c���|jr|j�y|st||�x}}t|�}|jj|d�||_|sj�yy)NT)rdr4rr$rT�registerProducerr'�startStreaming)r(r)�	streaming�streamingProducers    r*r�z%TLSMemoryBIOProtocol.registerProducerGsl���"�"��"�"�$���+6�x��+F�F�H�(�$�X�.�����'�'��$�7�!�����,�,�.�r,c�R�|j�yt|jjt�r$|jjj�d|_d|_|j
j
�|jr|js|j�yyy)NF)
r'r<r�
stopStreamingr.rT�unregisterProducerr{rPrgr0s r*r�z'TLSMemoryBIOProtocol.unregisterProducerZs���>�>�!���d�n�n�.�.��<��N�N�$�$�2�2�4����$������)�)�+����d�&9�&9�����':�r,�T) r5r6r7r8rsrYrdr'rWr+rMrQrSr[rirfrgr\ryror|rr`r�rlr�r�r��propertyr�r�r�r9r,r*rFrF~s���7�r�G��N����I��H�/�
#�%�4:�0(�(�T �:,�((�B#�( �$)�
�
�,� �4'%�R$�:�����4/�&
 r,rFc�(�eZdZdZd�Zd�Zd�Zd�Zy)�"_ContextFactoryToConnectionFactorya
    Adapter wrapping a L{twisted.internet.interfaces.IOpenSSLContextFactory}
    into a L{IOpenSSLClientConnectionCreator} or
    L{IOpenSSLServerConnectionCreator}.

    See U{https://twistedmatrix.com/trac/ticket/7215} for work that should make
    this unnecessary.
    c�2�|j�||_y)a(
        Construct a L{_ContextFactoryToConnectionFactory} with a
        L{twisted.internet.interfaces.IOpenSSLContextFactory}.

        Immediately call C{getContext} on C{oldStyleContextFactory} in order to
        force advance parameter checking, since old-style context factories
        don't actually check that their arguments to L{OpenSSL} are correct.

        @param oldStyleContextFactory: A factory that can produce contexts.
        @type oldStyleContextFactory:
            L{twisted.internet.interfaces.IOpenSSLContextFactory}
        N)�
getContext�_oldStyleContextFactory)r(�oldStyleContextFactorys  r*r+z+_ContextFactoryToConnectionFactory.__init__us��	�)�)�+�'=��$r,c�N�|jj�}t|d�S)z�
        Create an L{OpenSSL.SSL.Connection} object.

        @param protocol: The protocol initiating a TLS connection.
        @type protocol: L{TLSMemoryBIOProtocol}

        @return: a connection
        @rtype: L{OpenSSL.SSL.Connection}
        N)r�r�r)r(�protocol�contexts   r*�_connectionForTLSz4_ContextFactoryToConnectionFactory._connectionForTLS�s%���.�.�9�9�;���'�4�(�(r,c�$�|j|�S)a�
        Construct an OpenSSL server connection from the wrapped old-style
        context factory.

        @note: Since old-style context factories don't distinguish between
            clients and servers, this is exactly the same as
            L{_ContextFactoryToConnectionFactory.clientConnectionForTLS}.

        @param protocol: The protocol initiating a TLS connection.
        @type protocol: L{TLSMemoryBIOProtocol}

        @return: a connection
        @rtype: L{OpenSSL.SSL.Connection}
        �r��r(r�s  r*�serverConnectionForTLSz9_ContextFactoryToConnectionFactory.serverConnectionForTLS�����%�%�h�/�/r,c�$�|j|�S)a�
        Construct an OpenSSL server connection from the wrapped old-style
        context factory.

        @note: Since old-style context factories don't distinguish between
            clients and servers, this is exactly the same as
            L{_ContextFactoryToConnectionFactory.serverConnectionForTLS}.

        @param protocol: The protocol initiating a TLS connection.
        @type protocol: L{TLSMemoryBIOProtocol}

        @return: a connection
        @rtype: L{OpenSSL.SSL.Connection}
        r�r�s  r*�clientConnectionForTLSz9_ContextFactoryToConnectionFactory.clientConnectionForTLS�r�r,N)r5r6r7r8r+r�r�r�r9r,r*r�r�js���>� )�0�"0r,r�c�4�eZdZdZdZdd�Zd	d�Zd
d�Zd
d�Zy)�_AggregateSmallWritesam
    Aggregate small writes so they get written in large batches.

    If this is used as part of a transport, the transport needs to call
    ``flush()`` immediately when ``loseConnection()`` is called, otherwise any
    buffered writes will never get written.

    @cvar MAX_BUFFER_SIZE: The maximum amount of bytes to buffer before writing
        them out.
    i�c�^�||_||_g|_|j|_d|_yr&)r��_clock�_buffer�MAX_BUFFER_SIZE�_bufferLeft�
_scheduled)r(r`�clocks   r*r+z_AggregateSmallWrites.__init__�s,��������$&����/�/���26��r,c�&�|jj|�|xjt|�zc_|jdkr|j	�y|j
ry|jjd|j�|_y)z�
        Buffer the data, or write it immediately if we've accumulated enough to
        make it worth it.

        Accumulating too much data can result in higher memory usage.
        rN)	r�r�r�r��flushr�r��	callLater�_scheduledFlush)r(�datas  r*r`z_AggregateSmallWrites.write�ss��	
�����D�!����C��I�%�����a��
�J�J�L���?�?�
��+�+�/�/��4�3G�3G�H��r,c�2�d|_|j�y)z!Called in next reactor iteration.N)r�r�r0s r*r�z%_AggregateSmallWrites._scheduledFlush�s������
�
�r,c��|jrK|j|_|jdj	|j��|jdd�=yy)zFlush any buffered writes.r,N)r�r�r�r�r�r0s r*r�z_AggregateSmallWrites.flush�s@���<�<�#�3�3�D���K�K�������.�/����Q��r,N)r`zCallable[[bytes], object]r�r)r�rb�return�None�r�r�)	r5r6r7r8r�r+r`r�r�r9r,r*r�r��s#��	��O�7�I�2�
 r,r�c�.�ddlm}tt|�S)z�
    Return the default reactor.

    This is a function so it can be monkey-patched in tests, specifically
    L{twisted.web.test.test_agent}.
    r��reactor)�twisted.internetr�rrr�s r*�_get_default_clockr��s��)���g�&�&r,c�F��eZdZdZ	d					d�fd�
Zdd�Zd�fd�Z�xZS)	�BufferingTLSTransporta�
    A TLS transport implemented by wrapping buffering around a
    L{TLSMemoryBIOProtocol}.

    Doing many small writes directly to a L{OpenSSL.SSL.Connection}, as
    implemented in L{TLSMemoryBIOProtocol}, can add significant CPU and
    bandwidth overhead.  Thus, even when writing is possible, small writes will
    get aggregated and written as a single write at the next reactor iteration.
    c���t�|�|||�t�|�}t||j�|_|j
j|_yr&)�superr+r`r�r��_aggregator)r(rIrJrH�actual_write�	__class__s     �r*r+zBufferingTLSTransport.__init__sG���	����/�?�C��w�}��0��w�~�~�N����%�%�+�+��
r,c�X�|jjdj|��y)Nr,)r�r`r�)r(�sequences  r*r�z#BufferingTLSTransport.writeSequences�������s�x�x��1�2r,c�V��|jj�t�|�
�yr&)r�r�r�ro)r(r�s �r*roz$BufferingTLSTransport.loseConnections �������� �
��� r,r�)rI�TLSMemoryBIOFactoryrJrrH�bool)r�zIterable[bytes]r�r�r�)r5r6r7r8r+r�ro�
__classcell__)r�s@r*r�r��s=����*!%�	,�$�,�#�,��	,�3�!�!r,r�c�4�eZdZdZeZdZ	dd�Zd�Zd�Z	d�Z
y)	r�a�
    L{TLSMemoryBIOFactory} adds TLS to connections.

    @ivar _creatorInterface: the interface which L{_connectionCreator} is
        expected to implement.
    @type _creatorInterface: L{zope.interface.interfaces.IInterface}

    @ivar _connectionCreator: a callable which creates an OpenSSL Connection
        object.
    @type _connectionCreator: 1-argument callable taking
        L{TLSMemoryBIOProtocol} and returning L{OpenSSL.SSL.Connection}.
    FNc���tj||�|rt}nt}||_|j|�st
|�}||_|�
t�}||_	y)a2
        Create a L{TLSMemoryBIOFactory}.

        @param contextFactory: Configuration parameters used to create an
            OpenSSL connection.  In order of preference, what you should pass
            here should be:

                1. L{twisted.internet.ssl.CertificateOptions} (if you're
                   writing a server) or the result of
                   L{twisted.internet.ssl.optionsForClientTLS} (if you're
                   writing a client).  If you want security you should really
                   use one of these.

                2. If you really want to implement something yourself, supply a
                   provider of L{IOpenSSLClientConnectionCreator} or
                   L{IOpenSSLServerConnectionCreator}.

                3. If you really have to, supply a
                   L{twisted.internet.ssl.ContextFactory}.  This will likely be
                   deprecated at some point so please upgrade to the new
                   interfaces.

        @type contextFactory: L{IOpenSSLClientConnectionCreator} or
            L{IOpenSSLServerConnectionCreator}, or, for compatibility with
            older code, anything implementing
            L{twisted.internet.interfaces.IOpenSSLContextFactory}.  See
            U{https://twistedmatrix.com/trac/ticket/7215} for information on
            the upcoming deprecation of passing a
            L{twisted.internet.ssl.ContextFactory} here.

        @param isClient: Is this a factory for TLS client connections; in other
            words, those that will send a C{ClientHello} greeting?  L{True} if
            so, L{False} otherwise.  This flag determines what interface is
            expected of C{contextFactory}.  If L{True}, C{contextFactory}
            should provide L{IOpenSSLClientConnectionCreator}; otherwise it
            should provide L{IOpenSSLServerConnectionCreator}.
        @type isClient: L{bool}

        @param wrappedFactory: A factory which will create the
            application-level protocol.
        @type wrappedFactory: L{twisted.internet.interfaces.IProtocolFactory}
        N)
r!r+rr�_creatorInterfacer
r��_connectionCreatorr�r�)r(�contextFactory�isClient�wrappedFactoryr��creatorInterfaces      r*r+zTLSMemoryBIOFactory.__init__6s`��b	� � ��~�6��>��>��!1����*�*�>�:�?��O�N�"0����=�&�(�E���r,c��tj|j�r|jj�}n |jjj
}|�d�S)z�
        Annotate the wrapped factory's log prefix with some text indicating TLS
        is in use.

        @rtype: C{str}
        z (TLS))rr
r��	logPrefixr�r5)r(r�s  r*r�zTLSMemoryBIOFactory.logPrefixusP���%�%�d�&9�&9�:��+�+�5�5�7�I��+�+�5�5�>�>�I���F�#�#r,c��tj|j�r6|jj�}|j	�}t||�y)a8
        Applies ALPN/NPN protocol neogitation to the connection, if the factory
        supports it.

        @param connection: The OpenSSL connection object to have ALPN/NPN added
            to it.
        @type connection: L{OpenSSL.SSL.Connection}

        @return: Nothing
        @rtype: L{None}
        N)rr
r��acceptableProtocols�get_contextr)r(�
connection�	protocolsr�s    r*�_applyProtocolNegotiationz-TLSMemoryBIOFactory._applyProtocolNegotiation�sG��'�1�1�$�2E�2E�F��+�+�?�?�A�I� �,�,�.�G�#�G�Y�7�r,c��|j}|jtur4|j|�}|j	|�|j�|S|j
|�}|j	|�|j�|S)a8
        Create an OpenSSL connection and set it up good.

        @param tlsProtocol: The protocol which is establishing the connection.
        @type tlsProtocol: L{TLSMemoryBIOProtocol}

        @return: an OpenSSL connection object for C{tlsProtocol} to use
        @rtype: L{OpenSSL.SSL.Connection}
        )r�r�rr�r��set_connect_stater��set_accept_state)r(�tlsProtocol�connectionCreatorr�s    r*rOz%TLSMemoryBIOFactory._createConnection�s���!�3�3���!�!�%D�D�*�A�A�+�N�J��*�*�:�6��(�(�*�
��+�A�A�+�N�J��*�*�:�6��'�'�)��r,r&)r5r6r7r8r�r��noisyr+r�r�rOr9r,r*r�r�$s-���%�H��E��=�~$��&r,r�N)r@rr�r�)r�r)4r8�
__future__r�typingrrrr�zope.interfacerr	r
�OpenSSL.SSLrrr
rr�"twisted.internet._producer_helpersr�twisted.internet._sslverifyr�twisted.internet.interfacesrrrrrrrrrrrr�twisted.internet.mainr�twisted.internet.protocolr�twisted.protocols.policiesr r!�twisted.python.failurer"r$rDrFr�r�r�r�r�r9r,r*�<module>r�s���
 �D#�5�5�D�D�W�W�:�?�
�
�
�
�2�.�G�*�
�]��('�('��('�V
@�
�]�K��4�h �?�h �5�h �V
�
,�.M�N�G0�G0�O�G0�T8 �8 �v	'�%!�0�%!�PD�/�Dr,

Zerion Mini Shell 1.0