From 5c4229433661dfb2cddb4a502412797354b9f3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 9 Jun 2020 13:53:27 +0200 Subject: [PATCH] EventIndex: Store and restore the encryption info for encrypted events. --- src/Searching.js | 20 ++++++++++++++++++++ src/indexing/EventIndex.js | 33 +++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/Searching.js b/src/Searching.js index 663328fe41..fb59cc563d 100644 --- a/src/Searching.js +++ b/src/Searching.js @@ -107,6 +107,26 @@ async function localSearch(searchTerm, roomId = undefined) { const result = MatrixClientPeg.get()._processRoomEventsSearch( emptyResult, response); + // Restore our encryption info so we can properly re-verify the events. + for (let i = 0; i < result.results.length; i++) { + const timeline = result.results[i].context.getTimeline(); + + for (let j = 0; j < timeline.length; j++) { + const ev = timeline[j]; + if (ev.event.curve25519Key) { + ev.makeEncrypted( + "m.room.encrypted", + { algorithm: ev.event.algorithm }, + ev.event.curve25519Key, + ev.event.ed25519Key, + ); + ev._forwardingCurve25519KeyChain = ev.event.forwardingCurve25519KeyChain; + + delete ev.event.curve25519Key; + } + } + } + return result; } diff --git a/src/indexing/EventIndex.js b/src/indexing/EventIndex.js index fac7c92b65..d4e8ab0117 100644 --- a/src/indexing/EventIndex.js +++ b/src/indexing/EventIndex.js @@ -290,6 +290,33 @@ export default class EventIndex extends EventEmitter { return validEventType && validMsgType && hasContentValue; } + eventToJson(ev) { + const jsonEvent = ev.toJSON(); + const e = ev.isEncrypted() ? jsonEvent.decrypted : jsonEvent; + + if (ev.isEncrypted()) { + // Let us store some additional data so we can re-verify the event. + // The js-sdk checks if an event is encrypted using the algorithm, + // the sender key and ed25519 signing key are used to find the + // correct device that sent the event which allows us to check the + // verification state of the event, either directly or using cross + // signing. + e.curve25519Key = ev.getSenderKey(); + e.ed25519Key = ev.getClaimedEd25519Key(); + e.algorithm = ev.getWireContent().algorithm; + e.forwardingCurve25519KeyChain = ev.getForwardingCurve25519KeyChain(); + } else { + // Make sure that unencrypted events don't contain any of that data, + // despite what the server might give to us. + delete e.curve25519Key; + delete e.ed25519Key; + delete e.algorithm; + delete e.forwardingCurve25519KeyChain; + } + + return e; + } + /** * Queue up live events to be added to the event index. * @@ -300,8 +327,7 @@ export default class EventIndex extends EventEmitter { if (!this.isValidEvent(ev)) return; - const jsonEvent = ev.toJSON(); - const e = ev.isEncrypted() ? jsonEvent.decrypted : jsonEvent; + const e = this.eventToJson(ev); const profile = { displayname: ev.sender.rawDisplayName, @@ -477,8 +503,7 @@ export default class EventIndex extends EventEmitter { // Let us convert the events back into a format that EventIndex can // consume. const events = filteredEvents.map((ev) => { - const jsonEvent = ev.toJSON(); - const e = ev.isEncrypted() ? jsonEvent.decrypted : jsonEvent; + const e = this.eventToJson(ev); let profile = {}; if (e.sender in profiles) profile = profiles[e.sender];