Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Crypto/KmsMaterialsProviderV3.php

raw symbols SCIP
1 <?php
2 namespace Aws\Crypto;
3
4 use Aws\Exception\CryptoException;
5 use Aws\Kms\KmsClient;
6
7 /**
8 * Uses KMS to supply materials for encrypting and decrypting data. This
9 * V2 implementation should be used with the V2 encryption clients (i.e.
10 * S3EncryptionClientV2).
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 * @param KmsClient $kmsClient A KMS Client for use encrypting and
20 * decrypting keys.
21 * @param string $kmsKeyId The private KMS key id to be used for encrypting
22 * and decrypting keys.
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 * @inheritDoc
34 */
35 public function getWrapAlgorithmName(): string
36 {
37 return self::WRAP_ALGORITHM_NAME;
38 }
39
40 /**
41 * @inheritDoc
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 // no encryption context was provided we will use the one in the metadata
53 if (!isset($options['@kmsencryptioncontext'])) {
54 $encryptionContext = $materialDescription;
55 } else {
56 // encryption context was passed in, we have to make sure it is an array
57 // and that the reserved keywords were not used
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 //= specification/s3-encryption/materials/s3-kms-keyring.md#kms-context
75 //= type=implication
76 //# When decrypting using Kms+Context mode, the KmsKeyring MUST validate the provided (request) encryption context with the stored (materials) encryption context.
77 // We are validating the encryption context to match S3EC V2 behavior
78 // Refer to KMSMaterialsHandler in the V2 client for details
79 $materialsDescriptionContextCopy = $materialDescription;
80 unset($materialsDescriptionContextCopy["aws:x-amz-cek-alg"]);
81 unset($materialsDescriptionContextCopy["kms_cmk_id"]);
82
83 $requestEncryptionContext = $options['@kmsencryptioncontext'];
84 //= specification/s3-encryption/materials/s3-kms-keyring.md#kms-context
85 //= type=implication
86 //# The stored encryption context with the two reserved keys removed MUST match the provided encryption context.
87 if ($materialsDescriptionContextCopy !== $requestEncryptionContext) {
88 //= specification/s3-encryption/materials/s3-kms-keyring.md#kms-context
89 //= type=implication
90 //# If the stored encryption context with the two reserved keys removed does not match the provided encryption context, the KmsKeyring MUST throw an exception.
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 * @inheritDoc
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 }