Spamworldpro Mini Shell
Spamworldpro


Server : Apache/2.4.52 (Ubuntu)
System : Linux webserver 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64
User : www-data ( 33)
PHP Version : 8.1.2-1ubuntu2.21
Disable Function : NONE
Directory :  /lib/python3/dist-packages/twisted/conch/ssh/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/twisted/conch/ssh/__pycache__/transport.cpython-310.pyc
o

5�@gR:�@s~dZddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
ddlmZddl
mZddlmZddlmZddlmZmZmZdd	lmZmZmZdd
lmZddlmZm Z m!Z!ddl"m#Z#m$Z$m%Z%m&Z&m'Z'dd
l(m)Z)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0m1Z1dd�Z2Gdd�de3�Z4Gdd�d�Z5dd�Z6Gdd�de*j7�Z8Gdd�de8�Z9Gdd�de8�Z:Gdd �d �Z;Gd!d"�d"�Z<Gd#d$�d$�Z=e�>d%�\Z?Z@d&ZAd'ZBd(ZCd)ZDd*ZEd+ZFd,ZGd-ZHd.ZId/ZJd0ZKd/ZLd1ZMd0ZNd2ZOd3ZPd&ZQd'ZRd(ZSd)ZTd*ZUd+ZVd,ZWd4ZXd5ZYd6ZZd7Z[d8Z\d9Z]d:Z^d;Z_iZ`eaeb��c��D]\ZdZeed�fd<��r-ed�fd=��s-ede`ee<�qd>e`v�s9d?e`v�r=egd@��dS)Az�
The lowest level SSH protocol.  This handles the key negotiation, the
encryption and the compression.  The transport layer is described in
RFC 4253.

Maintainer: Paul Swartz
�N)�md5�sha1�sha256�sha384�sha512)�Dict)�UnsupportedAlgorithm)�default_backend)�
serialization)�dh�ec�x25519)�Cipher�
algorithms�modes)�__version__)�_kex�address�keys)�MP�NS�ffs�getMP�getNS)�defer�protocol)�Logger)�	randbytes)�	iterbytes�
networkStringcCstt�|d��S)a*Make an SSH multiple-precision integer from big-endian L{bytes}.

    Used in ECDH key exchange.

    @type data: L{bytes}
    @param data: The input data, interpreted as a big-endian octet string.

    @rtype: L{bytes}
    @return: The given data encoded as an SSH multiple-precision integer.
    �big)r�int�
from_bytes)�data�r$�=/usr/lib/python3/dist-packages/twisted/conch/ssh/transport.py�_mpFromBytes'sr&c@seZdZdZdS)�
_MACParamsaP
    L{_MACParams} represents the parameters necessary to compute SSH MAC
    (Message Authenticate Codes).

    L{_MACParams} is a L{tuple} subclass to maintain compatibility with older
    versions of the code.  The elements of a L{_MACParams} are::

        0. The digest object used for the MAC
        1. The inner pad ("ipad") string
        2. The outer pad ("opad") string
        3. The size of the digest produced by the digest object

    L{_MACParams} is also an object lesson in why tuples are a bad type for
    public APIs.

    @ivar key: The HMAC key which will be used.
    N)�__name__�
__module__�__qualname__�__doc__r$r$r$r%r'5sr'c@seZdZdZejdejfejdejfej	dejfej	dejfej	dejfej
dejfej	dejfej	dejfej	dejfejdejfejdejfej
dejfddejfd�
Ze
eeeedd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)�
SSHCiphersa
    SSHCiphers represents all the encryption operations that need to occur
    to encrypt and authenticate the SSH connection.

    @cvar cipherMap: A dictionary mapping SSH encryption names to 3-tuples of
        (<cryptography.hazmat.primitives.interfaces.CipherAlgorithm>,
        <block size>, <cryptography.hazmat.primitives.interfaces.Mode>)
    @cvar macMap: A dictionary mapping SSH MAC names to hash modules.

    @ivar outCipType: the string type of the outgoing cipher.
    @ivar inCipType: the string type of the incoming cipher.
    @ivar outMACType: the string type of the incoming MAC.
    @ivar inMACType: the string type of the incoming MAC.
    @ivar encBlockSize: the block size of the outgoing cipher.
    @ivar decBlockSize: the block size of the incoming cipher.
    @ivar verifyDigestSize: the size of the incoming MAC.
    @ivar outMAC: a tuple of (<hash module>, <inner key>, <outer key>,
        <digest size>) representing the outgoing MAC.
    @ivar inMAc: see outMAC, but for the incoming MAC.
    ��� Nr)
�3des-cbc�blowfish-cbc�
aes256-cbc�
aes192-cbc�
aes128-cbc�cast128-cbc�
aes128-ctr�
aes192-ctr�
aes256-ctr�3des-ctr�blowfish-ctr�cast128-ctr�none)�
hmac-sha2-512�
hmac-sha2-384�
hmac-sha2-256�	hmac-sha1�hmac-md5r<cCs:||_||_||_||_d|_d|_d|_d|_d|_dS)Nr�N�rCr)	�
outCipType�	inCipType�
outMACType�	inMACType�encBlockSize�decBlockSize�verifyDigestSize�outMAC�inMAC)�self�outCip�inCip�outMac�inMacr$r$r%�__init__ws
zSSHCiphers.__init__cCs�|�|j||�}|��|_|jjd|_|�|j||�}|��|_|jjd|_|�	|j
|�|_|�	|j|�|_
|j
rC|j
d|_dSdS)a�
        Set up the ciphers and hashes using the given keys,

        @param outIV: the outgoing initialization vector
        @param outKey: the outgoing encryption key
        @param inIV: the incoming initialization vector
        @param inKey: the incoming encryption key
        @param outInteg: the outgoing integrity key
        @param inInteg: the incoming integrity key.
        ��N)�
_getCipherrD�	encryptor�	algorithm�
block_sizerHrE�	decryptorrI�_getMACrFrKrGrLrJ)rM�outIV�outKey�inIV�inKey�outInteg�inInteg�or$r$r%�setKeys�s

�zSSHCiphers.setKeyscCsL|j|\}}}|durt�St||d|��||d|jd��t�d�S)z�
        Creates an initialized cipher object.

        @param cip: the name of the cipher, maps into cipherMap
        @param iv: the initialzation vector
        @param key: the encryption key

        @return: the cipher object.
        NrS��backend)�	cipherMap�_DummyCipherrrXr	)rM�cip�iv�key�algorithmClass�keySize�	modeClassr$r$r%rU�s
�zSSHCiphers._getCipherc
Csn|j|}|s	dS|�}|j}|j}|d|�d||}|�tj�}|�tj�}t||||f�}	||	_|	S)am
        Gets a 4-tuple representing the message authentication code.
        (<hash module>, <inner hash value>, <outer hash value>,
        <digest size>)

        @type mac: L{bytes}
        @param mac: a key mapping into macMap

        @type key: L{bytes}
        @param key: the MAC key.

        @rtype: L{bytes}
        @return: The MAC components.
        rBN�)	�macMap�digest_sizerX�	translate�hmac�trans_36�trans_5Cr'ri)
rM�macri�mod�
hashObject�
digestSize�	blockSize�ira�resultr$r$r%rZ�s
zSSHCiphers._getMACcC�|j�|�S)z�
        Encrypt some data.

        @type blocks: L{bytes}
        @param blocks: The data to encrypt.

        @rtype: L{bytes}
        @return: The encrypted data.
        )rV�update�rM�blocksr$r$r%�encrypt��
