| 1 | #include "mtxclient/crypto/types.hpp" |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
| 5 | namespace mtx { |
| 6 | namespace crypto { |
| 7 | |
| 8 | void |
| 9 | to_json(nlohmann::json &obj, const ExportedSession &s) |
| 10 | { |
| 11 | obj["sender_claimed_keys" ] = s.sender_claimed_keys; |
| 12 | obj["forwarding_curve25519_key_chain" ] = s.forwarding_curve25519_key_chain; |
| 13 | |
| 14 | obj["algorithm" ] = s.algorithm; |
| 15 | obj["room_id" ] = s.room_id; |
| 16 | obj["sender_key" ] = s.sender_key; |
| 17 | obj["session_id" ] = s.session_id; |
| 18 | obj["session_key" ] = s.session_key; |
| 19 | } |
| 20 | |
| 21 | void |
| 22 | from_json(const nlohmann::json &obj, ExportedSession &s) |
| 23 | { |
| 24 | s.room_id = obj.at(key: "room_id" ).get<std::string>(); |
| 25 | s.sender_key = obj.at(key: "sender_key" ).get<std::string>(); |
| 26 | s.session_id = obj.at(key: "session_id" ).get<std::string>(); |
| 27 | s.session_key = obj.at(key: "session_key" ).get<std::string>(); |
| 28 | |
| 29 | using ClaimedKeys = std::map<std::string, std::string>; |
| 30 | using KeyChain = std::vector<std::string>; |
| 31 | |
| 32 | if (obj.find(key: "sender_claimed_keys" ) != obj.end()) |
| 33 | s.sender_claimed_keys = obj.at(key: "sender_claimed_keys" ).get<ClaimedKeys>(); |
| 34 | |
| 35 | if (obj.find(key: "forwarding_curve25519_key_chain" ) != obj.end()) |
| 36 | s.forwarding_curve25519_key_chain = |
| 37 | obj.at(key: "forwarding_curve25519_key_chain" ).get<KeyChain>(); |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | to_json(nlohmann::json &obj, const ExportedSessionKeys &keys) |
| 42 | { |
| 43 | obj = keys.sessions; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | from_json(const nlohmann::json &obj, ExportedSessionKeys &keys) |
| 48 | { |
| 49 | try { |
| 50 | keys.sessions = obj.get<std::vector<ExportedSession>>(); |
| 51 | // might be the old format. |
| 52 | } catch (const nlohmann::json::exception &) { |
| 53 | keys.sessions = obj.at(key: "sessions" ).get<std::vector<ExportedSession>>(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | to_json(nlohmann::json &obj, const IdentityKeys &keys) |
| 59 | { |
| 60 | obj[ED25519] = keys.ed25519; |
| 61 | obj[CURVE25519] = keys.curve25519; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | from_json(const nlohmann::json &obj, IdentityKeys &keys) |
| 66 | { |
| 67 | keys.ed25519 = obj.at(key: ED25519).get<std::string>(); |
| 68 | keys.curve25519 = obj.at(key: CURVE25519).get<std::string>(); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | to_json(nlohmann::json &obj, const OneTimeKeys &keys) |
| 73 | { |
| 74 | obj[CURVE25519] = keys.curve25519; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | from_json(const nlohmann::json &obj, OneTimeKeys &keys) |
| 79 | { |
| 80 | keys.curve25519 = obj.at(key: CURVE25519).get<std::map<std::string, std::string>>(); |
| 81 | } |
| 82 | |
| 83 | } // namespace crypto |
| 84 | } // namespace mtx |
| 85 | |