U
    ¸~Hjƒ"  ã                	   @  sú  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	rjd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G dd„ deƒZeeƒG dd„ dƒƒZeeƒG dd„ dƒƒZeeƒG dd„ dƒƒZeeƒG dd„ dƒƒZeeƒG dd„ dƒƒZeeƒG dd„ dƒƒZeeƒG dd„ dƒƒZeeƒG d d!„ d!ƒƒZeƒ eƒ eƒ eƒ eƒ eƒ eƒ eƒ d"œZd#e d$< d%dd&œd'd(„Z!d%d)d&œd*d+„Z"d%d)d&œd,d-„Z#d%d	d&œd.d/„Z$d%d0d&œd1d2„Z%d3d4œd5d6„Z&d7S )8z
SSH key exchange handling.
é    )Úannotations)Úsha1Úsha256Úsha384Úsha512)ÚTYPE_CHECKINGÚProtocol)Ú	AttributeÚ	InterfaceÚimplementer)Úerror)Ú_Hashc                   @  s   e Zd Zddddœdd„ZdS )	Ú_HashFactory.Úbytesr   )ÚdataÚreturnc                 C  s   d S ©N© )Úselfr   r   r   ú:/tmp/pip-unpacked-wheel-ewod1tgb/twisted/conch/ssh/_kex.pyÚ__call__   s    z_HashFactory.__call__N).)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                   @  s2   e Zd ZU dZedƒZded< edƒZded< dS )	Ú_IKexAlgorithmzB
    An L{_IKexAlgorithm} describes a key exchange algorithm.
    z‹An 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      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.
    zdAn L{int} giving the prime number used in Diffie-Hellman key exchange, or L{None} if not applicable.z�An 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   ÿÿ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#   ?   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%   F   s   r%   c                   @  s   e Zd ZdZdZeZdS )Ú_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   s   r&   c                   @  s   e Zd ZdZdZeZdS )Ú_Curve25519SHA256LibSSHzN
    As L{_Curve25519SHA256}, but with a pre-standardized algorithm name.
    é   Nr(   r   r   r   r   r)   [   s   r)   c                   @  s   e Zd ZdZdZeZdS )Ú_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}.
    é   Nr(   r   r   r   r   r+   e   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-   v   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   r1   Œ   s   r1   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   r3   —   s   r3   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   r5   ¢   s   ÿr5   )ó   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]Ú_kexAlgorithmsr   )ÚkexAlgorithmr   c                 C  s    | t krt 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: )r8   r   Z
ConchError©r9   r   r   r   ÚgetKexÉ   s    r;   Úboolc                 C  s   t  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#   Ú
providedByr;   r:   r   r   r   ÚisEllipticCurveÛ   s    	r>   c                 C  s   t  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    r=   r;   r:   r   r   r   ÚisFixedGroupç   s    	r?   c                 C  s   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}).
    )r;   r   ©r9   Zkexr   r   r   ÚgetHashProcessoró   s    rA   ztuple[int, int]c                 C  s   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    r;   r"   r!   r@   r   r   r   ÚgetDHGeneratorAndPrimeÿ   s    
rB   zlist[bytes])r   c                    s¦   ddl m}  ddlm} ddlm} | ƒ }t ¡ ‰ tˆ ƒD ]V}| 	d¡rj| 
dd¡}| | ¡ || ¡}n| 	d¡r~| ¡ }n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.
    r   )Údefault_backend)Úec)Ú_curveTables   ecdhs   ecdsar7   Tc                   s
   ˆ |  j S r   )r   r:   ©ZkexAlgorithmsr   r   Ú<lambda>(  ó    z*getSupportedKeyExchanges.<locals>.<lambda>)Úkey)Zcryptography.hazmat.backendsrC   Z)cryptography.hazmat.primitives.asymmetricrD   Ztwisted.conch.ssh.keysrE   r8   ÚcopyÚlistÚ
startswithÚreplaceZ+elliptic_curve_exchange_algorithm_supportedZECDHZx25519_supportedÚpopÚsorted)rC   rD   rE   ÚbackendZkeyAlgorithmZkeyAlgorithmDsaÚ	supportedr   rF   r   ÚgetSupportedKeyExchanges  s*    
 ÿ

 
ÿrR   N)'r   Ú
__future__r   Úhashlibr   r   r   r   Útypingr   r   Zzope.interfacer	   r
   r   Ztwisted.conchr   r   r   r   r    r#   r%   r&   r)   r+   r-   r/   r1   r3   r5   r8   r   r;   r>   r?   rA   rB   rR   r   r   r   r   Ú<module>   sV   

	



ø