
    tf                       U d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*  e&d          Z+ee,         e-d<   e+rKdZ.ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7 ddlm8Z8m9Z9m:Z:m;Z; ddl<m=Z=  e2            >                                Z?n-e+sdZ.dZ? G d d          Z; G d d          Z9 G d d          Z8d  Z@d! ZA G d" d#e;jB                  ZC G d$ d%          ZD G d& d'          ZE G d( d)ejF                  ZG G d* d+e9jH                  ZI G d, d-eI          ZJ G d. d/eI          ZKd0 ZL G d1 d2e*          ZM G d3 d4          ZN G d5 d6          ZO G d7 d8          ZP G d9 d:          ZQ G d; d<          ZR G d= d>eReM          ZS G d? d@eR          ZT G dA dBeTeNeM          ZU G dC dDeTeOeM          ZV G dE dFeTePeM          ZWe@ G dG dHeTeQeM                      ZX G dI dJ          ZY G dK dLeY          ZZ G dM dNeZeM          Z[ G dO dPeZ          Z\ G dQ dRe\eNeM          Z] G dS dTe\eOeM          Z^ G dU dVeZ          Z_ G dW dXe_ePeM          Z`e@ G dY dZe_eQeM                      Za G d[ d\eY          Zb G d] d^ebeM          Zc G d_ d`eb          Zd G da dbedeNeM          Ze G dc ddedeOeM          Zf G de dfeb          Zg G dg dhegePeM          Zhe@ G di djegeQeM                      Zi G dk dle*          Zj G dm dne*          Zk G do dpe*          ZldS )qz5
Tests for ssh/transport.py and the classes therein.
    N)md5sha1sha256sha384sha512)DictListOptionalTupleType)__version__)
ConchError)_kexaddressservice)defer)loopback)	randbytes)	iterbytesinsecureRandom)requireModule)proto_helpers)TestCasecryptographydependencySkip)UnsupportedAlgorithm)default_backend)serialization)dhec)commonfactorykeys	transport)keydatazcan't run without cryptographyFc                   \    e Zd Z G d d          Z G d d          Z G d d          ZdS )r%   c                       e Zd ZdS )transport.SSHTransportBaseN__name__
__module____qualname__     `/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/conch/test/test_transport.pySSHTransportBaser)   2           Dr/   r1   c                       e Zd ZdS )transport.SSHServerTransportNr*   r.   r/   r0   SSHServerTransportr4   5   r2   r/   r5   c                       e Zd ZdS )transport.SSHClientTransportNr*   r.   r/   r0   SSHClientTransportr7   8   r2   r/   r8   N)r+   r,   r-   r1   r5   r8   r.   r/   r0   r%   r%   1   s        	 	 	 	 	 	 	 		 	 	 	 	 	 	 		 	 	 	 	 	 	 	 	 	r/   r%   c                   (    e Zd Z G d d          ZdS )r#   c                       e Zd ZdS )factory.SSHFactoryNr*   r.   r/   r0   
SSHFactoryr;   <   r2   r/   r<   N)r+   r,   r-   r<   r.   r/   r0   r#   r#   ;   s<        	 	 	 	 	 	 	 	 	 	r/   r#   c                   $    e Zd Zed             ZdS )r"   c                     dS )Nr/   r.   )selfargs     r0   NSz	common.NS@   s    3r/   N)r+   r,   r-   classmethodrA   r.   r/   r0   r"   r"   ?   s-        		 	 
	 	 	r/   r"   c                 "    t           sd| _        | S )Nz#x25519 not supported on this system)X25519_SUPPORTEDskipfs    r0   skipWithoutX25519rH   E   s     76Hr/   c                 H    t          j        t          | ||                    S )z3
    Return the MP version of C{(x ** y) % z}.
    )r"   MPpow)xyzs      r0   _MPpowrO   K   s     9SAq\\"""r/   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	MockTransportBasean  
    A base class for the client and server protocols.  Stores the messages
    it receives instead of ignoring them.

    @ivar errors: a list of tuples: (reasonCode, description)
    @ivar unimplementeds: a list of integers: sequence number
    @ivar debugs: a list of tuples: (alwaysDisplay, message, lang)
    @ivar ignoreds: a list of strings: ignored data
    c                     t           j                            |            g | _        g | _        g | _        g | _        d| _        dS )z,
        Set up instance variables.
        N)r%   r1   connectionMadeerrorsunimplementedsdebugsignoredsgotUnsupportedVersionr?   s    r0   rS   z MockTransportBase.connectionMade]   sF     	"11$777 %)"""r/   c                 P    || _         t          j                            | |          S )zZ
        Intercept unsupported version call.

        @type remoteVersion: L{str}
        )rX   r%   r1   _unsupportedVersionReceived)r?   remoteVersions     r0   r[   z-MockTransportBase._unsupportedVersionReceivedh   s,     &3")EE-
 
 	
r/   c                 >    | j                             ||f           dS )zp
        Store any errors received.

        @type reasonCode: L{int}
        @type description: L{str}
        NrT   append)r?   
reasonCodedescriptions      r0   receiveErrorzMockTransportBase.receiveErrors   s%     	J455555r/   c                 :    | j                             |           dS )zX
        Store any unimplemented packet messages.

        @type seqnum: L{int}
        N)rU   r_   )r?   seqnums     r0   receiveUnimplementedz&MockTransportBase.receiveUnimplemented|   s!     	""6*****r/   c                 @    | j                             |||f           dS )z
        Store any debug messages.

        @type alwaysDisplay: L{bool}
        @type message: L{str}
        @type lang: L{str}
        N)rV   r_   )r?   alwaysDisplaymessagelangs       r0   receiveDebugzMockTransportBase.receiveDebug   s'     	M7D9:::::r/   c                 :    | j                             |           dS )zG
        Store any ignored data.

        @type packet: L{str}
        N)rW   r_   r?   packets     r0   
ssh_IGNOREzMockTransportBase.ssh_IGNORE   s      	V$$$$$r/   N)
r+   r,   r-   __doc__rS   r[   rb   re   rj   rn   r.   r/   r0   rQ   rQ   R   sx         	* 	* 	*	
 	
 	
6 6 6+ + +; ; ;% % % % %r/   rQ   c                   `    e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZd Zd	 Zd
 Zd Zd ZdS )
MockCipherzH
    A mocked-up version of twisted.conch.ssh.transport.SSHCiphers.
       test      F)Nr/   r/   rt   r.   c                     d| _         t          |          | j        z  dk    r<t          dt          |          | j        t          |          | j        z  fz            |S )z~
        Called to encrypt the packet.  Simply record that encryption was used
        and return the data unchanged.
        Tr   *length %i modulo blocksize %i is not 0: %i)usedEncryptlenencBlockSizeRuntimeErrorr?   rL   s     r0   encryptzMockCipher.encrypt   h    
  FFT&&1,,<q664,c!fft7H.HIJ   r/   c                     d| _         t          |          | j        z  dk    r<t          dt          |          | j        t          |          | j        z  fz            |S )z~
        Called to decrypt the packet.  Simply record that decryption was used
        and return the data unchanged.
        Tr   rv   )usedDecryptrx   ry   rz   decBlockSizer{   s     r0   decryptzMockCipher.decrypt   r}   r/   c                 "    t          |f          S )zs
        Make a Message Authentication Code by sending the character value of
        the outgoing packet.
        bytes)r?   outgoingPacketSequencepayloads      r0   makeMACzMockCipher.makeMAC   s    
 ,.///r/   c                 *    t          |f          |k    S )zy
        Verify the Message Authentication Code by checking that the packet
        sequence number is the same.
        r   )r?   incomingPacketSequencerm   macDatas       r0   verifyzMockCipher.verify   s    
 ,.//7::r/   c                      ||||||f| _         dS )z"
        Record the keys.
        N)r$   )r?   ivOutkeyOutivInkeyInmacInmacOuts          r0   setKeyszMockCipher.setKeys   s     FD%?			r/   N)r+   r,   r-   ro   
outCipTypery   	inCipTyper   	inMACType
outMACTypeverifyDigestSizerw   r   outMACinMACr$   r|   r   r   r   r   r.   r/   r0   rq   rq      s          JLILIJKK FED    0 0 0; ; ;@ @ @ @ @r/   rq   c                   $    e Zd ZdZd Zd Zd ZdS )MockCompressionz
    A mocked-up compression, based on the zlib interface.  Instead of
    compressing, it reverses the data and adds a 0x66 byte to the end.
    c                     |d d d         S Nr.   r?   r   s     r0   compresszMockCompression.compress   s    ttt}r/   c                 (    |d d         d d d         S r   r.   r   s     r0   
decompresszMockCompression.decompress   s    ss|DDbD!!r/   c                     dS )N   fr.   r?   kinds     r0   flushzMockCompression.flush   s    wr/   N)r+   r,   r-   ro   r   r   r   r.   r/   r0   r   r      sK         
  " " "    r/   r   c                   @    e Zd ZdZdZdZdZdddZd Zd Z	d	 Z
d
 ZdS )MockServicez
    A mocked-up service, based on twisted.conch.ssh.service.SSHService.

    @ivar started: True if this service has been started.
    @ivar stopped: True if this service has been stopped.
       MockServiceFMSG_TESTMSG_fiction)   G   c                     dS )Nr   r.   rY   s    r0   	logPrefixzMockService.logPrefix   s    }r/   c                     d| _         dS )z6
        Record that the service was started.
        TN)startedrY   s    r0   serviceStartedzMockService.serviceStarted        r/   c                     d| _         dS )z6
        Record that the service was stopped.
        TN)stoppedrY   s    r0   serviceStoppedzMockService.serviceStopped   r   r/   c                 <    | j                             d|           dS )z:
        A message that this service responds to.
        r   N)r%   
sendPacketrl   s     r0   ssh_TESTzMockService.ssh_TEST  s"     	!!$/////r/   N)r+   r,   r-   ro   namer   r   protocolMessagesr   r   r   r   r.   r/   r0   r   r      sy          DGG(m<<      0 0 0 0 0r/   r   c                   ^    e Zd ZdZdeiZd Zd Zdee	e
ee	e	f                  f         fdZdS )MockFactoryzL
    A mocked-up factory based on twisted.conch.ssh.factory.SSHFactory.
       ssh-userauthc                     t           j                            t          j                  t           j                            t          j                  dS )zG
        Return the public keys that authenticate this server.
           ssh-rsas   ssh-dsa)r$   Key
fromStringr&   publicRSA_opensshpublicDSA_opensshrY   s    r0   getPublicKeyszMockFactory.getPublicKeys  s?    
 ++G,EFF++G,EFF
 
 	
r/   c                     t           j                            t          j                  t           j                            t          j                  dS )zH
        Return the private keys that authenticate this server.
        r   )r$   r   r   r&   privateRSA_opensshprivateDSA_opensshrY   s    r0   getPrivateKeyszMockFactory.getPrivateKeys  s?    
 ++G,FGG++G,FGG
 
 	
r/   returnc                 8    t          j        d          }|gdgdS )a/  
        Diffie-Hellman primes that can be used for key exchange algorithms
        that use group exchange to establish a prime / generator group.

        @return: The primes and generators.
        @rtype: L{dict} mapping the key size to a C{list} of
            C{(generator, prime)} tuple.
           diffie-hellman-group14-sha1)      )   i   )r   getDHGeneratorAndPrime)r?   group14s     r0   	getPrimeszMockFactory.getPrimes$  s'     -.LMMix000r/   N)r+   r,   r-   ro   r   servicesr   r   r   intr	   r   r   r.   r/   r0   r   r     sz           -H
 
 

 
 
14T%S/%: :; 1 1 1 1 1 1r/   r   c                       e Zd ZdZd ZdS )MockOldFactoryPublicKeysz
    The old SSHFactory returned mappings from key names to strings from
    getPublicKeys().  We return those here for testing.
    c                     t                               |           } |j                    dd         D ]\  }}|                                ||<   |S )zJ
        We used to map key types to public key blobs as strings.
        N)r   r   itemsblobr?   r$   r   keys       r0   r   z&MockOldFactoryPublicKeys.getPublicKeys<  sR     ((..#aaa 	$ 	$ID#DJJr/   N)r+   r,   r-   ro   r   r.   r/   r0   r   r   6  -         
    r/   r   c                       e Zd ZdZd ZdS )MockOldFactoryPrivateKeysz
    The old SSHFactory returned mappings from key names to cryptography key
    objects from getPrivateKeys().  We return those here for testing.
    c                     t                               |           } |j                    dd         D ]\  }}|j        ||<   |S )zG
        We used to map key types to cryptography key objects.
        N)r   r   r   	keyObjectr   s       r0   r   z(MockOldFactoryPrivateKeys.getPrivateKeysL  sL     ))$//#aaa 	' 	'ID#DJJr/   N)r+   r,   r-   ro   r   r.   r/   r0   r   r   F  r   r/   r   c           
      b   | j         }| j        }|                                }t          d t	          d|dz
  d          D                       }t          |||          }	 t          j        |t          j        |t          j	        ||                              
                    t                                | _        n(# t          $ r t          d| d| d| d            w xY wt          j        | j                                                                        j                  | _        d S )	Nc              3       K   | ]	}d |z  V  
dS )	   Nr.   .0rL   s     r0   	<genexpr>z)generatePredictableKey.<locals>.<genexpr>Z  s&      44C1H444444r/   r         z
p=z
g=z
x=
)pg
bit_lengthsumrangerK   r    DHPrivateNumbersDHPublicNumbersDHParameterNumbersprivate_keyr   dhSecretKey
ValueErrorprintr"   rJ   
public_keypublic_numbersrM   dhSecretKeyPublicMP)r%   r   r   bitsrL   rM   s         r0   generatePredictableKeyr   V  s3   AA<<>>D44eAtax3344444A 	Aq!A " 3r!!R%:1a%@%@AA!
 !

+o''
(
( 	    (Q((A((1((())) %+I((**99;;=% %I!!!s    AC   %C%c                   ^    e Zd ZU dZdZeeej                          e	d<   e
re
Zd Zd Zd ZdS )TransportTestCasez.
    Base class for transport test cases.
    Nklassc                 h    t          j                     _                                          _        g  _        d }                     t          d|           t          j	        t           j                   j        _         fd} j                             j                   | j        _        d S )Nc                     d| z  S )z;
            Return a consistent entropy value
               r.   )rx   s    r0   secureRandomz-TransportTestCase.setUp.<locals>.secureRandomy  s     S= r/   r  c                 @    j                             | |f           d S N)packetsr_   )messageTyper   r?   s     r0   stubSendPacketz/TransportTestCase.setUp.<locals>.stubSendPacket  s$    Lg 677777r/   )r   StringTransportr%   r   protor  patchr   types
