ó
ñ1Mc           @   s"  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d e f d „  ƒ  YZ e j e ƒ e j	 Z	 e j
 Z
 e j Z e j Z d d d „  ƒ  YZ d e f d „  ƒ  YZ d	 „  Z e j d
 „ Z e d „ Z e j d „ Z e j d „ Z e j d „ Z d „  Z d „  Z d „  Z d S(   sc   M2Crypto wrapper for OpenSSL RSA API.

Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved.iÿÿÿÿNt   RSAErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyR       s    t   RSAc           B   s
  e  Z d  Z e j Z d d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d e j d „ Z d e j d „ Z e Z d e j d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d d d „ Z d d „ Z d d „ Z RS(   s   
    RSA Key Pair.
    i    c         C   s   | |  _  | |  _ d  S(   N(   t   rsat   _pyfree(   t   selfR   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   __init__   s    	c         C   s)   t  |  d d ƒ r% |  j |  j ƒ n  d  S(   NR   i    (   t   getattrt   m2_rsa_freeR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   __del__   s    c         C   s   t  j |  j ƒ d >S(   Ni   (   t   m2t   rsa_sizeR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   __len__"   s    c         C   sB   | d k r t  j |  j ƒ S| d k r8 t  j |  j ƒ St ‚ d  S(   Nt   et   n(   R   t	   rsa_get_eR   t	   rsa_get_nt   AttributeError(   R   t   name(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   __getattr__%   s
    c         C   s"   t  j |  j ƒ t  j |  j ƒ f S(   N(   R   R   R   R   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   pub-   s    c         C   s   t  j |  j | | ƒ S(   N(   R   t   rsa_public_encryptR   (   R   t   datat   padding(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   public_encrypt1   s    c         C   s   t  j |  j | | ƒ S(   N(   R   t   rsa_public_decryptR   (   R   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   public_decrypt5   s    c         C   s   t  j |  j | | ƒ S(   N(   R   t   rsa_private_encryptR   (   R   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   private_encrypt9   s    c         C   s   t  j |  j | | ƒ S(   N(   R   t   rsa_private_decryptR   (   R   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   private_decrypt=   s    t   aes_128_cbcc         C   s‚   | d k r( t j |  j | j ƒ  | ƒ St t | d ƒ } | d k rV t d | ‚ n	 | ƒ  } t j |  j | j ƒ  | | ƒ Sd S(   sR  
        Save the key pair to an M2Crypto.BIO.BIO object in PEM format.

        @type bio: M2Crypto.BIO.BIO
        @param bio: M2Crypto.BIO.BIO object to save key to.

        @type cipher: string
        @param cipher: Symmetric cipher to protect the key. The default
        cipher is 'aes_128_cbc'. If cipher is None, then the key is saved
        in the clear.

        @type callback: Python callable
        @param callback: A Python callable object that is invoked
        to acquire a passphrase with which to protect the key.
        The default is util.passphrase_callback.
        s   not such cipher %sN(   t   NoneR   t   rsa_write_key_no_cipherR   t   _ptrR   R    t   rsa_write_key(   R   t   biot   ciphert   callbackt   ciph(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   save_key_bioA   s    	c         C   s%   t  j | d ƒ } |  j | | | ƒ S(   s+  
        Save the key pair to a file in PEM format.

        @type file: string
        @param file: Name of file to save key to.

        @type cipher: string
        @param cipher: Symmetric cipher to protect the key. The default
        cipher is 'aes_128_cbc'. If cipher is None, then the key is saved
        in the clear.

        @type callback: Python callable
        @param callback: A Python callable object that is invoked
        to acquire a passphrase with which to protect the key.
        The default is util.passphrase_callback.
        t   wb(   t   BIOt   openfileR)   (   R   t   fileR&   R'   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   save_key\   s    c         C   s)   t  j ƒ  } |  j | | | ƒ | j ƒ  S(   sB   
        Returns the key(pair) as a string in PEM format.
        (   R+   t   MemoryBufferR)   t   read(   R   R&   R'   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   as_pemr   s    c         C   s   t  j |  j | j ƒ  ƒ S(   s±   
        Save the key pair to an M2Crypto.BIO.BIO object in DER format.

        @type bio: M2Crypto.BIO.BIO
        @param bio: M2Crypto.BIO.BIO object to save key to.
        (   R   t   rsa_write_key_derR   R#   (   R   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   save_key_der_bioz   s    c         C   s   t  j | d ƒ } |  j | ƒ S(   s‚   
        Save the key pair to a file in DER format.

        @type file: str
        @param file: Filename to save key to
        R*   (   R+   R,   R3   (   R   R-   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   save_key_derƒ   s    c         C   s   t  j |  j | j ƒ  ƒ S(   s³   
        Save the public key to an M2Crypto.BIO.BIO object in PEM format.

        @type bio: M2Crypto.BIO.BIO
        @param bio: M2Crypto.BIO.BIO object to save key to.
        (   R   t   rsa_write_pub_keyR   R#   (   R   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   save_pub_key_bio   s    c         C   s+   t  j | d ƒ } t j |  j | j ƒ  ƒ S(   sŒ   
        Save the public key to a file in PEM format.

        @type file: string
        @param file: Name of file to save key to.
        R*   (   R+   R,   R   R5   R   R#   (   R   R-   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   save_pub_key–   s    c         C   s   t  j |  j ƒ S(   N(   R   t   rsa_check_keyR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt	   check_key    s    t   sha1i   c         C   sb   t  t | d ƒ } | d k r1 t d t ƒ ‚ n  t j |  j | | ƒ  | ƒ } |  j | t j ƒ S(   sµ  
        Signs a digest with the private key using RSASSA-PSS
        
        @requires: OpenSSL 0.9.7h or later.

        @type digest: str
        @param digest: A digest created by using the digest method

        @type salt_length: int
        @param salt_length: The length of the salt to use
        
        @type algo: str
        @param algo: The hash algorithm to use

        @return: a string which is the signature
        s   not such hash algorithm %sN(	   R   R   R!   t
   ValueErrort	   hash_algot   rsa_padding_add_pkcs1_pssR   R   t
   no_padding(   R   t   digestt   algot   salt_lengtht   hasht	   signature(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   sign_rsassa_pss£   s
    c         C   se   t  t | d ƒ } | d k r1 t d t ƒ ‚ n  |  j | t j ƒ } t j |  j | | | ƒ  | ƒ S(   s  
        Verifies the signature RSASSA-PSS

        @requires: OpenSSL 0.9.7h or later.

        @type data: str
        @param data: Data that has been signed

        @type signature: str
        @param signature: The signature signed with RSASSA-PSS
        
        @type salt_length: int
        @param salt_length: The length of the salt that was used

        @type algo: str
        @param algo: The hash algorithm to use

        @return: 1 or 0, depending on whether the signature was
        verified or not.  
        s   not such hash algorithm %sN(	   R   R   R!   R;   R<   R   R>   t   rsa_verify_pkcs1_pssR   (   R   R   RC   R@   RA   RB   t   plain_signature(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   verify_rsassa_pss¼   s
    c         C   sJ   t  t d | d ƒ } | d k r4 t d | f ‚ n  t j |  j | | ƒ S(   st  
        Signs a digest with the private key

        @type digest: str
        @param digest: A digest created by using the digest method

        @type algo: str
        @param algo: The method that created the digest.
        Legal values are 'sha1','sha224', 'sha256', 'ripemd160', 
        and 'md5'.
        
        @return: a string which is the signature
        t   NID_s   unknown algorithmN(   R   R   R!   R;   t   rsa_signR   (   R   R?   R@   t   digest_type(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   signÙ   s    c         C   sM   t  t d | d ƒ } | d k r4 t d | f ‚ n  t j |  j | | | ƒ S(   s
  
        Verifies the signature with the public key

        @type data: str
        @param data: Data that has been signed

        @type signature: str
        @param signature: The signature signed with the private key

        @type algo: str
        @param algo: The method use to create digest from the data 
        before it was signed.  Legal values are 'sha1','sha224',
        'sha256', 'ripemd160', and 'md5'.

        @return: True or False, depending on whether the signature was
        verified.  
        RH   s   unknown algorithmN(   R   R   R!   R;   t
   rsa_verifyR   (   R   R   RC   R@   RJ   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   verifyí   s    (   R   R   t   __doc__R   t   rsa_freeR	   R   R
   R   R   R   R   R   R   R   t   utilt   passphrase_callbackR)   R.   t   save_pemR1   R3   R4   R6   R7   R9   RD   RG   RK   RM   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyR      s0   												
			
	t   RSA_pubc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s0   
    Object interface to an RSA public key.
    c         C   s)   | d k r t  d ‚ n | |  j | <d  S(   NR   R   s0   use factory function new_pub_key() to set (e, n)(   R   R   (   R    t   __dict__(   R   R   t   value(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   __setattr__  s    	c         G   s   t  d ‚ d  S(   Ns!   RSA_pub object has no private key(   R    (   R   t   argv(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyR     s    c         G   s   t  d ‚ d  S(   Ns!   RSA_pub object has no private key(   R    (   R   RW   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyR     s    c         O   s   |  j  | ƒ S(   s*   
        Save public key to file.
        (   R7   (   R   R-   t   argst   kw(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyR.     s    c         O   s   |  j  | ƒ S(   s)   
        Save public key to BIO.
        (   R6   (   R   R%   RX   RY   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyR)     s    c         C   s   t  j |  j ƒ S(   N(   R   t   rsa_check_pub_keyR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyR9   )  s    (	   R   R   RN   RV   R   R   R.   R)   R9   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyRS     s   					
c           C   s   t  t j t j ƒ  ƒ ‚ d  S(   N(   R    R   t   err_reason_error_stringt   err_get_error(    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt	   rsa_error-  s    c         C   s1   d d d d g } | j  | |  ƒ | j ƒ  d S(   s)   
    Default callback for gen_key().
    t   .t   +t   *s   
N(   t   writet   flush(   t   pR   t   outt   ch(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   keygen_callback1  s    c         C   s   t  t j |  | | ƒ d ƒ S(   s·  
    Generate an RSA key pair.

    @type bits: int
    @param bits: Key length, in bits.

    @type e: int
    @param e: The RSA public exponent.

    @type callback: Python callable
    @param callback: A Python callable object that is invoked
    during key generation; its usual purpose is to provide visual
    feedback. The default callback is keygen_callback.

    @rtype: M2Crypto.RSA.RSA
    @return: M2Crypto.RSA.RSA object.
    i   (   R   R   t   rsa_generate_key(   t   bitsR   R'   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   gen_key:  s    c         C   s   t  j |  ƒ } t | | ƒ S(   s–  
    Load an RSA key pair from file.

    @type file: string
    @param file: Name of file containing RSA public key in PEM format.

    @type callback: Python callable
    @param callback: A Python callable object that is invoked
    to acquire a passphrase with which to unlock the key.
    The default is util.passphrase_callback.

    @rtype: M2Crypto.RSA.RSA
    @return: M2Crypto.RSA.RSA object.
    (   R+   R,   t   load_key_bio(   R-   R'   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   load_keyO  s    c         C   s;   t  j |  j ƒ  | ƒ } | d k r. t ƒ  n  t | d ƒ S(   sÁ  
    Load an RSA key pair from an M2Crypto.BIO.BIO object.

    @type bio: M2Crypto.BIO.BIO
    @param bio: M2Crypto.BIO.BIO object containing RSA key pair in PEM
    format.

    @type callback: Python callable
    @param callback: A Python callable object that is invoked
    to acquire a passphrase with which to unlock the key.
    The default is util.passphrase_callback.

    @rtype: M2Crypto.RSA.RSA
    @return: M2Crypto.RSA.RSA object.
    i   N(   R   t   rsa_read_keyR#   R!   R]   R   (   R%   R'   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyRj   b  s    
c         C   s   t  j |  ƒ } t | | ƒ S(   s–  
    Load an RSA key pair from a string.

    @type string: string
    @param string: String containing RSA key pair in PEM format.

    @type callback: Python callable
    @param callback: A Python callable object that is invoked
    to acquire a passphrase with which to unlock the key.
    The default is util.passphrase_callback.

    @rtype: M2Crypto.RSA.RSA
    @return: M2Crypto.RSA.RSA object.
    (   R+   R/   Rj   (   t   stringR'   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   load_key_stringx  s    c         C   s   t  j |  ƒ } t | ƒ S(   sÖ   
    Load an RSA public key from file.

    @type file: string
    @param file: Name of file containing RSA public key in PEM format.

    @rtype: M2Crypto.RSA.RSA_pub
    @return: M2Crypto.RSA.RSA_pub object.
    (   R+   R,   t   load_pub_key_bio(   R-   R%   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   load_pub_key‹  s    
c         C   s8   t  j |  j ƒ  ƒ } | d k r+ t ƒ  n  t | d ƒ S(   s  
    Load an RSA public key from an M2Crypto.BIO.BIO object.

    @type bio: M2Crypto.BIO.BIO
    @param bio: M2Crypto.BIO.BIO object containing RSA public key in PEM
    format.

    @rtype: M2Crypto.RSA.RSA_pub
    @return: M2Crypto.RSA.RSA_pub object.
    i   N(   R   t   rsa_read_pub_keyR#   R!   R]   RS   (   R%   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyRo   ™  s    
c         C   sE   |  \ } } t  j ƒ  } t  j | | ƒ t  j | | ƒ t | d ƒ S(   sÿ  
    Instantiate an RSA_pub object from an (e, n) tuple.

    @type e: string
    @param e: The RSA public exponent; it is a string in OpenSSL's MPINT 
    format - 4-byte big-endian bit-count followed by the appropriate 
    number of bits.

    @type n: string
    @param n: The RSA composite of primes; it is a string in OpenSSL's MPINT 
    format - 4-byte big-endian bit-count followed by the appropriate 
    number of bits.

    @rtype: M2Crypto.RSA.RSA_pub
    @return: M2Crypto.RSA.RSA_pub object.
    i   (   R   t   rsa_newt	   rsa_set_et	   rsa_set_nRS   (   t   .0R   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   new_pub_keyª  s
    	(    (   RN   t   sysRP   R+   t   ErrR   t	   ExceptionR    t   rsa_initR>   t   pkcs1_paddingt   sslv23_paddingt   pkcs1_oaep_paddingR   RS   R]   t   stdoutRf   Ri   RQ   Rk   Rj   Rn   Rp   Ro   Rv   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/RSA.pyt   <module>   s&   0				ô'				