
    C"                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r
  SSKJ	r	  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr   " S S\	R,                  5      rg)z&Decrypt a ciphertext file using a key.    )absolute_import)division)unicode_literals)
exceptions)base)crc32c)e2e_integrity)flags)log)
console_io)filesc                   F    \ rS rSrSr\S 5       rS rS rS r	S r
S rS	rg
)Decrypt!   aF  Decrypt a ciphertext file using a Cloud KMS key.

`{command}` decrypts the given ciphertext file using the given Cloud KMS key
and writes the result to the named plaintext file. Note that to permit users
to decrypt using a key, they must be have at least one of the following IAM
roles for that key: `roles/cloudkms.cryptoKeyDecrypter`,
`roles/cloudkms.cryptoKeyEncrypterDecrypter`.

Additional authenticated data (AAD) is used as an additional check by Cloud
KMS to authenticate a decryption request. If an additional authenticated data
file is provided, its contents must match the additional authenticated data
provided during encryption and must not be larger than 64KiB. If you don't
provide a value for `--additional-authenticated-data-file`, an empty string is
used. For a thorough explanation of AAD, refer to this
guide: https://cloud.google.com/kms/docs/additional-authenticated-data

If `--ciphertext-file` or `--additional-authenticated-data-file` is set to
'-', that file is read from stdin. Note that both files cannot be read from
stdin. Similarly, if `--plaintext-file` is set to '-', the decrypted plaintext
is written to stdout.

By default, the command performs integrity verification on data sent to and
received from Cloud KMS. Use `--skip-integrity-verification` to disable
integrity verification.

## EXAMPLES

To decrypt the file 'path/to/ciphertext' using the key `frodo` with key
ring `fellowship` and location `global` and write the plaintext
to 'path/to/plaintext.dec', run:

  $ {command} \
      --key=frodo \
      --keyring=fellowship \
      --location=global \
      --ciphertext-file=path/to/input/ciphertext \
      --plaintext-file=path/to/output/plaintext.dec

To decrypt the file 'path/to/ciphertext' using the key `frodo` and the
additional authenticated data that was used to encrypt the ciphertext, and
write the decrypted plaintext to stdout, run:

  $ {command} \
      --key=frodo \
      --keyring=fellowship \
      --location=global \
      --additional-authenticated-data-file=path/to/aad \
      --ciphertext-file=path/to/input/ciphertext \
      --plaintext-file='-'
