
    Yf                       d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlZddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZmZmZmZ ddl m!Z!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7 	 ddl8m9Z9m:Z: n# e;$ r ddl8m<Z9m=Z: Y nw xY w ej>                     ej?                     ej@                    dZAddddZBejC        ZCejD        ZD G d deE          ZF G d deE          ZG G d  d!eE          ZH G d" d#eE          ZI G d$ d%e5          ZJ G d& d'eE          ZKd( ZL G d) d*          ZMd-d,ZNdS ).z0
Handling of RSA, DSA, ECDSA, and Ed25519 keys.
    )annotationsN)	b64encodedecodebytesencodebytes)md5sha256)Any)utils)InvalidSignature)default_backend)hashesserialization)dsaeced25519paddingrsa)Cipher
algorithmsmodes)load_pem_private_keyload_ssh_public_key)Literal)commonsexpy)int_to_bytes)	randbytes)	iterbytesnativeString)NamedConstantNames)_mutuallyExclusiveArguments)decode_dss_signatureencode_dss_signature)decode_rfc6979_signatureencode_rfc6979_signature)s   ecdsa-sha2-nistp256s   ecdsa-sha2-nistp384s   ecdsa-sha2-nistp521s   nistp256s   nistp384s   nistp521)s	   secp256r1s	   secp384r1s	   secp521r1c                      e Zd ZdZdS )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__     H/var/www/html/env/lib/python3.11/site-packages/twisted/conch/ssh/keys.pyr(   r(   D   s           r/   r(   c                      e Zd ZdZdS )BadSignatureAlgorithmErrorzi
    Raised when a public key signature algorithm name isn't defined for this
    public key format.
    Nr)   r.   r/   r0   r2   r2   L              r/   r2   c                      e Zd ZdZdS )EncryptedKeyErrorzb
    Raised when an encrypted key is presented to fromString/fromFile without
    a password.
    Nr)   r.   r/   r0   r5   r5   S   r3   r/   r5   c                      e Zd ZdZdS )BadFingerPrintFormatzS
    Raises when unsupported fingerprint formats are presented to fingerprint.
    Nr)   r.   r/   r0   r7   r7   Z   s           r/   r7   c                  :    e Zd ZdZ e            Z e            ZdS )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/   r0   r9   r9   `   s0        
 
 mooG!MOOMMMr/   r9   c                      e Zd ZdZdS )PassphraseNormalizationErrorz
    Raised when a passphrase contains Unicode characters that cannot be
    normalized using the available Unicode character database.
    Nr)   r.   r/   r0   r=   r=   q   r3   r/   r=   c                    t          | t                    rOt          d | D                       rt                      t	          j        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.
    c              3  F   K   | ]}t          j        |          d k    V  dS )CnN)unicodedatacategory).0cs     r0   	<genexpr>z'_normalizePassphrase.<locals>.<genexpr>   s2      CC1{#A&&$.CCCCCCr/   NFKCzUTF-8)
isinstancestranyr=   rA   	normalizeencode
passphrases    r0   _normalizePassphraserN   x   sl    & *c"" 	 CC
CCCCC 	1 /000$VZ88??HHHr/   c                  p   e Zd ZdZed5d            Zed5d            Zed             Zed             Zed             Z	ed             Z
ed	             Zed
             Zed             Zed             Zed             Zed             Zed6d            Zed7d            Zed7d            Zed7d            Zed7d            Zd Zd8dZd9dZd Zd Zej        fdZd:d Zd! Zd" Z d# Z!d$ Z"d;d&Z#d' Z$d( Z% e&d)d*gd)d+gg          d6d,            Z'd7d-Z(d5d.Z)d7d/Z*d<d0Z+d1 Z,d2 Z-d7d3Z.d4 Z/dS )=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().
    Nc                    t          |d          5 }|                     |                                ||          cddd           S # 1 swxY w Y   dS )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typerM   fs        r0   fromFilezKey.fromFile   s    $ (D!! 	>Q>>!&&((D*==	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>s   )AAAc                   t          |t                    r|                    d          }t          |          }||                     |          }|t          d|          t          | d|                                 d          }|t          d|           |j        j	        dk    r|rt          d           ||          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-8Nzcannot guess the type of _fromString_zno _fromString method for    zkey not encrypted)
rG   rH   rK   rN   _guessStringTyper(   getattrupper__code__co_argcount)rV   datarX   rM   methods        r0   rT   zKey.fromString   s    . dC   	(;;w''D)*55
<''--D<B$BBCCC;TZZ\\;;TBB>A4AABBB?&!++ 7!"56666$<<6$
+++r/   c           
        t          j        |          \  }}|dk    rVt          j        |d          \  }}} | t          j        ||                              t                                          S |dk    rnt          j        |d          \  }}}}	} | t          j        |	t          j	        |||                                        t                                          S |t          v rM | t          j                            t          |         t          j        |d          d                             S |dk    r,t          j        |          \  }
}|                     |
          S t          d	|           )
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-rsar^      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(   )rV   blobkeyTyperestenrk   rl   rm   ro   as              r0   _fromString_BLOBzKey._fromString_BLOB   s   D T**j  dA..JAq$3s+Aq11<<_=N=NOOPPP
""%|D!44Aq!Q3$3+BQ!q+Q+Q+Q  *_..//  
 ##3)<<(&,tQ*?*?*B   
 &&l4((GAt--a000=G==>>>r/   c                   t          j        |          \  }}|dk    r7t          j        |d          \  }}}}}}	}|                     |||||	          S |dk    r6t          j        |d          \  }}	}
}}}|                     ||
||	|          S |t
          v rt
          |         }t          j        |d          \  }}	}|t          |j                            d                   k    rt          d	|d
|          t          j        |          \  }}| 
                    |	||          S |dk    r:t          j        |d          \  }}}|dd         }|                     ||          S t          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
        rg      r   r   drk   rl   rh      ro   rm   rk   rl   xr^   asciizECDSA curve name z does not match key type )encodedPointcurveprivateValuerr   N    )krs   )r   rt   ru   _fromRSAComponents_fromDSAComponentsrz   
_secToNistnamerK   r(   _fromECEncodedPointr}   )rV   r~   r   r   r   r   r   urk   rl   rm   ro   r   r   	curveNamer   r   combinedr   s                      r0   _fromString_PRIVATE_BLOBzKey._fromString_PRIVATE_BLOB  s   \ T**j  %+\$%:%:"Aq!Q1d))Aa1)BBB
"""(,tQ"7"7Aq!Q4))Aa1)BBB##(E!'dA!6!6Iq$Juz'8'8'A'ABBB!k!*GG5   "(d!3!3L$**gL +    && !'T1 5 5Ax"A--a1-555=G==>>>r/   c                    |                     d          r% | t          |t                                          S t          |                                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-sha2rq   )
startswithr   r   r   splitr   )rV   rd   r~   s      r0   _fromString_PUBLIC_OPENSSHzKey._fromString_PUBLIC_OPENSSHa  sg      ??=)) 	E3*41B1BCCDDD4::<<?++##D)))r/   c           	        |                                                                 }t          d                    |dd                             }|                    d          st          d          |t          d          d         }t          j        |d          \  }}}}t          j
        d|dd	                   d
         }	|	dk    rt          d          t          j        |d	d         d          \  }
}}
|dk    ro|st          d          |dv r+t          j        }d}t          |dd                   dz  }|}nt          d|          |dk    rWt          j        |          \  }}t          j
        d|dd	                   d
         }t          j        ||||z   |d          }nt          d|          t          |          |z  d
k    rt          d          t#           ||d|                   t%          j        ||||z                      t)                                                                }|                    |          |                                z   }n|dk    rt          d|d          |}t          j
        d|dd	                   d
         }t          j
        d|d	d                   d
         }||k    rt          d||fz            |                     |dd                   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
        r/   rq      openssh-key-v1 z"unknown OpenSSH private key formatN   !Lri   r   zDonly OpenSSH private key files containing a single key are supportedr^      none0Passphrase must be provided for an encrypted key)s
   aes128-ctrs
   aes192-ctr
   aes256-ctr   r      zunknown encryption type    bcryptT)ignore_few_roundszunknown KDF type zbad paddingbackendzprivate key specifies KDF z but no cipherz#check values do not match: %d != %d)strip
splitlinesr   joinr   r(   lenr   rt   structunpackr5   r   AESintbcryptkdfr   r   CTRr   	decryptorupdatefinalizer   )rV   rd   rM   lineskeyListcipherr   
kdfOptionsr   r   _encPrivKeyListalgorithmClass	blockSizekeySizeivSizesaltroundsdecKeyr   privKeyListcheck1check2s                          r0   _fromPrivateOpenSSH_v1zKey._fromPrivateOpenSSH_v1v  sI   : 

''))chhuQrT{3344!!"566 	DBCCC#122445(.Wa(@(@%ZM$RaR))!,66-  
  &|DHa88>1W 'I   FFF!+	fQqSk**a/"!"GV"G"GHHHi#\*55
dtT"1"X66q9f$&*   ""=c"="=>>>N##i/A55!-000vhwh/00	&7V+;!;<=='))   ikk	 
 $**>::Y=O=O=Q=QQKKg~~!kGJssL   )Kt[!_55a8t[1%566q9VCvvFVVWWW++KO<<<r/   c                `   |                                                                 }|d         dd         }|sd}|dv r_	 t          ||t                                }n5# t          $ r t          d          t          $ r t          d          w xY w | |          S t          d|           )	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      N)s   ECs   RSAs   DSAr   z&Failed to decode key (Bad Passphrase?)unknown key type )r   r   r   r   	TypeErrorr5   
