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