ó
ñ1Mc           @   sø   d  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 ƒ d d d „  ƒ  YZ d e f d „  ƒ  YZ	 e j
 d	 „ Z d
 „  Z e j d „ Z e j d „ Z e j d „ Z e j d „ Z e j d „ Z e j d „ Z d S(   sç   
    M2Crypto wrapper for OpenSSL DSA API.

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

    Portions created by Open Source Applications Foundation (OSAF) are
    Copyright (C) 2004 OSAF. All Rights Reserved.
iÿÿÿÿNt   DSAErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyR       s    t   DSAc           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 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sA  
    This class is a context supporting DSA key and parameter
    values, signing and verifying.
    
    Simple example::
    
        from M2Crypto import EVP, DSA, util
        
        message = 'Kilroy was here!'
        md = EVP.MessageDigest('sha1')
        md.update(message)        
        digest = md.final()
        
        dsa = DSA.gen_params(1024)
        dsa.gen_key()
        r, s = dsa.sign(digest)
        good = dsa.verify(digest, r, s)
        if good:
            print '  ** success **'
        else:
            print '  ** verification failed **'
    i    c         C   s   | |  _  | |  _ d S(   sI   
        Use one of the factory functions to create an instance.
        N(   t   dsat   _pyfree(   t   selfR   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   __init__,   s    	c         C   s)   t  |  d d ƒ r% |  j |  j ƒ n  d  S(   NR   i    (   t   getattrt   m2_dsa_freeR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   __del__4   s    c         C   s   t  j |  j ƒ S(   sp   
        Return the key length.
    
        @rtype:   int
        @return:  the DSA key length in bits
        (   t   m2t
   dsa_keylenR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   __len__8   s    c         C   s9   | d k r/ t  t d | f ƒ } | |  j ƒ St ‚ d S(	   s5  
        Return specified DSA parameters and key values.
    
        @type  name: str
        @param name: name of variable to be returned.  Must be 
                     one of 'p', 'q', 'g', 'pub', 'priv'.
        @rtype:      str
        @return:     value of specified variable (a "byte string")
        t   pt   qt   gt   pubt   privs
   dsa_get_%sN(   R   R   R   R   R   (   R   R   R   t   AttributeError(   R   t   namet   method(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   __getattr__B   s    
c         C   sG   | d k r t  d ƒ ‚ n( | d	 k r6 t  d ƒ ‚ n | |  j | <d  S(
   NR   R   R   s   set (p, q, g) via set_params()R   R   s"   generate (pub, priv) via gen_key()(   R   R   R   (   s   pubs   priv(   R    t   __dict__(   R   R   t   value(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   __setattr__S   s
    c         C   s=   t  j |  j | ƒ t  j |  j | ƒ t  j |  j | ƒ d S(   sö   
        Set new parameters.
        
        @warning: This does not change the private key, so it may be
                  unsafe to use this method. It is better to use
                  gen_params function to create a new DSA object.
        N(   R   t	   dsa_set_pR   t	   dsa_set_qt	   dsa_set_g(   R   R   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt
   set_params[   s    c         C   s   t  j |  j ƒ d S(   s&   
        Generate a key pair.
        N(   R   t   dsa_gen_keyR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   gen_keyg   s    c         C   s;   t  j | d ƒ } t j |  j | j ƒ  ƒ } | j ƒ  | S(   sÅ   
        Save the DSA parameters to a file.
    
        @type  filename: str
        @param filename: Save the DSA parameters to this file.
        @return:         1 (true) if successful
        t   wb(   t   BIOt   openfileR   t   dsa_write_params_bioR   t   _ptrt   close(   R   t   filenamet   biot   ret(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   save_paramsn   s    
c         C   s   t  j |  j | j ƒ  ƒ S(   sÆ   
        Save DSA parameters to a BIO object.
    
        @type  bio: M2Crypto.BIO object
        @param bio: Save DSA parameters to this object.
        @return:    1 (true) if successful
        (   R   R#   R   R$   (   R   R'   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   save_params_bio{   s    t   aes_128_cbcc         C   s5   t  j | d ƒ } |  j | | | ƒ } | j ƒ  | S(   sU  
        Save the DSA key pair to a file.
    
        @type  filename: str
        @param filename: Save the DSA key pair to this file.
        @type  cipher:   str
        @param cipher:   name of symmetric key algorithm and mode
                         to encrypt the private key.
        @return:         1 (true) if successful
        R    (   R!   R"   t   save_key_bioR%   (   R   R&   t   ciphert   callbackR'   R(   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   save_key…   s    
c         C   s…   | d k r( t j |  j | j ƒ  | ƒ St t | d ƒ } | d k rY t d | ƒ ‚ n	 | ƒ  } t j |  j | j ƒ  | | ƒ Sd S(   s[  
        Save DSA key pair to a BIO object.
    
        @type  bio:    M2Crypto.BIO object
        @param bio:    Save DSA parameters to this object.
        @type  cipher: str
        @param cipher: name of symmetric key algorithm and mode
                       to encrypt the private key.
        @return:       1 (true) if successful
        s   no such cipher: %sN(   t   NoneR   t   dsa_write_key_bio_no_cipherR   R$   R   R    t   dsa_write_key_bio(   R   R'   R-   R.   t   ciph(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyR,   –   s    	c         C   s/   t  j | d ƒ } |  j | ƒ } | j ƒ  | S(   sÿ   
        Save the DSA public key (with parameters) to a file.
    
        @type  filename: str
        @param filename: Save DSA public key (with parameters) 
                         to this file.
        @return:         1 (true) if successful
        R    (   R!   R"   t   save_pub_key_bioR%   (   R   R&   R'   R(   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   save_pub_key­   s    	
c         C   s   t  j |  j | j ƒ  ƒ S(   sý   
        Save DSA public key (with parameters) to a BIO object.
    
        @type  bio: M2Crypto.BIO object
        @param bio: Save DSA public key (with parameters) 
                    to this object.
        @return:  1 (true) if successful
        (   R   t   dsa_write_pub_key_bioR   R$   (   R   R'   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyR4   »   s    	c         C   s   t  j |  j | ƒ S(   sK  
        Sign the digest.
    
        @type  digest: str
        @param digest: SHA-1 hash of message (same as output 
                       from MessageDigest, a "byte string")
        @rtype:        tuple
        @return:       DSA signature, a tuple of two values, r and s,
                       both "byte strings".
        (   R   t   dsa_signR   (   R   t   digest(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   signÆ   s    c         C   s   t  j |  j | | | ƒ S(   s  
        Verify a newly calculated digest against the signature 
        values r and s.
    
        @type  digest: str
        @param digest: SHA-1 hash of message (same as output 
                       from MessageDigest, a "byte string")
        @type  r:      str
        @param r:      r value of the signature, a "byte string"
        @type  s:      str
        @param s:      s value of the signature, a "byte string"
        @rtype:        int
        @return:       1 (true) if verify succeeded, 0 if failed
        (   R   t
   dsa_verifyR   (   R   R8   t   rt   s(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   verifyÔ   s    c         C   s   t  j |  j | ƒ S(   N(   R   t   dsa_sign_asn1R   (   R   R8   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt	   sign_asn1æ   s    c         C   s   t  j |  j | | ƒ S(   N(   R   t   dsa_verify_asn1R   (   R   R8   t   blob(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   verify_asn1ê   s    c         C   s   t  j |  j ƒ S(   s—   
        Check to be sure the DSA object has a valid private key.
    
        @rtype:   int
        @return:  1 (true) if a valid private key
        (   R   t   dsa_check_keyR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt	   check_keyî   s    (   R   R   t   __doc__R   t   dsa_freeR	   R   R
   R   R   R   R   R   R)   R*   t   utilt   passphrase_callbackR/   R,   R5   R4   R9   R=   R?   RB   RD   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyR      s,   			
						
						t   DSA_pubc           B   s8   e  Z d  Z d „  Z e Z d „  Z e j Z e j	 Z
 RS(   s•   
    This class is a DSA context that only supports a public key 
    and verification.  It does NOT support a private key or 
    signing.
    
    c         G   s   t  d ƒ ‚ d  S(   Ns!   DSA_pub object has no private key(   R    (   R   t   argv(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyR9     s    c         C   s   t  j |  j ƒ S(   N(   R   t   dsa_check_pub_keyR   (   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyRD     s    (   R   R   RE   R9   R?   RD   R   R5   R/   R4   R,   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyRI   ú   s   			c         C   s:   t  j |  | ƒ } | d k r- t d ƒ ‚ n  t | d ƒ S(   sô  
    Factory function that generates DSA parameters and 
    instantiates a DSA object from the output.

    @type  bits: int
    @param bits: The length of the prime to be generated. If 
                 'bits' < 512, it is set to 512.
    @type  callback: function
    @param callback: A Python callback object that will be 
                 invoked during parameter generation; it usual 
                 purpose is to provide visual feedback.
    @rtype:   DSA
    @return:  instance of DSA.
    s!   problem generating DSA parametersi   N(   R   t   dsa_generate_parametersR0   R    R   (   t   bitsR.   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt
   gen_params  s    c         C   sI   t  j ƒ  } t  j | |  ƒ t  j | | ƒ t  j | | ƒ t | d ƒ S(   s8  
    Factory function that instantiates a DSA object with DSA
    parameters.

    @type  p: str
    @param p: value of p, a "byte string"
    @type  q: str
    @param q: value of q, a "byte string"
    @type  g: str
    @param g: value of g, a "byte string"
    @rtype:   DSA
    @return:  instance of DSA.
    i   (   R   t   dsa_newR   R   R   R   (   R   R   R   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyR   &  s
    c         C   s,   t  j |  ƒ } t | | ƒ } | j ƒ  | S(   s  
    Factory function that instantiates a DSA object with DSA 
    parameters from a file.

    @type  file:     str
    @param file:     Names the file (a path) that contains the PEM 
                     representation of the DSA parameters. 
    @type  callback: A Python callable
    @param callback: A Python callback object that will be 
                     invoked if the DSA parameters file is 
                     passphrase-protected.
    @rtype:          DSA
    @return:         instance of DSA.
    (   R!   R"   t   load_params_bioR%   (   t   fileR.   R'   R(   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   load_params:  s    
c         C   s@   t  j |  j ƒ  | ƒ } | d k r3 t d ƒ ‚ n  t | d ƒ S(   s  
    Factory function that instantiates a DSA object with DSA
    parameters from a M2Crypto.BIO object.

    @type  bio:      M2Crypto.BIO object
    @param bio:      Contains the PEM representation of the DSA 
                     parameters. 
    @type  callback: A Python callable
    @param callback: A Python callback object that will be 
                     invoked if the DSA parameters file is 
                     passphrase-protected.
    @rtype:          DSA
    @return:         instance of DSA.
    s   problem loading DSA parametersi   N(   R   t   dsa_read_paramsR$   R0   R    R   (   R'   R.   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyRP   O  s    c         C   s,   t  j |  ƒ } t | | ƒ } | j ƒ  | S(   sø  
    Factory function that instantiates a DSA object from a
    PEM encoded DSA key pair.

    @type  file:     str
    @param file:     Names the file (a path) that contains the PEM 
                     representation of the DSA key pair. 
    @type  callback: A Python callable
    @param callback: A Python callback object that will be 
                     invoked if the DSA key pair is 
                     passphrase-protected.
    @rtype:          DSA
    @return:         instance of DSA.
    (   R!   R"   t   load_key_bioR%   (   RQ   R.   R'   R(   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   load_keyd  s    
c         C   s:   t  j |  j ƒ  | ƒ } | s- t d ƒ ‚ n  t | d ƒ S(   së  
    Factory function that instantiates a DSA object from a
    PEM encoded DSA key pair.

    @type  bio:      M2Crypto.BIO object
    @param bio:      Contains the PEM representation of the DSA 
                     key pair. 
    @type  callback: A Python callable
    @param callback: A Python callback object that will be 
                     invoked if the DSA key pair is 
                     passphrase-protected.
    @rtype:          DSA
    @return:         instance of DSA.
    s   problem loading DSA key pairi   (   R   t   dsa_read_keyR$   R    R   (   R'   R.   R   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyRT   y  s    c         C   s,   t  j |  ƒ } t | | ƒ } | j ƒ  | S(   sg  
    Factory function that instantiates a DSA_pub object using
    a DSA public key contained in PEM file.  The PEM file 
    must contain the parameters in addition to the public key.

    @type  file:     str
    @param file:     Names the file (a path) that contains the PEM 
                     representation of the DSA public key. 
    @type  callback: A Python callable
    @param callback: A Python callback object that will be 
                     invoked should the DSA public key be 
                     passphrase-protected.
    @rtype:          DSA_pub
    @return:         instance of DSA_pub.
    (   R!   R"   t   load_pub_key_bioR%   (   RQ   R.   R'   R(   (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   load_pub_keyŽ  s    
c         C   s:   t  j |  j ƒ  | ƒ } | s- t d ƒ ‚ n  t | d ƒ S(   se  
    Factory function that instantiates a DSA_pub object using
    a DSA public key contained in PEM format.  The PEM 
    must contain the parameters in addition to the public key.

    @type  bio:      M2Crypto.BIO object
    @param bio:      Contains the PEM representation of the DSA 
                     public key (with params). 
    @type  callback: A Python callable
    @param callback: A Python callback object that will be 
                     invoked should the DSA public key be 
                     passphrase-protected.
    @rtype:          DSA_pub
    @return:         instance of DSA_pub.
    s   problem loading DSA public keyi   (   R   t   dsa_read_pub_keyR$   R    RI   (   R'   R.   t   dsapub(    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyRW   ¤  s    (    (   RE   t   sysRG   R!   R   t	   ExceptionR    t   dsa_initR   RI   t   genparam_callbackRN   R   RH   RR   RP   RU   RT   RX   RW   (    (    (    s2   /usr/lib64/python2.7/site-packages/M2Crypto/DSA.pyt   <module>   s   $é	