MethodTyper   _startEphemeralDHmakeConnectionr   )r?   r  r	  s   `  r0   setUpzTransportTestCase.setUpt  s    &688ZZ\\
	! 	! 	! 	

9nl;;;','7"DJ(
 (

$	8 	8 	8 	8 	8 	
!!$.111 .
r/   c                     |                     d           |                    t          j        | j                   |                    dd           |j        |_        dS )z
        Deliver enough additional messages to C{proto} so that the key exchange
        which is started in L{SSHTransportBase.connectionMade} completes and
        non-key exchange messages can be sent and received.
        s   SSH-2.0-BogoClient-1.2i
   foos   barN)dataReceiveddispatchMessager%   MSG_KEXINIT_A_KEXINIT_MESSAGE	_keySetup_KEY_EXCHANGE_NONE_keyExchangeStater?   r  s     r0   finishKeyExchangez#TransportTestCase.finishKeyExchange  s\     	9:::i3T5LMMM''' #(":r/   c                     | j         j        | j         _        g | j         _        | j                             ||           dS )z
        Finish a key exchange by calling C{_keySetup} with the given arguments.
        Also do extra whitebox stuff to satisfy that method's assumption that
        some kind of key exchange has actually taken place.
        N)r  _KEY_EXCHANGE_REQUESTEDr  _blockedByKeyExchanger  )r?   sharedSecretexchangeHashs      r0   simulateKeyExchangez%TransportTestCase.simulateKeyExchange  s;     (,z'I
$+-
(
\<88888r/   )r+   r,   r-   ro   r   r
   r   r%   r1   __annotations__r   rE   r  r  r"  r.   r/   r0   r   r   j  sy           9=E8D345<<< / / /.; ; ;"9 9 9 9 9r/   r   c                       e Zd ZdZdZeZdS )DHGroupExchangeSHA1Mixinz=
    Mixin for diffie-hellman-group-exchange-sha1 tests.
    "   diffie-hellman-group-exchange-sha1N)r+   r,   r-   ro   kexAlgorithmr   hashProcessorr.   r/   r0   r%  r%    s$          9LMMMr/   r%  c                       e Zd ZdZdZeZdS )DHGroupExchangeSHA256Mixinz?
    Mixin for diffie-hellman-group-exchange-sha256 tests.
    $   diffie-hellman-group-exchange-sha256Nr+   r,   r-   ro   r'  r   r(  r.   r/   r0   r*  r*    s$          ;LMMMr/   r*  c                       e Zd ZdZdZeZdS )	ECDHMixinz8
    Mixin for elliptic curve diffie-hellman tests.
       ecdh-sha2-nistp256Nr,  r.   r/   r0   r.  r.    s$          )LMMMr/   r.  c                       e Zd ZdZdZeZdS )Curve25519SHA256Mixinz,
    Mixin for curve25519-sha256 tests.
    s   curve25519-sha256Nr,  r.   r/   r0   r1  r1    s$          (LMMMr/   r1  c                   D    e Zd ZU dZeZeeej	                          e
d<   dS )BaseSSHTransportBaseCasez,
    Base case for TransportBase tests.
    r   N)r+   r,   r-   ro   rQ   r   r
   r   r%   r1   r#  r.   r/   r0   r3  r3    s<           9JE8D345IIIIIr/   r3  c                      e Zd ZdZereZd ej        d          z    ej        d          z    ej        d          z    ej        d          z    ej        d          z    ej        d          z    ej        d          z    ej        d          z    ej        d          z    ej        d          z   d	z   d
z   Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd  Zd! Zd" Z d# Z!d$ Z"d% Z#d& Z$d' Z%d( Z&d) Z'd* Z(d+ Z)d, Z*d- Z+d. Z,d/ Z-d0 Z.d1 Z/d2 Z0d3 Z1d4 Z2d5 Z3d6S )7BaseSSHTransportTestszs
    Test TransportBase. It implements the non-server/client specific
    parts of the SSH transport protocol.
       r   r   
   aes256-ctr	   hmac-sha1   noner/              c                    | j                                                             dd          d         }|                     |dt	          j        d          z              |                    d          t          d          d         }dd	                    d
 t          j
        D                       z   dz   }|                     ||           dS )a  
        Test that the first thing sent over the connection is the version
        string.  The 'softwareversion' part must consist of printable
        US-ASCII characters, with the exception of whitespace characters and
        the minus sign.

        RFC 4253, section 4.2.
           
rt   r   s   SSH-2.0-Twisted_asciizSSH-2.0-Nz^(|c              3   r   K   | ]2}|d k    |                                 t          j        |          V  3dS )-N)isspacereescape)r   cs     r0   r   z9BaseSSHTransportTests.test_sendVersion.<locals>.<genexpr>  sC        !"!s((199;;(	!(((( r/   z)*$)r%   valuesplitassertEqualtwisted_versionencodedecoderx   joinstring	printableassertRegex)r?   versionsoftwareVersionsoftwareVersionRegexs       r0   test_sendVersionz&BaseSSHTransportTests.test_sendVersion  s     .&&((..w::1="58Nw8W8W"WXXX!..11#j//2C2CD hh  &,&6     	 	 	*>?????r/   c                    t                      }|                    | j                   |                    d           |                    d           |                    d           |                     | j        j                   |                    d           |                     | j        j                   |                     d| j                                                   dS )z
        When the peer is not sending its SSH version but keeps sending data,
        the connection is disconnected after 4KB to prevent buffering too
        much and running our of memory.
        s   SSH-2-Server-Identifiers  1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890s   1235678s   1234567s%   Preventing a denial of service attackN)	rQ   r  r%   r  assertFalsedisconnecting
assertTrueassertInrF  r?   suts     r0   'test_dataReceiveVersionNotSentMemoryDOSz=BaseSSHTransportTests.test_dataReceiveVersionNotSentMemoryDOS  s      !!4>*** 	3444,---$$$5666 	$$$ 	4555>@T@T@V@VWWWWWr/   c                 ^   t                      }|                    | j                   |                     |           | j                                         t          d          }d}|                    ||           | j                                        }|                     |d           dS )a@  
        Test that plain (unencrypted, uncompressed) packets are sent
        correctly.  The format is::
            uint32 length (including type and padding length)
            byte padding length
            byte type
            bytes[length-padding length-2] data
            bytes[padding length] padding
        A   BCDEFGs      ABCDEFGN)	rQ   r  r%   r  clearordr   rF  rH  )r?   r  rh   r   rF  s        r0   test_sendPacketPlainz*BaseSSHTransportTests.test_sendPacketPlain  s     "##T^,,,u%%%c(('***$$&& NOOOOOr/   c                    t                      }|                    | j                   |                     |           t	                      x|_        }t          d          }d}| j                                         |                    ||           | 	                    |j
                   | j                                        }|                     |d           dS )z
        Test that packets sent while encryption is enabled are sent
        correctly.  The whole packet should be encrypted.
        r]     BCs      ABCN)rQ   r  r%   r  rq   currentEncryptionsr`  r_  r   rW  rw   rF  rH  r?   r  
testCipherrh   r   rF  s         r0   test_sendPacketEncryptedz.BaseSSHTransportTests.test_sendPacketEncrypted-  s    
 "##T^,,,u%%%0:< :c(('***
.///$$&&	
 	
 	
 	
 	
r/   c                 |   t                      }|                    | j                   |                     |           t	                      |_        | j                                         |                    t          d          d           | j        	                                }| 
                    |d           dS )z
        Test that packets sent while compression is enabled are sent
        correctly.  The packet type and data should be encrypted.
        r]     Bs      BAfN)rQ   r  r%   r  r   outgoingCompressionr_  r   r`  rF  rH  )r?   r  rF  s      r0   test_sendPacketCompressedz/BaseSSHTransportTests.test_sendPacketCompressedJ  s    
 "##T^,,,u%%%$3$5$5!S4((($$&&P	
 	
 	
 	
 	
r/   c                    t                      }|                    | j                   |                     |           t	                      x|_        }t                      |_        t          d          }d}| j        	                                 |
                    ||           |                     |j                   | j                                        }|                     |d           dS )z
        Test that packets sent while compression and encryption are
        enabled are sent correctly.  The packet type and data should be
        compressed and then the whole packet should be encrypted.
        r]  rc  s      	CBAfN)rQ   r  r%   r  rq   rd  r   rj  r`  r_  r   rW  rw   rF  rH  re  s         r0   test_sendPacketBothz)BaseSSHTransportTests.test_sendPacketBothZ  s     "##T^,,,u%%%0:< :$3$5$5!c(('***
.///$$&&	
 	
 	
 	
 	
r/   c                    t                      }|                    | j                   |                     |           | j                                         |                    t          d          d           | j                                        dz   |_        | 	                    |
                                d           | 	                    |j        d           dS )zt
        Test that packets are retrieved correctly out of the buffer when
        no encryption is enabled.
        r]  rc  s   extras   ABCN)rQ   r  r%   r  r_  r   r`  rF  bufrH  	getPacketr  s     r0   test_getPacketPlainz)BaseSSHTransportTests.test_getPacketPlainy  s    
 "##T^,,,u%%%S5)))N((**X5	**F333H-----r/   c                    t                      }d |_        |                    | j                   | j                                         t                      x|_        }|                    t          d          d           | j        	                                }|dt
          j
                 |_        |                     |                                           |                     |j                   |                     |j        d           |xj        |t
          j
        d         z  c_        |                     |                                d           |                     |j        d           dS )zl
        Test that encrypted packets are retrieved correctly.
        See test_sendPacketEncrypted.
        c                      d S r  r.   r.   r/   r0   <lambda>z?BaseSSHTransportTests.test_getPacketEncrypted.<locals>.<lambda>      D r/   r]     BCDNs      	A   ABCDr/   )rQ   sendKexInitr  r%   r_  rq   rd  r   r`  rF  r   ro  assertIsNonerp  rW  r   rH  first)r?   r  rf  rF  s       r0   test_getPacketEncryptedz-BaseSSHTransportTests.test_getPacketEncrypted  s@   
 "##(LT^,,,0:< :S6***$$&&3J334	%//++,,,
.///&>???		U:24455		**G444C(((((r/   c                    t                      }|                    | j                   |                     |           | j                                         t                      |_        |j        |_        |                    t          d          d           | j        
                                |_        |                     |                                d           dS )zo
        Test that compressed packets are retrieved correctly.  See
        test_sendPacketCompressed.
        r]  rv  rw  N)rQ   r  r%   r  r_  r   rj  incomingCompressionr   r`  rF  ro  rH  rp  r  s     r0   test_getPacketCompressedz.BaseSSHTransportTests.test_getPacketCompressed  s    
 "##T^,,,u%%%$3$5$5!$)$=!S6***N((**	**G44444r/   c                    t                      }d |_        |                    | j                   | j                                         t                      |_        t                      |_        |j        |_	        |
                    t          d          d           | j                                        |_        |                     |                                d           dS )zv
        Test that compressed and encrypted packets are retrieved correctly.
        See test_sendPacketBoth.
        c                      d S r  r.   r.   r/   r0   rt  z:BaseSSHTransportTests.test_getPacketBoth.<locals>.<lambda>  ru  r/   r]  r^  s   ABCDEFGN)rQ   rx  r  r%   r_  rq   rd  r   rj  r}  r   r`  rF  ro  rH  rp  r  s     r0   test_getPacketBothz(BaseSSHTransportTests.test_getPacketBoth  s    
 "##(LT^,,,#-<< $3$5$5!$)$=!S9---N((**	**J77777r/   c                     t          j        dddd          }dx}}| j        j        D ],}|                     |                    |||                     -dS )z?
        Test that all the supportedCiphers are valid.
           Ari     C   D                   N)r%   
SSHCiphersr  supportedCiphersrW  
_getCipher)r?   ciphersivr   cipNames        r0   test_ciphersAreValidz*BaseSSHTransportTests.test_ciphersAreValid  sm     &tT4>>Sz2 	B 	BGOOG..wC@@AAAA	B 	Br/   c                    | j                                                             dd          d         }|| j        _        | j                                        }|                     |dd         t          t           j        f                     |                     |dd         d           t          j
        |dd         d          \  }}}}}}}	}
}}}|                     |d                    | j        j        d	gz                        |                     |d                    | j        j                             |                     |d                    | j        j                             |                     |d                    | j        j                             |                     |d                    | j        j                             |                     |d                    | j        j                             |                     |	d                    | j        j                             |                     |
d                    | j        j                             |                     |d                    | j        j                             |                     |d                    | j        j                             |                     |d
           dS )a  
        Test that the KEXINIT (key exchange initiation) message is sent
        correctly.  Payload::
            bytes[16] cookie
            string key exchange algorithms
            string public key algorithms
            string outgoing ciphers
            string incoming ciphers
            string outgoing MACs
            string incoming MACs
            string outgoing compressions
            string incoming compressions
            bool first packet follows
            uint32 0
        r=  rt   r      s   N
      ,s
   ext-info-ss        )r%   rF  rG  r  ro  rp  rH  r   r  r"   getNSrL  supportedKeyExchangessupportedPublicKeysr  supportedMACssupportedCompressionssupportedLanguages)r?   rF  rm   keyExchangespubkeysciphers1ciphers2macs1macs2compressions1compressions2
languages1
languages2ro  s                 r0   test_sendKexInitz&BaseSSHTransportTests.test_sendKexInit  s     $$&&,,Wa88;
%%''!eY-B,D&E&EFFF"|444 Lb))	
 	$))DJ$D$VWW	
 	
 	
 	$))DJ,J"K"KLLL499TZ-H#I#IJJJ499TZ-H#I#IJJJ		$**B C CDDD		$**B C CDDD		$*2R(S(STTT		$*2R(S(STTTTYYtz/L%M%MNNNTYYtz/L%M%MNNNk*****r/   c                     | j                                          | j                            t           j        | j                   |                     | j        g            dS )zy
        Immediately after connecting, the transport expects a KEXINIT message
        and does not reply to it.
        N)r%   r_  r  r  r  r  rH  r  rY   s    r0   test_receiveKEXINITReplyz.BaseSSHTransportTests.test_receiveKEXINITReply  sT    
 	
""9#8$:QRRRr*****r/   c                 T   |                      | j                   | j        dd= | j                            t          j        | j                   |                     t          | j                  d           |                     | j        d         d         t          j                   dS )z
        When a KEXINIT message is received which is not a reply to an earlier
        KEXINIT message which was sent, a KEXINIT reply is sent.
        Nrt   r   )	r  r  r  r  r%   r  r  rH  rx   rY   s    r0   test_sendKEXINITReplyz+BaseSSHTransportTests.test_sendKEXINITReply  s    
 	tz***LO
""9#8$:QRRRT\**A...a+Y-BCCCCCr/   c                 P    |                      t          | j        j                   dS )a(  
        A new key exchange cannot be started while a key exchange is already in
        progress.  If an attempt is made to send a I{KEXINIT} message using
        L{SSHTransportBase.sendKexInit} while a key exchange is in progress
        causes that method to raise a L{RuntimeError}.
        N)assertRaisesrz   r  rx  rY   s    r0   test_sendKexInitTwiceFailsz0BaseSSHTransportTests.test_sendKexInitTwiceFails	  s%     	,
