| 1 |
<?php |
| 2 |
namespace Aws\Crypto; |
| 3 |
|
| 4 |
use Aws\Exception\CryptoException; |
| 5 |
use Aws\Kms\KmsClient; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class KmsMaterialsProviderV3 extends MaterialsProviderV3 implements MaterialsProviderInterfaceV3 |
| 13 |
{ |
| 14 |
const WRAP_ALGORITHM_NAME = 'kms+context'; |
| 15 |
private KmsClient $kmsClient; |
| 16 |
private ?string $kmsKeyId; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function __construct( |
| 25 |
KmsClient $kmsClient, |
| 26 |
?string $kmsKeyId = null |
| 27 |
) { |
| 28 |
$this->kmsClient = $kmsClient; |
| 29 |
$this->kmsKeyId = $kmsKeyId; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function getWrapAlgorithmName(): string |
| 36 |
{ |
| 37 |
return self::WRAP_ALGORITHM_NAME; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function decryptCek( |
| 44 |
string $encryptedCek, |
| 45 |
array $materialDescription, |
| 46 |
array $options |
| 47 |
): string |
| 48 |
{ |
| 49 |
$options = array_change_key_case($options); |
| 50 |
$encryptionContext = null; |
| 51 |
|
| 52 |
|
| 53 |
if (!isset($options['@kmsencryptioncontext'])) { |
| 54 |
$encryptionContext = $materialDescription; |
| 55 |
} else { |
| 56 |
|
| 57 |
|
| 58 |
if (!is_array($options['@kmsencryptioncontext'])) { |
| 59 |
throw new CryptoException("'When using @KmsMaterialsProviderV3, it" |
| 60 |
. " must be an associative array (or empty array)."); |
| 61 |
} |
| 62 |
|
| 63 |
if (isset($options['@kmsencryptioncontext']['aws:x-amz-cek-alg'])) { |
| 64 |
throw new CryptoException("Conflict in reserved @KmsEncryptionContext" |
| 65 |
. " key aws:x-amz-cek-alg. This value is reserved for the S3" |
| 66 |
. " Encryption Client and cannot be set by the user."); |
| 67 |
} |
| 68 |
|
| 69 |
if (isset($options['@kmsencryptioncontext']['kms_cmk_id'])) { |
| 70 |
throw new CryptoException("Conflict in reserved @KmsEncryptionContext" |
| 71 |
. " key kms_cmk_id. This value is reserved for the S3" |
| 72 |
. " Encryption Client and cannot be set by the user."); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
$materialsDescriptionContextCopy = $materialDescription; |
| 80 |
unset($materialsDescriptionContextCopy["aws:x-amz-cek-alg"]); |
| 81 |
unset($materialsDescriptionContextCopy["kms_cmk_id"]); |
| 82 |
|
| 83 |
$requestEncryptionContext = $options['@kmsencryptioncontext']; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if ($materialsDescriptionContextCopy !== $requestEncryptionContext) { |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
throw new CryptoException("Provided encryption context does not match information retrieved from S3"); |
| 92 |
} |
| 93 |
$encryptionContext = $materialDescription; |
| 94 |
} |
| 95 |
$params = [ |
| 96 |
'CiphertextBlob' => $encryptedCek, |
| 97 |
'EncryptionContext' => $encryptionContext |
| 98 |
]; |
| 99 |
|
| 100 |
if (empty($options['@kmsallowdecryptwithanycmk'])) { |
| 101 |
if (empty($this->kmsKeyId)) { |
| 102 |
throw new CryptoException('KMS CMK ID was not specified and the' |
| 103 |
. ' operation is not opted-in to attempting to use any valid' |
| 104 |
. ' CMK it discovers. Please specify a CMK ID, or explicitly' |
| 105 |
. ' enable attempts to use any valid KMS CMK with the' |
| 106 |
. ' @KmsAllowDecryptWithAnyCmk option.'); |
| 107 |
} |
| 108 |
$params['KeyId'] = $this->kmsKeyId; |
| 109 |
} |
| 110 |
|
| 111 |
$result = $this->kmsClient->decrypt($params); |
| 112 |
|
| 113 |
return $result['Plaintext']; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function generateCek($keySize, $context, $options): array |
| 120 |
{ |
| 121 |
if (empty($this->kmsKeyId)) { |
| 122 |
throw new CryptoException('A KMS key id is required for encryption' |
| 123 |
. ' with KMS keywrap. Use a KmsMaterialsProviderV2 that has been' |
| 124 |
. ' instantiated with a KMS key id.'); |
| 125 |
} |
| 126 |
$options = array_change_key_case($options); |
| 127 |
if (!isset($options['@kmsencryptioncontext']) |
| 128 |
|| !is_array($options['@kmsencryptioncontext']) |
| 129 |
) { |
| 130 |
throw new CryptoException("'@KmsEncryptionContext' is a" |
| 131 |
. " required argument when using KmsMaterialsProviderV3, and" |
| 132 |
. " must be an associative array (or empty array)."); |
| 133 |
} |
| 134 |
|
| 135 |
if (isset($options['@kmsencryptioncontext']['aws:x-amz-cek-alg'])) { |
| 136 |
throw new CryptoException("Conflict in reserved @KmsEncryptionContext" |
| 137 |
. " key aws:x-amz-cek-alg. This value is reserved for the S3" |
| 138 |
. " Encryption Client and cannot be set by the user."); |
| 139 |
} |
| 140 |
|
| 141 |
if (isset($options['@kmsencryptioncontext']['kms_cmk_id'])) { |
| 142 |
throw new CryptoException("Conflict in reserved @KmsEncryptionContext" |
| 143 |
. " key kms_cmk_id. This value is reserved for the S3" |
| 144 |
. " Encryption Client and cannot be set by the user."); |
| 145 |
} |
| 146 |
$context = array_merge($options['@kmsencryptioncontext'], $context); |
| 147 |
$result = $this->kmsClient->generateDataKey([ |
| 148 |
'KeyId' => $this->kmsKeyId, |
| 149 |
'KeySpec' => "AES_{$keySize}", |
| 150 |
'EncryptionContext' => $context |
| 151 |
]); |
| 152 |
|
| 153 |
return [ |
| 154 |
'Plaintext' => $result['Plaintext'], |
| 155 |
'Ciphertext' => base64_encode($result['CiphertextBlob']), |
| 156 |
'UpdatedContext' => $context |
| 157 |
]; |
| 158 |
} |
| 159 |
} |