Debian Codegraph @

src/CacheCryptoStructs.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 #include <QObject>
8 #include <QQmlEngine>
9
10 #include <map>
11 #include <mutex>
12 #include <set>
13
14 #include <mtx/responses/crypto.hpp>
15 #include <mtxclient/crypto/objects.hpp>
16
17 namespace mtx::events::msg {
18 struct Encrypted;
19 }
20
21 namespace crypto {
22 Q_NAMESPACE
23 QML_NAMED_ELEMENT(Crypto)
24
25 //! How much a participant is trusted.
26 enum Trust
27 {
28 Unverified, //! Device unverified or master key changed.
29 MessageUnverified, //! Only for messages. The sender might be trusted, but we don't know, who
30 //! was the sender for the message.
31 TOFU, //! Device is signed by the sender, but the user is not verified, but they never
32 //! changed the master key.
33 Verified, //! User was verified and has crosssigned this device or device is verified.
34 };
35 Q_ENUM_NS(Trust)
36 }
37
38 struct DeviceKeysToMsgIndex
39 {
40 // map from device key to message_index
41 // Using the device id is safe because we check for reuse on device list updates
42 // Using the device id makes our logic much easier to read.
43 std::map<std::string, uint32_t> deviceids;
44 };
45
46 struct SharedWithUsers
47 {
48 // userid to keys
49 std::map<std::string, DeviceKeysToMsgIndex> keys;
50 };
51
52 // Extra information associated with an outbound megolm session.
53 struct GroupSessionData
54 {
55 uint64_t timestamp = 0;
56 uint32_t message_index = 0;
57
58 // We generally don't trust keys unless they were sent to us by the original sender and include
59 // that senders signature.
60 bool trusted = false;
61
62 // the original 25519 key
63 std::string sender_key;
64
65 std::string sender_claimed_ed25519_key;
66 std::vector<std::string> forwarding_curve25519_key_chain;
67
68 //! map from index to event_id to check for replay attacks
69 std::map<uint32_t, std::string> indices;
70
71 // who has access to this session.
72 // Rotate, when a user leaves the room and share, when a user gets added.
73 SharedWithUsers currently;
74 };
75
76 void
77 to_json(nlohmann::json &obj, const GroupSessionData &msg);
78 void
79 from_json(const nlohmann::json &obj, GroupSessionData &msg);
80
81 struct OutboundGroupSessionDataRef
82 {
83 mtx::crypto::OutboundGroupSessionPtr session;
84 GroupSessionData data;
85 };
86
87 struct DevicePublicKeys
88 {
89 std::string ed25519;
90 std::string curve25519;
91 };
92
93 void
94 to_json(nlohmann::json &obj, const DevicePublicKeys &msg);
95 void
96 from_json(const nlohmann::json &obj, DevicePublicKeys &msg);
97
98 //! Represents a unique megolm session identifier.
99 struct MegolmSessionIndex
100 {
101 MegolmSessionIndex() = default;
102 MegolmSessionIndex(std::string room_id_, const mtx::events::msg::Encrypted &e);
103
104 //! The room in which this session exists.
105 std::string room_id;
106 //! The session_id of the megolm session.
107 std::string session_id;
108 };
109
110 void
111 to_json(nlohmann::json &obj, const MegolmSessionIndex &msg);
112 void
113 from_json(const nlohmann::json &obj, MegolmSessionIndex &msg);
114
115 struct StoredOlmSession
116 {
117 std::uint64_t last_message_ts = 0;
118 std::string pickled_session;
119 };
120 void
121 to_json(nlohmann::json &obj, const StoredOlmSession &msg);
122 void
123 from_json(const nlohmann::json &obj, StoredOlmSession &msg);
124
125 //! Verification status of a single user
126 struct VerificationStatus
127 {
128 //! True, if the users master key is verified
129 crypto::Trust user_verified = crypto::Trust::Unverified;
130 //! List of all devices marked as verified
131 std::set<std::string> verified_devices;
132 //! Map from sender key/curve25519 to trust status
133 std::map<std::string, crypto::Trust> verified_device_keys;
134 //! Count of unverified devices
135 int unverified_device_count = 0;
136 // if the keys are not in cache
137 bool no_keys = false;
138 };
139
140 //! In memory cache of verification status
141 struct VerificationStorage
142 {
143 //! mapping of user to verification status
144 std::map<std::string, VerificationStatus> status;
145 std::mutex verification_storage_mtx;
146 };
147
148 //! In memory cache of verification status
149 struct SecretsStorage
150 {
151 //! secret name -> secret
152 std::map<std::string, std::string> secrets;
153 std::mutex mtx;
154 };
155
156 // this will store the keys of the user with whom a encrypted room is shared with
157 struct UserKeyCache
158 {
159 //! Device id to device keys
160 std::map<std::string, mtx::crypto::DeviceKeys> device_keys;
161 //! cross signing keys
162 mtx::crypto::CrossSigningKeys master_keys, user_signing_keys, self_signing_keys;
163 //! Sync token when nheko last fetched the keys
164 std::string updated_at;
165 //! Sync token when the keys last changed. updated != last_changed means they are outdated.
166 std::string last_changed;
167 //! if the master key has ever changed
168 bool master_key_changed = false;
169 //! Device keys that were already used at least once
170 std::set<std::string> seen_device_keys;
171 //! Device ids that were already used at least once
172 std::set<std::string> seen_device_ids;
173 };
174
175 void
176 to_json(nlohmann::json &j, const UserKeyCache &info);
177 void
178 from_json(const nlohmann::json &j, UserKeyCache &info);
179
180 // the reason these are stored in a seperate cache rather than storing it in the user cache is
181 // UserKeyCache stores only keys of users with which encrypted room is shared
182 struct VerificationCache
183 {
184 //! list of verified device_ids with device-verification
185 std::set<std::string> device_verified;
186 //! list of devices the user blocks
187 std::set<std::string> device_blocked;
188 };
189
190 void
191 to_json(nlohmann::json &j, const VerificationCache &info);
192 void
193 from_json(const nlohmann::json &j, VerificationCache &info);
194
195 struct OnlineBackupVersion
196 {
197 //! the version of the online backup currently enabled
198 std::string version;
199 //! the algorithm used by the backup
200 std::string algorithm;
201 };
202
203 void
204 to_json(nlohmann::json &j, const OnlineBackupVersion &info);
205 void
206 from_json(const nlohmann::json &j, OnlineBackupVersion &info);