(>?????r/   c                 &   t           j        t           j        g}| j                                          | j        `|D ]J}| j                            |d           |                     | j                                         d           K|                     | j                   t                      | j        _
        | j                                         |                     | j                                                             d          d           dS )z
        After L{SSHTransportBase.sendKexInit} has been called, messages types
        other than the following are queued and not sent until after I{NEWKEYS}
        is sent by L{SSHTransportBase._keySetup}.

        RFC 4253, section 7.1.
        r  r/      N)r%   MSG_SERVICE_REQUESTr  r_  r  r   rH  rF  r  rq   nextEncryptions_newKeyscount)r?   disallowedMessageTypesr  s      r0   test_sendKexInitBlocksOthersz2BaseSSHTransportTests.test_sendKexInitBlocksOthers  s     )!"
 	 J!1 	: 	:KJ!!+v666T^1133S9999tz*** &0\\
" 	
--//55f==qAAAAAr/   c                 N   d| j         _        | j                             ddg           |                     | j        t
          j        dt          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   fg           d	S )
ze
        Test that EXT_INFO messages are sent correctly.  See RFC 8308,
        section 2.3.
        T   server-sig-algs   ssh-rsa,rsa-sha2-256)	   elevation   d      r  r  r  r  N)	r  _peerSupportsExtensionssendExtInforH  r  r%   MSG_EXT_INFOr"   rA   rY   s    r0   test_sendExtInfoz&BaseSSHTransportTests.test_sendExtInfo6  s    
 .2
*
=$	
 	
 	
 	L *'i 2334i 7889 i--. ioo	&		
 	
 	
 	
 	
r/   c                 r    | j                             dg           |                     | j        g            dS )z
        If the peer has not advertised support for extension negotiation, no
        EXT_INFO message is sent, since RFC 8308 only guarantees that the
        peer will be prepared to accept it if it has advertised support.
        r  N)r  r  rH  r  rY   s    r0   test_sendExtInfoUnsupportedz1BaseSSHTransportTests.test_sendExtInfoUnsupportedP  s<     	
 MNOOOr*****r/   c                 >   | j                             t          j        dt	          j        d          z   t	          j        d          z   t	          j        d          z   t	          j        d          z              |                     | j         j        ddd           dS )z
        When an EXT_INFO message is received, the transport stores a mapping
        of the peer's advertised extensions.  See RFC 8308, section 2.3.
        r  r  !   ssh-rsa,rsa-sha2-256,rsa-sha2-512   no-flow-control   s)r  r  N)r  r  r%   r  r"   rA   rH  peerExtensionsrY   s    r0   test_EXT_INFOz#BaseSSHTransportTests.test_EXT_INFOY  s    
 	
"""i*++,i<==> i*++, ioo		
 	
 	
 	J%$H$( 	
 	
 	
 	
 	
r/   c                     | j                             ddd           |                     | j        t          j        dfg           dS )z
        Test that debug messages are sent correctly.  Payload::
            bool always display
            string debug message
            string language
        rr   T   en      test   enN)r  	sendDebugrH  r  r%   	MSG_DEBUGrY   s    r0   test_sendDebugz$BaseSSHTransportTests.test_sendDebugn  sT     	
WdE222L!#PQR	
 	
 	
 	
 	
r/   c                     | j                             t          j        d           | j                             t          j        d           |                     | j         j        ddg           dS )zW
        Test that debug messages are received correctly.  See test_sendDebug.
        r  s       silent   en)Trr   r  )Fs   silentr  N)r  r  r%   r  rH  rV   rY   s    r0   test_receiveDebugz'BaseSSHTransportTests.test_receiveDebug{  s     	
""!N	
 	
 	
 	
""!P	
 	
 	
 	J 68QR	
 	
 	
 	
 	
r/   c                     | j                             d           |                     | j        t          j        dfg           dS )zk
        Test that ignored messages are sent correctly.  Payload::
            string ignored data
        rr   s      testN)r  
sendIgnorerH  r  r%   
MSG_IGNORErY   s    r0   test_sendIgnorez%BaseSSHTransportTests.test_sendIgnore  sO    
 	
g&&&LI02IJK	
 	
 	
 	
 	
r/   c                     | j                             t          j        d           |                     | j         j        dg           dS )zb
        Test that ignored messages are received correctly.  See
        test_sendIgnore.
        rr   N)r  r  r%   r  rH  rW   rY   s    r0   test_receiveIgnorez(BaseSSHTransportTests.test_receiveIgnore  sB    
 	
""9#7AAA,wi88888r/   c                     | j                                          |                     | j        t          j        dfg           dS )zt
        Test that unimplemented messages are sent correctly.  Payload::
            uint32 sequence number
        r;  N)r  sendUnimplementedrH  r  r%   MSG_UNIMPLEMENTEDrY   s    r0   test_sendUnimplementedz,BaseSSHTransportTests.test_sendUnimplemented  sM    
 	
$$&&&LI79LMN	
 	
 	
 	
 	
r/   c                     | j                             t          j        d           |                     | j         j        dg           dS )zo
        Test that unimplemented messages are received correctly.  See
        test_sendUnimplemented.
        s      r   N)r  r  r%   r  rH  rU   rY   s    r0   test_receiveUnimplementedz/BaseSSHTransportTests.test_receiveUnimplemented  sC    
 	
""9#>@STTT2SE:::::r/   c                     dgfd}|| j         _        | j                            dd           |                     | j        t           j        dfg           |                     d                    dS )z
        Test that disconnection messages are sent correctly.  Payload::
            uint32 reason code
            string reason description
            string language
        Fc                      d d<   d S NTr   r.   disconnecteds   r0   stubLoseConnectionzEBaseSSHTransportTests.test_sendDisconnect.<locals>.stubLoseConnection      "LOOOr/   r   rr   s         test    r   N)r%   loseConnectionr  sendDisconnectrH  r  MSG_DISCONNECTrW  r?   r  r  s     @r0   test_sendDisconnectz)BaseSSHTransportTests.test_sendDisconnect  s     w	# 	# 	# 	# 	# );%
!!$000L ,K	
 	
 	
 	Q(((((r/   c                     dgfd}|| j         _        | j                            t           j        d           |                     | j        j        dg           |                     d                    dS )zl
        Test that disconnection messages are received correctly.  See
        test_sendDisconnect.
        Fc                      d d<   d S r  r.   r  s   r0   r  zHBaseSSHTransportTests.test_receiveDisconnect.<locals>.stubLoseConnection  r  r/   s         testr   rr   r   N)r%   r  r  r  r  rH  rT   rW  r  s     @r0   test_receiveDisconnectz,BaseSSHTransportTests.test_receiveDisconnect  s    
 w	# 	# 	# 	# 	# );%
""$&M	
 	
 	
 	*^,<===Q(((((r/   c                 Z   dgfd}|| j         _        | j                             | j                                                   |                     | j         j                   |                     | j         j        | j         j	                   |                     d                    dS )ze
        Test that dataReceived parses packets and dispatches them to
        ssh_* methods.
        Fc                     dd<   d S r  r.   )rm   kexInits    r0   stubKEXINITz<BaseSSHTransportTests.test_dataReceived.<locals>.stubKEXINIT  s    GAJJJr/   r   N)
r  ssh_KEXINITr  r%   rF  rW  
gotVersionrH  ourVersionStringotherVersionString)r?   r  r  s     @r0   test_dataReceivedz'BaseSSHTransportTests.test_dataReceived  s    
 '	 	 	 	 	 "-

 4 4 6 6777
-...4dj6STTT
#####r/   c                 X   t                      }| j                            |           |                     | j        j        |           |                     |j                   | j                            dd           |                     | j        dg           t                      }| j                            |           |                     |j                   |                     |j	                   | j        
                    d           |                     |j	                   dS )z
        Test that the transport can set the running service and dispatches
        packets to the service's packetReceived method.
        r   rr   r  N)r   r  
setServicerH  r   rW  r   r  r  r   connectionLost)r?   r   service2s      r0   test_servicez"BaseSSHTransportTests.test_service  s    
 --
g&&&+W555(((
""4111'8999==
h'''()))(((
!!$'''()))))r/   c                     dgfd}|| j         _        d| j         _        | j                             d           |                     d                    dS )zP
        Test that the transport notifies the avatar of disconnections.
        Fc                      d d<   d S r  r.   r  s   r0   logoutz1BaseSSHTransportTests.test_avatar.<locals>.logout  r  r/   TNr   )r  logoutFunctionavatarr  rW  )r?   r  r  s     @r0   test_avatarz!BaseSSHTransportTests.test_avatar  sm     w	# 	# 	# 	# 	# %+
! 

!!$'''Q(((((r/   c                    |                      | j                            d                     |                      | j                            d                     |                      | j                            d                     t                      | j        _        |                     | j                            d                     |                     | j                            d                     |                     | j                            d                     t          j        dddd          | j        _        |                      | j                            d                     |                      | j                            d                     |                      | j                            d                     |                     t          | j        j        d           dS )zS
        Test that the transport accurately reflects its encrypted status.
        inoutbothr9  badN)
rU  r  isEncryptedrq   rd  rW  r%   r  r  	TypeErrorrY   s    r0   test_isEncryptedz&BaseSSHTransportTests.test_isEncrypted
  s    	//55666//66777//77888(2
%
..t44555
..u55666
..v66777(1(<Wgw)
 )

% 	//55666//66777//77888)TZ%;UCCCCCr/   c                    |                      | j                            d                     |                      | j                            d                     |                      | j                            d                     t                      | j        _        |                     | j                            d                     |                     | j                            d                     |                     | j                            d                     t          j        dddd          | j        _        |                      | j                            d                     |                      | j                            d                     |                      | j                            d                     |                     t          | j        j        d           dS )zR
        Test that the transport accurately reflects its verified status.
        r  r  r   r9  r  N)
rU  r  
isVerifiedrq   rd  rW  r%   r  r  r  rY   s    r0   test_isVerifiedz%BaseSSHTransportTests.test_isVerified  s    	..t44555..u55666..v66777(2
%
--d33444
--e44555
--f55666(1(<Wgw)
 )

% 	..t44555..u55666..v66777)TZ%:EBBBBBr/   c                 R   dgfd}|| j         _        | j                                         |                     | j        d         d         t           j                   |                     | j        d         d         dd         t          t           j        f                     dS )zh
        Test that loseConnection sends a disconnect message and closes the
        connection.
        Fc                      d d<   d S r  r.   r  s   r0   r  zEBaseSSHTransportTests.test_loseConnection.<locals>.stubLoseConnection9  r  r/   r   rt   r   r   N)r%   r  r  rH  r  r  r   DISCONNECT_CONNECTION_LOSTr  s     @r0   test_loseConnectionz)BaseSSHTransportTests.test_loseConnection2  s    
 w	# 	# 	# 	# 	# );%
!!###a+Y-EFFFLOAqs#UI,P+R%S%S	
 	
 	
 	
 	
r/   c                 T      fd} |d            |d            |d           dS )zU
        Test that the transport disconnects when it receives a bad version.
        c                    g _         dj        _        dgfd}|j        _        t          | dz             D ]}j                            |                               d                                        j         d         d         t          j	                                       j         d         d         dd         t          t          j        f                     d S )NFc                      d d<   d S r  r.   r  s   r0   r  zRBaseSSHTransportTests.test_badVersion.<locals>.testBad.<locals>.stubLoseConnectionM  s    "&Qr/   r=  r   rt   r   r   )r  r  r  r%   r  r   r  rW  rH  r  r   )DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED)rP  r  rE  r  r?   s      @r0   testBadz6BaseSSHTransportTests.test_badVersion.<locals>.testBadH  s    DL$)DJ!!7L' ' ' ' ' -?DN)w011 + +
''****OOLO,,,T\!_Q/1IJJJQ"1Q3'yJLMM    r/   s   SSH-1.5-OpenSSHs   SSH-3.0-Twisteds   GET / HTTP/1.1Nr.   )r?   r  s   ` r0   test_badVersionz%BaseSSHTransportTests.test_badVersionC  sW    
	 	 	 	 	$ 	"###"###!"""""r/   c                 4   t                                          t          j                               dj        z   dz   }fdt          |          D              |                     j                   |                     j	        j                   dS )zV
        Test that the transport ignores data sent before the version string.
        s5   here's some stuff beforehand
here's some other stuff
r=  c                 :    g | ]}                     |          S r.   )r  )r   rE  r  s     r0   
<listcomp>z@BaseSSHTransportTests.test_dataBeforeVersion.<locals>.<listcomp>k  s'    8881		A		888r/   N)
rQ   r  r   r
  r  r   rW  r  rH  r  )r?   datar  s     @r0   test_dataBeforeVersionz,BaseSSHTransportTests.test_dataBeforeVersion^  s     "##]:<<=== $% 	 	 	9888	$8888()))153IJJJJJr/   c                    t                      }|                    t          j                               |                    d           |                     |j                   |                     |j        d           dS )zw
        Test that the transport treats the compatibility version (1.99)
        as equivalent to version 2.0.
        s   SSH-1.99-OpenSSH
s   SSH-1.99-OpenSSHN	rQ   r  r   r
  r  rW  r  rH  r  r  s     r0   test_compatabilityVersionz/BaseSSHTransportTests.test_compatabilityVersiono  sv    
 "##]:<<===0111()))13FGGGGGr/   c                    t                      }|                    t          j                               |                    d           |                     |j                   |                     |j        d           dS )z
        It can parse the SSH version string even when it ends only in
        Unix newlines (CR) and does not follows the RFC 4253 to use
        network newlines (CR LF).
        s,   SSH-2.0-PoorSSHD Some-comment here
more-datas"   SSH-2.0-PoorSSHD Some-comment hereNr  rY  s     r0   &test_dataReceivedSSHVersionUnixNewlinez<BaseSSHTransportTests.test_dataReceivedSSHVersionUnixNewlinez  su      !!=8::;;;MNNN'''/1VWWWWWr/   c                    t                      }|                    t          j                               |                    d           |                     |j                   |                     |j        d           dS )a9  
        The trailing spaces from SSH version comment are not removed.

        The SSH version string needs to be kept as received
        (without CR LF end of line) as they are used in the host
        authentication process.

        This can happen with a Bitvise SSH server which hides its version.
        s>   SSH-2.0-9.99 FlowSsh: Bitvise SSH Server (WinSSHD) 
more-datas3   SSH-2.0-9.99 FlowSsh: Bitvise SSH Server (WinSSHD) Nr  rY  s     r0   )test_dataReceivedSSHVersionTrailingSpacesz?BaseSSHTransportTests.test_dataReceivedSSHVersionTrailingSpaces  s      !!=8::;;;S	
 	
 	
 	'''"B	
 	
 	
 	
 	
r/   c                     t                      }d|_        |                    t          j                               |                    d           |                     |j                   dS )z
        If an unusual SSH version is received and is included in
        C{supportedVersions}, an unsupported version error is not emitted.
        )   9.99   SSH-9.99-OpenSSH
N)rQ   supportedVersionsr  r   r
  r  rU  rX   r  s     r0    test_supportedVersionsAreAllowedz6BaseSSHTransportTests.test_supportedVersionsAreAllowed  sf    
 "##",]:<<===0111455555r/   c                     t                      }d|_        |                    t          j                               |                    d           |                     d|j                   dS )z
        If an unusual SSH version is received and is not included in
        C{supportedVersions}, an unsupported version error is emitted.
        )s   2.0r   r  N)rQ   r!  r  r   r
  r  rH  rX   r  s     r0   6test_unsupportedVersionsCallUnsupportedVersionReceivedzLBaseSSHTransportTests.test_unsupportedVersionsCallUnsupportedVersionReceived  sh    
 "##"+]:<<===0111%"=>>>>>r/   c                     t           j        f fd	} |d            |d            j        j        }t	                       j        _         |dt           j                   d  j        j        _         |d           | j        _        t                       j        _        d }| j        j        _	         |dt           j
                                                     d	S )
zi
        Test that the transport disconnects with an error when it receives
        bad packets.
        c                    g _         | j        _                            j                                                                       t          j                   d                               j         d         d         t          j                                       j         d         d         dd         t          |f                     d S )Nrt   r   r   r   )
r  r  ro  ry  rp  rH  rx   r%   r  r   )rm   errorr?   s     r0   r  z6BaseSSHTransportTests.test_badPackets.<locals>.testBad  s    DL#DJNdj2244555S..222T\!_Q/1IJJJT\!_Q/!4eUHooFFFFFr/   s   s	       BCDEs      AB123456c                     | d d         S r   r.   )rL   s    r0   rt  z7BaseSSHTransportTests.test_badPackets.<locals>.<lambda>  s    !CRC& r/   s      BCDEFGHIJKc                      t          d          )Nzbad compression)	Exception)r   s    r0   stubDecompressz=BaseSSHTransportTests.test_badPackets.<locals>.stubDecompress  s    -...r/   s	       BCDEN)r%   DISCONNECT_PROTOCOL_ERRORr  rd  rq   DISCONNECT_MAC_ERRORr   r   r}  r   DISCONNECT_COMPRESSION_ERRORflushLoggedErrors)r?   r  oldEncryptionsr+  s   `   r0   test_badPacketsz%BaseSSHTransportTests.test_badPackets  s    #,"E 	G 	G 	G 	G 	G 	G 	+,,,6(2
%+Y-K	
 	
 	
 1A0@
%-1222(6
%)8):):
&	/ 	/ 	/ 5C
&1'2	
 	
 	
 	     r/   c                 
     j         j        }|f fd	} j                             dd            |             dt          j        d<    j                             dd            |              j                             dd            |              j                             t                                  j                             dd            |              j                             dd            |             d	S )
zj
        Test that unimplemented packet types cause MSG_UNIMPLEMENTED packets
        to be sent.
        c                                         j        d         d         t          j                                        j        d         d         dd         t	          | f                     g j        _        | dz  } d S )Nr   rt   r   r   )rH  r  r%   r  r   r  )rd   r?   s    r0   checkUnimplementedzKBaseSSHTransportTests.test_unimplementedPackets.<locals>.checkUnimplemented  sr    T\!_Q/1LMMMT\!_Q/!4eVI6F6FGGG!#DJaKFFFr/   (   r/   s   MSG_fiction)   <   F   r   N)r  r   r  r%   messagesr  r   )r?   rd   r4  s   `  r0   test_unimplementedPacketsz/BaseSSHTransportTests.test_unimplementedPackets  s"   
 2&, 	 	 	 	 	 	 	
""2s+++!/	2
""2s+++
""2s+++
kmm,,,
""2s+++
""2s+++r/   c                 2   | j         }|                    | j                                                   t	                      |_        t                      |_        t                      |_        |	                    t                                 t                      }|                    t          j                               |                    d           |                     |j        |j                   |                     |j        |j                   |                     |j        |j                   |                     |j        |j                   |                     |j        |j                   |                     |j        |j                   dS )zD
        Test that multiple instances have distinct states.
        r/   N)r  r  r%   rF  rq   rd  r   rj  r}  r  r   rQ   r  r   r
  r  assertNotEqualr  r   r   r   )r?   r  proto2s      r0   test_multipleClassesz*BaseSSHTransportTests.test_multipleClasses  sT    
4>//11222#-<< $3$5$5!$3$5$5!'''"$$m;==>>>#E,f.?@@@EOV-=>>>E8&:WXXXE8&:WXXXE4f6OPPPEM6>:::::r/   N)4r+   r,   r-   ro   r   rE   r"   rA   r  rS  r[  ra  rg  rk  rm  rq  r{  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  r$  r1  r:  r>  r.   r/   r0   r5  r5    s        
   	
&)2
3
3	4
&)J

	  &)M
