Spamworldpro Mini Shell
Spamworldpro


Server : Apache/2.4.52 (Ubuntu)
System : Linux webserver 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64
User : www-data ( 33)
PHP Version : 8.1.2-1ubuntu2.21
Disable Function : NONE
Directory :  /lib/python3/dist-packages/twisted/conch/ssh/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/twisted/conch/ssh/__pycache__/keys.cpython-310.pyc
o

�bV�@sdZddlZddlZddlZddlZddlZddlmZmZm	Z	ddl
mZmZddl
Z
ddlmZddlmZddlmZddlmZmZdd	lmZmZmZmZmZdd
lmZmZm Z ddl!m"Z"m#Z#ddl$m%Z&m'Z(dd
l)m*Z*ddl+m,Z,ddl-m.Z.m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5m6Z6ddl7m8Z8m9Z9ddl:m;Z;z
ddl<m=Z=m>Z>Wne?y�ddl<m@Z=mAZ>Ynwe�B�e�C�e�D�d�ZEdddd�ZFGdd�deG�ZHGdd�deG�ZIGd d!�d!eG�ZJGd"d#�d#e9�ZKGd$d%�d%eG�ZLd&d'�ZMGd(d)�d)�ZNd-d+d,�ZOdS).z0
Handling of RSA, DSA, ECDSA, and Ed25519 keys.
�N)�	b64encode�decodebytes�encodebytes)�md5�sha256)�utils)�InvalidSignature)�default_backend)�hashes�
serialization)�dsa�ec�ed25519�padding�rsa)�Cipher�
algorithms�modes)�load_pem_private_key�load_ssh_public_key)�decoder�encoder)�PyAsn1Error)�univ)�common�sexpy)�int_to_bytes)�	randbytes)�	iterbytes�nativeString)�
NamedConstant�Names)�_mutuallyExclusiveArguments)�decode_dss_signature�encode_dss_signature)�decode_rfc6979_signature�encode_rfc6979_signature)secdsa-sha2-nistp256secdsa-sha2-nistp384secdsa-sha2-nistp521snistp256snistp384snistp521)s	secp256r1s	secp384r1s	secp521r1c@�eZdZdZdS)�BadKeyErrorzj
    Raised when a key isn't what we expected from it.

    XXX: we really need to check for bad keys
    N��__name__�
__module__�__qualname__�__doc__�r.r.�8/usr/lib/python3/dist-packages/twisted/conch/ssh/keys.pyr(F�r(c@r')�EncryptedKeyErrorzb
    Raised when an encrypted key is presented to fromString/fromFile without
    a password.
    Nr)r.r.r.r/r1Nr0r1c@r')�BadFingerPrintFormatzS
    Raises when unsupported fingerprint formats are presented to fingerprint.
    Nr)r.r.r.r/r2Ur0r2c@seZdZdZe�Ze�ZdS)�FingerprintFormatsa�
    Constants representing the supported formats of key fingerprints.

    @cvar MD5_HEX: Named constant representing fingerprint format generated
        using md5[RFC1321] algorithm in hexadecimal encoding.
    @type MD5_HEX: L{twisted.python.constants.NamedConstant}

    @cvar SHA256_BASE64: Named constant representing fingerprint format
        generated using sha256[RFC4634] algorithm in base64 encoding
    @type SHA256_BASE64: L{twisted.python.constants.NamedConstant}
    N)r*r+r,r-r �MD5_HEX�
SHA256_BASE64r.r.r.r/r3[s
r3c@r')�PassphraseNormalizationErrorz�
    Raised when a passphrase contains Unicode characters that cannot be
    normalized using the available Unicode character database.
    Nr)r.r.r.r/r6lr0r6cCs8t|t�rtdd�|D��rt��t�d|��d�S|S)a�
    Normalize a passphrase, which may be Unicode.

    If the passphrase is Unicode, this follows the requirements of U{NIST
    800-63B, section
    5.1.1.2<https://pages.nist.gov/800-63-3/sp800-63b.html#memsecretver>}
    for Unicode characters in memorized secrets: it applies the
    Normalization Process for Stabilized Strings using NFKC normalization.
    The passphrase is then encoded using UTF-8.

    @type passphrase: L{bytes} or L{unicode} or L{None}
    @param passphrase: The passphrase to normalize.

    @return: The normalized passphrase, if any.
    @rtype: L{bytes} or L{None}
    @raises PassphraseNormalizationError: if the passphrase is Unicode and
    cannot be normalized using the available Unicode character database.
    css�|]
}t�|�dkVqdS)�CnN)�unicodedata�category)�.0�cr.r.r/�	<genexpr>�s�z'_normalizePassphrase.<locals>.<genexpr>�NFKCzUTF-8)�
isinstance�str�anyr6r8�	normalize�encode��
passphraser.r.r/�_normalizePassphrasess

rEc@s�eZdZdZedTdd��ZedTdd��Zedd��Zed	d
��Zedd��Z	ed
d��Z
edd��Zedd��Zedd��Z
edd��Zedd��Zedd��ZedUdd��ZedVdd��ZedVdd ��ZedVd!d"��ZedVd#d$��Zd%d&�Zd'ed(efd)d*�Zd(efd+d,�Zd-d.�Zd/d0�Zejfd1d2�Z d3d4�Z!d5d6�Z"d7d8�Z#d9d:�Z$d;d<�Z%d=d>�Z&e'd?d@gd?dAgg�dUdBdC��Z(dVdDdE�Z)dTdFdG�Z*dVdHdI�Z+dWdJdK�Z,dLdM�Z-dNdO�Z.dPdQ�Z/dRdS�Z0dS)X�Keyau
    An object representing a key.  A key can be either a public or
    private key.  A public key can verify a signature; a private key can
    create or verify a signature.  To generate a string that can be stored
    on disk, use the toString method.  If you have a private key, but want
    the string representation of the public key, use Key.public().toString().
    NcCs@t|d��}|�|��||�Wd�S1swYdS)a�
        Load a key from a file.

        @param filename: The path to load key data from.

        @type type: L{str} or L{None}
        @param type: A string describing the format the key data is in, or
        L{None} to attempt detection of the type.

        @type passphrase: L{bytes} or L{None}
        @param passphrase: The passphrase the key is encrypted with, or L{None}
        if there is no encryption.

        @rtype: L{Key}
        @return: The loaded key.
        �rbN)�open�
