U
    6d                     @   s   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mZ ddlmZ d	Ze Ze	 Ze ZG d
d dejZG dd dejejZdS )zRSA 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.
    N)backends)hashes)serialization)padding)_helpers)bases   -----BEGIN CERTIFICATE-----c                   @   s8   e Zd ZdZdd Zeejdd Z	e
dd ZdS )	RSAVerifierzVerifies 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)_pubkey)self
public_key r   G/tmp/pip-unpacked-wheel-h3lf9jv9/google/auth/crypt/_cryptography_rsa.py__init__/   s    zRSAVerifier.__init__c              	   C   sF   t |}z| j||tt W dS  ttjj	fk
r@   Y dS X d S )NTF)
r   to_bytesr
   verify_PADDING_SHA256
ValueErrorcryptography
exceptionsZInvalidSignature)r   message	signaturer   r   r   r   2   s    
zRSAVerifier.verifyc                 C   s>   t |}t|kr*tj|t}| }nt	|t}| |S )ay  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   _CERTIFICATE_MARKERr   x509Zload_pem_x509_certificate_BACKENDr   r   Zload_pem_public_key)clsr   Zpublic_key_datacertZpubkeyr   r   r   from_string;   s    
 
zRSAVerifier.from_stringN)__name__
__module____qualname____doc__r   r   copy_docstringr   Verifierr   classmethodr   r   r   r   r   r   &   s   

r   c                   @   sT   e Zd ZdZdddZeeej	dd Z
eej	dd Zedd	d
ZdS )	RSASignera  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.
    Nc                 C   s   || _ || _d S r	   )_key_key_id)r   private_keykey_idr   r   r   r   d   s    zRSASigner.__init__c                 C   s   | j S r	   )r(   )r   r   r   r   r*   h   s    zRSASigner.key_idc                 C   s   t |}| j|ttS r	   )r   r   r'   signr   r   )r   r   r   r   r   r+   m   s    
zRSASigner.signc                 C   s&   t |}tj|dtd}| ||dS )al  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."
        N)passwordbackend)r*   )r   r   r   Zload_pem_private_keyr   )r   keyr*   r)   r   r   r   r   r   s    
  zRSASigner.from_string)N)N)r   r    r!   r"   r   propertyr   r#   r   Signerr*   r+   r%   r   r   r   r   r   r&   X   s   



r&   )r"   Zcryptography.exceptionsr   Zcryptography.hazmatr   Zcryptography.hazmat.primitivesr   r   Z)cryptography.hazmat.primitives.asymmetricr   Zcryptography.x509Zgoogle.authr   Zgoogle.auth.cryptr   r   Zdefault_backendr   ZPKCS1v15r   SHA256r   r$   r   r0   ZFromServiceAccountMixinr&   r   r   r   r   <module>   s   2