"
"	# &)M
"
"		#
 &)L
!
!	" &)L
!
!	" &)G

	 &)G

	 &)C..		 &)C..
	 	 	  @ @ @.X X X,P P P(
 
 
:
 
 
 
 
 
>. . .) ) )(5 5 58 8 8 B B B/+ /+ /+b+ + +
D 
D 
D@ @ @"B "B "BH
 
 
4+ + +
 
 
*
 
 

 
 

 
 
9 9 9
 
 
; ; ;) ) )2) ) )"$ $ $ * * *() ) )D D D(C C C(
 
 
"# # #6K K K"	H 	H 	HX X X
 
 
.	6 	6 	6	? 	? 	?"! "! "!H  4; ; ; ; ;r/   r5  c                       e Zd ZdZd ZdS )'BaseSSHTransportDHGroupExchangeBaseCasez@
    Diffie-Hellman group exchange tests for TransportBase.
    c                 (   | j         | j        _        d| j        _        |                     d| j        j        z                                             }|                     d|z                                             }|                     d|z   |z                                             }|                     d|z   |z   |z                                             }|                     | j                            ddd          ||z   |z   |z              dS )z?
        Test that _getKey generates the correct keys.
           EFs   ABCDKrw     K   AB   CDN)r'  r  kexAlg	sessionIDr(  digestrH  _getKey)r?   k1k2k3k4s        r0   test_getKeyz3BaseSSHTransportDHGroupExchangeBaseCase.test_getKey  s     !-
$
 4tz7K KLLSSUU"--4466"r 12299;;"r 1B 677>>@@++D%??b2PRARSSSSSr/   N)r+   r,   r-   ro   rN  r.   r/   r0   r@  r@    s2         T T T T Tr/   r@  c                       e Zd ZdZdS )(BaseSSHTransportDHGroupExchangeSHA1TestszE
    diffie-hellman-group-exchange-sha1 tests for TransportBase.
    Nr+   r,   r-   ro   r.   r/   r0   rP  rP               r/   rP  c                       e Zd ZdZdS )*BaseSSHTransportDHGroupExchangeSHA256TestszG
    diffie-hellman-group-exchange-sha256 tests for TransportBase.
    NrQ  r.   r/   r0   rT  rT  #          
   r/   rT  c                       e Zd ZdZdS )"BaseSSHTransportEllipticCurveTestsz4
    ecdh-sha2-nistp256 tests for TransportBase
    NrQ  r.   r/   r0   rW  rW  -  rR  r/   rW  c                       e Zd ZdZdS )%BaseSSHTransportCurve25519SHA256Testsz3
    curve25519-sha256 tests for TransportBase
    NrQ  r.   r/   r0   rY  rY  5             r/   rY  c                   L    e Zd ZdZddZddZd Zd Zd Zd Z	d	 Z
d
 Zd ZdS )#ServerAndClientSSHTransportBaseCasezF
    Tests that need to be run on both the server and the client.
    Nc                     |t           j        }|                     | j        d         d         t           j                   |                     | j        d         d         dd         t          |f                     dS )zI
        Helper function to check if the transport disconnected.
        Nr   r   rt   r   r   )r%   r,  rH  r  r  r   r   s     r0   checkDisconnectedz5ServerAndClientSSHTransportBaseCase.checkDisconnectedC  so     <6Db)!,i.FGGGb)!,QqS15$>>BBBBBr/   c                 <   |t           j        }|                                 } ||           |                    t	          j                               | j                            |j                                                    |r| 	                    |           |S )zy
        Helper function to connect a modified protocol to the test protocol
        and test for disconnection.
        )
r%   DISCONNECT_KEY_EXCHANGE_FAILEDr   r  r   r
  r  r  rF  r^  )r?   protoModificationr   r=  s       r0   connectModifiedProtocolz;ServerAndClientSSHTransportBaseCase.connectModifiedProtocolL  s    
 <;D&!!!m;==>>>
 0 6 6 8 8999 	)""4(((r/   c                 6    d }|                      |           dS )z`
        Test that the transport disconnects if it can't match the key
        exchange
        c                     g | _         d S r  r  r=  s    r0   blankKeyExchangesz\ServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchKex.<locals>.blankKeyExchangesa      +-F(((r/   Nrb  )r?   rg  s     r0   test_disconnectIfCantMatchKexzAServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchKex[  s.    	. 	. 	. 	$$%677777r/   c                 6    d }|                      |           dS )zP
        Like test_disconnectIfCantMatchKex, but for the key algorithm.
        c                     g | _         d S r  )r  rf  s    r0   blankPublicKeysz]ServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchKeyAlg.<locals>.blankPublicKeysk  s    )+F&&&r/   Nri  )r?   rm  s     r0    test_disconnectIfCantMatchKeyAlgzDServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchKeyAlgf  s-    
	, 	, 	, 	$$_55555r/   c                 6    d }|                      |           dS )zN
        Like test_disconnectIfCantMatchKex, but for the compression.
        c                     g | _         d S r  r  rf  s    r0   blankCompressionszdServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchCompression.<locals>.blankCompressionsu  rh  r/   Nri  )r?   rr  s     r0   %test_disconnectIfCantMatchCompressionzIServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchCompressionp  s.    
	. 	. 	. 	$$%677777r/   c                 6    d }|                      |           dS )zM
        Like test_disconnectIfCantMatchKex, but for the encryption.
        c                     g | _         d S r  r  rf  s    r0   blankCipherszZServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchCipher.<locals>.blankCiphers  s    &(F###r/   Nri  )r?   rw  s     r0    test_disconnectIfCantMatchCipherzDServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchCipherz  s-    
	) 	) 	) 	$$\22222r/   c                 6    d }|                      |           dS )zF
        Like test_disconnectIfCantMatchKex, but for the MAC.
        c                     g | _         d S r  r  rf  s    r0   	blankMACszTServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchMAC.<locals>.blankMACs  s    #%F   r/   Nri  )r?   r|  s     r0   test_disconnectIfCantMatchMACzAServerAndClientSSHTransportBaseCase.test_disconnectIfCantMatchMAC  s-    
	& 	& 	& 	$$Y/////r/   c                     |                      | j                                        t          j        | j        j                                                             dS )z
        Test that the transport's L{getPeer} method returns an
        L{SSHTransportAddress} with the L{IAddress} of the peer.
        N)rH  r  getPeerr   SSHTransportAddressr%   rY   s    r0   test_getPeerz0ServerAndClientSSHTransportBaseCase.test_getPeer  V    
 	J  '
(<(D(D(F(FGG	
 	
 	
 	
 	
r/   c                     |                      | j                                        t          j        | j        j                                                             dS )z
        Test that the transport's L{getHost} method returns an
        L{SSHTransportAddress} with the L{IAddress} of the host.
        N)rH  r  getHostr   r  r%   rY   s    r0   test_getHostz0ServerAndClientSSHTransportBaseCase.test_getHost  r  r/   r  )r+   r,   r-   ro   r^  rb  rj  rn  rs  rx  r}  r  r  r.   r/   r0   r\  r\  >  s         C C C C   	8 	8 	86 6 68 8 83 3 30 0 0
 
 

 
 
 
 
r/   r\  c                   Z    e Zd ZU dZej        Zeeej	                          e
d<   d Zd ZdS )ServerSSHTransportBaseCasez1
    Base case for SSHServerTransport tests.
    r   c                     t                               |            t                      | j        _        | j        j                                         d S r  )r   r  r   r  r#   startFactoryrY   s    r0   r  z ServerSSHTransportBaseCase.setUp  sA    %%%(]]

'')))))r/   c                     t                               |            | j        j                                         | j        `d S r  )r   tearDownr  r#   stopFactoryrY   s    r0   r  z#ServerSSHTransportBaseCase.tearDown  s;    ""4(((
&&(((Jr/   N)r+   r,   r-   ro   r%   r5   r   r
   r   r1   r#  r  r  r.   r/   r0   r  r    sa           9B8TE8D345TTT* * *
    r/   r  c                   ~    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )ServerSSHTransportTestsz'
    Tests for SSHServerTransport.
    c                 R   t           j                            t          j                  t           j                            t          j                  t           j                            t          j                  t           j                            t          j                  d| j        j	        _
        t           j                            t          j                  t           j                            t          j                  t           j                            t          j                  t           j                            t          j                  d| j        j	        _        |                     | j                            d          | j        j	        j
        d         | j        j	        j        d         f           |                     | j                            d          | j        j	        j
        d         | j        j	        j        d         f           |                     | j                            d          | j        j	        j
        d         | j        j	        j        d         f           |                     | j                            d          | j        j	        j
        d         | j        j	        j        d         f           |                     | j                            d          | j        j	        j
        d         | j        j	        j        d         f           |                     | j                            d          | j        j	        j
        d         | j        j	        j        d         f           |                     t&          | j        j        d           d	S )
z
        L{transport.SSHServerTransport._getHostKeys} returns host keys from
        the factory, looked up by public key signature algorithm.
        )r      ssh-dss   ecdsa-sha2-nistp256   ssh-ed25519r      rsa-sha2-256   rsa-sha2-512r  r  r  s   ecdsa-sha2-nistp384N)r$   r   r   r&   r   r   publicECDSA_opensshpublicEd25519_opensshr  r#   
publicKeysr   r   privateECDSA_opensshprivateEd25519_openssh_newprivateKeysrH  _getHostKeysr  KeyErrorrY   s    r0   test__getHostKeysz)ServerSSHTransportTests.test__getHostKeys  s    ++G,EFF++G,EFF$(H$7$78S$T$T H//0MNN	)
 )

% ++G,FGG++G,FGG$(H$7$78T$U$U H//0RSS	*
 *

& 	J##J//
"-j9
".z:	
 	
 	
 	J##O44
"-j9
".z:	
 	
 	
 	J##O44
"-j9
".z:	
 	
 	
 	J##J//
"-j9
".z:	
 	
 	
 	J##$:;;
"-.DE
"./EF	
 	
 	
 	J##N33
"-n=
".~>	
 	
 	
 	(DJ$;=STTTTTr/   c                 h   | j                             d           |                     | j         j        d           |                     | j         j        d           |                     | j         j        d           |                     | j         j        d           |                     | j         j                   | j         j	        }|                     |j
        d           |                     |j        d           |                     |j        d           |                     |j        d           dS )z
        Receiving a KEXINIT packet listing multiple supported algorithms will
        set up the first common algorithm found in the client's preference
        list.
        s	  SSH-2.0-Twisted
     bdiffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256   ssh-dss,ssh-rsa   aes128-ctr,aes128-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc,cast128-ctr,cast128-cbc,blowfish-ctr,blowfish-cbc,3des-ctr,3des-cbc   aes128-ctr,aes128-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc,cast128-ctr,cast128-cbc,blowfish-ctr,blowfish-cbc,3des-ctr,3des-cbc   hmac-md5,hmac-sha1   hmac-md5,hmac-sha1   	none,zlib   	none,zlib             r&  r  r9  s
   aes128-ctr   hmac-md5N)r  r  rH  rF  keyAlgoutgoingCompressionTypeincomingCompressionTyperU  r  r  r   r   r   r   r?   nes     r0   test_KEXINITMultipleAlgorithmsz6ServerSSHTransportTests.test_KEXINITMultipleAlgorithms  s    	
(	
 	
 	
$ 	*,QRRR*J777;WEEE;WEEE;<<<Z'666}555444{33333r/   c                 l   dt          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   dz   d	z   }| j                            |           |                     | j        j        d
           |                     | j        j                   dS )z
        If the client sends "ext-info-c" in its key exchange algorithms,
        then the server notes that the client supports extension
        negotiation.  See RFC 8308, section 2.1.
        r  s/   diffie-hellman-group-exchange-sha256,ext-info-cr   r7  r8  r9  r/   r:  r;  r+  N)r"   rA   r  r  rH  rF  rW  r  r?   kexInitPackets     r0    test_KEXINITExtensionNegotiationz8ServerSSHTransportTests.test_KEXINITExtensionNegotiation  s3    iJKKLi
##$ i&&' i&&	'
 i%%& i%%& i  ! i  ! inn	 inn
  "" 	 	
}---*,STTT
:;;;;;r/   c                    dd                     d d | j        j        ddd         | j        j        | j        j        | j        j        | j        j        | j        j        | j        j        | j        j        | j        j        | j        j        f
D             D                       z   dz   }| j                            |           | 	                    | j        j
                   | j                            d           | 	                    | j        j
                   | j                            d	           |                     | j        j
                   |                     | j        g            d
