1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

Merge branch 'e2e_backups' of git://github.com/uhoreg/matrix-js-sdk into uhoreg-e2e_backups

This commit is contained in:
David Baker
2018-08-24 13:29:29 +01:00
7 changed files with 509 additions and 1 deletions

View File

@@ -814,7 +814,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) {
}
console.log(`Adding key for megolm session ${senderKey}|${sessionId}`);
this._olmDevice.addInboundGroupSession(
return this._olmDevice.addInboundGroupSession(
content.room_id, senderKey, forwardingKeyChain, sessionId,
content.session_key, keysClaimed,
exportFormat,
@@ -829,6 +829,12 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) {
// have another go at decrypting events sent with this session.
this._retryDecryption(senderKey, sessionId);
}).then(() => {
return this.backupGroupSession(
content.room_id, senderKey, forwardingKeyChain,
content.session_id, content.session_key, keysClaimed,
exportFormat,
);
}).catch((e) => {
console.error(`Error handling m.room_key_event: ${e}`);
});
@@ -951,6 +957,54 @@ MegolmDecryption.prototype.importRoomKey = function(session) {
});
};
MegolmDecryption.prototype.backupGroupSession = async function(
roomId, senderKey, forwardingCurve25519KeyChain,
sessionId, sessionKey, keysClaimed,
exportFormat,
) {
// new session.
const session = new Olm.InboundGroupSession();
let first_known_index;
try {
if (exportFormat) {
session.import_session(sessionKey);
} else {
session.create(sessionKey);
}
if (sessionId != session.session_id()) {
throw new Error(
"Mismatched group session ID from senderKey: " +
senderKey,
);
}
if (!exportFormat) {
sessionKey = session.export_session();
}
const first_known_index = session.first_known_index();
const sessionData = {
algorithm: olmlib.MEGOLM_ALGORITHM,
sender_key: senderKey,
sender_claimed_keys: keysClaimed,
forwardingCurve25519KeyChain: forwardingCurve25519KeyChain,
session_key: sessionKey
};
const encrypted = this._crypto.backupKey.encrypt(JSON.stringify(sessionData));
const data = {
first_message_index: first_known_index,
forwarded_count: forwardingCurve25519KeyChain.length,
is_verified: false, // FIXME: how do we determine this?
session_data: encrypted
};
return this._baseApis.sendKeyBackup(roomId, sessionId, data);
} catch (e) {
return Promise.reject(e);
} finally {
session.free();
}
}
/**
* Have another go at decrypting events after we receive a key
*