From 1c9da050c00a875434de43168d314820f347a303 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Tue, 26 May 2020 15:58:14 +0200 Subject: [PATCH] smoothen out verification and signature uploading --- lib/src/client.dart | 34 +++++++++++++++++++++++++++-- lib/src/ssss.dart | 4 ++++ lib/src/utils/key_verification.dart | 16 +++++++++----- pubspec.yaml | 9 ++++---- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 42f348d..48952d6 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -148,6 +148,27 @@ class Client { /// Whether this client is able to encrypt and decrypt files. bool get fileEncryptionEnabled => true; + /// Wheather this session is unknown to others + bool get isUnknownSession { + if (!userDeviceKeys.containsKey(userID)) { + return true; + } + final masterKey = userDeviceKeys[userID].masterKey; + if (masterKey == null) { + return true; + } + if (!masterKey.directVerified) { + return true; + } + if (!userDeviceKeys[userID].deviceKeys.containsKey(deviceID)) { + return true; + } + if (!userDeviceKeys[userID].deviceKeys[deviceID].crossVerified) { + return true; + } + return false; + } + /// Warning! This endpoint is for testing only! set rooms(List newList) { print('Warning! This endpoint is for testing only!'); @@ -1028,8 +1049,9 @@ class Client { } on MatrixException catch (exception) { onError.add(exception); await Future.delayed(Duration(seconds: syncErrorTimeoutSec), _sync); - } catch (exception) { + } catch (exception, stack) { print('Error during processing events: ' + exception.toString()); + print(stack); await Future.delayed(Duration(seconds: syncErrorTimeoutSec), _sync); } } @@ -1108,8 +1130,9 @@ class Client { } void _cleanupKeyVerificationRequests() { + final actions = Function()>[]; for (final entry in _keyVerificationRequests.entries) { - (() async { + actions.add(() async { var dispose = entry.value.canceled || entry.value.state == KeyVerificationState.done || entry.value.state == KeyVerificationState.error; @@ -1120,6 +1143,13 @@ class Client { entry.value.dispose(); _keyVerificationRequests.remove(entry.key); } + }); + } + if (actions.isNotEmpty) { + (() async { + for (final a in actions) { + await a(); + } })(); } } diff --git a/lib/src/ssss.dart b/lib/src/ssss.dart index 1e6e311..b350da4 100644 --- a/lib/src/ssss.dart +++ b/lib/src/ssss.dart @@ -431,6 +431,10 @@ class OpenSSSS { return await ssss.getStored(type, keyId, privateKey); } + Future store(String type, String secret) async { + await ssss.store(type, secret, keyId, privateKey); + } + Future maybeCacheAll() async { await ssss.maybeCacheAll(keyId, privateKey); } diff --git a/lib/src/utils/key_verification.dart b/lib/src/utils/key_verification.dart index de501b4..7508c30 100644 --- a/lib/src/utils/key_verification.dart +++ b/lib/src/utils/key_verification.dart @@ -120,6 +120,7 @@ class KeyVerification { {this.client, this.room, this.userId, String deviceId, this.onUpdate}) { lastActivity = DateTime.now(); _deviceId ??= deviceId; + print('Setting device id constructor: ' + _deviceId.toString()); } void dispose() { @@ -135,10 +136,6 @@ class KeyVerification { } Future sendStart() async { - if (room == null) { - transactionId = - randomString(512) + DateTime.now().millisecondsSinceEpoch.toString(); - } await send('m.key.verification.request', { 'methods': VERIFICATION_METHODS, 'timestamp': DateTime.now().millisecondsSinceEpoch, @@ -149,8 +146,12 @@ class KeyVerification { } Future start() async { + if (room == null) { + transactionId = + randomString(512) + DateTime.now().millisecondsSinceEpoch.toString(); + } if (client.crossSigning.enabled && - !(await client.crossSigning.isCached())) { + !(await client.crossSigning.isCached()) && !client.isUnknownSession) { setState(KeyVerificationState.askSSSS); _nextAction = 'request'; } else { @@ -165,6 +166,7 @@ class KeyVerification { switch (type) { case 'm.key.verification.request': _deviceId ??= payload['from_device']; + print('Setting device id request: ' + _deviceId.toString()); transactionId ??= eventId ?? payload['transaction_id']; // verify the timestamp final now = DateTime.now(); @@ -200,6 +202,7 @@ class KeyVerification { break; case 'm.key.verification.start': _deviceId ??= payload['from_device']; + print('Setting device id start: ' + _deviceId.toString()); transactionId ??= eventId ?? payload['transaction_id']; if (!(await verifyLastStep(['m.key.verification.request', null]))) { return; // abort @@ -353,6 +356,7 @@ class KeyVerification { } // okay, we reached this far, so all the devices are verified! var verifiedMasterKey = false; + final wasUnknownSession = client.isUnknownSession; for (final key in _verifiedDevices) { await key.setVerified(true); if (key is CrossSigningKey && key.usage.contains('master')) { @@ -374,7 +378,7 @@ class KeyVerification { if (await client.crossSigning.isCached()) { // and now let's sign them all in the background unawaited(client.crossSigning.sign(_verifiedDevices)); - } else { + } else if (!wasUnknownSession) { askingSSSS = true; } } diff --git a/pubspec.yaml b/pubspec.yaml index 324dcd4..51d3e62 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,12 +24,13 @@ dependencies: olm: git: url: https://gitlab.com/famedly/libraries/dart-olm.git - ref: 1.x.y + ref: 8749474d611f02a89893e067b6e479ebfd40c51d matrix_file_e2ee: - git: - url: https://gitlab.com/famedly/libraries/matrix_file_e2ee.git - ref: 1.x.y + path: /home/sorunome/repos/famedly/matrix_file_e2ee +# git: +# url: https://gitlab.com/famedly/libraries/matrix_file_e2ee.git +# ref: 1.x.y dev_dependencies: test: ^1.0.0