
    h0                       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rSSKJ	r	  SSK
Jr  SSKJr  SSKJrJrJr  SSKJr  SS	KJrJrJrJr  \R                  \R2                  :X  a   SSKr\R6                  \R8                  4  \ " 5       r!S r" " S S\RF                  5      r$S r%S r&S r'\"" S5      S 5       r(\"" S5      S 5       r)\"" S5      S 5       r*\"" S5      S 5       r+S r,  S!       S"S jjr-S#S jr.S r/S r0S$S jr1 S%       S&S jjr2\3S :X  a  \%" 5         gg! \\4 a    S\R>                  S
'   \" \5         Nf = f)'z2
Implementation module for the `ckeygen` command.
    )annotationsN)Callable)wraps)reload)AnyDictOptional)keys)failurefilepathlogusagetermiosc                   ^  U 4S jnU$ )Nc                D   >^  [        T 5      U 4S j5       nU[        T'   U$ )Nc                    > T" U 0 UD6$ N )argskwargskeygenerators     d/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/conch/scripts/ckeygen.pywrapper:_keyGenerator.<locals>.assignkeygenerator.<locals>.wrapper%   s    000    )r   supportedKeyTypes)r   r   keyTypes   ` r   assignkeygenerator)_keyGenerator.<locals>.assignkeygenerator$   s,    	|		1 
	1 &-'"r   r   )r   r   s   ` r   _keyGeneratorr    #   s     r   c            	         \ rS rSrSrSr/ SQ/ SQ/ SQ/ SQ/ SQ/ S	Q/ S
Q/ SQ/r/ SQ/ SQ/ SQ/ SQ/ SQ/r\R                  " \R                  " \" \R                  5       5      5      \R                  " SS/5      S.S9rSrg)GeneralOptions/   zUsage:    ckeygen [options]
 z8ckeygen manipulates public/private keys in various ways.)bitsbNz$Number of bits in the key to create.)filenamefNzFilename of the key file.)typetNzSpecify type of key to create.)commentCNzProvide new comment.)newpassNNzProvide new passphrase.)passPNzProvide old passphrase.)formatosha256-base64zFingerprint format of key file.)private-key-subtypeNNz5OpenSSH private key subtype to write ("PEM" or "v1").)fingerprintlzShow fingerprint of key file.)
changepasspz&Change passphrase of private key file.)quietqzQuiet.)no-passphraseNz"Create the key with no passphrase.)showpubyz+Read private key file and print public key.PEMv1)r(   r3   )
optActionsr   N)__name__
__module____qualname____firstlineno__synopsislongdescoptParametersoptFlagsr   CompletionsCompleteListlistr   r
   compData__static_attributes__r   r   r   r"   r"   /   s    H JH 	D<=696K	
