1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Refactor key backup recovery to prepare for rust (#3708)

* Refactor key backup recovery to prepare for rust

* code review

* quick doc format

* code review fix
This commit is contained in:
Valere
2023-09-12 13:19:35 +02:00
committed by GitHub
parent f963ca5562
commit c7827d971c
8 changed files with 398 additions and 61 deletions

View File

@@ -3471,9 +3471,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
await this.cryptoBackend.deleteKeyBackupVersion(version);
}
private makeKeyBackupPath(roomId: undefined, sessionId: undefined, version?: string): IKeyBackupPath;
private makeKeyBackupPath(roomId: string, sessionId: undefined, version?: string): IKeyBackupPath;
private makeKeyBackupPath(roomId: string, sessionId: string, version?: string): IKeyBackupPath;
private makeKeyBackupPath(roomId?: string, sessionId?: string, version?: string): IKeyBackupPath {
let path: string;
if (sessionId !== undefined) {
@@ -3793,22 +3790,13 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
let totalKeyCount = 0;
let keys: IMegolmSessionData[] = [];
const path = this.makeKeyBackupPath(targetRoomId!, targetSessionId!, backupInfo.version);
const path = this.makeKeyBackupPath(targetRoomId, targetSessionId, backupInfo.version);
const algorithm = await BackupManager.makeAlgorithm(backupInfo, async () => {
return privKey;
});
const backupDecryptor = await this.cryptoBackend.getBackupDecryptor(backupInfo, privKey);
const untrusted = algorithm.untrusted;
const untrusted = !backupDecryptor.sourceTrusted;
try {
// If the pubkey computed from the private data we've been given
// doesn't match the one in the auth_data, the user has entered
// a different recovery key / the wrong passphrase.
if (!(await algorithm.keyMatches(privKey))) {
return Promise.reject(new MatrixError({ errcode: MatrixClient.RESTORE_BACKUP_ERROR_BAD_KEY }));
}
if (!(privKey instanceof Uint8Array)) {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
throw new Error(`restoreKeyBackup expects Uint8Array, got ${privKey}`);
@@ -3842,7 +3830,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
if (!roomData.sessions) continue;
totalKeyCount += Object.keys(roomData.sessions).length;
const roomKeys = await algorithm.decryptSessions(roomData.sessions);
const roomKeys = await backupDecryptor.decryptSessions(roomData.sessions);
for (const k of roomKeys) {
k.room_id = roomId;
keys.push(k);
@@ -3851,14 +3839,14 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
} else if ((res as IRoomKeysResponse).sessions) {
const sessions = (res as IRoomKeysResponse).sessions;
totalKeyCount = Object.keys(sessions).length;
keys = await algorithm.decryptSessions(sessions);
keys = await backupDecryptor.decryptSessions(sessions);
for (const k of keys) {
k.room_id = targetRoomId!;
}
} else {
totalKeyCount = 1;
try {
const [key] = await algorithm.decryptSessions({
const [key] = await backupDecryptor.decryptSessions({
[targetSessionId!]: res as IKeyBackupSession,
});
key.room_id = targetRoomId!;
@@ -3869,7 +3857,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
}
}
} finally {
algorithm.free();
backupDecryptor.free();
}
await this.cryptoBackend.importRoomKeys(keys, {
@@ -3878,7 +3866,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
source: "backup",
});
await this.checkKeyBackup();
/// in case entering the passphrase would add a new signature?
await this.cryptoBackend.checkKeyBackupAndEnable();
return { total: totalKeyCount, imported: keys.length };
}