o
    9F9`&                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlZeej	j
 Z
ejejeje f ZdddZG dd dejdZG d	d
 d
eZG dd deZG dd deZG dd deZe Ze Ze Ze ZdS )zWCommandline scripts.

These scripts are called by the executables defined in setup.py.
    Nreturnc                  C   s  t jddd} | jdddd | jdd	dd
d | jddddd | tjdd \}}t|dkr;|   tdzt	|d }W n t
y]   |   td|d  tjd tdw td| tjd t|\}}|jrtd|j tjd |j|jd}t|jd}|| W d   n1 sw   Y  |j|jd}|jrtd|j tjd t|jd}|| W d   dS 1 sw   Y  dS tdtjd tjj| dS )zKey generator.zusage: %prog [options] keysizez.Generates a new RSA keypair of "keysize" bits.usagedescriptionz--puboutstringzOutput filename for the public key. The public key is not saved if this option is not present. You can use pyrsa-priv2pub to create the public key file later.typehelp-oz--outz`Output filename for the private key. The key is written to stdout if this option is not present.z--formz7key format of the private and public keys - default PEMPEMZDERr   r	   choicesdefault   Nr   zNot a valid number: %sfilezGenerating %i-bit keyzWriting public key to %s)formatwbzWriting private key to %szWriting private key to stdout)optparseOptionParser
add_option
parse_argssysargvlen
print_help
SystemExitint
ValueErrorprintstderrrsaZnewkeysZpuboutZ
save_pkcs1Zformopenwriteoutstdoutbuffer)parserclicli_argsZkeysizepub_keypriv_keydataoutfile r/   'lib/python3.10/site-packages/rsa/cli.pykeygen!   sP   
"r1   c                	   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZejZd$ddZejdedejjdedejfddZd$ddZdejejeje  f fddZ!de de dejjfddZ"de defddZ#d ed!e ddfd"d#Z$dS )%CryptoOperationz9CLI callable that operates with input, output, and a key.publicz'usage: %%prog [options] %(keyname)s_key decrypt	decrypted
decryptingzEName of the file to %(operation)s. Reads from stdin if not specified.zjName of the file to write the %(operation_past)s file to. Written to stdout if this option is not present.r   Tr   Nc                 C   s4   | j | jj | _ | j| jj | _| j| jj | _d S )N)r   	__class____dict__
input_helpoutput_help)selfr/   r/   r0   __init__j   s   zCryptoOperation.__init__indatakeyr*   c                 C   s   dS )zPerforms the program's operation.

        Implement in a subclass.

        :returns: the data to write to the output.
        Nr/   )r<   r>   r?   r*   r/   r/   r0   perform_operationo   s    z!CryptoOperation.perform_operationc                 C   sh   |   \}}| |d |j}| |j}t| j tj	d | 
|||}| jr2| ||j dS dS )zRuns the program.r   r   N)	parse_cliread_keykeyformread_infileinputr    operation_progressivetitler   r!   r@   
has_outputwrite_outfileoutput)r<   r)   r*   r?   r>   outdatar/   r/   r0   __call__y   s   zCryptoOperation.__call__c                 C   s   t j| j| jd}|jddd| jd | jr |jddd| jd |jdd	| j d
dd |	t
jdd \}}t|| jkrG|  td||fS )zFParse the CLI options

        :returns: (cli_opts, cli_args)
        r   z-iz--inputr   r   r
   z--outputz	--keyformz&Key format of the %s key - default PEMr   r   r   r   N)r   r   r   r   r   r:   rH   r;   keynamer   r   r   r   expected_cli_argsr   r   )r<   r(   r)   r*   r/   r/   r0   rA      s   zCryptoOperation.parse_clifilenamerC   c                 C   sX   t d| j|f tjd t|d}| }W d   n1 s w   Y  | j||S )zReads a public or private key.zReading %s key from %sr   rbN)r    rM   r   r!   r#   read	key_classZ
load_pkcs1)r<   rO   rC   ZkeyfileZkeydatar/   r/   r0   rB      s
   