fromString�read)�cls�filename�typerD�fr.r.r/�fromFile�s$�zKey.fromFilecCs�t|t�r
|�d�}t|�}|dur|�|�}|dur"td|����t|d|����d�}|dur8td|����|jj	dkrH|rDtd��||�S|||�S)a
        Return a Key object corresponding to the string data.
        type is optionally the type of string, matching a _fromString_*
        method.  Otherwise, the _guessStringType() classmethod will be used
        to guess a type.  If the key is encrypted, passphrase is used as
        the decryption key.

        @type data: L{bytes}
        @param data: The key data.

        @type type: L{str} or L{None}
        @param type: A string describing the format the key data is in, or
        L{None} to attempt detection of the type.

        @type passphrase: L{bytes} or L{None}
        @param passphrase: The passphrase the key is encrypted with, or L{None}
        if there is no encryption.

        @rtype: L{Key}
        @return: The loaded key.
        �utf-8Nzcannot guess the type of �_fromString_zno _fromString method for �zkey not encrypted)
r>r?rBrE�_guessStringTyper(�getattr�upper�__code__�co_argcount)rK�datarMrD�methodr.r.r/rI�s



zKey.fromStringcCs�t�|�\}}|dkr t�|d�\}}}|t�||��t���S|dkrBt�|d�\}}}}	}|tj|	tj	|||d�d��t���S|t
vrW|tj�
t
|t�|d�d��S|dkrgt�|�\}
}|�|
�Std	|����)
a�
        Return a public key object corresponding to this public key blob.
        The format of a RSA public key blob is::
            string 'ssh-rsa'
            integer e
            integer n

        The format of a DSA public key blob is::
            string 'ssh-dss'
            integer p
            integer q
            integer g
            integer y

        The format of ECDSA-SHA2-* public key blob is::
            string 'ecdsa-sha2-[identifier]'
            integer x
            integer y

            identifier is the standard NIST curve name.

        The format of an Ed25519 public key blob is::
            string 'ssh-ed25519'
            string a

        @type blob: L{bytes}
        @param blob: The key data.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if the key type (the first string) is unknown.
        �ssh-rsarR�ssh-dss���p�q�g��y�parameter_numbers��ssh-ed25519�unknown blob type: )r�getNS�getMPr�RSAPublicNumbers�
public_keyr	r�DSAPublicNumbers�DSAParameterNumbers�_curveTabler
�EllipticCurvePublicKey�from_encoded_point�_fromEd25519Componentsr()rK�blob�keyType�rest�e�nr^r_r`rb�ar.r.r/�_fromString_BLOB�s,"����
zKey._fromString_BLOBcCst�|�\}}|dkr"t�|d�\}}}}}}	}|j|||||	d�S|dkr<t�|d�\}}	}
}}}|j||
||	|d�S|tvrnt|}
t�|d�\}}	}|t|
j�d�kr_t	d	||f��t�|�\}}|j
|	||d
�S|dkr�t�|d�\}}}|dd
�}|j||d�St	d|����)a6
        Return a private key object corresponding to this private key blob.
        The blob formats are as follows:

        RSA keys::
            string 'ssh-rsa'
            integer n
            integer e
            integer d
            integer u
            integer p
            integer q

        DSA keys::
            string 'ssh-dss'
            integer p
            integer q
            integer g
            integer y
            integer x

        EC keys::
            string 'ecdsa-sha2-[identifier]'
            string identifier
            string q
            integer privateValue

            identifier is the standard NIST curve name.

        Ed25519 keys::
            string 'ssh-ed25519'
            string a
            string k || a


        @type blob: L{bytes}
        @param blob: The key data.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if
            * the key type (the first string) is unknown
            * the curve name of an ECDSA key does not match the key type
        rZ��rurt�dr^r_r[��rbr`r^r_�xrR�asciiz.ECDSA curve name %r does not match key type %r)�encodedPoint�curve�privateValuereN� )�krf)rrgrh�_fromRSAComponents�_fromDSAComponentsrm�
_secToNist�namerBr(�_fromECEncodedPointrp)rKrqrrrsrurtrz�ur^r_r`rbr}r��	curveNamer�rv�combinedr�r.r.r/�_fromString_PRIVATE_BLOBs2.���zKey._fromString_PRIVATE_BLOBcCs4|�d�r
|t|t���St|��d�}|�|�S)a�
        Return a public key object corresponding to this OpenSSH public key
        string.  The format of an OpenSSH public key string is::
            <key type> <base64-encoded public key blob>

        @type data: L{bytes}
        @param data: The key data.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if the blob type is unknown.
        s
ecdsa-sha2rd)�
startswithrr	r�splitrw)rKrXrqr.r.r/�_fromString_PUBLIC_OPENSSH\s

zKey._fromString_PUBLIC_OPENSSHcCs|����}td�|dd���}|�d�std��|td�d�}t�|d�\}}}}t	�
d|dd	��d
}	|	dkr@td��t�|d	d�d�\}
}}
|d
kr�|sWtd��|dvrmtj
}d}
t|dd��d}|
}ntd|����|dkr�t�|�\}}t	�
d|dd	��d
}tj|||||dd�}ntd|����t|�|
d
kr�td��t||d|��t�||||��t�d���}|�|�|��}n
|d
kr�td|f��|}t	�
d|dd	��d
}t	�
d|d	d��d
}||kr�td||f��|�|dd��S)a*
        Return a private key object corresponding to this OpenSSH private key
        string, in the "openssh-key-v1" format introduced in OpenSSH 6.5.

        The format of an openssh-key-v1 private key string is::
            -----BEGIN OPENSSH PRIVATE KEY-----
            <base64-encoded SSH protocol string>
            -----END OPENSSH PRIVATE KEY-----

        The SSH protocol string is as described in
        U{PROTOCOL.key<https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.key>}.

        @type data: L{bytes}
        @param data: The key data.

        @type passphrase: L{bytes} or L{None}
        @param passphrase: The passphrase the key is encrypted with, or L{None}
        if it is not encrypted.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if
            * a passphrase is provided for an unencrypted key
            * the SSH protocol encoding is incorrect
        @raises EncryptedKeyError: if
            * a passphrase is not provided for an encrypted key
        �rd����openssh-key-v1z"unknown OpenSSH private key formatN��!Lr\rzDonly OpenSSH private key files containing a single key are supportedrR�none�0Passphrase must be provided for an encrypted key)s
aes128-ctrs
aes192-ctr�
aes256-ctr�rx��unknown encryption type �bcryptT)�ignore_few_roundszunknown KDF type zbad padding��backendz*private key specifies KDF %r but no cipherz#check values do not match: %d != %d)�strip�
splitlinesr�joinr�r(�lenrrg�struct�unpackr1r�AES�int�bcrypt�kdfrr�CTRr	�	decryptor�update�finalizer�)rKrXrD�lines�keyList�cipherr��
kdfOptionsrsru�_�encPrivKeyList�algorithmClass�	blockSize�keySize�ivSize�salt�rounds�decKeyr��privKeyList�check1�check2r.r.r/�_fromPrivateOpenSSH_v1qsl
���	���zKey._fromPrivateOpenSSH_v1cs@|����}|ddd�}|d�d�r�|std��z|d�dd�\}}|���d	d�\}�WntyAtd
|d����w|dvr_tj	}t
|�d�d�d
}	t��dkr^td��n|dkrstj}d}	t��dkrrtd��ntd|����t
t�fdd�tdt��d�D���}
t||
dd
����}t|||
dd
����}||d|	�}
td�|dd���}t||
�t�|
�t�d���}|�|�|��}t|dd��}|d|�}n
d�|dd��}t|�}z	t�|�d}Wnt�y}ztd|����d}~ww|dk�r|t||t���S|dk�r`t|�dk�r+|d}t|�dk�r6td ��d!d�|dd"�D�\}}}}}}}}|t j!||||||t j"||d#�d$��#t���S|d%k�r�d&d�|dd�D�\}}}}}t|�dk�r�td'��|t$j%|t$j&|t$j'|||d(�d)�d*�j#t�d��Std+|����),a�
        Return a private key object corresponding to this OpenSSH private key
        string, in the old PEM-based format.

        The format of a PEM-based OpenSSH private key string is::
            -----BEGIN <key type> PRIVATE KEY-----
            [Proc-Type: 4,ENCRYPTED
            DEK-Info: DES-EDE3-CBC,<initialization value>]
            <base64-encoded ASN.1 structure>
            ------END <key type> PRIVATE KEY------

        The ASN.1 structure of a RSA key is::
            (0, n, e, d, p, q)

        The ASN.1 structure of a DSA key is::
            (0, p, q, g, y, x)

        The ASN.1 structure of a ECDSA key is::
            (ECParameters, OID, NULL)

        @type data: L{bytes}
        @param data: The key data.

        @type passphrase: L{bytes} or L{None}
        @param passphrase: The passphrase the key is encrypted with, or L{None}
        if it is not encrypted.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if
            * a passphrase is provided for an unencrypted key
            * the ASN.1 encoding is incorrect
        @raises EncryptedKeyError: if
            * a passphrase is not provided for an encrypted key
        r����rd�Proc-Type: 4,ENCRYPTEDr�rR� �,zinvalid DEK-info )sAES-128-CBCsAES-256-CBC�-r�r�zAES encrypted key with a bad IVsDES-EDE3-CBC�r�zDES encrypted key with a bad IVr�c3s&�|]}t�||d�d�VqdS)rRr�N�r��r:�i��ivdatar.r/r<