M" 	>E EGH   &&t,=,B,B,D'EF#(#5#5udm#D
Hr   r"   c            	     V   [        5       n  U R                  [        R                  SS  5        [        R                  " 5         [        [        l        U S   (       a  U S   R                  5       [        ;   a2  [        SU S   -  5        [        U S   R                  5          " U 5        g [        R                  " SU S   < SSR                  [        R!                  5       5      < 35        g U S   (       a  [#        U 5        g U S	   (       a  [%        U 5        g U S
   (       a  ['        U 5        g U R                  5         [        R                  " S5        g ! [        R
                   a?  n[        SU-  5        U R                  5         [        R                  " S5         S nAGNuS nAff = f)N   z	ERROR: %sr(   z&Generating public/private %s key pair.zKey type was z, must be one of z, r4   r6   r;   )r"   parseOptionssysargvr   
UsageErrorprintopt_helpexitr   discardLogshandleErrordeferrlowerr   joinr
   printFingerprintchangePassPhrasedisplayPublicKey)optionsus     r   runr`   U   s7   GSXXab\*
 OOCJv6?  "&77:gfoNOgfo3356w?HH6?DII.?.D.D.F$GI 
	!		!		!/  kAos   "E F()4F##F(c                    U S   S:X  a  [         R                  R                  U S'   U $ U S   S:X  a  [         R                  R                  U S'   U $ [         R                  " SU S    35      e)Nr0   zmd5-hexr2   z Unsupported fingerprint format: )r
   FingerprintFormatsMD5_HEXSHA256_BASE64BadFingerPrintFormat)r^   s    r   enumrepresentationrf   s   su    xI% 33;;		o	- 33AA''.wx/@.AB
 	
r   c                 Z    Sq [        R                  " [        R                  " 5       5        e )N   )
exitStatusr   errr   Failurer   r   r   rW   rW      s    JGGGOO	r   rsac                    SSK Jn  SSKJn  U S   (       d  SU S'   UR	                  [        U S   5      SU" 5       S9n[        R                  " U5      n[        X@5        g )Nr   default_backend)rl   r$   i   i  )key_sizepublic_exponentbackend)	cryptography.hazmat.backendsro   )cryptography.hazmat.primitives.asymmetricrl   generate_private_keyintr
   Key_saveKey)r^   ro   rl   keyPrimitivekeys        r   generateRSAkeyr{      sZ    <=6?++WV_%! , L
 ((<
 CSr   dsac                    SSK Jn  SSKJn  U S   (       d  SU S'   UR	                  [        U S   5      U" 5       S9n[        R                  " U5      n[        X@5        g )Nr   rn   )r|   r$   i   )rp   rr   )	rs   ro   rt   r|   ru   rv   r
   rw   rx   )r^   ro   r|   ry   rz   s        r   generateDSAkeyr~      sW    <=6?++WV_%! , L ((<
 CSr   ecdsac                   SSK Jn  SSKJn  U S   (       d  SU S'   S[	        U S   5      R                  S5      -   nUR                  [        R                  U   U" 5       S9n[        R                  " U5      n[        XP5        g )	Nr   rn   )ecr$      s   ecdsa-sha2-nistpascii)curverr   )rs   ro   rt   r   strencoderu   r
   _curveTablerw   rx   )r^   ro   r   r   ry   rz   s         r   generateECDSAkeyr      sx    <<6?  #gfo"6"="=g"FFE**u%/@ + L ((<
 CSr   ed25519c                    [         R                  R                  5       n[         R                  " U5      n[	        X 5        g r   )r
   Ed25519PrivateKeygeneraterw   rx   )r^   ry   rz   s      r   generateEd25519keyr      s,    ))224L
((<
 CSr   c                    U S:X  a  gg)z
Return a reasonable default private key subtype for a given key type.

@type keyType: L{str}
@param keyType: A key type, as returned by
    L{twisted.conch.ssh.keys.Key.type}.

@rtype: L{str}
@return: A private OpenSSH key subtype (C{'PEM'} or C{'v1'}).
Ed25519r>   r=   r   )r   s    r   _defaultPrivateKeySubtyper      s     )r   c                $   Uc  [         nU S   nU(       dq  [        R                  R                  SU 35      n[        R
                  " 5       S:X  a"  [        R                  R                  SU 35      nU" SU-  5      =(       d    Un[        U5      $ )z
If C{options["filename"]} is None, prompt the user to enter a path
or attempt to set it to .ssh/id_rsa
@param options: command line options
@param inputCollector: dependency injection for testing
@param keyTypeName: key type or "rsa"
r&   z
~/.ssh/id_Windowsz%HOMEPATH %\.ssh\id_z%Enter file in which the key is (%s): )inputospath
expanduserplatformsystemr   )r^   inputCollectorkeyTypeNamer&   s       r   _getKeyOrDefaultr      s     z"H77%%
;-&@A??	)ww))-A+*OPHBXMN  	 x=r   c           	        [        U 5      n[        R                  R                  US-   5      (       a  US-  n[	        U 5      n  [
        R                  R                  U5      n[        UR                  5       < SUR                  U S   5      < S[        R                  R                  U5      < 35        g ! [
        R                   a    [        R                  " S5         g [         a    [        R                  " U S35         g f = f)N.pub r0   zbad key, could not be opened, please specify a file.)r   r   r   existsrf   r
   rw   fromFilerS   sizer4   basenameBadKeyErrorrP   rU   FileNotFoundError)r^   r&   rz   s      r   r[   r[      s    (H	ww~~h'((F )GLhh) 
 12  *		
   LH:IJKLs   A2B7 7*D#"DDc                   [        U 5      n [        R                  R                  U5      nU R                  S5      (       dD   [        R                  " S5      n[        R                  " S	5      nXE:X  a  O[        S
5        M?  X@S'   U R                  S5      c  [        WR                  5       5      U S'    WR                  SU S   U S   S9n [        R                  R!                  WU S   S9  [#        US5       nUR%                  W5        S S S 5        [        S5        g ! [        R                   a    U R                  S5      (       d  [        R                  " S5      U S'    [        R                  R                  XS   S9n GN[! [        R                   a    [        R                  " S5          GN[        R                   a%  n[        R                  " SU 35         S nA GNS nAff = f[        R                   a$  n[        R                  " SU 35         S nAGNS nAf[         a    [        R                  " U S35         GNf = f! [         a$  n[        R                  " SU 35         S nAGNS nAff = f! [        R                  [        R                  4 a$  n[        R                  " SU 35         S nAGNS nAff = f! , (       d  f       GN= f)Nr.   zEnter old passphrase: 
passphrasez1Could not change passphrase: old passphrase errorzCould not change passphrase: r   r,   z0Enter new passphrase (empty for no passphrase): Enter same passphrase again: %Passphrases do not match.  Try again.r3   opensshsubtyper   wbz;Your identification has been saved with the new passphrase.)r   r
   rw   r   EncryptedKeyErrorgetgetpassr   rP   rU   r   rS   r   r(   toString	Exception
fromStringopenwrite)r^   r&   rz   ep1p2
newkeydatafds           r   r\   r\      si   (HLhh)  ;;y!!!STB!@ABx9:   	{{()1)B388:)N%&6\\12y) " 

6J793EF 
h	
 
 

GHY !! 	:{{6""%oo.FGGFO	:((##H#IC 	JHHHII%% 	:HH4QC899	: 60455 LH:IJKL*  604556
 ""D$4$45 604556 
	s   D  5I !J  :K AI$ F*G-1I5G-G(!I(G--IH""&II
I=I88I= $K$KK
Kc                   [        U 5      n [        R                  R                  U5      nWR                  5       R                  S5      R                  S5      n[        U5        g ! [         a    [
        R                  " U S35         N_[        R                   aR    U R                  S5      (       d  [        R                  " S5      U S'   [        R                  R                  XS   S9n Nf = f)Nr   r.   zEnter passphrase: r   r   r   )r   r
   rw   r   r   rP   rU   r   r   r   publicr   decoderS   )r^   r&   rz   
displayKeys       r   r]   r]   2  s    (HFhh) &&y188AJ	*  LH:IJK!! F{{6""%oo.BCGFOhhV_EFs   A& &#C0A"C0/C0c                    [        U 5      $ )zj
Ask the user where to save the key.

This needs to be a separate function so the unit test can patch it.
)r   )prompts    r   _inputSaveFiler   @  s     =r   c                   Uc  [         nSSSSS.nX0R                  5          nUS   nU(       d4  [        XU5      n[        SU S	35      nUR	                  5       =(       d    Un[
        R                  R                  U5      (       aB  [        U S
35        U" S5      nUS   R                  5       S:w  a  [        R                  " 5         UR                  S5      (       a  SUS'   ONUS   (       dD   [        R                  " S5      n	[        R                  " S5      n
X:X  a  O[        S5        M?  XS'   UR                  S5      c  [        U R                  5       5      US'   [        R                  " 5        S[         R"                  " 5        3n[$        R&                  " U5      nUR)                  U R+                  SUS   US   S95        UR-                  S5        [$        R&                  " US-   5      R)                  U R/                  5       R+                  SUS95        [1        U5      n[        SU 35        [        SU S35        [        SUS    S35        [        U R3                  US   5      5        g) z
Persist a SSH key on local filesystem.

@param key: Key which is persisted on local filesystem.

@param options:

@param inputCollector: Dependency injection for testing.
Nr   r   rl   r|   )ECr   RSADSAr&   z%Enter file in which to save the key (z): z already exists.zOverwrite (y/n)? r   r<   r:   r   r.   z,Enter passphrase (empty for no passphrase): r   r   r3   @r   r   i  r   )r*   z&Your identification has been saved in z"Your public key has been saved in zThe key fingerprint in r0   z is:)r   r(   r   r   stripr   r   r   rS   rY   rP   rU   r   r   r   getusersocketgethostnamer   FilePath
setContentr   chmodr   rf   r4   )rz   r^   r   KeyTypeMappingr   r&   defaultPathnewPathynr   r   r*   fps                r   rx   rx   I  s>    #	%PUVN ,Kz"H&wL 3K=D
 ==?1k	ww~~h
*+,/0a5;;=CHHJ{{?##V_!OPB!@ABx9:  {{()1)B388:)N%&"#1V%7%7%9$:;G			8	$BMM12v 	 	
 HHXh'(33

i9 !)G	28*
=>	.xj
=>	#GH$5#6d
;<	#//'(+
,-r   __main__)Nrl   )r^   Dict[Any, Any]r   Optional[Callable[[str], str]]r   r   returnr   )r^   r   r   None)r   r   r   r   r   )rz   zkeys.Keyr^   r   r   r   r   r   )4__doc__
__future__r   r   r   r   r   rP   collections.abcr   	functoolsr   	importlibr   typingr   r   r	   twisted.conch.sshr
   twisted.pythonr   r   r   r   unix_getpassr   	tcgetattr	tcsetattrImportErrorAttributeErrormodulesdictr   r    Optionsr"   r`   rf   rW   r{   r~   r   r   r   r   r[   r\   r]   r   rx   r@   r   r   r   <module>r      s  
 #  	   
 $   & & " 8 8
??g***7,,,
 F 	#U]] #L<


 u  u  w   y ( 6:2  		4L*0If 6:C.	C.C. 3C. 
	C.L zE g ( !%Iws   &D# #EE