
    Yf0                    x   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Zddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZmZmZmZ ej        ej        k    r6	 ddlZej        ej        f n"# eef$ r dej        d
<    ee           Y nw xY w e             Z!d Z" G d dej#                  Z$d Z%d Z&d Z' e"d          d             Z( e"d          d             Z) e"d          d             Z* e"d          d             Z+d Z,	 	 d,d-d!Z-d.d#Z.d$ Z/d% Z0d/d'Z1	 d0d1d*Z2e3d+k    r e%             dS dS )2z2
Implementation module for the `ckeygen` command.
    )annotationsN)Callable)wraps)reload)AnyDictOptional)keys)failurefilepathlogusagetermiosc                      fd}|S )Nc                R     t                      fd            }|t          <   |S )Nc                      | i |S N )argskwargskeygenerators     O/var/www/html/env/lib/python3.11/site-packages/twisted/conch/scripts/ckeygen.pywrapperz:_keyGenerator.<locals>.assignkeygenerator.<locals>.wrapper%   s    <0000    )r   supportedKeyTypes)r   r   keyTypes   ` r   assignkeygeneratorz)_keyGenerator.<locals>.assignkeygenerator$   sB    	|			1 	1 	1 	1 
		1 &-'"r   r   )r   r   s   ` r   _keyGeneratorr   #   s$         r   c            	         e Zd ZdZdZg dg dg dg dg dg dg d	g d
gZg dg dg dg dg dgZ ej         ej	         e
e                                                     ej	        ddg          d          ZdS )GeneralOptionsz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%   r0   )
optActionsN)__name__
__module____qualname__synopsislongdescoptParametersoptFlagsr   CompletionsCompleteListlistr   r
   compDatar   r   r   r    r    /   s       H JH 	DCC<<<===666999666KKK	
 	
 	
M" 	>==EEE   EEEGGGH !u &E&tt,=,B,B,D,D'E'EFF#55#5udm#D#D
 
  HHHr   r    c            	        t                      } 	 |                     t          j        dd                     nV# t          j        $ rD}t          d|z             |                                  t          j        d           Y d }~nd }~ww xY wt          j
                     t          t          _        | d         r| d                                         t          v rGt          d| d         z             t          | d                                                  |            d S t          j        d| d         dd                    t                                                               d S | d         rt#          |            d S | d	         rt%          |            d S | d
         rt'          |            d S |                                  t          j        d           d S )N   z	ERROR: %sr%   z&Generating public/private %s key pair.zKey type was z, must be one of z, r1   r3   r8   )r    parseOptionssysargvr   
UsageErrorprintopt_helpexitr   discardLogshandleErrordeferrlowerr   joinr
   printFingerprintchangePassPhrasedisplayPublicKey)optionsus     r   runr[   U   s   GSXabb\****   kAo OCJv 6?  ""&777:gfoNOOOgfo33556w?????HH6???DII.?.D.D.F.F$G$G$GI     
	 !!!!!		 !!!!!		 !!!!!s   '8 B:BBc                    | d         dk    rt           j        j        | d<   | S | d         dk    rt           j        j        | d<   | S t          j        d| d                    )Nr-   zmd5-hexr/   z Unsupported fingerprint format: )r
   FingerprintFormatsMD5_HEXSHA256_BASE64BadFingerPrintFormat)rY   s    r   enumrepresentationra   s   sq    xI%% 3;		o	-	- 3A'Bwx/@BB
 
 	