zSSHCiphers.encryptcCr{)z�
        Decrypt some data.

        @type blocks: L{bytes}
        @param blocks: The data to decrypt.

        @rtype: L{bytes}
        @return: The decrypted data.
        )rYr|r}r$r$r%�decrypt�r�zSSHCiphers.decryptcCs:|jdsdSt�d|�|}t�|jj||jd���S)aa
        Create a message authentication code (MAC) for the given packet using
        the outgoing MAC values.

        @type seqid: L{int}
        @param seqid: The sequence ID of the outgoing packet.

        @type data: L{bytes}
        @param data: The data to create a MAC for.

        @rtype: L{str}
        @return: The serialized MAC.
        rrC�>L)rK�struct�packrq�HMACri�digest)rM�seqidr#r$r$r%�makeMAC�s
zSSHCiphers.makeMACcCsJ|jds	|dkSt�d|�|}t�|jj||jd���}t�||�S)a�
        Verify an incoming MAC using the incoming MAC values.

        @type seqid: L{int}
        @param seqid: The sequence ID of the incoming packet.

        @type data: L{bytes}
        @param data: The packet data to verify.

        @type mac: L{bytes}
        @param mac: The MAC sent with the packet.

        @rtype: L{bool}
        @return: C{True} if the MAC is valid.
        rrCr�)rLr�r�rqr�rir��compare_digest)rMr�r#rt�outerr$r$r%�verify�s

zSSHCiphers.verify)r(r)r*r+r�	TripleDESr�CBC�Blowfish�AES�CAST5�CTRrerrrrrrnrRrbrUrZrr�r�r�r$r$r$r%r,Is>
��	"r,c	Csvg}gd�}|D]0}tj|\}}}zt|d|�|d|jd�t�d���Wn	ty2Yqw|�|�q|S)z�
    Build a list of ciphers that are supported by the backend in use.

    @return: a list of supported ciphers.
    @rtype: L{list} of L{str}
    )r8r2r7r3r6r4r;r5r:r1r9r0� rSrc)r,rerrXr	rVr�append)�supportedCiphers�cs�cipherrjrkrlr$r$r%�_getSupportedCipherss 
�
�r�c@s^eZdZUdZe�ZdZde�d�Z	dZ
dede	de
��Ze
�Zgd	�Ze��ZgZeD]Ze�d
�dkrDee�d
d�g7Zq2ed
dg7Ze���rUe�d�ddgZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(e&Z)dZ*dZ+dZ,dZ-iZ.e/e0e0fe1d<dd�Z2dd �Z3d!d"�Z4d#d$�Z5d%d&�Z6d'd(�Z7d)d*�Z8d+d,�Z9d-d.�Z:d/d0�Z;d1d2�Z<e=d3d4��Z>e>j?d5d4��Z>d6d7�Z@d8d9�ZAd:d;�ZBd<d=�ZCd>d?�ZDd@dA�ZEdBdC�ZFdkdDdE�ZGdFdG�ZHdHdI�ZIdJdK�ZJdLdM�ZKdNdO�ZLdPdQ�ZMdRdS�ZNdTdU�ZOdVdW�ZPdldYdZ�ZQdld[d\�ZRd]d^�ZSd_d`�ZTdadb�ZUdcdd�ZVdedf�ZWdgdh�ZXdidj�ZYdS)m�SSHTransportBasea�
    Protocol supporting basic SSH functionality: sending/receiving packets
    and message dispatch.  To connect to or run a server, you must use
    SSHClientTransport or SSHServerTransport.

    @ivar protocolVersion: A string representing the version of the SSH
        protocol we support.  Currently defaults to '2.0'.

    @ivar version: A string representing the version of the server or client.
        Currently defaults to 'Twisted'.

    @ivar comment: An optional string giving more information about the
        server or client.

    @ivar supportedCiphers: A list of strings representing the encryption
        algorithms supported, in order from most-preferred to least.

    @ivar supportedMACs: A list of strings representing the message
        authentication codes (hashes) supported, in order from most-preferred
        to least.  Both this and supportedCiphers can include 'none' to use
        no encryption or authentication, but that must be done manually,

    @ivar supportedKeyExchanges: A list of strings representing the
        key exchanges supported, in order from most-preferred to least.

    @ivar supportedPublicKeys:  A list of strings representing the
        public key types supported, in order from most-preferred to least.

    @ivar supportedCompressions: A list of strings representing compression
        types supported, from most-preferred to least.

    @ivar supportedLanguages: A list of strings representing languages
        supported, from most-preferred to least.

    @ivar supportedVersions: A container of strings representing supported ssh
        protocol version numbers.

    @ivar isClient: A boolean indicating whether this is a client or server.

    @ivar gotVersion: A boolean indicating whether we have received the
        version string from the other side.

    @ivar buf: Data we've received but hasn't been parsed into a packet.

    @ivar outgoingPacketSequence: the sequence number of the next packet we
        will send.

    @ivar incomingPacketSequence: the sequence number of the next packet we
        are expecting from the other side.

    @ivar outgoingCompression: an object supporting the .compress(str) and
        .flush() methods, or None if there is no outgoing compression.  Used to
        compress outgoing data.

    @ivar outgoingCompressionType: A string representing the outgoing
        compression type.

    @ivar incomingCompression: an object supporting the .decompress(str)
        method, or None if there is no incoming compression.  Used to
        decompress incoming data.

    @ivar incomingCompressionType: A string representing the incoming
        compression type.

    @ivar ourVersionString: the version string that we sent to the other side.
        Used in the key exchange.

    @ivar otherVersionString: the version string sent by the other side.  Used
        in the key exchange.

    @ivar ourKexInitPayload: the MSG_KEXINIT payload we sent.  Used in the key
        exchange.

    @ivar otherKexInitPayload: the MSG_KEXINIT payload we received.  Used in
        the key exchange

    @ivar sessionID: a string that is unique to this SSH session.  Created as
        part of the key exchange, sessionID is used to generate the various
        encryption and authentication keys.

    @ivar service: an SSHService instance, or None.  If it's set to an object,
        it's the currently running service.

    @ivar kexAlg: the agreed-upon key exchange algorithm.

    @ivar keyAlg: the agreed-upon public key type for the key exchange.

    @ivar currentEncryptions: an SSHCiphers instance.  It represents the
        current encryption and authentication options for the transport.

    @ivar nextEncryptions: an SSHCiphers instance.  Held here until the
        MSG_NEWKEYS messages are exchanged, when nextEncryptions is
        transitioned to currentEncryptions.

    @ivar first: the first bytes of the next packet.  In order to avoid
        decrypting data twice, the first bytes are decrypted and stored until
        the whole packet is available.

    @ivar _keyExchangeState: The current protocol state with respect to key
        exchange.  This is either C{_KEY_EXCHANGE_NONE} if no key exchange is
        in progress (and returns to this value after any key exchange
        completqes), C{_KEY_EXCHANGE_REQUESTED} if this side of the connection
        initiated a key exchange, and C{_KEY_EXCHANGE_PROGRESSING} if the other
        side of the connection initiated a key exchange.  C{_KEY_EXCHANGE_NONE}
        is the initial value (however SSH connections begin with key exchange,
        so it will quickly change to another state).

    @ivar _blockedByKeyExchange: Whenever C{_keyExchangeState} is not
        C{_KEY_EXCHANGE_NONE}, this is a C{list} of pending messages which were
        passed to L{sendPacket} but could not be sent because it is not legal
        to send them while a key exchange is in progress.  When the key
        exchange completes, another attempt is made to send these messages.

    @ivar _peerSupportsExtensions: a boolean indicating whether the other side
        of the connection supports RFC 8308 extension negotiation.

    @ivar peerExtensions: a dict of extensions supported by the other side of
        the connection.
    �2.0sTwisted_�asciirC�SSH-�-r�)r=r>r?r@rAsecdh����ecdsasssh-rsasssh-dsssssh-ed25519r<�zlibr$)s1.99r�FrN�_KEY_EXCHANGE_NONE�_KEY_EXCHANGE_REQUESTED�_KEY_EXCHANGE_PROGRESSINGs
