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';
|
2020-09-06 12:48:06 +00:00
|
|
|
import 'dart:async';
|
2020-06-04 11:39:51 +00:00
|
|
|
|
|
|
|
import 'package:pedantic/pedantic.dart';
|
2020-08-13 08:40:39 +00:00
|
|
|
|
|
|
|
import '../famedlysdk.dart';
|
|
|
|
import '../matrix_api.dart';
|
2020-09-21 16:10:46 +00:00
|
|
|
import '../src/utils/run_in_root.dart';
|
2020-08-13 08:40:39 +00:00
|
|
|
import 'cross_signing.dart';
|
2020-06-04 11:39:51 +00:00
|
|
|
import 'key_manager.dart';
|
|
|
|
import 'key_verification_manager.dart';
|
2020-08-13 08:40:39 +00:00
|
|
|
import 'olm_manager.dart';
|
2020-06-05 20:03:28 +00:00
|
|
|
import 'ssss.dart';
|
2020-06-04 11:39:51 +00:00
|
|
|
|
|
|
|
class Encryption {
|
|
|
|
final Client client;
|
|
|
|
final bool debug;
|
|
|
|
final bool enableE2eeRecovery;
|
|
|
|
|
|
|
|
bool get enabled => olmManager.enabled;
|
|
|
|
|
|
|
|
/// Returns the base64 encoded keys to store them in a store.
|
|
|
|
/// This String should **never** leave the device!
|
|
|
|
String get pickledOlmAccount => olmManager.pickledOlmAccount;
|
|
|
|
|
|
|
|
String get fingerprintKey => olmManager.fingerprintKey;
|
|
|
|
String get identityKey => olmManager.identityKey;
|
|
|
|
|
|
|
|
KeyManager keyManager;
|
|
|
|
OlmManager olmManager;
|
|
|
|
KeyVerificationManager keyVerificationManager;
|
2020-06-05 20:03:28 +00:00
|
|
|
CrossSigning crossSigning;
|
|
|
|
SSSS ssss;
|
2020-06-04 11:39:51 +00:00
|
|
|
|
|
|
|
Encryption({
|
|
|
|
this.client,
|
|
|
|
this.debug,
|
|
|
|
this.enableE2eeRecovery,
|
|
|
|
}) {
|
2020-06-05 20:03:28 +00:00
|
|
|
ssss = SSSS(this);
|
2020-06-04 11:39:51 +00:00
|
|
|
keyManager = KeyManager(this);
|
|
|
|
olmManager = OlmManager(this);
|
|
|
|
keyVerificationManager = KeyVerificationManager(this);
|
2020-06-05 20:03:28 +00:00
|
|
|
crossSigning = CrossSigning(this);
|
2020-06-04 11:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> init(String olmAccount) async {
|
|
|
|
await olmManager.init(olmAccount);
|
2020-08-17 12:25:48 +00:00
|
|
|
_backgroundTasksRunning = true;
|
|
|
|
_backgroundTasks(); // start the background tasks
|
2020-06-04 11:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleDeviceOneTimeKeysCount(Map<String, int> countJson) {
|
2020-09-21 16:10:46 +00:00
|
|
|
runInRoot(() => olmManager.handleDeviceOneTimeKeysCount(countJson));
|
2020-06-04 11:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onSync() {
|
|
|
|
keyVerificationManager.cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> handleToDeviceEvent(ToDeviceEvent event) async {
|
2020-07-26 05:54:03 +00:00
|
|
|
if (event.type == 'm.room_key') {
|
|
|
|
// a new room key. We need to handle this asap, before other
|
2020-06-04 15:51:49 +00:00
|
|
|
// events in /sync are handled
|
2020-06-04 11:39:51 +00:00
|
|
|
await keyManager.handleToDeviceEvent(event);
|
|
|
|
}
|
2020-07-26 05:54:03 +00:00
|
|
|
if (['m.room_key_request', 'm.forwarded_room_key'].contains(event.type)) {
|
|
|
|
// "just" room key request things. We don't need these asap, so we handle
|
|
|
|
// them in the background
|
2020-09-21 16:10:46 +00:00
|
|
|
unawaited(runInRoot(() => keyManager.handleToDeviceEvent(event)));
|
2020-07-26 05:54:03 +00:00
|
|
|
}
|
2020-06-04 11:39:51 +00:00
|
|
|
if (event.type.startsWith('m.key.verification.')) {
|
2020-06-04 15:51:49 +00:00
|
|
|
// some key verification event. No need to handle it now, we can easily
|
|
|
|
// do this in the background
|
2020-09-17 10:53:18 +00:00
|
|
|
unawaited(
|
2020-09-21 16:10:46 +00:00
|
|
|
runInRoot(() => keyVerificationManager.handleToDeviceEvent(event)));
|
2020-06-04 11:39:51 +00:00
|
|
|
}
|
2020-06-06 11:38:19 +00:00
|
|
|
if (event.type.startsWith('m.secret.')) {
|
|
|
|
// some ssss thing. We can do this in the background
|
2020-09-21 16:10:46 +00:00
|
|
|
unawaited(runInRoot(() => ssss.handleToDeviceEvent(event)));
|
2020-06-06 11:38:19 +00:00
|
|
|
}
|
2020-09-10 11:19:05 +00:00
|
|
|
if (event.sender == client.userID) {
|
|
|
|
// maybe we need to re-try SSSS secrets
|
2020-09-21 16:10:46 +00:00
|
|
|
unawaited(runInRoot(() => ssss.periodicallyRequestMissingCache()));
|
2020-09-10 11:19:05 +00:00
|
|
|
}
|
2020-06-04 11:39:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 20:03:28 +00:00
|
|
|
Future<void> handleEventUpdate(EventUpdate update) async {
|
|
|
|
if (update.type == 'ephemeral') {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-06 11:47:37 +00:00
|
|
|
if (update.eventType.startsWith('m.key.verification.') ||
|
|
|
|
(update.eventType == 'm.room.message' &&
|
|
|
|
(update.content['content']['msgtype'] is String) &&
|
|
|
|
update.content['content']['msgtype']
|
|
|
|
.startsWith('m.key.verification.'))) {
|
2020-06-05 20:03:28 +00:00
|
|
|
// "just" key verification, no need to do this in sync
|
2020-09-17 10:53:18 +00:00
|
|
|
unawaited(
|
2020-09-21 16:10:46 +00:00
|
|
|
runInRoot(() => keyVerificationManager.handleEventUpdate(update)));
|
2020-06-05 20:03:28 +00:00
|
|
|
}
|
2020-09-10 11:19:05 +00:00
|
|
|
if (update.content['sender'] == client.userID &&
|
|
|
|
!update.content['unsigned'].containsKey('transaction_id')) {
|
|
|
|
// maybe we need to re-try SSSS secrets
|
2020-09-21 16:10:46 +00:00
|
|
|
unawaited(runInRoot(() => ssss.periodicallyRequestMissingCache()));
|
2020-09-10 11:19:05 +00:00
|
|
|
}
|
2020-06-05 20:03:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 11:39:51 +00:00
|
|
|
Future<ToDeviceEvent> decryptToDeviceEvent(ToDeviceEvent event) async {
|
|
|
|
return await olmManager.decryptToDeviceEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
Event decryptRoomEventSync(String roomId, Event event) {
|
|
|
|
if (event.type != EventTypes.Encrypted ||
|
|
|
|
event.content['ciphertext'] == null) return event;
|
|
|
|
Map<String, dynamic> decryptedPayload;
|
2020-06-10 08:44:22 +00:00
|
|
|
var canRequestSession = false;
|
2020-06-04 11:39:51 +00:00
|
|
|
try {
|
|
|
|
if (event.content['algorithm'] != 'm.megolm.v1.aes-sha2') {
|
|
|
|
throw (DecryptError.UNKNOWN_ALGORITHM);
|
|
|
|
}
|
|
|
|
final String sessionId = event.content['session_id'];
|
|
|
|
final String senderKey = event.content['sender_key'];
|
|
|
|
final inboundGroupSession =
|
|
|
|
keyManager.getInboundGroupSession(roomId, sessionId, senderKey);
|
|
|
|
if (inboundGroupSession == null) {
|
2020-06-10 08:44:22 +00:00
|
|
|
canRequestSession = true;
|
2020-06-04 11:39:51 +00:00
|
|
|
throw (DecryptError.UNKNOWN_SESSION);
|
|
|
|
}
|
2020-06-10 10:11:10 +00:00
|
|
|
// decrypt errors here may mean we have a bad session key - others might have a better one
|
|
|
|
canRequestSession = true;
|
2020-06-04 11:39:51 +00:00
|
|
|
final decryptResult = inboundGroupSession.inboundGroupSession
|
|
|
|
.decrypt(event.content['ciphertext']);
|
2020-06-10 10:11:10 +00:00
|
|
|
canRequestSession = false;
|
2020-09-21 08:24:15 +00:00
|
|
|
// we can't have the key be an int, else json-serializing will fail, thus we need it to be a string
|
|
|
|
final messageIndexKey = 'key-' + decryptResult.message_index.toString();
|
|
|
|
final messageIndexValue = event.eventId +
|
|
|
|
'|' +
|
2020-06-04 11:39:51 +00:00
|
|
|
event.originServerTs.millisecondsSinceEpoch.toString();
|
|
|
|
var haveIndex = inboundGroupSession.indexes.containsKey(messageIndexKey);
|
|
|
|
if (haveIndex &&
|
2020-09-21 08:24:15 +00:00
|
|
|
inboundGroupSession.indexes[messageIndexKey] != messageIndexValue) {
|
2020-06-04 11:39:51 +00:00
|
|
|
// TODO: maybe clear outbound session, if it is ours
|
|
|
|
throw (DecryptError.CHANNEL_CORRUPTED);
|
|
|
|
}
|
2020-09-21 08:24:15 +00:00
|
|
|
inboundGroupSession.indexes[messageIndexKey] = messageIndexValue;
|
2020-06-04 11:39:51 +00:00
|
|
|
if (!haveIndex) {
|
|
|
|
// now we persist the udpated indexes into the database.
|
|
|
|
// the entry should always exist. In the case it doesn't, the following
|
|
|
|
// line *could* throw an error. As that is a future, though, and we call
|
|
|
|
// it un-awaited here, nothing happens, which is exactly the result we want
|
|
|
|
client.database?.updateInboundGroupSessionIndexes(
|
|
|
|
json.encode(inboundGroupSession.indexes),
|
|
|
|
client.id,
|
|
|
|
roomId,
|
|
|
|
sessionId);
|
|
|
|
}
|
|
|
|
decryptedPayload = json.decode(decryptResult.plaintext);
|
|
|
|
} catch (exception) {
|
|
|
|
// alright, if this was actually by our own outbound group session, we might as well clear it
|
|
|
|
if (client.enableE2eeRecovery &&
|
|
|
|
(keyManager
|
|
|
|
.getOutboundGroupSession(roomId)
|
|
|
|
?.outboundGroupSession
|
|
|
|
?.session_id() ??
|
|
|
|
'') ==
|
|
|
|
event.content['session_id']) {
|
|
|
|
keyManager.clearOutboundGroupSession(roomId, wipe: true);
|
|
|
|
}
|
2020-06-10 08:44:22 +00:00
|
|
|
if (canRequestSession) {
|
2020-06-04 11:39:51 +00:00
|
|
|
decryptedPayload = {
|
|
|
|
'content': event.content,
|
|
|
|
'type': EventTypes.Encrypted,
|
|
|
|
};
|
|
|
|
decryptedPayload['content']['body'] = exception.toString();
|
|
|
|
decryptedPayload['content']['msgtype'] = 'm.bad.encrypted';
|
2020-06-10 08:44:22 +00:00
|
|
|
decryptedPayload['content']['can_request_session'] = true;
|
2020-06-04 11:39:51 +00:00
|
|
|
} else {
|
|
|
|
decryptedPayload = {
|
|
|
|
'content': <String, dynamic>{
|
|
|
|
'msgtype': 'm.bad.encrypted',
|
|
|
|
'body': exception.toString(),
|
|
|
|
},
|
|
|
|
'type': EventTypes.Encrypted,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (event.content['m.relates_to'] != null) {
|
|
|
|
decryptedPayload['content']['m.relates_to'] =
|
|
|
|
event.content['m.relates_to'];
|
|
|
|
}
|
|
|
|
return Event(
|
|
|
|
content: decryptedPayload['content'],
|
|
|
|
type: decryptedPayload['type'],
|
|
|
|
senderId: event.senderId,
|
|
|
|
eventId: event.eventId,
|
|
|
|
roomId: event.roomId,
|
|
|
|
room: event.room,
|
|
|
|
originServerTs: event.originServerTs,
|
|
|
|
unsigned: event.unsigned,
|
|
|
|
stateKey: event.stateKey,
|
|
|
|
prevContent: event.prevContent,
|
|
|
|
status: event.status,
|
|
|
|
sortOrder: event.sortOrder,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Event> decryptRoomEvent(String roomId, Event event,
|
|
|
|
{bool store = false, String updateType = 'timeline'}) async {
|
|
|
|
final doStore = () async {
|
|
|
|
await client.database?.storeEventUpdate(
|
|
|
|
client.id,
|
|
|
|
EventUpdate(
|
|
|
|
eventType: event.type,
|
|
|
|
content: event.toJson(),
|
|
|
|
roomID: event.roomId,
|
|
|
|
type: updateType,
|
|
|
|
sortOrder: event.sortOrder,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (updateType != 'history') {
|
|
|
|
event.room?.setState(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (event.type != EventTypes.Encrypted) {
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
event = decryptRoomEventSync(roomId, event);
|
|
|
|
if (event.type != EventTypes.Encrypted) {
|
|
|
|
if (store) {
|
|
|
|
await doStore();
|
|
|
|
}
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
if (client.database == null) {
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
await keyManager.loadInboundGroupSession(
|
|
|
|
roomId, event.content['session_id'], event.content['sender_key']);
|
|
|
|
event = decryptRoomEventSync(roomId, event);
|
|
|
|
if (event.type != EventTypes.Encrypted && store) {
|
|
|
|
await doStore();
|
|
|
|
}
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encrypts the given json payload and creates a send-ready m.room.encrypted
|
|
|
|
/// payload. This will create a new outgoingGroupSession if necessary.
|
|
|
|
Future<Map<String, dynamic>> encryptGroupMessagePayload(
|
|
|
|
String roomId, Map<String, dynamic> payload,
|
|
|
|
{String type = EventTypes.Message}) async {
|
|
|
|
final room = client.getRoomById(roomId);
|
|
|
|
if (room == null || !room.encrypted || !enabled) {
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
if (room.encryptionAlgorithm != 'm.megolm.v1.aes-sha2') {
|
|
|
|
throw ('Unknown encryption algorithm');
|
|
|
|
}
|
|
|
|
if (keyManager.getOutboundGroupSession(roomId) == null) {
|
|
|
|
await keyManager.loadOutboundGroupSession(roomId);
|
|
|
|
}
|
|
|
|
await keyManager.clearOutboundGroupSession(roomId);
|
|
|
|
if (keyManager.getOutboundGroupSession(roomId) == null) {
|
|
|
|
await keyManager.createOutboundGroupSession(roomId);
|
|
|
|
}
|
|
|
|
final sess = keyManager.getOutboundGroupSession(roomId);
|
|
|
|
if (sess == null) {
|
|
|
|
throw ('Unable to create new outbound group session');
|
|
|
|
}
|
2020-07-29 09:43:27 +00:00
|
|
|
// we clone the payload as we do not want to remove 'm.relates_to' from the
|
|
|
|
// original payload passed into this function
|
|
|
|
payload = Map<String, dynamic>.from(payload);
|
2020-06-04 11:39:51 +00:00
|
|
|
final Map<String, dynamic> mRelatesTo = payload.remove('m.relates_to');
|
|
|
|
final payloadContent = {
|
|
|
|
'content': payload,
|
|
|
|
'type': type,
|
|
|
|
'room_id': roomId,
|
|
|
|
};
|
|
|
|
var encryptedPayload = <String, dynamic>{
|
|
|
|
'algorithm': 'm.megolm.v1.aes-sha2',
|
|
|
|
'ciphertext':
|
|
|
|
sess.outboundGroupSession.encrypt(json.encode(payloadContent)),
|
|
|
|
'device_id': client.deviceID,
|
|
|
|
'sender_key': identityKey,
|
|
|
|
'session_id': sess.outboundGroupSession.session_id(),
|
|
|
|
if (mRelatesTo != null) 'm.relates_to': mRelatesTo,
|
|
|
|
};
|
|
|
|
sess.sentMessages++;
|
|
|
|
await keyManager.storeOutboundGroupSession(roomId, sess);
|
|
|
|
return encryptedPayload;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>> encryptToDeviceMessagePayload(
|
|
|
|
DeviceKeys device, String type, Map<String, dynamic> payload) async {
|
|
|
|
return await olmManager.encryptToDeviceMessagePayload(
|
|
|
|
device, type, payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>> encryptToDeviceMessage(
|
|
|
|
List<DeviceKeys> deviceKeys,
|
|
|
|
String type,
|
|
|
|
Map<String, dynamic> payload) async {
|
|
|
|
return await olmManager.encryptToDeviceMessage(deviceKeys, type, payload);
|
|
|
|
}
|
|
|
|
|
2020-09-10 10:56:50 +00:00
|
|
|
Future<void> autovalidateMasterOwnKey() async {
|
|
|
|
// check if we can set our own master key as verified, if it isn't yet
|
|
|
|
if (client.database != null &&
|
|
|
|
client.userDeviceKeys.containsKey(client.userID)) {
|
|
|
|
final masterKey = client.userDeviceKeys[client.userID].masterKey;
|
|
|
|
if (masterKey != null &&
|
|
|
|
!masterKey.directVerified &&
|
|
|
|
masterKey
|
|
|
|
.hasValidSignatureChain(onlyValidateUserIds: {client.userID})) {
|
|
|
|
await masterKey.setVerified(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 12:25:48 +00:00
|
|
|
// this method is responsible for all background tasks, such as uploading online key backups
|
|
|
|
bool _backgroundTasksRunning = true;
|
|
|
|
void _backgroundTasks() {
|
|
|
|
if (!_backgroundTasksRunning) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyManager.backgroundTasks();
|
|
|
|
|
2020-09-10 10:56:50 +00:00
|
|
|
autovalidateMasterOwnKey();
|
|
|
|
|
2020-08-17 12:25:48 +00:00
|
|
|
if (_backgroundTasksRunning) {
|
|
|
|
Timer(Duration(seconds: 10), _backgroundTasks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:39:51 +00:00
|
|
|
void dispose() {
|
|
|
|
keyManager.dispose();
|
|
|
|
olmManager.dispose();
|
|
|
|
keyVerificationManager.dispose();
|
2020-08-17 12:25:48 +00:00
|
|
|
_backgroundTasksRunning = false;
|
2020-06-04 11:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class DecryptError {
|
|
|
|
static const String NOT_ENABLED = 'Encryption is not enabled in your client.';
|
|
|
|
static const String UNKNOWN_ALGORITHM = 'Unknown encryption algorithm.';
|
|
|
|
static const String UNKNOWN_SESSION =
|
|
|
|
'The sender has not sent us the session key.';
|
|
|
|
static const String CHANNEL_CORRUPTED =
|
|
|
|
'The secure channel with the sender was corrupted.';
|
|
|
|
}
|