
    h                      X   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr  SSKJr  \R                  r	Sr
S	rS
r\rSrSrSrSr\\-  \-  rSrSr\R*                  r\R,                  \R.                  l        / \R.                  l        \R.                  r " S S\5      r " S S\5      rg)z.Utilities for encryption functions on Windows.    )absolute_import)division)unicode_literalsN)windll)wintypesi      z.Microsoft Enhanced Cryptographic Provider v1.0l      ` i   i   @      c                   ,   ^  \ rS rSrSU 4S jjrSrU =r$ )WindowsException4   c                 h   > [        5       nSU-  nU(       a  USU-  -  n[        [        U ]  U5        g )NzWindows Error: 0x%0xz
Extra Info: %s)get_last_errorsuperr   __init__)self
extra_datawindows_error_codemessage	__class__s       >lib/googlecloudsdk/api_lib/compute/windows_encryption_utils.pyr   WindowsException.__init__6   s:    ')$'99G#j00g	
D*73     N)__name__
__module____qualname____firstlineno__r   __static_attributes____classcell__)r   s   @r   r   r   4   s    4 4r   r   c                   :    \ rS rSrSrS rS rS rS
S jrS r	Sr
g	)WinCrypt>   z,Base Class for Windows encryption functions.c                    [         R                  U l        [        R                  U R                  l        [        R                  [        R                  [        R                  [        R                  [        R                  /U R                  l	        [         R                  U l        [        R                  U R                  l        [        R                  [        R                  /U R                  l	        [         R                  U l        [        R                  U R                  l        [        R                  [        R                  [        R                  [        R                  " [        R                  5      /U R                  l	        [         R                   U l        [        R                  U R"                  l        [        R                  /U R"                  l	        [         R$                  U l        [        R                  U R&                  l        [        R                  [        R                  [        R                  [        R                  [        R                  " [        R(                  5      [        R                  " [        R                  5      /U R&                  l	        [         R*                  U l        [        R                  U R,                  l        [        R                  [        R                  [        R                  [        R                  [        R                  " [        R(                  5      [        R                  " [        R                  5      /U R,                  l	        g r   )advapi32CryptAcquireContextWcrypt_acquire_contextr   BOOLrestypeHANDLELPCWSTRDWORDargtypesCryptReleaseContextcrypt_release_contextCryptGenKeycrypt_gen_keyctypesPOINTERCryptDestroyKeycrypt_destroy_keyCryptDecryptcrypt_decryptBYTECryptExportKeycrypt_export_key)r   s    r   r   WinCrypt.__init__A   s   !)!>!>D)1D&+3??+3+;+;+3+;+;+3>>+3>>	+;D' "*!=!=D)1D&+3??+3>>+;D' "--D!)D#+??#+>>#+>>#)>>(//#B#DD
 &55D%-]]D"'/&7D#!..D!)D#+??#+??#+==#+>>#)>>(--#@#)>>(..#A#CD %33D$,MMD!&.oo&.oo&.nn&.nn&,nnX]]&C&,nnX^^&D&FD"r   c                 N   [         R                  " 5       nSn[        n[        n[        n[
        n[         R                  " 5       nU R                  [        R                  " U5      UUUU5      (       d  [        e[        S-  nU R                  UUUU5      (       d
  [        5       eU$ )z%Returns a handle for an RSA key pair.N   )r   r+   MS_ENHANCED_PROVPROV_RSA_FULLCRYPT_VERIFYCONTEXTCALG_RSA_KEYXr(   r3   byrefr   RSA_KEY_LENGTHr2   )	r   crypt_provider_handlekey_container_nameproviderprovider_typeacquire_context_flagsalgorithm_id
key_handlegen_key_flagss	            r   
GetKeyPairWinCrypt.GetKeyPairl   s     %OO-H!M/ L"J %%fll3H&I&8&.&3&;	= =
  #b(M3*+(* * r   c           
         Sn[         nSnSn[        R                  " 5       nU R                  UUUUU[        R                  " U5      5        [
        R                  UR                  -  nU" 5       nU R                  UUUUU[        R                  " U5      5      (       d
  [        5       e[        R                  UR                  -  R                  U5      nU$ )z=Returns the public key for the referenced private key handle.Nr   )PUBLICKEYBLOBr3   c_ulongr;   rC   r   r9   valuer   c_charfrom_buffer)	r   keyuser_crypto_keykey_typeexport_key_flagskey_datakey_lenbyte_array_type
public_keys	            r   GetPublicKeyWinCrypt.GetPublicKey   s    OH HnnG#)"*" ,,w/1 mmgmm3O H  !0!)!1!)!'g!68 8 --'--/<<XFJr   c           
         [         R                  " U5      nUSSS2   n[        R                  " [	        U5      5      n[
        R                  UR                  -  R                  U5      nSnSn	[        n
U R                  UUU	U
U[        R                  " U5      5      (       d  [        U5      e[        R                  UR                  -  R                  U5      nU(       a  U R                  U5        UR                  $ )a  Returns a decrypted message from the given encrypted message and key.

Can optionally destroy the key (used only on Windows).

Args:
  key: An openssl key pair (private key) or a Windows key handle.
  enc_message: A base64 encoded encrypted message.
  destroy_key: If True, the key pointed to by the key handle is destroyed.

Returns:
  Decrypted version of enc_message

Raises:
  WindowsException: If message fails to decrypt
NT)base64	b64decoder3   rQ   lenr   r9   rR   from_buffer_copy
CRYPT_OAEPr8   rC   r   rS   rT   r6   )r   rU   enc_messagedestroy_keydecoded_messagelittle_endian_messagedata_lendata_bufhash_objectfinaldecrypt_flagsr   s               r   DecryptMessageWinCrypt.DecryptMessage   s      &&{3O+DbD1~~c"789H.@@H KEMc)#+&$ll846 6 X&&}}x~~-::8DG
S!==r   c                     USS SSS2   nUSS SSS2   n[         R                  " U5      n[         R                  " U5      nXE4$ )zBReturns a base64 encoded modulus and exponent from the public key.   Nr`   r>   )ra   	b64encode)r   r\   modulusexponentb64_modb64_exps         r   GetModulusExponentFromPublicKey(WinCrypt.GetModulusExponentFromPublicKey   sV     odd#G"R 2&Hw'Gx(Gr   )r(   r8   r6   r;   r2   r0   N)T)r   r   r   r   __doc__r   rM   r]   ro   rx   r    r   r   r   r#   r#   >   s!    4)FV<<'Rr   r#   )rz   
__future__r   r   r   ra   r3   r   r   r&   rD   r@   MS_ENHANCED_PROV_Wr?   rA   ALG_CLASS_KEY_EXCHANGEALG_TYPE_RSAALG_SID_RSA_ANYrB   re   rP   kernel32r-   GetLastErrorr*   r.   r   	Exceptionr   objectr#   r   r   r   <module>r      s    5 &  '    ?? E %    '4_D
 ?? (   !#   &&4y 4`v `r   