ext-info-cs
ext-info-s�peerExtensionscCs2|jr|j��t|d�r|��|j�d�dS)a
        When the underlying connection is closed, stop the running service (if
        any), and log out the avatar (if any).

        @type reason: L{twisted.python.failure.Failure}
        @param reason: The cause of the connection being closed.
        �avatarzconnection lostN)�service�serviceStopped�hasattr�logoutFunction�_log�info)rM�reasonr$r$r%�connectionLost�s


zSSHTransportBase.connectionLostcCsD|j�|jd�tdddd�|_|j�dddddd�|��dS)z�
        Called when the connection is made to the other side.  We sent our
        version and the MSG_KEXINIT packet.
        s
r<rCN)�	transport�write�ourVersionStringr,�currentEncryptionsrb�sendKexInit�rMr$r$r%�connectionMadeszSSHTransportBase.connectionMadecCs|j|jkrtd|jf��t|j�}|�|jr|jn|j�d�	t
tf�t�
d�td�	|��td�	|j��td�	|j��td�	|j��td�	|j��td�	|j��td�	|j��td�	|j��td�	|j��td�	|j��dg
�|_|�t|jdd��|j|_g|_dS)a7
        Send a I{KEXINIT} message to initiate key exchange or to respond to a
        key exchange initiated by the peer.

        @raise RuntimeError: If a key exchange has already been started and it
            is not appropriate to send a I{KEXINIT} message at this time.

        @return: L{None}
        z2Cannot send KEXINIT while key exchange state is %rrCr.�,s�N)�_keyExchangeStater��RuntimeError�list�supportedKeyExchangesr��isClient�_EXT_INFO_C�_EXT_INFO_S�join�bytes�MSG_KEXINITr�secureRandomr�supportedPublicKeysr��
supportedMACs�supportedCompressions�supportedLanguages�ourKexInitPayload�
sendPacketr��_blockedByKeyExchange)rMr�r$r$r%r�s:
��
	���
zSSHTransportBase.sendKexInitcCs`d|kr
dkrnn|tttfvSd|krdkr$nn|tfvSd|ko-dkSS)a�
        Determine if the given message type may be sent while key exchange is
        in progress.

        @param messageType: The type of message
        @type messageType: L{int}

        @return: C{True} if the given type of message may be sent while key
            exchange is in progress, C{False} if it may not.
        @rtype: L{bool}

        @see: U{http://tools.ietf.org/html/rfc4253#section-7.1}
        r������1)�MSG_SERVICE_REQUEST�MSG_SERVICE_ACCEPT�MSG_EXT_INFOr�)rM�messageTyper$r$r%�_allowedKeyExchangeMessageTypeDs�
z/SSHTransportBase._allowedKeyExchangeMessageTypecCs�|j|jkr|�|�s|j�||f�dSt|f�|}|jr+|j�|�|j�d�}|j	j
}dt|�}|||}|dkrC||}t�
d||d|�|t�|�}|j	�|�|j	�|j|�}|j�|�|jd7_dS)a�
        Sends a packet.  If it's been set up, compress the data, encrypt it,
        and authenticate it before sending.  If key exchange is in progress and
        the message is not part of key exchange, queue it to be sent later.

        @param messageType: The type of the packet; generally one of the
                            MSG_* values.
        @type messageType: L{int}
        @param payload: The payload for the message.
        @type payload: L{str}
        N����!LBr�)r�r�r�r�r�r��outgoingCompression�compress�flushr�rH�lenr�r�rr�rr��outgoingPacketSequencer�r�)rMr��payload�bs�	totalSize�lenPad�packet�	encPacketr$r$r%r�^s:
�
������zSSHTransportBase.sendPacketc
	Cs�|jj}|jj}t|j�|krdSt|d�s"|j�|jd|��}n|j}|`t�	d|dd��\}}|dkrD|�
ttd|����dSt|j�|d|krT||_dS|d|dkrq|�
ttd	|d||d|f��dS|jdd|�|jd|d�}|_||j�||d��}t|�d|kr�|�
td
�dS|r�|jd|�|j|d�}|_|j�
|j||�s�|�
td�dS|d|�}	|jr�z|j�|	�}	Wnty�|j�d�|�
td
�YdSw|jd7_|	S)z�
        Try to return a decrypted, authenticated, and decompressed packet
        out of the buffer.  If there is not enough data, return None.

        @rtype: L{str} or L{None}
        @return: The decoded packet, if any.
        N�firstr�r�izbad packet length r�rzbad packet mod (%i%%%i == %i)sbad decryptionsbad MACzError decompressing payloadscompression errorr�)r�rIrJr��bufr�r�r�r��unpack�sendDisconnect�DISCONNECT_PROTOCOL_ERRORrr��incomingPacketSequence�DISCONNECT_MAC_ERROR�incomingCompression�
decompress�	Exceptionr��failure�DISCONNECT_COMPRESSION_ERROR)
rMr��msr��	packetLen�
paddingLen�encDatar��macDatar�r$r$r%�	getPacket�sf
����( ��zSSHTransportBase.getPacketcCs|�td|�dS)z�
        Called when an unsupported version of the ssh protocol is received from
        the remote endpoint.

        @param remoteVersion: remote ssh protocol version which is unsupported
            by us.
        @type remoteVersion: L{str}
        sbad version N)r��)DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED)rM�
remoteVersionr$r$r%�_unsupportedVersionReceived�s	�z,SSHTransportBase._unsupportedVersionReceivedcCs|j||_|jset|j�dkr|�td�dS|j�d|j�d��dkr'dS|j�d�}|D]5}|�d�rdd|_|�d�|_	|�d	�d
}||j
vrS|�|�dS|�|�}d�
||d
d��|_q/|��}|r�t|dd
��}|�||d
d��|��}|skdSdS)a
        First, check for the version string (SSH-2.0-*).  After that has been
        received, this method adds data to the buffer, and pulls out any
        packets.

        @type data: L{bytes}
        @param data: The data that was received.
        isKPeer version string longer than 4KB. Preventing a denial of service attack.N�
r�r�T�
r�r�r)r��
gotVersionr�r��DISCONNECT_CONNECTION_LOST�find�split�
startswith�rstrip�otherVersionString�supportedVersionsr��indexr�r��ord�dispatchMessage)rMr#�lines�pr�ryr��
messageNumr$r$r%�dataReceived�s8	�



��zSSHTransportBase.dataReceivedcCs�|dkr2|tvr2t|dd�}t|d|��d�}|dur#||�dS|jjd||d�|��dS|jr>|j�||�dS|jjd||d�|��dS)	z�
        Send a received message to the appropriate method.

        @type messageNum: L{int}
        @param messageNum: The message number.

        @type payload: L{bytes}
        @param payload: The message payload.
        �2r�N�ssh_z*couldn't handle {messageType}: {payload!r})r�r�z)couldn't handle {messageNum}: {payload!r})rr�)�messages�getattrr��debug�sendUnimplementedr��packetReceived)rMrr�r��fr$r$r%r�s&
��z SSHTransportBase.dispatchMessagecC�t�|j���S)z�
        Returns an L{SSHTransportAddress} corresponding to the other (peer)
        side of this transport.

        @return: L{SSHTransportAddress} for the peer
        @rtype: L{SSHTransportAddress}
        @since: 12.1
        )r�SSHTransportAddressr��getPeerr�r$r$r%r�	zSSHTransportBase.getPeercCr)z�
        Returns an L{SSHTransportAddress} corresponding to the this side of
        transport.

        @return: L{SSHTransportAddress} for the peer
        @rtype: L{SSHTransportAddress}
        @since: 12.1
        )rrr��getHostr�r$r$r%r)rzSSHTransportBase.getHostcCs|jS)zS
        The key exchange algorithm name agreed between client and server.
        ��_kexAlgr�r$r$r%�kexAlg4�zSSHTransportBase.kexAlgcCs