r   c                 R    da t          j        t          j                                )N   )
exitStatusr   errr   Failurer   r   r   rR   rR      s"    JGGO	r   rsac                    ddl m} ddlm} | d         sd| d<   |                    t          | d                   d |                      }t          j        |          }t          ||            d S )Nr   default_backend)rg   r!   i   i  )key_sizepublic_exponentbackend)	cryptography.hazmat.backendsrj   )cryptography.hazmat.primitives.asymmetricrg   generate_private_keyintr
   Key_saveKey)rY   rj   rg   keyPrimitivekeys        r   generateRSAkeyrv      s    <<<<<<======6? ++WV_%%!! ,  L
 (<
 
 CS'r   dsac                    ddl m} ddlm} | d         sd| d<   |                    t          | d                    |                      }t          j        |          }t          ||            d S )Nr   ri   )rw   r!   i   )rk   rm   )	rn   rj   ro   rw   rp   rq   r
   rr   rs   )rY   rj   rw   rt   ru   s        r   generateDSAkeyry      s    <<<<<<======6? ++WV_%%!! ,  L (<
 
 CS'r   ecdsac                4   ddl m} ddlm} | d         sd| d<   dt	          | d                                       d          z   }|                    t          j        |          |                      }t          j	        |          }t          ||            d S )	Nr   ri   )ecr!      s   ecdsa-sha2-nistpascii)curverm   )rn   rj   ro   r|   strencoderp   r
   _curveTablerr   rs   )rY   rj   r|   r   rt   ru   s         r   generateECDSAkeyr      s    <<<<<<<<<<<<6?   #gfo"6"6"="=g"F"FFE**u%/@/@ +  L (<
 
 CS'r   ed25519c                    t           j                                        }t          j        |          }t	          ||            d S r   )r
   Ed25519PrivateKeygeneraterr   rs   )rY   rt   ru   s      r   generateEd25519keyr      s;    )2244L
(<
 
 CS'r   c                    | dk    rdS dS )a  
    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     )tur   rY   Dict[Any, Any]inputCollectorOptional[Callable[[str], str]]keyTypeNamer   returnc                   |t           }| d         }|skt          j                            d|           }t	          j                    dk    r"t          j                            d|           } |d|z            p|}t          |          S )a  
    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"
    Nr#   z
~/.ssh/id_Windowsz%HOMEPATH %\.ssh\id_z%Enter file in which the key is (%s): )inputospath
expanduserplatformsystemr   )rY   r   r   r#   s       r   _getKeyOrDefaultr      s     z"H 
7%%&@;&@&@AA?	))w))*O+*O*OPPHNBXMNN  	 x==r   Nonec           	     (   t          |           }t          j                            |dz             r|dz  }t	          |           } 	 t
          j                            |          }t          |	                                d|
                    | d                   dt          j                            |                     d S # t
          j        $ r t          j        d           Y d S t          $ r t          j        | d           Y d S w xY w)N.pub r-   zbad key, could not be opened, please specify a file.)r   r   r   existsra   r
   rr   fromFilerN   sizer1   basenameBadKeyErrorrK   rP   FileNotFoundError)rY   r#   ru   s      r   rV   rV      s/   ((H	w~~h'(( F ))GLh)) 



 12222  ***		
 	
 	
 	
 	
     L L LHJJJKKKKKKLs   A>C #D- DDc                   t          |           }	 t          j                            |          }n# t          j        $ r |                     d          st          j        d          | d<   	 t          j                            || d                   }nW# t          j        $ r t          j	        d           Y n2t          j        $ r!}t          j	        d|            Y d }~nd }~ww xY wY nTt          j        $ r!}t          j	        d|            Y d }~n*d }~wt          $ r t          j	        | d           Y nw xY w|                     d          sE	 t          j        d	          }t          j        d
          }||k    rnt          d           ?|| d<   |                     d          $t          |                                          | d<   	 |                    d| d         | d                   }n.# t          $ r!}t          j	        d|            Y d }~nd }~ww xY w	 t          j                            || d                    n?# t          j        t          j        f$ r!}t          j	        d|            Y d }~nd }~ww xY wt#          |d          5 }|                    |           d d d            n# 1 swxY w Y   t          d           d S )Nr+   zEnter old passphrase: 
passphrasez1Could not change passphrase: old passphrase errorzCould not change passphrase: r   r)   rI   z0Enter new passphrase (empty for no passphrase): Enter same passphrase again: %Passphrases do not match.  Try again.r0   opensshsubtyper   wbz;Your identification has been saved with the new passphrase.)r   r
   rr   r   EncryptedKeyErrorgetgetpassr   rK   rP   r   rN   r   r%   toString	Exception
