
    Vh"              	         U d Z ddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZmZmZ ddlmZ e	rddlmZ  G d d	e
      Z G d
 de      Z G d de      Z G d de      Z G d de      Z ee       G d d             Z ee       G d d             Z ee       G d d             Z ee       G d d             Z ee       G d d             Z ee       G d d             Z ee       G d d             Z ee       G d  d!             Z e        e        e        e        e        e        e        e       d"Zd#e d$<   d,d%Z!d-d&Z"d-d'Z#d.d(Z$d/d)Z%d0d*Z&y+)1z
SSH key exchange handling.
    )annotations)sha1sha256sha384sha512)TYPE_CHECKINGProtocol)	Attribute	Interfaceimplementer)error)_Hashc                      e Zd ZdddZy)_HashFactoryc                     y N )selfdatas     F/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/ssh/_kex.py__call__z_HashFactory.__call__   s        N).)r   bytesreturnr   )__name__
__module____qualname__r   r   r   r   r   r      s    r   r   c                  F    e Zd ZU dZ ed      Zded<    ed      Zded<   y)	_IKexAlgorithmzB
    An L{_IKexAlgorithm} describes a key exchange algorithm.
    zAn L{int} giving the preference of the algorithm when negotiating key exchange. Algorithms with lower precedence values are more preferred.int
preferencezqA callable hash algorithm constructor (e.g. C{hashlib.sha256}) suitable for use with this key exchange algorithm.r   hashProcessorN)r   r   r   __doc__r
   r!   __annotations__r"   r   r   r   r   r      s7      	J  #,	=#M< r   r   c                  0    e Zd ZdZ ed      Z ed      Zy)_IFixedGroupKexAlgorithmzu
    An L{_IFixedGroupKexAlgorithm} describes a key exchange algorithm with a
    fixed prime / generator group.
    zdAn L{int} giving the prime number used in Diffie-Hellman key exchange, or L{None} if not applicable.zAn L{int} giving the generator number used in Diffie-Hellman key exchange, or L{None} if not applicable. (This is not related to Python generator functions.)N)r   r   r   r#   r
   prime	generatorr   r   r   r&   r&   -   s)    
 	2E
 	'Ir   r&   c                      e Zd ZdZy)#_IEllipticCurveExchangeKexAlgorithmz
    An L{_IEllipticCurveExchangeKexAlgorithm} describes a key exchange algorithm
    that uses an elliptic curve exchange between the client and server.
    Nr   r   r   r#   r   r   r   r*   r*   ?   s    r   r*   c                      e Zd ZdZy)_IGroupExchangeKexAlgorithmz
    An L{_IGroupExchangeKexAlgorithm} describes a key exchange algorithm
    that uses group exchange between the client and server.

    A prime / generator group should be chosen at run time based on the
    requested size. See RFC 4419.
    Nr+   r   r   r   r-   r-   F   s    r   r-   c                      e Zd ZdZdZeZy)_Curve25519SHA256z
    Elliptic Curve Key Exchange using Curve25519 and SHA256. Defined in
    U{https://datatracker.ietf.org/doc/draft-ietf-curdle-ssh-curves/}.
       Nr   r   r   r#   r!   r   r"   r   r   r   r/   r/   P       
 JMr   r/   c                      e Zd ZdZdZeZy)_Curve25519SHA256LibSSHzN
    As L{_Curve25519SHA256}, but with a pre-standardized algorithm name.
       Nr1   r   r   r   r4   r4   [   s     JMr   r4   c                      e Zd ZdZdZeZy)_ECDH256aX  
    Elliptic Curve Key Exchange with SHA-256 as HASH. Defined in
    RFC 5656.

    Note that C{ecdh-sha2-nistp256} takes priority over nistp384 or nistp512.
    This is the same priority from OpenSSH.

    C{ecdh-sha2-nistp256} is considered preety good cryptography.
    If you need something better consider using C{curve25519-sha256}.
       Nr1   r   r   r   r7   r7   e   s    	 JMr   r7   c                      e Zd ZdZdZeZy)_ECDH384zT
    Elliptic Curve Key Exchange with SHA-384 as HASH. Defined in
    RFC 5656.
       N)r   r   r   r#   r!   r   r"   r   r   r   r:   r:   v   r2   r   r:   c                      e Zd ZdZdZeZy)_ECDH512zT
    Elliptic Curve Key Exchange with SHA-512 as HASH. Defined in
    RFC 5656.
       N)r   r   r   r#   r!   r   r"   r   r   r   r=   r=      r2   r   r=   c                      e Zd ZdZdZeZy)_DHGroupExchangeSHA256zc
    Diffie-Hellman Group and Key Exchange with SHA-256 as HASH. Defined in
    RFC 4419, 4.2.
       Nr1   r   r   r   r@   r@      r2   r   r@   c                      e Zd ZdZdZeZy)_DHGroupExchangeSHA1za
    Diffie-Hellman Group and Key Exchange with SHA-1 as HASH. Defined in
    RFC 4419, 4.1.
       N)r   r   r   r#   r!   r   r"   r   r   r   rC   rC      s    
 JMr   rC   c                  ,    e Zd ZdZdZeZ ed      ZdZ	y)_DHGroup14SHA1z
    Diffie-Hellman key exchange with SHA-1 as HASH and Oakley Group 14
    (2048-bit MODP Group). Defined in RFC 4253, 8.2.
       i  32317006071311007300338913926423828248817941241140239112842009751400741706634354222619689417363569347117901737909704191754605873209195028853758986185622153212175412514901774520270235796078236248884246189477587641105928646099411723245426622522193230540919037680524235519125679715870117001058055877651038861847280257976054903569732561526167081339361799541336476559160368317896729073178384589680639671900977202194168647225871031411336429319536193471636533209717077448227988588565369208645296636077250268955505928362751121174096972998068410554359584866583291642136218231078990999448652468262416972035911852507045361090559r5   N)
