o
    GChY                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZ z0d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZmZ dZW n eyd   dZ
d\ZZZZZd\ZZZZdZY nw 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$ erd dl%m&Z& edd Z'edd Z(G dd deZ)G dd deZ*G dd deZ+G dd  d eZ,G d!d" d"eZ-G d#d$ d$e-Z.G d%d& d&e-Z/dS )'    N)	b64decode)Sequence)TYPE_CHECKINGOptionalno_type_check)default_backend)MGF1OAEP)Cipher)AES)CBC)SHA1SHA256SHA512HashT)NNNNN)NNNNF)TelegramObject)parse_sequence_arg)TextEncoding)JSONDict)PassportDecryptionError)Botc           
      C   s   t stdtt t d}|| |  | }|dd |dd }}tt|t	|t d}|
 }|||  }tt t d}|| | }	|	|kr^td|	 d| ||d d S )	a  
    Decrypt per telegram docs at https://core.telegram.org/passport.

    Args:
        secret (:obj:`str` or :obj:`bytes`): The encryption secret, either as bytes or as a
            base64 encoded string.
        hash (:obj:`str` or :obj:`bytes`): The hash, either as bytes or as a
            base64 encoded string.
        data (:obj:`str` or :obj:`bytes`): The data to decrypt, either as bytes or as a
            base64 encoded string.
        file (:obj:`bool`): Force data to be treated as raw data, instead of trying to
            b64decode it.

    Raises:
        :class:`PassportDecryptionError`: Given hash does not match hash of decrypted data.

    Returns:
        :obj:`bytes`: The decrypted data as bytes.

    cTo use Telegram Passports, PTB must be installed via `pip install "python-telegram-bot[passport]"`.)backendN    0   zHashes are not equal! z != r   )CRYPTO_INSTALLEDRuntimeErrorr   r   r   updatefinalizer
   r   r   	decryptorr   r   )
