
    h                       S r SSKJr  SSKrSSKrSSKrSSKrSSKJrJ	r	J
r
  SSKJrJr  SSKJr  SSKrSSKJrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr  SSKJrJrJ r J!r!J"r"  SSK#J$r$J%r%J&r&  SSK'J(r(J)r)  SSK*J+r+  SSK,J-r-J.r.  SSK/J0r0  SSK1J2r2  SSK3J4r4J5r5  SSK6J7r7   SSK8J9r9J:r:  \R|                  " 5       \R~                  " 5       \R                  " 5       S.rASSSS.rB\ R                  rC\ R                  rD " S S\E5      rF " S S\E5      rG " S  S!\E5      rH " S" S#\E5      rI " S$ S%\5      rJ " S& S'\E5      rKS( rL " S) S*5      rMS,S+ jrNg! \; a    SSK8J<r9J=r:   Nf = f)-z0
Handling of RSA, DSA, ECDSA, and Ed25519 keys.
    )annotationsN)	b64encodedecodebytesencodebytes)md5sha256)Any)NamedConstantNames)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)_mutuallyExclusiveArguments)decode_dss_signatureencode_dss_signature)decode_rfc6979_signatureencode_rfc6979_signature)   ecdsa-sha2-nistp256s   ecdsa-sha2-nistp384s   ecdsa-sha2-nistp521s   nistp256s   nistp384s   nistp521)s	   secp256r1s	   secp384r1s	   secp521r1c                      \ rS rSrSrSrg)BadKeyErrorD   z^
Raised when a key isn't what we expected from it.

XXX: we really need to check for bad keys
 N__name__
__module____qualname____firstlineno____doc____static_attributes__r+       ]/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/conch/ssh/keys.pyr)   r)   D   s    r3   r)   c                      \ rS rSrSrSrg)BadSignatureAlgorithmErrorL   z]
Raised when a public key signature algorithm name isn't defined for this
public key format.
r+   Nr,   r+   r3   r4   r6   r6   L       r3   r6   c                      \ rS rSrSrSrg)EncryptedKeyErrorS   zV
Raised when an encrypted key is presented to fromString/fromFile without
a password.
r+   Nr,   r+   r3   r4   r:   r:   S   r8   r3   r:   c                      \ rS rSrSrSrg)BadFingerPrintFormatZ   zK
Raises when unsupported fingerprint formats are presented to fingerprint.
r+   Nr,   r+   r3   r4   r=   r=   Z   s    r3   r=   c                  4    \ rS rSrSr\" 5       r\" 5       rSrg)FingerprintFormats`   a  
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{constantly.NamedConstant}

@cvar SHA256_BASE64: Named constant representing fingerprint format
    generated using sha256[RFC4634] algorithm in base64 encoding
