Line data Source code
1 : /*
2 : * Famedly Matrix SDK
3 : * Copyright (C) 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:convert';
20 :
21 : import 'package:vodozemac/vodozemac.dart' as vod;
22 :
23 : import 'package:matrix/encryption/utils/pickle_key.dart';
24 : import 'package:matrix/matrix.dart';
25 :
26 : class OlmSession {
27 : String identityKey;
28 : String? sessionId;
29 : vod.Session? session;
30 : DateTime? lastReceived;
31 : final String key;
32 135 : String? get pickledSession => session?.toPickleEncrypted(key.toPickleKey());
33 :
34 8 : bool get isValid => session != null;
35 :
36 27 : OlmSession({
37 : required this.key,
38 : required this.identityKey,
39 : required this.sessionId,
40 : required this.session,
41 : required this.lastReceived,
42 : });
43 :
44 5 : OlmSession.fromJson(Map<String, Object?> dbEntry, this.key)
45 5 : : identityKey = dbEntry.tryGet<String>('identity_key') ?? '' {
46 : try {
47 : try {
48 9 : session = vod.Session.fromPickleEncrypted(
49 10 : pickleKey: key.toPickleKey(),
50 5 : pickle: dbEntry['pickle'] as String,
51 : );
52 : } catch (_) {
53 2 : Logs().d('Unable to unpickle Olm session. Try LibOlm format.');
54 1 : session = vod.Session.fromOlmPickleEncrypted(
55 2 : pickleKey: utf8.encode(key),
56 1 : pickle: dbEntry['pickle'] as String,
57 : );
58 : }
59 8 : sessionId = dbEntry['session_id'] as String;
60 8 : lastReceived = DateTime.fromMillisecondsSinceEpoch(
61 4 : dbEntry.tryGet<int>('last_received') ?? 0,
62 : );
63 16 : assert(sessionId == session!.sessionId);
64 : } catch (e, s) {
65 2 : Logs().e('[Vodozemac] Could not unpickle olm session', e, s);
66 : }
67 : }
68 : }
|