ó |~úVc@s®dZddlZddlmZddlZddlZddlZddlZddl Zej e ƒZ dZ defd„ƒYZdefd„ƒYZd efd „ƒYZejjed „Zd „Zd „Zee d„Zede d„Zdd dgZe dkrªddlZyex^edƒD]PZejƒ\ZZ erVPnerleddksxedkr4deGHq4q4WWne!k r¡dGHqªXdGHndS(sjRSA key generation code. Create new keys with the newkeys() function. It will give you a PublicKey and a PrivateKey object. Loading and saving keys requires the pyasn1 module. This module is imported as late as possible, such that other functionality will remain working in absence of pyasn1. .. note:: Storing public and private keys via the `pickle` module is possible. However, it is insecure to load a key from an untrusted source. The pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source. iÿÿÿÿN(tbit AbstractKeycBs\eZdZd Zd„Zedd„ƒZed„ƒZdd„Z d„Z d „Z RS( s0Abstract superclass for private and public keys.tntecCs||_||_dS(N(RR(tselfRR((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt__init__6s tPEMcCs6i|jd6|jd6}|j||ƒ}||ƒS(sLoads a key in PKCS#1 DER or PEM format. :param keyfile: contents of a DER- or PEM-encoded file that contains the public key. :param format: the format of the file to load; 'PEM' or 'DER' :return: a PublicKey object RtDER(t_load_pkcs1_pemt_load_pkcs1_dert_assert_format_exists(tclstkeyfiletformattmethodstmethod((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt load_pkcs1:s   cCsUy ||SWnBtk rPdjt|jƒƒƒ}td||fƒ‚nXdS(sBChecks whether the given file format exists in 'methods'. s, s%Unsupported format: %r, try one of %sN(tKeyErrortjointsortedtkeyst ValueError(t file_formatRtformats((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR Ms    cCs3i|jd6|jd6}|j||ƒ}|ƒS(s«Saves the public key in PKCS#1 DER or PEM format. :param format: the format to save; 'PEM' or 'DER' :returns: the DER- or PEM-encoded public key. RR(t_save_pkcs1_pemt_save_pkcs1_derR (RR RR((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt save_pkcs1Ys   cCs!|t||j|jƒ|jS(s¹Performs blinding on the message using random number 'r'. :param message: the message, as integer, to blind. :type message: int :param r: the random number to blind with. :type r: int :return: the blinded message. :rtype: int The blinding is such that message = unblind(decrypt(blind(encrypt(message))). See https://en.wikipedia.org/wiki/Blinding_%28cryptography%29 (tpowRR(Rtmessagetr((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytblindhscCs!tjj||jƒ||jS(s‚Performs blinding on the message using random number 'r'. :param blinded: the blinded message, as integer, to unblind. :param r: the random number to unblind with. :return: the original message. The blinding is such that message = unblind(decrypt(blind(encrypt(message))). See https://en.wikipedia.org/wiki/Blinding_%28cryptography%29 (trsatcommontinverseR(RtblindedR((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytunblindys (RR( t__name__t __module__t__doc__t __slots__Rt classmethodRt staticmethodR RRR#(((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR1s   t PublicKeycBs˜eZdZdZd„Zd„Zd„Zd„Zd„Zd„Z e d „ƒZ d „Z e d „ƒZ d „Ze d „ƒZe d„ƒZRS(s†Represents a public RSA key. This key is also known as the 'encryption key'. It contains the 'n' and 'e' values. Supports attributes as well as dictionary-like access. Attribute accesss is faster, though. >>> PublicKey(5, 3) PublicKey(5, 3) >>> key = PublicKey(5, 3) >>> key.n 5 >>> key['n'] 5 >>> key.e 3 >>> key['e'] 3 RRcCs t||ƒS(N(tgetattr(Rtkey((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt __getitem__¢scCsd|j|jfS(NsPublicKey(%i, %i)(RR(R((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt__repr__¥scCs|j|jfS(s&Returns the key as tuple for pickling.(RR(R((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt __getstate__¨scCs|\|_|_dS(sSets the key from tuple.N(RR(Rtstate((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt __setstate__¬scCsE|dkrtSt|tƒs#tS|j|jkoD|j|jkS(N(tNonetFalset isinstanceR*RR(Rtother((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt__eq__°s  cCs ||k S(N((RR5((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt__ne__¹scCseddlm}ddlm}|j|d|ƒƒ\}}|dt|dƒdt|dƒƒS( sÍLoads a key in PKCS#1 DER format. :param keyfile: contents of a DER-encoded file that contains the public key. :return: a PublicKey object First let's construct a DER encoded key: >>> import base64 >>> b64der = 'MAwCBQCNGmYtAgMBAAE=' >>> der = base64.standard_b64decode(b64der) This loads the file: >>> PublicKey._load_pkcs1_der(der) PublicKey(2367317549, 65537) iÿÿÿÿ(tdecoder(t AsnPubKeytasn1SpecRtmodulusRtpublicExponent(tpyasn1.codec.derR8trsa.asn1R9tdecodetint(R R R8R9tprivt_((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR ¼scCs\ddlm}ddlm}|ƒ}|jd|jƒ|jd|jƒ|j|ƒS(sbSaves the public key in PKCS#1 DER format. @returns: the DER-encoded public key. iÿÿÿÿ(tencoder(R9R;R<(R=RCR>R9tsetComponentByNameRRtencode(RRCR9tasn_key((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR×s  cCs"tjj|dƒ}|j|ƒS(sOLoads a PKCS#1 PEM-encoded public key file. The contents of the file before the "-----BEGIN RSA PUBLIC KEY-----" and after the "-----END RSA PUBLIC KEY-----" lines is ignored. :param keyfile: contents of a PEM-encoded file that contains the public key. :return: a PublicKey object sRSA PUBLIC KEY(Rtpemtload_pemR (R R tder((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRçs cCs|jƒ}tjj|dƒS(sƒSaves a PKCS#1 PEM-encoded public key file. :return: contents of a PEM-encoded file that contains the public key. sRSA PUBLIC KEY(RRRGtsave_pem(RRI((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRös cCs"tjj|dƒ}|j|ƒS(sÞLoads a PKCS#1.5 PEM-encoded public key file from OpenSSL. These files can be recognised in that they start with BEGIN PUBLIC KEY rather than BEGIN RSA PUBLIC KEY. The contents of the file before the "-----BEGIN PUBLIC KEY-----" and after the "-----END PUBLIC KEY-----" lines is ignored. :param keyfile: contents of a PEM-encoded file that contains the public key, from OpenSSL. :return: a PublicKey object s PUBLIC KEY(RRGRHtload_pkcs1_openssl_der(R R RI((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytload_pkcs1_openssl_pemÿscCsddlm}ddlm}ddlm}|j|d|ƒƒ\}}|dd|jdƒkrztd ƒ‚n|j |d d ƒS( sÖLoads a PKCS#1 DER-encoded public key file from OpenSSL. :param keyfile: contents of a DER-encoded file that contains the public key, from OpenSSL. :return: a PublicKey object iÿÿÿÿ(t OpenSSLPubKey(R8(tunivR:theadertoids1.2.840.113549.1.1.1s7This is not a DER-encoded OpenSSL-compatible public keyR,i( R>RMR=R8t pyasn1.typeRNR?tObjectIdentifiert TypeErrorR (R R RMR8RNtkeyinfoRB((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRKs (RR(R$R%R&R'R-R.R/R1R6R7R(R RRRRLRK(((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR*ˆs       t PrivateKeycBsžeZdZdZdddd „Zd „Zd „Zd „Zd „Z d„Z d„Z d„Z d„Z ed„ƒZd„Zed„ƒZd„ZRS(s;Represents a private RSA key. This key is also known as the 'decryption key'. It contains the 'n', 'e', 'd', 'p', 'q' and other values. Supports attributes as well as dictionary-like access. Attribute accesss is faster, though. >>> PrivateKey(3247, 65537, 833, 191, 17) PrivateKey(3247, 65537, 833, 191, 17) exp1, exp2 and coef can be given, but if None or omitted they will be calculated: >>> pk = PrivateKey(3727264081, 65537, 3349121513, 65063, 57287, exp2=4) >>> pk.exp1 55063 >>> pk.exp2 # this is of course not a correct value, but it is the one we passed. 4 >>> pk.coef 50797 If you give exp1, exp2 or coef, they will be used as-is: >>> pk = PrivateKey(1, 2, 3, 4, 5, 6, 7, 8) >>> pk.exp1 6 >>> pk.exp2 7 >>> pk.coef 8 RRtdtptqtexp1texp2tcoefc CsÀtj|||ƒ||_||_||_|dkrTt||dƒ|_n ||_|dkrƒt||dƒ|_n ||_|dkr³t j j ||ƒ|_ n ||_ dS(Ni( RRRVRWRXR2R@RYRZRR R!R[( RRRRVRWRXRYRZR[((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRKs        cCs t||ƒS(N(R+(RR,((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR-ascCsd|S(Ns-PrivateKey(%(n)i, %(e)i, %(d)i, %(p)i, %(q)i)((R((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR.dscCs4|j|j|j|j|j|j|j|jfS(s&Returns the key as tuple for pickling.(RRRVRWRXRYRZR[(R((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR/gsc Cs:|\|_|_|_|_|_|_|_|_dS(sSets the key from tuple.N(RRRVRWRXRYRZR[(RR0((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR1kscCs±|dkrtSt|tƒs#tS|j|jko°|j|jko°|j|jko°|j|jko°|j|jko°|j |j ko°|j |j ko°|j |j kS(N( R2R3R4RURRRVRWRXRYRZR[(RR5((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR6os cCs ||k S(N((RR5((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR7scCsYtjj|jdƒ}|j||ƒ}tjj||j|jƒ}|j||ƒS(sØDecrypts the message using blinding to prevent side-channel attacks. :param encrypted: the encrypted message :type encrypted: int :returns: the decrypted message :rtype: int i( RtrandnumtrandintRRtcoret decrypt_intRVR#(Rt encryptedtblind_rR"t decrypted((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytblinded_decrypt‚s cCsYtjj|jdƒ}|j||ƒ}tjj||j|jƒ}|j||ƒS(sÕEncrypts the message using blinding to prevent side-channel attacks. :param message: the message to encrypt :type message: int :returns: the encrypted message :rtype: int i( RR\R]RRR^t encrypt_intRVR#(RRRaR"R`((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytblinded_encrypt’s cCssddlm}|j|ƒ\}}|ddkrLtd|dƒ‚ntd„|dd!Dƒƒ}||ŒS(sLoads a key in PKCS#1 DER format. :param keyfile: contents of a DER-encoded file that contains the private key. :return: a PrivateKey object First let's construct a DER encoded key: >>> import base64 >>> b64der = 'MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt' >>> der = base64.standard_b64decode(b64der) This loads the file: >>> PrivateKey._load_pkcs1_der(der) PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) iÿÿÿÿ(R8is)Unable to read this file, version %s != 0css|]}t|ƒVqdS(N(R@(t.0tx((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pys Ësii (R=R8R?Rttuple(R R R8RARBtas_ints((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyR ¡s csddlm‰m‰ddlm}dˆjf‡‡fd†ƒY}|ƒ}|jddƒ|jd|jƒ|jd |jƒ|jd |j ƒ|jd |j ƒ|jd |j ƒ|jd |j ƒ|jd|j ƒ|jd|jƒ|j|ƒS(sdSaves the private key in PKCS#1 DER format. @returns: the DER-encoded private key. iÿÿÿÿ(RNt namedtype(RCt AsnPrivKeyc sÑeZˆjˆjdˆjƒƒˆjdˆjƒƒˆjdˆjƒƒˆjdˆjƒƒˆjdˆjƒƒˆjdˆjƒƒˆjdˆjƒƒˆjdˆjƒƒˆjdˆjƒƒƒ ZRS( tversionR;R<tprivateExponenttprime1tprime2t exponent1t exponent2t coefficient(R$R%t NamedTypest NamedTypetIntegert componentType((RjRN(s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRk×sRliR;R<RmRnRoRpRqRr(RQRNRjR=RCtSequenceRDRRRVRWRXRYRZR[RE(RRCRkRF((RjRNs2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRÎs" cCs(tjj|tdƒƒ}|j|ƒS(sTLoads a PKCS#1 PEM-encoded private key file. The contents of the file before the "-----BEGIN RSA PRIVATE KEY-----" and after the "-----END RSA PRIVATE KEY-----" lines is ignored. :param keyfile: contents of a PEM-encoded file that contains the private key. :return: a PrivateKey object sRSA PRIVATE KEY(RRGRHRR (R R RI((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRòs cCs%|jƒ}tjj|tdƒƒS(s…Saves a PKCS#1 PEM-encoded private key file. :return: contents of a PEM-encoded file that contains the private key. sRSA PRIVATE KEY(RRRGRJR(RRI((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRs (RRRVRWRXRYRZR[N(R$R%R&R'R2RR-R.R/R1R6R7RcReR(R RRR(((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyRU's         - $c sÒ|d‰|d}||}||}tjd|ƒ||ƒ}tjd|ƒ||ƒ}‡‡fd†}t} x;|||ƒsµ| rŸ||ƒ}n ||ƒ}| } q{Wt||ƒt||ƒfS(s%Returns a tuple of two different primes of nbits bits each. The resulting p * q has exacty 2 * nbits bits, and the returned p and q will not be equal. :param nbits: the number of bits in each of p and q. :param getprime_func: the getprime function, defaults to :py:func:`rsa.prime.getprime`. *Introduced in Python-RSA 3.1* :param accurate: whether to enable accurate mode or not. :returns: (p, q), where p > q >>> (p, q) = find_p_q(128) >>> from rsa import common >>> common.bit_size(p * q) 256 When not in accurate mode, the number of bits can be slightly less >>> (p, q) = find_p_q(128, accurate=False) >>> from rsa import common >>> common.bit_size(p * q) <= 256 True >>> common.bit_size(p * q) > 240 True iisfind_p_q(%i): Finding psfind_p_q(%i): Finding qcs:||krtSˆstStjj||ƒ}ˆ|kS(s“Returns True iff p and q are acceptable: - p and q differ - (p * q) has the right nr of bits (when accurate=True) (R3tTrueRR tbit_size(RWRXt found_size(taccuratet total_bits(s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt is_acceptable8s  (tlogtdebugR3tmaxtmin( tnbitst getprime_funcR{tshifttpbitstqbitsRWRXR}tchange_p((R{R|s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytfind_p_q s         cCs|d|d}ytjj||ƒ}Wn'tk rTtd||fƒ‚nX|||dkr…td|||fƒ‚n||fS(s¶Calculates an encryption and a decryption key given p, q and an exponent, and returns them as a tuple (e, d) :param p: the first large prime :param q: the second large prime :param exponent: the exponent for the key; only change this if you know what you're doing, as the exponent influences how difficult your private key can be cracked. A very common choice for e is 65537. :type exponent: int is.e (%d) and phi_n (%d) are not relatively primes6e (%d) and d (%d) are not mult. inv. modulo phi_n (%d)(RR R!R(RWRXtexponenttphi_nRV((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytcalculate_keys_custom_exponentYs  cCst||tƒS(sûCalculates an encryption and a decryption key given p and q, and returns them as a tuple (e, d) :param p: the first large prime :param q: the second large prime :return: tuple (e, d) with the encryption and decryption exponents. (R‹tDEFAULT_EXPONENT(RWRX((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytcalculate_keysus cCsmxZtr\t|d||ƒ\}}y t||d|ƒ\}}PWqtk rXqXqW||||fS(sWGenerate RSA keys of nbits bits. Returns (p, q, e, d). Note: this can take a long time, depending on the key size. :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and ``q`` will use ``nbits/2`` bits. :param getprime_func: either :py:func:`rsa.prime.getprime` or a function with similar signature. :param exponent: the exponent for the key; only change this if you know what you're doing, as the exponent influences how difficult your private key can be cracked. A very common choice for e is 65537. :type exponent: int iR‰(RxRˆR‹R(R‚RƒR{R‰RWRXRRV((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytgen_keys‚s  ic Csß|dkrtdƒ‚n|dkr:td|ƒ‚n|dkr}ddlm}ddl}|j|jd|ƒ}n tjj}t||d |d |ƒ\}}} } ||} t| | ƒt | | | ||ƒfS( sGenerates public and private keys, and returns them as (pub, priv). The public key is also known as the 'encryption key', and is a :py:class:`rsa.PublicKey` object. The private key is also known as the 'decryption key' and is a :py:class:`rsa.PrivateKey` object. :param nbits: the number of bits required to store ``n = p*q``. :param accurate: when True, ``n`` will have exactly the number of bits you asked for. However, this makes key generation much slower. When False, `n`` may have slightly less bits. :param poolsize: the number of processes to use to generate the prime numbers. If set to a number > 1, a parallel algorithm will be used. This requires Python 2.6 or newer. :param exponent: the exponent for the key; only change this if you know what you're doing, as the exponent influences how difficult your private key can be cracked. A very common choice for e is 65537. :type exponent: int :returns: a tuple (:py:class:`rsa.PublicKey`, :py:class:`rsa.PrivateKey`) The ``poolsize`` parameter was added in *Python-RSA 3.1* and requires Python 2.6 or newer. is Key too smallisPool size (%i) should be >= 1iÿÿÿÿ(tparallelNtpoolsizeR{R‰( RRRt functoolstpartialtgetprimetprimeRŽR*RU( R‚R{RR‰RR‘RƒRWRXRRVR((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pytnewkeysžs     '  R•t__main__idi is%i timestAborteds Doctests done("R&tloggingt rsa._compatRt rsa.primeRtrsa.pemt rsa.commont rsa.randnumtrsa.coret getLoggerR$R~RŒtobjectRR*RUR”R“RxRˆR‹RRŽR•t__all__tdoctesttrangetcountttestmodtfailurestteststKeyboardInterrupt(((s2/opt/awscli/lib/python2.7/site-packages/rsa/key.pyt"s<      WŸäN  5  "