|
4 | 4 | #include <aws/s3-encryption/S3EncryptionClient.h> |
5 | 5 | #include <aws/s3-encryption/materials/KMSEncryptionMaterials.h> |
6 | 6 | #include <aws/s3-encryption/materials/SimpleEncryptionMaterials.h> |
7 | | -#include <aws/core/utils/base64/Base64.h> |
| 7 | +#include <aws/core/utils/HashingUtils.h> |
8 | 8 | #include <aws/s3/model/GetObjectRequest.h> |
9 | 9 | #include <aws/s3/model/PutObjectRequest.h> |
10 | 10 | #include <microhttpd.h> |
@@ -133,64 +133,85 @@ MHD_Result handle_create_client(struct MHD_Connection *connection, |
133 | 133 | inst_put = request["config"]["instructionFileConfig"]["enableInstructionFilePutObject"]; |
134 | 134 | } |
135 | 135 |
|
136 | | - // Create appropriate encryption materials based on key type |
137 | | - std::shared_ptr<Aws::S3Encryption::Materials::EncryptionMaterials> materials; |
| 136 | + std::string commitmentPolicy = get_config(request, "commitmentPolicy"); |
| 137 | + std::string encryptionAlgorithm = get_config(request, "encryptionAlgorithm"); |
| 138 | + |
| 139 | + // Create CryptoConfigurationV3 and S3EncryptionClientV3 based on key type |
| 140 | + std::shared_ptr<S3EncryptionClientV3> encryption_client; |
138 | 141 |
|
139 | 142 | if (!aes_key_blob.empty()) { |
140 | 143 | // Base64 decode the AES key |
141 | | - auto decoded = Aws::Utils::Base64::Decode(aes_key_blob); |
142 | | - if (!decoded.IsSuccess()) { |
| 144 | + Aws::Utils::ByteBuffer decoded = Aws::Utils::HashingUtils::Base64Decode(aes_key_blob); |
| 145 | + if (decoded.GetLength() == 0) { |
143 | 146 | return send_response(connection, 400, |
144 | 147 | "{\"error\":\"Failed to decode AES key\"}"); |
145 | 148 | } |
146 | 149 |
|
147 | 150 | Aws::Utils::CryptoBuffer key_buffer( |
148 | | - decoded.GetResult().GetUnderlyingData(), |
149 | | - decoded.GetResult().GetLength() |
| 151 | + decoded.GetUnderlyingData(), |
| 152 | + decoded.GetLength() |
150 | 153 | ); |
151 | 154 |
|
152 | | - materials = std::make_shared< |
| 155 | + auto materials = std::make_shared< |
153 | 156 | Aws::S3Encryption::Materials::SimpleEncryptionMaterialsWithGCMAAD>( |
154 | 157 | key_buffer |
155 | 158 | ); |
| 159 | + CryptoConfigurationV3 config(materials); |
| 160 | + |
| 161 | + if (legacy1 || legacy2) |
| 162 | + config.AllowLegacy(); |
| 163 | + if (inst_put) |
| 164 | + config.SetStorageMethod(StorageMethod::INSTRUCTION_FILE); |
| 165 | + |
| 166 | + if (commitmentPolicy == "REQUIRE_ENCRYPT_REQUIRE_DECRYPT") { |
| 167 | + if (encryptionAlgorithm == "ALG_AES_256_GCM_IV12_TAG16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
| 168 | + if (encryptionAlgorithm == "ALG_AES_256_CBC_IV16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
| 169 | + config.SetCommitmentPolicy(CommitmentPolicy::REQUIRE_ENCRYPT_REQUIRE_DECRYPT); |
| 170 | + } else if (commitmentPolicy == "REQUIRE_ENCRYPT_ALLOW_DECRYPT") { |
| 171 | + if (encryptionAlgorithm == "ALG_AES_256_GCM_IV12_TAG16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
| 172 | + config.SetCommitmentPolicy(CommitmentPolicy::REQUIRE_ENCRYPT_ALLOW_DECRYPT); |
| 173 | + } else if (commitmentPolicy == "FORBID_ENCRYPT_ALLOW_DECRYPT") { |
| 174 | + if (encryptionAlgorithm == "ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
| 175 | + config.SetCommitmentPolicy(CommitmentPolicy::FORBID_ENCRYPT_ALLOW_DECRYPT); |
| 176 | + } |
| 177 | + |
| 178 | + // Configure ClientConfiguration with retry strategy for throttling |
| 179 | + Aws::Client::ClientConfiguration clientConfig; |
| 180 | + clientConfig.maxConnections = 25; |
| 181 | + clientConfig.retryStrategy = Aws::Client::InitRetryStrategy("standard"); |
| 182 | + |
| 183 | + encryption_client = std::make_shared<S3EncryptionClientV3>(config, clientConfig); |
156 | 184 | } else if (!kms_key_id.empty()) { |
157 | | - materials = std::make_shared<KMSWithContextEncryptionMaterials>(kms_key_id); |
| 185 | + auto materials = std::make_shared<KMSWithContextEncryptionMaterials>(kms_key_id); |
| 186 | + CryptoConfigurationV3 config(materials); |
| 187 | + |
| 188 | + if (legacy1 || legacy2) |
| 189 | + config.AllowLegacy(); |
| 190 | + if (inst_put) |
| 191 | + config.SetStorageMethod(StorageMethod::INSTRUCTION_FILE); |
| 192 | + |
| 193 | + if (commitmentPolicy == "REQUIRE_ENCRYPT_REQUIRE_DECRYPT") { |
| 194 | + if (encryptionAlgorithm == "ALG_AES_256_GCM_IV12_TAG16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
| 195 | + if (encryptionAlgorithm == "ALG_AES_256_CBC_IV16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
| 196 | + config.SetCommitmentPolicy(CommitmentPolicy::REQUIRE_ENCRYPT_REQUIRE_DECRYPT); |
| 197 | + } else if (commitmentPolicy == "REQUIRE_ENCRYPT_ALLOW_DECRYPT") { |
| 198 | + if (encryptionAlgorithm == "ALG_AES_256_GCM_IV12_TAG16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
| 199 | + config.SetCommitmentPolicy(CommitmentPolicy::REQUIRE_ENCRYPT_ALLOW_DECRYPT); |
| 200 | + } else if (commitmentPolicy == "FORBID_ENCRYPT_ALLOW_DECRYPT") { |
| 201 | + if (encryptionAlgorithm == "ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
| 202 | + config.SetCommitmentPolicy(CommitmentPolicy::FORBID_ENCRYPT_ALLOW_DECRYPT); |
| 203 | + } |
| 204 | + |
| 205 | + // Configure ClientConfiguration with retry strategy for throttling |
| 206 | + Aws::Client::ClientConfiguration clientConfig; |
| 207 | + clientConfig.maxConnections = 25; |
| 208 | + clientConfig.retryStrategy = Aws::Client::InitRetryStrategy("standard"); |
| 209 | + |
| 210 | + encryption_client = std::make_shared<S3EncryptionClientV3>(config, clientConfig); |
158 | 211 | } else { |
159 | 212 | return send_response(connection, 400, |
160 | 213 | "{\"error\":\"No valid key material provided\"}"); |
161 | 214 | } |
162 | | - |
163 | | - // Configure ClientConfiguration with retry strategy for throttling |
164 | | - Aws::Client::ClientConfiguration clientConfig; |
165 | | - clientConfig.maxConnections = 25; |
166 | | - clientConfig.retryStrategy = Aws::MakeShared<Aws::Client::DefaultRetryStrategy>( |
167 | | - "S3EncryptionClient", |
168 | | - 5 // maxRetries - will use exponential backoff for throttling |
169 | | - ); |
170 | | - |
171 | | - CryptoConfigurationV3 config(materials); |
172 | | - config.SetClientConfiguration(clientConfig); |
173 | | - if (legacy1 || legacy2) |
174 | | - config.AllowLegacy(); |
175 | | - if (inst_put) |
176 | | - config.SetStorageMethod(StorageMethod::INSTRUCTION_FILE); |
177 | | - |
178 | | - std::string commitmentPolicy = get_config(request, "commitmentPolicy"); |
179 | | - std::string encryptionAlgorithm = get_config(request, "encryptionAlgorithm"); |
180 | | - |
181 | | - if (commitmentPolicy == "REQUIRE_ENCRYPT_REQUIRE_DECRYPT") { |
182 | | - if (encryptionAlgorithm == "ALG_AES_256_GCM_IV12_TAG16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
183 | | - if (encryptionAlgorithm == "ALG_AES_256_CBC_IV16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
184 | | - config.SetCommitmentPolicy(CommitmentPolicy::REQUIRE_ENCRYPT_REQUIRE_DECRYPT); |
185 | | - } else if (commitmentPolicy == "REQUIRE_ENCRYPT_ALLOW_DECRYPT") { |
186 | | - if (encryptionAlgorithm == "ALG_AES_256_GCM_IV12_TAG16_NO_KDF") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
187 | | - config.SetCommitmentPolicy(CommitmentPolicy::REQUIRE_ENCRYPT_ALLOW_DECRYPT); |
188 | | - } else if (commitmentPolicy == "FORBID_ENCRYPT_ALLOW_DECRYPT") { |
189 | | - if (encryptionAlgorithm == "ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY") return unsupported(connection, commitmentPolicy, encryptionAlgorithm); |
190 | | - config.SetCommitmentPolicy(CommitmentPolicy::FORBID_ENCRYPT_ALLOW_DECRYPT); |
191 | | - } |
192 | | - |
193 | | - auto encryption_client = std::make_shared<S3EncryptionClientV3>(config); |
194 | 215 |
|
195 | 216 | std::string client_id = generate_uuid(); |
196 | 217 | set_client(client_id, encryption_client); |
|
0 commit comments