o
    tBhf                     @   s   d Z ddlm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	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G dd dejZG dd dejejZdS )zIECDSA (ES256) verifier and signer that use the ``cryptography`` library.
    )utilsN)backends)hashes)serialization)ec)padding)decode_dss_signature)encode_dss_signature)_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 )	ES256VerifierzVerifies ECDSA cryptographic signatures using public keys.

    Args:
        public_key (
                cryptography.hazmat.primitives.asymmetric.ec.ECDSAPublicKey):
            The public key used to verify signatures.
    c                 C   s
   || _ d S N)_pubkey)self
public_key r   n/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/google/auth/crypt/es256.py__init__/   s   
zES256Verifier.__init__c              	   C   s   t |}t|dkrdS tj|d d dd}tj|dd  dd}t||}t |}z| j||t	t
  W dS  ttjjfyK   Y dS w )N@   F    big)	byteorderT)r
   to_byteslenr   int_from_bytesr	   r   verifyr   ECDSAr   SHA256
ValueErrorcryptography
exceptionsInvalidSignature)r   message	signature	sig_bytesrsasn1_sigr   r   r   r   2   s   


zES256Verifier.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_stringC   s   
zES256Verifier.from_stringN)__name__
__module____qualname____doc__r   r
   copy_docstringr   Verifierr   classmethodr1   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 )ES256Signera  Signs messages with an ECDSA private key.

    Args:
        private_key (
                cryptography.hazmat.primitives.asymmetric.ec.ECDSAPrivateKey):
            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   l   s   
zES256Signer.__init__c                 C   s   | j S r   )r;   )r   r   r   r   r=   p   s   zES256Signer.key_idc                 C   sF   t |}| j|tt }t|\}}t	
|dt	
|d S )Nr   )r
   r   r:   signr   r   r   r   r   r   int_to_bytes)r   r"   asn1_signaturer%   r&   r   r   r   r>   u   s   
zES256Signer.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   r1   ~   s
   
zES256Signer.from_stringr   )r2   r3   r4   r5   r   propertyr
   r6   r   Signerr=   r>   r8   r1   r   r   r   r   r9   `   s    



r9   )r5   r   r   cryptography.exceptionscryptography.hazmatr   cryptography.hazmat.primitivesr   r   )cryptography.hazmat.primitives.asymmetricr   r   /cryptography.hazmat.primitives.asymmetric.utilsr   r	   cryptography.x509google.authr
   google.auth.cryptr   r(   default_backendr+   PKCS1v15_PADDINGr7   r   rF   FromServiceAccountMixinr9   r   r   r   r   <module>   s$   :