| 1 | #include "mtx/responses/crypto.hpp" |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
| 5 | namespace mtx { |
| 6 | namespace responses { |
| 7 | |
| 8 | void |
| 9 | from_json(const nlohmann::json &obj, UploadKeys &response) |
| 10 | { |
| 11 | response.one_time_key_counts = |
| 12 | obj.at(key: "one_time_key_counts" ).get<std::map<std::string, uint32_t>>(); |
| 13 | } |
| 14 | |
| 15 | void |
| 16 | from_json(const nlohmann::json &obj, QueryKeys &response) |
| 17 | { |
| 18 | if (obj.contains(key: "failures" )) |
| 19 | response.failures = obj.at(key: "failures" ).get<std::map<std::string, nlohmann::json>>(); |
| 20 | if (obj.contains(key: "device_keys" )) |
| 21 | response.device_keys = obj.at(key: "device_keys" ).get<std::map<std::string, DeviceToKeysMap>>(); |
| 22 | if (obj.contains(key: "master_keys" )) |
| 23 | response.master_keys = |
| 24 | obj.at(key: "master_keys" ).get<std::map<std::string, mtx::crypto::CrossSigningKeys>>(); |
| 25 | if (obj.contains(key: "user_signing_keys" )) |
| 26 | response.user_signing_keys = |
| 27 | obj.at(key: "user_signing_keys" ).get<std::map<std::string, mtx::crypto::CrossSigningKeys>>(); |
| 28 | if (obj.contains(key: "self_signing_keys" )) |
| 29 | response.self_signing_keys = |
| 30 | obj.at(key: "self_signing_keys" ).get<std::map<std::string, mtx::crypto::CrossSigningKeys>>(); |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | to_json(nlohmann::json &obj, const QueryKeys &response) |
| 35 | { |
| 36 | obj["failures" ] = response.failures; |
| 37 | obj["device_keys" ] = response.device_keys; |
| 38 | obj["master_keys" ] = response.master_keys; |
| 39 | obj["user_signing_keys" ] = response.user_signing_keys; |
| 40 | obj["self_signing_keys" ] = response.self_signing_keys; |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | from_json(const nlohmann::json &obj, KeySignaturesUpload &response) |
| 45 | { |
| 46 | if (obj.contains(key: "failures" )) |
| 47 | response.errors = obj.at(key: "failures" ).get<decltype(response.errors)>(); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | from_json(const nlohmann::json &obj, ClaimKeys &response) |
| 52 | { |
| 53 | if (obj.contains(key: "failures" )) |
| 54 | response.failures = obj.at(key: "failures" ).get<std::map<std::string, nlohmann::json>>(); |
| 55 | if (obj.contains(key: "one_time_keys" )) |
| 56 | response.one_time_keys = |
| 57 | obj.at(key: "one_time_keys" ) |
| 58 | .get<std::map<std::string, std::map<std::string, nlohmann::json>>>(); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | from_json(const nlohmann::json &obj, KeyChanges &response) |
| 63 | { |
| 64 | if (obj.contains(key: "changed" )) |
| 65 | response.changed = obj.at(key: "changed" ).get<std::vector<std::string>>(); |
| 66 | if (obj.contains(key: "left" )) |
| 67 | response.left = obj.at(key: "left" ).get<std::vector<std::string>>(); |
| 68 | } |
| 69 | |
| 70 | namespace backup { |
| 71 | void |
| 72 | from_json(const nlohmann::json &obj, EncryptedSessionData &response) |
| 73 | { |
| 74 | response.ephemeral = obj.at(key: "ephemeral" ).get<std::string>(); |
| 75 | response.ciphertext = obj.at(key: "ciphertext" ).get<std::string>(); |
| 76 | response.mac = obj.at(key: "mac" ).get<std::string>(); |
| 77 | } |
| 78 | void |
| 79 | to_json(nlohmann::json &obj, const EncryptedSessionData &response) |
| 80 | { |
| 81 | obj["ephemeral" ] = response.ephemeral; |
| 82 | obj["ciphertext" ] = response.ciphertext; |
| 83 | obj["mac" ] = response.mac; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | from_json(const nlohmann::json &obj, SessionBackup &response) |
| 88 | { |
| 89 | response.first_message_index = obj.at(key: "first_message_index" ).get<int64_t>(); |
| 90 | response.forwarded_count = obj.at(key: "forwarded_count" ).get<int64_t>(); |
| 91 | response.is_verified = obj.at(key: "is_verified" ).get<bool>(); |
| 92 | response.session_data = obj.at(key: "session_data" ).get<EncryptedSessionData>(); |
| 93 | } |
| 94 | void |
| 95 | to_json(nlohmann::json &obj, const SessionBackup &response) |
| 96 | { |
| 97 | obj["first_message_index" ] = response.first_message_index; |
| 98 | obj["forwarded_count" ] = response.forwarded_count; |
| 99 | obj["is_verified" ] = response.is_verified; |
| 100 | obj["session_data" ] = response.session_data; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | from_json(const nlohmann::json &obj, RoomKeysBackup &response) |
| 105 | { |
| 106 | response.sessions = obj.at(key: "sessions" ).get<decltype(response.sessions)>(); |
| 107 | } |
| 108 | void |
| 109 | to_json(nlohmann::json &obj, const RoomKeysBackup &response) |
| 110 | { |
| 111 | obj["sessions" ] = response.sessions; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | from_json(const nlohmann::json &obj, KeysBackup &response) |
| 116 | { |
| 117 | response.rooms = obj.at(key: "rooms" ).get<decltype(response.rooms)>(); |
| 118 | } |
| 119 | void |
| 120 | to_json(nlohmann::json &obj, const KeysBackup &response) |
| 121 | { |
| 122 | obj["rooms" ] = response.rooms; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | from_json(const nlohmann::json &obj, BackupVersion &response) |
| 127 | { |
| 128 | response.algorithm = obj.at(key: "algorithm" ).get<std::string>(); |
| 129 | response.auth_data = obj.at(key: "auth_data" ).dump(); |
| 130 | response.count = obj.at(key: "count" ).get<int64_t>(); |
| 131 | response.etag = |
| 132 | obj.at(key: "etag" ).dump(); // workaround, since synapse 1.15.1 and older sends this as integer |
| 133 | response.version = obj.at(key: "version" ).get<std::string>(); |
| 134 | } |
| 135 | void |
| 136 | to_json(nlohmann::json &obj, const BackupVersion &response) |
| 137 | { |
| 138 | obj["algorithm" ] = response.algorithm; |
| 139 | obj["auth_data" ] = nlohmann::json::parse(i: response.auth_data); |
| 140 | obj["count" ] = response.count; |
| 141 | obj["etag" ] = response.etag; |
| 142 | obj["version" ] = response.version; |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | to_json(nlohmann::json &obj, const SessionData &data) |
| 147 | { |
| 148 | obj["algorithm" ] = data.algorithm; |
| 149 | obj["forwarding_curve25519_key_chain" ] = data.forwarding_curve25519_key_chain; |
| 150 | obj["sender_key" ] = data.sender_key; |
| 151 | obj["sender_claimed_keys" ] = data.sender_claimed_keys; |
| 152 | obj["session_key" ] = data.session_key; |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | from_json(const nlohmann::json &obj, SessionData &data) |
| 157 | { |
| 158 | data.algorithm = obj.at(key: "algorithm" ).get<std::string>(); |
| 159 | data.forwarding_curve25519_key_chain = obj.at(key: "forwarding_curve25519_key_chain" ) |
| 160 | .get<decltype(data.forwarding_curve25519_key_chain)>(); |
| 161 | data.sender_key = obj.at(key: "sender_key" ).get<std::string>(); |
| 162 | // required, but some clients don't send it |
| 163 | data.sender_claimed_keys = |
| 164 | obj.value(key: "sender_claimed_keys" , default_value: std::map<std::string, std::string>()); |
| 165 | data.session_key = obj.at(key: "session_key" ).get<std::string>(); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |