more impl of SessionBackup

This commit is contained in:
Bruno Windels 2020-09-17 14:19:57 +02:00
parent f2fedae6aa
commit 3941af93d2
2 changed files with 19 additions and 8 deletions

View File

@ -25,32 +25,32 @@ export class SessionBackup {
} }
async getSession(roomId, sessionId) { async getSession(roomId, sessionId) {
const sessionResponse = await this._hsApi.roomKey(this._backupInfo.version, roomId, sessionId).response(); const sessionResponse = await this._hsApi.roomKeyForRoomAndSession(this._backupInfo.version, roomId, sessionId).response();
let sessionInfo;
const decryption = new this._olm.PkDecryption(); const decryption = new this._olm.PkDecryption();
try { try {
decryption.init_with_private_key(this._privateKey); decryption.init_with_private_key(this._privateKey);
sessionInfo = this._decryption.decrypt( const sessionInfo = this._decryption.decrypt(
sessionResponse.session_data.ephemeral, sessionResponse.session_data.ephemeral,
sessionResponse.session_data.mac, sessionResponse.session_data.mac,
sessionResponse.session_data.ciphertext, sessionResponse.session_data.ciphertext,
); );
return JSON.parse(sessionInfo);
} finally { } finally {
decryption.free(); decryption.free();
} }
return JSON.parse(sessionInfo);
} }
static async fromSecretStorage({olm, secretStorage, hsApi}) { static async fromSecretStorage({olm, secretStorage, hsApi}) {
const backupInfo = await hsApi.roomKeysVersion().response();
const base64PrivateKey = await secretStorage.readSecret("m.megolm_backup.v1"); const base64PrivateKey = await secretStorage.readSecret("m.megolm_backup.v1");
if (base64PrivateKey) { if (base64PrivateKey) {
const privateKey = base64.decode(base64PrivateKey); const privateKey = base64.decode(base64PrivateKey);
const backupInfo = await hsApi.roomKeysVersion().response();
const expectedPubKey = backupInfo.auth_data.public_key;
const decryption = new olm.PkDecryption(); const decryption = new olm.PkDecryption();
try { try {
const pubKey = decryption.init_with_private_key(this._privateKey); const pubKey = decryption.init_with_private_key(privateKey);
if (pubKey !== backupInfo.auth_data.public_key) { if (pubKey !== expectedPubKey) {
throw new Error(`Bad backup key, public key does not match. Calculated ${pubKey} but expected ${backupInfo.auth_data.public_key}`); throw new Error(`Bad backup key, public key does not match. Calculated ${pubKey} but expected ${expectedPubKey}`);
} }
} finally { } finally {
decryption.free(); decryption.free();

View File

@ -176,6 +176,17 @@ export class HomeServerApi {
sendToDevice(type, payload, txnId, options = null) { sendToDevice(type, payload, txnId, options = null) {
return this._put(`/sendToDevice/${encodeURIComponent(type)}/${encodeURIComponent(txnId)}`, null, payload, options); 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() { get mediaRepository() {
return this._mediaRepository; return this._mediaRepository;