| 1 | #include "mtx/secret_storage.hpp" |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
| 5 | namespace mtx { |
| 6 | namespace secret_storage { |
| 7 | |
| 8 | void |
| 9 | to_json(nlohmann::json &obj, const AesHmacSha2EncryptedData &data) |
| 10 | { |
| 11 | obj["iv" ] = data.iv; |
| 12 | obj["ciphertext" ] = data.ciphertext; |
| 13 | obj["mac" ] = data.mac; |
| 14 | } |
| 15 | |
| 16 | void |
| 17 | from_json(const nlohmann::json &obj, AesHmacSha2EncryptedData &data) |
| 18 | { |
| 19 | data.iv = obj.at(key: "iv" ).get<std::string>(); |
| 20 | data.ciphertext = obj.at(key: "ciphertext" ).get<std::string>(); |
| 21 | data.mac = obj.at(key: "mac" ).get<std::string>(); |
| 22 | } |
| 23 | |
| 24 | void |
| 25 | to_json(nlohmann::json &obj, const Secret &secret) |
| 26 | { |
| 27 | obj["encrypted" ] = secret.encrypted; |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | from_json(const nlohmann::json &obj, Secret &secret) |
| 32 | { |
| 33 | secret.encrypted = obj.at(key: "encrypted" ).get<decltype(secret.encrypted)>(); |
| 34 | } |
| 35 | |
| 36 | void |
| 37 | to_json(nlohmann::json &obj, const PBKDF2 &desc) |
| 38 | { |
| 39 | obj["algorithm" ] = desc.algorithm; |
| 40 | obj["salt" ] = desc.salt; |
| 41 | obj["iterations" ] = desc.iterations; |
| 42 | obj["bits" ] = desc.bits; |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | from_json(const nlohmann::json &obj, PBKDF2 &desc) |
| 47 | { |
| 48 | desc.algorithm = obj.at(key: "algorithm" ).get<std::string>(); |
| 49 | desc.salt = obj.at(key: "salt" ).get<std::string>(); |
| 50 | desc.iterations = obj.at(key: "iterations" ).get<uint32_t>(); |
| 51 | desc.bits = obj.value(key: "bits" , default_value: std::uint32_t{256}); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | to_json(nlohmann::json &obj, const AesHmacSha2KeyDescription &desc) |
| 56 | { |
| 57 | obj["name" ] = desc.name; |
| 58 | obj["algorithm" ] = desc.algorithm; |
| 59 | |
| 60 | if (desc.passphrase) |
| 61 | obj["passphrase" ] = desc.passphrase.value(); |
| 62 | if (!desc.iv.empty()) |
| 63 | obj["iv" ] = desc.iv; |
| 64 | if (!desc.mac.empty()) |
| 65 | obj["mac" ] = desc.mac; |
| 66 | |
| 67 | if (!desc.signatures.empty()) |
| 68 | obj["signatures" ] = desc.signatures; |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | from_json(const nlohmann::json &obj, AesHmacSha2KeyDescription &desc) |
| 73 | { |
| 74 | desc.name = obj.value(key: "name" , default_value: "" ); // Riot bug, not always present |
| 75 | desc.algorithm = obj.at(key: "algorithm" ).get<std::string>(); |
| 76 | |
| 77 | if (obj.contains(key: "passphrase" )) |
| 78 | desc.passphrase = obj["passphrase" ].get<PBKDF2>(); |
| 79 | desc.iv = obj.value(key: "iv" , default_value: "" ); |
| 80 | desc.mac = obj.value(key: "mac" , default_value: "" ); |
| 81 | |
| 82 | if (obj.contains(key: "signatures" )) |
| 83 | desc.signatures = obj["signatures" ].get<decltype(desc.signatures)>(); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |