From 3941af93d23da81cd45669a9d4606521603301eb Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 17 Sep 2020 14:19:57 +0200 Subject: [PATCH] more impl of SessionBackup --- src/matrix/e2ee/megolm/SessionBackup.js | 16 ++++++++-------- src/matrix/net/HomeServerApi.js | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/matrix/e2ee/megolm/SessionBackup.js b/src/matrix/e2ee/megolm/SessionBackup.js index 37db5e4b..3c6854f2 100644 --- a/src/matrix/e2ee/megolm/SessionBackup.js +++ b/src/matrix/e2ee/megolm/SessionBackup.js @@ -25,32 +25,32 @@ export class SessionBackup { } async getSession(roomId, sessionId) { - const sessionResponse = await this._hsApi.roomKey(this._backupInfo.version, roomId, sessionId).response(); - let sessionInfo; + const sessionResponse = await this._hsApi.roomKeyForRoomAndSession(this._backupInfo.version, roomId, sessionId).response(); const decryption = new this._olm.PkDecryption(); try { decryption.init_with_private_key(this._privateKey); - sessionInfo = this._decryption.decrypt( + const sessionInfo = this._decryption.decrypt( sessionResponse.session_data.ephemeral, sessionResponse.session_data.mac, sessionResponse.session_data.ciphertext, ); + return JSON.parse(sessionInfo); } finally { decryption.free(); } - return JSON.parse(sessionInfo); } static async fromSecretStorage({olm, secretStorage, hsApi}) { - const backupInfo = await hsApi.roomKeysVersion().response(); const base64PrivateKey = await secretStorage.readSecret("m.megolm_backup.v1"); if (base64PrivateKey) { const privateKey = base64.decode(base64PrivateKey); + const backupInfo = await hsApi.roomKeysVersion().response(); + const expectedPubKey = backupInfo.auth_data.public_key; const decryption = new olm.PkDecryption(); try { - const pubKey = decryption.init_with_private_key(this._privateKey); - if (pubKey !== backupInfo.auth_data.public_key) { - throw new Error(`Bad backup key, public key does not match. Calculated ${pubKey} but expected ${backupInfo.auth_data.public_key}`); + const pubKey = decryption.init_with_private_key(privateKey); + if (pubKey !== expectedPubKey) { + throw new Error(`Bad backup key, public key does not match. Calculated ${pubKey} but expected ${expectedPubKey}`); } } finally { decryption.free(); diff --git a/src/matrix/net/HomeServerApi.js b/src/matrix/net/HomeServerApi.js index 649a7462..c536ecf7 100644 --- a/src/matrix/net/HomeServerApi.js +++ b/src/matrix/net/HomeServerApi.js @@ -176,6 +176,17 @@ export class HomeServerApi { sendToDevice(type, payload, txnId, options = null) { return this._put(`/sendToDevice/${encodeURIComponent(type)}/${encodeURIComponent(txnId)}`, null, payload, options); } + + roomKeysVersion(version = null, options = null) { + if (!version) { + version = ""; + } + return this._get(`/room_keys/version/${encodeURIComponent(version)}`, null, null, options); + } + + roomKeyForRoomAndSession(version, roomId, sessionId, options = null) { + return this._get(`/room_keys/keys/${encodeURIComponent(roomId)}/${encodeURIComponent(sessionId)}`, {version}, null, options); + } get mediaRepository() { return this._mediaRepository;