
    AHj                        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ej,                        Zddd	d
Z G d de      Zy)zNImplements a Windows Specific TokenCache, and provides auxiliary helper types.    N)wintypes   c                   n    e Zd ZdZdej
                  fd ej                  ej                        fgZ	d Z
y)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                     t        | j                        }| j                  }t        j                  |      }t        |||       |j                  S )zCopies 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)selfcb_datapb_datablob_buffers       O/root/tools/cai/cai_env/lib/python3.12/site-packages/msal_extensions/windows.pyr   zDataBlob.raw   sA    
 dkk"++11':Wg.    N)__name__
__module____qualname____doc__r   DWORDr   POINTERc_char_fields_r    r   r   r   r      s5     8>>*X~v~~fmm7T,UVH	r   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                   $    e Zd ZdZddZd Zd Zy)WindowsDataProtectionAgentzUA mechanism for interacting with the Windows DP API Native library, e.g. Crypt32.dll.Nc                     d | _         |rK|j                  d      }t        j                  |t	        |            }t        t	        |      |      | _         y y )Nutf-8)_entropy_blobencoder   r   lenr   )r   entropyentropy_utf8r   s       r   __init__z#WindowsDataProtectionAgent.__init__9   sK    !">>'2L 55lCDUVK!)#l*;[!ID r   c                 J   |j                  d      }t        j                  |t        |            }t	        t        |      |      }t	               }| j
                  r t        j                  | j
                        }nd}t        t        j                  |      d|ddt        t        j                  |            r&	 |j                         t        |j                         S t               }t        dt        j                  |d      d|      # t        |j                         w xY w)zMEncrypts a message.
        :return cipher text holding the original message.r"   N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   messagemessage_buffermessage_blobresultr&   err_codes          r   protectz"WindowsDataProtectionAgent.protectA   s    
 ..)44Wc'lKGn=ll4#5#56GG\**V$&+zz|FMM*"$d,002>hOO FMM*s   :D D"c                 F   t        j                  |t        |            }t        t        |      |      }t               }| j                  r t        j
                  | j                        }nd}t        t        j
                  |      d|ddt        t        j
                  |            r5	 |j                         j                  d      t        |j                         S t               }t        dt        j                  |d      d|      # t        |j                         w xY w)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   r0   r1   r2   r3   )r   cipher_text	ct_bufferct_blobr7   r&   r8   s          r   	unprotectz$WindowsDataProtectionAgent.unprotect`   s     //S=MN	3{+Y7ll4#5#56GG W%*V$
+zz|**73FMM*"$d,002>hOO FMM*s   )D	 	D )N)r   r   r   r   r(   r9   r@   r   r   r   r    r    6   s    _JP>Pr   r    )r   r   r   windllkernel32	LocalFreer/   GetLastErrorr0   cdllmsvcrtmemcpyr   c_void_pc_size_targtypescrypt32CryptProtectDatar-   CryptUnprotectDatar;   r.   	Structurer   r2   objectr    r   r   r   <module>rP      s    T  mm$$..--((55
++


#
#OOV__fooF  mm++<< --@@ ! v 8 =o 	 EP EPr   