fix: Upload OTKs if the otk_count field is missing

fixes #180
This commit is contained in:
Nicolas Werner 2021-07-25 14:55:05 +02:00 committed by Christian Pauly
parent 686bfa3157
commit 179f73db3a
4 changed files with 23 additions and 17 deletions

View File

@ -262,19 +262,25 @@ class OlmManager {
final haveFallbackKeys = encryption.isMinOlmVersion(3, 2, 0);
// Check if there are at least half of max_number_of_one_time_keys left on the server
// and generate and upload more if not.
if ((countJson != null &&
((countJson.containsKey('signed_curve25519') &&
countJson['signed_curve25519'] <
(_olmAccount.max_number_of_one_time_keys() / 2)) ||
!countJson.containsKey('signed_curve25519'))) ||
(haveFallbackKeys &&
unusedFallbackKeyTypes?.contains('signed_curve25519') == false)) {
// If the server did not send us a count, assume it is 0
final keyCount = countJson?.tryGet<int>('signed_curve25519', 0) ?? 0;
// If the server does not support fallback keys, it will not tell us about them.
// If the server supports them but has no key, upload a new one.
var unusedFallbackKey = true;
if (unusedFallbackKeyTypes?.contains('signed_curve25519') == false) {
unusedFallbackKey = false;
}
// Only upload keys if they are less than half of the max or we have no unused fallback key
if (keyCount < (_olmAccount.max_number_of_one_time_keys() / 2) ||
!unusedFallbackKey) {
uploadKeys(
oldKeyCount:
countJson != null ? (countJson['signed_curve25519'] ?? 0) : null,
unusedFallbackKey: haveFallbackKeys
? unusedFallbackKeyTypes?.contains('signed_curve25519')
oldKeyCount: keyCount < (_olmAccount.max_number_of_one_time_keys() / 2)
? keyCount
: null,
unusedFallbackKey: haveFallbackKeys ? unusedFallbackKey : null,
);
}
}

View File

@ -1184,9 +1184,7 @@ class Client extends MatrixApi {
if (sync.deviceLists != null) {
await _handleDeviceListsEvents(sync.deviceLists);
}
if ((sync.deviceUnusedFallbackKeyTypes != null ||
sync.deviceOneTimeKeysCount != null) &&
encryptionEnabled) {
if (encryptionEnabled) {
encryption.handleDeviceOneTimeKeysCount(
sync.deviceOneTimeKeysCount, sync.deviceUnusedFallbackKeyTypes);
}

View File

@ -614,8 +614,9 @@ void main() {
expect(client2.deviceID, client1.deviceID);
expect(client2.deviceName, client1.deviceName);
if (client2.encryptionEnabled) {
expect(client2.encryption.pickledOlmAccount,
client1.encryption.pickledOlmAccount);
expect(client2.encryption.fingerprintKey,
client1.encryption.fingerprintKey);
expect(client2.encryption.identityKey, client1.encryption.identityKey);
expect(client2.rooms[1].id, client1.rooms[1].id);
}

View File

@ -112,13 +112,14 @@ void main() {
FakeMatrixApi.calledEndpoints.containsKey('/client/r0/keys/upload'),
true);
// this will upload keys because we assume the key count is 0, if the server doesn't send one
FakeMatrixApi.calledEndpoints.clear();
client.encryption.olmManager
.handleDeviceOneTimeKeysCount(null, ['signed_curve25519']);
await Future.delayed(Duration(milliseconds: 50));
expect(
FakeMatrixApi.calledEndpoints.containsKey('/client/r0/keys/upload'),
false);
true);
});
test('restoreOlmSession', () async {