secrethashdatadigestsecret_hash_hashkeyinit_vectorcipherr   	data_hash r)   W/var/www/html/govbot/env/lib/python3.10/site-packages/telegram/_passport/credentials.pydecrypt3   s"   
r+   c                 C   s   t t| ||tjS )zPDecrypts data using secret and hash and then decodes utf-8 string and loads json)jsonloadsr+   decoder   UTF_8)r    r!   r"   r)   r)   r*   decrypt_jsond   s   r0   c                
       s`   e Zd ZdZdZdddedededee f fd	d
Ze	de
fddZe	dddZ  ZS )EncryptedCredentialsaD  Contains data required for decrypting and authenticating EncryptedPassportElement. See the
    Telegram Passport Documentation for a complete description of the data decryption and
    authentication processes.

    Objects of this class are comparable in terms of equality. Two objects of this class are
    considered equal, if their :attr:`data`, :attr:`hash` and :attr:`secret` are equal.

    Note:
        This object is decrypted only when originating from
        :attr:`telegram.PassportData.decrypted_credentials`.

    Args:
        data (:class:`telegram.Credentials` | :obj:`str`): Decrypted data with unique user's
            nonce, data hashes and secrets used for EncryptedPassportElement decryption and
            authentication or base64 encrypted data.
        hash (:obj:`str`): Base64-encoded data hash for data authentication.
        secret (:obj:`str`): Decrypted or encrypted secret used for decryption.

    Attributes:
        data (:class:`telegram.Credentials` | :obj:`str`): Decrypted data with unique user's
            nonce, data hashes and secrets used for EncryptedPassportElement decryption and
            authentication or base64 encrypted data.
        hash (:obj:`str`): Base64-encoded data hash for data authentication.
        secret (:obj:`str`): Decrypted or encrypted secret used for decryption.

    )_decrypted_data_decrypted_secretr"   r!   r    N
api_kwargsr"   r!   r    r5   c                   sJ   t  j|d || _|| _|| _| j| j| jf| _d | _d | _|   d S Nr4   )	super__init__r"   r!   r    	_id_attrsr3   r2   _freeze)selfr"   r!   r    r5   	__class__r)   r*   r8      s   zEncryptedCredentials.__init__returnc              
   C   sv   | j du r8tstdz|  jt| jtt	t
 dt
 dd| _ W | j S  ty7 } zt||d}~ww | j S )z
        :obj:`bytes`: Lazily decrypt and return secret.

        Raises:
            telegram.error.PassportDecryptionError: Decryption failed. Usually due to bad
                private/public key but can also suggest malformed/tampered data.
        Nr   )	algorithm)mgfr?   label)r3   r   r   get_botprivate_keyr+   r   r    r	   r   r   
ValueErrorr   )r;   	exceptionr)   r)   r*   decrypted_secret   s    
	


z%EncryptedCredentials.decrypted_secretCredentialsc                 C   s8   | j du rtt| jt| jt| j|  | _ | j S )a~  
        :class:`telegram.Credentials`: Lazily decrypt and return credentials data. This object
            also contains the user specified nonce as
            `decrypted_data.nonce`.

        Raises:
            telegram.error.PassportDecryptionError: Decryption failed. Usually due to bad
                private/public key but can also suggest malformed/tampered data.
        N)	r2   rG   de_jsonr0   rF   r   r!   r"   rB   )r;   r)   r)   r*   decrypted_data   s   
z#EncryptedCredentials.decrypted_data)r>   rG   )__name__
__module____qualname____doc__	__slots__strr   r   r8   propertybytesrF   rI   __classcell__r)   r)   r<   r*   r1   j   s"    r1   c                	       sj   e Zd ZdZdZdddddedee f fd	d
Ze		ddee ded ded  f fddZ
  ZS )rG   z
    Attributes:
        secure_data (:class:`telegram.SecureData`): Credentials for encrypted data
        nonce (:obj:`str`): Bot-specified nonce
    )noncesecure_dataNr4   rT   
SecureDatarS   r5   c                   s&   t  j|d || _|| _|   d S r6   )r7   r8   rT   rS   r:   )r;   rT   rS   r5   r<   r)   r*   r8      s   zCredentials.__init__r"   botr   r>   c                    s:   |  |}|s	dS tj|d|d|d< t j||dS ),See :meth:`telegram.TelegramObject.de_json`.NrT   rV   r"   rV   )_parse_datarU   rH   getr7   clsr"   rV   r<   r)   r*   rH      s
   
zCredentials.de_jsonN)rJ   rK   rL   rM   rN   rO   r   r   r8   classmethodrH   rR   r)   r)   r<   r*   rG      s(    rG   c                       s   e Zd ZdZdZ											dddded ded ded d	ed d
ed ded ded ded ded ded ded dee f fddZe	ddee ded ded  f fddZ	  Z
S )rU   a  
    This object represents the credentials that were used to decrypt the encrypted data.
    All fields are optional and depend on fields that were requested.

    Args:
        personal_details (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            personal details.
        passport (:class:`telegram.SecureValue`, optional): Credentials for encrypted passport.
        internal_passport (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            internal passport.
        driver_license (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            driver license.
        identity_card (:class:`telegram.SecureValue`, optional): Credentials for encrypted ID card
        address (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            residential address.
        utility_bill (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            utility bill.
        bank_statement (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            bank statement.
        rental_agreement (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            rental agreement.
        passport_registration (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            registration from internal passport.
        temporary_registration (:class:`telegram.SecureValue`, optional): Credentials for encrypted
            temporary registration.

    Attributes:
        personal_details (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            personal details.
        passport (:class:`telegram.SecureValue`): Optional. Credentials for encrypted passport.
        internal_passport (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            internal passport.
        driver_license (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            driver license.
        identity_card (:class:`telegram.SecureValue`): Optional. Credentials for encrypted ID card
        address (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            residential address.
        utility_bill (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            utility bill.
        bank_statement (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            bank statement.
        rental_agreement (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            rental agreement.
        passport_registration (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            registration from internal passport.
        temporary_registration (:class:`telegram.SecureValue`): Optional. Credentials for encrypted
            temporary registration.
    )addressbank_statementdriver_licenseidentity_cardinternal_passportpassportpassport_registrationpersonal_detailsrental_agreementtemporary_registrationutility_billNr4   rg   SecureValuere   rd   rb   rc   r`   rj   ra   rh   rf   ri   r5   c                   s\   t  j|d || _|