||_dS)z6
        Set the key exchange algorithm name.
        Nr)rM�valuer$r$r%r;s
c
Csdttf�||_t|dd�d�}|dd�|d}}dd�|D�\
}}}}}	}
}}}
}||
|g}||	|g}|jr?||}}|j|j|j|j|j|j|j	|j	f}|||d|d|d|d|d	|d	f}|jro||}}t
|d|d�|_t
|d|d�|_t
t
|d	|d	�t
|d
|d
�t
|d|d�t
|d|d��|_t
|d
|d
�|_t
|d|d�|_d|j|j|j|jfvs�|j|j|jfvr�|�td�dSd|jj��vr�|�td�dS|jr�|jn|j|v|_|jjd|j|jd�|jjd|jj|jj|jd�|jjd|jj|jj|jd�|j|jk�r)|j |_n|�!�|||fS)a�
        Called when we receive a MSG_KEXINIT message.  Payload::
            bytes[16] cookie
            string keyExchangeAlgorithms
            string keyAlgorithms
            string incomingEncryptions
            string outgoingEncryptions
            string incomingAuthentications
            string outgoingAuthentications
            string incomingCompressions
            string outgoingCompressions
            string incomingLanguages
            string outgoingLanguages
            bool firstPacketFollows
            unit32 0 (reserved)

        Starts setting up the key exchange, keys, encryptions, and
        authentications.  Extended by ssh_KEXINIT in SSHServerTransport and
        SSHClientTransport.

        @type packet: L{bytes}
        @param packet: The message data.

        @return: A L{tuple} of negotiated key exchange algorithms, key
        algorithms, and unhandled data, or L{None} if something went wrong.
        r.N�
r�css�|]}|�d�VqdS)r�N)r�)�.0�sr$r$r%�	<genexpr>{s�z/SSHTransportBase.ssh_KEXINIT.<locals>.<genexpr>rr�r�rTr�r���scouldn't match all kex partsz%kex alg={kexAlg!r} key alg={keyAlg!r})r�keyAlgz)outgoing: {cip!r} {mac!r} {compression!r})rgrt�compressionz)incoming: {cip!r} {mac!r} {compression!r})"r�r��otherKexInitPayloadrr�r�r�r�r�r�rrrr,�nextEncryptions�outgoingCompressionType�incomingCompressionTyper�r�r��DISCONNECT_KEY_EXCHANGE_FAILED�__dict__�values�_peerSupportsExtensionsr�rrDrFrErGr�r�r�r�)rMr��k�strings�rest�kexAlgs�keyAlgs�encCS�encSC�macCS�macSC�compCS�compSC�langCS�langSC�outs�ins�server�clientr$r$r%�ssh_KEXINITPs��


�,

���
������

zSSHTransportBase.ssh_KEXINITcCsFt�d|dd��d}t|dd��\}}|�||�|j��dS)a<
        Called when we receive a MSG_DISCONNECT message.  Payload::
            long code
            string description

        This means that the other side has disconnected.  Pass the message up
        and disconnect ourselves.

        @type packet: L{bytes}
        @param packet: The message data.
        r�Nr�r)r�r�r�receiveErrorr��loseConnection)rMr��
reasonCode�description�foor$r$r%�ssh_DISCONNECT�szSSHTransportBase.ssh_DISCONNECTcCsdS)z�
        Called when we receive a MSG_IGNORE message.  No payload.
        This means nothing; we simply return.

        @type packet: L{bytes}
        @param packet: The message data.
        Nr$�rMr�r$r$r%�
ssh_IGNORE�szSSHTransportBase.ssh_IGNOREcCst�d|�\}|�|�dS)z�
        Called when we receive a MSG_UNIMPLEMENTED message.  Payload::
            long packet

        This means that the other side did not implement one of our packets.

        @type packet: L{bytes}
        @param packet: The message data.
        r�N)r�r��receiveUnimplemented)rMr��seqnumr$r$r%�ssh_UNIMPLEMENTED�s
z"SSHTransportBase.ssh_UNIMPLEMENTEDcCs>tt|dd���}t|dd�d�\}}}|�|||�dS)a0
        Called when we receive a MSG_DEBUG message.  Payload::
            bool alwaysDisplay
            string message
            string language

        This means the other side has passed along some debugging info.

        @type packet: L{bytes}
        @param packet: The message data.
        rr�Nr�)�boolrr�receiveDebug)rMr��
alwaysDisplay�message�langr?r$r$r%�	ssh_DEBUG�szSSHTransportBase.ssh_DEBUGcCsVt�d|dd��\}|dd�}i}t|�D]}t|d�\}}}|||<q||_dS)aD
        Called when we get a MSG_EXT_INFO message.  Payload::
            uint32 nr-extensions
            repeat the following 2 fields "nr-extensions" times:
              string extension-name
              string extension-value (binary)

        @type packet: L{bytes}
        @param packet: The message data.
        r�Nr�r�)r�r��rangerr�)rMr��
numExtensions�
extensions�_�extName�extValuer$r$r%�ssh_EXT_INFOs

zSSHTransportBase.ssh_EXT_INFOcCs<|jjd|jd�|jr|j��||_||_|j��dS)z�
        Set our service to service and start it running.  If we were
        running a service previously, stop it first.

        @type service: C{SSHService}
        @param service: The service to attach.
        zstarting service {service!r})r�N)r�r�namer�r�r��serviceStarted)rMr�r$r$r%�
setServices
zSSHTransportBase.setServicecCs(|�t|rdndt|�t|��dS)a�
        Send a debug message to the other side.

        @param message: the message to send.
        @type message: L{str}
        @param alwaysDisplay: if True, tell the other side to always
                              display this message.
        @type alwaysDisplay: L{bool}
        @param language: optionally, the language the message is in.
        @type language: L{str}
        �rmN)r��	MSG_DEBUGr)rMrIrH�languager$r$r%�	sendDebug#s�zSSHTransportBase.sendDebugcCs|�tt|��dS)a
        Send a message that will be ignored by the other side.  This is
        useful to fool attacks based on guessing packet sizes in the
        encrypted stream.

        @param message: data to send with the message
        @type message: L{str}
        N)r��
MSG_IGNOREr)rMrIr$r$r%�
sendIgnore3s	zSSHTransportBase.sendIgnorecCs|j}|�tt�d|��dS)zc
        Send a message to the other side that the last packet was not
        understood.
        z!LN)r�r��MSG_UNIMPLEMENTEDr�r��rMrDr$r$r%r>sz"SSHTransportBase.sendUnimplementedcCsD|�tt�d|�t|�td��|jjd||d�|j��dS)aJ
        Send a disconnect message to the other side and then disconnect.

        @param reason: the reason for the disconnect.  Should be one of the
                       DISCONNECT_* values.
        @type reason: L{int}
        @param desc: a descrption of the reason for the disconnection.
        @type desc: L{str}
        r�rCz;Disconnecting with error, code {code}
reason: {description}��coder>N)	r��MSG_DISCONNECTr�r�rr�r�r�r<)rMr��descr$r$r%r�Fs$
�zSSHTransportBase.sendDisconnectcCs@|jrd�t�dt|��gdd�|D��}|�t|�dSdS)a
        Send an RFC 8308 extension advertisement to the remote peer.

        Nothing is sent if the peer doesn't support negotiations.
        @type extensions: L{list} of (L{bytes}, L{bytes})
        @param extensions: a list of (extension-name, extension-value) pairs.
        rCr�cSs g|]\}}t|�t|��qSr$)r)rrSrr$r$r%�
