a
    !f                     @   sH   d Z ddlmZ ddlmZ G dd deZG dd deZdd	 Zd
S )z1OpenSSL Crypto-related routines for oauth2client.    )crypto)_helpersc                   @   s,   e Zd ZdZdd Zdd Zedd ZdS )	OpenSSLVerifierz$Verifies the signature on a message.c                 C   s
   || _ dS )zlConstructor.

        Args:
            pubkey: OpenSSL.crypto.PKey, The public key to verify with.
        N)_pubkey)selfpubkey r   \/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/_openssl_crypt.py__init__   s    zOpenSSLVerifier.__init__c                 C   sP   t j|dd}t j|dd}zt| j||d W dS  tjyJ   Y dS 0 dS )a  Verifies a message against a signature.

        Args:
        message: string or bytes, The message to verify. If string, will be
                 encoded to bytes as utf-8.
        signature: string or bytes, The signature on the message. If string,
                   will be encoded to bytes as utf-8.

        Returns:
            True if message was signed by the private key associated with the
            public key that this object was constructed with.
        utf-8encodingsha256TFN)r   	_to_bytesr   verifyr   Error)r   message	signaturer   r   r	   r       s    zOpenSSLVerifier.verifyc                 C   s4   t | } |rttj| }nttj| }t|S )a  Construct a Verified instance from a string.

        Args:
            key_pem: string, public key in PEM format.
            is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it
                          is expected to be an RSA key in PEM format.

        Returns:
            Verifier instance.

        Raises:
            OpenSSL.crypto.Error: if the key_pem can't be parsed.
        )r   r   r   Zload_certificateFILETYPE_PEMload_privatekeyr   )Zkey_pemZis_x509_certr   r   r   r	   from_string5   s
    
zOpenSSLVerifier.from_stringN)__name__
__module____qualname____doc__r
   r   staticmethodr   r   r   r   r	   r      s
   r   c                   @   s.   e Zd ZdZdd Zdd Zed
ddZd	S )OpenSSLSignerz"Signs messages with a private key.c                 C   s
   || _ dS )ztConstructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        N)_key)r   pkeyr   r   r	   r
   O   s    zOpenSSLSigner.__init__c                 C   s   t j|dd}t| j|dS )zSigns a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        r   r   r   )r   r   r   signr   )r   r   r   r   r	   r   W   s    	zOpenSSLSigner.sign
   notasecretc                 C   sN   t | } t | }|r(ttj|}nt j|dd}t| | }t|S )a>  Construct a Signer instance from a string.

        Args:
            key: string, private key in PKCS12 or PEM format.
            password: string, password for the private key file.

        Returns:
            Signer instance.

        Raises:
            OpenSSL.crypto.Error if the key can't be parsed.
        r   r   )	r   r   Z_parse_pem_keyr   r   r   load_pkcs12get_privatekeyr   )keypasswordZparsed_pem_keyr   r   r   r	   r   c   s    

zOpenSSLSigner.from_stringN)r    )r   r   r   r   r
   r   r   r   r   r   r   r	   r   L   s
   r   c                 C   s(   t |}t| |}ttj| S )a  Convert the contents of a PKCS#12 key to PEM using pyOpenSSL.

    Args:
        private_key_bytes: Bytes. PKCS#12 key in DER format.
        private_key_password: String. Password for PKCS#12 key.

    Returns:
        String. PEM contents of ``private_key_bytes``.
    )r   r   r   r!   Zdump_privatekeyr   r"   )Zprivate_key_bytesZprivate_key_passwordZpkcs12r   r   r	   pkcs12_key_as_pem{   s
    

r%   N)	r   ZOpenSSLr   Zoauth2clientr   objectr   r   r%   r   r   r   r	   <module>   s
   7/