Skip to content

Commit 8564447

Browse files
committed
fix c++?
1 parent 91933b7 commit 8564447

2 files changed

Lines changed: 87 additions & 58 deletions

File tree

test-server/cpp-v2-transition-server/main.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <aws/s3-encryption/S3EncryptionClient.h>
55
#include <aws/s3-encryption/materials/KMSEncryptionMaterials.h>
66
#include <aws/s3-encryption/materials/SimpleEncryptionMaterials.h>
7-
#include <aws/core/utils/base64/Base64.h>
7+
#include <aws/core/utils/HashingUtils.h>
88
#include <aws/s3/model/GetObjectRequest.h>
99
#include <aws/s3/model/PutObjectRequest.h>
1010
#include <microhttpd.h>
@@ -141,40 +141,48 @@ MHD_Result handle_create_client(struct MHD_Connection *connection,
141141
inst_put = request["config"]["instructionFileConfig"]["enableInstructionFilePutObject"];
142142
}
143143

144-
// Create appropriate encryption materials based on key type
145-
std::shared_ptr<Aws::S3Encryption::Materials::EncryptionMaterials> materials;
144+
// Create CryptoConfigurationV2 and S3EncryptionClientV2 based on key type
145+
std::shared_ptr<S3EncryptionClientV2> encryption_client;
146146

147147
if (!aes_key_blob.empty()) {
148148
// Base64 decode the AES key
149-
auto decoded = Aws::Utils::Base64::Decode(aes_key_blob);
150-
if (!decoded.IsSuccess()) {
149+
Aws::Utils::ByteBuffer decoded = Aws::Utils::HashingUtils::Base64Decode(aes_key_blob);
150+
if (decoded.GetLength() == 0) {
151151
return send_response(connection, 400,
152152
"{\"error\":\"Failed to decode AES key\"}");
153153
}
154154

155155
Aws::Utils::CryptoBuffer key_buffer(
156-
decoded.GetResult().GetUnderlyingData(),
157-
decoded.GetResult().GetLength()
156+
decoded.GetUnderlyingData(),
157+
decoded.GetLength()
158158
);
159159

160-
materials = std::make_shared<
160+
auto materials = std::make_shared<
161161
Aws::S3Encryption::Materials::SimpleEncryptionMaterialsWithGCMAAD>(
162162
key_buffer
163163
);
164+
CryptoConfigurationV2 config(materials);
165+
166+
if (legacy1 || legacy2)
167+
config.SetSecurityProfile(SecurityProfile::V2_AND_LEGACY);
168+
if (inst_put)
169+
config.SetStorageMethod(StorageMethod::INSTRUCTION_FILE);
170+
171+
encryption_client = std::make_shared<S3EncryptionClientV2>(config);
164172
} else if (!kms_key_id.empty()) {
165-
materials = std::make_shared<KMSWithContextEncryptionMaterials>(kms_key_id);
173+
auto materials = std::make_shared<KMSWithContextEncryptionMaterials>(kms_key_id);
174+
CryptoConfigurationV2 config(materials);
175+
176+
if (legacy1 || legacy2)
177+
config.SetSecurityProfile(SecurityProfile::V2_AND_LEGACY);
178+
if (inst_put)
179+
config.SetStorageMethod(StorageMethod::INSTRUCTION_FILE);
180+
181+
encryption_client = std::make_shared<S3EncryptionClientV2>(config);
166182
} else {
167183
return send_response(connection, 400,
168184
"{\"error\":\"No valid key material provided\"}");
169185
}
170-
171-
CryptoConfigurationV2 config(materials);
172-
if (legacy1 || legacy2)
173-
config.SetSecurityProfile(SecurityProfile::V2_AND_LEGACY);
174-
if (inst_put)
175-
config.SetStorageMethod(StorageMethod::INSTRUCTION_FILE);
176-
177-
auto encryption_client = std::make_shared<S3EncryptionClientV2>(config);
178186

179187
std::string client_id = generate_uuid();
180188
set_client(client_id, encryption_client);

test-server/cpp-v3-server/main.cpp

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <aws/s3-encryption/S3EncryptionClient.h>
55
#include <aws/s3-encryption/materials/KMSEncryptionMaterials.h>
66
#include <aws/s3-encryption/materials/SimpleEncryptionMaterials.h>
7-
#include <aws/core/utils/base64/Base64.h>
7+
#include <aws/core/utils/HashingUtils.h>
88
#include <aws/s3/model/GetObjectRequest.h>
99
#include <aws/s3/model/PutObjectRequest.h>
1010
#include <microhttpd.h>
@@ -133,64 +133,85 @@ MHD_Result handle_create_client(struct MHD_Connection *connection,
133133
inst_put = request["config"]["instructionFileConfig"]["enableInstructionFilePutObject"];
134134
}
135135

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;
138141

139142
if (!aes_key_blob.empty()) {
140143
// 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) {
143146
return send_response(connection, 400,
144147
"{\"error\":\"Failed to decode AES key\"}");
145148
}
146149

147150
Aws::Utils::CryptoBuffer key_buffer(
148-
decoded.GetResult().GetUnderlyingData(),
149-
decoded.GetResult().GetLength()
151+
decoded.GetUnderlyingData(),
152+
decoded.GetLength()
150153
);
151154

152-
materials = std::make_shared<
155+
auto materials = std::make_shared<
153156
Aws::S3Encryption::Materials::SimpleEncryptionMaterialsWithGCMAAD>(
154157
key_buffer
155158
);
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);
156184
} 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);
158211
} else {
159212
return send_response(connection, 400,
160213
"{\"error\":\"No valid key material provided\"}");
161214
}
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);
194215

195216
std::string client_id = generate_uuid();
196217
set_client(client_id, encryption_client);

0 commit comments

Comments
 (0)