fromStringopenwrite)rY   r#   ru   ep1p2
newkeydatafds           r   rW   rW      s   ((HLh))! 	: 	: 	:{{6"" 	H%o.FGGGFO	:(##H#IICC 	J 	J 	JHHIIIII% 	: 	: 	:H8Q8899999999	: 6 6 644455555555 L L LHJJJKKKKKL ;;y!!  	;!STTB!@AABRxx9:::	;  	{{())1)B388::)N)N%&6\\12y) " 
 



  6 6 64445555555566J793EFFFF"D$45 6 6 6444555555556 
h		 
               

GHHHHHs   2 ;E .'BE #C*9E ;C*	C% E %C**E /E =D$E ?E $G< <
H'H""H'+'I J.J

J"KKKc                   t          |           }	 t          j                            |          }n# t          $ r t          j        | d           Y ngt          j        $ rV |                     d          st          j	        d          | d<   t          j                            || d                   }Y nw xY w|
                                                    d                              d          }t          |           d S )Nr   r+   zEnter passphrase: r   r   r~   )r   r
   rr   r   r   rK   rP   r   r   r   publicr   decoderN   )rY   r#   ru   
displayKeys       r   rX   rX   2  s   ((HFh)) L L LHJJJKKKKK! F F F{{6"" 	D%o.BCCGFOhWV_EEF &&y1188AAJ	*s   1 !B8A!B87B8promptc                     t          |           S )zv
    Ask the user where to save the key.

    This needs to be a separate function so the unit test can patch it.
    )r   )r   s    r   _inputSaveFiler   @  s     ==r   ru   keys.Keyc                   |t           }ddddd}||                                          }|d         }|s:t          |||          }t          d| d	          }|                                p|}t
          j                            |          rNt          | d
            |d          }|d         	                                dk    rt          j                     |                    d          rd|d<   nM|d         sE	 t          j        d          }	t          j        d          }
|	|
k    rnt          d           ?|	|d<   |                    d          $t          |                                           |d<   t          j                     dt!          j                     }t%          j        |          }|                    |                     d|d         |d                              |                    d           t%          j        |dz                                 |                                                     d|                     t1          |          }t          d|            t          d| d           t          d|d          d            t          |                     |d                              dS )!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.
    Nrz   r   rg   rw   )ECr   RSADSAr#   z%Enter file in which to save the key (z): z already exists.zOverwrite (y/n)? r   r9   r7   r   r+   rI   z,Enter passphrase (empty for no passphrase): r   r   r0   @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 r-   z is:)r   r%   r   r   stripr   r   r   rN   rT   rK   rP   r   r   r   getusersocketgethostnamer   FilePath
setContentr   chmodr   ra   r1   )ru   rY   r   KeyTypeMappingr   r#   defaultPathnewPathynr   r   r'   fps                r   rs   rs   I  s    #	%PUVVN ,Kz"H 2&wLL DKDDD
 
 ==??1k	w~~h +++,,,^/00a5;;==CHJJJ{{?## 	V_ 	;!OPPB!@AABRxx9:::	; {{())1)B388::)N)N%&"";;V%7%9%9;;G		8	$	$BMM12v 	 	
 	
   HHXh'((33

i99   !))G	
=8
=
=>>>	
=x
=
=
=>>>	
;GH$5
;
;
;<<<	#//'(+
,
,-----r   __main__)Nrg   )rY   r   r   r   r   r   r   r   )rY   r   r   r   )r   r   r   r   r   )ru   r   rY   r   r   r   r   r   )4__doc__
__future__r   r   r   r   r   rK   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[   ra   rR   rv   ry   r   r   r   r   rV   rW   rX   r   rs   r=   r   r   r   <module>r      s  
  # " " " " "  				   



 $ $ $ $ $ $             & & & & & & & & & & " " " " " " 8 8 8 8 8 8 8 8 8 8 8 8
?g***7,,,,(   !%Iw DFF 	 	 	# # # # #U] # # #L  <

 

 


 
 
 u   u   w    y    ( 6:    4L L L L*0I 0I 0If      6:C. C. C. C. C.L zCEEEEE s   A/ /BB