| j        _
        | j                            d           |                     | j        j
                   |                     | j        g            dS )a<  
        The client is allowed to send a guessed key exchange packet
        after it sends the KEXINIT packet.  However, if the key exchanges
        do not match, that guess packet must be ignored.  This tests that
        the packet is ignored in the case of the key exchange method not
        matching.
        r  r/   c                 6    g | ]}t          j        |          S r.   r"   rA   r   s     r0   r  zEServerSSHTransportTests.test_ignoreGuessPacketKex.<locals>.<listcomp>?  0        	!  r/   c                 8    g | ]}d                      |          S r  rL  r   rM   s     r0   r  zEServerSSHTransportTests.test_ignoreGuessPacketKex.<locals>.<listcomp>A  2     " " " ! !IIaLL" " "r/   Nr                test          T            rL  r  r  r  r  r  r  r  r  rW  ignoreNextPacket	ssh_DEBUGssh_KEX_DH_GEX_REQUEST_OLDrU  rH  r  ssh_KEX_DH_GEX_REQUESTr  s     r0   test_ignoreGuessPacketKexz1ServerSSHTransportTests.test_ignoreGuessPacketKex3  s     " " !%
 @2 F $
 > $
 ; $
 ; $
 8 $
 8 $
 @ $
 @ $
 = $
 =&" " "   . '/( 	4 	
}---
3444
HIII
3444
--.ABBB4555r***&*
#
))*ABBB4555r*****r/   c                    dd                     d d | j        j        | j        j        ddd         | j        j        | j        j        | j        j        | j        j        | j        j        | j        j        | j        j        | j        j        f
D             D                       z   dz   }| j                            |           | 	                    | j        j
                   | j                            d           | 	                    | j        j
                   | j                            d	           |                     | j        j
                   |                     | j        g            d
| j        _
        | j                            d           |                     | j        j
                   |                     | j        g            dS )zk
        Like test_ignoreGuessPacketKex, but for an incorrectly guessed
        public key format.
        r  r/   c                 6    g | ]}t          j        |          S r.   r  r   s     r0   r  zEServerSSHTransportTests.test_ignoreGuessPacketKey.<locals>.<listcomp>l  r  r/   c                 8    g | ]}d                      |          S r  r  r  s     r0   r  zEServerSSHTransportTests.test_ignoreGuessPacketKey.<locals>.<listcomp>n  r  r/   Nr   r  r  r  Tr  r  r  s     r0   test_ignoreGuessPacketKeyz1ServerSSHTransportTests.test_ignoreGuessPacketKeyc  s     " " !%
 @ $
 >ttt D $
 ; $
 ; $
 8 $
 8 $
 @ $
 @ $
 = $
 =&" " "   . '/( 	4 	
}---
3444
HIII
3444
--.ABBB4555r***&*
#
))*ABBB4555r*****r/   c                    |g| j         _        |g| j         _        | j                             | j                                                   | j                             |          \  }}t          j        |          \  }}t          |d|          }| j         
                    t          j        |                     t          j        t          j        d|dz  z                      d         }	t          | j         j        |	| j         j                  }
|                     | j         j        |
           t          ||	| j         j                  }t)                      }|                    t          j        | j         j                  dz             |                    t          j        | j         j                  dz             |                    t          j        |                                                     |                    t          j        |                     |                    |
           |                    |           |                                }|                    ||          }|                     | j        t          j        t          j        |                                          |
z   t          j        |          z   ft          j        dfg           dS )	a  
        Test that the KEXDH_INIT packet causes the server to send a
        KEXDH_REPLY with the server's public key and a signature.

        @param kexAlgorithm: The key exchange algorithm to use.
        @type kexAlgorithm: L{bytes}

        @param keyAlgorithm: The public key signature algorithm to use.
        @type keyAlgorithm: L{bytes}

        @param bits: The bit length of the DH modulus.
        @type bits: L{int}
        i  r     r   r  signatureTyper/   N)r  r  r  r  r%   rF  r  r   r   rK   r  r"   rJ   getMPrA   rO   r   r   rH  r   r   updater  ourKexInitPayloadr   rH  signr  MSG_KEXDH_REPLYMSG_NEWKEYS)r?   r'  keyAlgorithmr   
pubHostKeyprivHostKeyr   r   erM   rG   r   hr!  	signatures                  r0   assertKexDHInitResponsez/ServerSSHTransportTests.assertKexDHInitResponse  sW    -9>
(*6
&
 4 4 6 6777"&*"9"9,"G"G
K*<8814OO
--fill;;;L7dai#899::1=4:<DJL117;;;aDJL11FF	4:677!;<<<	4:7881<===	:??,,--...	1		xxzz$$\$NN	L -Ijoo//0014vy7K7KK &,		
 		
 		
 		
 		
r/   c                     d| j         _        d| j         _        |                     t          | j         j        t          j        d                     dS )
        Test that if the server receives a KEX_DH_GEX_REQUEST_OLD message
        and the key exchange algorithm is not set, we raise a ConchError.
        s	   bad-curver   s
   unused-keyN)r  rF  r  r  r   _ssh_KEX_ECDH_INITr"   rA   rY   s    r0   %test_checkBad_KEX_ECDH_INIT_CurveNamez=ServerSSHTransportTests.test_checkBad_KEX_ECDH_INIT_CurveName  sR    
 )
&
 J)Im$$	
 	
 	
 	
 	
r/   c                 V   dt          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   dz   d	z   }| j                            |           |                     t
                     |                     t                     d
S )zy
        Test that if the server received a bad name for a curve
        we raise an UnsupportedAlgorithm error.
        r6  r/  r   r7  r8  r9  r/   r:  r;  N)r"   rA   r  r  r  AttributeErrorr   )r?   kexmsgs     r0    test_checkBad_KEX_INIT_CurveNamez8ServerSSHTransportTests.test_checkBad_KEX_INIT_CurveName  s(    i-../i
##$ i&&' i&&	'
 i%%& i%%& i  ! i  ! inn	 inn
  "" 	  	
v&&&.)))./////r/   c                 4    |                      ddd           dS )z
        KEXDH_INIT messages are processed when the
        diffie-hellman-group14-sha1 key exchange algorithm and the ssh-rsa
        public key signature algorithm are requested.
        r   r   r   Nr  rY   s    r0   test_KEXDH_INIT_GROUP14z/ServerSSHTransportTests.test_KEXDH_INIT_GROUP14  s$     	$$%CZQUVVVVVr/   c                 4    |                      ddd           dS )
        KEXDH_INIT messages are processed when the
        diffie-hellman-group14-sha1 key exchange algorithm and the
        rsa-sha2-256 public key signature algorithm are requested.
        r   r  r   Nr  rY   s    r0   $test_KEXDH_INIT_GROUP14_rsa_sha2_256z<ServerSSHTransportTests.test_KEXDH_INIT_GROUP14_rsa_sha2_256  -     	$$*OT	
 	
 	
 	
 	
r/   c                 4    |                      ddd           dS )r  r   r  r   Nr  rY   s    r0   $test_KEXDH_INIT_GROUP14_rsa_sha2_512z<ServerSSHTransportTests.test_KEXDH_INIT_GROUP14_rsa_sha2_512  r  r/   c           
      V    d j         _        t                       j         _                             dd                                 j         j        d                                dd                                 j         j        d                                 j        d         t          j	        df            fdt          d          D             }                      j         j        j        |d	         |d
         |d         |d         |d         |d         f           dS )G
        Test that _keySetup sets up the next encryption keys.
        r   rD  rE  rB  r   r/   c                 H    g | ]}j                             |d d          S rD  rB  r  rI  r   rE  r?   s     r0   r  z9ServerSSHTransportTests.test_keySetup.<locals>.<listcomp>  -    UUU14:%%a66UUUr/      ABCDEFrt   r   r   r  r   r   Nr  rF  rq   r  r"  rH  rG  r  r%   r  r   r$   r?   newKeyss   ` r0   test_keySetupz%ServerSSHTransportTests.test_keySetup      ;
%/\\
"  ...-u555  ...-u555b)I,A3+GHHHUUUU	)@T@TUUUJ&+QZWQZWQZQRT	
 	
 	
 	
 	
r/   c                 <   g d| j         _        d| j         _        t                      | j         _        d| j         _        |                     dd           |                     | j        d         t          j
        df           |                     | j        d         t          j        d	t          j        d
          z   t          j        d          z   f           |                     dd           |                     | j        d         t          j
        df           dS )a  
        If the client advertised support for extension negotiation, then
        _keySetup sends SSH_MSG_EXT_INFO with the "server-sig-algs"
        extension as the next packet following the server's first
        SSH_MSG_NEWKEYS.  See RFC 8308, sections 2.4 and 3.1.
        )r   r  r  r   TrD  rE  r/   r   s      r  r  rB  N)r  r  rF  rq   r  r  r"  rH  r  r%   r  r  r"   rA   rY   s    r0   test_keySetupWithExtInfoz0ServerSSHTransportTests.test_keySetupWithExtInfo  s    *X)W)W
&:
%/\\
"-1
*  ...b)I,A3+GHHHL&#).//0)@AAB	
 	
 	
 	  ...b)I,A3+GHHHHHr/   c           
      V    d j         _        t                       j         _                             dd                                 j         j        d                                dd                                 j         j        d                                 j        d         t          j	        df            fdt          d          D             }                      j         j        j        |d	         |d
         |d         |d         |d         |d         f           dS )r  r/  rD  rE  rB  r   r/   c                 H    g | ]}j                             |d d          S r  r  r  s     r0   r  z>ServerSSHTransportTests.test_ECDH_keySetup.<locals>.<listcomp>9  r  r/   r  rt   r   r   r  r   r   Nr  r  s   ` r0   test_ECDH_keySetupz*ServerSSHTransportTests.test_ECDH_keySetup.  s    2
%/\\
"  ...-u555  ...-u555b)I,A3+GHHHUUUU	)@T@TUUUJ&+QZWQZWQZQRT	
 	
 	
 	
 	
r/   c                    |                                   t          j        dddd          | j        _        | j                            d           |                     | j        j        | j        j                   |                     | j        j	                   |                     | j        j
                   d| j        _        |                     dd           | j                            d           |                     | j        j	                   d| j        _        |                     dd           | j                            d           |                     | j        j
                   dS )zj
        Test that NEWKEYS transitions the keys in nextEncryptions to
        currentEncryptions.
        r9  r/      zlibrD  rE  rB  N)r  r%   r  r  r  ssh_NEWKEYSassertIsrd  ry  rj  r}  r  r"  assertIsNotNoner  rY   s    r0   test_NEWKEYSz$ServerSSHTransportTests.test_NEWKEYS?  sB   
 	++---%.%9Wgw&
 &

" 	
s###dj3TZ5OPPP$*8999$*8999-4
*  ...
s###TZ;<<<-4
*  ...
s###TZ;<<<<<r/   c                    | j                             t          j        d                     |                     | j        t          j        t          j        d          fg           |                     | j         j        j	        d           dS )z^
        Test that the SERVICE_REQUEST message requests and starts a
        service.
        r   r   N)
r  ssh_SERVICE_REQUESTr"   rA   rH  r  r%   MSG_SERVICE_ACCEPTr   r   rY   s    r0   test_SERVICE_REQUESTz,ServerSSHTransportTests.test_SERVICE_REQUESTV  s~    
 	
&&vy'A'ABBBLI8&)O:T:TUV	
 	
 	
 	+0.AAAAAr/   c                 b    | j                             d           |                                  dS zD
        Test that NEWKEYS disconnects if it receives data.
        s
   bad packetNr  r  r^  rY   s    r0   test_disconnectNEWKEYSDataz2ServerSSHTransportTests.test_disconnectNEWKEYSDataa  2     	
}---     r/   c                     | j                             t          j        d                     |                     t
          j                   dS )zd
        Test that SERVICE_REQUESTS disconnects if an unknown service is
        requested.
        s
   no serviceN)r  r  r"   rA   r^  r%    DISCONNECT_SERVICE_NOT_AVAILABLErY   s    r0   (test_disconnectSERVICE_REQUESTBadServicez@ServerSSHTransportTests.test_disconnectSERVICE_REQUESTBadServiceh  sA    
 	
&&vy'?'?@@@yIJJJJJr/   N)r+   r,   r-   ro   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r.   r/   r0   r  r    sB        ;U ;U ;Uz!4 !4 !4F< < <2.+ .+ .+`++ ++ ++Z0
 0
 0
d
 
 
0 0 02W W W
 
 

 
 

 
 
"I I I2
 
 
"= = =.	B 	B 	B! ! !K K K K Kr/   r  c                   8    e Zd ZdZd Zd Zd
dZd Zd Zd Z	d	S ))ServerSSHTransportDHGroupExchangeBaseCasezE
    Diffie-Hellman group exchange tests for SSHServerTransport.
    c                 H   | j         g| j        _        dg| j        _        | j                            | j                                                   | j                            d           | j        j        	                                
                    d          d         \  }}|                     | j        t
          j        t          j        |          dz   fg           |                     | j        j        d           |                     | j        j        |           dS )z
        Test that the KEX_DH_GEX_REQUEST_OLD message causes the server
        to reply with a KEX_DH_GEX_GROUP message with the correct
        Diffie-Hellman group.
        r         r   r         r  N)r'  r  r  r  r  r%   rF  r  r#   r   getrH  r  MSG_KEX_DH_GEX_GROUPr"   rJ   r   r   )r?   dhGeneratordhPrimes      r0   test_KEX_DH_GEX_REQUEST_OLDzEServerSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_REQUEST_OLDv  s    -1,=+>
(*4
&
 4 4 6 6777
--.ABBB#z1;;==AA$GGJWL 2Ig&&)@@	
 	
 	
 	q)))w/////r/   c                 j    d| j         _        |                     t          | j         j        d           dS )r  N)r  rF  r  r   r  rY   s    r0   %test_KEX_DH_GEX_REQUEST_OLD_badKexAlgzOServerSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_REQUEST_OLD_badKexAlg  s1    
 !
*dj&KTRRRRRr/   r   c                 H   | j         g| j        _        |g| j        _        | j                            | j                                                   | j                            d           | j        j        	                                
                    d          d         \  }}|                     | j        t
          j        t          j        |          dz   fg           |                     | j        j        d           |                     | j        j        |           dS )z
        Test that the KEX_DH_GEX_REQUEST message causes the server to reply
        with a KEX_DH_GEX_GROUP message with the correct Diffie-Hellman
        group.
                    r   r   r  r  N)r'  r  r  r  r  r%   rF  r  r#   r   r  rH  r  r  r"   rJ   r   r   )r?   r  r  r  s       r0   test_KEX_DH_GEX_REQUESTzAServerSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_REQUEST  s    -1,=+>
(*6
&
 4 4 6 6777
))E	
 	
 	
  $z1;;==AA$GGJWL 2Ig&&)@@	
 	
 	
 	q)))w/////r/   c           
         |                                   | j                            d          \  }}t          | j        j        d| j        j                  }t          j        d          d         }|                     | j        j	        
                                j        |           t          | j        j        || j        j                  }|                     | j        j        |           t          ||| j        j                  }|                                 }|                    t          j        | j        j                  dz             |                    t          j        | j        j                  dz             |                    t          j        |                                                     |                    d           |                    t          j        | j        j                             |                    t          j        | j        j                             |                    t          j        |                     |                    |           |                    |           |                                }| j                            t          j        |                     |                     | j        dd         t0          j        t          j        |                                          |z   t          j        |                    |                    z   ft0          j        d	fg           dS )
z
        Test that the KEX_DH_GEX_INIT message after the client sends
        KEX_DH_GEX_REQUEST_OLD causes the server to send a KEX_DH_GEX_INIT
        message with a public key and signature.
        r   r   s     r   r  r  rt   Nr/   )r  r  r  rK   r   r   r"   r  rH  r   private_numbersrL   rO   r   r(  r  rA   r  r  r   rJ   rH  ssh_KEX_DH_GEX_INITr  r%   MSG_KEX_DH_GEX_REPLYr  r  	r?   r  r  r  rM   rG   r   r  r!  s	            r0   &test_KEX_DH_GEX_INIT_after_REQUEST_OLDzPServerSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_INIT_after_REQUEST_OLD  s    	((***"&*"9"9*"E"E
