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

Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved.

Portions Copyright (c) 2004-2007 Open Source Applications Foundation.
Author: Heikki Toivonen
iÿÿÿÿ(   t   Errt   utilt   BIOt   RSANt   EVPErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR      s    c         C   s   t  j |  | | | ƒ S(   s§  
    Derive a key from password using PBKDF2 algorithm specified in RFC 2898.
    
    @param password: Derive the key from this password.
    @type password:  str
    @param salt:     Salt.
    @type salt:      str
    @param iter:     Number of iterations to perform.
    @type iter:      int 
    @param keylen:   Length of key to produce.
    @type keylen:    int
    @return:         Key.
    @rtype:          str
    (   t   m2t   pkcs5_pbkdf2_hmac_sha1(   t   passwordt   saltt   itert   keylen(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   pbkdf2   s    t   MessageDigestc           B   sA   e  Z d  Z e j Z d „  Z d „  Z d „  Z d „  Z	 e	 Z
 RS(   s   
    Message Digest
    c         C   se   t  t | d  ƒ } | d  k r0 t d | f ‚ n  | ƒ  |  _ t j ƒ  |  _ t j |  j |  j ƒ d  S(   Ns   unknown algorithm(   t   getattrR   t   Nonet
   ValueErrort   mdt
   md_ctx_newt   ctxt   digest_init(   t   selft   algoR   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   __init__(   s    c         C   s)   t  |  d d  ƒ r% |  j |  j ƒ n  d  S(   NR   (   R   R   t   m2_md_ctx_freeR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   __del__0   s    c         C   s   t  j |  j | ƒ S(   s   
        Add data to be digested.
        
        @return: -1 for Python error, 1 for success, 0 for OpenSSL failure.
        (   R   t   digest_updateR   (   R   t   data(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   update4   s    c         C   s   t  j |  j ƒ S(   N(   R   t   digest_finalR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   final<   s    (   R   R   t   __doc__R   t   md_ctx_freeR   R   R   R   R   t   digest(    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   "   s   					t   HMACc           B   sG   e  Z e j Z d  d „ Z d „  Z d „  Z d „  Z d „  Z	 e	 Z
 RS(   t   sha1c         C   sh   t  t | d  ƒ } | d  k r0 t d | f ‚ n  | ƒ  |  _ t j ƒ  |  _ t j |  j | |  j ƒ d  S(   Ns   unknown algorithm(   R   R   R   R   R   t   hmac_ctx_newR   t	   hmac_init(   R   t   keyR   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   G   s    c         C   s)   t  |  d d  ƒ r% |  j |  j ƒ n  d  S(   NR   (   R   R   t   m2_hmac_ctx_freeR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   O   s    c         C   s   t  j |  j | |  j ƒ d  S(   N(   R   R&   R   R   (   R   R'   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   resetS   s    c         C   s   t  j |  j | ƒ d  S(   N(   R   t   hmac_updateR   (   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   V   s    c         C   s   t  j |  j ƒ S(   N(   R   t
   hmac_finalR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   Y   s    (   R   R   R   t   hmac_ctx_freeR(   R   R   R)   R   R   R"   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR#   C   s   					R$   c         C   sF   t  t | d  ƒ } | d  k r0 t d | f ‚ n  t j |  | | ƒ  ƒ S(   Ns   unknown algorithm(   R   R   R   R   t   hmac(   R'   R   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR-   ^   s    t   Cipherc           B   sP   e  Z e j Z d  d d d d d „ Z d „  Z d „  Z d „  Z d d „ Z	 RS(	   i    t   md5t   12345678i   c
         C   sÛ   t  t | d  ƒ }
 |
 d  k r0 t d | f ‚ n  |
 ƒ  |  _ | r™ t  t | d  ƒ } | d  k rr t d | f ‚ n  t j |  j | ƒ  | | | | ƒ } n  t j ƒ  |  _ t j |  j |  j | | | ƒ |  j	 |	 ƒ ~ d  S(   Ns   unknown ciphers   unknown message digest(
   R   R   R   R   t   ciphert   bytes_to_keyt   cipher_ctx_newR   t   cipher_initt   set_padding(   R   t   algR'   t   ivt   opt   key_as_bytest   dR
   t   it   paddingR1   t   kmd(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   i   s    'c         C   s)   t  |  d d  ƒ r% |  j |  j ƒ n  d  S(   NR   (   R   R   t   m2_cipher_ctx_freeR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   x   s    c         C   s   t  j |  j | ƒ S(   N(   R   t   cipher_updateR   (   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   |   s    c         C   s   t  j |  j ƒ S(   N(   R   t   cipher_finalR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR      s    c         C   s   t  j |  j | ƒ S(   N(   R   t   cipher_set_paddingR   (   R   R<   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR5   ‚   s    (
   R   R   R   t   cipher_ctx_freeR>   R   R   R   R   R5   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR.   e   s   				t   PKeyc           B   s  e  Z d  Z e j Z e j Z d d d d „ Z	 d „  Z
 d „  Z d „  Z d d „ Z d „  Z d	 „  Z d
 „  Z e Z e Z d „  Z d „  Z d „  Z d d „ Z d „  Z d e j d „ Z d e j d „ Z d e j d „ Z d „  Z d „  Z d „  Z RS(   s   
    Public Key
    i    R$   c         C   sJ   | d  k	 r! | |  _ | |  _ n t j ƒ  |  _ d |  _ |  j | ƒ d  S(   Ni   (   R   t   pkeyt   _pyfreeR   t   pkey_newt   _set_context(   R   RD   RE   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   Ž   s    		c         C   sN   t  |  d d ƒ r% |  j |  j ƒ n  t  |  d d  ƒ rJ |  j |  j ƒ n  d  S(   NRE   i    R   (   R   t   m2_pkey_freeRD   R   R   R   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR   —   s    c         C   s   |  j  S(   N(   RD   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   _ptr   s    c         C   sO   t  t | d  ƒ } | d  k r0 t d | f ‚ n  | ƒ  |  _ t j ƒ  |  _ d  S(   Ns   unknown message digest(   R   R   R   R   R   R   R   (   R   R   t   mda(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyRG       s
    c         C   s   |  j  | ƒ d S(   sƒ   
        Reset internal message digest context.

        @type md: string
        @param md: The message digest algorithm.
        N(   RG   (   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   reset_context§   s    c         C   s   t  j |  j |  j ƒ d S(   s9   
        Initialise signing operation with self.
        N(   R   t	   sign_initR   R   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyRL   °   s    c         C   s   t  j |  j | ƒ d S(   su   
        Feed data to signing operation.

        @type data: string
        @param data: Data to be signed.
        N(   R   t   sign_updateR   (   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyRM   ¶   s    c         C   s   t  j |  j |  j ƒ S(   s[   
        Return signature.

        @rtype: string
        @return: The signature.
        (   R   t
   sign_finalR   RD   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyRN   ¿   s    c         C   s   t  j |  j |  j ƒ d S(   sH   
        Initialise signature verification operation with self.
        N(   R   t   verify_initR   R   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyRO   Ì   s    c         C   s   t  j |  j | ƒ S(   sÄ   
        Feed data to verification operation.

        @type data: string
        @param data: Data to be verified.
        @return: -1 on Python error, 1 for success, 0 for OpenSSL error
        (   R   t   verify_updateR   (   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyRP   Ò   s    c         C   s   t  j |  j | |  j ƒ S(   sç   
        Return result of verification.

        @param sign: Signature to use for verification
        @rtype: int
        @return: Result of verification: 1 for success, 0 for failure, -1 on
                 other error.
        (   R   t   verify_finalR   RD   (   R   t   sign(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyRQ   Ü   s    	i   c         C   sO   | r3 t  j |  j | j ƒ } | rK d | _ qK n t  j |  j | j ƒ } | S(   sò  
        Assign the RSA key pair to self.

        @type rsa: M2Crypto.RSA.RSA
        @param rsa: M2Crypto.RSA.RSA object to be assigned to self.

        @type capture:  boolean
        @param capture: If true (default), this PKey object will own the RSA
                        object, meaning that once the PKey object gets
                        deleted it is no longer safe to use the RSA object.
        
        @rtype: int
        @return: Return 1 for success and 0 for failure.
        i    (   R   t   pkey_assign_rsaRD   t   rsaRE   t   pkey_set1_rsa(   R   RT   t   capturet   ret(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt
   assign_rsaç   s    c         C   sC   t  j |  j ƒ } | d k r- t d ƒ ‚ n  t j | d ƒ } | S(   sd   
        Return the underlying RSA key if that is what the EVP
        instance is holding.
        s&   PKey instance is not holding a RSA keyi   N(   R   t   pkey_get1_rsaRD   R   R   R   t   RSA_pub(   R   t   rsa_ptrRT   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   get_rsaþ   s
    t   aes_128_cbcc         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(   R   t   openfilet   save_key_bio(   R   t   fileR1   t   callbackt   bio(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   save_key
  s    c         C   s|   | d k r( t j |  j | j ƒ  | ƒ St t | d ƒ } | d k rV t d | ‚ n  t j |  j | j ƒ  | ƒ  | ƒ Sd S(   sN  
        Save the key pair to the M2Crypto.BIO object 'bio' in PEM format.

        @type bio: M2Crypto.BIO
        @param bio: M2Crypto.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   no such cipher %sN(   R   R   t   pkey_write_pem_no_cipherRD   RI   R   R   t   pkey_write_pem(   R   Rc   R1   Rb   t   proto(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyR`     s    c         C   s)   t  j ƒ  } |  j | | | ƒ | j ƒ  S(   sÙ  
        Return key in PEM format in a string.

        @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.
        (   R   t   MemoryBufferR`   t   read_all(   R   R1   Rb   Rc   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   as_pem7  s    c         C   s+   t  j |  j ƒ } t j | ƒ } | j ƒ  S(   s6   
        Return key in DER format in a string
        (   R   t   pkey_as_derRD   R   Rh   Ri   (   R   t   bufRc   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   as_derI  s    c         C   s   t  j |  j ƒ S(   s6   
        Return the size of the key in bytes.
        (   R   t	   pkey_sizeRD   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   sizeQ  s    c         C   s   t  j |  j ƒ S(   s3   
        Return the modulus in hex format.
        (   R   t   pkey_get_modulusRD   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   get_modulusW  s    N(    R   R   R    R   t	   pkey_freeRH   R!   R   R   R   R   RI   RG   RK   RL   RM   RN   R   R   RO   RP   RQ   RX   R\   R   t   passphrase_callbackRd   R`   Rj   Rm   Ro   Rq   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyRC   †   s0   											
		
				c         C   sƒ   t  j |  d ƒ } | d k r6 t j t j ƒ  ƒ ‚ n  t  j | | ƒ } t  j | ƒ | d k rv t	 t j ƒ  ƒ ‚ n  t
 | d ƒ S(   sj  
    Load an M2Crypto.EVP.PKey from file.

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

    @type callback: Python callable
    @param callback: A Python callable object that is invoked
    to acquire a passphrase with which to protect the key.

    @rtype: M2Crypto.EVP.PKey
    @return: M2Crypto.EVP.PKey object.
    t   ri   N(   R   t   bio_new_fileR   R   t   BIOErrorR    t	   get_errort   pkey_read_pemt   bio_freeR   RC   (   Ra   Rb   Rc   t   cptr(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   load_key^  s    c         C   sF   t  j |  j ƒ  | ƒ } | d k r9 t t j ƒ  ƒ ‚ n  t | d ƒ S(   s‡  
    Load an M2Crypto.EVP.PKey from an M2Crypto.BIO object.

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

    @type callback: Python callable
    @param callback: A Python callable object that is invoked
    to acquire a passphrase with which to protect the key.

    @rtype: M2Crypto.EVP.PKey
    @return: M2Crypto.EVP.PKey object.
    i   N(   R   Rx   RI   R   R   R    Rw   RC   (   Rc   Rb   Rz   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   load_key_biou  s    c         C   s   t  j |  ƒ } t | | ƒ S(   sl  
    Load an M2Crypto.EVP.PKey from a string.

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

    @type callback: Python callable
    @param callback: A Python callable object that is invoked
    to acquire a passphrase with which to protect the key.

    @rtype: M2Crypto.EVP.PKey
    @return: M2Crypto.EVP.PKey object.
    (   R   Rh   R|   (   t   stringRb   Rc   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   load_key_stringˆ  s    (    (    (    (    (   R    t   M2CryptoR    R   R   R   R   t	   ExceptionR   t   evp_initR   R   R#   R-   R.   RC   Rs   R{   R|   R~   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/EVP.pyt   <module>   s   "	!!Ø