3
uQh&                 @   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
j Z
ejejeje f Zd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 | jtjdd \}}t|dkrv| j  tdyt	|d }W nJ t
k
r } z.| j  td|d  tjd td|W Y dd}~X nX td| tjd tj|\}}|jr>td|j tjd |j|jd}t|jd}|j| W dQ R X |j|jd}|jrtd|j tjd t|jd}|j| W dQ R X ntdtjd tjjj| dS )zKey generator.zusage: %prog [options] keysizez/Generates a new RSA key pair 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helpz-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PEMDER)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)r   r	   )optparseOptionParser
add_option
parse_argssysargvlen
print_help
SystemExitint
ValueErrorprintstderrrsaZnewkeysZpuboutZ
save_pkcs1formopenwriteoutstdoutbuffer)	parserclicli_argsZkeysizeexpub_keypriv_keydataoutfile r,   &/tmp/pip-build-2nz6shyl/rsa/rsa/cli.pykeygen!   sR    r.   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dZejeejjeejdddZddddZejejeje  f dddZ!e e ejjdddZ"e edddZ#ee dd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   TN)r   c             C   s4   | j | jj | _ | j| jj | _| j| jj | _d S )N)r   	__class____dict__
input_helpoutput_help)selfr,   r,   r-   __init__w   s    zCryptoOperation.__init__)indatakeyr&   r   c             C   s   dS )zPerforms the program's operation.

        Implement in a subclass.

        :returns: the data to write to the output.
        Nr,   )r9   r;   r<   r&   r,   r,   r-   perform_operation|   s    z!CryptoOperation.perform_operationc             C   sd   | j  \}}| j|d |j}| j|j}t| jj tj	d | j
|||}| jr`| j||j 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)r9   r%   r&   r<   r;   outdatar,   r,   r-   __call__   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 |j	t
jdd \}}t|| jkr|j  td||fS )zFParse the CLI options

        :returns: (cli_opts, cli_args)
        )r   r   z-iz--inputr   )r   r   z-oz--outputz	--keyformz&Key format of the %s key - default PEMr   r	   )r   r
   r   r   N)r   r	   )r   r   r   r   r   r7   rE   r8   keynamer   r   r   r   expected_cli_argsr   r   )r9   r$   r%   r&   r,   r,   r-   r>      s    zCryptoOperation.parse_cli)filenamer@   r   c          
   C   sD   t d| j|f tjd t|d}|j }W dQ R X | jj||S )zReads a public or private key.zReading %s key from %s)r   rbN)r   rJ   r   r   r   read	key_classZ
load_pkcs1)r9   rL   r@   keyfileZkeydatar,   r,   r-   r?      s    zCryptoOperation.read_key)innamer   c             C   sJ   |r0t d| tjd t|d
}|j S Q R X t dtjd tjjj S )zRead the input filezReading input from %s)r   rM   NzReading input from stdin)r   r   r   r   rN   stdinr#   )r9   rQ   infiler,   r,   r-   rA      s    zCryptoOperation.read_infile)rH   outnamer   c          	   C   sX   |r8t d| tjd t|d}|j| W dQ R X nt dtjd tjjj| dS )zWrite the output filezWriting output to %s)r   r   NzWriting output to stdout)r   r   r   r   r    r"   r#   )r9   rH   rT   r+   r,   r,   r-   rF      s    zCryptoOperation.write_outfile)%__name__
__module____qualname____doc__rJ   r   r   	operationoperation_pastrC   r7   r8   rK   rE   r   	PublicKeyrO   r:   abcabstractmethodbytesr<   AbstractKey	IndexabletypingZAnyr=   rI   Tupler   ValuesListstrr>   r?   rA   rF   r,   r,   r,   r-   r/   d   s*   
 	r/   )	metaclassc               @   s@   e Zd ZdZdZdZdZdZdZf fe	e
jjee	ddd	Zd
S )EncryptOperationzEncrypts a file.r0   zWEncrypts a file. The file must be shorter than the key length in order to be encrypted.encryptZ	encryptedZ
encrypting)r;   r(   r&   r   c             C   s   t |tjjsttj||S )zEncrypts files.)
isinstancer   r<   r[   AssertionErrorrh   )r9   r;   r(   r&   r,   r,   r-   r=      s    z"EncryptOperation.perform_operationN)rU   rV   rW   rX   rJ   r   rY   rZ   rC   r^   r   r<   r_   r`   r=   r,   r,   r,   r-   rg      s   rg   c               @   sF   e Zd ZdZdZdZdZdZdZe	j
Zf fee	jjeed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.r2   r3   r4   )r;   r)   r&   r   c             C   s   t |tjjsttj||S )zDecrypts files.)ri   r   r<   
PrivateKeyrj   r2   )r9   r;   r)   r&   r,   r,   r-   r=      s    z"DecryptOperation.perform_operationN)rU   rV   rW   rX   rJ   r   rY   rZ   rC   r   rm   rO   r^   r<   r_   r`   r=   r,   r,   r,   r-   rk      s   rk   c               @   sX   e Zd ZdZdZdZddje ZdZ	dZ
dZejZd	Zd
ZeejjeedddZdS )SignOperationzSigns a file.rl   z/usage: %%prog [options] private_key hash_methodzCSigns a file, outputs the signature. Choose the hash method from %sz, 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t|d }|tkr4tddjt tj|||S )zSigns files.r   z%Invalid hash method, choose one of %sz, )	ri   r   r<   rm   rj   HASH_METHODSr   joinro   )r9   r;   r)   r&   Zhash_methodr,   r,   r-   r=     s
    zSignOperation.perform_operationN)rU   rV   rW   rX   rJ   r   rs   rr   r   rY   rZ   rC   r   rm   rO   rK   r8   r^   r<   r_   r`   r=   r,   r,   r,   r-   rn      s   
rn   c               @   sN   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ee
jjed
dddZd
S )VerifyOperationzVerify a signature.r0   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	Verifyingrq   FN)r;   r(   r&   r   c             C   s   t |tjjst|d }t|d}|j }W dQ R X ytj||| W n. tjk
rx } zt	d|W Y dd}~X nX t
dtjd dS )zVerifies files.r   rM   NzVerification failed.zVerification OK)r   )ri   r   r<   r[   rj   r   rN   ru   ZVerificationErrorr   r   r   r   )r9   r;   r(   r&   Zsignature_fileZsigfilerp   r'   r,   r,   r-   r=   +  s    z!VerifyOperation.perform_operation)rU   rV   rW   rX   rJ   r   r   rY   rZ   rC   r   r[   rO   rK   rE   r^   r<   r_   r`   r=   r,   r,   r,   r-   rt     s   
rt   )rX   r\   r   ra   r   r   Zrsa.keyZ	rsa.pkcs1sortedZpkcs1rr   keysUnionrb   rd   re   r`   r.   ABCMetar/   rg   rk   rn   rt   rh   r2   ro   ru   r,   r,   r,   r-   <module>   s&   Cn!#