From 0fabed9cc3e5512f3cf954e73b7c4d36c877a45e Mon Sep 17 00:00:00 2001 From: Sorunome Date: Wed, 20 May 2020 09:37:32 +0200 Subject: [PATCH] make sure keys are valid --- lib/src/client.dart | 38 +++++++++++++++-------------- lib/src/utils/device_keys_list.dart | 9 ++++++- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index f66ac06..84471aa 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1648,28 +1648,30 @@ class Client { // Set the new device key for this device if (!oldKeys.containsKey(deviceId)) { - _userDeviceKeys[userId].deviceKeys[deviceId] = - DeviceKeys.fromJson(rawDeviceKeyEntry.value); - if (deviceId == deviceID && - _userDeviceKeys[userId].deviceKeys[deviceId].ed25519Key == - fingerprintKey) { - // Always trust the own device - _userDeviceKeys[userId].deviceKeys[deviceId].verified = true; + final entry = DeviceKeys.fromJson(rawDeviceKeyEntry.value); + if (entry.isValid) { + _userDeviceKeys[userId].deviceKeys[deviceId] = entry; + if (deviceId == deviceID && + entry.ed25519Key == + fingerprintKey) { + // Always trust the own device + entry.verified = true; + } + } + if (database != null) { + dbActions.add(() => database.storeUserDeviceKey( + id, + userId, + deviceId, + json.encode( + _userDeviceKeys[userId].deviceKeys[deviceId].toJson()), + _userDeviceKeys[userId].deviceKeys[deviceId].verified, + _userDeviceKeys[userId].deviceKeys[deviceId].blocked, + )); } } else { _userDeviceKeys[userId].deviceKeys[deviceId] = oldKeys[deviceId]; } - if (database != null) { - dbActions.add(() => database.storeUserDeviceKey( - id, - userId, - deviceId, - json.encode( - _userDeviceKeys[userId].deviceKeys[deviceId].toJson()), - _userDeviceKeys[userId].deviceKeys[deviceId].verified, - _userDeviceKeys[userId].deviceKeys[deviceId].blocked, - )); - } } if (database != null) { for (final oldDeviceKeyEntry in oldKeys.entries) { diff --git a/lib/src/utils/device_keys_list.dart b/lib/src/utils/device_keys_list.dart index 8a76e5d..8cacbdc 100644 --- a/lib/src/utils/device_keys_list.dart +++ b/lib/src/utils/device_keys_list.dart @@ -15,7 +15,12 @@ class DeviceKeysList { outdated = dbEntry.outdated; deviceKeys = {}; for (final childEntry in childEntries) { - deviceKeys[childEntry.deviceId] = DeviceKeys.fromDb(childEntry); + final entry = DeviceKeys.fromDb(childEntry); + if (entry.isValid) { + deviceKeys[childEntry.deviceId] = entry; + } else { + outdated = true; + } } } @@ -62,6 +67,8 @@ class DeviceKeys { String get curve25519Key => keys['curve25519:$deviceId']; String get ed25519Key => keys['ed25519:$deviceId']; + bool get isValid => userId != null && deviceId != null && curve25519Key != null && ed25519Key != null; + Future setVerified(bool newVerified, Client client) { verified = newVerified; return client.database?.setVerifiedUserDeviceKey(newVerified, client.id, userId, deviceId);