-
-
Notifications
You must be signed in to change notification settings - Fork 649
Add a function to share decryption keys for room history #1629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1681,6 +1681,140 @@ MegolmDecryption.prototype.retryDecryptionFromSender = async function(senderKey) | |
| return !this._pendingEvents[senderKey]; | ||
| }; | ||
|
|
||
| /** | ||
| * Share the keys with the given users for the given messages. | ||
| * | ||
| * @param {object} devicesByUser a map of user to array of module:crypto/deviceinfo. | ||
| * @param {function} nextMessage a function that returns the next Matrix message to | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would a general user of the JS SDK (outside of Element) have any idea what this function should be...? Either we need much more extensive documentation here, or we should more likely move this function into the JS SDK itself, since at least the draft version doesn't seem to have a UI component. |
||
| * to share keys for each time it is called. The function should return a | ||
| * {module:models/event.MatrixEvent}. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we should more explicitly say it's a promise of an event. |
||
| */ | ||
| MegolmDecryption.prototype.shareKeysForMessages = async function(devicesByUser, nextMessage) { | ||
| await olmlib.ensureOlmSessionsForDevices( | ||
| this._olmDevice, this._baseApis, devicesByUser, | ||
| ); | ||
|
|
||
| logger.log("shareKeysForMessages to users", Object.keys(devicesByUser)); | ||
|
|
||
| const shareSession = async (senderKey, sessionId, index) => { | ||
| logger.log("Sharing session", senderKey, sessionId, index); | ||
| const key = await this._olmDevice.getInboundGroupSessionKey( | ||
| this._roomId, senderKey, sessionId, index, | ||
| ); | ||
|
|
||
| const payload = { | ||
| type: "m.forwarded_room_key", | ||
| content: { | ||
| algorithm: olmlib.MEGOLM_ALGORITHM, | ||
| room_id: this._roomId, | ||
| session_id: sessionId, | ||
| session_key: key.key, | ||
| chain_index: key.chain_index, | ||
| sender_key: senderKey, | ||
| sender_claimed_ed25519_key: key.sender_claimed_ed25519_key, | ||
| forwarding_curve25519_key_chain: key.forwarding_curve25519_key_chain, | ||
| }, | ||
| }; | ||
|
|
||
| const promises = []; | ||
| const contentMap = {}; | ||
| for (const [userId, devices] of Object.entries(devicesByUser)) { | ||
| contentMap[userId] = {}; | ||
| for (const deviceInfo of devices) { | ||
| const encryptedContent = { | ||
| algorithm: olmlib.OLM_ALGORITHM, | ||
| sender_key: this._olmDevice.deviceCurve25519Key, | ||
| ciphertext: {}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be empty...? 🤔 |
||
| }; | ||
| contentMap[userId][deviceInfo.deviceId] = encryptedContent; | ||
| promises.push( | ||
| olmlib.encryptMessageForDevice( | ||
| encryptedContent.ciphertext, | ||
| this._userId, | ||
| this._deviceId, | ||
| this._olmDevice, | ||
| userId, | ||
| deviceInfo, | ||
| payload, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| await Promise.all(promises); | ||
|
|
||
| // prune out any devices that encryptMessageForDevice could not encrypt for, | ||
| // in which case it will have just not added anything to the ciphertext object. | ||
| // There's no point sending messages to devices if we couldn't encrypt to them, | ||
| // since that's effectively a blank message. | ||
| for (const userId of Object.keys(contentMap)) { | ||
| for (const deviceId of Object.keys(contentMap[userId])) { | ||
| if (Object.keys(contentMap[userId][deviceId].ciphertext).length === 0) { | ||
| logger.log( | ||
| "No ciphertext for device " + | ||
| userId + ":" + deviceId + ": pruning", | ||
| ); | ||
| delete contentMap[userId][deviceId]; | ||
| } | ||
| } | ||
| // No devices left for that user? Strip that too. | ||
| if (Object.keys(contentMap[userId]).length === 0) { | ||
| logger.log("Pruned all devices for user " + userId); | ||
| delete contentMap[userId]; | ||
| } | ||
| } | ||
|
|
||
| // Is there anything left? | ||
| if (Object.keys(contentMap).length === 0) { | ||
| logger.log("No users left to send to: aborting"); | ||
| return; | ||
| } | ||
|
|
||
| await this._baseApis.sendToDevice("m.room.encrypted", contentMap); | ||
| }; | ||
|
|
||
| const sessionBySenderKey = {}; | ||
| for (let message = await nextMessage(); message !== undefined; message = await nextMessage()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe test for just |
||
| if (!message.isEncrypted() || | ||
| message.getWireContent().algorithm !== olmlib.MEGOLM_ALGORITHM) { | ||
| continue; | ||
| } | ||
| const content = message.getWireContent(); | ||
| const senderKey = content.sender_key; | ||
| const sessionId = content.session_id; | ||
| let res; | ||
| try { | ||
| res = await this._olmDevice.decryptGroupMessage( | ||
| message.getRoomId(), content.sender_key, content.session_id, content.ciphertext, | ||
| message.getId(), message.getTs(), | ||
| ); | ||
| } catch (e) { | ||
| continue; | ||
| } | ||
| const index = res.message_index; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update here as well if naming is changed. |
||
| if (!Number.isFinite(index)) { | ||
| continue; | ||
| } | ||
| if (senderKey in sessionBySenderKey) { | ||
| const [oldSessionId, oldIndex] = sessionBySenderKey[senderKey]; | ||
| if (oldSessionId === sessionId) { | ||
| if (oldIndex >= index) { | ||
| sessionBySenderKey[senderKey] = [sessionId, index]; | ||
| } | ||
| } else { | ||
| sessionBySenderKey[senderKey] = [sessionId, index]; | ||
|
|
||
| await shareSession(senderKey, oldSessionId, oldIndex); | ||
| } | ||
| } else { | ||
| sessionBySenderKey[senderKey] = [sessionId, index]; | ||
| } | ||
| } | ||
|
|
||
| for (const [senderKey, [sessionId, index]] of Object.entries(sessionBySenderKey)) { | ||
| await shareSession(senderKey, sessionId, index); | ||
| } | ||
| }; | ||
|
|
||
| registerAlgorithm( | ||
| olmlib.MEGOLM_ALGORITHM, MegolmEncryption, MegolmDecryption, | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this instead match the style of other properties...?