Line data Source code
1 : /*
2 : * Famedly Matrix SDK
3 : * Copyright (C) 2019, 2020, 2021 Famedly GmbH
4 : *
5 : * This program is free software: you can redistribute it and/or modify
6 : * it under the terms of the GNU Affero General Public License as
7 : * published by the Free Software Foundation, either version 3 of the
8 : * License, or (at your option) any later version.
9 : *
10 : * This program is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : * GNU Affero General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Affero General Public License
16 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 : */
18 :
19 : import 'dart:async';
20 : import 'dart:convert';
21 :
22 : import 'package:collection/collection.dart';
23 : import 'package:olm/olm.dart' as olm;
24 :
25 : import 'package:matrix/encryption/encryption.dart';
26 : import 'package:matrix/encryption/utils/base64_unpadded.dart';
27 : import 'package:matrix/encryption/utils/outbound_group_session.dart';
28 : import 'package:matrix/encryption/utils/session_key.dart';
29 : import 'package:matrix/encryption/utils/stored_inbound_group_session.dart';
30 : import 'package:matrix/matrix.dart';
31 : import 'package:matrix/src/utils/run_in_root.dart';
32 :
33 : const megolmKey = EventTypes.MegolmBackup;
34 :
35 : class KeyManager {
36 : final Encryption encryption;
37 :
38 72 : Client get client => encryption.client;
39 : final outgoingShareRequests = <String, KeyManagerKeyShareRequest>{};
40 : final incomingShareRequests = <String, KeyManagerKeyShareRequest>{};
41 : final _inboundGroupSessions = <String, Map<String, SessionKey>>{};
42 : final _outboundGroupSessions = <String, OutboundGroupSession>{};
43 : final Set<String> _loadedOutboundGroupSessions = <String>{};
44 : final Set<String> _requestedSessionIds = <String>{};
45 :
46 24 : KeyManager(this.encryption) {
47 73 : encryption.ssss.setValidator(megolmKey, (String secret) async {
48 1 : final keyObj = olm.PkDecryption();
49 : try {
50 1 : final info = await getRoomKeysBackupInfo(false);
51 2 : if (info.algorithm !=
52 : BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2) {
53 : return false;
54 : }
55 3 : return keyObj.init_with_private_key(base64decodeUnpadded(secret)) ==
56 2 : info.authData['public_key'];
57 : } catch (_) {
58 : return false;
59 : } finally {
60 1 : keyObj.free();
61 : }
62 : });
63 73 : encryption.ssss.setCacheCallback(megolmKey, (String secret) {
64 : // we got a megolm key cached, clear our requested keys and try to re-decrypt
65 : // last events
66 2 : _requestedSessionIds.clear();
67 3 : for (final room in client.rooms) {
68 1 : final lastEvent = room.lastEvent;
69 : if (lastEvent != null &&
70 2 : lastEvent.type == EventTypes.Encrypted &&
71 0 : lastEvent.content['can_request_session'] == true) {
72 0 : final sessionId = lastEvent.content.tryGet<String>('session_id');
73 0 : final senderKey = lastEvent.content.tryGet<String>('sender_key');
74 : if (sessionId != null && senderKey != null) {
75 0 : maybeAutoRequest(
76 0 : room.id,
77 : sessionId,
78 : senderKey,
79 : );
80 : }
81 : }
82 : }
83 : });
84 : }
85 :
86 92 : bool get enabled => encryption.ssss.isSecret(megolmKey);
87 :
88 : /// clear all cached inbound group sessions. useful for testing
89 4 : void clearInboundGroupSessions() {
90 8 : _inboundGroupSessions.clear();
91 : }
92 :
93 23 : Future<void> setInboundGroupSession(
94 : String roomId,
95 : String sessionId,
96 : String senderKey,
97 : Map<String, dynamic> content, {
98 : bool forwarded = false,
99 : Map<String, String>? senderClaimedKeys,
100 : bool uploaded = false,
101 : Map<String, Map<String, int>>? allowedAtIndex,
102 : }) async {
103 23 : final senderClaimedKeys_ = senderClaimedKeys ?? <String, String>{};
104 23 : final allowedAtIndex_ = allowedAtIndex ?? <String, Map<String, int>>{};
105 46 : final userId = client.userID;
106 0 : if (userId == null) return Future.value();
107 :
108 23 : if (!senderClaimedKeys_.containsKey('ed25519')) {
109 46 : final device = client.getUserDeviceKeysByCurve25519Key(senderKey);
110 6 : if (device != null && device.ed25519Key != null) {
111 12 : senderClaimedKeys_['ed25519'] = device.ed25519Key!;
112 : }
113 : }
114 23 : final oldSession = getInboundGroupSession(
115 : roomId,
116 : sessionId,
117 : );
118 46 : if (content['algorithm'] != AlgorithmTypes.megolmV1AesSha2) {
119 : return;
120 : }
121 : late olm.InboundGroupSession inboundGroupSession;
122 : try {
123 23 : inboundGroupSession = olm.InboundGroupSession();
124 : if (forwarded) {
125 6 : inboundGroupSession.import_session(content['session_key']);
126 : } else {
127 46 : inboundGroupSession.create(content['session_key']);
128 : }
129 : } catch (e, s) {
130 0 : inboundGroupSession.free();
131 0 : Logs().e('[LibOlm] Could not create new InboundGroupSession', e, s);
132 0 : return Future.value();
133 : }
134 23 : final newSession = SessionKey(
135 : content: content,
136 : inboundGroupSession: inboundGroupSession,
137 23 : indexes: {},
138 : roomId: roomId,
139 : sessionId: sessionId,
140 : key: userId,
141 : senderKey: senderKey,
142 : senderClaimedKeys: senderClaimedKeys_,
143 : allowedAtIndex: allowedAtIndex_,
144 : );
145 : final oldFirstIndex =
146 2 : oldSession?.inboundGroupSession?.first_known_index() ?? 0;
147 46 : final newFirstIndex = newSession.inboundGroupSession!.first_known_index();
148 : if (oldSession == null ||
149 1 : newFirstIndex < oldFirstIndex ||
150 1 : (oldFirstIndex == newFirstIndex &&
151 3 : newSession.forwardingCurve25519KeyChain.length <
152 2 : oldSession.forwardingCurve25519KeyChain.length)) {
153 : // use new session
154 1 : oldSession?.dispose();
155 : } else {
156 : // we are gonna keep our old session
157 1 : newSession.dispose();
158 : return;
159 : }
160 :
161 : final roomInboundGroupSessions =
162 69 : _inboundGroupSessions[roomId] ??= <String, SessionKey>{};
163 23 : roomInboundGroupSessions[sessionId] = newSession;
164 92 : if (!client.isLogged() || client.encryption == null) {
165 : return;
166 : }
167 :
168 46 : final storeFuture = client.database
169 23 : ?.storeInboundGroupSession(
170 : roomId,
171 : sessionId,
172 23 : inboundGroupSession.pickle(userId),
173 23 : json.encode(content),
174 46 : json.encode({}),
175 23 : json.encode(allowedAtIndex_),
176 : senderKey,
177 23 : json.encode(senderClaimedKeys_),
178 : )
179 46 : .then((_) async {
180 92 : if (!client.isLogged() || client.encryption == null) {
181 : return;
182 : }
183 : if (uploaded) {
184 2 : await client.database
185 1 : ?.markInboundGroupSessionAsUploaded(roomId, sessionId);
186 : }
187 : });
188 46 : final room = client.getRoomById(roomId);
189 : if (room != null) {
190 : // attempt to decrypt the last event
191 7 : final event = room.lastEvent;
192 : if (event != null &&
193 14 : event.type == EventTypes.Encrypted &&
194 6 : event.content['session_id'] == sessionId) {
195 4 : final decrypted = encryption.decryptRoomEventSync(roomId, event);
196 4 : if (decrypted.type != EventTypes.Encrypted) {
197 : // Update the last event in memory first
198 2 : room.lastEvent = decrypted;
199 :
200 : // To persist it in database and trigger UI updates:
201 8 : await client.database?.transaction(() async {
202 4 : await client.handleSync(
203 2 : SyncUpdate(
204 : nextBatch: '',
205 2 : rooms: switch (room.membership) {
206 2 : Membership.join =>
207 4 : RoomsUpdate(join: {room.id: JoinedRoomUpdate()}),
208 1 : Membership.ban ||
209 1 : Membership.leave =>
210 4 : RoomsUpdate(leave: {room.id: LeftRoomUpdate()}),
211 0 : Membership.invite =>
212 0 : RoomsUpdate(invite: {room.id: InvitedRoomUpdate()}),
213 0 : Membership.knock =>
214 0 : RoomsUpdate(knock: {room.id: KnockRoomUpdate()}),
215 : },
216 : ),
217 : );
218 : });
219 : }
220 : }
221 : // and finally broadcast the new session
222 14 : room.onSessionKeyReceived.add(sessionId);
223 : }
224 :
225 0 : return storeFuture ?? Future.value();
226 : }
227 :
228 23 : SessionKey? getInboundGroupSession(String roomId, String sessionId) {
229 51 : final sess = _inboundGroupSessions[roomId]?[sessionId];
230 : if (sess != null) {
231 10 : if (sess.sessionId != sessionId && sess.sessionId.isNotEmpty) {
232 : return null;
233 : }
234 : return sess;
235 : }
236 : return null;
237 : }
238 :
239 : /// Attempt auto-request for a key
240 3 : void maybeAutoRequest(
241 : String roomId,
242 : String sessionId,
243 : String? senderKey, {
244 : bool tryOnlineBackup = true,
245 : bool onlineKeyBackupOnly = true,
246 : }) {
247 6 : final room = client.getRoomById(roomId);
248 3 : final requestIdent = '$roomId|$sessionId';
249 : if (room != null &&
250 4 : !_requestedSessionIds.contains(requestIdent) &&
251 4 : !client.isUnknownSession) {
252 : // do e2ee recovery
253 0 : _requestedSessionIds.add(requestIdent);
254 :
255 0 : runInRoot(
256 0 : () async => request(
257 : room,
258 : sessionId,
259 : senderKey,
260 : tryOnlineBackup: tryOnlineBackup,
261 : onlineKeyBackupOnly: onlineKeyBackupOnly,
262 : ),
263 : );
264 : }
265 : }
266 :
267 : /// Loads an inbound group session
268 8 : Future<SessionKey?> loadInboundGroupSession(
269 : String roomId,
270 : String sessionId,
271 : ) async {
272 21 : final sess = _inboundGroupSessions[roomId]?[sessionId];
273 : if (sess != null) {
274 10 : if (sess.sessionId != sessionId && sess.sessionId.isNotEmpty) {
275 : return null; // session_id does not match....better not do anything
276 : }
277 : return sess; // nothing to do
278 : }
279 : final session =
280 15 : await client.database?.getInboundGroupSession(roomId, sessionId);
281 : if (session == null) return null;
282 4 : final userID = client.userID;
283 : if (userID == null) return null;
284 2 : final dbSess = SessionKey.fromDb(session, userID);
285 : final roomInboundGroupSessions =
286 6 : _inboundGroupSessions[roomId] ??= <String, SessionKey>{};
287 2 : if (!dbSess.isValid ||
288 4 : dbSess.sessionId.isEmpty ||
289 4 : dbSess.sessionId != sessionId) {
290 : return null;
291 : }
292 2 : roomInboundGroupSessions[sessionId] = dbSess;
293 : return sess;
294 : }
295 :
296 5 : Map<String, Map<String, bool>> _getDeviceKeyIdMap(
297 : List<DeviceKeys> deviceKeys,
298 : ) {
299 5 : final deviceKeyIds = <String, Map<String, bool>>{};
300 8 : for (final device in deviceKeys) {
301 3 : final deviceId = device.deviceId;
302 : if (deviceId == null) {
303 0 : Logs().w('[KeyManager] ignoring device without deviceid');
304 : continue;
305 : }
306 9 : final userDeviceKeyIds = deviceKeyIds[device.userId] ??= <String, bool>{};
307 6 : userDeviceKeyIds[deviceId] = !device.encryptToDevice;
308 : }
309 : return deviceKeyIds;
310 : }
311 :
312 : /// clear all cached inbound group sessions. useful for testing
313 3 : void clearOutboundGroupSessions() {
314 6 : _outboundGroupSessions.clear();
315 : }
316 :
317 : /// Clears the existing outboundGroupSession but first checks if the participating
318 : /// devices have been changed. Returns false if the session has not been cleared because
319 : /// it wasn't necessary. Otherwise returns true.
320 5 : Future<bool> clearOrUseOutboundGroupSession(
321 : String roomId, {
322 : bool wipe = false,
323 : bool use = true,
324 : }) async {
325 10 : final room = client.getRoomById(roomId);
326 5 : final sess = getOutboundGroupSession(roomId);
327 4 : if (room == null || sess == null || sess.outboundGroupSession == null) {
328 : return true;
329 : }
330 :
331 : if (!wipe) {
332 : // first check if it needs to be rotated
333 : final encryptionContent =
334 6 : room.getState(EventTypes.Encryption)?.parsedRoomEncryptionContent;
335 3 : final maxMessages = encryptionContent?.rotationPeriodMsgs ?? 100;
336 3 : final maxAge = encryptionContent?.rotationPeriodMs ??
337 : 604800000; // default of one week
338 6 : if ((sess.sentMessages ?? maxMessages) >= maxMessages ||
339 3 : sess.creationTime
340 6 : .add(Duration(milliseconds: maxAge))
341 6 : .isBefore(DateTime.now())) {
342 : wipe = true;
343 : }
344 : }
345 :
346 4 : final inboundSess = await loadInboundGroupSession(
347 4 : room.id,
348 8 : sess.outboundGroupSession!.session_id(),
349 : );
350 : if (inboundSess == null) {
351 : wipe = true;
352 : }
353 :
354 : if (!wipe) {
355 : // next check if the devices in the room changed
356 3 : final devicesToReceive = <DeviceKeys>[];
357 3 : final newDeviceKeys = await room.getUserDeviceKeys();
358 3 : final newDeviceKeyIds = _getDeviceKeyIdMap(newDeviceKeys);
359 : // first check for user differences
360 9 : final oldUserIds = Set.from(sess.devices.keys);
361 6 : final newUserIds = Set.from(newDeviceKeyIds.keys);
362 6 : if (oldUserIds.difference(newUserIds).isNotEmpty) {
363 : // a user left the room, we must wipe the session
364 : wipe = true;
365 : } else {
366 3 : final newUsers = newUserIds.difference(oldUserIds);
367 3 : if (newUsers.isNotEmpty) {
368 : // new user! Gotta send the megolm session to them
369 : devicesToReceive
370 5 : .addAll(newDeviceKeys.where((d) => newUsers.contains(d.userId)));
371 : }
372 : // okay, now we must test all the individual user devices, if anything new got blocked
373 : // or if we need to send to any new devices.
374 : // for this it is enough if we iterate over the old user Ids, as the new ones already have the needed keys in the list.
375 : // we also know that all the old user IDs appear in the old one, else we have already wiped the session
376 5 : for (final userId in oldUserIds) {
377 4 : final oldBlockedDevices = sess.devices.containsKey(userId)
378 2 : ? Set.from(
379 6 : sess.devices[userId]!.entries
380 6 : .where((e) => e.value)
381 2 : .map((e) => e.key),
382 : )
383 : : <String>{};
384 2 : final newBlockedDevices = newDeviceKeyIds.containsKey(userId)
385 2 : ? Set.from(
386 2 : newDeviceKeyIds[userId]!
387 2 : .entries
388 6 : .where((e) => e.value)
389 4 : .map((e) => e.key),
390 : )
391 : : <String>{};
392 : // we don't really care about old devices that got dropped (deleted), we only care if new ones got added and if new ones got blocked
393 : // check if new devices got blocked
394 4 : if (newBlockedDevices.difference(oldBlockedDevices).isNotEmpty) {
395 : wipe = true;
396 : break;
397 : }
398 : // and now add all the new devices!
399 4 : final oldDeviceIds = sess.devices.containsKey(userId)
400 2 : ? Set.from(
401 6 : sess.devices[userId]!.entries
402 6 : .where((e) => !e.value)
403 6 : .map((e) => e.key),
404 : )
405 : : <String>{};
406 2 : final newDeviceIds = newDeviceKeyIds.containsKey(userId)
407 2 : ? Set.from(
408 2 : newDeviceKeyIds[userId]!
409 2 : .entries
410 6 : .where((e) => !e.value)
411 6 : .map((e) => e.key),
412 : )
413 : : <String>{};
414 :
415 : // check if a device got removed
416 4 : if (oldDeviceIds.difference(newDeviceIds).isNotEmpty) {
417 : wipe = true;
418 : break;
419 : }
420 :
421 : // check if any new devices need keys
422 2 : final newDevices = newDeviceIds.difference(oldDeviceIds);
423 2 : if (newDeviceIds.isNotEmpty) {
424 2 : devicesToReceive.addAll(
425 2 : newDeviceKeys.where(
426 10 : (d) => d.userId == userId && newDevices.contains(d.deviceId),
427 : ),
428 : );
429 : }
430 : }
431 : }
432 :
433 : if (!wipe) {
434 : if (!use) {
435 : return false;
436 : }
437 : // okay, we use the outbound group session!
438 3 : sess.devices = newDeviceKeyIds;
439 3 : final rawSession = <String, dynamic>{
440 : 'algorithm': AlgorithmTypes.megolmV1AesSha2,
441 3 : 'room_id': room.id,
442 6 : 'session_id': sess.outboundGroupSession!.session_id(),
443 6 : 'session_key': sess.outboundGroupSession!.session_key(),
444 : };
445 : try {
446 5 : devicesToReceive.removeWhere((k) => !k.encryptToDevice);
447 3 : if (devicesToReceive.isNotEmpty) {
448 : // update allowedAtIndex
449 2 : for (final device in devicesToReceive) {
450 4 : inboundSess!.allowedAtIndex[device.userId] ??= <String, int>{};
451 3 : if (!inboundSess.allowedAtIndex[device.userId]!
452 2 : .containsKey(device.curve25519Key) ||
453 0 : inboundSess.allowedAtIndex[device.userId]![
454 0 : device.curve25519Key]! >
455 0 : sess.outboundGroupSession!.message_index()) {
456 : inboundSess
457 5 : .allowedAtIndex[device.userId]![device.curve25519Key!] =
458 2 : sess.outboundGroupSession!.message_index();
459 : }
460 : }
461 3 : await client.database?.updateInboundGroupSessionAllowedAtIndex(
462 2 : json.encode(inboundSess!.allowedAtIndex),
463 1 : room.id,
464 2 : sess.outboundGroupSession!.session_id(),
465 : );
466 : // send out the key
467 2 : await client.sendToDeviceEncryptedChunked(
468 : devicesToReceive,
469 : EventTypes.RoomKey,
470 : rawSession,
471 : );
472 : }
473 : } catch (e, s) {
474 0 : Logs().e(
475 : '[LibOlm] Unable to re-send the session key at later index to new devices',
476 : e,
477 : s,
478 : );
479 : }
480 : return false;
481 : }
482 : }
483 2 : sess.dispose();
484 4 : _outboundGroupSessions.remove(roomId);
485 6 : await client.database?.removeOutboundGroupSession(roomId);
486 : return true;
487 : }
488 :
489 : /// Store an outbound group session in the database
490 5 : Future<void> storeOutboundGroupSession(
491 : String roomId,
492 : OutboundGroupSession sess,
493 : ) async {
494 10 : final userID = client.userID;
495 : if (userID == null) return;
496 15 : await client.database?.storeOutboundGroupSession(
497 : roomId,
498 10 : sess.outboundGroupSession!.pickle(userID),
499 10 : json.encode(sess.devices),
500 10 : sess.creationTime.millisecondsSinceEpoch,
501 : );
502 : }
503 :
504 : final Map<String, Future<OutboundGroupSession>>
505 : _pendingNewOutboundGroupSessions = {};
506 :
507 : /// Creates an outbound group session for a given room id
508 5 : Future<OutboundGroupSession> createOutboundGroupSession(String roomId) async {
509 10 : final sess = _pendingNewOutboundGroupSessions[roomId];
510 : if (sess != null) {
511 : return sess;
512 : }
513 10 : final newSess = _pendingNewOutboundGroupSessions[roomId] =
514 5 : _createOutboundGroupSession(roomId);
515 :
516 : try {
517 : await newSess;
518 : } finally {
519 5 : _pendingNewOutboundGroupSessions
520 15 : .removeWhere((_, value) => value == newSess);
521 : }
522 :
523 : return newSess;
524 : }
525 :
526 : /// Prepares an outbound group session for a given room ID. That is, load it from
527 : /// the database, cycle it if needed and create it if absent.
528 1 : Future<void> prepareOutboundGroupSession(String roomId) async {
529 1 : if (getOutboundGroupSession(roomId) == null) {
530 0 : await loadOutboundGroupSession(roomId);
531 : }
532 1 : await clearOrUseOutboundGroupSession(roomId, use: false);
533 1 : if (getOutboundGroupSession(roomId) == null) {
534 1 : await createOutboundGroupSession(roomId);
535 : }
536 : }
537 :
538 5 : Future<OutboundGroupSession> _createOutboundGroupSession(
539 : String roomId,
540 : ) async {
541 5 : await clearOrUseOutboundGroupSession(roomId, wipe: true);
542 10 : await client.firstSyncReceived;
543 10 : final room = client.getRoomById(roomId);
544 : if (room == null) {
545 0 : throw Exception(
546 0 : 'Tried to create a megolm session in a non-existing room ($roomId)!',
547 : );
548 : }
549 10 : final userID = client.userID;
550 : if (userID == null) {
551 0 : throw Exception(
552 : 'Tried to create a megolm session without being logged in!',
553 : );
554 : }
555 :
556 5 : final deviceKeys = await room.getUserDeviceKeys();
557 5 : final deviceKeyIds = _getDeviceKeyIdMap(deviceKeys);
558 11 : deviceKeys.removeWhere((k) => !k.encryptToDevice);
559 5 : final outboundGroupSession = olm.OutboundGroupSession();
560 : try {
561 5 : outboundGroupSession.create();
562 : } catch (e, s) {
563 0 : outboundGroupSession.free();
564 0 : Logs().e('[LibOlm] Unable to create new outboundGroupSession', e, s);
565 : rethrow;
566 : }
567 5 : final rawSession = <String, dynamic>{
568 : 'algorithm': AlgorithmTypes.megolmV1AesSha2,
569 5 : 'room_id': room.id,
570 5 : 'session_id': outboundGroupSession.session_id(),
571 5 : 'session_key': outboundGroupSession.session_key(),
572 : };
573 5 : final allowedAtIndex = <String, Map<String, int>>{};
574 8 : for (final device in deviceKeys) {
575 3 : if (!device.isValid) {
576 0 : Logs().e('Skipping invalid device');
577 : continue;
578 : }
579 9 : allowedAtIndex[device.userId] ??= <String, int>{};
580 12 : allowedAtIndex[device.userId]![device.curve25519Key!] =
581 3 : outboundGroupSession.message_index();
582 : }
583 5 : await setInboundGroupSession(
584 : roomId,
585 5 : rawSession['session_id'],
586 10 : encryption.identityKey!,
587 : rawSession,
588 : allowedAtIndex: allowedAtIndex,
589 : );
590 5 : final sess = OutboundGroupSession(
591 : devices: deviceKeyIds,
592 5 : creationTime: DateTime.now(),
593 : outboundGroupSession: outboundGroupSession,
594 : key: userID,
595 : );
596 : try {
597 10 : await client.sendToDeviceEncryptedChunked(
598 : deviceKeys,
599 : EventTypes.RoomKey,
600 : rawSession,
601 : );
602 5 : await storeOutboundGroupSession(roomId, sess);
603 10 : _outboundGroupSessions[roomId] = sess;
604 : } catch (e, s) {
605 0 : Logs().e(
606 : '[LibOlm] Unable to send the session key to the participating devices',
607 : e,
608 : s,
609 : );
610 0 : sess.dispose();
611 : rethrow;
612 : }
613 : return sess;
614 : }
615 :
616 : /// Get an outbound group session for a room id
617 5 : OutboundGroupSession? getOutboundGroupSession(String roomId) {
618 10 : return _outboundGroupSessions[roomId];
619 : }
620 :
621 : /// Load an outbound group session from database
622 3 : Future<void> loadOutboundGroupSession(String roomId) async {
623 6 : final database = client.database;
624 6 : final userID = client.userID;
625 6 : if (_loadedOutboundGroupSessions.contains(roomId) ||
626 6 : _outboundGroupSessions.containsKey(roomId) ||
627 : database == null ||
628 : userID == null) {
629 : return; // nothing to do
630 : }
631 6 : _loadedOutboundGroupSessions.add(roomId);
632 3 : final sess = await database.getOutboundGroupSession(
633 : roomId,
634 : userID,
635 : );
636 1 : if (sess == null || !sess.isValid) {
637 : return;
638 : }
639 2 : _outboundGroupSessions[roomId] = sess;
640 : }
641 :
642 23 : Future<bool> isCached() async {
643 46 : await client.accountDataLoading;
644 23 : if (!enabled) {
645 : return false;
646 : }
647 46 : await client.userDeviceKeysLoading;
648 69 : return (await encryption.ssss.getCached(megolmKey)) != null;
649 : }
650 :
651 : GetRoomKeysVersionCurrentResponse? _roomKeysVersionCache;
652 : DateTime? _roomKeysVersionCacheDate;
653 :
654 5 : Future<GetRoomKeysVersionCurrentResponse> getRoomKeysBackupInfo([
655 : bool useCache = true,
656 : ]) async {
657 5 : if (_roomKeysVersionCache != null &&
658 3 : _roomKeysVersionCacheDate != null &&
659 : useCache &&
660 1 : DateTime.now()
661 2 : .subtract(Duration(minutes: 5))
662 2 : .isBefore(_roomKeysVersionCacheDate!)) {
663 1 : return _roomKeysVersionCache!;
664 : }
665 15 : _roomKeysVersionCache = await client.getRoomKeysVersionCurrent();
666 10 : _roomKeysVersionCacheDate = DateTime.now();
667 5 : return _roomKeysVersionCache!;
668 : }
669 :
670 1 : Future<void> loadFromResponse(RoomKeys keys) async {
671 1 : if (!(await isCached())) {
672 : return;
673 : }
674 : final privateKey =
675 4 : base64decodeUnpadded((await encryption.ssss.getCached(megolmKey))!);
676 1 : final decryption = olm.PkDecryption();
677 1 : final info = await getRoomKeysBackupInfo();
678 : String backupPubKey;
679 : try {
680 1 : backupPubKey = decryption.init_with_private_key(privateKey);
681 :
682 2 : if (info.algorithm != BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2 ||
683 3 : info.authData['public_key'] != backupPubKey) {
684 : return;
685 : }
686 3 : for (final roomEntry in keys.rooms.entries) {
687 1 : final roomId = roomEntry.key;
688 4 : for (final sessionEntry in roomEntry.value.sessions.entries) {
689 1 : final sessionId = sessionEntry.key;
690 1 : final session = sessionEntry.value;
691 1 : final sessionData = session.sessionData;
692 : Map<String, Object?>? decrypted;
693 : try {
694 1 : decrypted = json.decode(
695 1 : decryption.decrypt(
696 1 : sessionData['ephemeral'] as String,
697 1 : sessionData['mac'] as String,
698 1 : sessionData['ciphertext'] as String,
699 : ),
700 : );
701 : } catch (e, s) {
702 0 : Logs().e('[LibOlm] Error decrypting room key', e, s);
703 : }
704 1 : final senderKey = decrypted?.tryGet<String>('sender_key');
705 : if (decrypted != null && senderKey != null) {
706 1 : decrypted['session_id'] = sessionId;
707 1 : decrypted['room_id'] = roomId;
708 1 : await setInboundGroupSession(
709 : roomId,
710 : sessionId,
711 : senderKey,
712 : decrypted,
713 : forwarded: true,
714 : senderClaimedKeys:
715 1 : decrypted.tryGetMap<String, String>('sender_claimed_keys') ??
716 0 : <String, String>{},
717 : uploaded: true,
718 : );
719 : }
720 : }
721 : }
722 : } finally {
723 1 : decryption.free();
724 : }
725 : }
726 :
727 : /// Loads and stores all keys from the online key backup. This may take a
728 : /// while for older and big accounts.
729 1 : Future<void> loadAllKeys() async {
730 1 : final info = await getRoomKeysBackupInfo();
731 3 : final ret = await client.getRoomKeys(info.version);
732 1 : await loadFromResponse(ret);
733 : }
734 :
735 : /// Loads all room keys for a single room and stores them. This may take a
736 : /// while for older and big rooms.
737 1 : Future<void> loadAllKeysFromRoom(String roomId) async {
738 1 : final info = await getRoomKeysBackupInfo();
739 3 : final ret = await client.getRoomKeysByRoomId(roomId, info.version);
740 2 : final keys = RoomKeys.fromJson({
741 1 : 'rooms': {
742 1 : roomId: {
743 5 : 'sessions': ret.sessions.map((k, s) => MapEntry(k, s.toJson())),
744 : },
745 : },
746 : });
747 1 : await loadFromResponse(keys);
748 : }
749 :
750 : /// Loads a single key for the specified room from the online key backup
751 : /// and stores it.
752 1 : Future<void> loadSingleKey(String roomId, String sessionId) async {
753 1 : final info = await getRoomKeysBackupInfo();
754 : final ret =
755 3 : await client.getRoomKeyBySessionId(roomId, sessionId, info.version);
756 2 : final keys = RoomKeys.fromJson({
757 1 : 'rooms': {
758 1 : roomId: {
759 1 : 'sessions': {
760 1 : sessionId: ret.toJson(),
761 : },
762 : },
763 : },
764 : });
765 1 : await loadFromResponse(keys);
766 : }
767 :
768 : /// Request a certain key from another device
769 3 : Future<void> request(
770 : Room room,
771 : String sessionId,
772 : String? senderKey, {
773 : bool tryOnlineBackup = true,
774 : bool onlineKeyBackupOnly = false,
775 : }) async {
776 2 : if (tryOnlineBackup && await isCached()) {
777 : // let's first check our online key backup store thingy...
778 2 : final hadPreviously = getInboundGroupSession(room.id, sessionId) != null;
779 : try {
780 2 : await loadSingleKey(room.id, sessionId);
781 : } catch (err, stacktrace) {
782 0 : if (err is MatrixException && err.errcode == 'M_NOT_FOUND') {
783 0 : Logs().i(
784 : '[KeyManager] Key not in online key backup, requesting it from other devices...',
785 : );
786 : } else {
787 0 : Logs().e(
788 : '[KeyManager] Failed to access online key backup',
789 : err,
790 : stacktrace,
791 : );
792 : }
793 : }
794 : // TODO: also don't request from others if we have an index of 0 now
795 : if (!hadPreviously &&
796 2 : getInboundGroupSession(room.id, sessionId) != null) {
797 : return; // we managed to load the session from online backup, no need to care about it now
798 : }
799 : }
800 : if (onlineKeyBackupOnly) {
801 : return; // we only want to do the online key backup
802 : }
803 : try {
804 : // while we just send the to-device event to '*', we still need to save the
805 : // devices themself to know where to send the cancel to after receiving a reply
806 2 : final devices = await room.getUserDeviceKeys();
807 4 : final requestId = client.generateUniqueTransactionId();
808 2 : final request = KeyManagerKeyShareRequest(
809 : requestId: requestId,
810 : devices: devices,
811 : room: room,
812 : sessionId: sessionId,
813 : );
814 2 : final userList = await room.requestParticipants();
815 4 : await client.sendToDevicesOfUserIds(
816 6 : userList.map<String>((u) => u.id).toSet(),
817 : EventTypes.RoomKeyRequest,
818 2 : {
819 : 'action': 'request',
820 2 : 'body': {
821 2 : 'algorithm': AlgorithmTypes.megolmV1AesSha2,
822 4 : 'room_id': room.id,
823 2 : 'session_id': sessionId,
824 2 : if (senderKey != null) 'sender_key': senderKey,
825 : },
826 : 'request_id': requestId,
827 4 : 'requesting_device_id': client.deviceID,
828 : },
829 : );
830 6 : outgoingShareRequests[request.requestId] = request;
831 : } catch (e, s) {
832 0 : Logs().e('[Key Manager] Sending key verification request failed', e, s);
833 : }
834 : }
835 :
836 : Future<void>? _uploadingFuture;
837 :
838 24 : void startAutoUploadKeys() {
839 144 : _uploadKeysOnSync = encryption.client.onSync.stream.listen(
840 48 : (_) async => uploadInboundGroupSessions(skipIfInProgress: true),
841 : );
842 : }
843 :
844 : /// This task should be performed after sync processing but should not block
845 : /// the sync. To make sure that it never gets executed multiple times, it is
846 : /// skipped when an upload task is already in progress. Set `skipIfInProgress`
847 : /// to `false` to await the pending upload task instead.
848 24 : Future<void> uploadInboundGroupSessions({
849 : bool skipIfInProgress = false,
850 : }) async {
851 48 : final database = client.database;
852 48 : final userID = client.userID;
853 : if (database == null || userID == null) {
854 : return;
855 : }
856 :
857 : // Make sure to not run in parallel
858 23 : if (_uploadingFuture != null) {
859 : if (skipIfInProgress) return;
860 : try {
861 0 : await _uploadingFuture;
862 : } finally {
863 : // shouldn't be necessary, since it will be unset already by the other process that started it, but just to be safe, also unset the future here
864 0 : _uploadingFuture = null;
865 : }
866 : }
867 :
868 23 : Future<void> uploadInternal() async {
869 : try {
870 46 : await client.userDeviceKeysLoading;
871 :
872 23 : if (!(await isCached())) {
873 : return; // we can't backup anyways
874 : }
875 5 : final dbSessions = await database.getInboundGroupSessionsToUpload();
876 5 : if (dbSessions.isEmpty) {
877 : return; // nothing to do
878 : }
879 : final privateKey =
880 20 : base64decodeUnpadded((await encryption.ssss.getCached(megolmKey))!);
881 : // decryption is needed to calculate the public key and thus see if the claimed information is in fact valid
882 5 : final decryption = olm.PkDecryption();
883 5 : final info = await getRoomKeysBackupInfo(false);
884 : String backupPubKey;
885 : try {
886 5 : backupPubKey = decryption.init_with_private_key(privateKey);
887 :
888 10 : if (info.algorithm !=
889 : BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2 ||
890 15 : info.authData['public_key'] != backupPubKey) {
891 1 : decryption.free();
892 : return;
893 : }
894 4 : final args = GenerateUploadKeysArgs(
895 : pubkey: backupPubKey,
896 4 : dbSessions: <DbInboundGroupSessionBundle>[],
897 : userId: userID,
898 : );
899 : // we need to calculate verified beforehand, as else we pass a closure to an isolate
900 : // with 500 keys they do, however, noticably block the UI, which is why we give brief async suspentions in here
901 : // so that the event loop can progress
902 : var i = 0;
903 8 : for (final dbSession in dbSessions) {
904 : final device =
905 12 : client.getUserDeviceKeysByCurve25519Key(dbSession.senderKey);
906 8 : args.dbSessions.add(
907 4 : DbInboundGroupSessionBundle(
908 : dbSession: dbSession,
909 4 : verified: device?.verified ?? false,
910 : ),
911 : );
912 4 : i++;
913 4 : if (i > 10) {
914 0 : await Future.delayed(Duration(milliseconds: 1));
915 : i = 0;
916 : }
917 : }
918 : final roomKeys =
919 12 : await client.nativeImplementations.generateUploadKeys(args);
920 16 : Logs().i('[Key Manager] Uploading ${dbSessions.length} room keys...');
921 : // upload the payload...
922 12 : await client.putRoomKeys(info.version, roomKeys);
923 : // and now finally mark all the keys as uploaded
924 : // no need to optimze this, as we only run it so seldomly and almost never with many keys at once
925 8 : for (final dbSession in dbSessions) {
926 4 : await database.markInboundGroupSessionAsUploaded(
927 4 : dbSession.roomId,
928 4 : dbSession.sessionId,
929 : );
930 : }
931 : } finally {
932 5 : decryption.free();
933 : }
934 : } catch (e, s) {
935 2 : Logs().e('[Key Manager] Error uploading room keys', e, s);
936 : }
937 : }
938 :
939 46 : _uploadingFuture = uploadInternal();
940 : try {
941 23 : await _uploadingFuture;
942 : } finally {
943 23 : _uploadingFuture = null;
944 : }
945 : }
946 :
947 : /// Handle an incoming to_device event that is related to key sharing
948 23 : Future<void> handleToDeviceEvent(ToDeviceEvent event) async {
949 46 : if (event.type == EventTypes.RoomKeyRequest) {
950 3 : if (event.content['request_id'] is! String) {
951 : return; // invalid event
952 : }
953 3 : if (event.content['action'] == 'request') {
954 : // we are *receiving* a request
955 2 : Logs().i(
956 4 : '[KeyManager] Received key sharing request from ${event.sender}:${event.content['requesting_device_id']}...',
957 : );
958 2 : if (!event.content.containsKey('body')) {
959 2 : Logs().w('[KeyManager] No body, doing nothing');
960 : return; // no body
961 : }
962 2 : final body = event.content.tryGetMap<String, Object?>('body');
963 : if (body == null) {
964 0 : Logs().w('[KeyManager] Wrong type for body, doing nothing');
965 : return; // wrong type for body
966 : }
967 1 : final roomId = body.tryGet<String>('room_id');
968 : if (roomId == null) {
969 0 : Logs().w(
970 : '[KeyManager] Wrong type for room_id or no room_id, doing nothing',
971 : );
972 : return; // wrong type for roomId or no roomId found
973 : }
974 4 : final device = client.userDeviceKeys[event.sender]
975 4 : ?.deviceKeys[event.content['requesting_device_id']];
976 : if (device == null) {
977 2 : Logs().w('[KeyManager] Device not found, doing nothing');
978 : return; // device not found
979 : }
980 4 : if (device.userId == client.userID &&
981 4 : device.deviceId == client.deviceID) {
982 0 : Logs().i('[KeyManager] Request is by ourself, ignoring');
983 : return; // ignore requests by ourself
984 : }
985 2 : final room = client.getRoomById(roomId);
986 : if (room == null) {
987 2 : Logs().i('[KeyManager] Unknown room, ignoring');
988 : return; // unknown room
989 : }
990 1 : final sessionId = body.tryGet<String>('session_id');
991 : if (sessionId == null) {
992 0 : Logs().w(
993 : '[KeyManager] Wrong type for session_id or no session_id, doing nothing',
994 : );
995 : return; // wrong type for session_id
996 : }
997 : // okay, let's see if we have this session at all
998 2 : final session = await loadInboundGroupSession(room.id, sessionId);
999 : if (session == null) {
1000 2 : Logs().i('[KeyManager] Unknown session, ignoring');
1001 : return; // we don't have this session anyways
1002 : }
1003 3 : if (event.content['request_id'] is! String) {
1004 0 : Logs().w(
1005 : '[KeyManager] Wrong type for request_id or no request_id, doing nothing',
1006 : );
1007 : return; // wrong type for request_id
1008 : }
1009 1 : final request = KeyManagerKeyShareRequest(
1010 2 : requestId: event.content.tryGet<String>('request_id')!,
1011 1 : devices: [device],
1012 : room: room,
1013 : sessionId: sessionId,
1014 : );
1015 3 : if (incomingShareRequests.containsKey(request.requestId)) {
1016 0 : Logs().i('[KeyManager] Already processed this request, ignoring');
1017 : return; // we don't want to process one and the same request multiple times
1018 : }
1019 3 : incomingShareRequests[request.requestId] = request;
1020 : final roomKeyRequest =
1021 1 : RoomKeyRequest.fromToDeviceEvent(event, this, request);
1022 4 : if (device.userId == client.userID &&
1023 1 : device.verified &&
1024 1 : !device.blocked) {
1025 2 : Logs().i('[KeyManager] All checks out, forwarding key...');
1026 : // alright, we can forward the key
1027 1 : await roomKeyRequest.forwardKey();
1028 1 : } else if (device.encryptToDevice &&
1029 1 : session.allowedAtIndex
1030 2 : .tryGet<Map<String, Object?>>(device.userId)
1031 2 : ?.tryGet(device.curve25519Key!) !=
1032 : null) {
1033 : // if we know the user may see the message, then we can just forward the key.
1034 : // we do not need to check if the device is verified, just if it is not blocked,
1035 : // as that is the logic we already initially try to send out the room keys.
1036 : final index =
1037 5 : session.allowedAtIndex[device.userId]![device.curve25519Key]!;
1038 2 : Logs().i(
1039 1 : '[KeyManager] Valid foreign request, forwarding key at index $index...',
1040 : );
1041 1 : await roomKeyRequest.forwardKey(index);
1042 : } else {
1043 1 : Logs()
1044 1 : .i('[KeyManager] Asking client, if the key should be forwarded');
1045 2 : client.onRoomKeyRequest
1046 1 : .add(roomKeyRequest); // let the client handle this
1047 : }
1048 0 : } else if (event.content['action'] == 'request_cancellation') {
1049 : // we got told to cancel an incoming request
1050 0 : if (!incomingShareRequests.containsKey(event.content['request_id'])) {
1051 : return; // we don't know this request anyways
1052 : }
1053 : // alright, let's just cancel this request
1054 0 : final request = incomingShareRequests[event.content['request_id']]!;
1055 0 : request.canceled = true;
1056 0 : incomingShareRequests.remove(request.requestId);
1057 : }
1058 46 : } else if (event.type == EventTypes.ForwardedRoomKey) {
1059 : // we *received* an incoming key request
1060 1 : final encryptedContent = event.encryptedContent;
1061 : if (encryptedContent == null) {
1062 2 : Logs().w(
1063 : 'Ignoring an unencrypted forwarded key from a to device message',
1064 1 : event.toJson(),
1065 : );
1066 : return;
1067 : }
1068 3 : final request = outgoingShareRequests.values.firstWhereOrNull(
1069 1 : (r) =>
1070 5 : r.room.id == event.content['room_id'] &&
1071 4 : r.sessionId == event.content['session_id'],
1072 : );
1073 1 : if (request == null || request.canceled) {
1074 : return; // no associated request found or it got canceled
1075 : }
1076 2 : final device = request.devices.firstWhereOrNull(
1077 1 : (d) =>
1078 3 : d.userId == event.sender &&
1079 3 : d.curve25519Key == encryptedContent['sender_key'],
1080 : );
1081 : if (device == null) {
1082 : return; // someone we didn't send our request to replied....better ignore this
1083 : }
1084 : // we add the sender key to the forwarded key chain
1085 3 : if (event.content['forwarding_curve25519_key_chain'] is! List) {
1086 0 : event.content['forwarding_curve25519_key_chain'] = <String>[];
1087 : }
1088 2 : (event.content['forwarding_curve25519_key_chain'] as List)
1089 2 : .add(encryptedContent['sender_key']);
1090 3 : if (event.content['sender_claimed_ed25519_key'] is! String) {
1091 0 : Logs().w('sender_claimed_ed255519_key has wrong type');
1092 : return; // wrong type
1093 : }
1094 : // TODO: verify that the keys work to decrypt a message
1095 : // alright, all checks out, let's go ahead and store this session
1096 1 : await setInboundGroupSession(
1097 2 : request.room.id,
1098 1 : request.sessionId,
1099 1 : device.curve25519Key!,
1100 1 : event.content,
1101 : forwarded: true,
1102 1 : senderClaimedKeys: {
1103 2 : 'ed25519': event.content['sender_claimed_ed25519_key'] as String,
1104 : },
1105 : );
1106 2 : request.devices.removeWhere(
1107 7 : (k) => k.userId == device.userId && k.deviceId == device.deviceId,
1108 : );
1109 3 : outgoingShareRequests.remove(request.requestId);
1110 : // send cancel to all other devices
1111 2 : if (request.devices.isEmpty) {
1112 : return; // no need to send any cancellation
1113 : }
1114 : // Send with send-to-device messaging
1115 1 : final sendToDeviceMessage = {
1116 : 'action': 'request_cancellation',
1117 1 : 'request_id': request.requestId,
1118 2 : 'requesting_device_id': client.deviceID,
1119 : };
1120 1 : final data = <String, Map<String, Map<String, dynamic>>>{};
1121 2 : for (final device in request.devices) {
1122 3 : final userData = data[device.userId] ??= {};
1123 2 : userData[device.deviceId!] = sendToDeviceMessage;
1124 : }
1125 2 : await client.sendToDevice(
1126 : EventTypes.RoomKeyRequest,
1127 2 : client.generateUniqueTransactionId(),
1128 : data,
1129 : );
1130 46 : } else if (event.type == EventTypes.RoomKey) {
1131 46 : Logs().v(
1132 69 : '[KeyManager] Received room key with session ${event.content['session_id']}',
1133 : );
1134 23 : final encryptedContent = event.encryptedContent;
1135 : if (encryptedContent == null) {
1136 2 : Logs().v('[KeyManager] not encrypted, ignoring...');
1137 : return; // the event wasn't encrypted, this is a security risk;
1138 : }
1139 46 : final roomId = event.content.tryGet<String>('room_id');
1140 46 : final sessionId = event.content.tryGet<String>('session_id');
1141 : if (roomId == null || sessionId == null) {
1142 0 : Logs().w(
1143 : 'Either room_id or session_id are not the expected type or missing',
1144 : );
1145 : return;
1146 : }
1147 92 : final sender_ed25519 = client.userDeviceKeys[event.sender]
1148 4 : ?.deviceKeys[event.content['requesting_device_id']]?.ed25519Key;
1149 : if (sender_ed25519 != null) {
1150 0 : event.content['sender_claimed_ed25519_key'] = sender_ed25519;
1151 : }
1152 46 : Logs().v('[KeyManager] Keeping room key');
1153 23 : await setInboundGroupSession(
1154 : roomId,
1155 : sessionId,
1156 23 : encryptedContent['sender_key'],
1157 23 : event.content,
1158 : forwarded: false,
1159 : );
1160 : }
1161 : }
1162 :
1163 : StreamSubscription<SyncUpdate>? _uploadKeysOnSync;
1164 :
1165 21 : void dispose() {
1166 : // ignore: discarded_futures
1167 42 : _uploadKeysOnSync?.cancel();
1168 46 : for (final sess in _outboundGroupSessions.values) {
1169 4 : sess.dispose();
1170 : }
1171 62 : for (final entries in _inboundGroupSessions.values) {
1172 40 : for (final sess in entries.values) {
1173 20 : sess.dispose();
1174 : }
1175 : }
1176 : }
1177 : }
1178 :
1179 : class KeyManagerKeyShareRequest {
1180 : final String requestId;
1181 : final List<DeviceKeys> devices;
1182 : final Room room;
1183 : final String sessionId;
1184 : bool canceled;
1185 :
1186 2 : KeyManagerKeyShareRequest({
1187 : required this.requestId,
1188 : List<DeviceKeys>? devices,
1189 : required this.room,
1190 : required this.sessionId,
1191 : this.canceled = false,
1192 0 : }) : devices = devices ?? [];
1193 : }
1194 :
1195 : class RoomKeyRequest extends ToDeviceEvent {
1196 : KeyManager keyManager;
1197 : KeyManagerKeyShareRequest request;
1198 :
1199 1 : RoomKeyRequest.fromToDeviceEvent(
1200 : ToDeviceEvent toDeviceEvent,
1201 : this.keyManager,
1202 : this.request,
1203 1 : ) : super(
1204 1 : sender: toDeviceEvent.sender,
1205 1 : content: toDeviceEvent.content,
1206 1 : type: toDeviceEvent.type,
1207 : );
1208 :
1209 3 : Room get room => request.room;
1210 :
1211 4 : DeviceKeys get requestingDevice => request.devices.first;
1212 :
1213 1 : Future<void> forwardKey([int? index]) async {
1214 2 : if (request.canceled) {
1215 0 : keyManager.incomingShareRequests.remove(request.requestId);
1216 : return; // request is canceled, don't send anything
1217 : }
1218 1 : final room = this.room;
1219 : final session =
1220 5 : await keyManager.loadInboundGroupSession(room.id, request.sessionId);
1221 1 : if (session?.inboundGroupSession == null) {
1222 0 : Logs().v("[KeyManager] Not forwarding key we don't have");
1223 : return;
1224 : }
1225 :
1226 2 : final message = session!.content.copy();
1227 1 : message['forwarding_curve25519_key_chain'] =
1228 2 : List<String>.from(session.forwardingCurve25519KeyChain);
1229 :
1230 2 : if (session.senderKey.isNotEmpty) {
1231 2 : message['sender_key'] = session.senderKey;
1232 : }
1233 1 : message['sender_claimed_ed25519_key'] =
1234 2 : session.senderClaimedKeys['ed25519'] ??
1235 2 : (session.forwardingCurve25519KeyChain.isEmpty
1236 3 : ? keyManager.encryption.fingerprintKey
1237 : : null);
1238 3 : message['session_key'] = session.inboundGroupSession!.export_session(
1239 2 : index ?? session.inboundGroupSession!.first_known_index(),
1240 : );
1241 : // send the actual reply of the key back to the requester
1242 3 : await keyManager.client.sendToDeviceEncrypted(
1243 2 : [requestingDevice],
1244 : EventTypes.ForwardedRoomKey,
1245 : message,
1246 : );
1247 5 : keyManager.incomingShareRequests.remove(request.requestId);
1248 : }
1249 : }
1250 :
1251 : /// you would likely want to use [NativeImplementations] and
1252 : /// [Client.nativeImplementations] instead
1253 4 : RoomKeys generateUploadKeysImplementation(GenerateUploadKeysArgs args) {
1254 4 : final enc = olm.PkEncryption();
1255 : try {
1256 8 : enc.set_recipient_key(args.pubkey);
1257 : // first we generate the payload to upload all the session keys in this chunk
1258 8 : final roomKeys = RoomKeys(rooms: {});
1259 8 : for (final dbSession in args.dbSessions) {
1260 12 : final sess = SessionKey.fromDb(dbSession.dbSession, args.userId);
1261 4 : if (!sess.isValid) {
1262 : continue;
1263 : }
1264 : // create the room if it doesn't exist
1265 : final roomKeyBackup =
1266 20 : roomKeys.rooms[sess.roomId] ??= RoomKeyBackup(sessions: {});
1267 : // generate the encrypted content
1268 4 : final payload = <String, dynamic>{
1269 : 'algorithm': AlgorithmTypes.megolmV1AesSha2,
1270 4 : 'forwarding_curve25519_key_chain': sess.forwardingCurve25519KeyChain,
1271 4 : 'sender_key': sess.senderKey,
1272 4 : 'sender_claimed_keys': sess.senderClaimedKeys,
1273 4 : 'session_key': sess.inboundGroupSession!
1274 12 : .export_session(sess.inboundGroupSession!.first_known_index()),
1275 : };
1276 : // encrypt the content
1277 8 : final encrypted = enc.encrypt(json.encode(payload));
1278 : // fetch the device, if available...
1279 : //final device = args.client.getUserDeviceKeysByCurve25519Key(sess.senderKey);
1280 : // aaaand finally add the session key to our payload
1281 16 : roomKeyBackup.sessions[sess.sessionId] = KeyBackupData(
1282 8 : firstMessageIndex: sess.inboundGroupSession!.first_known_index(),
1283 8 : forwardedCount: sess.forwardingCurve25519KeyChain.length,
1284 4 : isVerified: dbSession.verified, //device?.verified ?? false,
1285 4 : sessionData: {
1286 4 : 'ephemeral': encrypted.ephemeral,
1287 4 : 'ciphertext': encrypted.ciphertext,
1288 4 : 'mac': encrypted.mac,
1289 : },
1290 : );
1291 : }
1292 4 : enc.free();
1293 : return roomKeys;
1294 : } catch (e, s) {
1295 0 : Logs().e('[Key Manager] Error generating payload', e, s);
1296 0 : enc.free();
1297 : rethrow;
1298 : }
1299 : }
1300 :
1301 : class DbInboundGroupSessionBundle {
1302 4 : DbInboundGroupSessionBundle({
1303 : required this.dbSession,
1304 : required this.verified,
1305 : });
1306 :
1307 0 : factory DbInboundGroupSessionBundle.fromJson(Map<dynamic, dynamic> json) =>
1308 0 : DbInboundGroupSessionBundle(
1309 : dbSession:
1310 0 : StoredInboundGroupSession.fromJson(Map.from(json['dbSession'])),
1311 0 : verified: json['verified'],
1312 : );
1313 :
1314 0 : Map<String, Object> toJson() => {
1315 0 : 'dbSession': dbSession.toJson(),
1316 0 : 'verified': verified,
1317 : };
1318 : StoredInboundGroupSession dbSession;
1319 : bool verified;
1320 : }
1321 :
1322 : class GenerateUploadKeysArgs {
1323 4 : GenerateUploadKeysArgs({
1324 : required this.pubkey,
1325 : required this.dbSessions,
1326 : required this.userId,
1327 : });
1328 :
1329 0 : factory GenerateUploadKeysArgs.fromJson(Map<dynamic, dynamic> json) =>
1330 0 : GenerateUploadKeysArgs(
1331 0 : pubkey: json['pubkey'],
1332 0 : dbSessions: (json['dbSessions'] as Iterable)
1333 0 : .map((e) => DbInboundGroupSessionBundle.fromJson(e))
1334 0 : .toList(),
1335 0 : userId: json['userId'],
1336 : );
1337 :
1338 0 : Map<String, Object> toJson() => {
1339 0 : 'pubkey': pubkey,
1340 0 : 'dbSessions': dbSessions.map((e) => e.toJson()).toList(),
1341 0 : 'userId': userId,
1342 : };
1343 :
1344 : String pubkey;
1345 : List<DbInboundGroupSessionBundle> dbSessions;
1346 : String userId;
1347 : }
|