3 M(Y/@sdZddlmZmZddlZddlZddlmZddlZddl Zddl Ze ej j jZ ddZGdddeZGd d d eZGd d d eZGd ddeZGdddeZGdddeZGdddeZGdddeZeZeZeZeZeZeZdS)zWCommandline scripts. These scripts are called by the executables defined in setup.py. )with_statementprint_functionN) OptionParsercCstddd}|jdddd|jdd dd d|jd d dd d|jtjdd\}}t|dkrt|jtdyt|d}Wn:t k r|jt d|dtj dtdYnXt d|tj dt j |\}}|jr,t d|jtj d|j|jd}t|jd}|j|WdQRX|j|jd}|jrzt d|jtj dt|jd}|j|WdQRXnt dtj dtjj|dS)zKey generator.zusage: %prog [options] keysizez.Generates a new RSA keypair of "keysize" bits.)usage descriptionz--puboutstringzOutput 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.)typehelpz-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 PEMPEMDER)r choicesdefaultNrzNot a valid number: %s)filezGenerating %i-bit keyzWriting public key to %s)formatwbzWriting private key to %szWriting private key to stdout)r r )r add_option parse_argssysargvlen print_help SystemExitint ValueErrorprintstderrrsaZnewkeysZpuboutZ save_pkcs1formopenwriteoutstdout)parserclicli_argsZkeysizepub_keypriv_keydataoutfiler*./private/tmp/pip-build-nl73fm5q/rsa/rsa/cli.pykeygen#sB   r,c@seZdZdZejZdZdZdZ dZ dZ dZ dZ d Zd Zd ZejZd d ZejdddZddZddZddZddZddZdS)CryptoOperationz9CLI callable that operates with input, output, and a key.publicz'usage: %%prog [options] %(keyname)s_keyNdecrypt 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.rTcCs4|j|jj|_|j|jj|_|j|jj|_dS)N)r __class____dict__ input_help output_help)selfr*r*r+__init__nszCryptoOperation.__init__cCsdS)zPerforms the program's operation. Implement in a subclass. :returns: the data to write to the output. Nr*)r6indatakeyr%r*r*r+perform_operationssz!CryptoOperation.perform_operationcCsd|j\}}|j|d|j}|j|j}t|jjtj d|j |||}|j r`|j ||j dS)zRuns the program.r)rN) parse_cliread_keykeyform read_infileinputroperation_progressivetitlerrr: has_output write_outfileoutput)r6r$r%r9r8outdatar*r*r+__call__|s  zCryptoOperation.__call__cCst|j|jd}|jddd|jd|jr>|jddd|jd|jdd |jdd d |jt j d d\}}t ||j kr|j td ||fS)zFParse the CLI options :returns: (cli_opts, cli_args) )rrz-iz--inputr)rr z-oz--outputz --keyformz&Key format of the %s key - default PEMr r )r r r rN)r r )rrrrr4rBr5keynamerrrrexpected_cli_argsrr)r6r#r$r%r*r*r+r;s zCryptoOperation.parse_clic CsDtd|j|ftjdt|d}|j}WdQRX|jj||S)zReads a public or private key.zReading %s key from %s)rrbN)rrGrrrread key_classZ load_pkcs1)r6filenamer=keyfileZkeydatar*r*r+r<s zCryptoOperation.read_keyc CsH|r0td|tjdt|d }|jSQRXtdtjdtjjS)zRead the input filezReading input from %s)rrINzReading input from stdin)rrrrrJstdin)r6innameinfiler*r*r+r>s  zCryptoOperation.read_infilec CsV|r8td|tjdt|d}|j|WdQRXntdtjdtjj|dS)zWrite the output filezWriting output to %s)rrNzWriting output to stdout)rrrrr r")r6rEoutnamer)r*r*r+rCs  zCryptoOperation.write_outfile)N)__name__ __module__ __qualname____doc__abcABCMetaZ __metaclass__rGrr operationoperation_pastr@r4r5rHrBr PublicKeyrKr7abstractmethodr:rFr;r<r>rCr*r*r*r+r-Zs*   r-c@s.eZdZdZdZdZdZdZdZd dd Z dS) EncryptOperationzEncrypts a file.r.zEncrypts a file. The file must be shorter than the key length in order to be encrypted. For larger files, use the pyrsa-encrypt-bigfile command.encrypt encrypted encryptingNcCs tj||S)zEncrypts files.)rr])r6r8r&r%r*r*r+r:sz"EncryptOperation.perform_operation)N) rRrSrTrUrGrrXrYr@r:r*r*r*r+r\sr\c@s4eZdZdZdZdZdZdZdZe j Z d dd Z dS) DecryptOperationzDecrypts a file.privatezDecrypts a file. The original file must be shorter than the key length in order to have been encrypted. For larger files, use the pyrsa-decrypt-bigfile command.r/r0r1NcCs tj||S)zDecrypts files.)rr/)r6r8r'r%r*r*r+r:sz"DecryptOperation.perform_operation)N) rRrSrTrUrGrrXrYr@r PrivateKeyrKr:r*r*r*r+r`sr`c@sHeZdZdZdZdZddjeZdZ dZ dZ e j Zd Zd Zd d Zd S) SignOperationz Signs a file.raz/usage: %%prog [options] private_key hash_methodzCSigns a file, outputs the signature. Choose the hash method from %sz, sign signatureZSigningz\Name of the file to write the signature to. Written to stdout if this option is not present.cCs0|d}|tkr"tddjttj|||S)z Signs files.rz%Invalid hash method, choose one of %sz, ) HASH_METHODSrjoinrrd)r6r8r'r%Z hash_methodr*r*r+r:s zSignOperation.perform_operationN)rRrSrTrUrGrrhrgrrXrYr@rrbrKrHr5r:r*r*r*r+rcs rcc@s>eZdZdZdZdZdZdZdZdZ e j Z dZ d Zd d Zd S) VerifyOperationzVerify a signature.r.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.verifyZverifiedZ VerifyingrfFcCsj|d}t|d}|j}WdQRXytj|||Wntjk rVtdYnXtdtjddS)zVerifies files.rrINzVerification failed.zVerification OK)r) rrJrrjZVerificationErrorrrrr)r6r8r&r%Zsignature_fileZsigfilerer*r*r+r:s z!VerifyOperation.perform_operationN)rRrSrTrUrGrrrXrYr@rrZrKrHrBr:r*r*r*r+risric@s8eZdZdZddZddZddZdd Zd d Zd S) BigfileOperationz>CryptoOperation that doesn't read the entire file into memory.cCstj|g|_dS)N)r-r7 file_objects)r6r*r*r+r7#s zBigfileOperation.__init__cCsx|jD] }|jqWdS)zCloses any open file handles.N)rlclose)r6fobjr*r*r+__del__(s zBigfileOperation.__del__cCs^|j\}}|j|d|j}|j|j}|j|j}t|jj t j d|j ||||dS)zRuns the program.r)rN) r;r<r= get_infiler? get_outfilerDrr@rArrr:)r6r$r%r9rPr)r*r*r+rF.s    zBigfileOperation.__call__cCsF|r.td|tjdt|d}|jj|ntdtjdtj}|S)zReturns the input file objectzReading input from %s)rrIzReading input from stdin)rrrrrlappendrN)r6rOrnr*r*r+rp=s zBigfileOperation.get_infilecCsF|r.td|tjdt|d}|jj|ntdtjdtj}|S)zReturns the output file objectzWill write output to %s)rrzWill write output to stdout)rrrrrlrrr")r6rQrnr*r*r+rqJs zBigfileOperation.get_outfileN) rRrSrTrUr7rorFrprqr*r*r*r+rk s  rkc@s.eZdZdZdZdZdZdZdZd dd Z dS) EncryptBigfileOperationz#Encrypts a file to VARBLOCK format.r.zEncrypts a file to an encrypted VARBLOCK file. The file can be larger than the key length, but the output file is only compatible with Python-RSA.r]r^r_NcCstjj|||S)zEncrypts files to VARBLOCK.)rbigfileencrypt_bigfile)r6rPr)r&r%r*r*r+r:csz)EncryptBigfileOperation.perform_operation)N) rRrSrTrUrGrrXrYr@r:r*r*r*r+rsXsrsc@s4eZdZdZdZdZdZdZdZe j Z d dd Z dS) DecryptBigfileOperationz#Decrypts a file in VARBLOCK format.razQDecrypts an encrypted VARBLOCK file that was encrypted with pyrsa-encrypt-bigfiler/r0r1NcCstjj|||S)zDecrypts a VARBLOCK file.)rrtdecrypt_bigfile)r6rPr)r'r%r*r*r+r:tsz)DecryptBigfileOperation.perform_operation)N) rRrSrTrUrGrrXrYr@rrbrKr:r*r*r*r+rvisrv)rU __future__rrrVroptparserrZ rsa.bigfileZ rsa.pkcs1sortedZpkcs1rgkeysr,objectr-r\r`rcrirkrsrvr]r/rdrjrurwr*r*r*r+s. 7j8