
    h                         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 Z G d de          Z G 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                 >   t          |           }t                      }t          j                            d|d          D ]P}| ||dz            }t          d t          t          |          D                       }|                    |           Qt          |          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              3   &   K   | ]\  }}||z  V  d S N ).0valdigits      w/home/visionen/pythonvenv/lib/python3.11/site-packages/oauth2client-4.1.3-py3.11.egg/oauth2client/_pure_python_crypt.py	<genexpr>z%_bit_list_to_bytes.<locals>.<genexpr>;   sF       @ @%3 U{ @ @ @ @ @ @    )
len	bytearraysixmovesxrangesumzip_POW2appendbytes)bit_listnum_bits	byte_valsstart	curr_bitschar_vals         r   _bit_list_to_bytesr*   1   s     8}}HI!!!Xq11 # #U519_-	 @ @),UI)>)>@ @ @ @ @""""r   c                   4    e Zd ZdZd Zd Zed             ZdS )RsaVerifierzVerifies the signature on a message.

    Args:
        pubkey: rsa.key.PublicKey (or equiv), The public key to verify with.
    c                     || _         d S r   )_pubkey)selfpubkeys     r   __init__zRsaVerifier.__init__H   s    r   c                     t          j        |d          }	 t          j                            ||| j                  S # t          t          j        j        f$ r Y dS w xY 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	signatures      r   r9   zRsaVerifier.verifyK   sf     $Ww???	9##GYEEECI78 	 	 	55	s   %> AAc                    t          j        |          }|rt          j                            |d          }t          j        |t                                \  }}|dk    rt          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asn1Specr   Unused bytestbsCertificatesubjectPublicKeyInfosubjectPublicKeyDERPEM)r   r6   r7   r   load_pemr   decoder   r:   r*   	PublicKey
load_pkcs1)	clskey_pemis_x509_certder	asn1_cert	remaining	cert_info	key_bytesr0   s	            r   from_stringzRsaVerifier.from_string^   s    & $W-- 
	>'""7M::C#*>##N#N#N IyC ;;;!"234JKI*95G+HIII]--i??FF]--gu==Fs6{{r   N)__name__
__module____qualname____doc__r1   r9   classmethodrT   r   r   r   r,   r,   A   sW             &   [  r   r,   c                   6    e Zd ZdZd Zd Ze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                     || _         d S r   )_key)r/   pkeys     r   r1   zRsaSigner.__init__   s    			r   c                 z    t          j        |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.
        r3   r4   zSHA-256)r   r6   r7   r8   signr]   )r/   r<   s     r   r`   zRsaSigner.sign   s3     $Ww???y~~gty)<<<r   
notasecretc                 2   t          j        |          }t          j        t	          j        |          t          t                    \  }}|dk    r't          j	        j
                            |d          }n|dk    rt          j        |t                    \  }}|dk    rt          d|          |                    d          }t          j	        j
                            |                                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   rF   )formatr   r@   r   rB   
privateKeyzNo key could be detected.)r   _from_bytesr   readPemBlocksFromFiler   StringIO_PKCS1_MARKER_PKCS8_MARKERr7   key
PrivateKeyrK   r   rI   _PKCS8_SPECr:   getComponentByNameasOctets)	rL   rj   password	marker_idrS   r^   key_inforQ   	pkey_infos	            r   rT   zRsaSigner.from_string   s     "3''"8L}m =  =	9 >>7%008= 1 ? ?DD!^^").K#1 #1 #1HiC ;;; 33LAAI7%001C1C1E1E8= 1 ? ?DD 8999s4yyr   N)ra   )rU   rV   rW   rX   r1   r`   rY   rT   r   r   r   r[   r[      s\           
= 
= 
= ! ! ! [! ! !r   r[   )rX   pyasn1.codec.derr   pyasn1_modulesr   pyasn1_modules.rfc2459r   pyasn1_modules.rfc5208r   r7   r   oauth2clientr   _PKCS12_ERRORr!   rh   ri   rl   r*   objectr,   r[   r   r   r   <module>rz      s    % $ $ $ $ $       . . . . . . 1 1 1 1 1 1 



 



 ! ! ! ! ! ! 	&2.n   < < < < <& < < <~8 8 8 8 8 8 8 8 8 8r   