ValueErrorr(   )rV   rd   rM   r   kindkeys         r0   _fromPrivateOpenSSH_PEMzKey._fromPrivateOpenSSH_PEM  s    J 

''))Qx3  	J***L*4_=N=NOO   'F    L L L!"JKKKL3s88O8$88999s    A 2Bc                    |                                                                 d         dd         dk    r|                     ||          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
        r   r   r   s   OPENSSH)r   r   r   r   )rV   rd   rM   s      r0   _fromString_PRIVATE_OPENSSHzKey._fromString_PRIVATE_OPENSSH  s_    , ::<<""$$Q'3/:==--dJ??? ..tZ@@@r/   c                *   t          j        t          |dd                             }|d         dk    sJ i }|d         dd         D ]4\  }}t          j        t          j        |                    d         ||<   5|d         d         dk    r1|                     |d         |d         |d	         |d
                   S |d         d         dk    r#|                     |d         |d                   S t          d|d         d                    )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
        rq   r   r   
   public-keyN   dsa   y   g   p   qro   rm   rk   rl      rsa-pkcs1-sha1   n   er   r   unknown lsh key type )	r   parser   r   ru   NSr   r   r(   rV   rd   sexpkdr   s        r0   _fromString_PUBLIC_LSHzKey._fromString_PUBLIC_LSH%  s   " {;tAbDz2233Aw-''''q'!""+ 	8 	8JD$|FIdOO44Q7BtHH71:))T(bh"T(bh *    !WQZ,,,))BtH4)AAABd1gajBBCCCr/   c                2   t          j        |          }|d         dk    sJ i }|d         dd         D ]4\  }}t          j        t          j        |                    d         ||<   5|d         d         dk    rbt          |          dk    sJ t          |                      |                     |d         |d         |d	         |d
         |d                   S |d         d         dk    rt          |          dk    sJ t          |                      |d	         |d
         k    r|d
         |d	         c|d	<   |d
