Skip to content

Commit 37fb21f

Browse files
authored
Merge pull request #1640 from uhoreg/room-history-key-sharing2
Add function to share megolm keys for historical messages.
2 parents bf25cb6 + f92b620 commit 37fb21f

File tree

6 files changed

+277
-32
lines changed

6 files changed

+277
-32
lines changed

src/client.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,39 @@ MatrixClient.prototype.deleteKeysFromBackup = function(roomId, sessionId, versio
23032303
);
23042304
};
23052305

2306+
/**
2307+
* Share shared-history decryption keys with the given users.
2308+
*
2309+
* @param {string} roomId the room for which keys should be shared.
2310+
* @param {array} userIds a list of users to share with. The keys will be sent to
2311+
* all of the user's current devices.
2312+
*/
2313+
MatrixClient.prototype.sendSharedHistoryKeys = async function(roomId, userIds) {
2314+
if (this._crypto === null) {
2315+
throw new Error("End-to-end encryption disabled");
2316+
}
2317+
2318+
const roomEncryption = this._roomList.getRoomEncryption(roomId);
2319+
if (!roomEncryption) {
2320+
// unknown room, or unencrypted room
2321+
logger.error("Unknown room. Not sharing decryption keys");
2322+
return;
2323+
}
2324+
2325+
const deviceInfos = await this._crypto.downloadKeys(userIds);
2326+
const devicesByUser = {};
2327+
for (const [userId, devices] of Object.entries(deviceInfos)) {
2328+
devicesByUser[userId] = Object.values(devices);
2329+
}
2330+
2331+
const alg = this._crypto._getRoomDecryptor(roomId, roomEncryption.algorithm);
2332+
if (alg.sendSharedHistoryInboundSessions) {
2333+
await alg.sendSharedHistoryInboundSessions(devicesByUser);
2334+
} else {
2335+
logger.warning("Algorithm does not support sharing previous keys", roomEncryption.algorithm);
2336+
}
2337+
};
2338+
23062339
// Group ops
23072340
// =========
23082341
// Operations on groups that come down the sync stream (ie. ones the

src/crypto/OlmDevice.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,7 @@ OlmDevice.prototype.addInboundGroupSession = async function(
10481048
'readwrite', [
10491049
IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS,
10501050
IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS_WITHHELD,
1051+
IndexedDBCryptoStore.STORE_SHARED_HISTORY_INBOUND_GROUP_SESSIONS,
10511052
], (txn) => {
10521053
/* if we already have this session, consider updating it */
10531054
this._getInboundGroupSession(
@@ -1104,6 +1105,12 @@ OlmDevice.prototype.addInboundGroupSession = async function(
11041105
this._cryptoStore.storeEndToEndInboundGroupSession(
11051106
senderKey, sessionId, sessionData, txn,
11061107
);
1108+
1109+
if (!existingSession && extraSessionData.sharedHistory) {
1110+
this._cryptoStore.addSharedHistoryInboundGroupSession(
1111+
roomId, senderKey, sessionId, txn,
1112+
);
1113+
}
11071114
} finally {
11081115
session.free();
11091116
}
@@ -1383,6 +1390,7 @@ OlmDevice.prototype.getInboundGroupSessionKey = async function(
13831390
"forwarding_curve25519_key_chain":
13841391
sessionData.forwardingCurve25519KeyChain || [],
13851392
"sender_claimed_ed25519_key": senderEd25519Key,
1393+
"shared_history": sessionData.sharedHistory || false,
13861394
};
13871395
},
13881396
);
@@ -1415,10 +1423,24 @@ OlmDevice.prototype.exportInboundGroupSession = function(
14151423
"session_key": session.export_session(messageIndex),
14161424
"forwarding_curve25519_key_chain": session.forwardingCurve25519KeyChain || [],
14171425
"first_known_index": session.first_known_index(),
1426+
"org.matrix.msc3061.shared_history": sessionData.sharedHistory || false,
14181427
};
14191428
});
14201429
};
14211430

1431+
OlmDevice.prototype.getSharedHistoryInboundGroupSessions = async function(roomId) {
1432+
let result;
1433+
await this._cryptoStore.doTxn(
1434+
'readonly', [
1435+
IndexedDBCryptoStore.STORE_SHARED_HISTORY_INBOUND_GROUP_SESSIONS,
1436+
], (txn) => {
1437+
result = this._cryptoStore.getSharedHistoryInboundGroupSessions(roomId, txn);
1438+
},
1439+
logger.withPrefix("[getSharedHistoryInboundGroupSessionsForRoom]"),
1440+
);
1441+
return result;
1442+
};
1443+
14221444
// Utilities
14231445
// =========
14241446

0 commit comments

Comments
 (0)