diff --git a/src/matrix/e2ee/megolm/decryption/KeyLoader.ts b/src/matrix/e2ee/megolm/decryption/KeyLoader.ts index c1925e84..884203a3 100644 --- a/src/matrix/e2ee/megolm/decryption/KeyLoader.ts +++ b/src/matrix/e2ee/megolm/decryption/KeyLoader.ts @@ -213,6 +213,9 @@ class KeyOperation { } } +import {KeySource} from "../../../storage/idb/stores/InboundGroupSessionStore"; + + export function tests() { let instances = 0; @@ -237,6 +240,8 @@ export function tests() { get serializationKey(): string { return `key-${this.sessionId}-${this._firstKnownIndex}`; } get serializationType(): string { return "type"; } get eventIds(): string[] | undefined { return undefined; } + get keySource(): KeySource { return KeySource.DeviceMessage; } + loadInto(session: Olm.InboundGroupSession) { const mockSession = session as MockInboundSession; mockSession.sessionId = this.sessionId; diff --git a/src/matrix/e2ee/megolm/decryption/RoomKey.ts b/src/matrix/e2ee/megolm/decryption/RoomKey.ts index 6112ccef..7b648d92 100644 --- a/src/matrix/e2ee/megolm/decryption/RoomKey.ts +++ b/src/matrix/e2ee/megolm/decryption/RoomKey.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {BackupStatus} from "../../../storage/idb/stores/InboundGroupSessionStore"; +import {BackupStatus, KeySource} from "../../../storage/idb/stores/InboundGroupSessionStore"; import type {InboundGroupSessionEntry} from "../../../storage/idb/stores/InboundGroupSessionStore"; import type {Transaction} from "../../../storage/idb/Transaction"; import type {DecryptionResult} from "../../DecryptionResult"; @@ -83,6 +83,7 @@ export abstract class IncomingRoomKey extends RoomKey { sessionId: this.sessionId, session: pickledSession, backup: this.backupStatus, + source: this.keySource, claimedKeys: {"ed25519": this.claimedEd25519Key}, }; txn.inboundGroupSessions.set(sessionEntry); @@ -131,6 +132,8 @@ export abstract class IncomingRoomKey extends RoomKey { protected get backupStatus(): BackupStatus { return BackupStatus.NotBackedUp; } + + protected abstract get keySource(): KeySource; } class DeviceMessageRoomKey extends IncomingRoomKey { @@ -147,10 +150,12 @@ class DeviceMessageRoomKey extends IncomingRoomKey { get claimedEd25519Key() { return this._decryptionResult.claimedEd25519Key; } get serializationKey(): string { return this._decryptionResult.event.content?.["session_key"]; } get serializationType(): string { return "create"; } + protected get keySource(): KeySource { return KeySource.DeviceMessage; } loadInto(session) { session.create(this.serializationKey); } + } class BackupRoomKey extends IncomingRoomKey { @@ -164,7 +169,8 @@ class BackupRoomKey extends IncomingRoomKey { get claimedEd25519Key() { return this._backupInfo["sender_claimed_keys"]?.["ed25519"]; } get serializationKey(): string { return this._backupInfo["session_key"]; } get serializationType(): string { return "import_session"; } - + protected get keySource(): KeySource { return KeySource.Backup; } + loadInto(session) { session.import_session(this.serializationKey); } diff --git a/src/matrix/storage/idb/schema.ts b/src/matrix/storage/idb/schema.ts index e6644fd0..9c105a71 100644 --- a/src/matrix/storage/idb/schema.ts +++ b/src/matrix/storage/idb/schema.ts @@ -6,7 +6,7 @@ import {addRoomToIdentity} from "../../e2ee/DeviceTracker.js"; import {SESSION_E2EE_KEY_PREFIX} from "../../e2ee/common.js"; import {SummaryData} from "../../room/RoomSummary"; import {RoomMemberStore, MemberData} from "./stores/RoomMemberStore"; -import {InboundGroupSessionStore, InboundGroupSessionEntry, BackupStatus} from "./stores/InboundGroupSessionStore"; +import {InboundGroupSessionStore, InboundGroupSessionEntry, BackupStatus, KeySource} from "./stores/InboundGroupSessionStore"; import {RoomStateEntry} from "./stores/RoomStateStore"; import {SessionStore} from "./stores/SessionStore"; import {Store} from "./Store"; @@ -283,6 +283,11 @@ async function addInboundSessionBackupIndex(db: IDBDatabase, txn: IDBTransaction const inboundGroupSessions = txn.objectStore("inboundGroupSessions"); await iterateCursor(inboundGroupSessions.openCursor(), (value, key, cursor) => { value.backup = BackupStatus.NotBackedUp; + // we'll also have backup keys in here, we can't tell, + // but the worst thing that can happen is that we try + // to backup keys that were already in backup, which + // the server will ignore + value.source = KeySource.DeviceMessage; return NOT_DONE; }); inboundGroupSessions.createIndex("byBackup", "backup", {unique: false}); diff --git a/src/matrix/storage/idb/stores/InboundGroupSessionStore.ts b/src/matrix/storage/idb/stores/InboundGroupSessionStore.ts index 72ea6e9a..96b127f7 100644 --- a/src/matrix/storage/idb/stores/InboundGroupSessionStore.ts +++ b/src/matrix/storage/idb/stores/InboundGroupSessionStore.ts @@ -22,6 +22,11 @@ export enum BackupStatus { BackedUp = 1 } +export enum KeySource { + DeviceMessage = 1, + Backup, +} + export interface InboundGroupSessionEntry { roomId: string; senderKey: string; @@ -29,7 +34,8 @@ export interface InboundGroupSessionEntry { session?: string; claimedKeys?: { [algorithm : string] : string }; eventIds?: string[]; - backup: BackupStatus + backup: BackupStatus, + source: KeySource } type InboundGroupSessionStorageEntry = InboundGroupSessionEntry & { key: string };