K
a..L<==a@/??AACQGGG4:<DJL117;;;aDJL11  	4:677!;<<<	4:7881<===	:??,,--...	$%%%	4:<(()))	4:<(()))	1		xxzz
&&vy||444L 2Ijoo//00i 0 0 > >??@ &,	
 	
 	
 	
 	
r/   c           
         |                                   | j                            d          \  }}t          | j        j        d| j        j                  }t          j        d          d         }t          | j        j        || j        j                  }t          ||| j        j                  }| 	                                }|
                    t          j        | j        j                  dz             |
                    t          j        | j        j                  dz             |
                    t          j        |                                                     |
                    d           |
                    t          j        | j        j                             |
                    t          j        | j        j                             |
                    t          j        |                     |
                    |           |
                    |           |                                }| j                            t          j        |                     |                     | j        d         t(          j        t          j        |                                          |z   t          j        |                    |                    z   f           dS )	z
        Test that the KEX_DH_GEX_INIT message after the client sends
        KEX_DH_GEX_REQUEST causes the server to send a KEX_DH_GEX_INIT message
        with a public key and signature.
        r   r        r   r  r  rt   Nr  r  r  rK   r   r   r"   r  rO   r(  r  rA   r  r  r   rJ   rH  r  rH  r  r%   r  r  r  s	            r0   "test_KEX_DH_GEX_INIT_after_REQUESTzLServerSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_INIT_after_REQUEST  s=    	$$&&&"&*"9"9*"E"E
K
a..L<==a@4:<DJL11aDJL11  	4:677!;<<<	4:7881<===	:??,,--...	DEEE	4:<(()))	4:<(()))	1		xxzz
&&vy||444LO.	*//++,,)K,,\::;;<	
 	
 	
 	
 	
r/   c                    |                      d           | j                            d          \  }}t          | j        j        d| j        j                  }t          j        d          d         }t          | j        j        || j        j                  }t          ||| j        j                  }| 	                                }|
                    t          j        | j        j                  dz             |
                    t          j        | j        j                  dz             |
                    t          j        |                                                     |
                    d           |
                    t          j        | j        j                             |
                    t          j        | j        j                             |
                    t          j        |                     |
                    |           |
                    |           |                                }| j                            t          j        |                     |                     | j        d         t(          j        t          j        |                                          |z   t          j        |                    |d	                    z   f           d
S )a&  
        Test that the KEX_DH_GEX_INIT message after the client sends
        KEX_DH_GEX_REQUEST using a public key signature algorithm other than
        the default for the public key format causes the server to send a
        KEX_DH_GEX_INIT message with a public key and signature.
        r  )r  r   r  r   r  r  rt   r  Nr  r  s	            r0   /test_KEX_DH_GEX_INIT_after_REQUEST_rsa_sha2_512zYServerSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_INIT_after_REQUEST_rsa_sha2_512  sL    	$$/$BBB"&*"9"9/"J"J
K
a..L<==a@4:<DJL11aDJL11  	4:677!;<<<	4:7881<===	:??,,--...	DEEE	4:<(()))	4:<(()))	1		xxzz
&&vy||444LO.	*//++,,)$$\$QQ 
	
 
	
 
	
 
	
 
	
r/   N)r   )
r+   r,   r-   ro   r  r  r  r  r   r"  r.   r/   r0   r	  r	  q  s         0 0 0.S S S0 0 0 02%
 %
 %
N"
 "
 "
H%
 %
 %
 %
 %
r/   r	  c                       e Zd ZdZdS )*ServerSSHTransportDHGroupExchangeSHA1TestszJ
    diffie-hellman-group-exchange-sha1 tests for SSHServerTransport.
    NrQ  r.   r/   r0   r$  r$  !  rU  r/   r$  c                       e Zd ZdZdS ),ServerSSHTransportDHGroupExchangeSHA256TestszL
    diffie-hellman-group-exchange-sha256 tests for SSHServerTransport.
    NrQ  r.   r/   r0   r&  r&  +  rU  r/   r&  c                       e Zd ZdZd ZdS )ServerSSHTransportECDHBaseCasezE
    Elliptic Curve Diffie-Hellman tests for SSHServerTransport.
    c           
         | j         g| j        _        dg| j        _        | j                            | j                                                   | j                            d          \  }}| j                                        }|	                                }| j        
                    |          }| j                            t          j        |                     | j                            || j        
                    | j        j                            }|                                 }|                    t          j        | j        j                             |                    t          j        | j        j                             |                    t          j        | j        j                             |                    t          j        | j        j                             |                    t          j        |                                                     |                    t          j        |                     |                    t          j        | j        
                    | j        j                                       |                    |           |                                }|                    |          }	|                     | j        t
          j        t          j        |                                          t          j        | j        
                    | j        j                            z   t          j        |	          z   ft
          j        dfg           dS )z
        Test that the KEXDH_INIT message causes the server to send a
        KEXDH_REPLY with the server's public key and a signature.
        r   r/   N)r'  r  r  r  r  r%   rF  r  _generateECPrivateKeyr   _encodeECPublicKeyr  r"   rA   _generateECSharedSecretecPubr(  r  r  r  otherKexInitPayloadr  r   rH  r  rH  r  r  r  )
r?   r  r  ecPrivr-  encPubr   r  r!  r  s
             r0   test_KEX_ECDH_INITz1ServerSSHTransportECDHBaseCase.test_KEX_ECDH_INIT:  s   
 -1,=+>
(*4
&
 4 4 6 6777"&*"9"9*"E"E
K1133!!##..u55
--fi.?.?@@@z99DJ11$*2BCC
 
   	4:899:::	4:677888	4:9::;;;	4:788999	:??,,--...	6""###	4:889IJJKKLLL	xxzz$$\22	L -Ijoo//00i
 = =dj>N O OPPQi	**+ &,	
 	
 	
 	
 	
r/   N)r+   r,   r-   ro   r1  r.   r/   r0   r(  r(  5  s-         ,
 ,
 ,
 ,
 ,
r/   r(  c                       e Zd ZdZdS )ServerSSHTransportECDHTestsz:
    ecdh-sha2-nistp256 tests for SSHServerTransport.
    NrQ  r.   r/   r0   r3  r3  i  rR  r/   r3  c                       e Zd ZdZdS )'ServerSSHTransportCurve25519SHA256Testsz9
    curve25519-sha256 tests for SSHServerTransport.
    NrQ  r.   r/   r0   r5  r5  q  rZ  r/   r5  c                   Z    e Zd ZU dZej        Zeeej	                          e
d<   d Zd ZdS )ClientSSHTransportBaseCasez1
    Base case for SSHClientTransport tests.
    r   c                 $   d| _         |                     || j                   |                     |                    dd          t	          j        t          |                                                               t          j	        d          S )zC
        Mock version of SSHClientTransport.verifyHostKey.
        T   :r/   )
calledVerifyHostKeyrH  r   replacebinasciihexlifyr   rH  r   succeed)r?   pubKeyfingerprints      r0   verifyHostKeyz(ClientSSHTransportBaseCase.verifyHostKey  s     $( +++c**H,<S[[=O=O=Q=Q,R,R	
 	
 	
 }T"""r/   c                 F   t                               |            t          j                            t
          j                                                  | _        t          j                            t
          j                  | _	        d| _
        | j        | j        _        d S )NF)r   r  r$   r   r   r&   r   r   r   privObjr:  rA  r  rY   s    r0   r  z ClientSSHTransportBaseCase.setUp  sq    %%%H''(ABBGGII	x**7+EFF#( #'#5
   r/   N)r+   r,   r-   ro   r%   r8   r   r
   r   r1   r#  rA  r  r.   r/   r0   r7  r7  z  sa           9B8TE8D345TTT	# 	# 	#6 6 6 6 6r/   r7  c                   x    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZdS )ClientSSHTransportTestsz'
    Tests for SSHClientTransport.
    c                 *   | j                             d           |                     | j         j        d           |                     | j         j        d           |                     | j         j        d           |                     | j         j        d           | j         j        }|                     |j        d           |                     |j	        d           |                     |j
        d           |                     |j        d           dS )z
        Receiving a KEXINIT packet listing multiple supported
        algorithms will set up the first common algorithm, ordered after our
        preference.
        s	  SSH-2.0-Twisted
     bdiffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256   ssh-dss,ssh-rsa   aes128-ctr,aes128-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc,cast128-ctr,cast128-cbc,blowfish-ctr,blowfish-cbc,3des-ctr,3des-cbc   aes128-ctr,aes128-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc,cast128-ctr,cast128-cbc,blowfish-ctr,blowfish-cbc,3des-ctr,3des-cbc   hmac-md5,hmac-sha1   hmac-md5,hmac-sha1   	zlib,none   	zlib,none             r+  r   r9  r7  r8  N)r  r  rH  rF  r  r  r  r  r   r   r   r   r  s     r0   r  z6ClientSSHTransportTests.test_KEXINITMultipleAlgorithms  s    	
(	
 	
 	
$ 	*,STTT*J777;WEEE;WEEEZ'666}555555|44444r/   c                    |                      t          |                                 j                   d }|                                                     dd          }|                    | j                                      |          S )z
        verifyHostKey() should return a Deferred which fails with a
        NotImplementedError exception.  connectionSecure() should raise
        NotImplementedError().
        c                 :    |                      t                     d S r  )trapNotImplementedErrorrF   s    r0   _checkRaiseszNClientSSHTransportTests.test_notImplementedClientMethods.<locals>._checkRaises  s    FF&'''''r/   N)r  rJ  r   connectionSecurerA  addCallbackfail
addErrback)r?   rK  ds      r0    test_notImplementedClientMethodsz8ClientSSHTransportTests.test_notImplementedClientMethods  sv     	-tzz||/LMMM	( 	( 	( JJLL&&tT22}}TY''22<@@@r/   c                    |g| j         _        | j                             | j                                                   | j         j                                        j        }|                     t          j
        |          dd         d|dz  z             |                     | j        t          j        | j         j        fg           dS )z
        Test that a KEXINIT packet with a group1 or group14 key exchange
        results in a correct KEXDH_INIT response.

        @param kexAlgorithm: The key exchange algorithm to use
        @type kexAlgorithm: L{str}
        r   Nr  r  )r  r  r  r%   rF  r   r  rL   rH  r"   rJ   r  MSG_KEXDH_INITr   )r?   r'  r   rL   s       r0   assertKexInitResponseForDHz2ClientSSHTransportTests.assertKexInitResponseForDH  s     -9>
( 	
 4 4 6 6777J"224461abb)7dai+@AAA 	LI4dj6TUV	
 	
 	
 	
 	
r/   c                 2    |                      dd           dS )zq
        KEXINIT messages requesting diffie-hellman-group14-sha1 result in
        KEXDH_INIT responses.
        r   r   N)rT  rY   s    r0   test_KEXINIT_group14z,ClientSSHTransportTests.test_KEXINIT_group14  s!    
 	''(FMMMMMr/   c                     dg| j         _        | j                                                            dd          }|                     t          | j         j        |           dS )z
        Test that the client raises a ConchError if it receives a
        KEXINIT message but doesn't have a key exchange algorithm that we
        understand.
        s   diffie-hellman-group24-sha1s   group14s   group24N)r  r  r%   rF  r;  r  r   r  )r?   r  s     r0   test_KEXINIT_badKexAlgz.ClientSSHTransportTests.test_KEXINIT_badKexAlg  sX     -K+K
(~##%%--j*EE*dj&=tDDDDDr/   c                 ,   dt          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   dz   d	z   }| j                            |           |                     | j        j                   d
S )z
        If the server sends "ext-info-s" in its key exchange algorithms,
        then the client notes that the server supports extension
        negotiation.  See RFC 8308, section 2.1.
        r  s/   diffie-hellman-group-exchange-sha256,ext-info-sr   r7  r8  r9  r/   r:  r;  N)r"   rA   r  r  rW  r  r  s     r0   r  z8ClientSSHTransportTests.test_KEXINITExtensionNegotiation  s    iJKKLi
##$ i&&' i&&	'
 i%%& i%%& i  ! i  ! inn	 inn
  "" 	 	
}---
:;;;;;r/   c                 F   |                                   d}t          j        |          }| j        j                                        j        }| j        j        }t          |||          }t                      }|
                    t          j        | j        j                  dz             |
                    t          j        | j        j                  dz             |
                    t          j        | j                             |
                    | j        j                   |
                    |           |
                    |           |                                }| j                            |          }||t          j        | j                  |z   fS )a  
        Utility for test_KEXDH_REPLY and
        test_disconnectKEXDH_REPLYBadSignature.

        Begins a Diffie-Hellman key exchange in the named group
        Group-14 and computes information needed to return either a
        correct or incorrect signature.

        r  )rV  r"   rJ   r  r   r  rL   r   rO   r   r  rA   r  r  r   r   rH  rC  r  )	r?   rG   fMPrL   r   r   r  r!  r  s	            r0   begin_KEXDH_REPLYz)ClientSSHTransportTests.begin_KEXDH_REPLY		  sD    	!!###illJ"22446JLaAFF	4:677!;<<<	4:7881<===	49%%&&&	/000		xxzzL%%l33	i49)=)=)CDDr/   c                                                        \  }} fd} j                            |t          j        |          z             }|                    |           |S )zH
        Test that the KEXDH_REPLY message verifies the server.
        c                                          |                                j                                       j        j                   d S r  ry  rW  r:  rH  r  rG  rF  r!  r?   s    r0   _cbTestKEXDH_REPLYzDClientSSHTransportTests.test_KEXDH_REPLY.<locals>._cbTestKEXDH_REPLY-	  M    e$$$OOD4555TZ1<@@@@@r/   r\  r  ssh_KEX_DH_GEX_GROUPr"   rA   rM  r?   r  packetStartra  rP  r!  s   `    @r0   test_KEXDH_REPLYz(ClientSSHTransportTests.test_KEXDH_REPLY'	  s     261G1G1I1I.y+	A 	A 	A 	A 	A 	A
 J++K&)I:N:N,NOO	()))r/   c           
      V    d j         _        t                       j         _                             dd                                 j         j        d                                dd                                 j         j        d                                 j        d         t          j	        df            fdt          d          D             }                      j         j        j        |d	         |d
         |d         |d         |d         |d         f           dS )r  r   rD  rE  rB  r   r/   c                 H    g | ]}j                             |d d          S r  r  r  s     r0   r  z9ClientSSHTransportTests.test_keySetup.<locals>.<listcomp>B	  r  r/   r  r   r  rt   r   r   r   Nr  r  s   ` r0   r  z%ClientSSHTransportTests.test_keySetup7	  r  r/   c                    |                                   dgfd}|| j        _        t          j        dddd          | j        _        |                     dd           |                     | j        j        | j        j                   t                      | j        _        | j        
                    d           |                     | j        j                   |                     | j        j                   |                     | j        j        | j        j                   |                     d                    d| j        _        |                     dd	           | j        
                    d           |                     | j        j                   d| j        _        |                     dd
           | j        
                    d           |                     | j        j                   dS )zl
        Test that NEWKEYS transitions the keys from nextEncryptions to
        currentEncryptions.
        Fc                      d d<   d S r  r.   )secures   r0   stubConnectionSecurezBClientSSHTransportTests.test_NEWKEYS.<locals>.stubConnectionSecureP	  s    F1IIIr/   r9  rD  rE  r/   r   r  s   GHs   IJN)r  r  rL  r%   r  r  r"  assertIsNotrd  rq   r  ry  rj  r}  r  rW  r  r  r  )r?   rm  rl  s     @r0   r  z$ClientSSHTransportTests.test_NEWKEYSH	  s   
 	++---	 	 	 	 	 ';
#%.%9Wgw&
 &

" 	  ...6
8RSSS%/\\
"
s###$*8999$*8999dj3TZ5OPPPq	"""-4
*  ...
s###TZ;<<<-4
*  ...
s###TZ;<<<<<r/   c                     t                      | j        _        | j                            d           |                     | j        j        j                   dS )zS
        Test that the SERVICE_ACCEPT packet starts the requested service.
              MockServiceN)r   r  instancessh_SERVICE_ACCEPTrW  r   rY   s    r0   test_SERVICE_ACCEPTz+ClientSSHTransportTests.test_SERVICE_ACCEPTj	  sI     *mm

%%&DEEE
+344444r/   c                     | j                             t                                 |                     | j        t
          j        dfg           dS )zP
        Test that requesting a service sends a SERVICE_REQUEST packet.
        rp  N)r  requestServicer   rH  r  r%   r  rY   s    r0   test_requestServicez+ClientSSHTransportTests.test_requestServicer	  sT     	