r   r   r   r#   r!   r   r"   r    r'   r(   r   r   r   rF   rF      s)    
 JM
	E Ir   rF   )   curve25519-sha256s   curve25519-sha256@libssh.orgs$   diffie-hellman-group-exchange-sha256s"   diffie-hellman-group-exchange-sha1s   diffie-hellman-group14-sha1s   ecdh-sha2-nistp256s   ecdh-sha2-nistp384s   ecdh-sha2-nistp521zdict[bytes, _IKexAlgorithm]_kexAlgorithmsc                T    | t         vrt        j                  d|       t         |    S )aY  
    Get a description of a named key exchange algorithm.

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

    @return: A description of the key exchange algorithm named by
        C{kexAlgorithm}.
    @rtype: L{_IKexAlgorithm}

    @raises ConchError: if the key exchange algorithm is not found.
    z$Unsupported key exchange algorithm: )rJ   r   
ConchErrorkexAlgorithms    r   getKexrO      s1     >)!ElEUVWW,''r   c                >    t         j                  t        |             S )z
    Returns C{True} if C{kexAlgorithm} is an elliptic curve.

    @param kexAlgorithm: The key exchange algorithm name.

    @return: C{True} if C{kexAlgorithm} is an elliptic curve, otherwise
        C{False}.
    )r*   
providedByrO   rM   s    r   isEllipticCurverR      s     /99&:NOOr   c                >    t         j                  t        |             S )z
    Returns C{True} if C{kexAlgorithm} has a fixed prime / generator group.

    @param kexAlgorithm: The key exchange algorithm name.

    @return: C{True} if C{kexAlgorithm} has a fixed prime / generator group,
        otherwise C{False}.
    )r&   rQ   rO   rM   s    r   isFixedGrouprT      s     $..vl/CDDr   c                0    t        |       }|j                  S )z
    Get the hash algorithm callable to use in key exchange.

    @param kexAlgorithm: The key exchange algorithm name.

    @return: A callable hash algorithm constructor (e.g. C{hashlib.sha256}).
    )rO   r"   rN   kexs     r   getHashProcessorrX      s     
Cr   c                Z    t        t        |             }|j                  |j                  fS )z
    Get the generator and the prime to use in key exchange.

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

    @return: A L{tuple} containing L{int} generator and L{int} prime.
    @rtype: L{tuple}
    )r&   rO   r(   r'   rV   s     r   getDHGeneratorAndPrimerZ      s&     #6,#7
8C==#))##r   c                    ddl m}  ddlm} ddlm}  |        }t        j                         t              D ]  }|j                  d      r6|j                  dd      }|j                  |j                         ||         }n$|j                  d      r|j                         }nd}|rqj                  |        t        fd	
      S )z
    Get a list of supported key exchange algorithm names in order of
    preference.

    @return: A C{list} of supported key exchange algorithm names.
    r   )default_backend)ec)_curveTables   ecdhs   ecdsarI   Tc                "    |    j                   S r   )r!   )rN   kexAlgorithmss    r   <lambda>z*getSupportedKeyExchanges.<locals>.<lambda>(  s    l0K0V0V r   )key)cryptography.hazmat.backendsr\   )cryptography.hazmat.primitives.asymmetricr]   twisted.conch.ssh.keysr^   rJ   copylist
startswithreplace+elliptic_curve_exchange_algorithm_supportedECDHx25519_supportedpopsorted)r\   r]   r^   backendkeyAlgorithmkeyAlgorithmDsa	supportedr`   s          @r   getSupportedKeyExchangesrs     s     =<2G"'')M]+ ,""7+*227HEOKK	;7I $$%9:002IIl+, V r   N)rN   r   r   r   )rN   r   r   bool)rN   r   r   r   )rN   r   r   ztuple[int, int])r   zlist[bytes])'r#   
__future__r   hashlibr   r   r   r   typingr   r	   zope.interfacer
   r   r   twisted.conchr   r   r   r   r&   r*   r-   r/   r4   r7   r:   r=   r@   rC   rF   rJ   r$   rO   rR   rT   rX   rZ   rs   r   r   r   <module>rz      s  
 # 0 0 * < < 8 
Y "~ $. .  01  2 01  2 01  2  01  2 01  2 ()  * ()  * %&  '6 ,-%<%>-C-E+?+A$2$4#:#:#:	/+ 	($	P	E	$r   