forked from aws/aws-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptoModulesTest.cpp
874 lines (702 loc) · 43.3 KB
/
CryptoModulesTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/*
* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#ifndef NO_SYMMETRIC_ENCRYPTION
#include <aws/external/gtest.h>
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/utils/Outcome.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/utils/StringUtils.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/utils/crypto/ContentCryptoScheme.h>
#include <aws/core/utils/crypto/CryptoStream.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/utils/Array.h>
#include <aws/s3-encryption/modules/CryptoModuleFactory.h>
#include <aws/s3-encryption/materials/KMSEncryptionMaterials.h>
#include <aws/s3-encryption/materials/SimpleEncryptionMaterials.h>
#include <aws/s3-encryption/CryptoConfiguration.h>
#include <aws/s3-encryption/handlers/InstructionFileHandler.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/CreateBucketRequest.h>
#include <aws/s3/model/DeleteBucketRequest.h>
#include <aws/s3/model/DeleteObjectRequest.h>
#include <aws/kms/KMSClient.h>
#include <aws/kms/model/EncryptRequest.h>
#include <aws/kms/model/DecryptRequest.h>
namespace
{
static const char* const ALLOCATION_TAG = "CryptoModuleTests";
static const char* const BUCKET_TEST_NAME = "testbucket";
static const char* const KEY_TEST_NAME = "testKey";
static const char* const BODY_STREAM_TEST = "This is a test message for encryption and decryption.";
static const char* const TEST_CMK_ID = "ARN:SOME_COMBINATION_OF_LETTERS_AND_NUMBERS";
static size_t const CBC_IV_SIZE_BYTES = 16u;
#ifndef ENABLE_COMMONCRYPTO_ENCRYPTION
#if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST)
static const char* const BYTES_SPECIFIER = "bytes=0-10";
static const char* const ASSERTION_FAILED = "Assertion .*";
#endif // !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST)
static const char* const GET_RANGE_SPECIFIER = "bytes=20-40";
static const char* const GET_RANGE_OUTPUT = "ge for encryption and";
static size_t const GCM_TAG_LENGTH = 128u;
static size_t const GCM_IV_SIZE_BYTES = 12u;
#endif
using namespace Aws::Auth;
using namespace Aws::Client;
using namespace Aws::S3Encryption;
using namespace Aws::Utils::Crypto;
using namespace Aws::Utils::Crypto::ContentCryptoSchemeMapper;
using namespace Aws::Utils::Crypto::KeyWrapAlgorithmMapper;
using namespace Aws::S3Encryption::Modules;
using namespace Aws::S3Encryption::Materials;
using namespace Aws::S3::Model;
using namespace Aws::S3;
using namespace Aws::KMS;
using namespace Aws::KMS::Model;
/*
* This is a class that represents a mock KMS Client which is used to encrypt/decrypt the content encryption key
* in the S3 Encryption Client module. When encrypt is called, it will take the plain text key and store it and
* return the user a different generated key to take place as the encrypted key. Then when decrypt is called, the
* plaintext key will be returned to the user. This does not actually decrypt or encrypt anything, but acts in
* the same fashion as an actual KMS Client.
*/
class MockKMSClient : public KMSClient
{
public:
MockKMSClient(ClientConfiguration clientConfiguration = ClientConfiguration()) :
KMSClient(Aws::Auth::AWSCredentials("", ""), clientConfiguration), m_encryptCalledCount(0), m_decryptCalledCount(0),
m_encryptedKey(Aws::Utils::Crypto::SymmetricCipher::GenerateKey()), m_decryptedKey(nullptr)
{
}
EncryptOutcome Encrypt(const EncryptRequest& request) const override
{
Aws::Utils::CryptoBuffer buffer(request.GetPlaintext().GetUnderlyingData(), request.GetPlaintext().GetLength());
m_decryptedKey = Aws::MakeShared<Aws::Utils::CryptoBuffer>(ALLOCATION_TAG, buffer);
m_encryptCalledCount++;
return PopulateSuccessfulEncryptOutcome();
}
DecryptOutcome Decrypt(const DecryptRequest&) const override
{
m_decryptCalledCount++;
return PopulateSuccessfulDecryptOutcome();
}
mutable size_t m_encryptCalledCount;
mutable size_t m_decryptCalledCount;
Aws::Utils::ByteBuffer m_encryptedKey;
mutable std::shared_ptr<Aws::Utils::CryptoBuffer> m_decryptedKey;
private:
EncryptOutcome PopulateSuccessfulEncryptOutcome() const
{
EncryptOutcome outcome;
EncryptResult result(outcome.GetResult());
result.SetCiphertextBlob(m_encryptedKey);
return result;
}
DecryptOutcome PopulateSuccessfulDecryptOutcome() const
{
DecryptOutcome outcome;
DecryptResult result(outcome.GetResult());
result.SetPlaintext(*m_decryptedKey);
return result;
}
};
/*
* This is a class that represents a S3 Client which is used to mimic the put/get operations of a actual s3 client.
* During a put object, the body of the request is stored as well as the metadata of the requet. This data is then
* populated into a get object result when a get operation is called. If a get request has a range specifying the
* last 16 bytes of data, we know this is the crypto tag stored at the end of the body for GCM encryption, and we
* return this. If the range is everything but the last 16 bytes then we only return that part of the body to the
* result.
*/
class MockS3Client : public Aws::S3::S3Client
{
public:
MockS3Client(Aws::Client::ClientConfiguration clientConfiguration = Aws::Client::ClientConfiguration()) :
S3Client(Aws::Auth::AWSCredentials("", ""), clientConfiguration), m_putObjectCalled(0), m_getObjectCalled(0), m_body(nullptr)
{
}
Aws::S3::Model::PutObjectOutcome PutObject(const Aws::S3::Model::PutObjectRequest& request) const override
{
m_putObjectCalled++;
if (request.GetKey().find(Aws::S3Encryption::Handlers::DEFAULT_INSTRUCTION_FILE_SUFFIX) != Aws::String::npos)
{
Aws::S3::Model::PutObjectOutcome outcome;
Aws::S3::Model::PutObjectResult result(outcome.GetResultWithOwnership());
return result;
}
m_metadata = request.GetMetadata();
m_requestContentLength = static_cast< size_t >(request.GetContentLength());
std::shared_ptr<Aws::IOStream> body = request.GetBody();
Aws::String tempBodyString((Aws::IStreamBufIterator(*body)), Aws::IStreamBufIterator());
bodyString = tempBodyString;
Aws::S3::Model::PutObjectOutcome outcome;
Aws::S3::Model::PutObjectResult result(outcome.GetResultWithOwnership());
return result;
}
Aws::S3::Model::GetObjectOutcome GetObject(const Aws::S3::Model::GetObjectRequest& request) const override
{
m_getObjectCalled++;
auto factory = request.GetResponseStreamFactory();
Aws::Utils::Stream::ResponseStream responseStream(factory);
Aws::String range = request.GetRange();
size_t written = 0;
if (!range.empty())
{
auto bytes = m_requestContentLength;
auto rangeBytes = CryptoModule::ParseGetObjectRequestRange(range, bytes);
responseStream.GetUnderlyingStream().write((const char*)bodyString.c_str() + rangeBytes.first, rangeBytes.second - rangeBytes.first + 1);
written = static_cast< size_t >(rangeBytes.second - rangeBytes.first);
}
else
{
responseStream.GetUnderlyingStream().write((const char*)bodyString.c_str(), m_requestContentLength);
written = m_requestContentLength;
}
responseStream.GetUnderlyingStream().flush();
Aws::AmazonWebServiceResult<Aws::Utils::Stream::ResponseStream> awsStream(std::move(responseStream), Aws::Http::HeaderValueCollection());
Aws::S3::Model::GetObjectResult getObjectResult(std::move(awsStream));
getObjectResult.SetContentLength(written);
getObjectResult.SetMetadata(m_metadata);
return Aws::S3::Model::GetObjectOutcome(std::move(getObjectResult));
}
Aws::S3::Model::HeadObjectOutcome HeadObject(const Aws::S3::Model::HeadObjectRequest&) const override
{
Aws::S3::Model::HeadObjectOutcome outcome;
Aws::S3::Model::HeadObjectResult result(outcome.GetResultWithOwnership());
result.SetMetadata(m_metadata);
result.SetContentLength(m_requestContentLength);
return result;
}
const Aws::Map<Aws::String, Aws::String>& GetMetadata() const
{
return m_metadata;
}
size_t GetRequestContentLength() const
{
return m_requestContentLength;
}
mutable size_t m_putObjectCalled;
mutable size_t m_getObjectCalled;
mutable Aws::String bodyString;
mutable Aws::Map<Aws::String, Aws::String> m_metadata;
mutable std::shared_ptr<Aws::IOStream> m_body;
mutable std::shared_ptr<Aws::IOStream> m_instructionBody;
mutable size_t m_requestContentLength;
};
class CryptoModulesTest : public ::testing::Test
{
protected:
/*
* Function to check if metadata map contains content crypto material.
* Use this function to make sure a put object request contains all the material after
* a PutObjectSecurely call.
*/
static void MetadataFilled(Aws::Map<Aws::String, Aws::String> metadataMap)
{
auto metadataEnd = metadataMap.end();
ASSERT_TRUE(metadataMap.find(CONTENT_KEY_HEADER) != metadataEnd && metadataMap.find(IV_HEADER) != metadataEnd
&& metadataMap.find(CRYPTO_TAG_LENGTH_HEADER) != metadataEnd && metadataMap.find(CONTENT_CRYPTO_SCHEME_HEADER) != metadataEnd
&& metadataMap.find(KEY_WRAP_ALGORITHM) != metadataEnd && metadataMap.find(MATERIALS_DESCRIPTION_HEADER) != metadataEnd);
ASSERT_TRUE(metadataMap[CONTENT_KEY_HEADER].size() > 0u);
ASSERT_TRUE(metadataMap[IV_HEADER].size() > 0u);
ASSERT_TRUE(metadataMap[CRYPTO_TAG_LENGTH_HEADER].size() > 0u);
ASSERT_TRUE(metadataMap[CONTENT_CRYPTO_SCHEME_HEADER].size() > 0u);
ASSERT_TRUE(metadataMap[KEY_WRAP_ALGORITHM].size() > 0u);
ASSERT_TRUE(metadataMap[MATERIALS_DESCRIPTION_HEADER].size() > 0u);
}
};
TEST_F(CryptoModulesTest, EncryptionOnlyOperationsTestWithSimpleEncryptionMaterials)
{
Aws::Utils::CryptoBuffer masterKey = Aws::Utils::Crypto::SymmetricCipher::GenerateKey();
SimpleEncryptionMaterials materials(masterKey);
CryptoConfiguration cryptoConfig(StorageMethod::METADATA, CryptoMode::ENCRYPTION_ONLY);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(cryptoTagLength, 0u);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(ivBuffer.GetLength(), CBC_IV_SIZE_BYTES);
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(scheme, ContentCryptoScheme::CBC);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(keyWrapAlgorithm, KeyWrapAlgorithm::AES_KEY_WRAP);
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
GetObjectOutcome getOutcome = decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction);
ASSERT_TRUE(getOutcome.IsSuccess());
Aws::OStream& ostream = getOutcome.GetResult().GetBody();
Aws::OStringStream ss;
ss << ostream.rdbuf();
ASSERT_STREQ(BODY_STREAM_TEST, ss.str().c_str());
ASSERT_EQ(s3Client.GetMetadata(), getOutcome.GetResult().GetMetadata());
ASSERT_EQ(s3Client.m_getObjectCalled, 1u);
ASSERT_EQ(s3Client.m_putObjectCalled, 1u);
}
#ifndef ENABLE_COMMONCRYPTO_ENCRYPTION
TEST_F(CryptoModulesTest, AuthenticatedEncryptionOperationsTestWithSimpleEncryptionMaterials)
{
Aws::Utils::CryptoBuffer masterKey = Aws::Utils::Crypto::SymmetricCipher::GenerateKey();
SimpleEncryptionMaterials materials(masterKey);
CryptoConfiguration cryptoConfig(StorageMethod::METADATA, CryptoMode::AUTHENTICATED_ENCRYPTION);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(cryptoTagLength, GCM_TAG_LENGTH);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(ivBuffer.GetLength(), GCM_IV_SIZE_BYTES);
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(scheme, ContentCryptoScheme::GCM);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(keyWrapAlgorithm, KeyWrapAlgorithm::AES_KEY_WRAP);
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
GetObjectOutcome getOutcome = decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction);
ASSERT_TRUE(getOutcome.IsSuccess());
Aws::OStream& ostream = getOutcome.GetResult().GetBody();
Aws::OStringStream ss;
ss << ostream.rdbuf();
ASSERT_STREQ(BODY_STREAM_TEST, ss.str().c_str());
ASSERT_EQ(getOutcome.GetResult().GetMetadata(), s3Client.GetMetadata());
ASSERT_EQ(s3Client.m_getObjectCalled, 2u);
ASSERT_EQ(s3Client.m_putObjectCalled, 1u);
}
TEST_F(CryptoModulesTest, StrictAuthenticatedEncryptionOperationsTestWithSimpleEncryptionMaterials)
{
Aws::Utils::CryptoBuffer masterKey = Aws::Utils::Crypto::SymmetricCipher::GenerateKey();
SimpleEncryptionMaterials materials(masterKey);
CryptoConfiguration cryptoConfig(StorageMethod::METADATA, CryptoMode::STRICT_AUTHENTICATED_ENCRYPTION);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(cryptoTagLength, GCM_TAG_LENGTH);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(ivBuffer.GetLength(), GCM_IV_SIZE_BYTES);
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(scheme, ContentCryptoScheme::GCM);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(keyWrapAlgorithm, KeyWrapAlgorithm::AES_KEY_WRAP);
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
GetObjectOutcome getOutcome = decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction);
ASSERT_TRUE(getOutcome.IsSuccess());
Aws::OStream& ostream = getOutcome.GetResult().GetBody();
Aws::OStringStream ss;
ss << ostream.rdbuf();
ASSERT_STREQ(ss.str().c_str(), BODY_STREAM_TEST);
ASSERT_EQ(getOutcome.GetResult().GetMetadata(), s3Client.GetMetadata());
ASSERT_EQ(s3Client.m_getObjectCalled, 2u);
ASSERT_EQ(s3Client.m_putObjectCalled, 1u);
}
#endif
TEST_F(CryptoModulesTest, EncryptionOnlyOperationsTestWithKMSEncryptionMaterials)
{
auto kmsClient = Aws::MakeShared<MockKMSClient>(ALLOCATION_TAG, ClientConfiguration());
KMSEncryptionMaterials materials(TEST_CMK_ID, kmsClient);
CryptoConfiguration cryptoConfig(StorageMethod::METADATA, CryptoMode::ENCRYPTION_ONLY);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<KMSEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(cryptoTagLength, 0u);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(ivBuffer.GetLength(), CBC_IV_SIZE_BYTES);
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(scheme, ContentCryptoScheme::CBC);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(keyWrapAlgorithm, KeyWrapAlgorithm::KMS);
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<KMSEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
GetObjectOutcome getOutcome = decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction);
ASSERT_TRUE(getOutcome.IsSuccess());
Aws::OStream& ostream = getOutcome.GetResult().GetBody();
Aws::OStringStream ss;
ss << ostream.rdbuf();
ASSERT_STREQ(ss.str().c_str(), BODY_STREAM_TEST);
ASSERT_EQ(getOutcome.GetResult().GetMetadata(), s3Client.GetMetadata());
ASSERT_EQ(s3Client.m_getObjectCalled, 1u);
ASSERT_EQ(s3Client.m_putObjectCalled, 1u);
ASSERT_EQ(kmsClient->m_encryptCalledCount, 1u);
ASSERT_EQ(kmsClient->m_decryptCalledCount, 1u);
}
#ifndef ENABLE_COMMONCRYPTO_ENCRYPTION
TEST_F(CryptoModulesTest, AuthenticatedEncryptionOperationsTestWithKMSEncryptionMaterials)
{
auto kmsClient = Aws::MakeShared<MockKMSClient>(ALLOCATION_TAG, ClientConfiguration());
KMSEncryptionMaterials materials(TEST_CMK_ID, kmsClient);
CryptoConfiguration cryptoConfig(StorageMethod::METADATA, CryptoMode::AUTHENTICATED_ENCRYPTION);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<KMSEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(cryptoTagLength, GCM_TAG_LENGTH);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(ivBuffer.GetLength(), GCM_IV_SIZE_BYTES);
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(scheme, ContentCryptoScheme::GCM);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(keyWrapAlgorithm, KeyWrapAlgorithm::KMS);
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<KMSEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
GetObjectOutcome getOutcome = decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction);
ASSERT_TRUE(getOutcome.IsSuccess());
Aws::OStream& ostream = getOutcome.GetResult().GetBody();
Aws::OStringStream ss;
ss << ostream.rdbuf();
ASSERT_STREQ(ss.str().c_str(), BODY_STREAM_TEST);
ASSERT_EQ(getOutcome.GetResult().GetMetadata(), s3Client.GetMetadata());
ASSERT_EQ(s3Client.m_getObjectCalled, 2u);
ASSERT_EQ(s3Client.m_putObjectCalled, 1u);
ASSERT_EQ(kmsClient->m_encryptCalledCount, 1u);
ASSERT_EQ(kmsClient->m_decryptCalledCount, 1u);
}
TEST_F(CryptoModulesTest, AERangeGet)
{
SimpleEncryptionMaterials materials(Aws::Utils::Crypto::SymmetricCipher::GenerateKey());
CryptoConfiguration cryptoConfig(StorageMethod::METADATA, CryptoMode::AUTHENTICATED_ENCRYPTION);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(GCM_TAG_LENGTH, cryptoTagLength);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(GCM_IV_SIZE_BYTES, ivBuffer.GetLength());
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(ContentCryptoScheme::GCM, scheme);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(KeyWrapAlgorithm::AES_KEY_WRAP, keyWrapAlgorithm);
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
getRequest.SetRange(GET_RANGE_SPECIFIER);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
auto getOutcome = decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction);
ASSERT_TRUE(getOutcome.IsSuccess());
Aws::OStream& ostream = getOutcome.GetResult().GetBody();
Aws::OStringStream ss;
ss << ostream.rdbuf();
ASSERT_STREQ(GET_RANGE_OUTPUT, ss.str().c_str());
ASSERT_EQ(getOutcome.GetResult().GetMetadata(), s3Client.GetMetadata());
//we should not have pulled a tag here.
ASSERT_EQ(s3Client.m_getObjectCalled, 1u);
ASSERT_EQ(s3Client.m_putObjectCalled, 1u);
}
TEST_F(CryptoModulesTest, StrictAuthenticatedEncryptionOperationsTestWithKMSEncryptionMaterials)
{
auto kmsClient = Aws::MakeShared<MockKMSClient>(ALLOCATION_TAG, ClientConfiguration());
KMSEncryptionMaterials materials(TEST_CMK_ID, kmsClient);
CryptoConfiguration cryptoConfig(StorageMethod::METADATA, CryptoMode::STRICT_AUTHENTICATED_ENCRYPTION);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<KMSEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(cryptoTagLength, GCM_TAG_LENGTH);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(ivBuffer.GetLength(), GCM_IV_SIZE_BYTES);
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(scheme, ContentCryptoScheme::GCM);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(keyWrapAlgorithm, KeyWrapAlgorithm::KMS);
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<KMSEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
GetObjectOutcome getOutcome = decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction);
ASSERT_TRUE(getOutcome.IsSuccess());
Aws::OStream& ostream = getOutcome.GetResult().GetBody();
Aws::OStringStream ss;
ss << ostream.rdbuf();
ASSERT_STREQ(ss.str().c_str(), BODY_STREAM_TEST);
ASSERT_EQ(getOutcome.GetResult().GetMetadata(), s3Client.GetMetadata());
ASSERT_EQ(s3Client.m_getObjectCalled, 2u);
ASSERT_EQ(s3Client.m_putObjectCalled, 1u);
ASSERT_EQ(kmsClient->m_encryptCalledCount, 1u);
ASSERT_EQ(kmsClient->m_decryptCalledCount, 1u);
}
#if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST)
TEST_F(CryptoModulesTest, StrictAERangeGet)
{
SimpleEncryptionMaterials materials(Aws::Utils::Crypto::SymmetricCipher::GenerateKey());
CryptoConfiguration cryptoConfig(StorageMethod::METADATA, CryptoMode::STRICT_AUTHENTICATED_ENCRYPTION);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(cryptoTagLength, GCM_TAG_LENGTH);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(ivBuffer.GetLength(), GCM_IV_SIZE_BYTES);
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(scheme, ContentCryptoScheme::GCM);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(keyWrapAlgorithm, KeyWrapAlgorithm::AES_KEY_WRAP);
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), cryptoConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
getRequest.SetRange(BYTES_SPECIFIER);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
ASSERT_DEATH({ decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction); }, ASSERTION_FAILED);
}
TEST_F(CryptoModulesTest, StrictAEDecryptionFailure)
{
SimpleEncryptionMaterials materials(Aws::Utils::Crypto::SymmetricCipher::GenerateKey());
CryptoConfiguration eoConfig(StorageMethod::METADATA, CryptoMode::ENCRYPTION_ONLY);
CryptoConfiguration strictAEConfig(StorageMethod::METADATA, CryptoMode::STRICT_AUTHENTICATED_ENCRYPTION);
MockS3Client s3Client;
CryptoModuleFactory factory;
auto module = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), eoConfig);
PutObjectRequest putRequest;
putRequest.SetBucket(BUCKET_TEST_NAME);
std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>(ALLOCATION_TAG);
*objectStream << BODY_STREAM_TEST;
objectStream->flush();
putRequest.SetBody(objectStream);
putRequest.SetKey(KEY_TEST_NAME);
auto putObjectFunction = [&s3Client](Aws::S3::Model::PutObjectRequest putRequest) -> Aws::S3::Model::PutObjectOutcome { return s3Client.PutObject(putRequest); };
PutObjectOutcome putOutcome = module->PutObjectSecurely(putRequest, putObjectFunction);
ASSERT_TRUE(putOutcome.IsSuccess());
auto metadata = s3Client.GetMetadata();
MetadataFilled(metadata);
size_t cryptoTagLength = static_cast<size_t>(Aws::Utils::StringUtils::ConvertToInt64(metadata[CRYPTO_TAG_LENGTH_HEADER].c_str()));
ASSERT_EQ(cryptoTagLength, 0u);
Aws::Utils::CryptoBuffer ivBuffer = Aws::Utils::HashingUtils::Base64Decode(metadata[IV_HEADER]);
ASSERT_EQ(ivBuffer.GetLength(), CBC_IV_SIZE_BYTES);
ContentCryptoScheme scheme = ContentCryptoSchemeMapper::GetContentCryptoSchemeForName(metadata[CONTENT_CRYPTO_SCHEME_HEADER]);
ASSERT_EQ(scheme, ContentCryptoScheme::CBC);
KeyWrapAlgorithm keyWrapAlgorithm = KeyWrapAlgorithmMapper::GetKeyWrapAlgorithmForName(metadata[KEY_WRAP_ALGORITHM]);
ASSERT_EQ(keyWrapAlgorithm, KeyWrapAlgorithm::AES_KEY_WRAP);
//check to make sure content length is now padded since StrictAE appends the tag to the end of the body
ASSERT_TRUE(s3Client.GetRequestContentLength() > strlen(BODY_STREAM_TEST));
auto decryptionModule = factory.FetchCryptoModule(Aws::MakeShared<SimpleEncryptionMaterials>(ALLOCATION_TAG, materials), strictAEConfig);
GetObjectRequest getRequest;
getRequest.SetBucket(BUCKET_TEST_NAME);
getRequest.SetKey(KEY_TEST_NAME);
HeadObjectRequest headObject;
headObject.WithBucket(BUCKET_TEST_NAME);
headObject.WithKey(KEY_TEST_NAME);
HeadObjectOutcome headOutcome = s3Client.HeadObject(headObject);
Aws::S3Encryption::Handlers::MetadataHandler handler;
ContentCryptoMaterial contentCryptoMaterial = handler.ReadContentCryptoMaterial(headOutcome.GetResult());
auto getObjectFunction = [&s3Client](Aws::S3::Model::GetObjectRequest getRequest) -> Aws::S3::Model::GetObjectOutcome { return s3Client.GetObject(getRequest); };
ASSERT_DEATH({ decryptionModule->GetObjectSecurely(getRequest, headOutcome.GetResult(), contentCryptoMaterial, getObjectFunction); }, ASSERTION_FAILED);
}
#endif // !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST)
#endif
TEST_F(CryptoModulesTest, RangeParserSuccess)
{
SimpleEncryptionMaterials materials(Aws::Utils::Crypto::SymmetricCipher::GenerateKey());
CryptoConfiguration config(StorageMethod::METADATA, CryptoMode::ENCRYPTION_ONLY);
CryptoModuleFactory factory;
int64_t contentLength = 90;
Aws::Vector<Aws::String> rangeOptions = { "bytes=10-20", "bytes=-20", "bytes=20-", "bytes=1-9" };
Aws::Vector<std::pair<int64_t, int64_t>> resultPairs = {
std::make_pair(10LL, 20LL),
std::make_pair(70LL, contentLength - 1),
std::make_pair(20LL, contentLength - 1),
std::make_pair(1LL, 9LL)
};
for (size_t i = 0; i < rangeOptions.size(); ++i)
{
auto pair = CryptoModule::ParseGetObjectRequestRange(rangeOptions[i], contentLength);
ASSERT_EQ(pair, resultPairs[i]);
}
}
TEST_F(CryptoModulesTest, RangeParserFailure)
{
SimpleEncryptionMaterials materials(Aws::Utils::Crypto::SymmetricCipher::GenerateKey());
CryptoConfiguration config(StorageMethod::METADATA, CryptoMode::ENCRYPTION_ONLY);
CryptoModuleFactory factory;
int64_t contentLength = 90;
Aws::Vector<Aws::String> rangeOptions = { "bytes10-20", "bytes=20", "20=-", "bytes19" };
for (size_t i = 0; i < rangeOptions.size(); ++i)
{
auto pair = CryptoModule::ParseGetObjectRequestRange(rangeOptions[i], contentLength);
ASSERT_EQ(pair, std::make_pair(static_cast<int64_t>(0), static_cast<int64_t>(0)));
}
}
}
#endif