6

我可以通过 Java SDK 添加客户端加密文件,也可以获取该文件。我现在正在尝试使用 boto3 访问它。(我知道 boto3 不支持此功能,但 s3-wrapper 支持。不过,这涉及 boto3)。

我正在获取 s3 元数据,然后像这样调用 kms.decrypt:

object_info = s3.head_object(Bucket=bucket_name, Key=key_name)
metadata = object_info['Metadata'] # metadata is a dict with lots of x-amz-key, x-amz-iv, etc
ekey = kms.decrypt(CiphertextBlob=metadata,EncryptionContext=metadata)

# fails with:
# ParamValidationError:
# Parameter validation failed: Invalid type for parameter CiphertextBlob, value: .. type: <class 'dict'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object`

那么,如果我应该将密钥作为 CiphertextBlob 传递怎么办?

# this key looks like 'CiAlgoyM4k...
ekey = kms.decrypt(CiphertextBlob=metadata['x-amz-key-v2'],EncryptionContext=metadata)

# fails with:
# botocore.exceptions.ClientError: An error occurred (InvalidCiphertextException) when calling the Decrypt operation: None

然后我尝试传入一个base64的密钥:

# this key looks like b'\n %\x82\x8c\x8c\xe2ML...
cblob = base64.b64decode(metadata['x-amz-key-v2'])
ekey = kms.decrypt(CiphertextBlob=cblob,EncryptionContext=metadata)

# (still) fails with:
# botocore.exceptions.ClientError: An error occurred (InvalidCiphertextException) when calling the Decrypt operation: None

然后我尝试将 s3 内容作为 blob 传递。

full_object = s3.get_object(Bucket=bucket_name, Key=key_name)
ekey = kms.decrypt(CiphertextBlob=full_object['Body'].read(),EncryptionContext=metadata)

# Still fails with:
# botocore.exceptions.ClientError: An error occurred (InvalidCiphertextException) when calling the Decrypt operation: None

所以,我想我的问题是,什么是 S3 元数据中正确的 CiphertextBlob?由于我将它与 Java SDK 的EncryptedPutObjectRequestAmazonS3EncryptionClient 放在一起,将它抽象出来,我不知道那个 blob 应该是什么样子。

相关链接

4

2 回答 2

2

根据您的评论,我几乎可以肯定您使用信封加密而不是客户主密钥 ( # metadata is a dict with lots of x-amz-key, x-amz-iv, etc) 对文件进行了加密。另一个问题是你传递了一个加密上下文,但总是让它成为整个字典。这是你的意图吗?

所以,我的建议是:

  1. 确保调用kms.decrypt信封密钥,然后使用解密的密钥实际解密数据(假设我上面的评论是正确的)。
  2. 仔细检查您是否在加密上下文中传递了您想要的内容。
于 2017-05-01T19:23:08.510 回答
0

如果要使用EncryptionContext选项,请先在加密时添加“Encryption Context”选项

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/kms.html#KMS.Client.encrypt

于 2020-07-29T07:08:56.470 回答