ó
Ÿ^Yc           @   s_   d  Z  d d l m Z d d l m Z d e f d     YZ d e f d     YZ d   Z d	 S(
   s1   OpenSSL Crypto-related routines for oauth2client.iÿÿÿÿ(   t   crypto(   t   _helperst   OpenSSLVerifierc           B   s/   e  Z d  Z d   Z d   Z e d    Z RS(   s$   Verifies the signature on a message.c         C   s   | |  _  d S(   sl   Constructor.

        Args:
            pubkey: OpenSSL.crypto.PKey, The public key to verify with.
        N(   t   _pubkey(   t   selft   pubkey(    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyt   __init__   s    c         C   sg   t  j | d d } t  j | d d } y! t j |  j | | d  t SWn t j k
 rb t SXd S(   sØ  Verifies a message against a signature.

        Args:
        message: string or bytes, The message to verify. If string, will be
                 encoded to bytes as utf-8.
        signature: string or bytes, The signature on the message. If string,
                   will be encoded to bytes as utf-8.

        Returns:
            True if message was signed by the private key associated with the
            public key that this object was constructed with.
        t   encodings   utf-8t   sha256N(   R   t	   _to_bytesR    t   verifyR   t   Truet   Errort   False(   R   t   messaget	   signature(    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyR
       s    c         C   sL   t  j |   }  | r- t j t j |   } n t j t j |   } t |  S(   s  Construct a Verified instance from a string.

        Args:
            key_pem: string, public key in PEM format.
            is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it
                          is expected to be an RSA key in PEM format.

        Returns:
            Verifier instance.

        Raises:
            OpenSSL.crypto.Error: if the key_pem can't be parsed.
        (   R   R	   R    t   load_certificatet   FILETYPE_PEMt   load_privatekeyR   (   t   key_pemt   is_x509_certR   (    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyt   from_string5   s
    (   t   __name__t
   __module__t   __doc__R   R
   t   staticmethodR   (    (    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyR      s   		t   OpenSSLSignerc           B   s2   e  Z d  Z d   Z d   Z e d d   Z RS(   s"   Signs messages with a private key.c         C   s   | |  _  d S(   st   Constructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        N(   t   _key(   R   t   pkey(    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyR   O   s    c         C   s+   t  j | d d } t j |  j | d  S(   s°   Signs a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        R   s   utf-8R   (   R   R	   R    t   signR   (   R   R   (    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyR   W   s    	t
   notasecretc         C   ss   t  j |   }  t  j |   } | r< t j t j |  } n- t  j | d d } t j |  |  j   } t |  S(   s>  Construct a Signer instance from a string.

        Args:
            key: string, private key in PKCS12 or PEM format.
            password: string, password for the private key file.

        Returns:
            Signer instance.

        Raises:
            OpenSSL.crypto.Error if the key can't be parsed.
        R   s   utf-8(	   R   R	   t   _parse_pem_keyR    R   R   t   load_pkcs12t   get_privatekeyR   (   t   keyt   passwordt   parsed_pem_keyR   (    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyR   c   s    (   R   R   R   R   R   R   R   (    (    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyR   L   s
   		c         C   s:   t  j |  } t j |  |  } t j t j | j    S(   s  Convert the contents of a PKCS#12 key to PEM using pyOpenSSL.

    Args:
        private_key_bytes: Bytes. PKCS#12 key in DER format.
        private_key_password: String. Password for PKCS#12 key.

    Returns:
        String. PEM contents of ``private_key_bytes``.
    (   R   R	   R    R    t   dump_privatekeyR   R!   (   t   private_key_bytest   private_key_passwordt   pkcs12(    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyt   pkcs12_key_as_pem{   s    
N(	   R   t   OpenSSLR    t   oauth2clientR   t   objectR   R   R)   (    (    (    sA   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/_openssl_crypt.pyt   <module>   s
   7/