U
    e]                     @   s   d dl mZmZmZ d dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZmZ dd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd ZdS )    )absolute_importdivisionprint_functionN)utils)UnsupportedAlgorithm)dsaeced25519rsac           	      C   s   |  dd}t|dk r td|d }|dkr6t}n2|dkrDt}n$|dkrRt}n|dkr`t}ntd	|d
 }zt	|}W n t
k
r   tdY nX t|\}}||krtd||||S )N       z7Key is not in the proper format or contains extra data.r   s   ssh-rsas   ssh-dss)s   ecdsa-sha2-nistp256s   ecdsa-sha2-nistp384s   ecdsa-sha2-nistp521s   ssh-ed25519zKey type is not supported.   z Key is not in the proper format.:Key header and key body contain different key type values.)splitlen
ValueError_load_ssh_rsa_public_key_load_ssh_dss_public_key_load_ssh_ecdsa_public_key_load_ssh_ed25519_public_keyr   base64Z	b64decode	TypeError_ssh_read_next_string)	databackendZ	key_partskey_typeloaderZkey_bodydecoded_dataZinner_key_typerest r   R/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/serialization/ssh.pyload_ssh_public_key   s4    r!   c                 C   s6   t |\}}t |\}}|r$tdt|||S NKey body contains extra bytes.)_ssh_read_next_mpintr   r
   ZRSAPublicNumbers
public_key)r   r   r   er   nr   r   r    r   8   s
    r   c           
      C   s`   t |\}}t |\}}t |\}}t |\}}|r<tdt|||}t||}	|	|S r"   )r$   r   r   ZDSAParameterNumbersZDSAPublicNumbersr%   )
r   r   r   pr   qgyZparameter_numbersZpublic_numbersr   r   r    r   B   s    r   c                 C   sv   t |\}}t |\}}| d| kr,td|r8tdtjtjtjd|  }t|ddkrhtdtj	
||S )Ns   ecdsa-sha2-r   r#   )s   nistp256s   nistp384s   nistp521r      z2Compressed elliptic curve points are not supported)r   r   r   Z	SECP256R1Z	SECP384R1Z	SECP521R1six
indexbytesNotImplementedErrorZEllipticCurvePublicKeyZfrom_encoded_point)expected_key_typer   r   Z
curve_namer   r   Zcurver   r   r    r   Q   s&    r   c                 C   s$   t |\}}|rtdtj|S r"   )r   r   r	   ZEd25519PublicKeyZfrom_public_bytes)r0   r   r   r   r   r   r   r    r   k   s    r   c                 C   sb   t | dk rtdtd| dd \}t | |d k rBtd| dd|  | d| d fS )z
    Retrieves the next RFC 4251 string value from the data.

    While the RFC calls these strings, in Python they are bytes objects.
    r,   zKey is not in the proper format>IN)r   r   structZunpack)r   Zstr_lenr   r   r    r   t   s    r   c                 C   s    t | \}}tj|ddd|fS )ze
    Reads the next mpint from the data.

    Currently, all mpints are interpreted as unsigned.
    ZbigF)	byteorderZsigned)r   r   Zint_from_bytes)r   Z
mpint_datar   r   r   r    r$      s     r$   c                 C   s   t dt| |  S )Nr1   )r2   Zpackr   )r   r   r   r    _ssh_write_string   s    r4   c                 C   s*   t | }t|dd@ r"d| }t|S )Nr          )r   Zint_to_bytesr-   r.   r4   )valuer   r   r   r    _ssh_write_mpint   s    
r8   )Z
__future__r   r   r   r   r2   r-   Zcryptographyr   Zcryptography.exceptionsr   Z)cryptography.hazmat.primitives.asymmetricr   r   r	   r
   r!   r   r   r   r   r   r$   r4   r8   r   r   r   r    <module>   s   '
	