<listcomp>cs z0SSHTransportBase.sendExtInfo.<locals>.<listcomp>N)r(r�r�r�r�r�r�)rMrNr�r$r$r%�sendExtInfoXs���zSSHTransportBase.sendExtInfocCsDt�|j|j�}|�t��}|��|_|j���	�j
}t|�|_dS)z�
        Prepares for a Diffie-Hellman key agreement exchange.

        Creates an ephemeral keypair in the group defined by (self.g,
        self.p) and stores it.
        N)
r�DHParameterNumbersr�g�
parametersr	�generate_private_key�dhSecretKey�
public_key�public_numbers�yr�dhSecretKeyPublicMP)rM�numbersrfrkr$r$r%�_startEphemeralDHgs

z"SSHTransportBase._startEphemeralDHcCs�t�|t�|j|j���t��}|j�|�}|`|�	d�}t
|dd��}|d@r9t�dt
|�d�d}||St�dt
|��}||S)a
        Completes the Diffie-Hellman key agreement started by
        _startEphemeralDH, and forgets the ephemeral secret key.

        @type remoteDHpublicKey: L{int}
        @rtype: L{bytes}
        @return: The new shared secret, in SSH C{mpint} format.

        rmrr��r�)r�DHPublicNumbersrdrrerir	rh�exchange�lstriprr�r�r�)rM�remoteDHpublicKey�	remoteKey�secret�ch�prefixr$r$r%�_finishEphemeralDHus��
�z#SSHTransportBase._finishEphemeralDHc	Cs�t�|j�}|||||j�}|��}||||���}|||||���}||||||���}||||S)a�
        Get one of the keys for authentication/encryption.

        @type c: L{bytes}
        @param c: The letter identifying which key this is.

        @type sharedSecret: L{bytes}
        @param sharedSecret: The shared secret K.

        @type exchangeHash: L{bytes}
        @param exchangeHash: The hash H from key exchange.

        @rtype: L{bytes}
        @return: The derived key.
        )r�getHashProcessorr�	sessionIDr�)	rM�c�sharedSecret�exchangeHash�
hashProcessor�k1�k2�k3�k4r$r$r%�_getKey�szSSHTransportBase._getKeyc	Cs�|js||_|�d||�}|�d||�}|�d||�}|�d||�}|�d||�}|�d||�}|||g}	|||g}
|jrB|
|	}	}
|j�|	d|	d|
d|
d|	d	|
d	�|�td
�dS)a�
        Set up the keys for the connection and sends MSG_NEWKEYS when
        finished,

        @param sharedSecret: a secret string agreed upon using a Diffie-
                             Hellman exchange, so it is only shared between
                             the server and the client.
        @type sharedSecret: L{str}
        @param exchangeHash: A hash of various data known by both sides.
        @type exchangeHash: L{str}
        �A�B�C�D�E�Frr�r�rCN)rzr�r�r"rbr��MSG_NEWKEYS)rMr|r}�initIVCS�initIVSC�encKeyCS�encKeySC�
integKeyCS�
integKeySCr6r7r$r$r%�	_keySetup�s


.zSSHTransportBase._keySetupcCsp|j�d�|j|_|jdkrt�d�|_|jdkrt�	�|_
|j|_|j
}d|_
|D]
\}}|�||�q+dS)a*
        Called back by a subclass once a I{MSG_NEWKEYS} message has been
        received.  This indicates key exchange has completed and new encryption
        and compression parameters should be adopted.  Any messages which were
        queued during key exchange will also be flushed.
        zNEW KEYSr�rN)r�rr"r�r#�zlib�compressobjr�r$�
decompressobjr�r�r�r�r�)rMr	r�r�r$r$r%�_newKeys�s


�zSSHTransportBase._newKeys�outcC�L|dkr
|jjdkS|dkr|jjdkS|dkr"|�d�o!|�d�Std��)z�
        Check if the connection is encrypted in the given direction.

        @type direction: L{str}
        @param direction: The direction: one of 'out', 'in', or 'both'.

        @rtype: L{bool}
        @return: C{True} if it is encrypted.
        r�r<�in�both�(direction must be "out", "in", or "both")r�rDrE�isEncrypted�	TypeError�rM�	directionr$r$r%r���
zSSHTransportBase.isEncryptedcCr�)a

        Check if the connection is verified/authentication in the given direction.

        @type direction: L{str}
        @param direction: The direction: one of 'out', 'in', or 'both'.

        @rtype: L{bool}
        @return: C{True} if it is verified.
        r�r<r�r�r�)r�rFrG�
isVerifiedr�r�r$r$r%r��r�zSSHTransportBase.isVerifiedcCs|�td�dS)zn
        Lose the connection to the other side, sending a
        DISCONNECT_CONNECTION_LOST message.
        suser closed connectionN)r�r�r�r$r$r%r<szSSHTransportBase.loseConnectioncCs|jjd||d�dS)a�
        Called when we receive a disconnect error message from the other
        side.

        @param reasonCode: the reason for the disconnect, one of the
                           DISCONNECT_ values.
        @type reasonCode: L{int}
        @param description: a human-readable description of the
                            disconnection.
        @type description: L{str}
        z3Got remote error, code {code}
reason: {description}r^N)r��error)rMr=r>r$r$r%r;	s

�zSSHTransportBase.receiveErrorcCs|jjd|d�dS)z�
        Called when we receive an unimplemented packet message from the other
        side.

        @param seqnum: the sequence number that was not understood.
        @type seqnum: L{int}
        z)other side unimplemented packet #{seqnum})rDN)r��warnr]r$r$r%rCsz%SSHTransportBase.receiveUnimplementedcCs|r|jjd|d�dSdS)a�
        Called when we receive a debug message from the other side.

        @param alwaysDisplay: if True, this message should always be
                              displayed.
        @type alwaysDisplay: L{bool}
        @param message: the debug message
        @type message: L{str}
        @param lang: optionally the language the message is in.
        @type lang: L{str}
        zRemote Debug Message: {message})rIN)r�r)rMrHrIrJr$r$r%rG%s�zSSHTransportBase.receiveDebugcCsr|j�d�r'ztjd|jdd�}Wntytd��wt�|t��S|jdvr1t	j
��Std�|j���)a>
        Generate an private key for ECDH key exchange.

        @rtype: The appropriate private key type matching C{self.kexAlg}:
            L{ec.EllipticCurvePrivateKey} for C{ecdh-sha2-nistp*}, or
            L{x25519.X25519PrivateKey} for C{curve25519-sha256}.
        @return: The generated private key.
        �ecdh-sha2-nistpr�r�N�
unused-key�scurve25519-sha256scurve25519-sha256@libssh.orgz3Cannot generate elliptic curve private key for {!r})
rr�r�_curveTable�KeyErrorrrrgr	r
�X25519PrivateKey�generate�format)rM�curver$r$r%�_generateECPrivateKey4s	�

��z&SSHTransportBase._generateECPrivateKeycCsN|j�d�r|�tjjtjj�S|jdvr|�tjjtjj�St	d|j����)a�
        Encode an elliptic curve public key to bytes.

        @type ecPub: The appropriate public key type matching
            C{self.kexAlg}: L{ec.EllipticCurvePublicKey} for
            C{ecdh-sha2-nistp*}, or L{x25519.X25519PublicKey} for
            C{curve25519-sha256}.
        @param ecPub: The public key to encode.

        @rtype: L{bytes}
        @return: The encoded public key.
        r�r�z,Cannot encode elliptic curve public key for )
