o
    ¾­§iÜ  ã                   @   s’   d Z ddlZddlmZ ejjjZejjjZej	j
jZejejejge_ejjjZejjjZdZG dd„ dejƒZddd	d
œZG dd„ deƒZdS )zNImplements a Windows Specific TokenCache, and provides auxiliary helper types.é    N)Úwintypesé   c                   @   s2   e Zd ZdZdejfde ej¡fgZ	dd„ Z
dS )ÚDataBloba°  A wrapper for interacting with the _CRYPTOAPI_BLOB type and its many aliases. This type is
    exposed from Wincrypt.h in XP and above.

    The memory associated with a DataBlob itself does not need to be freed, as the Python runtime
    will correctly clean it up. However, depending on the data it points at, it may still need to be
    freed. For instance, memory created by ctypes.create_string_buffer is already managed, and needs
    to not be freed. However, memory allocated by CryptProtectData and CryptUnprotectData must have
    LocalFree called on pbData.

    See documentation for this type at:
    https://msdn.microsoft.com/en-us/7a06eae5-96d8-4ece-98cb-cf0710d2ddbd
    ÚcbDataÚpbDatac                 C   s,   t | jƒ}| j}t |¡}t|||ƒ |jS )z³Copies the message from the DataBlob in natively allocated memory into Python controlled
        memory.
        :return A byte array that matches what is stored in native-memory.)Úintr   r   ÚctypesÚcreate_string_bufferÚ_MEMCPYÚraw)ÚselfZcb_dataZpb_dataÚblob_buffer© r   úN/home/app/Keep/.python/lib/python3.10/site-packages/msal_extensions/windows.pyr      s
   

zDataBlob.rawN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZDWORDr   ZPOINTERZc_charZ_fields_r   r   r   r   r   r      s    r   z)Key not valid for use in specified state.aB  The requested operation cannot be completed. The computer must be trusted for delegation and the current user account must be configured to allow delegation. See also https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/enable-computer-and-user-accounts-to-be-trusted-for-delegationzThe data is invalid.)i 	€iE	€é   c                   @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚWindowsDataProtectionAgentzUA mechanism for interacting with the Windows DP API Native library, e.g. Crypt32.dll.Nc                 C   s<   d | _ |r| d¡}t |t|ƒ¡}tt|ƒ|ƒ| _ d S d S )Núutf-8)Ú_entropy_blobÚencoder   r	   Úlenr   )r   ÚentropyZentropy_utf8r   r   r   r   Ú__init__9   s   
ýz#WindowsDataProtectionAgent.__init__c              
   C   s¤   |  d¡}t |t|ƒ¡}tt|ƒ|ƒ}tƒ }| jr!t | j¡}nd}tt |¡d|ddtt |¡ƒrDz
| 	¡ W t
|jƒ S t
|jƒ w tƒ }tdt |d¡d|ƒ‚)zMEncrypts a message.
        :return cipher text holding the original message.r   NZpython_dataÚ )r   r   r	   r   r   r   ÚbyrefÚ_CRYPT_PROTECT_DATAÚ_CRYPTPROTECT_UI_FORBIDDENr   Ú_LOCAL_FREEr   Ú_GET_LAST_ERRORÚOSErrorÚ_err_descriptionÚget)r   ÚmessageZmessage_bufferZmessage_blobÚresultr   Úerr_coder   r   r   ÚprotectA   s*   
ùz"WindowsDataProtectionAgent.protectc              
   C   s    t  |t|ƒ¡}tt|ƒ|ƒ}tƒ }| jrt  | j¡}nd}tt  |¡d|ddtt  |¡ƒrBz| ¡  	d¡W t
|jƒ S t
|jƒ w tƒ }tdt |d¡d|ƒ‚)zfDecrypts cipher text that is provided.
        :return The original message hidden in the cipher text.Nr   r   )r   r	   r   r   r   r   Ú_CRYPT_UNPROTECT_DATAr   r   Údecoder    r   r!   r"   r#   r$   )r   Zcipher_textZ	ct_bufferZct_blobr&   r   r'   r   r   r   Ú	unprotect`   s(   ù	z$WindowsDataProtectionAgent.unprotect)N)r   r   r   r   r   r(   r+   r   r   r   r   r   6   s
    
r   )r   r   r   ZwindllZkernel32Z	LocalFreer    ZGetLastErrorr!   ZcdllÚmsvcrtZmemcpyr
   Zc_void_pZc_size_tÚargtypesZcrypt32ZCryptProtectDatar   ZCryptUnprotectDatar)   r   Z	Structurer   r#   Úobjectr   r   r   r   r   Ú<module>   s     




ù