c                     [         R                  " U S5        [         R                  " U S5        [         R                  " U S5        [         R                  " U 5        [         R
                  " U 5        g )Na  Cloud KMS key to use for decryption.
* For symmetric keys, Cloud KMS detects the decryption key version from the ciphertext. If you specify a key version as part of a symmetric decryption request, an error is logged and decryption fails.
* For asymmetric keys, the encryption key version can't be detected automatically. You must keep track of this information and provide the key version in the decryption request. The key version itself is not sensitive data and does not need to be encrypted.z^to decrypt. This file should contain the result of encrypting a file with `gcloud kms encrypt`z	to output)r
   AddKeyResourceFlagsAddCiphertextFileFlagAddPlaintextFileFlagAddAadFileFlagAddSkipIntegrityVerification)parsers    lib/surface/kms/decrypt.pyArgsDecrypt.ArgsU   s`    	 C	D 
 +, 
v{3	 	&&v.    c                     [         R                  " USS9n[        U5      U:  a%  [        R                  " SR                  X5      5      eU$ )NT)binaryz<The file [{0}] is larger than the maximum size of {1} bytes.)r   ReadFromFileOrStdinlenr   BadFileExceptionformat)selfpath	max_bytesdatas       r   _ReadFileOrStdinDecrypt._ReadFileOrStdinh   sK    ))$t<D
4y9''
H
O
O    Kr   c                 $    UR                   (       + $ )N)skip_integrity_verification)r"   argss     r   _PerformIntegrityVerification%Decrypt._PerformIntegrityVerificationp   s    ////r   c                 V   UR                   S:X  a'  UR                  S:X  a  [        R                  " SS5      e U R	                  UR                   SS9nS nUR                  (       a   U R	                  UR                  SS9n[        R                  " U5      nS	UR                  ;   a0  [        R                  " S
SR                  UR                  5      5      e[        R                  " 5       nUR                  UR                  5       S9nU R!                  U5      (       a^  ["        R$                  " U5      nUb  ["        R$                  " U5      O["        R$                  " S5      n	UR'                  UUUU	S9Ul        U$ UR'                  X$S9Ul        U$ ! [
        R                   a5  n[        R                  " SR                  UR                   U5      5      eS nAff = f! [
        R                   a5  n[        R                  " SR                  UR                  U5      5      eS nAff = f)N-z--ciphertext-filezW--ciphertext-file and --additional-authenticated-data-file cannot both read from stdin.i   )r$   z)Failed to read ciphertext file [{0}]: {1}i   z<Failed to read additional authenticated data file [{0}]: {1}z/cryptoKeyVersions/z--keyz<{} includes cryptoKeyVersion which is not valid for decrypt.)namer   )
ciphertextadditionalAuthenticatedDataciphertextCrc32c!additionalAuthenticatedDataCrc32c)r0   r1   )ciphertext_file"additional_authenticated_data_filer   InvalidArgumentExceptionr&   r   Errorr    r!   r
   ParseCryptoKeyNamecryptoKeysIdcloudkms_baseGetMessagesModule9CloudkmsProjectsLocationsKeyRingsCryptoKeysDecryptRequestRelativeNamer+   r   Crc32cDecryptRequestdecryptRequest)
r"   r*   r0   eaadcrypto_key_refmessagesreqciphertext_crc32c
aad_crc32cs
             r   _CreateDecryptRequestDecrypt._CreateDecryptRequests   s)   #//36//
"# #
	( ((


) ) 5j C..A##33u $ F --d3N
  ; ;;//
 VN778: : ..0H

L
L((* M ,C ))$// --
3),6==%fmmC>Pj#22&),,6	 3 8c J $22 3 Bc J[ ;; (''
5
<
<""A'( (( [[ A))JVD;;Q?A 	AAs/   F 'G G'0GGH(30H##H(c                     [         R                  " UR                  UR                  5      (       d)  [        R
                  " [        R                  " 5       5      eg)z&Verifies integrity fields in response.N)r   Crc32cMatches	plaintextplaintextCrc32cr	   $ClientSideIntegrityVerificationError*GetResponseFromServerCorruptedErrorMessage)r"   rE   resps      r   _VerifyResponseIntegrityFields&Decrypt._VerifyResponseIntegrityFields   sH     0D0DEE>>

B
B
DF F Fr   c                    U R                  U5      n[        R                  " 5       n UR                  R	                  U5      nU R                  U5      (       a  U R                  UW5         WR                  cA  [        R                  " UR                  5          S S S 5        [        R                   " S5        g [        R"                  " UR                  UR                  SSS9  g ! [
        R                   a   n[        R                  " U5         S nANS nAff = f! , (       d  f       N= f! [        R$                   a  n[&        R(                  " U5      eS nAff = f)NzDecrypted file is emptyT)r   	overwrite)rH   r:   GetClientInstance&projects_locations_keyRings_cryptoKeysr   apitools_exceptionsHttpBadRequestErrorr	   ProcessHttpBadRequestErrorr+   rQ   rL   r   
FileWriterplaintext_filer   PrintWriteToFileOrStdoutr7   r   r    )r"   r*   rE   clientrP   errorrA   s          r   RunDecrypt.Run   s   

$
$T
*C,,.F6::BB3Gd ))$//
))#t4
+		d112
 3 			+,	N 22 6..u556 32 ;; +''**+sM   C& ,-D. DD. :+D. &D:DD
D+'D. .EEE N)__name__
__module____qualname____firstlineno____doc__staticmethodr   r&   r+   rH   rQ   r`   __static_attributes__rb   r   r   r   r   !   s7    1f / /$0;zF+r   r   N)rg   
__future__r   r   r   apitools.base.pyr   rW   googlecloudsdk.api_lib.cloudkmsr   r:   googlecloudsdk.calliopegooglecloudsdk.command_lib.kmsr   r	   r
   googlecloudsdk.corer   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   Commandr   rb   r   r   <module>rs      sC    - &  ' > A ( . 1 8 0 # 2 *n+dll n+r   