Debian Codegraph @

mtxclient/include/mtx/events/encrypted.hpp

raw symbols SCIP
1 #pragma once
2
3 /// @file
4 /// @brief Various event types used in E2EE.
5
6 #if __has_include(<nlohmann/json_fwd.hpp>)
7 #include <nlohmann/json_fwd.hpp>
8 #else
9 #include <nlohmann/json.hpp>
10 #endif
11
12 #include "mtx/events/common.hpp"
13
14 namespace mtx {
15 namespace events {
16 namespace msg {
17
18 //! Display methods for Short Authentication Strings
19 enum class SASMethods
20 {
21 Decimal, //!< Display 3 times 4 digits
22 Emoji, //! Display 7 emoji
23 Unsupported
24 };
25
26 void
27 from_json(const nlohmann::json &obj, SASMethods &method);
28
29 void
30 to_json(nlohmann::json &obj, const SASMethods &method);
31
32 //! The different verification methods
33 enum class VerificationMethods
34 {
35 SASv1, //!< Short Authentication Strings
36 Unsupported //!< Unsupported method
37 };
38
39 void
40 from_json(const nlohmann::json &obj, VerificationMethods &method);
41
42 void
43 to_json(nlohmann::json &obj, const VerificationMethods &method);
44
45 //! Content of an individual message encrypted for a certain key.
46 struct OlmCipherContent
47 {
48 //! Ciphertext of the message.
49 std::string body;
50 /// @brief Olm message type.
51 ///
52 /// `0` for initial pre-key messages.
53 /// `1` for normal messages after the initial exchange.
54 uint8_t type;
55
56 friend void from_json(const nlohmann::json &obj, OlmCipherContent &event);
57
58 friend void to_json(nlohmann::json &obj, const OlmCipherContent &event);
59 };
60
61 //! Content of the `m.room.encrypted` Olm event.
62 struct OlmEncrypted
63 {
64 //! Algorithm used for encrypting this event.
65 std::string algorithm;
66 //! curve25519 key of the sender.
67 std::string sender_key;
68
69 using RecipientKey = std::string;
70 //! Map of recipient curve25519 key to the encrypted message.
71 std::map<RecipientKey, OlmCipherContent> ciphertext;
72
73 friend void from_json(const nlohmann::json &obj, OlmEncrypted &event);
74
75 friend void to_json(nlohmann::json &obj, const OlmEncrypted &event);
76 };
77
78 //! Content of the `m.room.encrypted` event.
79 struct Encrypted
80 {
81 //! Used for one-on-one exchanges.
82 std::string algorithm;
83 //! The actual encrypted payload.
84 std::string ciphertext;
85 //! Sender's device id.
86 std::string device_id;
87 //! The curve25519 device key.
88 std::string sender_key;
89 //! Outbound group session id.
90 std::string session_id;
91 //! Relations like rich replies
92 common::Relations relations;
93
94 friend void from_json(const nlohmann::json &obj, Encrypted &event);
95
96 friend void to_json(nlohmann::json &obj, const Encrypted &event);
97 };
98
99 //! Content of the `m.dummy` event.
100 struct Dummy
101 {
102 friend void from_json(const nlohmann::json &obj, Dummy &event);
103 friend void to_json(nlohmann::json &obj, const Dummy &event);
104 };
105
106 //! Content of the `m.room_key` event.
107 struct RoomKey
108 {
109 /// @brief *Required.* The encryption algorithm the key in this event is to be used with.
110 ///
111 /// Must be 'm.megolm.v1.aes-sha2'.
112 std::string algorithm;
113 std::string room_id; //!< *Required.* The room where the key is used.
114 std::string session_id; //!< *Required.* The ID of the session that the key is for.
115 std::string session_key; //!< *Required.* The key to be exchanged.
116
117 friend void from_json(const nlohmann::json &obj, RoomKey &event);
118 friend void to_json(nlohmann::json &obj, const RoomKey &event);
119 };
120
121 //! Content of the `m.forwarded_room_key` event.
122 struct ForwardedRoomKey
123 {
124 /// @brief *Required.* The encryption algorithm the key in this event is to be used with.
125 std::string algorithm;
126 std::string room_id; //!< *Required.* The room where the key is used.
127 std::string session_id; //!< *Required.* The ID of the session that the key is for.
128 std::string session_key; //!< *Required.* The key to be exchanged.
129
130 //! *Required.* The Curve25519 key of the device which initiated the session originally.
131 std::string sender_key;
132 /// @brief *Required.* The Ed25519 key of the device which initiated the session originally.
133 ///
134 /// It is 'claimed' because the receiving device has no way to tell that the original
135 /// room_key actually came from a device which owns the private part of this key unless they
136 /// have done device verification.
137 std::string sender_claimed_ed25519_key;
138 /// @brief *Required.* Chain of Curve25519 keys.
139 ///
140 /// It starts out empty, but each time the key is forwarded to another device, the previous
141 /// sender in the chain is added to the end of the list. For example, if the key is
142 /// forwarded from A to B to C, this field is empty between A and B, and contains A's
143 /// Curve25519 key between B and C.
144 std::vector<std::string> forwarding_curve25519_key_chain;
145
146 friend void from_json(const nlohmann::json &obj, ForwardedRoomKey &event);
147 friend void to_json(nlohmann::json &obj, const ForwardedRoomKey &event);
148 };
149
150 //! The type of key request.
151 enum class RequestAction
152 {
153 Request, //!< request
154 Cancellation, //!< request_cancellation
155 Unknown, //!< Unknown request action
156 };
157
158 void
159 from_json(const nlohmann::json &obj, RequestAction &action);
160
161 void
162 to_json(nlohmann::json &obj, const RequestAction &action);
163
164 //! A request to share a session key.
165 struct KeyRequest
166 {
167 //! The type of request.
168 RequestAction action;
169
170 /// @brief The encryption algorithm of the session we want keys for.
171 ///
172 /// Always m.megolm.v1.aes-sha2.
173 std::string algorithm;
174 //! The room in which the session was created.
175 std::string room_id;
176 //! The curve25519 key of the session creator.
177 std::string sender_key;
178 //! The session_id of the outbound megolm session.
179 std::string session_id;
180
181 //! A unique identifier for this request.
182 std::string request_id;
183 //! The device requesting the keys.
184 std::string requesting_device_id;
185
186 friend void from_json(const nlohmann::json &obj, KeyRequest &event);
187 friend void to_json(nlohmann::json &obj, const KeyRequest &event);
188 };
189
190 //! Content of the `m.key.verification.request` event
191 struct KeyVerificationRequest
192 {
193 std::optional<std::string> body;
194 //! The device ID which is initiating the request.
195 std::string from_device;
196 //! The device ID to which the key verification request is meant,used only for to-device
197 //! verification
198 std::optional<std::string> to;
199 //! An opaque identifier for the verification request. Must be unique with respect to the
200 //! devices involved.
201 std::optional<std::string> transaction_id;
202 //! must be `key.verification.request`, this field will be needed only in room verification
203 std::optional<std::string> msgtype;
204 //! The verification methods supported by the sender.
205 std::vector<VerificationMethods> methods;
206 //! The POSIX timestamp in milliseconds for when the request was made. If the request is in
207 //! the future by more than 5 minutes or more than 10 minutes in the past, the message
208 //! should be ignored by the receiver.
209 std::optional<uint64_t> timestamp;
210
211 friend void from_json(const nlohmann::json &obj, KeyVerificationRequest &event);
212 friend void to_json(nlohmann::json &obj, const KeyVerificationRequest &event);
213 };
214
215 //! Content of the `m.key.verification.start` event
216 struct KeyVerificationStart
217 {
218 //! The device ID which is initiating the process.
219 std::string from_device;
220 /// @brief An opaque identifier for the verification process.
221 ///
222 /// Must be unique with respect to the devices involved. Must be the same as the
223 /// transaction_id given in the `m.key.verification.request` if this process is originating
224 /// from a request.
225 /// @note Used in verification via to_device messaging
226 std::optional<std::string> transaction_id;
227 //! The verification method to use. Must be 'm.sas.v1'
228 VerificationMethods method = VerificationMethods::SASv1;
229 /// @brief Optional method to use to verify the other user's key with.
230 //
231 // Applicable when the method chosen only verifies one user's key. This field will never be
232 // present if the method verifies keys both ways.
233 /// @note This appears to be unused in SAS verification
234 std::optional<std::string> next_method;
235 /// @brief The key agreement protocols the sending device understands.
236 ///
237 /// Must include at least curve25519.
238 std::vector<std::string> key_agreement_protocols;
239 //! The hash methods the sending device understands. Must include at least sha256.
240 std::vector<std::string> hashes;
241 /// @brief The message authentication codes that the sending device understands.
242 ///
243 /// Must include at least hkdf-hmac-sha256.
244 std::vector<std::string> message_authentication_codes;
245 /// @brief The SAS methods the sending device (and the sending device's user) understands.
246 ///
247 /// Must include at least decimal. Optionally can include emoji.
248 ///
249 /// One of:
250 /// - `decimal`
251 /// - `emoji`
252 std::vector<SASMethods> short_authentication_string;
253 /// @brief This is used for relating this message with previously sent
254 /// `key.verification.request`
255 ///
256 /// @note Will be used only for room-verification msgs where this is used in place of
257 /// transaction_id.
258 common::Relations relations;
259
260 friend void from_json(const nlohmann::json &obj, KeyVerificationStart &event);
261 friend void to_json(nlohmann::json &obj, const KeyVerificationStart &event);
262 };
263
264 //! Implements the `m.key.verification.ready` event
265 struct KeyVerificationReady
266 {
267 //! the deviceId of the device which send the `m.key.verification.request`
268 std::string from_device;
269 //! transactionId of the current flow
270 std::optional<std::string> transaction_id;
271 //! Sends the user the supported methods
272 std::vector<VerificationMethods> methods;
273 //! this is used for relating this message with previously sent
274 //! key.verification.request will be used only for room-verification msgs where this
275 //! is used in place of txnid
276 common::Relations relations;
277
278 friend void from_json(const nlohmann::json &obj, KeyVerificationReady &event);
279 friend void to_json(nlohmann::json &obj, const KeyVerificationReady &event);
280 };
281
282 // ! Implements the `m.key.verification.done` event
283 struct KeyVerificationDone
284 {
285 //! transactionId of the current flow
286 std::optional<std::string> transaction_id;
287 //! this is used for relating this message with previously sent key.verification.request
288 //! will be used only for room-verification msgs where this is used in place of txnid
289 common::Relations relations;
290
291 friend void from_json(const nlohmann::json &obj, KeyVerificationDone &event);
292 friend void to_json(nlohmann::json &obj, const KeyVerificationDone &event);
293 };
294
295 //! Implements the `m.key.verification.accept` event
296 struct KeyVerificationAccept
297 {
298 //! when the method chosen only verifies one user's key. This field will never be present
299 //! if the method verifies keys both ways.
300 std::optional<std::string> transaction_id;
301 //! The verification method to use. Must be 'm.sas.v1'
302 VerificationMethods method = VerificationMethods::SASv1;
303 //! The key agreement protocol the device is choosing to use, out of the options in the
304 //! m.key.verification.start message.
305 std::string key_agreement_protocol;
306 //! The hash method the device is choosing to use, out of the options in the
307 //! m.key.verification.start message.
308 std::string hash;
309 //! The message authentication code the device is choosing to use, out of the options in
310 //! the m.key.verification.start message.
311 std::string message_authentication_code;
312 //! The SAS methods both devices involed in the verification process understand. Must be
313 //! a subset of the options in the m.key.verification.start message.
314 //! One of: ["decimal", "emoji"]
315 std::vector<SASMethods> short_authentication_string;
316 //! The hash (encoded as unpadded base64) of the concatenation of the device's ephemeral
317 //! public key (encoded as unpadded base64) and the canonical JSON representation of the
318 //! m.key.verification.start message.
319 std::string commitment;
320 //! this is used for relating this message with previously sent key.verification.request
321 //! will be used only for room-verification msgs where this is used in place of txnid
322 common::Relations relations;
323
324 friend void from_json(const nlohmann::json &obj, KeyVerificationAccept &event);
325 friend void to_json(nlohmann::json &obj, const KeyVerificationAccept &event);
326 };
327
328 //! implementation of the `m.key.verification.cancel` event
329 struct KeyVerificationCancel
330 {
331 //! The opaque identifier for the verification process/request.
332 std::optional<std::string> transaction_id;
333 //! A human readable description of the code. The client should only rely on this string
334 //! if it does not understand the code.
335 std::string reason;
336 //! The error code for why the process/request was cancelled by the user. Error codes
337 //! should use the Java package naming convention if not in the following list:
338 //! m.user: The user cancelled the verification.
339 //! m.timeout: The verification process timed out. Verification processes can define
340 //! their own timeout parameters.
341 //! m.unknown_transaction: The device does not know about the given transaction ID.
342 //! m.unknown_method: The device does not know how to handle the requested method.
343 //! This should be sent for m.key.verification.start messages and messages
344 //! defined by individual verification processes.
345 //! m.unexpected_message: The device received an unexpected message. Typically raised
346 //! when one of the parties is handling the verification out of order.
347 //! m.key_mismatch: The key was not verified.
348 //! m.user_mismatch: The expected user did not match the user verified.
349 //! m.invalid_message: The message received was invalid.
350 //! m.accepted: A m.key.verification.request was accepted by a different device.
351 //! The device receiving this error can ignore the verification request.
352 //! m.unknown_method: The devices are unable to agree on the key agreement,
353 //! hash, MAC, or SAS method.
354 //! m.mismatched_commitment: The hash commitment did not match.
355 //! m.mismatched_sas: The SAS did not match.
356 //! Clients should be careful to avoid error loops. For example, if a device sends an
357 //! incorrect message and the client returns m.invalid_message to which it gets an
358 //! unexpected response with m.unexpected_message, the client should not respond
359 //! again with m.unexpected_message to avoid the other device potentially sending
360 //! another error response.
361 std::string code;
362 //! this is used for relating this message with previously sent key.verification.request
363 //! will be used only for room-verification msgs where this is used in place of txnid
364 common::Relations relations;
365
366 friend void from_json(const nlohmann::json &obj, KeyVerificationCancel &event);
367 friend void to_json(nlohmann::json &obj, const KeyVerificationCancel &event);
368 };
369
370 struct KeyVerificationKey
371 {
372 //! An opaque identifier for the verification process. Must be the same as the one
373 //! used for the m.key.verification.start message.
374 std::optional<std::string> transaction_id;
375 //! The device's ephemeral public key, encoded as unpadded base64.
376 std::string key;
377 //! this is used for relating this message with previously sent key.verification.request
378 //! will be used only for room-verification msgs where this is used in place of txnid
379 common::Relations relations;
380
381 friend void from_json(const nlohmann::json &obj, KeyVerificationKey &event);
382 friend void to_json(nlohmann::json &obj, const KeyVerificationKey &event);
383 };
384
385 struct KeyVerificationMac
386 {
387 //! An opaque identifier for the verification process. Must be the same as the one
388 //! used for the m.key.verification.start message.
389 std::optional<std::string> transaction_id;
390 //! A map of the key ID to the MAC of the key, using the algorithm in the
391 //! verification process. The MAC is encoded as unpadded base64.
392 std::map<std::string, std::string> mac;
393 //! The MAC of the comma-separated, sorted, list of key IDs given in the mac
394 //! property, encoded as unpadded base64.
395 std::string keys;
396 //! this is used for relating this message with previously sent key.verification.request
397 //! will be used only for room-verification msgs where this is used in place of txnid
398 common::Relations relations;
399
400 friend void from_json(const nlohmann::json &obj, KeyVerificationMac &event);
401 friend void to_json(nlohmann::json &obj, const KeyVerificationMac &event);
402 };
403
404 struct SecretRequest
405 {
406 //! The type of request.
407 RequestAction action;
408
409 //! Required if action is request. The name of the secret that is being requested.
410 std::string name;
411
412 //! A unique identifier for this request.
413 std::string request_id;
414 //! The device requesting the keys.
415 std::string requesting_device_id;
416
417 friend void from_json(const nlohmann::json &obj, SecretRequest &event);
418 friend void to_json(nlohmann::json &obj, const SecretRequest &event);
419 };
420
421 struct SecretSend
422 {
423 //! Required. The contents of the secret.
424 std::string secret;
425
426 //! A unique identifier for this request.
427 std::string request_id;
428
429 friend void from_json(const nlohmann::json &obj, SecretSend &event);
430 friend void to_json(nlohmann::json &obj, const SecretSend &event);
431 };
432
433 } // namespace msg
434 } // namespace events
435 } // namespace mtx