s�$z.Key._fromPrivateOpenSSH_PEM.<locals>.<genexpr>Nr�r�r�r�z(Failed to decode key (Bad Passphrase?): sECsRSArxz!RSA key failed to decode properlycs��|]}t|�VqdS�Nr��r:�valuer.r.r/r<-���	�rtru�r^r_rz�dmp1�dmq1�iqmp�public_numberssDSAcsr�r�r�r�r.r.r/r<:r�z!DSA key failed to decode properlyr]ra�r}r��unknown key type )(r�r�r�r1r��rstrip�
ValueErrorr(rr�r�r��	TripleDES�bytes�	bytearray�ranger�digestrr�rr�CBCr	r�r�r��ord�
berDecoder�decoderrr�RSAPrivateNumbersri�private_keyr�DSAPrivateNumbersrkrl)rKrXrDr��kindr��cipherIVInfor�r�r��iv�ba�bbr��b64Datar��keyData�	removeLen�
decodedKey�	asn1Errorrurtrzr^r_r�r�r�r`rbr}r.r�r/�_fromPrivateOpenSSH_PEM�s�%���� �����

&��
 ����	zKey._fromPrivateOpenSSH_PEMcCs4|����ddd�dkr|�||�S|�||�S)a�
        Return a private key object corresponding to this OpenSSH private key
        string.  If the key is encrypted, passphrase MUST be provided.
        Providing a passphrase for an unencrypted key is an error.

        @type data: L{bytes}
        @param data: The key data.

        @type passphrase: L{bytes} or L{None}
        @param passphrase: The passphrase the key is encrypted with, or L{None}
        if it is not encrypted.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if
            * a passphrase is provided for an unencrypted key
            * the encoding is incorrect
        @raises EncryptedKeyError: if
            * a passphrase is not provided for an encrypted key
        rr�r�sOPENSSH)r�r�r�r�)rKrXrDr.r.r/�_fromString_PRIVATE_OPENSSHHszKey._fromString_PRIVATE_OPENSSHcCs�t�t|dd���}|ddksJ�i}|ddd�D]\}}t�t�|��d||<q|dddkrG|j|d|d|d	|d
d�S|dddkrZ|j|d
|dd�Std|dd����)a
        Return a public key corresponding to this LSH public key string.
        The LSH public key string format is::
            <s-expression: ('public-key', (<key type>, (<name, <value>)+))>

        The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e.
        The names for a DSA (key type 'dsa') key are: y, g, p, q.

        @type data: L{bytes}
        @param data: The key data.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if the key type is unknown
        rdr�r�
public-keyN�dsa�y�g�p�q�rbr`r^r_�rsa-pkcs1-sha1�n�e�rurt�unknown lsh key type )	r�parserrrh�NSr�r�r(�rKrX�sexp�kdr�r.r.r/�_fromString_PUBLIC_LSHes�zKey._fromString_PUBLIC_LSHcCs0t�|�}|ddks
J�i}|ddd�D]\}}t�t�|��d||<q|dddkrPt|�dks<Jt|���|j|d|d|d	|d
|dd�S|ddd
kr�t|�dksdJt|���|d	|d
kry|d
|d	|d	<|d
<|j|d|d|d|d	|d
d�Std|dd����)a+
        Return a private key corresponding to this LSH private key string.
        The LSH private key string format is::
            <s-expression: ('private-key', (<key type>, (<name>, <value>)+))>

        The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e, d, p, q.
        The names for a DSA (key type 'dsa') key are: y, g, p, q, x.

        @type data: L{bytes}
        @param data: The key data.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if the key type is unknown
        r�private-keyrdNr�r{r�r�r�r��xr|�	rsa-pkcs1r�r�r��dryr�)	rr�rrhr�r�r�r�r(rr.r.r/�_fromString_PRIVATE_LSH�s$
��zKey._fromString_PRIVATE_LSHcCs�t�|�\}}|dkr8t�|�\}}t�|�\}}t�|�\}}t�|�\}}t�|�\}}|j|||||d�S|dkrqt�|�\}}t�|�\}	}t�|�\}
}t�|�\}}t�|�\}}t�|�\}}|j|
||	|||d�Std|����)a�
        Return a private key object corresponsing to the Secure Shell Key
        Agent v3 format.

        The SSH Key Agent v3 format for a RSA key is::
            string 'ssh-rsa'
            integer e
            integer d
            integer n
            integer u
            integer p
            integer q

        The SSH Key Agent v3 format for a DSA key is::
            string 'ssh-dss'
            integer p
            integer q
            integer g
            integer y
            integer x

        @type data: L{bytes}
        @param data: The key data.

        @return: A new key.
        @rtype: L{twisted.conch.ssh.keys.Key}
        @raises BadKeyError: if the key type (the first string) is unknown
        r[r|rZ�rurtrzr^r_r�r�)rrgrhr�r�r()rKrXrrr^r_r`rbr}rtrzrur�r.r.r/�_fromString_AGENTV3�s"zKey._fromString_AGENTV3cCs�|�d�s
|�d�rdS|�d�rdS|�d�rdS|�d�r!d	S|�d
�s0|�d�s0|�d�rPt�|�\}}d
}|rH|d7}t�|�\}}|s;|dkrNdSdSdS)z�
        Guess the type of key in data.  The types map to _fromString_*
        methods.

        @type data: L{bytes}
        @param data: The key data.
        sssh-�ecdsa-sha2-�public_opensshs
-----BEGIN�private_openssh�{�
public_lsh�(�private_lshsssh-s
ecdsa-sssh-ed25519rrdr\�agentv3rqN)r�rrgrh)rKrX�ignoredrs�countr.r.r/rS�s0	


�����zKey._guessStringTypec

Csntj||d�}|dur|�t��}||�Stj|||t�||�t�||�t�||�|d�}	|	�t��}||�S)a�
        Build a key from RSA numerical components.

        @type n: L{int}
        @param n: The 'n' RSA variable.

        @type e: L{int}
        @param e: The 'e' RSA variable.

        @type d: L{int} or L{None}
        @param d: The 'd' RSA variable (optional for a public key).

        @type p: L{int} or L{None}
        @param p: The 'p' RSA variable (optional for a public key).

        @type q: L{int} or L{None}
        @param q: The 'q' RSA variable (optional for a public key).

        @type u: L{int} or L{None}
        @param u: The 'u' RSA variable. Ignored, as its value is determined by
        p and q.

        @rtype: L{Key}
        @return: An RSA key constructed from the values as given.
        r�Nr�)	rrirjr	r��rsa_crt_dmp1�rsa_crt_dmq1�rsa_crt_iqmpr�)
rKrurtrzr^r_r��
publicNumbers�	keyObject�privateNumbersr.r.r/r��s
�


�	zKey._fromRSAComponentsc	CsXtj|tj|||d�d�}|dur|�t��}||�Stj||d�}|�t��}||�S)a
        Build a key from DSA numerical components.

        @type y: L{int}
        @param y: The 'y' DSA variable.

        @type p: L{int}
        @param p: The 'p' DSA variable.

        @type q: L{int}
        @param q: The 'q' DSA variable.

        @type g: L{int}
        @param g: The 'g' DSA variable.

        @type x: L{int} or L{None}
        @param x: The 'x' DSA variable (optional for a public key)

        @rtype: L{Key}
        @return: A DSA key constructed from the values as given.
        r]raNr�)rrkrlrjr	r�r�)	rKrbr^r_r`r}rrrr.r.r/r�)s��zKey._fromDSAComponentscCsRtj||t|d�}|dur|�t��}||�Stj||d�}|�t��}||�S)a�
        Build a key from EC components.

        @param x: The affine x component of the public point used for verifying.
        @type x: L{int}

        @param y: The affine y component of the public point used for verifying.
        @type y: L{int}

        @param curve: NIST name of elliptic curve.
        @type curve: L{bytes}

        @param privateValue: The private value.
        @type privateValue: L{int}
        �r}rbr�N)�
