AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
CryptoModule.h
1
5#pragma once
6
7#include <aws/s3-encryption/s3Encryption_EXPORTS.h>
8#include <aws/core/utils/crypto/ContentCryptoMaterial.h>
9#include <aws/core/utils/crypto/Cipher.h>
10#include <aws/core/utils/crypto/EncryptionMaterials.h>
11#include <aws/s3-encryption/CryptoConfiguration.h>
12#include <aws/s3-encryption/handlers/DataHandler.h>
13#include <aws/s3-encryption/handlers/InstructionFileHandler.h>
14#include <aws/s3-encryption/handlers/MetadataHandler.h>
15#include <aws/core/auth/AWSCredentialsProvider.h>
16#include <aws/s3/S3Client.h>
17#include <aws/s3-encryption/S3EncryptionClient.h>
18#include <aws/s3/model/PutObjectRequest.h>
19#include <aws/s3/model/PutObjectResult.h>
20#include <aws/s3/model/GetObjectResult.h>
21#include <aws/s3/model/GetObjectRequest.h>
22#include <aws/s3/model/HeadObjectRequest.h>
23
24namespace Aws
25{
26 namespace S3Encryption
27 {
28 namespace Modules
29 {
32
33 class AWS_S3ENCRYPTION_API CryptoModule
34 {
35 public:
36 /*
37 * Constructor to initialize encryption materials, crypto configuration, and internal S3 client.
38 */
39 CryptoModule(const std::shared_ptr<Aws::Utils::Crypto::EncryptionMaterials>& encryptionMaterials, const CryptoConfiguration & cryptoConfig);
40
41
42 /*
43 * Default destructor
44 */
45 virtual ~CryptoModule() = default;
46
47 /*
48 * Function to put an encrypted object to S3.
49 */
51
52 /*
53 * Function to get an encrypted object from S3. This function takes a headObjectResult as well to collect metadata.
54 */
56 const Aws::Utils::Crypto::ContentCryptoMaterial& contentCryptoMaterial, const GetObjectFunction& getObjectFunction);
57
58 /*
59 * Function to parse range of a get object request and return a pair containing the lower and upper bounds.
60 */
61 static std::pair<int64_t, int64_t> ParseGetObjectRequestRange(const Aws::String& range, int64_t contentLength);
62
63 private:
64 /*
65 * This function is used to encrypt the given S3 PutObjectRequest.
66 */
67 S3EncryptionPutObjectOutcome WrapAndMakeRequestWithCipher(Aws::S3::Model::PutObjectRequest& request, const PutObjectFunction& putObjectFunction);
68
69 /*
70 * This function is used to decrypt the given S3 GetObjectResult.
71 */
72 S3EncryptionGetObjectOutcome UnwrapAndMakeRequestWithCipher(Aws::S3::Model::GetObjectRequest& request, const GetObjectFunction& getObjectFunction, int16_t firstBlockOffset = 0);
73
74 protected:
75 /*
76 * This function sets the content length of the put object request, accounting for any additional content appended after encryption.
77 */
79
83 virtual void InitEncryptionCipher() = 0;
84
88 virtual void InitDecryptionCipher(int64_t rangeStart = 0, int64_t rangeEnd = 0, const Aws::Utils::CryptoBuffer& tag = Aws::Utils::CryptoBuffer()) = 0;
89
90 /*
91 * This function populates the content crypto material with the module specific details for encryption.
92 */
94
95 /*
96 * This function is used to get the crypto tag appended to the end of the body. It creates a separate get request to obtain the tag.
97 */
98 virtual Aws::Utils::CryptoBuffer GetTag(const Aws::S3::Model::GetObjectRequest& request, const std::function < Aws::S3::Model::GetObjectOutcome(const Aws::S3::Model::GetObjectRequest&) >& getObjectFunction) = 0;
99
100 /*
101 * This function checks for any prohibited actions within each module.
102 */
103 virtual bool DecryptionConditionCheck(const Aws::String& requestRange) = 0;
104
105 /*
106 * This function adjusts the get object request range to specifically get only the body of the content and not any addition content.
107 * It also adjusts the range if a range-get request was specified according to the range for the cipher block.
108 */
109 virtual std::pair<int64_t, int64_t> AdjustRange(Aws::S3::Model::GetObjectRequest& getObjectRequest, const Aws::S3::Model::HeadObjectResult& headObjectResult) = 0;
110
111 std::shared_ptr<Aws::Utils::Crypto::EncryptionMaterials> m_encryptionMaterials;
114 std::shared_ptr<Aws::Utils::Crypto::SymmetricCipher> m_cipher;
115 };
116
117 class AWS_S3ENCRYPTION_API CryptoModuleEO : public CryptoModule
118 {
119 public:
120 /*
121 * Constructor to initialize encryption materials, crypto configuration, and internal S3 client.
122 */
123 CryptoModuleEO(const std::shared_ptr<Aws::Utils::Crypto::EncryptionMaterials>& encryptionMaterials, const CryptoConfiguration & cryptoConfig);
124
125 private:
126 /*
127 * Function to set content length of request which accounts for CBC padding.
128 */
129 void SetContentLength(Aws::S3::Model::PutObjectRequest& request) override;
130
131 /*
132 * Function to populate the crypto content member variable.
133 */
134 void PopulateCryptoContentMaterial() override;
135
139 void InitEncryptionCipher() override;
140
144 void InitDecryptionCipher(int64_t rangeStart = 0, int64_t rangeEnd = 0, const Aws::Utils::CryptoBuffer& tag = Aws::Utils::CryptoBuffer()) override;
145
146 /*
147 * Function to get the crypto tag according to the module.
148 */
149 Aws::Utils::CryptoBuffer GetTag(const Aws::S3::Model::GetObjectRequest& request, const std::function < Aws::S3::Model::GetObjectOutcome(const Aws::S3::Model::GetObjectRequest&) >& getObjectFunction) override;
150
151 /*
152 * Function to check for any prohibited actions specific to each module for decryption.
153 */
154 virtual bool DecryptionConditionCheck(const Aws::String& requestRange) override;
155
156 /*
157 * Function to adjust getObjectRequest range to only specify the encrypted body.
158 */
159 std::pair<int64_t, int64_t> AdjustRange(Aws::S3::Model::GetObjectRequest& getObjectRequest, const Aws::S3::Model::HeadObjectResult& headObjectResult) override;
160 };
161
162 class AWS_S3ENCRYPTION_API CryptoModuleAE : public CryptoModule
163 {
164 public:
165 /*
166 * Constructor to initialize encryption materials, crypto configuration, and internal S3 client.
167 */
168 CryptoModuleAE(const std::shared_ptr<Aws::Utils::Crypto::EncryptionMaterials>& encryptionMaterials, const CryptoConfiguration & cryptoConfig);
169
170 private:
171 /*
172 * Function to set content length of request which accounts for the GCM tag appended to the body of the request.
173 */
174 void SetContentLength(Aws::S3::Model::PutObjectRequest& request) override;
175 /*
176 * Function to populate the crypto content member variable.
177 */
178 void PopulateCryptoContentMaterial() override;
179
183 void InitEncryptionCipher() override;
184
188 void InitDecryptionCipher(int64_t rangeStart = 0, int64_t rangeEnd = 0, const Aws::Utils::CryptoBuffer& tag = Aws::Utils::CryptoBuffer()) override;
189
190 /*
191 * Function to get the crypto tag according to the module.
192 */
193 Aws::Utils::CryptoBuffer GetTag(const Aws::S3::Model::GetObjectRequest& request, const std::function < Aws::S3::Model::GetObjectOutcome(const Aws::S3::Model::GetObjectRequest&) >& getObjectFunction) override;
194
195 /*
196 * Function to check for any prohibited actions specific to each module for decryption.
197 */
198 virtual bool DecryptionConditionCheck(const Aws::String& requestRange) override;
199
200 /*
201 * Function adjust getObjectRequest range to only specify the encrypted body.
202 */
203 std::pair<int64_t, int64_t> AdjustRange(Aws::S3::Model::GetObjectRequest& getObjectRequest, const Aws::S3::Model::HeadObjectResult& headObjectResult) override;
204 };
205
206 class AWS_S3ENCRYPTION_API CryptoModuleStrictAE : public CryptoModule
207 {
208 public:
209 /*
210 * Constructor to initialize encryption materials, crypto configuration, and internal S3 client.
211 */
212 CryptoModuleStrictAE(const std::shared_ptr<Aws::Utils::Crypto::EncryptionMaterials>& encryptionMaterials, const CryptoConfiguration & cryptoConfig);
213
214 private:
215 /*
216 * Function to set content length of request which accounts for the GCM tag appended to the body of the request.
217 */
218 void SetContentLength(Aws::S3::Model::PutObjectRequest& request) override;
219 /*
220 * Function to populate the crypto content member variable.
221 */
222 void PopulateCryptoContentMaterial() override;
223
227 void InitEncryptionCipher() override;
228
232 void InitDecryptionCipher(int64_t rangeStart = 0, int64_t rangeEnd = 0, const Aws::Utils::CryptoBuffer& tag = Aws::Utils::CryptoBuffer()) override;
233
234 /*
235 * Function to get the crypto tag according to the module.
236 */
237 Aws::Utils::CryptoBuffer GetTag(const Aws::S3::Model::GetObjectRequest& request, const std::function < Aws::S3::Model::GetObjectOutcome(const Aws::S3::Model::GetObjectRequest&) >& getObjectFunction) override;
238
239 /*
240 * Function to check for any prohibited actions specific to each module for decryption.
241 */
242 virtual bool DecryptionConditionCheck(const Aws::String& requestRange) override;
243
244 /*
245 * Function adjust getObjectRequest range to only specify the encrypted body.
246 */
247 std::pair<int64_t, int64_t> AdjustRange(Aws::S3::Model::GetObjectRequest& getObjectRequest, const Aws::S3::Model::HeadObjectResult& headObjectResult) override;
248 };
249
255 {
256 public:
261
262 operator bool() const override;
263
268
274
279
284
288 void Reset() override;
289
290 private:
291 std::shared_ptr<Aws::Utils::Crypto::SymmetricCipher> m_cipher;
292 };
293
294 }
295 }
296}
AES_GCM_AppendedTag(const Aws::Utils::CryptoBuffer &key)
Aws::Utils::CryptoBuffer DecryptBuffer(const Aws::Utils::CryptoBuffer &unEncryptedData) override
Aws::Utils::CryptoBuffer FinalizeEncryption() override
Aws::Utils::CryptoBuffer EncryptBuffer(const Aws::Utils::CryptoBuffer &unEncryptedData) override
Aws::Utils::CryptoBuffer FinalizeDecryption() override
CryptoModuleAE(const std::shared_ptr< Aws::Utils::Crypto::EncryptionMaterials > &encryptionMaterials, const CryptoConfiguration &cryptoConfig)
CryptoModuleEO(const std::shared_ptr< Aws::Utils::Crypto::EncryptionMaterials > &encryptionMaterials, const CryptoConfiguration &cryptoConfig)
virtual Aws::Utils::CryptoBuffer GetTag(const Aws::S3::Model::GetObjectRequest &request, const std::function< Aws::S3::Model::GetObjectOutcome(const Aws::S3::Model::GetObjectRequest &) > &getObjectFunction)=0
S3EncryptionPutObjectOutcome PutObjectSecurely(const Aws::S3::Model::PutObjectRequest &request, const PutObjectFunction &putObjectFunction, const Aws::Map< Aws::String, Aws::String > &contextMap={})
virtual bool DecryptionConditionCheck(const Aws::String &requestRange)=0
S3EncryptionGetObjectOutcome GetObjectSecurely(const Aws::S3::Model::GetObjectRequest &request, const Aws::S3::Model::HeadObjectResult &headObjectResult, const Aws::Utils::Crypto::ContentCryptoMaterial &contentCryptoMaterial, const GetObjectFunction &getObjectFunction)
virtual void InitDecryptionCipher(int64_t rangeStart=0, int64_t rangeEnd=0, const Aws::Utils::CryptoBuffer &tag=Aws::Utils::CryptoBuffer())=0
std::shared_ptr< Aws::Utils::Crypto::EncryptionMaterials > m_encryptionMaterials
virtual void SetContentLength(Aws::S3::Model::PutObjectRequest &request)=0
CryptoModule(const std::shared_ptr< Aws::Utils::Crypto::EncryptionMaterials > &encryptionMaterials, const CryptoConfiguration &cryptoConfig)
static std::pair< int64_t, int64_t > ParseGetObjectRequestRange(const Aws::String &range, int64_t contentLength)
virtual std::pair< int64_t, int64_t > AdjustRange(Aws::S3::Model::GetObjectRequest &getObjectRequest, const Aws::S3::Model::HeadObjectResult &headObjectResult)=0
std::shared_ptr< Aws::Utils::Crypto::SymmetricCipher > m_cipher
Aws::Utils::Crypto::ContentCryptoMaterial m_contentCryptoMaterial
CryptoModuleStrictAE(const std::shared_ptr< Aws::Utils::Crypto::EncryptionMaterials > &encryptionMaterials, const CryptoConfiguration &cryptoConfig)
Aws::Utils::Outcome< GetObjectResult, S3Error > GetObjectOutcome
Aws::Utils::Outcome< PutObjectResult, S3Error > PutObjectOutcome
std::function< Aws::S3::Model::PutObjectOutcome(const Aws::S3::Model::PutObjectRequest &)> PutObjectFunction
std::function< Aws::S3::Model::GetObjectOutcome(const Aws::S3::Model::GetObjectRequest &)> GetObjectFunction
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String