<   |                     |d         |d         |d         |d	         |d
                   S t          d|d         d                    )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-keyrq   Nr   r   r   r   r   r      xr   	   rsa-pkcs1r   r   r      dr   r   )	r   r   r   ru   r   r   r   r   r(   r   s        r0   _fromString_PRIVATE_LSHzKey._fromString_PRIVATE_LSHE  s   " {4  Aw.((((q'!""+ 	8 	8JD$|FIdOO44Q7BtHH71:r77a<<<R<<<))T(bh"T(bh"T( *    !WQZ<''r77a<<<R<<<$x"T(""%'Xr$x"4"T())T(bh"T(bh"T( *   
 Bd1gajBBCCCr/   c                   t          j        |          \  }}|dk    rt          j        |          \  }}t          j        |          \  }}t          j        |          \  }}t          j        |          \  }}t          j        |          \  }}|                     |||||          S |dk    rt          j        |          \  }}t          j        |          \  }	}t          j        |          \  }
}t          j        |          \  }}t          j        |          \  }}t          j        |          \  }}|                     |
||	|||          S t          d|           )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
        rh   r   rg   r   r   r   rk   rl   r   r   )r   rt   ru   r   r   r(   )rV   rd   r   rk   rl   rm   ro   r   r   r   r   r   s               r0   _fromString_AGENTV3zKey._fromString_AGENTV3k  sU   < T**j  l4((GAtl4((GAtl4((GAtl4((GAtl4((GAt))Aa1)BBB
""l4((GAtl4((GAtl4((GAtl4((GAtl4((GAtl4((GAt))Aa1Q)GGG;';;<<<r/   c                   |                     d          s|                     d          rdS |                     d          rdS |                     d          rdS |                     d          rd	S |                     d
          s*|                     d          s|                     d          rCt          j        |          \  }}d}|r|dz  }t          j        |          \  }}||dk    rdS dS dS )z
        Guess the type of key in data.  The types map to _fromString_*
        methods.

        @type data: L{bytes}
        @param data: The key data.
        s   ssh-   ecdsa-sha2-public_opensshs
   -----BEGINprivate_openssh   {
public_lsh   (private_lshs      ssh-s
      ecdsa-s      ssh-ed25519r   rq   ri   agentv3r~   N)r   r   rt   ru   )rV   rd   ignoredr   counts        r0   r_   zKey._guessStringType  s%    ??7## 	t~'F'F 	##__]++ 	$$__T"" 	<__T"" 	 =OO344	899	 =>>	
 #L..MGTE 3
 &T 2 2  3 qyy yv	 	r/   c                v   t          j        ||          }|"|                    t                                }nut          j        |||t          j        ||          t          j        ||          t          j        ||          |          }	|	                    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   r   N)rk   rl   r   dmp1dmq1iqmppublic_numbers)	r   rv   rw   r   RSAPrivateNumbersrsa_crt_dmp1rsa_crt_dmq1rsa_crt_iqmpprivate_key)
rV   r   r   r   rk   rl   r   publicNumbers	keyObjectprivateNumberss
             r0   r   zKey._fromRSAComponents  s    6 ,qA6669%001B1BCCII 2%a++%a++%a++,  N '22?3D3DEEIs9~~r/   c                $   t          j        |t          j        |||                    }|"|                    t	                                }n7t          j        ||          }|                    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.
        rj   rn   N)r   r  )r   rx   ry   rw   r   DSAPrivateNumbersr  )	rV   ro   rk   rl   rm   r   r  r	  r
  s	            r0   r   zKey._fromDSAComponents  s    . ,3#:Q!q#I#I#I
 
 
 9%001B1BCCII 2Q}UUUN&22?3D3DEEIs9~~r/   c                   t          j        ||t          |                   }|"|                    t	                                }n7t          j        ||          }|                    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   ro   r   N)private_valuer  )r   EllipticCurvePublicNumbersrz   rw   r   EllipticCurvePrivateNumbersr  )rV   r   ro   r   r   r  r	  r
  s           r0   _fromECComponentszKey._fromECComponents  s    $ 51K.
 
 
 %001B1BCCII;*=  N '22?3D3DEEIs9~~r/   c                    |,t           j                            t          |         |          }n-t          j        |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}
        )r   r{   r|   rz   derive_private_keyr   )rV   r   r   r   r	  s        r0   r   zKey._fromECEncodedPoint,  sd      1DDE"L II -k%0/2C2C I s9~~r/   c                    t           t          t          d          |t                               |          }nt                              |          } | |          S )a  Build 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}
        Nz)Ed25519 keys not supported on this system)Ed25519PublicKeyEd25519PrivateKeyr(   from_public_bytesfrom_private_bytes)rV   r   r   r	  s       r0   r}   zKey._fromEd25519ComponentsH  sZ     #'8'@IJJJ9(::1==II)<<Q??Is9~~r/   c                    || _         dS )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)selfr	  s     r0   __init__zKey.__init___  s     $r/   otherobjectreturnboolc                    t          |t                    rT|                                 |                                k    o)|                                 |                                k    S t          S )zN
        Return True if other represents an object with the same key.
        )rG   rP   rX   rd   NotImplemented)r  r  s     r0   __eq__z