private_valuer�)r
�EllipticCurvePublicNumbersrmrjr	�EllipticCurvePrivateNumbersr�)rKr}rbr�r�rrrr.r.r/�_fromECComponentsLs
���zKey._fromECComponentscCs>|durtj�t||�}||�St�|t|t��}||�S)aa
        Build a key from an EC encoded point.

        @param encodedPoint: The public point encoded as in SEC 1 v2.0
        section 2.3.3.
        @type encodedPoint: L{bytes}

        @param curve: NIST name of elliptic curve.
        @type curve: L{bytes}

        @param privateValue: The private value.
        @type privateValue: L{int}
        N)r
rnrorm�derive_private_keyr	)rKrr�r�rr.r.r/r�ls���zKey._fromECEncodedPointcCs0|durtj�|�}||�Stj�|�}||�S)aBuild a key from Ed25519 components.

        @param a: The Ed25519 public key, as defined in RFC 8032 section
            5.1.5.
        @type a: L{bytes}

        @param k: The Ed25519 private key, as defined in RFC 8032 section
            5.1.5.
        @type k: L{bytes}
        N)r�Ed25519PublicKey�from_public_bytes�Ed25519PrivateKey�from_private_bytes)rKrvr�rr.r.r/rp�s

�zKey._fromEd25519ComponentscCs
||_dS)z�
        Initialize with a private or public
        C{cryptography.hazmat.primitives.asymmetric} key.

        @param keyObject: Low level key.
        @type keyObject: C{cryptography.hazmat.primitives.asymmetric} key.
        N)�
_keyObject)�selfrr.r.r/�__init__�s
zKey.__init__�other�returncCs.t|t�r|��|��ko|��|��kStS)zN
        Return True if other represents an object with the same key.
        )r>rFrMrX�NotImplemented)r&r(r.r.r/�__eq__�s
 z
Key.__eq__cCs�|��dkrO|��}|d�d�}|��r d|dd��d�}n
d|dd��d�}t|���D]\}}|dkr@|d	|��7}q0|d
|�d|��7}q0|dSd
t|���|��r[dp\d|��fg}t|�����D]T\}}|�d|�d��|��dkr�|nt	�
|�dd�}|r�|dd�}|dd�}d}	t|�D]}
|	t|
�d�d�}	q�t
|�dkr�|	dd�}	|�d|	�|s�qk|dd|d<d
�|�S)z@
        Return a pretty representation of this object.
        �ECr�rPz<Elliptic Curve Public Key (���Nz bits)z<Elliptic Curve Private Key (z	
curve:
	�
z:
	z>
z<%s %s (%s bits)z
Public KeyzPrivate Keyzattr �:�Ed25519r\���02xr��	�>)rMrXr��isPublic�sorted�itemsr�size�appendr�MPrr�r�r�)r&rXr��outr��vr��by�m�or;r.r.r/�__repr__�sD
���"��	
zKey.__repr__cCst|jtjtjtjtj	f�S)zl
        Check if this instance is a public key.

        @return: C{True} if this is a public key.
        )
r>r%r�RSAPublicKeyr�DSAPublicKeyr
rnrr!�r&r.r.r/r6�s��zKey.isPubliccCs|��r|St|j���S)z�
        Returns a version of this key containing only the public key data.
        If this is a public key, this may or may not be the same object
        as self.

        @rtype: L{Key}
        @return: A public key.
        )r6rFr%rjrDr.r.r/�public�s	z
Key.publiccCsb|tjurttt|�������S|tjur*td�dd�t	t
|������D���Std|����)aO
        The fingerprint of a public key consists of the output of the
        message-digest algorithm in the specified format.
        Supported formats include L{FingerprintFormats.MD5_HEX} and
        L{FingerprintFormats.SHA256_BASE64}

        The input to the algorithm is the public key data as specified by [RFC4253].

        The output of sha256[RFC4634] algorithm is presented to the
        user in the form of base64 encoded sha256 hashes.
        Example: C{US5jTUa0kgX5ZxdqaGF0yGRu8EgKXHNmoT8jHKo1StM=}

        The output of the MD5[RFC1321](default) algorithm is presented to the user as
        a sequence of 16 octets printed as hexadecimal with lowercase letters
        and separated by colons.
        Example: C{c1:b1:30:29:d7:b8:de:6c:97:77:10:d7:46:41:63:87}

        @param format: Format for fingerprint generation. Consists
            hash function and representation format.
            Default is L{FingerprintFormats.MD5_HEX}

        @since: 8.2

        @return: the user presentation of this L{Key}'s fingerprint, as a
        string.

        @rtype: L{str}
        �:cSsg|]}t�|��qSr.)�binascii�hexlify�r:r}r.r.r/�
<listcomp>sz#Key.fingerprint.<locals>.<listcomp>z Unsupported fingerprint format: )r3r5rrrrqr�r4r�rrr2)r&�formatr.r.r/�fingerprint�s

��zKey.fingerprintcCspt|jtjtjf�rdSt|jtjtjf�rdSt|jtj	tj
f�r$dSt|jtjtj
f�r0dStd|j����)z�
        Return the type of the object we wrap.  Currently this can only be
        'RSA', 'DSA', 'EC', or 'Ed25519'.

        @rtype: L{str}
        @raises RuntimeError: If the object type is unknown.
        �RSA�DSAr,r0zunknown type of object: )r>r%rrB�
RSAPrivateKeyrrC�
DSAPrivateKeyr
rn�EllipticCurvePrivateKeyrr!r#�RuntimeErrorrDr.r.r/rM!s��zKey.typecCs8|��dkrdt|jjj�d�Sdddd�|��S)aK
        Get the type of the object we wrap as defined in the SSH protocol,
        defined in RFC 4253, Section 6.6. Currently this can only be b'ssh-rsa',
        b'ssh-dss' or b'ecdsa-sha2-[identifier]'.

        identifier is the standard NIST curve name

        @return: The key type format.
        @rtype: L{bytes}
        r,rr~rZr[re)rMrNr0)rMr�r%r�r�rBrDr.r.r/�sshType8s���zKey.sshTypecCs<|jdurdS|��dkr|jjjS|��dkrdS|jjS)zv
        Return the size of the object we wrap.

        @return: The size of the key.
        @rtype: L{int}
        Nrr,r0�)r%rMr��key_sizerDr.r.r/r9Ns

zKey.sizec	Cs�t|jtj�r|j��}|j|jd�St|jtj�r5|j��}|jj|jj|j	|j
|jt�|j|j
�d�St|jt
j�rO|j��}|j|jj|jj
|jjd�St|jt
j�ro|j��}|j|jj|jjj|jjj
|jjjd�St|jtj�r�|j��}|j|j|��d�St|jtj�r�|j��}|jj|jj|j|��d�St|jtj�r�d|j�tjjtjj�iSt|jtj �r�|j�!��tjjtjj�|j�"tjjtj#jt�$��d�St%d	|j����)
z_
        Return the values of the public key as a dictionary.

        @rtype: L{dict}
        r�r	r�)r}rbr`r^r_r)r}rbr�r�rv)rvr�zUnexpected key type: )&r>r%rrBr�rurtrO�private_numbersrzr^r_rrrCrbrcr`rPr}r
rnrSrQrrr!�public_bytesr�Encoding�Raw�PublicFormatr#rj�
private_bytes�
PrivateFormat�NoEncryptionrR)r&�numbersr.r.r/rX]st
�
�	
�
�
�
���
���zKey.datacCs|��}|��}|dkrt�d�t�|d�t�|d�S|dkrDt�d�t�|d�t�|d�t�|d	�t�|d
�S|dkrx|jjjdd
}t�|d�t�|ddd��t�dt�	|d|�t�	|d
|��S|dkr�t�d�t�|d�St
d|����)a�
        Return the public key blob for this key. The blob is the
        over-the-wire format for public keys.

        SECSH-TRANS RFC 4253 Section 6.6.

        RSA keys::
            string 'ssh-rsa'
            integer e
            integer n

        DSA keys::
            string 'ssh-dss'
            integer p
            integer q
            integer g
            integer y

        EC keys::
            string 'ecdsa-sha2-[identifier]'
            integer x
            integer y

            identifier is the standard NIST curve name

        Ed25519 keys::
            string 'ssh-ed25519'
            string a

        @rtype: L{bytes}
        rMrZrtrurNr[r^r_r`rbr,�r�r����N�r}r0rerv�unknown key type: )rMrXrr�r;r%r�rUrrr()r&rMrX�
byteLengthr.r.r/rq�s@ &�����������	zKey.blobcCs�|��}|��}|dkrCt�|d|d�}t�d�t�|d�t�|d�t�|d�t�|�t�|d�t�|d�S|dkrot�d	�t�|d�t�|d�t�|d
�t�|d�t�|d�S|d
kr�|j���	t
jjt
j
j�}t�|d�t�|ddd��t�|�t�|d�S|dkr�t�d�t�|d�t�|d|d�Std|����)a1
        Return the private key blob for this key. The blob is the
        over-the-wire format for private keys:

        Specification in OpenSSH PROTOCOL.agent

        RSA keys::

            string 'ssh-rsa'
            integer n
            integer e
            integer d
            integer u
            integer p
            integer q

        DSA keys::

            string 'ssh-dss'
            integer p
            integer q
            integer g
            integer y
            integer x

        EC keys::

            string 'ecdsa-sha2-[identifier]'
            integer x
            integer y
            integer privateValue

            identifier is the NIST standard curve name.

        Ed25519 keys::

            string 'ssh-ed25519'
            string a
            string k || a
        rMr^r_rZrurtrzrNr[r`rbr}r,r�r`Nr�r0rervr�rb)rMrXrrrr�r;r%rjrWrrX�X962rZ�UncompressedPointr()r&rMrXr��encPubr.r.r/�privateBlob�sh)�������	������
��������zKey.privateBlob�extra�commentrDcCs�|durtjdtdd�|��r|}n|}t|t�r|�d�}t|�}t|d|�	���d�}|dur9t
d|����||||d�S)	a
        Create a string representation of this key.  If the key is a private
        key and you want the representation of its public key, use
        C{key.public().toString()}.  type maps to a _toString_* method.

        @param type: The type of string to emit.  Currently supported values
            are C{'OPENSSH'}, C{'LSH'}, and C{'AGENTV3'}.
        @type type: L{str}

        @param extra: Any extra data supported by the selected format which
            is not part of the key itself.  For public OpenSSH keys, this is
            a comment.  For private OpenSSH keys, this is a passphrase to
            encrypt with.  (Deprecated since Twisted 20.3.0; use C{comment}
            or C{passphrase} as appropriate instead.)
        @type extra: L{bytes} or L{unicode} or L{None}

        @param subtype: A subtype of the requested C{type} to emit.  Only
            supported for private OpenSSH keys, for which the currently
            supported subtypes are C{'PEM'} and C{'v1'}.  If not given, an
            appropriate default is used.
        @type subtype: L{str} or L{None}

        @param comment: A comment to include with the key.  Only supported
            for OpenSSH keys.

            Present since Twisted 20.3.0.

        @type comment: L{bytes} or L{unicode} or L{None}

        @param passphrase: A passphrase to encrypt the key with.  Only
            supported for private OpenSSH keys.

            Present since Twisted 20.3.0.

        @type passphrase: L{bytes} or L{unicode} or L{None}

        @rtype: L{bytes}
        Nz�The 'extra' argument to twisted.conch.ssh.keys.Key.toString was deprecated in Twisted 20.3.0; use 'comment' or 'passphrase' instead.r�)�
stacklevelrP�
_toString_rb)�subtyperirD)�warnings�warn�DeprecationWarningr6r>r?rBrErTrUr()r&rMrhrlrirDrYr.r.r/�toString8s -�

zKey.toStringcCsn|��dkr|s
d}|j�tjjtjj�d|��St|�	���
dd�}|s)d}|��d|d|��S)a
        Return a public OpenSSH key string.

        See _fromString_PUBLIC_OPENSSH for the string format.

        @type comment: L{bytes} or L{None}
        @param comment: A comment to include with the key, or L{None} to
        omit the comment.
        r,r�r��
)rMr%rWrrX�OpenSSHrZr�rrq�replacerS)r&rir�r.r.r/�_toPublicOpenSSHzs 
����zKey._toPublicOpenSSHcs�|r%tj}d}d}|jd}d}|}t�|�}	d}
t�|	�t�d|
�}nd}d}d}d}t�d	�}|||�	�t�|p>d�}
d
}t
|
�|r\|d7}|
t|d@f�7}
t
|
�|sI|r�t�
||	||d�}t||d
|��t�||||��t�d���}|�|
�|��}n|
}dt�|�t�|�t�|�t�dd�t�|���t�|�}t|��dd��dg�fdd�td
t
��d�D�dg}d�|�dS)aP
        Return a private OpenSSH key string, in the "openssh-key-v1" format
        introduced in OpenSSH 6.5.

        See _fromPrivateOpenSSH_v1 for the string format.

        @type passphrase: L{bytes} or L{None}
        @param passphrase: The passphrase to encrypt the key with, or L{None}
        if it is not encrypted.
        r�r�r�r��dr�r�r�r\rrd�Nr�r�rqs#-----BEGIN OPENSSH PRIVATE KEY-----c�g|]
}�||d��qS��@r.r��r�r.r/rJ��z,Key._toPrivateOpenSSH_v1.<locals>.<listcomp>rys!-----END OPENSSH PRIVATE KEY-----)rr��
block_sizer�secureRandomrr�r��packrgr�r�r�r�rrr�r	�	encryptorr�r�rqrrsr�r�)r&rirDr��
cipherName�kdfNamer�r�r�r�r�r��checkr��padByte�encKeyrr�rqr�r.rzr/�_toPrivateOpenSSH_v1�sl


������
����	���zKey._toPrivateOpenSSH_v1c
sl|��dkr|s
t��}nt�|�}|j�tjjtjj	|�S|��dkr(t
d��|��}d�d|���
d�df�g}|��dkrm|d	|d
}}t�||�}d|d|d
|d|||d|d|d|d|f	}nd|d	|d
|d|d|df}t��}	tt��|�D]
\}
}|	�|
t�|��q�t�
|	�}|�rt�d�}
d�dd�t|
�D��}|�
d�}|�d�|�d|d�t||
���}t|||
���}||dd�}dt|�d}|t|f�|7}t t!�"|�t#�$|
�t%�d��&�}|�'|�|�(�}t)|��*dd��|�fdd�t+dt��d�D�7}|�d�d|���
d�df��d�|�S) a,
        Return a private OpenSSH key string, in the old PEM-based format.

        See _fromPrivateOpenSSH_PEM for the string format.

        @type passphrase: L{bytes} or L{None}
        @param passphrase: The passphrase to encrypt the key with, or L{None}
        if it is not encrypted.
        r,r0zBcannot serialize Ed25519 key to OpenSSH PEM format; use v1 insteadr�s-----BEGIN r~s PRIVATE KEY-----rMr^r_rrurtrzrdr`rbr}r�r2cSsg|]}t|�d��qS)�02X)r�rIr.r.r/rJ