zCryptoOperation.read_keyinnamec                 C   sb   |r$t d| tjd t|d}| W  d   S 1 sw   Y  t dtjd tjj S )zRead the input filezReading input from %sr   rP   NzReading input from stdin)r    r   r!   r#   rQ   stdinr'   )r<   rS   Zinfiler/   r/   r0   rD      s    zCryptoOperation.read_infilerK   outnamec                 C   sp   |r(t d| tjd t|d}|| W d   dS 1 s!w   Y  dS t dtjd tjj| dS )zWrite the output filezWriting output to %sr   r   NzWriting output to stdout)r    r   r!   r#   r$   r&   r'   )r<   rK   rU   r.   r/   r/   r0   rI      s   "zCryptoOperation.write_outfiler   N)%__name__
__module____qualname____doc__rM   r   r   	operationoperation_pastrF   r:   r;   rN   rH   r"   	PublicKeyrR   r=   abcabstractmethodbytesr?   AbstractKey	IndexabletypingZAnyr@   rL   Tupler   ZValuesListstrrA   rB   rD   rI   r/   r/   r/   r0   r2   X   s2    

	 	r2   )	metaclassc                	   @   sF   e Zd ZdZdZdZdZdZdZ	dde	d	e
jjd
ede	fddZdS )EncryptOperationzEncrypts a file.r3   zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encryptZ	encryptedZ
encryptingr/   r>   r+   r*   r   c                 C      t |tjjs	J t||S )zEncrypts files.)
isinstancer"   r?   r]   ri   )r<   r>   r+   r*   r/   r/   r0   r@         z"EncryptOperation.perform_operationNr/   )rW   rX   rY   rZ   rM   r   r[   r\   rF   r`   r"   r?   ra   rb   r@   r/   r/   r/   r0   rh      s    rh   c                	   @   sL   e Zd ZdZdZdZdZdZdZe	j
Z	dded	e	jjd
edefddZdS )DecryptOperationzDecrypts a file.privatezgDecrypts a file. The original file must be shorter than the key length in order to have been encrypted.r5   r6   r7   r/   r>   r,   r*   r   c                 C   rj   )zDecrypts files.)rk   r"   r?   
PrivateKeyr5   )r<   r>   r,   r*   r/   r/   r0   r@      rl   z"DecryptOperation.perform_operationNrm   )rW   rX   rY   rZ   rM   r   r[   r\   rF   r"   rp   rR   r`   r?   ra   rb   r@   r/   r/   r/   r0   rn      s    rn   c                   @   s^   e Zd ZdZdZdZdde ZdZ	dZ
dZejZd	Zd
ZdedejjdedefddZdS )SignOperationzSigns a file.ro   z/usage: %%prog [options] private_key hash_methodzCSigns a file, outputs the signature. Choose the hash method from %s, sign	signatureZSigning   z\Name of the file to write the signature to. Written to stdout if this option is not present.r>   r,   r*   r   c                 C   sB   t |tjjs	J |d }|tvrtddt t|||S )zSigns files.r   z%Invalid hash method, choose one of %srr   )rk   r"   r?   rp   HASH_METHODSr   joinrs   )r<   r>   r,   r*   Zhash_methodr/   r/   r0   r@      s   zSignOperation.perform_operationN)rW   rX   rY   rZ   rM   r   rw   rv   r   r[   r\   rF   r"   rp   rR   rN   r;   r`   r?   ra   rb   r@   r/   r/   r/   r0   rq      s$    rq   c                   @   sT   e Zd ZdZdZdZdZdZdZdZ	e
jZdZd	Zd
ede
jjdeddfddZdS )VerifyOperationzVerify a signature.r3   z1usage: %%prog [options] public_key signature_filezsVerifies a signature, exits with status 0 upon success, prints an error message and exits with status 1 upon error.verifyZverifiedZ	Verifyingru   Fr>   r+   r*   r   Nc                 C   s   t |tjjs	J |d }t|d}| }W d   n1 s!w   Y  z	t||| W n tjy;   tdw t	dt
jd dS )zVerifies files.r   rP   NzVerification failed.zVerification OKr   )rk   r"   r?   r]   r#   rQ   ry   ZVerificationErrorr   r    r   r!   )r<   r>   r+   r*   Zsignature_fileZsigfilert   r/   r/   r0   r@     s   
z!VerifyOperation.perform_operation)rW   rX   rY   rZ   rM   r   r   r[   r\   rF   r"   r]   rR   rN   rH   r`   r?   ra   rb   r@   r/   r/   r/   r0   rx     s     rx   rV   )rZ   r^   r   rc   r   r"   Zrsa.keyZ	rsa.pkcs1sortedZpkcs1rv   keysZUnionrd   re   rf   rb   r1   ABCMetar2   rh   rn   rq   rx   ri   r5   rs   ry   r/   r/   r/   r0   <module>   s(   
7i 