Key.__eq__i  sQ     eS!! 	"99;;%**,,.N499;;%**,,3NN!!r/   rH   c                j   |                                  dk    r|                                 }|d                             d          }|                                 rd|dd          d}nd|dd          d}t	          |                                          D ]\  }}|dk    r	|d	| z  }|d
| d| z  } |dz   S dt          |                                            d|                                 rdpdd|                                 dg}t	          |                                                                           D ]\  }}|                    d| d           |                                  dk    r|nt          j
        |          dd         }|ru|dd         }|dd         }d}	t          |          D ]}
|	t          |
          ddz   }	t          |          dk     r
|	dd         }	|                    d|	z              |u|d         dz   |d<   d
                    |          S )z@
        Return a pretty representation of this object.
        ECr   r\   z<Elliptic Curve Public Key (Nz bits)z<Elliptic Curve Private Key (z	
curve:
	
z:
	z>
< z
Public KeyzPrivate Keyz (zattr :Ed25519ri       02xr   	>)rX   rd   decodeisPublicsorteditemsr   sizeappendr   MPr   ordr   r   )r  rd   r   outr   vr   bymorD   s              r0   __repr__zKey.__repr__r  so    99;;$99;;D=''00D}} HFT"##YFFFGd233iGGGtzz||,, , ,1<<0$000CC++++++CC; ( !----MMOO4EEEIIKKKK	E tyy{{002233 + +1\Q\\\***))++22QQ	!QRR8H +3B3ABCCBA&q\\ 1 13q66 0 0 0 001vv{{crcFLL***  + b	CE"I99U###r/   c                    t          | j        t          j        t          j        t          j        t          j	        f          S )zl
        Check if this instance is a public key.

        @return: C{True} if this is a public key.
        )
rG   r  r   RSAPublicKeyr   DSAPublicKeyr   r{   r   r  r  s    r0   r3  zKey.isPublic  s8     O  )(	
 
 	
r/   c                z    |                                  r| S t          | 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.
        )r3  rP   r  rw   rC  s    r0   publicz
Key.public  s5     ==?? 	5Kt1133444r/   c                   |t           j        u rMt          t          t	          |                                                                                               S |t           j        u rjt          d                    d t          t          |                                                                                     D                                 S t          d|           )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}
           :c                6    g | ]}t          j        |          S r.   )binasciihexlify)rC   r   s     r0   
