
    b                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z
 ddlm	Z	 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ  G d de	j(                        Zy)z5Verify a user signature file using a MAC signing key.    )absolute_import)division)unicode_literals)
exceptions)base)crc32c)e2e_integrity)flags)log)
console_ioc                   >    e Zd ZdZed        Zd Zd Zd Zd Z	d Z
y)		MacVerifya  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                     t        j                  | d       t        j                  | d       t        j                  | d       t        j                  | d       t        j
                  |        y )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ArgszMacVerify.Args8   sT    	f&;<	!!&*>?	6#<=	v'78	&&v.    c                     t        j                  |d      }t        |      |kD  r%t        j                  dj                  ||            |S )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   _ReadFileOrStdinzMacVerify._ReadFileOrStdin@   sM    ))$t<D
4y9''
H
O
OI    Kr   c                     |j                    S )N)skip_integrity_verification)r    argss     r   _PerformIntegrityVerificationz'MacVerify._PerformIntegrityVerificationH   s    ////r   c                    	 | j                  |j                  d      }	 | j                  |j                  d      }t        j                         }|j                  t        j                  |      j                               }| j                  |      rFt        j                  |      }t        j                  |      }|j!                  ||||      |_        |S |j!                  ||      |_        |S # t        $ r4}t        j                  dj                  |j                  |            d }~ww xY w# t        $ r4}t        j                  dj                  |j                  |            d }~ww xY w)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   _CreateMacVerifyRequestz!MacVerify._CreateMacVerifyRequestK   sm   M""4??e"DdM!!$"5"5!Dc
 ..0H

_
_,,T2??A ` CC ))$/MM$'k==%j%66
 7 Lc
 J &66Dc6JcJ-  M''
0
7
7
KM MM  M''
0
7
7
KM MMs.   C5 D5 5	D2>/D--D25	E2>/E--E2c                    |j                   |j                   k7  r=t        j                  t        j                  |j                   |j                               |j                  s't        j
                  t        j                               |j                  s't        j
                  t        j                               |j                  |j                  k7  r't        j
                  t        j                               y)z/Verifies integrity fields in MacVerifyResponse.N)r+   r	   ResourceNameVerificationError#GetResourceNameMismatchErrorMessageverifiedDataCrc32c$ClientSideIntegrityVerificationError'GetRequestToServerCorruptedErrorMessageverifiedMacCrc32csuccessverifiedSuccessIntegrity*GetResponseFromServerCorruptedErrorMessage)r    r<   resps      r   _VerifyResponseIntegrityFieldsz(MacVerify._VerifyResponseIntegrityFieldsg   s     xx49977

;
;hh		#$ $
 "">>

?
?
AC C !!>>

?
?
AC C ||t444>>

B
B
DF F 5r   c                 z   t        j                         }| j                  |      }	 |j                  j	                  |      }| j                  |      r| j                  |       t        j                  dj                  d       y # t
        j                  $ r}t        j                  |       Y d }~sd }~ww xY w)N-Fr   )r2   GetClientInstancer?   8projects_locations_keyRings_cryptoKeys_cryptoKeyVersionsr   apitools_exceptionsHttpBadRequestErrorr	   ProcessHttpBadRequestErrorr(   rK   r   WriteToFileOrStdoutrG   )r    r'   clientr<   rJ   errors         r   RunzMacVerify.Run   s    ,,.F

&
&t
,C6

I
I9S>  ))$/
))#t4 22 6..u556s   B B:B55B:N)__name__
__module____qualname____doc__staticmethodr   r$   r(   r?   rK   rV    r   r   r   r       s5    . / /08F0r   r   N)rZ   
__future__r   r   r   apitools.base.pyr   rP   googlecloudsdk.api_lib.cloudkmsr   r2   googlecloudsdk.calliopegooglecloudsdk.command_lib.kmsr   r	   r
   googlecloudsdk.corer   googlecloudsdk.core.consoler   Commandr   r\   r   r   <module>re      s@    < &  ' > A ( . 1 8 0 # 2q qr   