o
    jDiB                     @   s   d Z ddlmZ ddlmZmZmZ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eG dd dZG dd dej Z!G dd dej"ej#Z$dS )zAECDSA verifier and signer that use the ``cryptography`` library.
    )	dataclass)AnyDictOptionalUnionN)backends)hashes)serialization)ec)padding)decode_dss_signature)encode_dss_signature)_helpers)bases   -----BEGIN CERTIFICATE-----c                   @   s^   e Zd ZU dZeed< ejed< eed< e	de
ejejf fddZe	dejfd	d
ZdS )_ESAttributeszA class that models ECDSA attributes.

    Attributes:
        rs_size (int): Size for ASN.1 r and s size.
        sha_algo (hashes.HashAlgorithm): Hash algorithm.
        algorithm (str): Algorithm name.
    rs_sizesha_algo	algorithmkeyc                 C   s   |  |jS N)
from_curvecurve)clsr    r   T/home/air/goalskill_t/back/venv/lib/python3.10/site-packages/google/auth/crypt/es.pyfrom_key6   s   z_ESAttributes.from_keyr   c                 C   s,   t |tjr| dt dS | dt dS )N0   ES384    ES256)
isinstancer
   	SECP384R1r   SHA384SHA256)r   r   r   r   r   r   <   s   z_ESAttributes.from_curveN)__name__
__module____qualname____doc__int__annotations__r   HashAlgorithmstrclassmethodr   r
   EllipticCurvePublicKeyEllipticCurvePrivateKeyr   EllipticCurver   r   r   r   r   r   (   s   
 
r   c                   @   sd   e Zd ZdZdejddfddZee	j
dededefd	d
Zedeeef dd fddZdS )
EsVerifierzVerifies ECDSA cryptographic signatures using public keys.

    Args:
        public_key (
                cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey):
            The public key used to verify signatures.
    
public_keyreturnNc                 C   s   || _ t|| _d S r   )_pubkeyr   r   _attributes)selfr1   r   r   r   __init__R   s   zEsVerifier.__init__message	signaturec              	   C   s   t |}t|| jjd krdS tj|d | jj dd}tj|| jjd  dd}t||}t |}z| j	||t
| jj W dS  ttjjfyS   Y dS w )N   Fbig	byteorderT)r   to_byteslenr4   r   r(   
from_bytesr   r3   verifyr
   ECDSAr   
ValueErrorcryptography
exceptionsInvalidSignature)r5   r7   r8   	sig_bytesrsasn1_sigr   r   r   r@   V   s   


zEsVerifier.verifyc                 C   sR   t |}t|v rtj|t}| }nt	|t}t
|tjs%td| |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.
        z2Expected public key of type EllipticCurvePublicKey)r   r=   _CERTIFICATE_MARKERrC   x509load_pem_x509_certificate_BACKENDr1   r	   load_pem_public_keyr    r
   r-   	TypeError)r   r1   public_key_datacertpubkeyr   r   r   from_stringg   s   

zEsVerifier.from_string)r$   r%   r&   r'   r
   r-   r6   r   copy_docstringr   Verifierbytesboolr@   r,   r   r+   rS   r   r   r   r   r0   I   s    
 r0   c                   @   s   e Zd ZdZ	ddejdee ddfddZe	defdd	Z
e	eejdee fd
dZeejdedefddZe	ddeeef dee dd fddZdeeef fddZdeeef ddfddZdS )EsSignera  Signs messages with an ECDSA private key.

    Args:
        private_key (
                cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey):
            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.
    Nprivate_keykey_idr2   c                 C   s   || _ || _t|| _d S r   )_key_key_idr   r   r4   )r5   rY   rZ   r   r   r   r6      s   zEsSigner.__init__c                 C   s   | j jS )zkName of the algorithm used to sign messages.
        Returns:
            str: The algorithm name.
        )r4   r   r5   r   r   r   r      s   zEsSigner.algorithmc                 C   s   | j S r   )r\   r]   r   r   r   rZ      s   zEsSigner.key_idr7   c                 C   sR   t |}| j|t| jj}t|\}}|j| jj	dd|j| jj	dd S )Nr:   r;   )
r   r=   r[   signr
   rA   r4   r   r   r   )r5   r7   asn1_signaturerG   rH   r   r   r   r^      s   
zEsSigner.signr   c                 C   s:   t |}tj|dtd}t|tjs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backendz4Expected private key of type EllipticCurvePrivateKey)rZ   )	r   r=   r	   load_pem_private_keyrM   r    r
   r.   rO   )r   r   rZ   	key_bytesrY   r   r   r   rS      s   
zEsSigner.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r5   stater   r   r   __getstate__   s   

zEsSigner.__getstate__rp   c                 C   s$   t |d d|d< | j| dS )z3Pickle helper that deserializes the _key attribute.r[   N)r	   rb   rg   updatero   r   r   r   __setstate__   s   zEsSigner.__setstate__r   )r$   r%   r&   r'   r
   r.   r   r+   r6   propertyr   r   rT   r   SignerrZ   rV   r^   r,   r   rS   r   r   rq   rs   r   r   r   r   rX      s8    





rX   )%r'   dataclassesr   typingr   r   r   r   cryptography.exceptionsrC   cryptography.hazmatr   cryptography.hazmat.primitivesr   r	   )cryptography.hazmat.primitives.asymmetricr
   r   /cryptography.hazmat.primitives.asymmetric.utilsr   r   cryptography.x509google.authr   google.auth.cryptr   rJ   default_backendrM   PKCS1v15_PADDINGr   rU   r0   ru   FromServiceAccountMixinrX   r   r   r   r   <module>   s*    >