<listcomp>z#Key.fingerprint.<locals>.<listcomp>  s#    WWWQX%a((WWWr/   z Unsupported fingerprint format: )r9   r;   r   r   r   r~   digestr:   r   r   r   r7   )r  formats     r0   fingerprintzKey.fingerprint  s    : '555	&*=*=*D*D*F*F G GHHH)111		WW)C		<L<L<S<S<U<U2V2VWWW    ''R&'R'RSSSr/   &Literal['RSA', 'DSA', 'EC', 'Ed25519']c                   t          | j        t          j        t          j        f          rdS t          | j        t
          j        t
          j        f          rdS t          | j        t          j	        t          j
        f          rdS t          | j        t          j        t          j        f          rdS t          d| 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&  r,  zunknown type of object: )rG   r  r   rA  RSAPrivateKeyr   rB  DSAPrivateKeyr   r{   EllipticCurvePrivateKeyr   r  r  RuntimeErrorrC  s    r0   rX   zKey.type  s     do(8#:K'LMM 	O5#*:C<M)NOO 	O5Ob79ST
 
 		O 4Og68QR
 
 	O 9M$/MMNNNr/   c                    |                                  dk    r2dt          | j        j        j                            d                   z   S dddd|                                           S )a  
        Get the type of the object we wrap as defined in the SSH protocol,
        defined in RFC 4253, Section 6.6 and RFC 8332, section 4 (this is a
        public key format name, not a public key algorithm name). Currently
        this can only be b'ssh-rsa', b'ssh-dss', b'ecdsa-sha2-[identifier]'
        or b'ssh-ed25519'.

        identifier is the standard NIST curve name

        @return: The key type format.
        @rtype: L{bytes}
        r&  r   r   rg   rh   rr   )rQ  rR  r,  )rX   r   r  r   r   rK   rC  s    r0   sshTypezKey.sshType  sk     99;;$DO,A,F,M,Mg,V,V!WW
 "!)  iikk	 r/   c                d    |                                  dk    rg dS |                                 gS )z
        Get the public key signature algorithms supported by this key.

        @return: A list of supported public key signature algorithm names.
        @rtype: L{list} of L{bytes}
        rQ  )   rsa-sha2-512   rsa-sha2-256rg   )rX   rX  rC  s    r0   supportedSignatureAlgorithmsz Key.supportedSignatureAlgorithms  s4     99;;%AAAALLNN##r/   c                    |                                  dk    rs||                                 k    rY|                                 }|dk    rt          j                    S |dk    rt          j                    S t          j                    S dS t          j                    t          j                    t          j                    t          j                    t          j                    d                    |                                  |f          S )z
        Return a hash algorithm for this key type given an SSH signature
        algorithm name, or L{None} if no such hash algorithm is defined for
        this key type.
        r&     i  N))rQ  rg   )rQ  r[  )rQ  rZ  )rR  rh   )r,  rr   )	rX   rX  r6  r   SHA256SHA384SHA512SHA1get)r  signatureTyper   s      r0   _getHashAlgorithmzKey._getHashAlgorithm  s     99;;$..))++c>>!=??*^^!=??*!=??*t &,[]]*0-//*0-//%+[]]-3]__  c499;;.//0r/   c                    | j         dS |                                 dk    r| j         j        j        S |                                 dk    rdS | j         j        S )zv
        Return the size of the object we wrap.

        @return: The size of the key.
        @rtype: L{int}
        Nr   r&  r,  r^  )r  rX   r   key_sizerC  s    r0   r6  zKey.size:  sU     ?"1YY[[D  ?(11YY[[I%%3''r/   dict[str, Any]c           	     $   t          | j        t          j                  r(| j                                        }|j        |j        dS t          | j        t          j                  rb| j                                        }|j        j        |j        j        |j	        |j
        |j        t          j        |j        |j
                  dS t          | j        t          j                  rC| j                                        }|j        |j        j        |j        j
        |j        j        dS t          | j        t          j                  r]| j                                        }|j        |j        j        |j        j        j        |j        j        j
        |j        j        j        dS t          | j        t(          j                  r;| j                                        }|j        |j        |                                 dS t          | j        t(          j                  rK| j                                        }|j        j        |j        j        |j        |                                 dS t          | j        t2          j                  r;d| j                            t8          j        j        t8          j        j                  iS t          | j        t2          j                   r| j        !                                                    t8          j        j        t8          j        j                  | j        "                    t8          j        j        t8          j#        j        t9          j$                              dS tK          d	| j                   )
z_
        Return the values of the public key as a dictionary.

        @rtype: L{dict}
        r   r   r   )r   ro   rm   rk   rl   r  )r   ro   r   r   r   )r   r   zUnexpected key type: )&rG   r  r   rA  r  r   r   rS  private_numbersr   rk   rl   r  r   rB  ro   rp   rm   rT  r   r   r{   rX  rU  r  r   r  public_bytesr   EncodingRawPublicFormatr  rw   private_bytesPrivateFormatNoEncryptionrV  )r  rsa_pub_numbersrsa_priv_numbersdsa_pub_numbersdsa_priv_numbersec_pub_numbersec_priv_numberss          r0   rd   zKey.dataI  s:    dos'788 D	J"o<<>>O$&$&   ):;; >	J#>>@@%46%46%'%'%'%&6&8:J:LMM   )9:: 3	J"o<<>>O$&$68$68$68	   ):;; +	J#>>@@%'%46%4FH%4FH%4FH   )BCC "	J!_;;==N#%#%  
 )CDD 	J"o==??O$35$35 / =	   )ABB 	JT_11!*.0J0N  
 )BCC 	J_//11>>!*.0J0N  _22!*.!/3!.00 		 	 	 HtHHIIIr/   c                   |                                  }|                                 }|dk    rJt          j        d          t          j        |d                   z   t          j        |d                   z   S |dk    rt          j        d          t          j        |d                   z   t          j        |d                   z   t          j        |d	                   z   t          j        |d
                   z   S |dk    r| j        j        j        dz   dz  }t          j        |d                   t          j        |d         dd                   z   t          j        dt          j	        |d         |          z   t          j	        |d
         |          z             z   S |dk    r/t          j        d          t          j        |d                   z   S t          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}
        rQ  rg   r   r   rR  rh   rk   rl   rm   ro   r&     r   r   N   r   r,  rr   r   unknown key type: )rX   rd   r   r   r8  r  r   rg  r
   r   r(   )r  rX   rd   
