a
    !f                     @   s   d 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
Z
ddlmZ dZd	Zd
ZdZe Zdd ZG dd deZG dd deZdS )zPure Python crypto-related routines for oauth2client.

Uses the ``rsa``, ``pyasn1`` and ``pyasn1_modules`` packages
to parse PEM files storing PKCS#1 or PKCS#8 keys as well as
certificates.
    )decoder)pem)Certificate)PrivateKeyInfoN)_helpersz\
PKCS12 format is not supported by the RSA library.
Either install PyOpenSSL, or please convert .p12 format
to .pem format:
    $ cat key.p12 | \
    >   openssl pkcs12 -nodes -nocerts -passin pass:notasecret | \
    >   openssl rsa > key.pem
)   @                      )z-----BEGIN RSA PRIVATE KEY-----z-----END RSA PRIVATE KEY-----)z-----BEGIN PRIVATE KEY-----z-----END PRIVATE KEY-----c                 C   s^   t | }t }tjd|dD ]6}| ||d  }tdd tt|D }|| qt	|S )zConverts an iterable of 1's and 0's to bytes.

    Combines the list 8 at a time, treating each group of 8 bits
    as a single byte.
    r   r   c                 s   s   | ]\}}|| V  qd S N ).0valdigitr   r   `/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/_pure_python_crypt.py	<genexpr>;   s   z%_bit_list_to_bytes.<locals>.<genexpr>)
len	bytearraysixmovesxrangesumzip_POW2appendbytes)Zbit_listZnum_bitsZ	byte_valsstartZ	curr_bitsZchar_valr   r   r   _bit_list_to_bytes1   s    r!   c                   @   s,   e Zd ZdZdd Zdd Zedd ZdS )	RsaVerifierzVerifies the signature on a message.

    Args:
        pubkey: rsa.key.PublicKey (or equiv), The public key to verify with.
    c                 C   s
   || _ d S r   )_pubkey)selfpubkeyr   r   r   __init__H   s    zRsaVerifier.__init__c              	   C   sD   t j|dd}ztj||| jW S  ttjjfy>   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encodingFN)r   	_to_bytesrsapkcs1verifyr#   
ValueErrorZVerificationError)r$   message	signaturer   r   r   r-   K   s
    zRsaVerifier.verifyc           	      C   s   t |}|rjtj|d}tj|t d\}}|dkrBtd||d d }t	|d }tj
|d}ntj
|d	}| |S )
a  Construct an RsaVerifier 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:
            RsaVerifier instance.

        Raises:
            ValueError: if the key_pem can't be parsed. In either case, error
                        will begin with 'No PEM start marker'. If
                        ``is_x509_cert`` is True, will fail to find the
                        "-----BEGIN CERTIFICATE-----" error, otherwise fails
                        to find "-----BEGIN RSA PUBLIC KEY-----".
        CERTIFICATEZasn1Spec    Unused bytesZtbsCertificateZsubjectPublicKeyInfoZsubjectPublicKeyDERZPEM)r   r*   r+   r   Zload_pemr   decoder   r.   r!   Z	PublicKey
load_pkcs1)	clsZkey_pemZis_x509_certZderZ	asn1_cert	remainingZ	cert_info	key_bytesr%   r   r   r   from_string^   s    

zRsaVerifier.from_stringN)__name__
__module____qualname____doc__r&   r-   classmethodr;   r   r   r   r   r"   A   s
   r"   c                   @   s.   e Zd ZdZdd Zdd Zed
ddZd	S )	RsaSignerz}Signs messages with a private key.

    Args:
        pkey: rsa.key.PrivateKey (or equiv), The private key to sign with.
    c                 C   s
   || _ d S r   )_key)r$   pkeyr   r   r   r&      s    zRsaSigner.__init__c                 C   s    t j|dd}tj|| 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(   zSHA-256)r   r*   r+   r,   signrB   )r$   r/   r   r   r   rD      s    	zRsaSigner.sign
notasecretc           	      C   s   t |}tt|tt\}}|dkr>tj	j
j|dd}nV|dkrtj|td\}}|dkrjtd||d}tj	j
j| dd}ntd	| |S )
a  Construct an RsaSigner instance from a string.

        Args:
            key: string, private key in PEM format.
            password: string, password for private key file. Unused for PEM
                      files.

        Returns:
            RsaSigner instance.

        Raises:
            ValueError if the key cannot be parsed as PKCS#1 or PKCS#8 in
            PEM format.
        r   r5   )formatr   r2   r3   r4   Z
privateKeyzNo key could be detected.)r   Z_from_bytesr   ZreadPemBlocksFromFiler   StringIO_PKCS1_MARKER_PKCS8_MARKERr+   keyZ
PrivateKeyr7   r   r6   _PKCS8_SPECr.   ZgetComponentByNameZasOctets)	r8   rJ   passwordZ	marker_idr:   rC   Zkey_infor9   Z	pkey_infor   r   r   r;      s(    




zRsaSigner.from_stringN)rE   )r<   r=   r>   r?   r&   rD   r@   r;   r   r   r   r   rA      s
   rA   )r?   Zpyasn1.codec.derr   Zpyasn1_modulesr   Zpyasn1_modules.rfc2459r   Zpyasn1_modules.rfc5208r   r+   r   Zoauth2clientr   Z_PKCS12_ERRORr   rH   rI   rK   r!   objectr"   rA   r   r   r   r   <module>   s   	?