U
    
W[                     @   sz  d Z ddlmZmZ ddlmZmZmZmZ ddl	m
Z
mZmZ ddlmZ ddlmZ G dd deZG d	d
 d
eZG dd deZG dd deZeeG dd deZeeG dd deZeeG dd deZeeG dd deZeeG dd deZeeG dd deZe e e e e e dZdd Zdd Zd d! Zd"d# Z d$d% Z!d&d' Z"d(S ))z
SSH key exchange handling.
    )absolute_importdivision)sha1sha256sha384sha512)	Attributeimplementer	Interface)error)longc                   @   s    e Zd ZdZedZedZdS )_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.zqA callable hash algorithm constructor (e.g. C{hashlib.sha256}) suitable for use with this key exchange algorithm.N)__name__
__module____qualname____doc__r   
preferencehashProcessor r   r   8/usr/lib/python3/dist-packages/twisted/conch/ssh/_kex.pyr      s   r   c                   @   s    e Zd ZdZedZedZdS )_IFixedGroupKexAlgorithmzu
    An L{_IFixedGroupKexAlgorithm} describes a key exchange algorithm with a
    fixed prime / generator group.
    zdA L{long} giving the prime number used in Diffie-Hellman key exchange, or L{None} if not applicable.zA L{long} 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   r   c                   @   s   e Zd ZdZdS )#_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   4   s   r   c                   @   s   e Zd ZdZdS )_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   <   s   r   c                   @   s   e Zd ZdZdZeZdS )_ECDH256zT
    Elliptic Curve Key Exchange with SHA-256 as HASH. Defined in
    RFC 5656.
       Nr   r   r   r   r   r   r   r   r   r   r   r   G   s   r   c                   @   s   e Zd ZdZdZeZdS )_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   R   s   r   c                   @   s   e Zd ZdZdZeZdS )_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!   ]   s   r!   c                   @   s   e Zd ZdZdZeZdS )_DHGroupExchangeSHA256zc
    Diffie-Hellman Group and Key Exchange with SHA-256 as HASH. Defined in
    RFC 4419, 4.2.
       Nr   r   r   r   r   r#   h   s   r#   c                   @   s   e Zd ZdZdZeZdS )_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   r   r%   t   s   r%   c                   @   s$   e Zd ZdZdZeZedZdZ	dS )_DHGroup14SHA1z
    Diffie-Hellman key exchange with SHA-1 as HASH and Oakley Group 14
    (2048-bit MODP Group). Defined in RFC 4253, 8.2.
       Ai  32317006071311007300338913926423828248817941241140239112842009751400741706634354222619689417363569347117901737909704191754605873209195028853758986185622153212175412514901774520270235796078236248884246189477587641105928646099411723245426622522193230540919037680524235519125679715870117001058055877651038861847280257976054903569732561526167081339361799541336476559160368317896729073178384589680639671900977202194168647225871031411336429319536193471636533209717077448227988588565369208645296636077250268955505928362751121174096972998068410554359584866583291642136218231078990999448652468262416972035911852507045361090559r    N)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r'      s
   
r'   )s$   diffie-hellman-group-exchange-sha256s"   diffie-hellman-group-exchange-sha1s   diffie-hellman-group14-sha1s   ecdh-sha2-nistp256s   ecdh-sha2-nistp384s   ecdh-sha2-nistp521c                 C   s    | t krtd| f 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: %s)_kexAlgorithmsr   Z
ConchErrorkexAlgorithmr   r   r   getKex   s
    r,   c                 C   s   t t| S )a  
    Returns C{True} if C{kexAlgorithm} is an elliptic curve.

    @param kexAlgorithm: The key exchange algorithm name.
    @type kexAlgorithm: C{str}

    @return: C{True} if C{kexAlgorithm} is an elliptic curve,
        otherwise C{False}.
    @rtype: C{bool}
    )r   
providedByr,   r*   r   r   r   isEllipticCurve   s    r.   c                 C   s   t t| S )a+  
    Returns C{True} if C{kexAlgorithm} has a fixed prime / generator group.

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

    @return: C{True} if C{kexAlgorithm} has a fixed prime / generator group,
        otherwise C{False}.
    @rtype: L{bool}
    )r   r-   r,   r*   r   r   r   isFixedGroup   s    r/   c                 C   s   t | }|jS )a  
    Get the hash algorithm callable to use in key exchange.

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

    @return: A callable hash algorithm constructor (e.g. C{hashlib.sha256}).
    @rtype: C{callable}
    )r,   r   r+   Zkexr   r   r   getHashProcessor   s    
r1   c                 C   s   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{long} generator and L{long} prime.
    @rtype: L{tuple}
    )r,   r   r   r0   r   r   r   getDHGeneratorAndPrime   s    
r2   c                     s   ddl m}  ddlm} ddlm} |  }t  t D ]<}|	dr:|
dd}|| || }|s: | q:t  fdd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.
    @rtype: C{list} of L{bytes}
    r   )default_backend)ec)_curveTables   ecdhs   ecdsac                    s
    |  j S )N)r   r*   ZkexAlgorithmsr   r   <lambda>      z*getSupportedKeyExchanges.<locals>.<lambda>)key)Zcryptography.hazmat.backendsr3   Z)cryptography.hazmat.primitives.asymmetricr4   Ztwisted.conch.ssh.keysr5   r)   copylist
startswithreplaceZ+elliptic_curve_exchange_algorithm_supportedZECDHpopsorted)r3   r4   r5   ZbackendZkeyAlgorithmZkeyAlgorithmDsaZ	supportedr   r6   r   getSupportedKeyExchanges   s$    
 
r@   N)#r   Z
__future__r   r   Zhashlibr   r   r   r   Zzope.interfacer   r	   r
   Ztwisted.conchr   Ztwisted.python.compatr   r   r   r   r   objectr   r   r!   r#   r%   r'   r)   r,   r.   r/   r1   r2   r@   r   r   r   r   <module>   sD   


