o
    "h(Y                     @   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mZ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/Z0G d%d& d&e/Z1dS )'    N)	b64decode)Sequence)TYPE_CHECKINGOptionalno_type_check)default_backend)MGF1OAEP)Cipher)AES)CBC)SHA1SHA256SHA512HashT)NNNNN)NNNNF)TelegramObject)de_json_optionalde_list_optional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+   Q/var/www/html/venv/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   r2   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"   r7   c                   sJ   t  j|d || _|| _|| _| j| j| jf| _d | _d | _|   d S Nr6   )	super__init__r$   r#   r"   	_id_attrsr5   r4   _freeze)selfr$   r#   r"   r7   	__class__r+   r,   r:      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)mgfrA   label)r5   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)	r4   rI   de_jsonr2   rH   r   r#   r$   rD   )r=   r+   r+   r,   decrypted_data   s   
z#EncryptedCredentials.decrypted_data)r@   rI   )__name__
__module____qualname____doc__	__slots__strr   r   r:   propertybytesrH   rK   __classcell__r+   r+   r>   r,   r3   j   s"    r3   c                       s`   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ded dd f fddZ
  ZS )rI   z
    Attributes:
        secure_data (:class:`telegram.SecureData`): Credentials for encrypted data
        nonce (:obj:`str`): Bot-specified nonce
    )noncesecure_dataNr6   rV   
SecureDatarU   r7   c                   s&   t  j|d || _|| _|   d S r8   )r9   r:   rV   rU   r<   )r=   rV   rU   r7   r>   r+   r,   r:      s   zCredentials.__init__r$   botr   r@   c                    s0   |  |}t|dt||d< t j||dS ),See :meth:`telegram.TelegramObject.de_json`.rV   r$   rX   )_parse_datar   getrW   r9   rJ   clsr$   rX   r>   r+   r,   rJ      s   
zCredentials.de_jsonN)rL   rM   rN   rO   rP   rQ   r   r   r:   classmethodrJ   rT   r+   r+   r>   r,   rI      s    *rI   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ded dd f fddZ	  Z
S )rW   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_billNr6   rh   SecureValuerf   re   rc   rd   ra   rk   rb   ri   rg   rj   r7   c                   s\   t  j|d || _|
| _|	| _|| _|| _|| _|| _|| _	|| _
|| _|| _|   d S r8   )r9   r:   rj   rg   ri   rb   rk   ra   rd   rc   re   rf   rh   r<   )r=   rh   rf   re   rc   rd   ra   rk   rb   ri   rg   rj   r7   r>   r+   r,   r:   6  s   zSecureData.__init__r$   rX   r   r@   c                    s  |  |}t|dt||d< t|dt||d< t|dt||d< t|dt||d< t|dt||d< t|dt||d< t|dt||d< t|dt||d< t|d	t||d	< t|d
t||d
< t|dt||d< t j||dS )rY   rj   rg   ri   rb   rk   ra   rd   rc   re   rf   rh   rZ   )r[   r   r\   rl   r9   rJ   r]   r>   r+   r,   rJ   W  s&   
zSecureData.de_json)NNNNNNNNNNNr_   )rL   rM   rN   rO   rP   r   r   r:   r`   rJ   rT   r+   r+   r>   r,   rW      sV    1	
!*rW   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ded dd f fddZ
  ZS )rl   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translationNr6   r$   DataCredentialsrn   FileCredentialsro   rp   rm   rq   r7   c                   sF   t  j|d || _|| _|| _|| _t|| _t|| _| 	  d S r8   )
r9   r:   r$   rn   ro   rp   r   rm   rq   r<   )r=   r$   rn   ro   rp   rm   rq   r7   r>   r+   r,   r:     s   

zSecureValue.__init__rX   r   r@   c                    s   |  |}t|dt||d< t|dt||d< t|dt||d< t|dt||d< t|dt||d< t|dt||d< t j||dS )rY   r$   rn   ro   rp   rm   rq   rZ   )r[   r   r\   rr   rs   r   r9   rJ   r]   r>   r+   r,   rJ     s   
zSecureValue.de_json)NNNNNNr_   )rL   rM   rN   rO   rP   r   r   r   r:   r`   rJ   rT   r+   r+   r>   r,   rl   q  s8    7	

	*rl   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"   Nr6   r#   r"   r7   c                   sX   t  j|d |   || _|| _| j| _| j| _W d    d S 1 s%w   Y  d S r8   )r9   r:   	_unfrozenr#   r"   rv   r*   )r=   r#   r"   r7   r>   r+   r,   r:     s   

"z_CredentialsBase.__init__
rL   rM   rN   rO   rP   rQ   r   r   r:   rT   r+   r+   r>   r,   ru     s    ru   c                       rt   )
rr   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+   Nr6   r*   r"   r7   c                      t  j|||d |   d S N)r#   r"   r7   r9   r:   r<   )r=   r*   r"   r7   r>   r+   r,   r:        zDataCredentials.__init__rx   r+   r+   r>   r,   rr         ,rr   c                       rt   )
rs   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+   Nr6   rv   r"   r7   c                   ry   rz   r{   )r=   rv   r"   r7   r>   r+   r,   r:   
  r|   zFileCredentials.__init__rx   r+   r+   r>   r,   rs     r}   rs   )2r.   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   r   r   telegram._utils.stringsr   telegram._utils.typesr   telegram.errorr   telegramr   r-   r2   r3   rI   rW   rl   ru   rr   rs   r+   r+   r+   r,   <module>   sH   
0
k!{^