byteLengths       r0   r~   zKey.blob  s   @ yy{{yy{{5==9Z((69T#Y+?+??&)DQTIBVBVVVU]]	*%%)DI&&')DI&&' )DI&&' )DI&&	' T\\//81<BJ	$w-(()DM"##.//0)(cJ??@(cJ??@  Y9^,,vyc/C/CCC9499:::r/   c                   |                                  }|                                 }|dk    rt          j        |d         |d                   }t	          j        d          t	          j        |d                   z   t	          j        |d                   z   t	          j        |d                   z   t	          j        |          z   t	          j        |d                   z   t	          j        |d                   z   S |dk    rt	          j        d	          t	          j        |d                   z   t	          j        |d                   z   t	          j        |d
                   z   t	          j        |d                   z   t	          j        |d                   z   S |dk    r| j                                        	                    t          j        j        t          j        j                  }t	          j        |d                   t	          j        |d         dd                   z   t	          j        |          z   t	          j        |d                   z   S |dk    rSt	          j        d          t	          j        |d                   z   t	          j        |d         |d         z             z   S t          d|           )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
        rQ  rk   rl   rg   r   r   r   rR  rh   rm   ro   r   r&  r   rz  Nr   r,  rr   r   r   r|  )rX   rd   r   r  r   r   r8  r  rw   rk  r   rl  X962rn  UncompressedPointr(   )r  rX   rd   r  encPubs        r0   privateBlobzKey.privateBlob  sz   R yy{{yy{{5==#DItCy99D	*%%)DI&&')DI&&' )DI&&' )D//	"
 )DI&&' )DI&&' U]]	*%%)DI&&')DI&&' )DI&&' )DI&&	'
 )DI&&' T\\_//11>>&+*< F
 	$w-(()DM"##.//0)F##$ )D0112 Y	.)))DI&&')DIS	1223 9499:::r/   extracommentrM   c                r   |5t          j        dt          d           |                                 r|}n|}t	          |t
                    r|                    d          }t          |          }t          | d|	                                 d          }|t          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}
        NzThe 'extra' argument to twisted.conch.ssh.keys.Key.toString was deprecated in Twisted 20.3.0; use 'comment' or 'passphrase' instead.r   )
