1
|
1 /**
|
|
2 * Cloud storage plugins for Orthanc
|
37
|
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
|
|
19
|
|
20 #include <orthanc/OrthancCPlugin.h>
|
|
21 #include <OrthancPluginCppWrapper.h>
|
|
22
|
|
23 #include "EncryptionConfigurator.h"
|
|
24
|
|
25 bool ReadMasterKey(uint32_t& id, std::string& keyPath, const Json::Value& node)
|
|
26 {
|
|
27 if (!node.isArray() || node.size() != 2 || !node[0].isUInt() || !node[1].isString())
|
|
28 {
|
|
29 OrthancPlugins::LogWarning("Encryption: Invalid master key configuration");
|
|
30 return false;
|
|
31 }
|
|
32
|
|
33 id = node[0].asUInt();
|
|
34 keyPath = node[1].asString();
|
|
35
|
|
36 return true;
|
|
37 }
|
|
38
|
|
39
|
|
40 EncryptionHelpers* EncryptionConfigurator::CreateEncryptionHelpers(const OrthancPlugins::OrthancConfiguration& cryptoSection)
|
|
41 {
|
|
42 bool enabled = cryptoSection.GetBooleanValue("Enable", true);
|
|
43
|
|
44 if (!enabled)
|
|
45 {
|
|
46 return nullptr;
|
|
47 }
|
|
48
|
|
49 Json::Value cryptoJson = cryptoSection.GetJson();
|
|
50
|
|
51 if (!cryptoJson.isMember("MasterKey") || !cryptoJson["MasterKey"].isArray())
|
|
52 {
|
|
53 OrthancPlugins::LogWarning("Encryption: MasterKey missing. Unable to initialize encryption");
|
|
54 return nullptr;
|
|
55 }
|
|
56
|
|
57 unsigned int maxConcurrentInputSizeInMb = cryptoSection.GetUnsignedIntegerValue("MaxConcurrentInputSize", 1024);
|
|
58
|
|
59 std::unique_ptr<EncryptionHelpers> crypto(new EncryptionHelpers(maxConcurrentInputSizeInMb * 1024*1024));
|
|
60
|
|
61 uint32_t masterKeyId;
|
|
62 std::string masterKeyPath;
|
|
63
|
|
64 if (!ReadMasterKey(masterKeyId, masterKeyPath, cryptoJson["MasterKey"]))
|
|
65 {
|
|
66 return nullptr;
|
|
67 }
|
|
68 crypto->SetCurrentMasterKey(masterKeyId, masterKeyPath);
|
|
69
|
|
70 if (cryptoJson.isMember("PreviousMasterKeys") && cryptoJson["PreviousMasterKeys"].isArray())
|
|
71 {
|
|
72 for (size_t i = 0; i < cryptoJson["PreviousMasterKeys"].size(); i++)
|
|
73 {
|
|
74 uint32_t keyId;
|
|
75 std::string keyPath;
|
|
76 if (!ReadMasterKey(keyId, keyPath, cryptoJson["PreviousMasterKeys"][(unsigned int)i]))
|
|
77 {
|
|
78 return nullptr;
|
|
79 }
|
|
80 crypto->AddPreviousMasterKey(keyId, keyPath);
|
|
81 }
|
|
82 }
|
|
83
|
|
84 return crypto.release();
|
|
85 }
|