cse_5.py 834 B

1234567891011121314151617181920
  1. def decrypt(file_input, s3_metadata, kms_client):
  2. """
  3. Method to decrypt an S3 object with KMS based Client-side encryption (CSE).
  4. The object's metadata is used to fetch the encryption envelope such as
  5. the KMS key ID and the algorithm.
  6. """
  7. logger.info("Decrypting Object with CSE-KMS")
  8. alg = s3_metadata.get(HEADER_ALG, None)
  9. iv = base64.b64decode(s3_metadata[HEADER_IV])
  10. material_description = json.loads(s3_metadata[HEADER_MATDESC])
  11. key = s3_metadata[HEADER_KEY]
  12. decryption_key = base64.b64decode(key)
  13. aes_key = get_decryption_aes_key(decryption_key, material_description, kms_client)
  14. content = file_input.read()
  15. decrypted = (
  16. decrypt_gcm(content, aes_key, iv)
  17. if alg == ALG_GCM
  18. else decrypt_cbc(content, aes_key, iv)
  19. )
  20. return BytesIO(decrypted)