o
    i                     @   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 ]}| ||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   Z/var/www/edux/Edux_v2/venv/lib/python3.10/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)bit_listnum_bits	byte_valsstart	curr_bits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   
   || _ d S r   )_pubkey)selfpubkeyr   r   r   __init__H      
zRsaVerifier.__init__c              	   C   s@   t j|dd}z
tj||| jW S  ttjjfy   Y dS w )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encodingF)r   	_to_bytesrsapkcs1verifyr)   
ValueErrorVerificationError)r*   message	signaturer   r   r   r4   K   s   zRsaVerifier.verifyc           	      C   s   t |}|r8tj|d}tj|t d\}}|dkr!td||d d }t	|d }tj
|d}| |S 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asn1Spec    Unused bytestbsCertificatesubjectPublicKeyInfosubjectPublicKeyDERPEM)r   r1   r2   r   load_pemr   decoder   r5   r&   	PublicKey
load_pkcs1)	clskey_pemis_x509_certder	asn1_cert	remaining	cert_info	key_bytesr+   r   r   r   from_string^   s   

zRsaVerifier.from_stringN)__name__
__module____qualname____doc__r,   r4   classmethodrO   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   r(   r   )_key)r*   pkeyr   r   r   r,      r-   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   r1   r2   r3   signrV   )r*   r7   r   r   r   rX      s   	zRsaSigner.sign
notasecretc           	      C   s   t |}tt|tt\}}|dkr"tj	j
j|dd}| |S |dkrLtj|td\}}|dkr8td||d}tj	j
j| dd}| |S td	)
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   rA   )formatr   r:   r<   r=   
privateKeyzNo key could be detected.)r   _from_bytesr   readPemBlocksFromFiler   StringIO_PKCS1_MARKER_PKCS8_MARKERr2   key
PrivateKeyrF   r   rD   _PKCS8_SPECr5   getComponentByNameasOctets)	rG   ra   password	marker_idrN   rW   key_inforL   	pkey_infor   r   r   rO      s*   




zRsaSigner.from_stringN)rY   )rP   rQ   rR   rS   r,   rX   rT   rO   r   r   r   r   rU      s    rU   )rS   pyasn1.codec.derr   pyasn1_modulesr   pyasn1_modules.rfc2459r   pyasn1_modules.rfc5208r   r2   r   oauth2clientr   _PKCS12_ERRORr   r_   r`   rc   r&   objectr'   rU   r   r   r   r   <module>   s    	?