sz-Key._toPrivateOpenSSH_PEM.<locals>.<listcomp>r�sDEK-Info: DES-EDE3-CBC,rqNr�r�crwrxr.r�rzr.r/rJr{rys	-----END ),rMrr]�BestAvailableEncryptionr%r[rX�PEMr\�TraditionalOpenSSLr�rXr�rBrrr�Sequence�zip�	itertoolsr�setComponentByPosition�Integer�
berEncoderrr}rr:rr�r�r�rrr�rr�r	rr�r�rrsr�)r&rDrrXr�r^r_r��objData�asn1Sequence�indexr��asn1Datar��hexivr�r�r��padLenr.rzr/�_toPrivateOpenSSH_PEM�sv


�����$



��"�
zKey._toPrivateOpenSSH_PEMcCsh|��r
|j|d�S|dks|dur|��dkr|j||d�S|dus'|dkr-|j|d�Std|����)	ar
        Return a public or private OpenSSH string.  See
        L{_fromString_PUBLIC_OPENSSH} and L{_fromPrivateOpenSSH_PEM} for the
        string formats.

        @param subtype: A subtype to emit.  Only supported for private keys,
            for which the currently supported subtypes are C{'PEM'} and C{'v1'}.
            If not given, an appropriate default is used.
        @type subtype: L{str} or L{None}

        @param comment: Comment for a public key.
        @type comment: L{bytes}

        @param passphrase: Passphrase for a private key.
        @type passphrase: L{bytes}

        @rtype: L{bytes}
        )ri�v1Nr0)rirDr�rCzunknown subtype )r6rtrMr�r�r�)r&rlrirDr.r.r/�_toString_OPENSSH!szKey._toString_OPENSSHcKs�|��}|��}|��r�|dkr2t�dddt�|d�dd�gdt�|d	�dd�gggg�}nE|d