| _|	| _|| _|| _|| _|| _|| _	|| _
|| _|| _|   d S r6   )r7   r8   ri   rf   rh   ra   rj   r`   rc   rb   rd   re   rg   r:   )r;   rg   re   rd   rb   rc   r`   rj   ra   rh   rf   ri   r5   r<   r)   r*   r8   ;  s   zSecureData.__init__r"   rV   r   r>   c                    s*  |  |}|s	dS tj|d|d|d< tj|d|d|d< tj|d|d|d< tj|d|d|d< tj|d|d|d< tj|d|d|d< tj|d	|d|d	< tj|d
|d|d
< tj|d|d|d< tj|d|d|d< tj|d|d|d< t j||dS )rW   Nri   rX   rf   rh   ra   rj   r`   rc   rb   rd   re   rg   rY   )rZ   rk   rH   r[   r7   r\   r<   r)   r*   rH   \  s&   




zSecureData.de_json)NNNNNNNNNNNr^   )rJ   rK   rL   rM   rN   r   r   r8   r_   rH   rR   r)   r)   r<   r*   rU      sd    1	
!rU   c                       s   e Zd ZdZdZ						dddded ded d	ed d
ed deed  deed  dee f fddZe		ddee ded ded  f fddZ
  ZS )rk   a?  
    This object represents the credentials that were used to decrypt the encrypted value.
    All fields are optional and depend on the type of field.

    Args:
        data (:class:`telegram.DataCredentials`, optional): Credentials for encrypted Telegram
            Passport data. Available for "personal_details", "passport", "driver_license",
            "identity_card", "identity_passport" and "address" types.
        front_side (:class:`telegram.FileCredentials`, optional): Credentials for encrypted
            document's front side. Available for "passport", "driver_license", "identity_card"
            and "internal_passport".
        reverse_side (:class:`telegram.FileCredentials`, optional): Credentials for encrypted
            document's reverse side. Available for "driver_license" and "identity_card".
        selfie (:class:`telegram.FileCredentials`, optional): Credentials for encrypted selfie
            of the user with a document. Can be available for "passport", "driver_license",
            "identity_card" and "internal_passport".
        translation (list[:class:`telegram.FileCredentials`], optional): Credentials for an
            encrypted translation of the document. Available for "passport", "driver_license",
            "identity_card", "internal_passport", "utility_bill", "bank_statement",
            "rental_agreement", "passport_registration" and "temporary_registration".
        files (list[:class:`telegram.FileCredentials`], optional): Credentials for encrypted
            files. Available for "utility_bill", "bank_statement", "rental_agreement",
            "passport_registration" and "temporary_registration" types.

    Attributes:
        data (:class:`telegram.DataCredentials`): Optional. Credentials for encrypted Telegram
            Passport data. Available for "personal_details", "passport", "driver_license",
            "identity_card", "identity_passport" and "address" types.
        front_side (:class:`telegram.FileCredentials`): Optional. Credentials for encrypted
            document's front side. Available for "passport", "driver_license", "identity_card"
            and "internal_passport".
        reverse_side (:class:`telegram.FileCredentials`): Optional. Credentials for encrypted
            document's reverse side. Available for "driver_license" and "identity_card".
        selfie (:class:`telegram.FileCredentials`): Optional. Credentials for encrypted selfie
            of the user with a document. Can be available for "passport", "driver_license",
            "identity_card" and "internal_passport".
        translation (tuple[:class:`telegram.FileCredentials`]): Optional. Credentials for an
            encrypted translation of the document. Available for "passport", "driver_license",
            "identity_card", "internal_passport", "utility_bill", "bank_statement",
            "rental_agreement", "passport_registration" and "temporary_registration".

            .. versionchanged:: 20.0
                |tupleclassattrs|

        files (tuple[:class:`telegram.FileCredentials`]): Optional. Credentials for encrypted
            files. Available for "utility_bill", "bank_statement", "rental_agreement",
            "passport_registration" and "temporary_registration" types.

            .. versionchanged:: 20.0

                * |tupleclassattrs|
                * |alwaystuple|

    )r"   files
front_sidereverse_sideselfietranslationNr4   r"   DataCredentialsrm   FileCredentialsrn   ro   rl   rp   r5   c                   sF   t  j|d || _|| _|| _|| _t|| _t|| _| 	  d S r6   )
r7   r8   r"   rm   rn   ro   r   rl   rp   r:   )r;   r"   rm   rn   ro   rl   rp   r5   r<   r)   r*   r8     s   

zSecureValue.__init__rV   r   r>   c                    s   |  |}|s	dS tj|d|d|d< tj|d|d|d< tj|d|d|d< tj|d|d|d< tj|d|d|d< tj|d|d|d< t j||d	S )
rW   Nr"   rX   rm   rn   ro   rl   rp   rY   )rZ   rq   rH   r[   rr   de_listr7   r\   r<   r)   r*   rH     s   
zSecureValue.de_json)NNNNNNr^   )rJ   rK   rL   rM   rN   r   r   r   r8   r_   rH   rR   r)   r)   r<   r*   rk   y  sF    7	

	rk   c                       <   e Zd ZdZdZdddededee f fdd	Z  Z	S )
_CredentialsBasez3Base class for DataCredentials and FileCredentials.)r(   	file_hashr!   r    Nr4   r!   r    r5   c                   sX   t  j|d |   || _|| _| j| _| j| _W d    d S 1 s%w   Y  d S r6   )r7   r8   	_unfrozenr!   r    rv   r(   )r;   r!   r    r5   r<   r)   r*   r8     s   

"z_CredentialsBase.__init__
rJ   rK   rL   rM   rN   rO   r   r   r8   rR   r)   r)   r<   r*   ru     s    ru   c                       rt   )
rq   al  
    These credentials can be used to decrypt encrypted data from the data field in
    EncryptedPassportData.

    Args:
        data_hash (:obj:`str`): Checksum of encrypted data
        secret (:obj:`str`): Secret of encrypted data

    Attributes:
        hash (:obj:`str`): Checksum of encrypted data
        secret (:obj:`str`): Secret of encrypted data
    r)   Nr4   r(   r    r5   c                      t  j|||d |   d S N)r!   r    r5   r7   r8   r:   )r;   r(   r    r5   r<   r)   r*   r8        zDataCredentials.__init__rx   r)   r)   r<   r*   rq         ,rq   c                       rt   )
rr   a  
    These credentials can be used to decrypt encrypted files from the front_side,
    reverse_side, selfie and files fields in EncryptedPassportData.

    Args:
        file_hash (:obj:`str`): Checksum of encrypted file
        secret (:obj:`str`): Secret of encrypted file

    Attributes:
        hash (:obj:`str`): Checksum of encrypted file
        secret (:obj:`str`): Secret of encrypted file
    r)   Nr4   rv   r    r5   c                   ry   rz   r{   )r;   rv   r    r5   r<   r)   r*   r8     r|   zFileCredentials.__init__rx   r)   r)   r<   r*   rr     r}   rr   )0r,   base64r   collections.abcr   typingr   r   r   cryptography.hazmat.backendsr   1cryptography.hazmat.primitives.asymmetric.paddingr   r	   &cryptography.hazmat.primitives.ciphersr
   1cryptography.hazmat.primitives.ciphers.algorithmsr   ,cryptography.hazmat.primitives.ciphers.modesr   %cryptography.hazmat.primitives.hashesr   r   r   r   r   ImportErrortelegram._telegramobjectr   telegram._utils.argumentparsingr   telegram._utils.stringsr   telegram._utils.typesr   telegram.errorr   telegramr   r+   r0   r1   rG   rU   rk   ru   rq   rr   r)   r)   r)   r*   <module>   sH   
0
k&~c