@type SHA256_BASE64: L{constantly.NamedConstant}
r+   N)	r-   r.   r/   r0   r1   r
   MD5_HEXSHA256_BASE64r2   r+   r3   r4   r@   r@   `   s    
 oG!OMr3   r@   c                      \ rS rSrSrSrg)PassphraseNormalizationErrorq   z
Raised when a passphrase contains Unicode characters that cannot be
normalized using the available Unicode character database.
r+   Nr,   r+   r3   r4   rE   rE   q   r8   r3   rE   c                    [        U [        5      (       aG  [        S U  5       5      (       a
  [        5       e[        R
                  " SU 5      R                  S5      $ U $ )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  T   #    U  H  n[         R                  " U5      S :H  v   M      g7f)CnN)unicodedatacategory).0cs     r4   	<genexpr>'_normalizePassphrase.<locals>.<genexpr>   s      C
1{##A&$.
s   &(NFKCzUTF-8)
isinstancestranyrE   rJ   	normalizeencode
passphrases    r4   _normalizePassphraserX   x   sS    & *c"" C
CCC /00$$VZ8??HHr3   c                  6   \ rS rSrSr\S0S j5       r\S0S j5       r\S 5       r\S 5       r	\S 5       r
\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S1S j5       r\S2S j5       r\S2S j5       r\S2S j5       r\S2S j5       rS rS3S jrS4S jrS rS r\R:                  4S jrS5S jrS r S r!S r"S r#S6S  jr$S! r%S" r&\'" S#S$/S#S%//5      S1S& j5       r(S2S' jr)S0S( jr*S2S) jr+S7S* jr,S+ r-S, r.S2S8S- jjr/S. r0S/r1g)9Key   a]  
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                    [        US5       nU R                  UR                  5       X#5      sSSS5        $ ! , (       d  f       g= f)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typerW   fs        r4   fromFileKey.fromFile   s0    $ (D!Q>>!&&(D= "!!s	    7
Ac                   [        U[        5      (       a  UR                  S5      n[        U5      nUc  U R	                  U5      nUc  [        SU< 35      e[        U SUR                  5        3S5      nUc  [        SU 35      eUR                  R                  S:X  a  U(       a  [        S5      eU" U5      $ U" X5      $ )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)
rQ   rR   rU   rX   _guessStringTyper)   getattrupper__code__co_argcount)ra   datarc   rW   methods        r4   r_   Key.fromString   s    . dC  ;;w'D)*5
<''-D< 9$BCCTZZ\N;TB> :4&ABB??&&!+!"566$<$++r3   c                h   [         R                  " U5      u  p#US:X  aM  [         R                  " US5      u  pEnU " [        R                  " XE5      R                  [        5       5      5      $ US:X  a`  [         R                  " US5      u  pgpnU " [        R                  " U	[        R                  " XgUS9S9R                  [        5       5      5      $ U[        ;   aE  U " [        R                  R                  [        U   [         R                  " US5      S   5      5      $ US:X  a1  U R                  [         R                  " US5      S   S	S
9n
SU
l        U
$ US;   aH  [         R                  " U5      u  pU R!                  U5      n
UR#                  S5      (       a  SU
l        U
$ [%        SU 35      e)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-rsarj      ssh-dss   pqgyparameter_numbers   "   sk-ecdsa-sha2-nistp256@openssh.comr'   )encodedPointcurveT)   ssh-ed25519   sk-ssh-ed25519@openssh.coms   sk-ssh-unknown blob type: )r   getNSgetMPr   RSAPublicNumbers
public_keyr   r   DSAPublicNumbersDSAParameterNumbers_curveTabler   EllipticCurvePublicKeyfrom_encoded_point_fromECEncodedPoint_sk_fromEd25519Components
startswithr)   )ra   blobkeyTyperestenrx   ry   rz   r|   	keyObjectas               r4   _fromString_BLOBKey._fromString_BLOB   s   D T*j dA.JA$s++A1<<_=NOPPj %||D!4A!$$3+B+BQq+Q*_./  k!))<<(&,,tQ*?*B  ;;//#\\$215, 0 I !IMEEll4(GA2215I!!*-- $	/y9::r3   c                   [         R                  " U5      u  p#US:X  a-  [         R                  " US5      u  pEpgpnU R                  XEXhU	S9$ US:X  a,  [         R                  " US5      u  pppU R	                  XXUS9$ U[
        ;   a  [
        U   n[         R                  " US5      u  pnU[        UR                  R                  S5         :w  a  [        S	U< S
U< 35      e[         R                  " U5      u  pU R                  XUS9$ US:X  a0  [         R                  " US5      u  nnnUSS nU R                  UUS9$ [        SU 35      e)a  
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
rt      r   r   drx   ry   ru      r|   rz   rx   ry   xrj   asciizECDSA curve name z does not match key type )r   r   privateValuer   N    )kr   )r   r   r   _fromRSAComponents_fromDSAComponentsr   
_secToNistnamerU   r)   r   r   )ra   r   r   r   r   r   r   urx   ry   rz   r|   r   r   	curveNamer   r   combinedr   s                      r4   _fromString_PRIVATE_BLOBKey._fromString_PRIVATE_BLOB$  sf   \ T*j %+\\$%:"A!d))Aa)BB
""(,,tQ"7A!))Aa)BB#(E!'dA!6I$Juzz'8'8'ABB!!*G5  "(d!3L**L +   & !'T1 5Ax"A--a1-55 3G9=>>r3   c                    UR                  S5      (       a  U " [        U[        5       5      5      $ [        UR	                  5       S   5      nU R                  U5      $ )aL  
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-sha2r~   )r   r   r   r   splitr   )ra   rp   r   s      r4   _fromString_PUBLIC_OPENSSHKey._fromString_PUBLIC_OPENSSHo  sN      ??=))*41BCDD4::<?+##D))r3   c           	        UR                  5       R                  5       n[        SR                  USS 5      5      nUR	                  S5      (       d  [        S5      eU[        S5      S n[        R                  " US5      u  pVpx[        R                  " SUSS	 5      S
   n	U	S:w  a  [        S5      e[        R                  " US	S S5      u  pn
US:w  Ga6  U(       d  [        S5      eUS;   a&  [        R                  nSn[        USS 5      S-  nUnO[        SU< 35      eUS:X  aQ  [        R                  " U5      u  nn[        R                  " SUSS	 5      S
   n[        R                   " UUX-   USS9nO[        SU< 35      e[        U5      U-  S
:w  a  [        S5      e[#        U" USU 5      [$        R&                  " UXU-    5      [)        5       S9R+                  5       nUR-                  U5      UR/                  5       -   nOUS:w  a  [        SU< S35      eUn[        R                  " SUSS	 5      S
   n[        R                  " SUS	S 5      S
   nUU:w  a  [        SUU4-  5      eU R1                  USS 5      $ )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
r3   r~      openssh-key-v1 z"unknown OpenSSH private key formatN   !Lrv   r   zDonly OpenSSH private key files containing a single key are supportedrj      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   r   structunpackr:   r   AESintbcryptkdfr   r   CTRr   	decryptorupdatefinalizer   )ra   rp   rW   lineskeyListcipherr   
kdfOptionsr   r   _encPrivKeyListalgorithmClass	blockSizekeySizeivSizesaltroundsdecKeyr   privKeyListcheck1check2s                          r4   _fromPrivateOpenSSH_v1Key._fromPrivateOpenSSH_v1  s   : 

'')chhuQr{34!!"566BCC#1245(.Wa(@%ZMM$Ra)!,6- 
  &||DHa81W'I  FF!+	fQqk*a/"!$<VJ"GHHi#\\*5
dtT"1X6q9$&* "$5cW"=>>N#i/A5!-00vhw/0		&V+;<=') ik	 
 $**>:Y=O=O=QQKg~!GJL  )Kt[!_5a8t[1%56q9VCvvFVVWW++KO<<r3   c                   UR                  5       R                  5       nUS   SS nU(       d  SnUS;   a   [        X[        5       5      nU " U5      $ [        SU 35      e! [         a    [        S5      e[         a    [        S5      ef = f)	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   	TypeErrorr:   
ValueErrorr)   )ra   rp   rW   r   kindkeys         r4   _fromPrivateOpenSSH_PEMKey._fromPrivateOpenSSH_PEM  s    J 

'')Qx3 J**L*4_=NO s8O 1$899  'F   L!"JKKLs   A! !*Bc                    UR                  5       R                  5       S   SS S:X  a  U R                  X5      $ U R                  X5      $ )ax  
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   )ra   rp   rW   s      r4   _fromString_PRIVATE_OPENSSHKey._fromString_PRIVATE_OPENSSH  sK    , ::<""$Q'3/:=--d?? ..t@@r3   c                   [         R                  " [        USS 5      5      nUS   S:X  d   e0 nUS   SS  H4  u  pA[        R                  " [        R
                  " U5      5      S   X4'   M6     US   S   S:X  a  U R                  US   US   US	   US
   S9$ US   S   S:X  a  U R                  US   US   S9$ [        SUS   S    35      e)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
r~   r   r   
   public-keyN   dsa   y   g   p   qr|   rz   rx   ry      rsa-pkcs1-sha1   n   er   r   unknown lsh key type )	r   parser   r   r   NSr   r   r)   ra   rp   sexpkdr   s        r4   _fromString_PUBLIC_LSHKey._fromString_PUBLIC_LSH3  s    " {{;tAbz23Aw-'''q'!"+JD||FIIdO4Q7BH &71:))T(bh"T(bh *   !WQZ,,))BtH4)AA 5d1gaj\BCCr3   c                ^   [         R                  " U5      nUS   S:X  d   e0 nUS   SS  H4  u  pA[        R                  " [        R                  " U5      5      S   X4'   M6     US   S   S:X  aA  [        U5      S:X  d   [        U5      5       eU R                  US   US   US	   US
   US   S9$ US   S   S:X  a^  [        U5      S:X  d   [        U5      5       eUS	   US
   :  a  US
   US	   sUS	'   US
'   U R                  US   US   US   US	   US
   S9$ [        SUS   S    35      e)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-keyr~   Nr   r   r   r   r   r      xr   	   rsa-pkcs1r   r   r      dr   r   )	r   r   r   r   r   r   r   r   r)   r   s        r4   _fromString_PRIVATE_LSHKey._fromString_PRIVATE_LSHS  sg   " {{4 Aw.(((q'!"+JD||FIIdO4Q7BH &71:r7a<(R(<))T(bh"T(bh"T( *   !WQZ<'r7a<(R(<$x"T("%'Xr$x"4"T())T(bh"T(bh"T( *  
  5d1gaj\BCCr3   c           	        [         R                  " U5      u  p!US:X  a  [         R                  " U5      u  p1[         R                  " U5      u  pA[         R                  " U5      u  pQ[         R                  " U5      u  pa[         R                  " U5      u  pqU R                  XeX4US9$ US:X  a  [         R                  " U5      u  p[         R                  " U5      u  p[         R                  " U5      u  p[         R                  " U5      u  p[         R                  " U5      u  p1[         R                  " U5      u  pAU R	                  XXXKS9$ [        SU 35      e)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
ru   r   rt   r   r   r   rx   ry   r   r   )r   r   r   r   r   r)   )ra   rp   r   rx   ry   rz   r|   r   r   r   r   r   s               r4   _fromString_AGENTV3Key._fromString_AGENTV3y  s   < T*j ll4(GAll4(GAll4(GAll4(GAll4(GA))Aa)BB
"ll4(GAll4(GAll4(GAll4(GAll4(GAll4(GA))Aa)GG 1';<<r3   c                *   UR                  S5      (       d  UR                  S5      (       a  gUR                  S5      (       d  UR                  S5      (       a  [        S5      eUR                  S5      (       d  UR                  S5      (       a  gUR                  S	5      (       a  g
UR                  S5      (       a  gUR                  S5      (       a  gUR                  S5      (       dX  UR                  S5      (       dB  UR                  S5      (       d,  UR                  S5      (       d  UR                  S5      (       aO  [        R                  " U5      u  p#SnU(       a&  US-  n[        R                  " U5      u  p#U(       a  M&  US:  a  ggg)zz
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   sk-ecdsa-sha2-nistp256-cert-v01s   sk-ssh-ed25519-cert-v01z(certificate based keys are not supporteds   sk-ecdsa-sha2-nistp256s   sk-ssh-ed25519s
   -----BEGINprivate_openssh   {
public_lsh   (private_lshs      ssh-s
      ecdsa-s      ssh-ed25519s&      "sk-ecdsa-sha2-nistp256@openssh.coms      sk-ssh-ed25519@openssh.comr   r~   rv   agentv3r   N)r   r)   r   r   r   )ra   rp   ignoredr   counts        r4   rk   Key._guessStringType  sV    ??7##t~'F'F# ??=>>$//&C
 C
 HII??455:
 :
 $??=))$??4  ??4    OO344899=>>QRRLMM"LL.MGE
 &T 2 $ qy  Nr3   c                R   [         R                  " X!S9nUc  UR                  [        5       5      nOo[         R                  " UUU[         R
                  " X45      [         R                  " X55      [         R                  " XE5      US9n	U	R                  [        5       5      nU " U5      $ )aL  
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   )rx   ry   r   dmp1dmq1iqmppublic_numbers)	r   r   r   r   RSAPrivateNumbersrsa_crt_dmp1rsa_crt_dmq1rsa_crt_iqmpprivate_key)
ra   r   r   r   rx   ry   r   publicNumbersr   privateNumberss
             r4   r   Key._fromRSAComponents  s    6 ,,q69%001BCI 22%%a+%%a+%%a+,N '22?3DEI9~r3   c           	         [         R                  " U[         R                  " X#US9S9nUc  UR                  [	        5       5      nO-[         R
                  " XVS9nUR                  [	        5       5      nU " U5      $ )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.
rw   r{   )r   r  )r   r   r   r   r   DSAPrivateNumbersr"  )	ra   r|   rx   ry   rz   r   r#  r   r$  s	            r4   r   Key._fromDSAComponents  sm    . ,,3#:#:Qq#I
 9%001BCI 22QUN&22?3DEI9~r3   c                    [         R                  " X[        U   S9nUc  UR                  [	        5       5      nO-[         R
                  " XES9nUR                  [	        5       5      nU " U5      $ )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   r|   r   )private_valuer  )r   EllipticCurvePublicNumbersr   r   r   EllipticCurvePrivateNumbersr"  )ra   r   r|   r   r   r#  r   r$  s           r4   _fromECComponentsKey._fromECComponents/  sk    $ 55K.
 %001BCI;;*N '22?3DEI9~r3   c                    Uc(  [         R                  R                  [        U   U5      nO'[         R                  " U[        U   [        5       5      nU " U5      $ )a  
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   r   derive_private_keyr   )ra   r   r   r   r   s        r4   r   Key._fromECEncodedPointO  sW      11DDE"LI --k%0/2CI 9~r3   c                    [         b  [        c  [        S5      eUc  [         R                  U5      nO[        R	                  U5      nU " U5      $ )zBuild 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}
z)Ed25519 keys not supported on this system)Ed25519PublicKeyEd25519PrivateKeyr)   from_public_bytesfrom_private_bytes)ra   r   r   r   s       r4   r   Key._fromEd25519Componentsk  sK     #'8'@IJJ9(::1=I)<<Q?I9~r3   c                    Xl         SU l        g)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.
FN
_keyObjectr   )selfr   s     r4   __init__Key.__init__  s     $r3   c                    [        U[        5      (       aI  U R                  5       UR                  5       :H  =(       a!    U R                  5       UR                  5       :H  $ [        $ )z>
Return True if other represents an object with the same key.
)rQ   rZ   rc   rp   NotImplemented)r<  others     r4   __eq__
Key.__eq__  sD     eS!!99;%**,.N499;%**,3NN!!r3   c                   U R                  5       S:X  a  U R                  5       nUS   R                  S5      nU R                  5       (       a
  SUSS  S3nO	SUSS  S3n[	        UR                  5       5       H   u  pEUS:X  a
  US	U 3-  nM  US
U SU 3-  nM"     US-   $ S[        U R                  5       5      < SU R                  5       =(       a    S=(       d    S< SU R                  5       < S3/n[	        U R                  5       R                  5       5       H  u  pEUR                  SU S35        U R                  5       S:X  a  UO[        R                  " U5      SS nU(       d  MR  USS nUSS nSn	[        U5       H  n
U	[        U
5      S S3-   n	M     [        U5      S:  a  U	SS n	UR                  SU	-   5        U(       a  Ma  M     US   S-   US'   S
R                  U5      $ )z0
Return a pretty representation of this object.
ECr   rh   z<Elliptic Curve Public Key (Nz bits)z<Elliptic Curve Private Key (z	
curve:
	
z:
	z>
< z
Public KeyzPrivate Keyz (zattr :Ed25519rv       02xr   	>)rc   rp   decodeisPublicsorteditemsr!   sizeappendr   MPr    ordr   r   )r<  rp   r   outr   vr   bymorM   s              r4   __repr__Key.__repr__  s    99;$99;D=''0D}}4T"#YKvF5d23i[Gtzz|,<\$00CRs%s++C	 - ; ( !-MMO4EEIIK	E tyy{0023uQCq\*))+2Q		!QR8Hb3BABCBA&q\3q6#,a 00 *1v{crFLL* b 4 b	CE"I99U##r3   c                    [        U R                  [        R                  [        R
                  [        R                  [        R                  45      $ )zT
Check if this instance is a public key.

@return: C{True} if this is a public key.
)
rQ   r;  r   RSAPublicKeyr   DSAPublicKeyr   r   r   r4  r<  s    r4   rR  Key.isPublic  sA     OO    ))((	
 	
r3   c                v    U R                  5       (       a  U $ [        U R                  R                  5       5      $ )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.
)rR  rZ   r;  r   rc  s    r4   public
Key.public  s,     ==??Kt11344r3   c           
        U[         R                  L a9  [        [        [	        U R                  5       5      R                  5       5      5      $ U[         R                  L am  [        SR                  [        [        U R                  5       5      R                  5       5       Vs/ s H  n[        R                  " U5      PM     sn5      5      $ [        SU 35      es  snf )a  
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}
   :z Unsupported fingerprint format: )r@   rC   r!   r   r   r   digestrB   r   r    r   binasciihexlifyr=   )r<  formatr   s      r4   fingerprintKey.fingerprint  s    : '555	&*=*D*D*F GHH)111		2;C		<L<S<S<U2VW2VQX%%a(2VW  ')I&'RSS	 Xs   " Cc                   [        U R                  [        R                  [        R                  45      (       a  g[        U R                  [
        R                  [
        R                  45      (       a  g[        U R                  [        R                  [        R                  45      (       a  g[        U R                  [        R                  [        R                  45      (       a  g[        SU R                  < 35      e)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DSArE  rK  zunknown type of object: )rQ   r;  r   ra  RSAPrivateKeyr   rb  DSAPrivateKeyr   r   EllipticCurvePrivateKeyr   r4  r5  RuntimeErrorrc  s    r4   rc   Key.type  s     doo(8(8#:K:K'LMM#*:*:C<M<M)NOOOOb779S9ST
 
 OOg668Q8QR
 
 !9$//9LMNNr3   c                   U R                   (       a  U R                  5       S:X  a  ggU R                  5       S:X  a9  S[        U R                  R                  R
                  R                  S5         -   $ SSSS	.U R                  5          $ )
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}
rE  r   r   r  r   rt   ru   r   )rq  rr  rK  )r   rc   r   r;  r   r   rU   rc  s    r4   sshTypeKey.sshType  s     88yy{d"<
 199;$DOO,A,A,F,F,M,Mg,V!WW
 %
 ))+	 	r3   c                T    U R                  5       S:X  a  / SQ$ U R                  5       /$ )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-256rt   )rc   ry  rc  s    r4   supportedSignatureAlgorithms Key.supportedSignatureAlgorithms@  s'     99;%AALLN##r3   c                >   U R                  5       S:X  ao  XR                  5       :X  a[  U R                  5       nUS::  a  [        R                  " 5       $ US::  a  [        R
                  " 5       $ [        R                  " 5       $ gU R                  5       S:X  a  [        R                  " 5       $ [        R                  " 5       [        R                  " 5       [        R                  " 5       [        R                  " 5       S.R                  U R                  5       U45      $ )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.
rE     i  NrK  ))rq  rt   )rq  r}  )rq  r|  )rr  ru   )	rc   ry  rU  r   SHA256SHA384SHA512SHA1get)r<  signatureTyper   s      r4   _getHashAlgorithmKey._getHashAlgorithmL  s     99;$.))+c>!==?*^!==?*!==?*99;)#==?" "(&,mmo&,mmo!'	

 #tyy{M*
+	,r3   c                    U R                   c  gU R                  5       S:X  a   U R                   R                  R                  $ U R                  5       S:X  a  gU R                   R                  $ )zV
Return the size of the object we wrap.

@return: The size of the key.
@rtype: L{int}
r   rE  rK  r  )r;  rc   r   key_sizerc  s    r4   rU  Key.sizei  sV     ??"YY[D ??((111YY[I%'''r3   c           	     	   [        U R                  [        R                  5      (       a3  U R                  R	                  5       nUR
                  UR                  S.$ [        U R                  [        R                  5      (       a  U R                  R                  5       nUR                  R
                  UR                  R                  UR                  UR                  UR                  [        R                  " UR                  UR                  5      S.$ [        U R                  [        R                  5      (       ag  U R                  R	                  5       nUR                  UR                   R"                  UR                   R                  UR                   R                  S.$ [        U R                  [        R$                  5      (       a  U R                  R                  5       nUR&                  UR                  R                  UR                  R                   R"                  UR                  R                   R                  UR                  R                   R                  S.$ [        U R                  [(        R*                  5      (       aB  U R                  R	                  5       nUR&                  UR                  U R-                  5       S.$ [        U R                  [(        R.                  5      (       aa  U R                  R                  5       nUR                  R&                  UR                  R                  UR0                  U R-                  5       S.$ [        U R                  [2        R4                  5      (       aN  SU R                  R7                  [8        R:                  R<                  [8        R>                  R<                  5      0$ [        U R                  [2        R@                  5      (       a  U R                  RC                  5       R7                  [8        R:                  R<                  [8        R>                  R<                  5      U R                  RE                  [8        R:                  R<                  [8        RF                  R<                  [8        RH                  " 5       5      S.$ [K        S	U R                   35      e)
zG
Return the values of the public key as a dictionary.

@rtype: L{dict}
r   r
  r   )r   r|   rz   rx   ry   r*  )r   r|   r   r   r   )r   r   zUnexpected key type: )&rQ   r;  r   ra  r  r   r   rs  private_numbersr   rx   ry   r!  r   rb  r|   r}   rz   rt  r   r   r   ry  ru  r+  r   r4  public_bytesr   EncodingRawPublicFormatr5  r   private_bytesPrivateFormatNoEncryptionrv  )r<  rsa_pub_numbersrsa_priv_numbersdsa_pub_numbersdsa_priv_numbersec_pub_numbersec_priv_numberss          r4   rp   Key.datax  s    doos'7'788"oo<<>O$&&$&&  ):):;;#>>@%4466%4466%''%''%''%%&6&8&8:J:L:LM  )9)9::"oo<<>O$&&$6688$6688$6688	  ):):;;#>>@%''%4466%44FFHH%44FFHH%44FFHH  )B)BCC!__;;=N#%%#%% 
 )C)CDD"oo==?O$3355$3355 / = =	  )A)ABBT__11!**..0J0J0N0N 
 )B)BCC__//1>>!**..0J0J0N0N __22!**..!//33!..0		 	 !6t6GHIIr3   c                   U R                  5       nU R                  5       nUS:X  aJ  [        R                  " S5      [        R                  " US   5      -   [        R                  " US   5      -   $ US:X  a~  [        R                  " S5      [        R                  " US   5      -   [        R                  " US   5      -   [        R                  " US	   5      -   [        R                  " US
   5      -   $ US:X  a  U R
                  R                  R                  S-   S-  n[        R                  " US   5      [        R                  " US   SS 5      -   [        R                  " S[        R                  " US   U5      -   [        R                  " US
   U5      -   5      -   $ US:X  a0  [        R                  " S5      [        R                  " US   5      -   $ [        SU 35      e)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  rt   r   r   rr  ru   rx   ry   rz   r|   rE     r   r   N   r   rK  r   r   unknown key type: )rc   rp   r   r   rW  r;  r   r  r   r   r)   )r<  rc   rp   
byteLengths       r4   r   Key.blob  s   @ yy{yy{5=99Z(699T#Y+??&))DQTIBVVVU]		*%))DI&'))DI&' ))DI&' ))DI&	' T\////881<BJ		$w-())DM"#./0))((cJ?@((cJ?@ Y99^,vyyc/CCC 24&9::r3   c                x   U R                  5       nU R                  5       nUS:X  a  [        R                  " US   US   5      n[        R
                  " S5      [        R                  " US   5      -   [        R                  " US   5      -   [        R                  " US   5      -   [        R                  " U5      -   [        R                  " US   5      -   [        R                  " US   5      -   $ US:X  a  [        R
                  " S	5      [        R                  " US   5      -   [        R                  " US   5      -   [        R                  " US
   5      -   [        R                  " US   5      -   [        R                  " US   5      -   $ US:X  a  U R                  R                  5       R                  [        R                  R                  [        R                  R                  5      n[        R
                  " US   5      [        R
                  " US   SS 5      -   [        R
                  " U5      -   [        R                  " US   5      -   $ US:X  aP  [        R
                  " S5      [        R
                  " US   5      -   [        R
                  " US   US   -   5      -   $ [        SU 35      e)aI  
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  rx   ry   rt   r   r   r   rr  ru   rz   r|   r   rE  r   r  Nr   rK  r   r   r   r  )rc   rp   r   r!  r   r   rW  r;  r   r  r   r  X962r  UncompressedPointr)   )r<  rc   rp   r  encPubs        r4   privateBlobKey.privateBlob   sZ   R yy{yy{5=##DItCy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>>&&++**<<F
 		$w-())DM"#./0))F#$ ))D012 Y		.)))DI&'))DIS	123  24&9::r3   extracommentrW   c                >   Ub4  [         R                  " S[        SS9  U R                  5       (       a  UnOUn[	        U[
        5      (       a  UR                  S5      n[        U5      n[        U SUR                  5        3S5      nUc  [        SU 35      eU" X4US9$ )	aB  
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   )
stacklevelrh   
_toString_r  )subtyper  rW   )warningswarnDeprecationWarningrR  rQ   rR   rU   rX   rl   rm   r)   )r<  rc   r  r  r  rW   rq   s          r4   toStringKey.toStringS  s    Z MMI # }}"
gs##nnW-G)*5
DJJL>:DA> 24&9::g:NNr3   c                   U R                  5       S:X  ai  U(       d  SnU R                  R                  [        R                  R
                  [        R                  R
                  5      S-   U-   R                  5       $ [        U R                  5       5      R                  SS5      nU(       d  SnU R                  5       S-   U-   S-   U-   R                  5       $ )z
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.
rE  r3          
)rc   r;  r  r   r  OpenSSHr  r   r   r   replacery  )r<  r  b64Datas      r4   _toPublicOpenSSHKey._toPublicOpenSSH  s     99;$,,!**22M4N4N4V4V  	
 eg diik*225#>G%/$6@GGIIr3   c           	     0   U(       an  [         R                  nSnSnUR                  S-  nSnUn[        R                  " U5      n	Sn
[
        R                  " U	5      [        R                  " SU
5      -   nOSnSnSnSn[        R                  " S	5      nX-   U R                  5       -   [
        R                  " U=(       d    S5      -   nS
n[        U5      U-  (       a,  US-  nU[        US-  45      -  n[        U5      U-  (       a  M,  U(       a  [        R                  " UW	WW-   S5      n[        W" USU 5      [        R                   " XXx-    5      [#        5       S9R%                  5       nUR'                  U5      UR)                  5       -   nOUnS[
        R                  " U5      -   [
        R                  " U5      -   [
        R                  " U5      -   [        R                  " SS5      -   [
        R                  " U R+                  5       5      -   [
        R                  " U5      -   n[-        U5      R/                  SS5      nS/[1        S
[        U5      S5       Vs/ s H  nUUUS-    PM     sn-   S/-   nSR3                  U5      S-   $ s  snf )a  
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   r3   rv   r   r~      Nr   r   r  s#   -----BEGIN OPENSSH PRIVATE KEY-----@   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  rW   r   
cipherNamekdfNamer   r   r   r   r   r   checkr   padByteencKeyr  r   r   r  ir   s                         r4   _toPrivateOpenSSH_v1Key._toPrivateOpenSSH_v1  st      ^^F&JG))Q.IGF))&1DF46;;tV+DDJ JGIJ&&q)md&6&6&88699W^PS;TT+*qLG5'D.!233K +** ZZ
D'F2BCHFvhw'(		&7+;<=') ik	 
 '--k:Y=O=O=QQN(Nii
#$ii ! ii
#$ kk$"	#
 ii		$% ii'( 	 d#++E3734,1!S\2,FG,Fqwq1r6",FGH345 	
 zz% 5(( Hs   $Jc                l   U(       d  [         R                  " 5       nO[         R                  " U5      nU R                  5       S:w  aM  U R                  R                  [         R                  R                  [         R                  R                  U5      $ U R                  5       S:X  d   e[        S5      e)z
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.
rK  zBcannot serialize Ed25519 key to OpenSSH PEM format; use v1 instead)r   r  BestAvailableEncryptionrc   r;  r  r  PEMr  TraditionalOpenSSLr   )r<  rW   r  s      r4   _toPrivateOpenSSH_PEMKey._toPrivateOpenSSH_PEM  s     %224I%==jII99;)#??00&&**++>>  99;)+++W r3   c                    U R                  5       (       a  U R                  US9$ US:X  d  Uc#  U R                  5       S:X  a  U R                  X#S9$ Ub  US:X  a  U R	                  US9$ [        SU 35      e)a
  
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  v1rK  )r  rW   r  rV   zunknown subtype )rR  r  rc   r  r  r   )r<  r  r  rW   s       r4   _toString_OPENSSHKey._toString_OPENSSH	  s    & ==??(((99_TYY[I5M,,W,TT_5 0---DD/y9::r3   c                   U R                  5       nU R                  5       nU R                  5       (       Ga  US:X  aU  [        R                  " SSS[
        R                  " US   5      SS /S[
        R                  " US	   5      SS ////5      nOUS
:X  a  [        R                  " SSS[
        R                  " US   5      SS /S[
        R                  " US   5      SS /S[
        R                  " US   5      SS /S[
        R                  " US   5      SS ////5      nO[        SU 35      eS[        U5      R                  SS5      -   S-   $ US:X  Ga$  US   US   pe[        R                  " XV5      n[        R                  " SSS[
        R                  " US   5      SS /S[
        R                  " US	   5      SS /S[
        R                  " US   5      SS /S[
        R                  " U5      SS /S[
        R                  " U5      SS /S[
        R                  " US   US-
  -  5      SS /S[
        R                  " US   US-
  -  5      SS /S [
        R                  " U5      SS //	//5      $ US
:X  a  [        R                  " SSS[
        R                  " US   5      SS /S[
        R                  " US   5      SS /S[
        R                  " US   5      SS /S[
        R                  " US   5      SS /S![
        R                  " US"   5      SS ////5      $ [        SU S#35      e)$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   rv   Nr   r   rr  r   r   rx   r   ry   r   rz   r   r|   r   r  r  r3      }r  r  r  r      ar~      b   cr  r   ')rp   rc   rR  r   r  r   rW  r)   r   r  r   r!  )r<  kwargsrp   rc   keyDatarx   ry   r  s           r4   _toString_LSHKey._toString_LSH&  sc    yy{yy{==??u}** * 1!%vyyc';AB'? @!%vyyc';AB'? @	 ** * &!%vyyc';AB'? @!%vyyc';AB'? @!%vyyc';AB'? @!%vyyc';AB'? @	 "$5dV"<==+g.66ucBBTIIu}Cy$s)1''-zz + ,!%vyyc';AB'? @!%vyyc';AB'? @!%vyyc';AB'? @!%vyy|AB'7 8!%vyy|AB'7 8!%vyyca!e1D'Eab'I J!%vyyca!e1D'Eab'I J!%vyyqr': ;
 $ zz + &!%vyyc';AB'? @!%vyyc';AB'? @!%vyyc';AB'? @!%vyyc';AB'? @!%vyyc';AB'? @
   "$5dV1"=>>r3   c                   U R                  5       nU R                  5       (       d  U R                  5       S:X  a  US   US   US   US   US   US   4nO*U R                  5       S:X  a  US   US   US	   US
   US   4n[        R                  " U R                  5       5      SR                  [        [        R                  W5      5      -   $ g)zl
Return a private Secure Shell Agent v3 key.  See
_fromString_AGENTV3 for the key format.

@rtype: L{bytes}
rq  r   r   r   r   rx   ry   rr  rz   r|   r   r3   N)	rp   rR  rc   r   r   ry  r   maprW  )r<  r  rp   valuess       r4   _toString_AGENTV3Key._toString_AGENTV3x  s     yy{}}yy{e#IIIIII %s)T#YS	49d3iP99T\\^,sxxFIIv8N/OOO r3   c                   U R                  5       nUc  U R                  5       nU R                  U5      nUc  [        SU< SU S35      eUS:X  aH  U R                  R                  U[        R                  " 5       U5      n[        R                  " U5      nGOTUS:X  aV  U R                  R                  X5      n[        U5      u  px[        R                  " [        US5      [        US5      -   5      nOUS:X  a  U R                  R                  U[        R                  " U5      5      n	[        U	5      u  px[        U5      n
[        U5      nU
S   nUS	-  (       a  S
U
-   n
US   nUS	-  (       a  S
U-   n[        R                  " [        R                  " U
5      [        R                  " U5      -   5      nO5US:X  a/  [        R                  " U R                  R                  U5      5      n[        R                  " U5      W-   $ )a  
Sign some data with this key.

SECSH-TRANS RFC 4253 Section 6.6.

@param data: The data to sign.

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

@return: A signature for the given data.
zpublic key signature algorithm z is not defined for z keysrq  rr     rE  r          rK  )rc   ry  r  r6   r;  signr   PKCS1v15r   r   r#   r   r   ECDSA)r<  rp   r  r   hashAlgorithmsigretrs	signaturer]   sbrcompscomps                 r4   r  Key.sign  s    ))+ 
 !LLNM..}= ,1-1B C&iu. 
 e//&&tW-=-=-?OC))C.C//&&t;C)#.FQ
 ))LB/,q"2EEFC_,,T288M3JKI))4FQaBaBqEE t|r\qEEt|r\))FIIbMFIIbM9:C	!))DOO0067Cyy'#--r3   c                   [        U5      S:X  a  S[        R                  " U5      pO[        R                  " U5      u  p1U R	                  U5      nUc  gU R                  5       nUS:X  ac  U R                  nU R                  5       (       d  UR                  5       n[        R                  " U5      S   U[        R                  " 5       U4nGOUS:X  a  [        R                  " U5      S   n[        R                  USS S	5      n	[        R                  USS S	5      n
[        X5      nU R                  nU R                  5       (       d  UR                  5       nXU4nGOUS
:X  a  [        R                  " U5      S   n[        R                  " US5      u  pn[        R                  US	5      n	[        R                  US	5      n
[        X5      nU R                  nU R                  5       (       d  UR                  5       nX[        R                  " U5      4nORUS:X  aL  U R                  nU R                  5       (       d  UR                  5       n[        R                  " U5      S   U4n WR                   " W6   g! ["         a     gf = f)z
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.
(   ru   NFrq  r   rr  r  bigrE  rj   rK  T)r   r   r   r   r  rc   r;  rR  r   r   r  r   
from_bytesr$   r   r  verifyr   )r<  r  rp   r  r  r   r   argsconcatenatedSignaturer  r  rstrsstrr   s                 r4   r  
Key.verify  s    y>R'1699Y3G9'-||I'>$M..}= ))+eA==??LLNY'*  "	D $*LL$;A$>!4Sb95AA4RS95AA,Q2IA==??LLN]3D_$*LL$;A$>!%||,A1EDtU+AtU+A,Q2IA==??LLNRXXm%<=D	!A==??LLNLL+A.5D	HHdO    		s   .I> >
J
Jr:  )NN)NNNN)N)rA  objectreturnbool)r  rR   )r  z&Literal['RSA', 'DSA', 'EC', 'Ed25519'])r  zdict[str, Any])NNN)rp   r  r  zbytes | Noner  r  )2r-   r.   r/   r0   r1   classmethodre   r_   r   r   r   r   r   r   r   r  r  rk   r   r   r.  r   r   r=  rB  r^  rR  rf  r@   rB   rn  rc   ry  r~  r  rU  rp   r   r  r"   r  r  r  r  r  r  r  r  r  r2   r+   r3   r4   rZ   rZ      s-    > >( %, %,N D; D;L H? H?T * *( V= V=p 6: 6:p A A8 D D> #D #DJ .= .=` 2 2h * *X    D  >  6  ,	"*$X
 5 "4!;!; &TPO.B
$,:(JJX:;xQ;f !i l#	
:O:OxJ4<)|8;:P?dP,?.BDr3   rZ   c                2   U R                  5       R                  SS9  U R                  5       (       d  [        R                  " SU[        5       S9nUR                  [        R                  R                  [        R                  R                  [        R                  " 5       S9nU R                  U5        U R                  S5       n[        R                  " UR!                  5       S[        5       S9n[#        U5      sSSS5        $ ! , (       d  f       g= f)	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_exponentr  r   )encodingrm  encryption_algorithmr]   N)passwordr   )parentmakedirsexistsr   generate_private_keyr   r  r   r  r  r  r  r  
setContentr^   r   r`   rZ   )locationr   
privateKeypemkeyFiles        r4   _getPersistentRSAKeyr    s      OOt< ??--!G_=N

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

 :	 
		s   7D
D)i   )Or1   
__future__r   rk  r   rJ   r  base64r   r   r   hashlibr   r   typingr	   r   
constantlyr
   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.deprecater"   /cryptography.hazmat.primitives.asymmetric.utilsr#   r$   ImportErrorr%   r&   	SECP256R1	SECP384R1	SECP521R1r   r   r4  r5  	Exceptionr)   r6   r:   r=   r@   rE   rX   rZ   r  r+   r3   r4   <module>r/     s3  
 #     6 6    +  4 8 @ T T L L & + 1 $ 9 @	 LLNLLNLLN 
 ++ -- )  	 9 $ $"9 >| |~3(W7   s    E EE