2020-06-04 11:39:51 +00:00
|
|
|
/*
|
|
|
|
* Famedly Matrix SDK
|
|
|
|
* Copyright (C) 2019, 2020 Famedly GmbH
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:olm/olm.dart' as olm;
|
|
|
|
|
2020-08-13 08:40:39 +00:00
|
|
|
import '../../famedlysdk.dart';
|
2020-06-04 11:39:51 +00:00
|
|
|
import '../../src/database/database.dart' show DbInboundGroupSession;
|
2020-08-13 08:40:39 +00:00
|
|
|
import '../../src/utils/logs.dart';
|
2020-06-04 11:39:51 +00:00
|
|
|
|
|
|
|
class SessionKey {
|
|
|
|
Map<String, dynamic> content;
|
2020-09-21 08:24:15 +00:00
|
|
|
Map<String, String> indexes;
|
2020-06-04 11:39:51 +00:00
|
|
|
olm.InboundGroupSession inboundGroupSession;
|
|
|
|
final String key;
|
2020-08-11 11:38:50 +00:00
|
|
|
List<String> get forwardingCurve25519KeyChain =>
|
|
|
|
(content['forwarding_curve25519_key_chain'] != null
|
|
|
|
? List<String>.from(content['forwarding_curve25519_key_chain'])
|
|
|
|
: null) ??
|
|
|
|
<String>[];
|
|
|
|
Map<String, String> senderClaimedKeys;
|
|
|
|
String senderKey;
|
2020-06-04 11:39:51 +00:00
|
|
|
bool get isValid => inboundGroupSession != null;
|
2020-08-17 12:25:48 +00:00
|
|
|
String roomId;
|
|
|
|
String sessionId;
|
2020-06-04 11:39:51 +00:00
|
|
|
|
2020-08-11 11:38:50 +00:00
|
|
|
SessionKey(
|
|
|
|
{this.content,
|
|
|
|
this.inboundGroupSession,
|
|
|
|
this.key,
|
|
|
|
this.indexes,
|
2020-08-17 12:25:48 +00:00
|
|
|
this.roomId,
|
|
|
|
this.sessionId,
|
2020-08-11 11:38:50 +00:00
|
|
|
String senderKey,
|
|
|
|
Map<String, String> senderClaimedKeys}) {
|
|
|
|
_setSenderKey(senderKey);
|
|
|
|
_setSenderClaimedKeys(senderClaimedKeys);
|
|
|
|
}
|
2020-06-04 11:39:51 +00:00
|
|
|
|
|
|
|
SessionKey.fromDb(DbInboundGroupSession dbEntry, String key) : key = key {
|
|
|
|
final parsedContent = Event.getMapFromPayload(dbEntry.content);
|
|
|
|
final parsedIndexes = Event.getMapFromPayload(dbEntry.indexes);
|
2020-08-11 11:38:50 +00:00
|
|
|
final parsedSenderClaimedKeys =
|
|
|
|
Event.getMapFromPayload(dbEntry.senderClaimedKeys);
|
2020-06-04 11:39:51 +00:00
|
|
|
content =
|
|
|
|
parsedContent != null ? Map<String, dynamic>.from(parsedContent) : null;
|
2020-09-21 08:24:15 +00:00
|
|
|
// we need to try...catch as the map used to be <String, int> and that will throw an error.
|
|
|
|
try {
|
|
|
|
indexes = parsedIndexes != null
|
|
|
|
? Map<String, String>.from(parsedIndexes)
|
|
|
|
: <String, String>{};
|
|
|
|
} catch (e) {
|
|
|
|
indexes = <String, String>{};
|
|
|
|
}
|
2020-08-17 12:25:48 +00:00
|
|
|
roomId = dbEntry.roomId;
|
|
|
|
sessionId = dbEntry.sessionId;
|
2020-08-11 11:38:50 +00:00
|
|
|
_setSenderKey(dbEntry.senderKey);
|
|
|
|
_setSenderClaimedKeys(Map<String, String>.from(parsedSenderClaimedKeys));
|
|
|
|
|
2020-06-04 11:39:51 +00:00
|
|
|
inboundGroupSession = olm.InboundGroupSession();
|
|
|
|
try {
|
|
|
|
inboundGroupSession.unpickle(key, dbEntry.pickle);
|
2020-08-06 09:35:02 +00:00
|
|
|
} catch (e, s) {
|
2020-06-04 11:39:51 +00:00
|
|
|
dispose();
|
2020-08-06 09:35:02 +00:00
|
|
|
Logs.error(
|
|
|
|
'[LibOlm] Unable to unpickle inboundGroupSession: ' + e.toString(),
|
|
|
|
s);
|
2020-06-04 11:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:38:50 +00:00
|
|
|
void _setSenderKey(String key) {
|
|
|
|
senderKey = key ?? content['sender_key'] ?? '';
|
|
|
|
}
|
|
|
|
|
|
|
|
void _setSenderClaimedKeys(Map<String, String> keys) {
|
|
|
|
senderClaimedKeys = (keys != null && keys.isNotEmpty)
|
|
|
|
? keys
|
|
|
|
: (content['sender_claimed_keys'] is Map
|
|
|
|
? Map<String, String>.from(content['sender_claimed_keys'])
|
|
|
|
: (content['sender_claimed_ed25519_key'] is String
|
|
|
|
? <String, String>{
|
|
|
|
'ed25519': content['sender_claimed_ed25519_key']
|
|
|
|
}
|
|
|
|
: <String, String>{}));
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:39:51 +00:00
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
final data = <String, dynamic>{};
|
|
|
|
if (content != null) {
|
|
|
|
data['content'] = content;
|
|
|
|
}
|
|
|
|
if (indexes != null) {
|
|
|
|
data['indexes'] = indexes;
|
|
|
|
}
|
|
|
|
data['inboundGroupSession'] = inboundGroupSession.pickle(key);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
inboundGroupSession?.free();
|
|
|
|
inboundGroupSession = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() => json.encode(toJson());
|
|
|
|
}
|