Mercurial > hg > orthanc-object-storage
annotate Common/EncryptionHelpers.cpp @ 68:58a03fce4897
new option for AWS: EnableLegacyUnknownFiles
author | Alain Mazy <am@osimis.io> |
---|---|
date | Fri, 09 Jul 2021 15:40:07 +0200 |
parents | b922ae86bbe1 |
children | ac596874d997 |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
37
f55b2afdf53d
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
30
diff
changeset
|
3 * Copyright (C) 2020-2021 Osimis S.A., Belgium |
1 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
37
diff
changeset
|
19 |
1 | 20 #include "EncryptionHelpers.h" |
21 #include <assert.h> | |
22 | |
23 #include <boost/lexical_cast.hpp> | |
24 #include <iostream> | |
30
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
25 |
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
26 #include <cryptopp/cryptlib.h> |
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
27 #include <cryptopp/modes.h> |
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
28 #include <cryptopp/hex.h> |
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
29 #include <cryptopp/gcm.h> |
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
30 #include <cryptopp/files.h> |
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
31 #include <cryptopp/filters.h> |
1 | 32 |
33 const std::string EncryptionHelpers::HEADER_VERSION = "A1"; | |
34 | |
35 using namespace CryptoPP; | |
36 | |
30
662b9d3f217d
fix missing definition of "byte" from CryptoPP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
37 std::string EncryptionHelpers::ToHexString(const void* block, size_t size) |
1 | 38 { |
39 std::string blockAsString = std::string(reinterpret_cast<const char*>(block), size); | |
40 | |
41 return ToHexString(blockAsString); | |
42 } | |
43 | |
44 std::string EncryptionHelpers::ToHexString(const std::string& block) | |
45 { | |
46 std::string hexString; | |
47 StringSource ss(block, true, | |
48 new HexEncoder( | |
49 new StringSink(hexString) | |
50 ) // StreamTransformationFilter | |
51 ); // StringSource | |
52 | |
53 return hexString; | |
54 } | |
55 | |
56 std::string EncryptionHelpers::ToHexString(const SecByteBlock& block) | |
57 { | |
58 return ToHexString(ToString(block)); | |
59 } | |
60 | |
61 std::string EncryptionHelpers::ToString(const CryptoPP::SecByteBlock& block) | |
62 { | |
63 return std::string(reinterpret_cast<const char*>(block.data()), block.size()); | |
64 } | |
65 | |
66 std::string EncryptionHelpers::ToString(uint32_t value) | |
67 { | |
68 return std::string(reinterpret_cast<const char*>(&value), 4); | |
69 } | |
70 | |
71 void EncryptionHelpers::ReadKey(CryptoPP::SecByteBlock& key, const std::string& path) | |
72 { | |
73 try | |
74 { | |
75 FileSource fs(path.c_str(), true, | |
76 new HexDecoder( | |
77 new ArraySink(key.begin(), key.size()) | |
78 ) | |
79 ); | |
80 } | |
81 catch (CryptoPP::Exception& ex) | |
82 { | |
83 throw EncryptionException("unabled to read key from file '" + path + "': " + ex.what()); | |
84 } | |
85 } | |
86 | |
87 void EncryptionHelpers::SetCurrentMasterKey(uint32_t id, const std::string& path) | |
88 { | |
89 SecByteBlock key(AES_KEY_SIZE); | |
90 | |
91 ReadKey(key, path); | |
92 SetCurrentMasterKey(id, key); | |
93 } | |
94 | |
95 void EncryptionHelpers::AddPreviousMasterKey(uint32_t id, const std::string& path) | |
96 { | |
97 SecByteBlock key(AES_KEY_SIZE); | |
98 | |
99 ReadKey(key, path); | |
100 AddPreviousMasterKey(id, key); | |
101 } | |
102 | |
103 EncryptionHelpers::EncryptionHelpers(size_t maxConcurrentInputSize) | |
104 : concurrentInputSizeSemaphore_(maxConcurrentInputSize), | |
105 maxConcurrentInputSize_(maxConcurrentInputSize) | |
106 { | |
107 } | |
108 | |
109 void EncryptionHelpers::SetCurrentMasterKey(uint32_t id, const CryptoPP::SecByteBlock& key) | |
110 { | |
111 encryptionMasterKey_ = key; | |
112 encryptionMasterKeyId_ = ToString(id); | |
113 } | |
114 | |
115 void EncryptionHelpers::AddPreviousMasterKey(uint32_t id, const CryptoPP::SecByteBlock& key) | |
116 { | |
117 previousMasterKeys_[ToString(id)] = key; | |
118 } | |
119 | |
120 const CryptoPP::SecByteBlock& EncryptionHelpers::GetMasterKey(const std::string& keyId) | |
121 { | |
122 if (encryptionMasterKeyId_ == keyId) | |
123 { | |
124 return encryptionMasterKey_; | |
125 } | |
126 | |
127 if (previousMasterKeys_.find(keyId) == previousMasterKeys_.end()) | |
128 { | |
129 throw EncryptionException("The master key whose id is '" + ToHexString(keyId) + "' could not be found. Unable to decrypt file"); | |
130 } | |
131 | |
132 return previousMasterKeys_.at(keyId); | |
133 } | |
134 | |
135 void EncryptionHelpers::GenerateKey(CryptoPP::SecByteBlock& key) | |
136 { | |
137 AutoSeededRandomPool prng; | |
138 | |
139 SecByteBlock tempKey(AES_KEY_SIZE); | |
140 prng.GenerateBlock( tempKey, tempKey.size() ); | |
141 key = tempKey; | |
142 } | |
143 | |
144 void EncryptionHelpers::Encrypt(std::string &output, const std::string &input) | |
145 { | |
146 Encrypt(output, input.data(), input.size()); | |
147 } | |
148 | |
149 void EncryptionHelpers::Encrypt(std::string &output, const char* data, size_t size) | |
150 { | |
151 if (size > maxConcurrentInputSize_) | |
152 { | |
153 throw EncryptionException("The file is too large to encrypt: " + boost::lexical_cast<std::string>(size) + " bytes. Try increasing the MaxConcurrentInputSize"); | |
154 } | |
155 | |
156 Orthanc::Semaphore::Locker lock(concurrentInputSizeSemaphore_, size); | |
157 | |
158 EncryptInternal(output, data, size, encryptionMasterKey_); | |
159 } | |
160 | |
161 void EncryptionHelpers::Decrypt(std::string &output, const std::string &input) | |
162 { | |
163 output.resize(input.size() - OVERHEAD_SIZE); | |
164 Decrypt(const_cast<char*>(output.data()), input.data(), input.size()); | |
165 } | |
166 | |
167 void EncryptionHelpers::Decrypt(char* output, const char* data, size_t size) | |
168 { | |
169 if (size > maxConcurrentInputSize_) | |
170 { | |
171 throw EncryptionException("The file is too large to decrypt: " + boost::lexical_cast<std::string>(size) + " bytes. Try increasing the MaxConcurrentInputSize"); | |
172 } | |
173 | |
174 Orthanc::Semaphore::Locker lock(concurrentInputSizeSemaphore_, size); | |
175 | |
176 if (size < HEADER_VERSION_SIZE) | |
177 { | |
178 throw EncryptionException("Unable to decrypt data, no header found"); | |
179 } | |
180 | |
181 std::string version = std::string(data, HEADER_VERSION_SIZE); | |
182 | |
183 if (version != "A1") | |
184 { | |
185 throw EncryptionException("Unable to decrypt data, version '" + version + "' is not supported"); | |
186 } | |
187 | |
188 if (size < (HEADER_VERSION_SIZE + MASTER_KEY_ID_SIZE)) | |
189 { | |
190 throw EncryptionException("Unable to decrypt data, no master key id found"); | |
191 } | |
192 | |
193 std::string decryptionMasterKeyId = std::string(data + HEADER_VERSION_SIZE, MASTER_KEY_ID_SIZE); | |
194 | |
195 const SecByteBlock& decryptionMasterKey = GetMasterKey(decryptionMasterKeyId); | |
196 DecryptInternal(output, data, size, decryptionMasterKey); | |
197 } | |
198 | |
199 void EncryptionHelpers::EncryptPrefixSecBlock(std::string& output, const CryptoPP::SecByteBlock& input, const CryptoPP::SecByteBlock& masterKey) | |
200 { | |
201 try | |
202 { | |
203 SecByteBlock iv(16); | |
204 memset(iv.data(), 0, iv.size()); | |
205 | |
206 CTR_Mode<AES>::Encryption e; | |
207 e.SetKeyWithIV(masterKey, masterKey.size(), iv.data(), iv.size()); | |
208 | |
209 std::string inputString = ToString(input); | |
210 | |
211 // The StreamTransformationFilter adds padding | |
212 // as required. ECB and CBC Mode must be padded | |
213 // to the block size of the cipher. | |
214 StringSource ss(inputString, true, | |
215 new StreamTransformationFilter(e, | |
216 new StringSink(output) | |
217 ) // StreamTransformationFilter | |
218 ); // StringSource | |
219 } | |
220 catch (CryptoPP::Exception& e) | |
221 { | |
222 throw EncryptionException(e.what()); | |
223 } | |
224 | |
225 assert(output.size() == input.size()); | |
226 } | |
227 | |
228 void EncryptionHelpers::DecryptPrefixSecBlock(CryptoPP::SecByteBlock& output, const std::string& input, const CryptoPP::SecByteBlock& masterKey) | |
229 { | |
230 try | |
231 { | |
232 SecByteBlock iv(16); | |
233 memset(iv.data(), 0, iv.size()); | |
234 | |
235 CTR_Mode<AES>::Decryption d; | |
236 d.SetKeyWithIV(masterKey, masterKey.size(), iv.data(), iv.size()); | |
237 | |
238 std::string outputString; | |
239 | |
240 // The StreamTransformationFilter adds padding | |
241 // as required. ECB and CBC Mode must be padded | |
242 // to the block size of the cipher. | |
243 StringSource ss(input, true, | |
244 new StreamTransformationFilter(d, | |
245 new StringSink(outputString) | |
246 ) // StreamTransformationFilter | |
247 ); // StringSource | |
248 | |
249 output.Assign((const byte*)outputString.data(), outputString.size()); | |
250 } | |
251 catch (CryptoPP::Exception& e) | |
252 { | |
253 throw EncryptionException(e.what()); | |
254 } | |
255 | |
256 assert(output.size() == input.size()); | |
257 } | |
258 | |
259 | |
260 void EncryptionHelpers::EncryptInternal(std::string& output, const char* data, size_t size, const CryptoPP::SecByteBlock& masterKey) | |
261 { | |
262 SecByteBlock iv(IV_SIZE); | |
263 randomGenerator_.GenerateBlock(iv, iv.size()); // with GCM, the iv is supposed to be a nonce (not a random number). However, since each dataKey is used only once, we consider a random number is fine. | |
264 | |
265 SecByteBlock dataKey; | |
266 GenerateKey(dataKey); | |
267 | |
268 // std::cout << ToHexString(dataKey) << std::endl; | |
269 // std::cout << ToHexString(iv) << std::endl; | |
270 std::string encryptedDataKey; | |
271 std::string encryptedIv; | |
272 | |
273 EncryptPrefixSecBlock(encryptedIv, iv, masterKey); | |
274 EncryptPrefixSecBlock(encryptedDataKey, dataKey, masterKey); | |
275 | |
276 std::string prefix = HEADER_VERSION + encryptionMasterKeyId_ + encryptedIv + encryptedDataKey; | |
277 | |
278 try | |
279 { | |
280 GCM<AES>::Encryption e; | |
25 | 281 e.SetKeyWithIV(dataKey, dataKey.size(), iv, iv.size()); |
1 | 282 |
283 // the output text starts with the unencrypted prefix | |
284 output = prefix; | |
285 | |
286 AuthenticatedEncryptionFilter ef(e, | |
287 new StringSink(output), false, INTEGRITY_CHECK_TAG_SIZE | |
288 ); | |
289 | |
290 | |
291 // AuthenticatedEncryptionFilter::ChannelPut | |
292 // defines two channels: "" (empty) and "AAD" | |
293 // channel "" is encrypted and authenticated | |
294 // channel "AAD" is authenticated | |
295 ef.ChannelPut("AAD", (const byte*)prefix.data(), prefix.size()); | |
296 ef.ChannelMessageEnd("AAD"); | |
297 | |
298 // Authenticated data *must* be pushed before | |
299 // Confidential/Authenticated data. Otherwise | |
300 // we must catch the BadState exception | |
301 ef.ChannelPut("", (const byte*)data, size); | |
302 ef.ChannelMessageEnd(""); | |
303 } | |
304 catch(CryptoPP::Exception& e) | |
305 { | |
306 throw EncryptionException(e.what()); | |
307 } | |
308 } | |
309 | |
310 void EncryptionHelpers::DecryptInternal(char* output, const char* data, size_t size, const CryptoPP::SecByteBlock& masterKey) | |
311 { | |
312 size_t prefixSize = HEADER_VERSION_SIZE + MASTER_KEY_ID_SIZE + IV_SIZE + AES_KEY_SIZE; | |
313 | |
314 std::string prefix = std::string(data, prefixSize); | |
315 std::string mac = std::string(data + size - INTEGRITY_CHECK_TAG_SIZE, INTEGRITY_CHECK_TAG_SIZE); | |
316 | |
317 std::string encryptedIv = prefix.substr(HEADER_VERSION_SIZE + MASTER_KEY_ID_SIZE, IV_SIZE); | |
318 std::string encryptedDataKey = prefix.substr(HEADER_VERSION_SIZE + MASTER_KEY_ID_SIZE + IV_SIZE, AES_KEY_SIZE); | |
319 | |
320 SecByteBlock dataKey; | |
321 SecByteBlock iv; | |
322 | |
323 DecryptPrefixSecBlock(iv, encryptedIv, masterKey); | |
324 DecryptPrefixSecBlock(dataKey, encryptedDataKey, masterKey); | |
325 // std::cout << ToHexString(dataKey) << std::endl; | |
326 // std::cout << ToHexString(iv) << std::endl; | |
327 | |
328 GCM<AES>::Decryption d; | |
25 | 329 d.SetKeyWithIV(dataKey, dataKey.size(), iv, iv.size()); |
1 | 330 |
331 try { | |
332 AuthenticatedDecryptionFilter df(d, NULL, | |
333 AuthenticatedDecryptionFilter::MAC_AT_BEGIN | | |
334 AuthenticatedDecryptionFilter::THROW_EXCEPTION, INTEGRITY_CHECK_TAG_SIZE); | |
335 | |
336 // The order of the following calls are important | |
337 df.ChannelPut("", (const byte*)mac.data(), mac.size()); | |
338 df.ChannelPut("AAD", (const byte*)prefix.data(), prefix.size()); | |
339 df.ChannelPut("", (const byte*)(data) + prefixSize, size - INTEGRITY_CHECK_TAG_SIZE - prefixSize); | |
340 | |
341 // If the object throws, it will most likely occur | |
342 // during ChannelMessageEnd() | |
343 df.ChannelMessageEnd("AAD"); | |
344 df.ChannelMessageEnd(""); | |
345 | |
346 // If the object does not throw, here's the only | |
347 // opportunity to check the data's integrity | |
348 if (!df.GetLastResult()) | |
349 { | |
350 throw EncryptionException("The decryption filter failed for some unknown reason. Integrity check failed ?"); | |
351 } | |
352 | |
353 // Remove data from channel | |
354 size_t n = (size_t)-1; | |
355 | |
356 // Recover plain text | |
357 df.SetRetrievalChannel(""); | |
358 n = (size_t)df.MaxRetrievable(); | |
359 | |
360 if(n > 0) | |
361 { | |
362 assert(n == size - OVERHEAD_SIZE); | |
363 | |
364 df.Get((byte*)output, n); | |
365 } | |
366 } | |
367 catch (CryptoPP::Exception& ex) | |
368 { | |
369 throw EncryptionException(ex.what()); | |
370 } | |
371 } |