stacklevelr\   
_toString_r|  )subtyper  rM   )warningswarnDeprecationWarningr3  rG   rH   rK   rN   r`   ra   r(   )r  rX   r  r  r  rM   re   s          r0   toStringzKey.toString$  s    Z MI #    }} #"
gs## 	.nnW--G)*55
:DJJLL::DAA>9499:::vgw:NNNNr/   c                   |                                  dk    rU|sd}| j                            t          j        j        t          j        j                  dz   |z                                   S t          | 	                                          
                    dd          }|sd}|                                 dz   |z   dz   |z                                   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/          
)rX   r  rk  r   rl  OpenSSHrn  r   r   r~   replacerX  )r  r  b64Datas      r0   _toPublicOpenSSHzKey._toPublicOpenSSHf  s     99;;$ ,,!*2M4N4V   	
 egg diikk**225#>> 	G%/$6@GGIIIr/   c           	     0   |r_t           j        }d}d}|j        dz  }d}|}t          j        |          }	d}
t          j        |	          t          j        d|
          z   }nd}d}d}d}t          j        d	          }||z   | 	                                z   t          j        |pd          z   }d
}t          |          |z  r-|dz  }|t          |dz  f          z  }t          |          |z  -|rt          j        ||	||z   d          }t           ||d|                   t          j        ||||z                      t#                                                                }|                    |          |                                z   }n|}dt          j        |          z   t          j        |          z   t          j        |          z   t          j        dd          z   t          j        |                                           z   t          j        |          z   }t-          |                              dd          dgfdt1          d
t                    d          D             z   dgz   }d                    |          dz   S )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   d   r   r   r/   ri   r   rq      Nr   r   r  s#   -----BEGIN OPENSSH PRIVATE KEY-----c                *    g | ]}||d z            S )@   r.   )rC   ir  s     r0   rK  z,Key._toPrivateOpenSSH_v1.<locals>.<listcomp>  s&    GGGqwq1r6z"GGGr/   r  s!   -----END OPENSSH PRIVATE KEY-----)r   r   
block_sizer   secureRandomr   r   r   packr  r   bytesr   r   r   r   r   r   	encryptorr   r   r~   r   r  ranger   )r  r  rM   r   
cipherNamekdfNamer   r   r   r   r   r   checkr   padByteencKeyr  r   r~   r   r  s                       @r0   _toPrivateOpenSSH_v1zKey._toPrivateOpenSSH_v1  s     	  ^F&JG)Q.IGF)&11DF46;tV+D+DDJJ JGIJ&q))emd&6&6&8&8869W^PS;T;TT+* 	4qLG5'D.!2333K +* 	4  		)Z
D'F2BCHHFvhwh'((	&7V+;!;<=='))   ikk	 
 '--k::Y=O=O=Q=QQNN(Ni
##$i  ! i
##$ k$""	#
 i		$$% i''( 	 d##++E37734GGGGE!S\\2,F,FGGGH345 	
 zz%  5((r/   c                L   |st          j                    }nt          j        |          }|                                 dk    r:| j                            t           j        j        t           j        j	        |          S |                                 dk    sJ t          d          )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,  zBcannot serialize Ed25519 key to OpenSSH PEM format; use v1 instead)r   rq  BestAvailableEncryptionrX   r  ro  rl  PEMrp  TraditionalOpenSSLr   )r  rM   r  s      r0   _toPrivateOpenSSH_PEMzKey._toPrivateOpenSSH_PEM  s      	J%244II%=jIII99;;)##?00&*+>   99;;)++++W  r/   c                $   |                                  r|                     |          S |dk    s|/|                                 dk    r|                     ||          S ||dk    r|                     |          S t          d|           )	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}
        )r  v1Nr,  )r  rM   r  rL   zunknown subtype )r3  r  rX   r  r  r   )r  r  r  rM   s       r0   _toString_OPENSSHzKey._toString_OPENSSH  s    & ==?? 	;(((999__TYY[[I5M5M,,W,TTT_5 0 0---DDD999:::r/   c                ~   |                                  }|                                 }|                                 rK|dk    r_t          j        dddt          j        |d                   dd         gdt          j        |d	                   dd         gggg          }n|d
k    rt          j        dddt          j        |d                   dd         gdt          j        |d                   dd         gdt          j        |d                   dd         gdt          j        |d                   dd         gggg          }nt          d|           dt          |          	                    dd          z   dz   S |dk    rO|d         |d         }}t          j        ||          }t          j        dddt          j        |d                   dd         gdt          j        |d	                   dd         gdt          j        |d                   dd         gdt          j        |          dd         gdt          j        |          dd         gdt          j        |d         |dz
  z            dd         gdt          j        |d         |dz
  z            dd         gd t          j        |          dd         gg	gg          S |d
k    rt          j        dddt          j        |d                   dd         gdt          j        |d                   dd         gdt          j        |d                   dd         gdt          j        |d                   dd         gd!t          j        |d"                   dd         gggg          S t          d| d#          )$z
        Return a public or private LSH key.  See _fromString_PUBLIC_LSH and
        _fromString_PRIVATE_LSH for the key formats.

        @rtype: L{bytes}
        rQ  r   r   r   r   ri   Nr   r   rR  r   r   rk   r   rl   r   rm   r   ro   r   r   r  r/      }r   r   r   r      arq      b   cr   r   ')rd   rX   r3  r   r  r   r8  r(   r   r  r   r  )r  kwargsrd   rX   keyDatark   rl   r  s           r0   _toString_LSHzKey._toString_LSH  s    yy{{yy{{==?? G	?u}}* * 1!%vyc';';ABB'? @!%vyc';';ABB'? @	  * * &!%vyc';';ABB'? @!%vyc';';ABB'? @!%vyc';';ABB'? @!%vyc';';ABB'? @	  ""<d"<"<===+g..66ucBBBTIIu}}Cy$s)1'1--z + ,!%vyc';';ABB'? @!%vyc';';ABB'? @!%vyc';';ABB'? @!%vy||ABB'7 8!%vy||ABB'7 8!%vyca!e1D'E'Eabb'I J!%vyca!e1D'E'Eabb'I J!%vyqrr': ;
  $ z + &!%vyc';';ABB'? @!%vyc';';ABB'? @!%vyc';';ABB'? @!%vyc';';ABB'? @!%vyc';';ABB'? @
    ""=d"="="=>>>r/   c                   |                                  }|                                 s|                                 dk    r-|d         |d         |d         |d         |d         |d         f}n=|                                 dk    r%|d         |d         |d	         |d
         |d         f}t          j        |                                           d                    t          t          j        |                    z   S dS )z
        Return a private Secure Shell Agent v3 key.  See
        _fromString_AGENTV3 for the key format.

        @rtype: L{bytes}
        rQ  r   r   r   r   rk   rl   rR  rm   ro   r   r/   N)	rd   r3  rX   r   r   rX  r   mapr8  )r  r  rd   valuess       r0   _toString_AGENTV3zKey._toString_AGENTV3I  s     yy{{}} 	Pyy{{e##IIIIII %%s)T#YS	49d3iP9T\\^^,,sxxFIv8N8N/O/OOO	P 	Pr/   c                   |                                  }||                                 }|                     |          }|t          d| d| d          |dk    rC| j                            |t          j                    |          }t          j	        |          }n|dk    rb| j                            ||          }t          |          \  }}t          j	        t          |d          t          |d          z             }nZ|dk    r!| j                            |t          j        |                    }	t          |	          \  }}t          |          }
t          |          }t          |
d	                   t          u rt          |
d	                   }n|
d	         }|d
z  rd|
z   }
t          |d	                   t          u rt          |d	                   }n|d	         }|d
z  rd|z   }t          j	        t          j	        |
          t          j	        |          z             }n2|dk    r,t          j	        | j                            |                    }t          j	        |          |z   S )a  
        Sign some data with this key.

        SECSH-TRANS RFC 4253 Section 6.6.

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

        @type signatureType: L{bytes}
        @param signatureType: The SSH public key algorithm name to sign this
        data with, or L{None} to use a reasonable default for the key.

        @rtype: L{bytes}
        @return: A signature for the given data.
        Nzpublic key signature algorithm z is not defined for z keysrQ  rR     r&  r          r,  )rX   rX  re  r2   r  signr   PKCS1v15r   r   r#   r   r   ECDSArH   r9  )r  rd   rd  r   hashAlgorithmsigretrs	signaturerR   sbrcompscomps                 r0   r  zKey.sign_  sS     ))++ 
 !LLNNM..}== ,.- . .&. . .  
 e/&&tW-=-?-?OOC)C..CC/&&t];;C)#..FQ
 )LB//,q"2E2EEFFCC__,,T28M3J3JKKI))44FQaBaB BqE{{c!!BqE

1 t| "r\BqE{{c!!BqE

1t| "r\)FIbMMFIbMM9::CC	!!)DO006677Cy''#--r/   c                l   t          |          dk    rdt          j        |          }}nt          j        |          \  }}|                     |          }|dS |                                 }|dk    r`| j        }|                                 s|                                }t          j        |          d         |t          j
                    |f}n|dk    rt          j        |          d         }t                              |dd         d	          }	t                              |dd         d	          }
