
                             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
\R                   5      rg)z5Verify a user signature file using a MAC signing key.    )
exceptions)base)crc32c)e2e_integrity)flags)log)
console_ioc                   F    \ rS rSrSr\S 5       rS rS rS r	S r
S rS	rg
)	MacVerify   a  Verify a user signature file using a MAC key version.

Verifies a digital signature using the provided MAC signing key version.

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
The following command will read the file '/tmp/my/file.to.verify', and verify
it using the symmetric MAC CryptoKey `dont-panic` Version 3 and the file
used previously to generate the MAC tag ('/tmp/my/original.data.file').

  $ {command} \
  --location=us-central1 \
  --keyring=hitchhiker \
  --key=dont-panic \
  --version=3 \
  --input-file=/tmp/my/original.data.file \
  --signature-file=/tmp/my/file.to.verify

c                     [         R                  " U S5        [         R                  " U S5        [         R                  " U S5        [         R                  " U S5        [         R
                  " U 5        g )Nzto use for signing.zto use for signingzto use for verificationzto be verified)r   AddKeyResourceFlagsAddCryptoKeyVersionFlagAddInputFileFlagAddSignatureFileFlagAddSkipIntegrityVerification)parsers    lib/surface/kms/mac_verify.pyArgsMacVerify.Args5   sT    	f&;<	!!&*>?	6#<=	v'78	&&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MacVerify._ReadFileOrStdin=   sK    ))$t<D
4y9''
H
O
O    Kr   c                 $    UR                   (       + $ )N)skip_integrity_verification)r   argss     r   _PerformIntegrityVerification'MacVerify._PerformIntegrityVerificationE   s    ////r   c                     U R                  UR                  SS9n U R                  UR                  SS9n[        R                  " 5       nUR                  [        R                  " U5      R                  5       S9nU R                  U5      (       aC  [        R                  " U5      n[        R                  " U5      nUR!                  X$XxS9Ul        U$ UR!                  X$S9Ul        U$ ! [         a5  n[        R                  " SR                  UR                  U5      5      eS nAff = f! [         a5  n[        R                  " SR                  UR                  U5      5      eS nAff = f)Ni   )r!   z$Failed to read input file [{0}]: {1}@   )name)r"   mac
dataCrc32c	macCrc32c)r"   r-   )r#   
input_fileEnvironmentErrorr   r   r   signature_filecloudkms_baseGetMessagesModuleLCloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsMacVerifyRequestr   ParseCryptoKeyVersionNameRelativeNamer(   r   Crc32cMacVerifyRequestmacVerifyRequest)	r   r'   r"   er-   messagesreqdata_crc32c
mac_crc32cs	            r   _CreateMacVerifyRequest!MacVerify._CreateMacVerifyRequestH   sj   M""4??e"DdM!!$"5"5!Dc
 ..0H

_
_,,T2??A ` CC ))$//MM$'k==%j%66 7 Lc
 J &66D6JcJ-  M''
0
7
7
KM MM  M''
0
7
7
KM MMs.   C, D. ,
D+60D&&D+.
E-80E((E-c                 $   UR                   UR                   :w  a?  [        R                  " [        R                  " UR                   UR                   5      5      eUR                  (       d)  [        R
                  " [        R                  " 5       5      eUR                  (       d)  [        R
                  " [        R                  " 5       5      eUR                  UR                  :w  a)  [        R
                  " [        R                  " 5       5      eg)z/Verifies integrity fields in MacVerifyResponse.N)r,   r   ResourceNameVerificationError#GetResourceNameMismatchErrorMessageverifiedDataCrc32c$ClientSideIntegrityVerificationError'GetRequestToServerCorruptedErrorMessageverifiedMacCrc32csuccessverifiedSuccessIntegrity*GetResponseFromServerCorruptedErrorMessage)r   r=   resps      r   _VerifyResponseIntegrityFields(MacVerify._VerifyResponseIntegrityFieldsd   s     xx49977

;
;hh		#$ $
 "">>

?
?
AC C !!>>

?
?
AC C ||t444>>

B
B
DF F 5r   c                    [         R                  " 5       nU R                  U5      n UR                  R	                  U5      nU R                  U5      (       a  U R                  UW5        [        R                  " SWR                  SS9  g ! [
        R                   a   n[        R                  " U5         S nANxS nAff = f)N-Fr   )r3   GetClientInstancer@   8projects_locations_keyRings_cryptoKeys_cryptoKeyVersionsr   apitools_exceptionsHttpBadRequestErrorr   ProcessHttpBadRequestErrorr(   rM   r   WriteToFileOrStdoutrI   )r   r'   clientr=   rL   errors         r   RunMacVerify.Run|   s    ,,.F

&
&t
,C6

I
I9S>  ))$//
))#t4 22 6..u556s   B C  B;;C  N)__name__
__module____qualname____firstlineno____doc__staticmethodr   r#   r(   r@   rM   rY   __static_attributes__r[   r   r   r   r      s5    . / /08F0r   r   N)r`   apitools.base.pyr   rS   googlecloudsdk.api_lib.cloudkmsr   r3   googlecloudsdk.calliopegooglecloudsdk.command_lib.kmsr   r   r   googlecloudsdk.corer   googlecloudsdk.core.consoler	   Commandr   r[   r   r   <module>rj      s9    < ? A ( . 1 8 0 # 2q qr   