!!+--000L+-KLM	
 	
 	
 	
 	
r/   c                                                        \  }}} j                            |t          j        d          z             }|                     fd          S )zL
        Test that KEXDH_REPLY disconnects if the signature is bad.
           bad signaturec                 B                         t          j                  S r  r^  r%   r`  _r?   s    r0   rt  zPClientSSHTransportTests.test_disconnectKEXDH_REPLYBadSignature.<locals>.<lambda>	      d,,Y-UVV r/   rc  r?   r!  r  rf  rP  s   `    r0   &test_disconnectKEXDH_REPLYBadSignaturez>ClientSSHTransportTests.test_disconnectKEXDH_REPLYBadSignature|	  d     261G1G1I1I.y+J++K&)DT:U:U,UVV}}VVVV
 
 	
r/   c                    dt          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   dz   d	z   }| j                            |           | j                            d
           t          j        t          j                    t                                | j        _	        | j        j	        
                                | j        _        t          j        t          j                    t                                }|
                                }|                    t          j        j        t          j        j                  }t          j                    | j        _        d| j        _        | j                            t          j        t+                                                      d                                                   t          j        |          z   t          j        d          z              |                     t2          j                   dS O
        Test that KEX_ECDH_REPLY disconnects if the signature is bad.
        r6  r/  r   r7  r8  r9  r/   r:  r;  s   SSH-2.0-OpenSSH
s   bad-signatureNr"   rA   r  r  r  r!   generate_private_key	SECP256R1r   r/  r   r-  public_bytesr   EncodingX962PublicFormatUncompressedPointcurverF  _ssh_KEX_ECDH_REPLYr   r   r   r^  r%   r`  r?   r  thisPrivthisPubr0  s        r0   )test_disconnectKEX_ECDH_REPLYBadSignaturezAClientSSHTransportTests.test_disconnectKEX_ECDH_REPLYBadSignature	  M   
 i-../i
##$ i&&' i&&	'
 i%%& i%%& i  ! i  ! inn	 inn
  "" 	  	
v&&&
 67773BLNNODUDUVV
:,7799
 *2<>>?;L;LMM%%''%%"')C)U
 
 <>>
1

&&Ikmm1133J?DDFFGGi i())*	
 	
 	
 	yGHHHHHr/   c                 b    | j                             d           |                                  dS r  r  rY   s    r0   r  z2ClientSSHTransportTests.test_disconnectNEWKEYSData	  r  r/   c                     t                      | j        _        | j                            d           |                                  dS )z
        Test that SERVICE_ACCEPT disconnects if the accepted protocol is
        differet from the asked-for protocol.
        s      badN)r   r  rq  rr  r^  rY   s    r0   test_disconnectSERVICE_ACCEPTz5ClientSSHTransportTests.test_disconnectSERVICE_ACCEPT	  sA    
 *mm

%%&<===     r/   c                    t                      | j        _        | j                            d           |                     | j        j        j                   |                     t          | j                  d           dS )z
        Some commercial SSH servers don't send a payload with the
        SERVICE_ACCEPT message.  Conch pretends that it got the correct
        name of the service.
        r/   r   N)	r   r  rq  rr  rW  r   rH  rx   r  rY   s    r0   test_noPayloadSERVICE_ACCEPTz4ClientSSHTransportTests.test_noPayloadSERVICE_ACCEPT	  sh     *mm

%%c***
+3444T\**A.....r/   N)r+   r,   r-   ro   r  rQ  rT  rV  rX  r  r\  rg  r  r  rs  rv  r  r  r  r  r  r.   r/   r0   rE  rE    s/         5  5  5DA A A
 
 
.N N NE E E< < <0E E E<   
 
 
" =  =  =D5 5 5
 
 
	
 	
 	
,I ,I ,I\! ! !! ! !	/ 	/ 	/ 	/ 	/r/   rE  c                   P    e Zd ZdZ	  edd          Zd Zd Zd Zd Z	d Z
d	 Zd
S ))ClientSSHTransportDHGroupExchangeBaseCasezE
    Diffie-Hellman group exchange tests for SSHClientTransport.
      FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF   c                     | j         g| j        _        | j                            | j                                                   |                     | j        t          j        dfg           dS )zt
        KEXINIT packet with a group-exchange key exchange results
        in a KEX_DH_GEX_REQUEST message.
                     N)	r'  r  r  r  r%   rF  rH  r  MSG_KEX_DH_GEX_REQUESTrY   s    r0   test_KEXINIT_groupexchangezDClientSSHTransportDHGroupExchangeBaseCase.test_KEXINIT_groupexchange	  st    
 -1,=+>
(
 4 4 6 6777L 4G	
 	
 	
 	
 	
r/   c           
         |                                   | j                            t          j        | j                  t          j        d          z              |                     | j        j        | j                   |                     | j        j        d           | j        j	        
                                j        }|                     t          j        |          dd         d           |                     | j        j        t          j        t          d|| j                                       |                     | j        dd         t          j        | j        j        fg           dS )z
        Test that the KEX_DH_GEX_GROUP message results in a
        KEX_DH_GEX_INIT message with the client's Diffie-Hellman public key.
        r  r   Ns   rt   )r  r  rd  r"   rJ   P1536rH  r   r   r   r  rL   r   rK   r  r%   MSG_KEX_DH_GEX_INITr{   s     r0   test_KEX_DH_GEX_GROUPz?ClientSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_GROUP	  s:   
 	'')))
''	$*(=(=	!(LMMMtz222q)))J"224461abb)=999J*FIc!Q
6K6K,L,L	
 	
 	
 	L+TZ-KLM	
 	
 	
 	
 	
r/   c                    |                                   | j        j        }d}t          j        |          }t          || j        j                                        j        |          }| 	                                }|
                    t          j        | j        j                  dz             |
                    t          j        | j        j                  dz             |
                    t          j        | j                             |
                    d           |
                    t          j        | j                  t          j        d          z              |
                    | j        j                   |
                    |           |
                    |           |                                }| j                            |          }||t          j        | j                  |z   fS )a  
        Utility for test_KEX_DH_GEX_REPLY and
        test_disconnectGEX_REPLYBadSignature.

        Begins a Diffie-Hellman key exchange in an unnamed
        (server-specified) group and computes information needed to
        return either a correct or incorrect signature.
        r   r  r  )r  r  r   r"   rJ   rO   r   r  rL   r(  r  rA   r  r  r   r  r   rH  rC  r  )r?   r   rG   r[  r   r  r!  r  s           r0   begin_KEX_DH_GEX_REPLYz@ClientSSHTransportDHGroupExchangeBaseCase.begin_KEX_DH_GEX_REPLY

  s    	""$$$JLilla!7!G!G!I!I!KQOO  	4:677!;<<<	4:7881<===	49%%&&&	DEEE	4:&&15666	/000		xxzzL%%l33	i49)=)=)CDDr/   c                                                        \  }} fd} j                            |t          j        |          z             }|                    |           |S )z^
        Test that the KEX_DH_GEX_REPLY message results in a verified
        server.
        c                                          |                                j                                       j        j                   d S r  r_  r`  s    r0   _cbTestKEX_DH_GEX_REPLYz`ClientSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_REPLY.<locals>._cbTestKEX_DH_GEX_REPLY0
  rb  r/   r  r  ssh_KEX_DH_GEX_REPLYr"   rA   rM  )r?   r  rf  r  rP  r!  s   `    @r0   test_KEX_DH_GEX_REPLYz?ClientSSHTransportDHGroupExchangeBaseCase.test_KEX_DH_GEX_REPLY)
  s    
 261L1L1N1N.y+	A 	A 	A 	A 	A 	A
 J++K&)I:N:N,NOO	-...r/   c                                                        \  }}} j                            |t          j        d          z             }|                     fd          S )zQ
        Test that KEX_DH_GEX_REPLY disconnects if the signature is bad.
        rx  c                 B                         t          j                  S r  rz  r{  s    r0   rt  z`ClientSSHTransportDHGroupExchangeBaseCase.test_disconnectGEX_REPLYBadSignature.<locals>.<lambda>A
  r}  r/   r  r~  s   `    r0   $test_disconnectGEX_REPLYBadSignaturezNClientSSHTransportDHGroupExchangeBaseCase.test_disconnectGEX_REPLYBadSignature9
  sd     261L1L1N1N.y+J++K&)DT:U:U,UVV}}VVVV
 
 	
r/   c                    dt          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   t          j        d          z   dz   d	z   }| j                            |           | j                            d
           t          j        t          j                    t                                | j        _	        | j        j	        
                                | j        _        t          j        t          j                    t                                }|
                                }|                    t          j        j        t          j        j                  }t          j                    | j        _        d| j        _        | j                            t          j        t+                                                      d                                                   t          j        |          z   t          j        d          z              |                     t2          j                   dS r  r  r  s        r0   r  zSClientSSHTransportDHGroupExchangeBaseCase.test_disconnectKEX_ECDH_REPLYBadSignatureD
  r  r/   N)r+   r,   r-   ro   r   r  r  r  r  r  r  r  r.   r/   r0   r  r  	  s          C	; 	
 
E
 
 
$
 
 
&E E E>   	
 	
 	
,I ,I ,I ,I ,Ir/   r  c                       e Zd ZdZdS )*ClientSSHTransportDHGroupExchangeSHA1TestszJ
    diffie-hellman-group-exchange-sha1 tests for SSHClientTransport.
    NrQ  r.   r/   r0   r  r  s
  rU  r/   r  c                       e Zd ZdZdS ),ClientSSHTransportDHGroupExchangeSHA256TestszL
    diffie-hellman-group-exchange-sha256 tests for SSHClientTransport.
    NrQ  r.   r/   r0   r  r  }
  rU  r/   r  c                   *    e Zd ZdZd Zd Zd Zd ZdS )ClientSSHTransportECDHBaseCasezE
    Elliptic Curve Diffie-Hellman tests for SSHClientTransport.
    c           	      D   | j         g| j        _        | j                            | j                                                   |                     | j        t          j        t          j
        | j                            | j        j                            fg           dS )zm
        KEXINIT packet with an elliptic curve key exchange results
        in a KEXDH_INIT message.
        N)r'  r  r  r  r%   rF  rH  r  rS  r"   rA   r+  r-  rY   s    r0   test_KEXINITz+ClientSSHTransportECDHBaseCase.test_KEXINIT
  s    
 -1,=+>
(
 4 4 6 6777L ,Idj;;DJ<LMMNN	
 	
 	
 	
 	
r/   c                    |                                   t                                                      d         }t                                                      d         }| j                                        }|                                }| j                            |          }| j                            || j                            | j        j	                            }| 
                                }|                    t          j        | j        j                             |                    t          j        | j        j                             |                    t          j        | j        j                             |                    t          j        | j        j                             |                    t          j        |                                                     |                    t          j        | j                            | j        j	                                       |                    t          j        |                     |                    |           |                                }|                    |          }	||	t          j        |                                          t          j        |          z   fS )a  
        Utility for test_KEXDH_REPLY and
        test_disconnectKEXDH_REPLYBadSignature.

        Begins an Elliptic Curve Diffie-Hellman key exchange and computes
        information needed to return either a correct or incorrect
        signature.
        r   )r  r   r   r   r  r*  r   r+  r,  r-  r(  r  r"   rA   r  r  r  r.  r   rH  r  )
r?   privKeyr?  r/  r-  r0  r   r  r!  r  s
             r0   r\  z0ClientSSHTransportECDHBaseCase.begin_KEXDH_REPLY
  s    	--..00<,,..z:1133!!##..u55z99DJ11$*2BCC
 
   	4:677888	4:899:::	4:788999	4:9::;;;	6;;==))***	4:889IJJKKLLL	6""###	xxzzLL..	i6;;==)A)AFIfDUDU)UVVr/   c                                                        \  }} fd} j                            |t          j        |          z             }|                    |           |S )zO
        Test that the KEXDH_REPLY message completes the key exchange.
        c                                          |                                j                                       j        j                   d S r  r_  r`  s    r0   ra  zKClientSSHTransportECDHBaseCase.test_KEXDH_REPLY.<locals>._cbTestKEXDH_REPLY
  rb  r/   rc  re  s   `    @r0   rg  z/ClientSSHTransportECDHBaseCase.test_KEXDH_REPLY
  s     261G1G1I1I.y+	A 	A 	A 	A 	A 	A
 J++K&)I:N:N,NOO	()))r/   c                                                        \  }}} j                            |t          j        d          z             }|                     fd          S )r  rx  c                 B                         t          j                  S r  rz  r{  s    r0   rt  zWClientSSHTransportECDHBaseCase.test_disconnectKEXDH_REPLYBadSignature.<locals>.<lambda>
  r}  r/   rc  r~  s   `    r0   r  zEClientSSHTransportECDHBaseCase.test_disconnectKEXDH_REPLYBadSignature
  r  r/   N)r+   r,   r-   ro   r  r\  rg  r  r.   r/   r0   r  r  
  s^         
 
 
$"W "W "WH  	
 	
 	
 	
 	
r/   r  c                       e Zd ZdZdS )ClientSSHTransportECDHTestsz:
    ecdh-sha2-nistp256 tests for SSHClientTransport.
    NrQ  r.   r/   r0   r  r  
  rR  r/   r  c                       e Zd ZdZdS )'ClientSSHTransportCurve25519SHA256Testsz9
    curve25519-sha256 tests for SSHClientTransport.
    NrQ  r.   r/   r0   r  r  
  rZ  r/   r  c                   P    e Zd ZdZereZd Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 ZdS )GetMACTestsz*
    Tests for L{SSHCiphers._getMAC}.
    c                 >    t          j        dddd          | _        d S )Nr  ri  r  r  )r%   r  r  rY   s    r0   r  zGetMACTests.setUp
  s     +D$dCCr/   c                      t          d          S )z
        Generate a new shared secret to be used with the tests.

        @return: A new secret.
        @rtype: L{bytes}
        @   r   rY   s    r0   getSharedSecretzGetMACTests.getSharedSecret
  s     b!!!r/   c                    |                                  }| j                            ||          }|d|         d|z  z   }t          d t	          |          D                       }t          d t	          |          D                       }	|                     |||	|f|           |                     ||j                   dS )a  
        Check that when L{SSHCiphers._getMAC} is called with a supportd HMAC
        algorithm name it returns a tuple of
        (digest object, inner pad, outer pad, digest size) with a C{key}
        attribute set to the value of the key supplied.

        @param hmacName: Identifier of HMAC algorithm.
        @type hmacName: L{bytes}

        @param hashProcessor: Callable for the hash algorithm.
        @type hashProcessor: C{callable}

        @param digestSize: Size of the digest for algorithm.
        @type digestSize: L{int}

        @param blockPadSize: Size of padding applied to the shared secret to
            match the block size.
        @type blockPadSize: L{int}
        Nr:  c              3   :   K   | ]}t          |          d z  V  dS )6   Nr`  r   bs     r0   r   z+GetMACTests.assertGetMAC.<locals>.<genexpr>  ,      ??1Q$??????r/   c              3   :   K   | ]}t          |          d z  V  dS )\   Nr  r  s     r0   r   z+GetMACTests.assertGetMAC.<locals>.<genexpr>  r  r/   )r  r  _getMACr   r   rH  r   )