t          |	|
          }| j        }|                                 s|                                }|||f}n|d
k    rt          j        |          d         }t          j        |d          \  }}}t                              |d	          }	t                              |d	          }
t          |	|
          }| j        }|                                 s|                                }||t          j        |          f}nQ|dk    rK| j        }|                                 s|                                }t          j        |          d         |f}	  |j        |  dS # t"          $ r Y dS w xY w)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.
        (   rh   NFrQ  r   rR  r  bigr&  r^   r,  T)r   r   r   rt   re  rX   r  r3  rw   r   r  r   
from_bytesr$   r   r  verifyr   )r  r  rd   rd  r  r   r   argsconcatenatedSignaturer  r  rstrsstrr   s                 r0   r  z
Key.verify  s    y>>R'169Y3G3G9MM'-|I'>'>$M9..}== 5))++eA==?? #LLNNY''* ""	DD $*L$;$;A$>!4SbS95AAA4RSS95AAA,Q22IA==?? #LLNNt]3DD__$*L$;$;A$>!%|,A1EED$tU++AtU++A,Q22IA==?? #LLNNtRXm%<%<=DD	!!A==?? #LLNNL++A.5D	AHdOO 4   	 	 	55	s   
J% %
J32J3)NN)NNNN)N)r  r  r   r!  )r   rH   )r   rO  )r   rh  )NNN)0r*   r+   r,   r-   classmethodrZ   rT   r   r   r   r   r   r   r   r   r   r_   r   r   r  r   r}   r  r$  r?  r3  rE  r9   r:   rN  rX   rX  r\  re  r6  rd   r~   r  r"   r  r  r  r  r  r  r  r  r  r.   r/   r0   rP   rP      s          > > > [>( %, %, %, [%,N 6? 6? [6?p H? H? [H?T * * [*( V= V= [V=p 6: 6: [6:p A A [A8 D D [D> #D #D [#DJ .= .= [.=`   [> * * * [*X       [ D    [>    [6    [,$ $ $" " " "*$ *$ *$ *$X
 
 
 5 5 5 "4!; &T &T &T &TPO O O O.  0
$ 
$ 
$0 0 06( ( (JJ JJ JJ JJX:; :; :;xQ; Q; Q;f ! i l#	
 :O :O :O :OxJ J J J4<) <) <) <)|   8; ; ; ;:P? P? P?dP P P,J. J. J. J.XD D D D Dr/   rP      c                ^   |                                                      d           |                                 st          j        d|t                                }|                    t          j        j	        t          j
        j        t          j                              }|                     |           |                     d          5 }t          j        |                                dt                                }t#          |          cddd           S # 1 swxY w Y   dS )	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_exponentrg  r   )encodingrM  encryption_algorithmrR   N)passwordr   )parentmakedirsexistsr   generate_private_keyr   ro  r   rl  r  rp  r  rq  
setContentrS   r   rU   rP   )locationr   
privateKeypemkeyFiles        r0   _getPersistentRSAKeyr    sY     OOt<<< ?? !-!G_=N=N
 
 

 &&"+/ .A!.!;!=!= ' 
 
 	C    
t		 "7LLNNT?3D3D
 
 

 :	                 s   AD""D&)D&)r  )Or-   
__future__r   rI  r   rA   r  base64r   r   r   hashlibr   r   typingr	   r   cryptographyr
   cryptography.exceptionsr   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   r   )cryptography.hazmat.primitives.asymmetricr   r   r   r   r   &cryptography.hazmat.primitives.ciphersr   r   r   ,cryptography.hazmat.primitives.serializationr   r   typing_extensionsr   twisted.conch.sshr   r   twisted.conch.ssh.commonr   twisted.pythonr   twisted.python.compatr   r   twisted.python.constantsr    r!   twisted.python.deprecater"   /cryptography.hazmat.primitives.asymmetric.utilsr#   r$   ImportErrorr%   r&   	SECP256R1	SECP384R1	SECP521R1rz   r   r  r  	Exceptionr(   r2   r5   r7   r9   r=   rN   rP   r  r.   r/   r0   <module>r     s   
  # " " " " "        6 6 6 6 6 6 6 6 6 6                      4 4 4 4 4 4 8 8 8 8 8 8 @ @ @ @ @ @ @ @ T T T T T T T T T T T T T T L L L L L L L L L L        & % % % % % + + + + + + + + 1 1 1 1 1 1 $ $ $ $ $ $ 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 @ @ @ @ @ @	                      )BLNN(BLNN(BLNN   
 + -     )              	       9   $ $ $ $ $ $ $ $"    9     >X X X X X X X Xv2( ( ( ( ( (s    B) )B98B9