o
    Mf&                     @   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   e/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/google/auth/crypt/_cryptography_rsa.py__init__/   s   
zRSAVerifier.__init__c              	   C   s@   t |}z| j||tt W dS  ttjj	fy   Y dS w )NTF)
r   to_bytesr
   verify_PADDING_SHA256
ValueErrorcryptography
exceptionsInvalidSignature)r   message	signaturer   r   r   r   2   s   
zRSAVerifier.verifyc                 C   sD   t |}t|v rtj|t}| }| |S 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   x509load_pem_x509_certificate_BACKENDr   r   load_pem_public_key)clsr   public_key_datacert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                   @   sd   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d Z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   r0   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   load_pem_private_keyr   )r   keyr/   r.   r   r   r   r#   r   s
   
zRSASigner.from_stringc                 C   s0   | j  }| jjtjjtjjt	 d|d< |S )z1Pickle helper that serializes the _key attribute.)encodingformatencryption_algorithmr,   )
__dict__copyr,   private_bytesr   EncodingPEMPrivateFormatPKCS8NoEncryptionr   stater   r   r   __getstate__   s   

zRSASigner.__getstate__c                 C   s$   t |d d|d< | j| dS )z3Pickle helper that deserializes the _key attribute.r,   N)r   r3   r8   updater@   r   r   r   __setstate__   s   zRSASigner.__setstate__r	   )r$   r%   r&   r'   r   propertyr   r(   r   Signerr/   r0   r*   r#   rB   rD   r   r   r   r   r+   X   s    




r+   )r'   cryptography.exceptionsr   cryptography.hazmatr   cryptography.hazmat.primitivesr   r   )cryptography.hazmat.primitives.asymmetricr   cryptography.x509google.authr   google.auth.cryptr   r   default_backendr   PKCS1v15r   SHA256r   r)   r   rF   FromServiceAccountMixinr+   r   r   r   r   <module>   s   2