r?   hmacNamer(  
digestSizeblockPadSizesecretparamsr   innerPadouterPads
             r0   assertGetMACzGetMACTests.assertGetMAC  s    ( %%''%%h77[j[!Gl$::??	#???????	#?????-8ZH&QQQfj)))))r/   c                 B    |                      dt          dd           dS )a  
        When L{SSHCiphers._getMAC} is called with the C{b"hmac-sha2-512"} MAC
        algorithm name it returns a tuple of (sha512 digest object, inner pad,
        outer pad, sha512 digest size) with a C{key} attribute set to the
        value of the key supplied.
        s   hmac-sha2-512r  r  r  N)r  r   rY   s    r0   test_hmacsha2512zGetMACTests.test_hmacsha2512   )     	*FrPRSSSSSr/   c                 B    |                      dt          dd           dS )a  
        When L{SSHCiphers._getMAC} is called with the C{b"hmac-sha2-384"} MAC
        algorithm name it returns a tuple of (sha384 digest object, inner pad,
        outer pad, sha384 digest size) with a C{key} attribute set to the
        value of the key supplied.
        s   hmac-sha2-3840   P   r  N)r  r   rY   s    r0   test_hmacsha2384zGetMACTests.test_hmacsha2384)  r  r/   c                 B    |                      dt          dd           dS )a  
        When L{SSHCiphers._getMAC} is called with the C{b"hmac-sha2-256"} MAC
        algorithm name it returns a tuple of (sha256 digest object, inner pad,
        outer pad, sha256 digest size) with a C{key} attribute set to the
        value of the key supplied.
        s   hmac-sha2-256    r  N)r  r   rY   s    r0   test_hmacsha2256zGetMACTests.test_hmacsha22562  r  r/   c                 B    |                      dt          dd           dS )a  
        When L{SSHCiphers._getMAC} is called with the C{b"hmac-sha1"} MAC
        algorithm name it returns a tuple of (sha1 digest object, inner pad,
        outer pad, sha1 digest size) with a C{key} attribute set to the value
        of the key supplied.
        r8     ,   r  N)r  r   rY   s    r0   test_hmacsha1zGetMACTests.test_hmacsha1;  s'     	,"MMMMMr/   c                 B    |                      dt          dd           dS )a  
        When L{SSHCiphers._getMAC} is called with the C{b"hmac-md5"} MAC
        algorithm name it returns a tuple of (md5 digest object, inner pad,
        outer pad, md5 digest size) with a C{key} attribute set to the value of
        the key supplied.
        r  r  r  r  N)r  r   rY   s    r0   test_hmacmd5zGetMACTests.test_hmacmd5D  s'     	+srKKKKKr/   c                     |                                  }| j                            d|          }|                     d|           dS )z
        When L{SSHCiphers._getMAC} is called with the C{b"none"} MAC algorithm
        name it returns a tuple of (None, "", "", 0).
        r9  )Nr/   r/   r   N)r  r  r  rH  )r?   r   r  s      r0   	test_nonezGetMACTests.test_noneM  sI    
 ""$$%%gs33,f55555r/   N)r+   r,   r-   ro   r   rE   r  r  r  r  r  r  r  r  r  r.   r/   r0   r  r  
  s           D D D" " "* * *<T T TT T TT T TN N NL L L	6 	6 	6 	6 	6r/   r  c                   8    e Zd ZdZereZd Zd Zd Zd Z	d Z
dS )SSHCiphersTestsz0
    Tests for the SSHCiphers helper class.
    c                    t          j        dddd          }|                     |j        d           |                     |j        d           |                     |j        d           |                     |j        d           dS )zJ
        Test that the initializer sets up the SSHCiphers object.
        r  ri  r  r  N)r%   r  rH  r   r   r   r   )r?   r  s     r0   	test_initzSSHCiphersTests.test_inita  s     &tT4>>+T222*D111+T222*D11111r/   c                 4   t          j        dddd          }dx}}|j                                        D ]b\  }\  }}}|                    |||          }|dk    r!|                     |t           j                   G|                     |j        |           cdS )zM
        Test that the _getCipher method returns the correct cipher.
        r  ri  r  r  r  r9  N)r%   r  	cipherMapr   r  assertIsInstance_DummyCipher	algorithm)	r?   r  r  r   r  algClasskeySizecountercips	            r0   test_getCipherzSSHCiphersTests.test_getCipherk  s     &tT4>>S5<5F5L5L5N5N 	? 	?1G1h$$Wb#66C'!!%%c9+ABBBB%%cmX>>>>	? 	?r/   c           	         d}t           j        j        D ]}t           j        j        |         \  }}}t          j        |ddd          }t          j        d|dd          }|                    |||          }|j        j        dz  }	|                    ||dddd           |                    dd||dd           | 	                    |j
        |	           | 	                    |j        |	           |                                }
|
                    |d|	                   }|
                    |d|	                   }| 	                    |                    |d|	                   |           | 	                    |                    |d|	                   |           | 	                    |                    |          |d|	                    | 	                    |                    |          |d|	                    dS )z8
        Test that setKeys sets up the ciphers.
        @                                                                   r9  r  r/   N)r%   r1   r  r  r  r  r  
block_sizer   rH  ry   r   	encryptorr  r|   r   )r?   r   r  modNamer  r  	encCipher	decCipherr  bsr  encenc2s                r0   test_setKeysCiphersz#SSHCiphersTests.test_setKeysCiphersx  s     1B 	@ 	@G(1(<(Fw(O%GWg!,WgwPPI!,WgwPPI&&wS99C)Q.Bc3S#s;;;c3S#s;;;Y3R888Y3R888I""3ss8,,C##CH--DY..s3B3x88#>>>Y..s3B3x88$???Y..s33S"X>>>Y..t44c#2#h????!	@ 	@r/   c           	         d}t           j        j                                        D ]T\  }}t          j        dd|d          }t          j        ddd|          }|                    dddd|d           |                    ddddd|           |r |            j        }nd}|                     |j        |           |r|                    ||          \  }}}}d}	|}
d|z   }|r? || |||z             	                                z             	                                }nd}|                     |
                    |	|
          |           |                     |                    |	|
|                     VdS )z5
        Test that setKeys sets up the MACs.
        r  r9  r/   r   r;  N)r%   r  macMapr   r   digest_sizerH  r   r  rH  r   rW  r   )r?   r   macNamemodoutMacinMacdsioseqidr  rm   macs                r0   test_setKeysMACsz SSHCiphersTests.test_setKeysMACs  s    %07==?? 	< 	<LGS)'7GWMMF('7GLLENN3S#sC888MM#sCc3777 SUU&U3R888 = &w < <Q2ED 3&F c!cc!f*oo4466677>>@@V^^E488#>>>OOELLc::;;;;)	< 	<r/   c           
      \   g d}|D ]\  }}}t          j        dddd          }|                    d|          |_        t	          j        d|dd                   \  }|dd         }|                     |t          j        |	                    ||                    d|d|           dS )	z
        L{SSHCiphers.makeMAC} computes the HMAC of an outgoing SSH message with
        a particular sequence id and content data.
        ))s   s   Hi Theres    9294727a3638bb1c13f48ef8158bfc9d)s   Jefes   what do ya want for nothing?s    750c783e6ab0b503eaa86e310a5db738)r6  s2   s    56be34521d144c88dbb8c733f0e8b3f6r9  r  z>LNr   zFailed HMAC test vector; key=z data=)
r%   r  r  r   structunpackrH  r<  r=  r   )r?   vectorsr   r  r  r   r  	shorteneds           r0   test_makeMACzSSHCiphersTests.test_makeMAC  s    
 
 
 & 		 		NCs)'7KQQF"NN;<<FM}T4844HUQRRI y!A!ABBEEETEE   		 		r/   N)r+   r,   r-   ro   r   rE   r  r  r  r  r  r.   r/   r0   r  r  Y  s{           2 2 2? ? ?@ @ @.< < <6    r/   r  c                   8    e Zd ZdZereZd Zd Zd Zd Z	d Z
dS )TransportLoopbackTestszL
    Test the server transport and client transport against each other,
    c                     t                      }t          j                    |_         |j                     g _        fd_        t          j                    d _        g _        fd_        fd_	        t          j                                                                                  _         |           |           fd}t          j                  }|                    |           |S )z
        Run an async client and server, modifying each using the mod function
        provided.  Returns a Deferred called back when both Protocols have
        disconnected.

        @type mod: C{func}
        @rtype: C{defer.Deferred}
        c                 <    j                             | |f          S r  r^   )codedescservers     r0   rt  z9TransportLoopbackTests._runClientServer.<locals>.<lambda>      1E1EtTl1S1S r/   c                 *    t          j        d           S r  )r   r>  )rL   rM   s     r0   rt  z9TransportLoopbackTests._runClientServer.<locals>.<lambda>  s    EM$,?,? r/   c                 <    j                             | |f          S r  r^   )r  r  clients     r0   rt  z9TransportLoopbackTests._runClientServer.<locals>.<lambda>  r   r/   c                  ,                                      S r  )r  )r#  s   r0   rt  z9TransportLoopbackTests._runClientServer.<locals>.<lambda>  s    &*?*?*A*A r/   c                    t          |j        d         |j        d         |j        d         |j        d         g          }                    |j        g                                |j        t          j        dfg           |j        d         dk    rQ	                    |
                                |           	                    |
                                |           nP                    |
                                |                               |
                                |           |j        d         dk    rR	                    |                                |           	                    |                                |           d S                     |                                |                               |                                |           d S )Nr   s   user closed connectionr9  )reprr  r  r  r  rH  rT   r%   r
  rU  r  rW  r  )ignoredr  r#  r   r?   s       r0   checkz6TransportLoopbackTests._runClientServer.<locals>.check  s   +A.(+0303	 D V]B///68QRS   &q)W44  !3!3!5!5t<<<  !3!3!5!5t<<<< 2 2 4 4d;;; 2 2 4 4d;;;#A&'11  !2!2!4!4d;;;  !2!2!4!4d;;;;; 1 1 3 3T::: 1 1 3 3T:::::r/   )r   r%   r5   r#   r  rT   rb   r8   rA  rL  listr   r$   r  r   loopbackAsyncrM  )r?   r
  r#   r(  rP  r#  r  s   `    @@r0   _runClientServerz'TransportLoopbackTests._runClientServer  s    ---// SSSS-//??SSSS"A"A"A"A%)&.*F*F*H*H*M*M*O*O%P%P"VV	; 	; 	; 	; 	;6 "6622	eVV,,,r/   c                     g }t           j        j        dgz   D ]/fd}|                    |                     |                     0t          j        |d          S )z{
        Test that the client and server play nicely together, in all
        the various combinations of ciphers.
        r9  c                     g| _         | S r  rv  )r  ciphers    r0   	setCipherz6TransportLoopbackTests.test_ciphers.<locals>.setCipher  s    *0&r/   TfireOnOneErrback)r%   r1   r  r_   r+  r   DeferredList)r?   	deferredsr/  r.  s      @r0   test_ciphersz#TransportLoopbackTests.test_ciphers  s|    
 	0AWIM 	? 	?F     T229==>>>>!)dCCCCr/   c                     g }t           j        j        dgz   D ]/fd}|                    |                     |                     0t          j        |d          S )z>
        Like test_ciphers, but for the various MACs.
        r9  c                     g| _         | S r  r{  )r  r  s    r0   setMACz0TransportLoopbackTests.test_macs.<locals>.setMAC  s    '*e#r/   Tr0  )r%   r1   r  r_   r+  r   r2  )r?   r3  r7  r  s      @r0   	test_macsz TransportLoopbackTests.test_macs  s|     	-;wiG 	< 	<C     T226::;;;;!)dCCCCr/   c                     g }t           j        j        D ]/fd}|                    |                     |                     0t          j        |d          S )zG
        Like test_ciphers, but for the various key exchanges.
        c                     g| _         | S r  re  )r  r'  s    r0   setKeyExchangez@TransportLoopbackTests.test_keyexchanges.<locals>.setKeyExchange)  s    /;n+r/   Tr0  )r%   r1   r  r_   r+  r   r2  )r?   r3  r;  r'  s      @r0   test_keyexchangesz(TransportLoopbackTests.test_keyexchanges"  sw     	%6L 	D 	DL     T22>BBCCCC!)dCCCCr/   c                     g }t           j        j        D ]/fd}|                    |                     |                     0t          j        |d          S )zF
        Like test_ciphers, but for the various compressions.
        c                     g| _         | S r  rq  )r  compressions    r0   setCompressionz@TransportLoopbackTests.test_compressions.<locals>.setCompression7  s    /:m+r/   Tr0  )r%   r1   r  r_   r+  r   r2  )r?   r3  r@  r?  s      @r0   test_compressionsz(TransportLoopbackTests.test_compressions0  sw     	$5K 	D 	DK     T22>BBCCCC!)dCCCCr/   N)r+   r,   r-   ro   r   rE   r+  r4  r8  r<  rA  r.   r/   r0   r  r    s           5 5 5nD D DD D DD D DD D D D Dr/   r  )mro   r<  rC  rM  r  r  hashlibr   r   r   r   r   typingr   r	   r
   r   r   twistedr   rI  twisted.conch.errorr   twisted.conch.sshr   r   r   twisted.internetr   twisted.protocolsr   twisted.pythonr   twisted.python.compatr   twisted.python.randbytesr   twisted.python.reflectr   twisted.testr   twisted.trial.unittestr   r   strr#  r   cryptography.exceptionsr   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   )cryptography.hazmat.primitives.asymmetricr    r!   r"   r#   r$   r%   twisted.conch.testr&   x25519_supportedrD   rH   rO   r1   rQ   rq   r   
SSHServicer   r<   r   r   r   r   r   r%  r*  r.  r1  r3  r5  r@  rP  rT  rW  rY  r\  r  r  r	  r$  r&  r(  r3  r5  r7  rE  r  r  r  r  r  r  r  r  r  r.   r/   r0   <module>rW     s}
    
  				    5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 * * * * * * 4 4 4 4 4 4 4 4 4 4 " " " " " " & & & & & & $ $ $ $ $ $ + + + + + + 3 3 3 3 3 3 0 0 0 0 0 0 & & & & & & + + + + + +}^,,    "N<<<<<<<<<<<<<<<<<<@@@@@@@@BBBBBBBBBBBB******&((99;; :9                       # # #B% B% B% B% B%	2 B% B% B%J>@ >@ >@ >@ >@ >@ >@ >@B         0  0  0  0  0'$  0  0  0F(1 (1 (1 (1 (1'$ (1 (1 (1V    {              (:9 :9 :9 :9 :9 :9 :9 :9z                            J J J J J J J Jr; r; r; r; r;46G r; r; r;jT T T T T.F T T T&    +-EGX      +      +Y8I       +-BDU  b
 b
 b
 b
 b
 b
 b
 b
J    !D   $yK yK yK yK yK8:K yK yK yKxm
 m
 m
 m
 m
0J m
 m
 m
`    -      -  1
 1
 1
 1
 1
%? 1
 1
 1
h    "I/@       "$9;L  6 6 6 6 6!D 6 6 64z/ z/ z/ z/ z/8:K z/ z/ z/z	_I _I _I _I _I0J _I _I _ID    -      -  S
 S
 S
 S
 S
%? S
 S
 S
l    "I/@       "$9;L  h6 h6 h6 h6 h6( h6 h6 h6Vj j j j jh j j jZvD vD vD vD vDX vD vD vD vD vDr/   