rr��public_bytesr
�Encoding�X962�PublicFormat�UncompressedPoint�Rawr)rM�ecPubr$r$r%�_encodeECPublicKeyMs
�
�
�z#SSHTransportBase._encodeECPublicKeycCs�|j�d�r3ztjd|jdd�}Wntytd��wtj�||�}|�	t�
�|�}t|�S|jdvrGtj�
|�}|�	|�}t|�Std�|j���)a�
        Generate a shared secret for ECDH key exchange.

        @type ecPriv: The appropriate private key type matching
            C{self.kexAlg}: L{ec.EllipticCurvePrivateKey} for
            C{ecdh-sha2-nistp*}, or L{x25519.X25519PrivateKey} for
            C{curve25519-sha256}.
        @param ecPriv: Our private key.

        @rtype: L{bytes}
        @return: The generated shared secret, as an SSH multiple-precision
            integer.
        r�r�r�Nr�r�z5Cannot generate elliptic curve shared secret for {!r})rr�rr�r�rr�EllipticCurvePublicKey�from_encoded_pointrq�ECDHr
�X25519PublicKey�from_public_bytesr�r&)rM�ecPriv�theirECPubBytesr��
theirECPubr|r$r$r%�_generateECSharedSecreths(��
�
���z(SSHTransportBase._generateECSharedSecret)FrC)r�)Zr(r)r*r+rr��protocolVersion�twisted_version�encode�version�comment�stripr�r�r�r�r�getSupportedKeyExchangesr�r��eckeyr��replacer	�ed25519_supportedr�r�r�r�r�r�r�r�r�r�r�rzr�r�r�r�r�r�r�r�r(r�rr��__annotations__r�r�r�r�r�r�r�rrrr�propertyr�setterr:r@rBrErKrRrUrYr[rr�rcrnrxr�r�r�r�r�r<r;rCrGr�r�r�r$r$r$r%r�5s�
x�	�


2'>
. 

z	



	
r�c@s`eZdZdZdZdZdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�ZdS)�SSHServerTransporta/
    SSHServerTransport implements the server side of the SSH protocol.

    @ivar isClient: since we are never the client, this is always False.

    @ivar ignoreNextPacket: if True, ignore the next key exchange packet.  This
        is set when the client sends a guessed key exchange packet but with
        an incorrect guess.

    @ivar dhGexRequest: the KEX_DH_GEX_REQUEST(_OLD) that the client sent.
        The key generation needs this to be stored.

    @ivar g: the Diffie-Hellman group generator.

    @ivar p: the Diffie-Hellman group prime.
    FrcCsdt�||�}|s
dS|\}}}t|dd��r.|d|jdks)|d|jdkr0d|_dSdSdS)aM
        Called when we receive a MSG_KEXINIT message.  For a description
        of the packet, see SSHTransportBase.ssh_KEXINIT().  Additionally,
        this method checks if a guessed key exchange packet was sent.  If
        it was sent, and it guessed incorrectly, the next key exchange
        packet MUST be ignored.
        Nrr�T)r�r:rr�r��ignoreNextPacket)rMr��retvalr,r-r+r$r$r%r:�s

�zSSHServerTransport.ssh_KEXINITc
Cst|�\}}|jj|j}|jj|j}|��}|��|_|�|j�}|�	||�}t
�|j��}|�
t|j��|�
t|j��|�
t|j��|�
t|j��|�
t|����|�
t|��|�
t|��|�
|�|��}	|�tt|���t|�t|�|	���|�||	�dS)a
        Called from L{ssh_KEX_DH_GEX_REQUEST_OLD} to handle
        elliptic curve key exchanges.

        Payload::

            string client Elliptic Curve Diffie-Hellman public key

        Just like L{_ssh_KEXDH_INIT} this message type is also not dispatched
        directly. Extra check to determine if this is really KEX_ECDH_INIT
        is required.

        First we load the host's public/private keys.
        Then we generate the ECDH public/private keypair for the given curve.
        With that we generate the shared secret key.
        Then we compute the hash to sign and send back to the client
        Along with the server's public key and the ECDH public key.

        @type packet: L{bytes}
        @param packet: The message data.

        @return: None.
        N)r�factory�
publicKeysr�privateKeysr�rir�r�r�rryrr|rr�r�r!r��blobr�r��MSG_KEXDH_REPLY�signr�)
rMr��pktPub�
pubHostKey�privHostKeyr��encPubr|�hr}r$r$r%�_ssh_KEX_ECDH_INIT�s,

 �z%SSHServerTransport._ssh_KEX_ECDH_INITcCst|�\}}t�|j�\|_|_|��|�|�}t�|j��}|�	t
|j��|�	t
|j��|�	t
|j
��|�	t
|j��|�	t
|jj|j����|�	t|��|�	|j�|�	|�|��}|�tt
|jj|j���|jt
|jj|j�|���|�||�dS)aW
        Called to handle the beginning of a non-group key exchange.

        Unlike other message types, this is not dispatched automatically.  It
        is called from C{ssh_KEX_DH_GEX_REQUEST_OLD} because an extra check is
        required to determine if this is really a KEXDH_INIT message or if it
        is a KEX_DH_GEX_REQUEST_OLD message.

        The KEXDH_INIT payload::

                integer e (the client's Diffie-Hellman public key)

        We send the KEXDH_REPLY with our host key and signature.

        @type packet: L{bytes}
        @param packet: The message data.
        N)rr�getDHGeneratorAndPrimerrerrnrxryr|rr�r�r!r�r�r�rr�rrlr�r�r�r�r�r��rMr��clientDHpublicKeyr?r|r�r}r$r$r%�_ssh_KEXDH_INIT�s.

���z"SSHServerTransport._ssh_KEXDH_INITcCs�|jrd|_dSt�|j�r|�|�St�|j�r|�|�S||_t�	d|�d}|j
�|�\|_|_
|��|�tt|j
�t|j��dS)a�
        This represents different key exchange methods that share the same
        integer value.  If the message is determined to be a KEXDH_INIT,
        L{_ssh_KEXDH_INIT} is called to handle it. If it is a KEX_ECDH_INIT,
        L{_ssh_KEX_ECDH_INIT} is called.
        Otherwise, for KEX_DH_GEX_REQUEST_OLD payload::

                integer ideal (ideal size for the Diffie-Hellman prime)

            We send the KEX_DH_GEX_GROUP message with the group that is
            closest in size to ideal.

        If we were told to ignore the next key exchange packet by ssh_KEXINIT,
        drop it on the floor and return.

        @type packet: L{bytes}
        @param packet: The message data.
        rNr�)r�r�isFixedGrouprr��isEllipticCurver��dhGexRequestr�r�r��
getDHPrimererrnr��MSG_KEX_DH_GEX_GROUPr)rMr��idealr$r$r%�ssh_KEX_DH_GEX_REQUEST_OLDs

 z-SSHServerTransport.ssh_KEX_DH_GEX_REQUEST_OLDcCsd|jrd|_dS||_t�d|�\}}}|j�|�\|_|_|��|�	t
t|j�t|j��dS)a#
        Called when we receive a MSG_KEX_DH_GEX_REQUEST message.  Payload::
            integer minimum
            integer ideal
            integer maximum

        The client is asking for a Diffie-Hellman group between minimum and
        maximum size, and close to ideal if possible.  We reply with a
        MSG_KEX_DH_GEX_GROUP message.

        If we were told to ignore the next key exchange packet by ssh_KEXINIT,
        drop it on the floor and return.

        @type packet: L{bytes}
        @param packet: The message data.
        rNz>3L)r�r�r�r�r�r�rerrnr�r�r)rMr��minr��maxr$r$r%�ssh_KEX_DH_GEX_REQUEST>s z)SSHServerTransport.ssh_KEX_DH_GEX_REQUESTcCs$t|�\}}|�|�}t�|j��}|�t|j��|�t|j��|�t|j	��|�t|j
��|�t|jj|j
����|�|j�|�t|j��|�t|j��|�t|��|�|j�|�|�|��}|�tt|jj|j
���|jt|jj|j
�|���|�||�dS)a
        Called when we get a MSG_KEX_DH_GEX_INIT message.  Payload::
            integer e (client DH public key)

        We send the MSG_KEX_DH_GEX_REPLY message with our host key and
        signature.

        @type packet: L{bytes}
        @param packet: The message data.
        N)rrxrryrr|rr�r�r!r�r�r�rr�r�rrrerlr�r��MSG_KEX_DH_GEX_REPLYr�r�r�r�r$r$r%�ssh_KEX_DH_GEX_INITXs0

���z&SSHServerTransport.ssh_KEX_DH_GEX_INITcCs<|jdu}t�|||�|r|�dd�|j�fg�dSdS)�3
        See SSHTransportBase._keySetup().
        Nsserver-sig-algsr�)rzr�r�rcr�r�)rMr|r}�firstKeyr$r$r%r��s
��zSSHServerTransport._keySetupcCs$|dkr|�td�dS|��dS)a
        Called when we get a MSG_NEWKEYS message.  No payload.
        When we get this, the keys have been set on both sides, and we
        start using them to encrypt and authenticate the connection.

        @type packet: L{bytes}
        @param packet: The message data.
        rC�NEWKEYS takes no dataN)r�r�r�rAr$r$r%�ssh_NEWKEYS�s	zSSHServerTransport.ssh_NEWKEYScCsRt|�\}}|j�||�}|s|�td|�dS|�tt|��|�|��dS)aX
        Called when we get a MSG_SERVICE_REQUEST message.  Payload::
            string serviceName

        The client has requested a service.  If we can start the service,
        start it; otherwise, disconnect with
        DISCONNECT_SERVICE_NOT_AVAILABLE.

        @type packet: L{bytes}
        @param packet: The message data.
        sdon't have service N)	rr��
getServicer�� DISCONNECT_SERVICE_NOT_AVAILABLEr�r�rrU)rMr�r�r+�clsr$r$r%�ssh_SERVICE_REQUEST�s�z&SSHServerTransport.ssh_SERVICE_REQUESTN)r(r)r*r+r�r�r:r�r�r�r�r�r�r�r�r$r$r$r%r��s;(%)r�c@s�eZdZdZdZdZdZdZdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"S)#�SSHClientTransporta)
    SSHClientTransport implements the client side of the SSH protocol.

    @ivar isClient: since we are always the client, this is always True.

    @ivar _gotNewKeys: if we receive a MSG_NEWKEYS message before we are
        ready to transition to the new keys, this is set to True so we
        can transition when the keys are ready locally.

    @ivar x: our Diffie-Hellman private key.

    @ivar e: our Diffie-Hellman public key.

    @ivar g: the Diffie-Hellman group generator.

    @ivar p: the Diffie-Hellman group prime

    @ivar instance: the SSHService object we are requesting.

    @ivar _dhMinimalGroupSize: Minimal acceptable group size advertised by the
        client in MSG_KEX_DH_GEX_REQUEST.
    @type _dhMinimalGroupSize: int

    @ivar _dhMaximalGroupSize: Maximal acceptable group size advertised by the
        client in MSG_KEX_DH_GEX_REQUEST.
    @type _dhMaximalGroupSize: int

    @ivar _dhPreferredGroupSize: Preferred group size advertised by the client
        in MSG_KEX_DH_GEX_REQUEST.
    @type _dhPreferredGroupSize: int
    Tii icCst�|�d|_dS)z{
        Called when the connection is started with the server.  Just sets
        up a private instance variable.
        rN)r�r��_gotNewKeysr�r$r$r%r��s

z!SSHClientTransport.connectionMadec	Cs�t�||�dur
dSt�|j�r)|��|_|j��|_|�	t
t|�|j���dSt�
|j�rFt�|j�\|_|_|��|�	t|j�dS|�	tt�d|j|j|j��dS)a>
        Called when we receive a MSG_KEXINIT message.  For a description
        of the packet, see SSHTransportBase.ssh_KEXINIT().  Additionally,
        this method sends the first key exchange packet.

        If the agreed-upon exchange is ECDH, generate a key pair for the
        corresponding curve and send the public key.

        If the agreed-upon exchange has a fixed prime/generator group,
        generate a public key and send it in a MSG_KEXDH_INIT message.
        Otherwise, ask for a 2048 bit group with a MSG_KEX_DH_GEX_REQUEST
        message.
        N�!LLL)r�r:rr�rr�r�rir�r��MSG_KEX_DH_GEX_REQUEST_OLDrr�r�r�rerrn�MSG_KEXDH_INITrl�MSG_KEX_DH_GEX_REQUESTr�r��_dhMinimalGroupSize�_dhPreferredGroupSize�_dhMaximalGroupSizerAr$r$r%r:�s*
���zSSHClientTransport.ssh_KEXINITcsp�fdd�}t|d�\}}}}d�dd�tt|����D��}��||�}|�||||�|��fdd��|S)	a�
        Called to handle a reply to a ECDH exchange message(KEX_ECDH_INIT).

        Like the handler for I{KEXDH_INIT}, this message type has an
        overlapping value.  This method is called from C{ssh_KEX_DH_GEX_GROUP}
        if that method detects a non-group key exchange is in progress.

        Payload::

            string serverHostKey
            string server Elliptic Curve Diffie-Hellman public key
            string signature

        We verify the host key and continue if it passes verificiation.
        Otherwise raise an exception and return.

        @type packet: L{bytes}
        @param packet: The message data.

        @return: A deferred firing when key exchange is complete.
        cs�|}���j|�}t��j��}|�t�j��|�t�j��|�t�j	��|�t�j
��|�t|��|�t���j���|�t|��|�|�|�
�}tj�|��||�sd��td�dS��||�dS)N�
bad signature)r�r�rryrr|rr�r�r�r!r�r�r�r�Key�
fromStringr�r�r%r�)�ignored�hostKey�pubKey�	signature�theirECHostr|r�r}r�r$r%�_continue_KEX_ECDH_REPLY0s
zHSSHClientTransport._ssh_KEX_ECDH_REPLY.<locals>._continue_KEX_ECDH_REPLYrT�:cS�g|]}t�|��qSr$��binascii�hexlify�rrvr$r$r%rbP�z:SSHClientTransport._ssh_KEX_ECDH_REPLY.<locals>.<listcomp>c���td�S�Nsbad host key�r��"DISCONNECT_HOST_KEY_NOT_VERIFIABLE��unusedr�r$r%�<lambda>U��z8SSHClientTransport._ssh_KEX_ECDH_REPLY.<locals>.<lambda>)rr�rrr��
verifyHostKey�addCallback�
addErrback)rMr�rr�rr�fingerprint�dr$r�r%�_ssh_KEX_ECDH_REPLYs�
�z&SSHClientTransport._ssh_KEX_ECDH_REPLYc�xt|�\}}t|�\}}t|�\}}d�dd�tt|����D��}��||�}|��j|||�|�	�fdd��|S)a�
        Called to handle a reply to a non-group key exchange message
        (KEXDH_INIT).

        Like the handler for I{KEXDH_INIT}, this message type has an
        overlapping value.  This method is called from C{ssh_KEX_DH_GEX_GROUP}
        if that method detects a non-group key exchange is in progress.

        Payload::

            string serverHostKey
            integer f (server Diffie-Hellman public key)
            string signature

        We verify the host key by calling verifyHostKey, then continue in
        _continueKEXDH_REPLY.

        @type packet: L{bytes}
        @param packet: The message data.

        @return: A deferred firing when key exchange is complete.
        rcSrr$rr	r$r$r%rbvr
z7SSHClientTransport._ssh_KEXDH_REPLY.<locals>.<listcomp>crrr
rr�r$r%r{rz5SSHClientTransport._ssh_KEXDH_REPLY.<locals>.<lambda>)
rrr�rrr�rr�_continueKEXDH_REPLYr�rMr�rrrrrr$r�r%�_ssh_KEXDH_REPLY[s�
�z#SSHClientTransport._ssh_KEXDH_REPLYcCsbt�|j�r|�|�St�|j�r|�|�St|�\|_}t|�\|_}|�	�|�
t|j�dS)a�
        This handles different messages which share an integer value.

        If the key exchange does not have a fixed prime/generator group,
        we generate a Diffie-Hellman public key and send it in a
        MSG_KEX_DH_GEX_INIT message.

        Payload::
            string g (group generator)
            string p (group prime)

        @type packet: L{bytes}
        @param packet: The message data.
        N)
rr�rrr�rrrrernr��MSG_KEX_DH_GEX_INITrl)rMr�r+r$r$r%�ssh_KEX_DH_GEX_GROUP�s

z'SSHClientTransport.ssh_KEX_DH_GEX_GROUPc	Cs�tj�|�}|�|�}t�|j��}|�t|j	��|�t|j
��|�t|j��|�t|j��|�t|��|�|j
�|�t|��|�|�|��}|�||�s]|�td�dS|�||�dS)�
        The host key has been verified, so we generate the keys.

        @param ignored: Ignored.

        @param pubKey: the public key blob for the server's public key.
        @type pubKey: L{str}
        @param f: the server's Diffie-Hellman public key.
        @type f: L{int}
        @param signature: the server's signature, verifying that it has the
            correct private key.
        @type signature: L{str}
        r�N)rr�r�rxrryrr|rr�r�r�r!rlrr�r�r�r%r��	rMr�rrr�	serverKeyr|r�r}r$r$r%r�s 

z'SSHClientTransport._continueKEXDH_REPLYcr)a�
        Called when we receive a MSG_KEX_DH_GEX_REPLY message.  Payload::
            string server host key
            integer f (server DH public key)

        We verify the host key by calling verifyHostKey, then continue in
        _continueGEX_REPLY.

        @type packet: L{bytes}
        @param packet: The message data.

        @return: A deferred firing once key exchange is complete.
        rcSrr$r)rr{r$r$r%rb�r
z;SSHClientTransport.ssh_KEX_DH_GEX_REPLY.<locals>.<listcomp>crrr
rr�r$r%r�rz9SSHClientTransport.ssh_KEX_DH_GEX_REPLY.<locals>.<lambda>)
rrr�rrr�rr�_continueGEX_REPLYrrr$r�r%�ssh_KEX_DH_GEX_REPLY�s�
�z'SSHClientTransport.ssh_KEX_DH_GEX_REPLYc	Cstj�|�}|�|�}t�|j��}|�t|j	��|�t|j
��|�t|j��|�t|j��|�t|��|�t
�d|j|j|j��|�t|j��|�t|j��|�|j�|�t|��|�|�|��}|�||�s{|�td�dS|�||�dS)rr�r�N)rr�r�rxrryrr|rr�r�r�r!r�r�r�r�r�rrrerlr�r�r�r%r�r r$r$r%r"�s4
��
z%SSHClientTransport._continueGEX_REPLYcCs&t�|||�|jr|�d�dSdS)r�rCN)r�r�r�r�)rMr|r}r$r$r%r��s�zSSHClientTransport._keySetupcCs>|dkr|�td�dS|jjsd|_dS|��|��dS)a
        Called when we receive a MSG_NEWKEYS message.  No payload.
        If we've finished setting up our own keys, start using them.
        Otherwise, remember that we've received this message.

        @type packet: L{bytes}
        @param packet: The message data.
        rCr�Nr�)r�r�r"rHr�r��connectionSecurerAr$r$r%r�s	zSSHClientTransport.ssh_NEWKEYScCsJ|dkr|j�d�nt|�d}||jjkr|�td�|�|j�dS)z�
        Called when we receive a MSG_SERVICE_ACCEPT message.  Payload::
            string service name

        Start the service we requested.

        @type packet: L{bytes}
        @param packet: The message data.
        rCz"got SERVICE_ACCEPT without payloadrs.received accept for service we did not requestN)r�r�r�instancerSr�r�rU)rMr�rSr$r$r%�ssh_SERVICE_ACCEPTs
�z%SSHClientTransport.ssh_SERVICE_ACCEPTcCs|�tt|j��||_dS)z�
        Request that a service be run over this transport.

        @type instance: subclass of L{twisted.conch.ssh.service.SSHService}
        @param instance: The service to run.
        N)r�r�rrSr%)rMr%r$r$r%�requestService.s
z!SSHClientTransport.requestServicecCst�t��S)aa
        Returns a Deferred that gets a callback if it is a valid key, or
        an errback if not.

        @type hostKey: L{bytes}
        @param hostKey: The host key to verify.

        @type fingerprint: L{bytes}
        @param fingerprint: The fingerprint of the key.

        @return: A deferred firing with C{True} if the key is valid.
        )r�fail�NotImplementedError)rMr�rr$r$r%r:s
z SSHClientTransport.verifyHostKeycCst��)z�
        Called when the encryption has been set up.  Generally,
        requestService() is called to run another service over the transport.
        )r)r�r$r$r%r$Irz#SSHClientTransport.connectionSecureN)r(r)r*r+r�r�r�r�r�r:rrrrr#r"r�r�r&r'rr$r$r$r$r%r��s( /B&)r�c@seZdZdZdd�ZdS)�_NullEncryptionContextzH
    An encryption context that does not actually encrypt anything.
    cCs|S)z�
        'Encrypt' new data by doing nothing.

        @type data: L{bytes}
        @param data: The data to 'encrypt'.

        @rtype: L{bytes}
        @return: The 'encrypted' data.
        r$)rMr#r$r$r%r|Vs
z_NullEncryptionContext.updateN)r(r)r*r+r|r$r$r$r%r*Qsr*c@seZdZdZdZdS)�_DummyAlgorithmzJ
    An encryption algorithm that does not actually encrypt anything.
    �@N)r(r)r*r+rXr$r$r$r%r+csr+c@s&eZdZdZe�Zdd�Zdd�ZdS)rfz�
    A cipher for the none encryption method.

    @ivar block_size: the block size of the encryption.  In the case of the
    none cipher, this is 8 bytes.
    cC�t�S)zN
        Construct a noop encryptor.

        @return: The encryptor.
        �r*r�r$r$r%rVu�z_DummyCipher.encryptorcCr-)zN
        Construct a noop decryptor.

        @return: The decryptor.
        r.r�r$r$r%rY}r/z_DummyCipher.decryptorN)r(r)r*r+r+rWrVrYr$r$r$r%rfks
rfsdiffie-hellman-group14-sha1r�r�rTr�r�rrr��r���"r/�!rS�	r���
���MSG_�
MSG_KEXDH_r�r�z7legacy SSH mnemonics should not end up in messages dict)hr+rrqr�r��hashlibrrrrr�typingr�cryptography.exceptionsr�cryptography.hazmat.backendsr	�cryptography.hazmat.primitivesr
�)cryptography.hazmat.primitives.asymmetricrrr
�&cryptography.hazmat.primitives.ciphersrrr�twistedrr��twisted.conch.sshrrr�twisted.conch.ssh.commonrrrrr�twisted.internetrr�twisted.loggerr�twisted.pythonr�twisted.python.compatrrr&�tupler'r,r��Protocolr�r�r�r*r+rfr��DH_GENERATOR�DH_PRIMEr`rZr\rWr�r�r�r�r�r�r�r�r�r�rr��&DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECTr�r%�DISCONNECT_RESERVEDr�r�r�r�rr��DISCONNECT_BY_APPLICATION�DISCONNECT_TOO_MANY_CONNECTIONS�!DISCONNECT_AUTH_CANCELLED_BY_USER�)DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE�DISCONNECT_ILLEGAL_USER_NAMEr	r��globals�itemsrSrr�r�r$r$r$r%�<module>s�	H%`,��

Spamworldpro Mini