fix: Better handle online key backup

This commit is contained in:
Sorunome 2020-09-18 09:42:17 +02:00
parent 024a27bfc2
commit f6259efa59
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C

View file

@ -46,7 +46,7 @@ class KeyManager {
encryption.ssss.setValidator(MEGOLM_KEY, (String secret) async { encryption.ssss.setValidator(MEGOLM_KEY, (String secret) async {
final keyObj = olm.PkDecryption(); final keyObj = olm.PkDecryption();
try { try {
final info = await client.getRoomKeysBackup(); final info = await getRoomKeysBackupInfo(false);
if (info.algorithm != RoomKeysAlgorithmType.v1Curve25519AesSha2) { if (info.algorithm != RoomKeysAlgorithmType.v1Curve25519AesSha2) {
return false; return false;
} }
@ -204,7 +204,8 @@ class KeyManager {
final requestIdent = '$roomId|$sessionId|$senderKey'; final requestIdent = '$roomId|$sessionId|$senderKey';
if (client.enableE2eeRecovery && if (client.enableE2eeRecovery &&
room != null && room != null &&
!_requestedSessionIds.contains(requestIdent)) { !_requestedSessionIds.contains(requestIdent) &&
!client.isUnknownSession) {
// do e2ee recovery // do e2ee recovery
_requestedSessionIds.add(requestIdent); _requestedSessionIds.add(requestIdent);
unawaited(request(room, sessionId, senderKey, askOnlyOwnDevices: true)); unawaited(request(room, sessionId, senderKey, askOnlyOwnDevices: true));
@ -367,6 +368,23 @@ class KeyManager {
return (await encryption.ssss.getCached(MEGOLM_KEY)) != null; return (await encryption.ssss.getCached(MEGOLM_KEY)) != null;
} }
RoomKeysVersionResponse _roomKeysVersionCache;
DateTime _roomKeysVersionCacheDate;
Future<RoomKeysVersionResponse> getRoomKeysBackupInfo(
[bool useCache = true]) async {
if (_roomKeysVersionCache != null &&
_roomKeysVersionCacheDate != null &&
useCache &&
DateTime.now()
.subtract(Duration(minutes: 5))
.isBefore(_roomKeysVersionCacheDate)) {
return _roomKeysVersionCache;
}
_roomKeysVersionCache = await client.getRoomKeysBackup();
_roomKeysVersionCacheDate = DateTime.now();
return _roomKeysVersionCache;
}
Future<void> loadFromResponse(RoomKeys keys) async { Future<void> loadFromResponse(RoomKeys keys) async {
if (!(await isCached())) { if (!(await isCached())) {
return; return;
@ -374,7 +392,7 @@ class KeyManager {
final privateKey = final privateKey =
base64.decode(await encryption.ssss.getCached(MEGOLM_KEY)); base64.decode(await encryption.ssss.getCached(MEGOLM_KEY));
final decryption = olm.PkDecryption(); final decryption = olm.PkDecryption();
final info = await client.getRoomKeysBackup(); final info = await getRoomKeysBackupInfo();
String backupPubKey; String backupPubKey;
try { try {
backupPubKey = decryption.init_with_private_key(privateKey); backupPubKey = decryption.init_with_private_key(privateKey);
@ -426,7 +444,7 @@ class KeyManager {
} }
Future<void> loadSingleKey(String roomId, String sessionId) async { Future<void> loadSingleKey(String roomId, String sessionId) async {
final info = await client.getRoomKeysBackup(); final info = await getRoomKeysBackupInfo();
final ret = final ret =
await client.getRoomKeysSingleKey(roomId, sessionId, info.version); await client.getRoomKeysSingleKey(roomId, sessionId, info.version);
final keys = RoomKeys.fromJson({ final keys = RoomKeys.fromJson({
@ -449,7 +467,7 @@ class KeyManager {
bool tryOnlineBackup = true, bool tryOnlineBackup = true,
bool askOnlyOwnDevices = false, bool askOnlyOwnDevices = false,
}) async { }) async {
if (tryOnlineBackup) { if (tryOnlineBackup && await isCached()) {
// let's first check our online key backup store thingy... // let's first check our online key backup store thingy...
var hadPreviously = var hadPreviously =
getInboundGroupSession(room.id, sessionId, senderKey) != null; getInboundGroupSession(room.id, sessionId, senderKey) != null;
@ -530,7 +548,7 @@ class KeyManager {
base64.decode(await encryption.ssss.getCached(MEGOLM_KEY)); base64.decode(await encryption.ssss.getCached(MEGOLM_KEY));
// decryption is needed to calculate the public key and thus see if the claimed information is in fact valid // decryption is needed to calculate the public key and thus see if the claimed information is in fact valid
final decryption = olm.PkDecryption(); final decryption = olm.PkDecryption();
final info = await client.getRoomKeysBackup(); final info = await getRoomKeysBackupInfo(false);
String backupPubKey; String backupPubKey;
try { try {
backupPubKey = decryption.init_with_private_key(privateKey); backupPubKey = decryption.init_with_private_key(privateKey);