ó
ùR‹_c           @   sS  d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 d d l
 Z d d l Z d d l m Z d d l m Z d	 Z y: e j d
 ƒ j Z e e j d ƒ k  rÉ e e ƒ ‚ n  Wn  e j k
 rì e e ƒ ‚ n Xd Z e j ƒ  Z e	 j ƒ  Z e j ƒ  Z d e j f d „  ƒ  YZ d e j  e j! f d „  ƒ  YZ" d S(   sÐ   RSA verifier and signer that use the ``cryptography`` library.

This is a much faster implementation than the default (in
``google.auth.crypt._python_rsa``), which depends on the pure-Python
``rsa`` library.
iÿÿÿÿN(   t   backends(   t   hashes(   t   serialization(   t   padding(   t   _helpers(   t   basesM   cryptography>=1.4.0 is required to use cryptography-based RSA implementation.t   cryptographys   1.4.0s   -----BEGIN CERTIFICATE-----t   RSAVerifierc           B   sA   e  Z d  Z d „  Z e j e j ƒ d „  ƒ Z e	 d „  ƒ Z
 RS(   sà   Verifies RSA cryptographic signatures using public keys.

    Args:
        public_key (
                cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey):
            The public key used to verify signatures.
    c         C   s   | |  _  d  S(   N(   t   _pubkey(   t   selft
   public_key(    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyt   __init__<   s    c         C   sU   t  j | ƒ } y! |  j j | | t t ƒ t SWn t t j	 j
 f k
 rP t SXd  S(   N(   R   t   to_bytesR   t   verifyt   _PADDINGt   _SHA256t   Truet
   ValueErrorR   t
   exceptionst   InvalidSignaturet   False(   R	   t   messaget	   signature(    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyR   ?   s    c         C   s[   t  j | ƒ } t | k r? t j j | t ƒ } | j ƒ  } n t j	 | t ƒ } |  | ƒ S(   sy  Construct an Verifier instance from a public key or public
        certificate string.

        Args:
            public_key (Union[str, bytes]): The public key in PEM format or the
                x509 public key certificate.

        Returns:
            Verifier: The constructed verifier.

        Raises:
            ValueError: If the public key can't be parsed.
        (
   R   R   t   _CERTIFICATE_MARKERR   t   x509t   load_pem_x509_certificatet   _BACKENDR
   R   t   load_pem_public_key(   t   clsR
   t   public_key_datat   certt   pubkey(    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyt   from_stringH   s    	(   t   __name__t
   __module__t   __doc__R   R   t   copy_docstringR   t   VerifierR   t   classmethodR    (    (    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyR   3   s   		t	   RSASignerc           B   sh   e  Z d  Z d d „ Z e e j e j	 ƒ d „  ƒ ƒ Z
 e j e j	 ƒ d „  ƒ Z e d d „ ƒ Z RS(   s…  Signs messages with an RSA private key.

    Args:
        private_key (
                cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey):
            The private key to sign with.
        key_id (str): Optional key ID used to identify this private key. This
            can be useful to associate the private key with its associated
            public key or certificate.
    c         C   s   | |  _  | |  _ d  S(   N(   t   _keyt   _key_id(   R	   t   private_keyt   key_id(    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyR   q   s    	c         C   s   |  j  S(   N(   R)   (   R	   (    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyR+   u   s    c         C   s%   t  j | ƒ } |  j j | t t ƒ S(   N(   R   R   R(   t   signR   R   (   R	   R   (    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyR,   z   s    c         C   s:   t  j | ƒ } t j | d d d t ƒ} |  | d | ƒS(   sl  Construct a RSASigner from a private key in PEM format.

        Args:
            key (Union[bytes, str]): Private key in PEM format.
            key_id (str): An optional key id used to identify the private key.

        Returns:
            google.auth.crypt._cryptography_rsa.RSASigner: The
            constructed signer.

        Raises:
            ValueError: If ``key`` is not ``bytes`` or ``str`` (unicode).
            UnicodeDecodeError: If ``key`` is ``bytes`` but cannot be decoded
                into a UTF-8 ``str``.
            ValueError: If ``cryptography`` "Could not deserialize key data."
        t   passwordt   backendR+   N(   R   R   R   t   load_pem_private_keyt   NoneR   (   R   t   keyR+   R*   (    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyR       s    N(   R!   R"   R#   R0   R   t   propertyR   R$   R   t   SignerR+   R,   R&   R    (    (    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyR'   e   s   
(#   R#   t   cryptography.exceptionsR   t   cryptography.hazmatR    t   cryptography.hazmat.primitivesR   R   t)   cryptography.hazmat.primitives.asymmetricR   t   cryptography.x509t   pkg_resourcest   google.authR   t   google.auth.cryptR   t   _IMPORT_ERROR_MSGt   get_distributiont   parsed_versiont   releaset   parse_versiont   ImportErrort   DistributionNotFoundR   t   default_backendR   t   PKCS1v15R   t   SHA256R   R%   R   R3   t   FromServiceAccountMixinR'   (    (    (    s\   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/crypt/_cryptography_rsa.pyt   <module>   s,   2