krpt�dddt�|d
�dd�gdt�|d�dd�gdt�|d�dd�gdt�|d�dd�gggg�}ntd|����dt|��	dd�dS|dk�r|d
|d}}t
�||�}t�dddt�|d�dd�gdt�|d	�dd�gdt�|d�dd�gdt�|�dd�gdt�|�dd�gdt�|d|d�dd�gdt�|d|d�dd�gd t�|�dd�gg	gg�S|d
k�rLt�dddt�|d
�dd�gdt�|d�dd�gdt�|d�dd�gdt�|d�dd�gd!t�|d"�dd�gggg�Std|�d#���)$z�
        Return a public or private LSH key.  See _fromString_PUBLIC_LSH and
        _fromString_PRIVATE_LSH for the key formats.

        @rtype: L{bytes}
        rMr�r�r�rur\Nr�rtrNr�r�r^r�r_r�r`r�rbr�rrqr��}rrrrz�ard�b�crr}�')rXrMr6rr~rr;r(rrsrr)r&�kwargsrXrMr�r^r_r�r.r.r/�
_toString_LSH>sv��������
  ����
����zKey._toString_LSHcKs�|��}|��sJ|��dkr#|d|d|d|d|d|df}n|��dkr:|d|d|d	|d
|df}t�|���d�ttj|��Sd
S)z�
        Return a private Secure Shell Agent v3 key.  See
        _fromString_AGENTV3 for the key format.

        @rtype: L{bytes}
        rMrtrzrur�r^r_rNr`rbr}r�N)	rXr6rMrr�rSr��mapr;)r&r�rX�valuesr.r.r/�_toString_AGENTV3�s�" �zKey._toString_AGENTV3cCs�|��}|dkr|j�|t��t���}t�|�}n�|dkr;|j�|t���}t	|�\}}t�t
|d�t
|d��}n�|dkr�|��}|dkrLt��}n
|dkrUt�
�}nt��}|j�|t�|��}	t	|	�\}}t
|�}
t
|�}t|
d�tur�t|
d�}n|
d}|d@r�d	|
}
t|d�tur�t|d�}
n|d}
|
d@r�d	|}t�t�|
�t�|��}n
|d
kr�t�|j�|��}t�|���|S)z�
        Sign some data with this key.

        SECSH-TRANS RFC 4253 Section 6.6.

        @type data: L{bytes}
        @param data: The data to sign.

        @rtype: L{bytes}
        @return: A signature for the given data.
        rMrN�r,rT�r��r0)rMr%�signr�PKCS1v15r
�SHA1rr�r#rr9�SHA256�SHA384�SHA512r
�ECDSAr?r�rS)r&rXrr�sig�ret�r�sr��hashSize�	signaturerG�sb�rcomp�scompr.r.r/r��sB

zKey.signcCs�t|�dkrdt�|�}}nt�|�\}}||��krdS|��}|dkrA|j}|��s1|��}t�|�d|t	�
�t��f}n�|dkrxt�|�d}t
�|dd�d	�}t
�|dd�d	�}	t||	�}|j}|��sp|��}||t��f}nk|d
kr�t�|�d}t�|d�\}
}}t
�|
d	�}t
�|d	�}	t||	�}|j}|��s�|��}|��}
|
dkr�t��}n
|
d
kr�t��}nt��}||t�|�f}n|dkr�|j}|��s�|��}t�|�d|f}z|j|�WdSty�YdSw)a
        Verify a signature using this key.

        @type signature: L{bytes}
        @param signature: The signature to verify.

        @type data: L{bytes}
        @param data: The signed data.

        @rtype: L{bool}
        @return: C{True} if the signature is valid.
        �(r[FrMrrNNr��bigr,rRrTr�r0T)r�rr�rgrSrMr%r6rjrr�r
r�r��
from_bytesr$r9r�r�r�r
r��verifyr)r&r�rX�
signatureTyperrr��args�concatenatedSignaturer�r��rstr�sstrrsr�r�r.r.r/r��sf
�



��z
Key.verify)NN)NNNNr�)NNN)1r*r+r,r-�classmethodrOrIrwr�r�r�r�r�rrr
rSr�r�rr�rpr'�object�boolr+r?rAr6rEr3r4rLrMrSr9rXrqrgr"rprtr�r�r�r�r�r�r�r.r.r.r/rF�s|'
8
J

X
}


%
0
,"
	,(L<S��
<

>
ORBrF�cCs�|��jdd�|��s(tjd|t�d�}|jtjj	tj
jt��d�}|�
|�|�d��}tj|��dt�d�}t|�Wd�S1sGwYdS)	a�
    This function returns a persistent L{Key}.

    The key is loaded from a PEM file in C{location}. If it does not exist, a
    key with the key size of C{keySize} is generated and saved.

    @param location: Where the key is stored.
    @type location: L{twisted.python.filepath.FilePath}

    @param keySize: The size of the key, if it needs to be generated.
    @type keySize: L{int}

    @returns: A persistent key.
    @rtype: L{Key}
    T)�ignoreExistingDirectoryi)�public_exponentrUr�)�encodingrK�encryption_algorithmrGN)�passwordr�)�parent�makedirs�existsr�generate_private_keyr	r[rrXr�r\r�r]�
setContentrHrrJrF)�locationr��
privateKey�pem�keyFiler.r.r/�_getPersistentRSAKey5s"��
�$�r�)r�)Pr-rGr�r�r8rm�base64rrr�hashlibrrr��cryptographyr�cryptography.exceptionsr�cryptography.hazmat.backendsr	�cryptography.hazmat.primitivesr
r�)cryptography.hazmat.primitives.asymmetricrr
rrr�&cryptography.hazmat.primitives.ciphersrrr�,cryptography.hazmat.primitives.serializationrr�pyasn1.codec.berrr�rr��pyasn1.errorr�pyasn1.typer�twisted.conch.sshrr�twisted.conch.ssh.commonr�twisted.pythonr�twisted.python.compatrr�twisted.python.constantsr r!�twisted.python.deprecater"�/cryptography.hazmat.primitives.asymmetric.utilsr#r$�ImportErrorr%r&�	SECP256R1�	SECP384R1�	SECP521R1rmr��	Exceptionr(r1r2r3r6rErFr�r.r.r.r/�<module>sv�	��0

Spamworldpro Mini