hydrogen-web/src/matrix/e2ee/megolm/Decryption.js

69 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-09-02 17:54:38 +05:30
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
export class Decryption {
2020-09-02 18:22:19 +05:30
constructor({pickleKey, olm}) {
2020-09-02 17:54:38 +05:30
this._pickleKey = pickleKey;
2020-09-02 18:22:19 +05:30
this._olm = olm;
2020-09-02 17:54:38 +05:30
}
async addRoomKeys(payloads, txn) {
const newSessions = [];
for (const {senderKey, event} of payloads) {
const roomId = event.content?.["room_id"];
const sessionId = event.content?.["session_id"];
const sessionKey = event.content?.["session_key"];
if (
typeof roomId !== "string" ||
typeof sessionId !== "string" ||
typeof senderKey !== "string" ||
typeof sessionKey !== "string"
) {
return;
}
const hasSession = await txn.inboundGroupSessions.has(roomId, senderKey, sessionId);
if (!hasSession) {
const session = new this._olm.InboundGroupSession();
try {
session.create(sessionKey);
const sessionEntry = {
roomId,
senderKey,
sessionId,
session: session.pickle(this._pickleKey),
claimedKeys: event.keys,
};
2020-09-02 18:23:50 +05:30
txn.inboundGroupSessions.set(sessionEntry);
2020-09-02 17:54:38 +05:30
newSessions.push(sessionEntry);
} finally {
session.free();
}
}
}
return newSessions;
}
applyRoomKeyChanges(newSessions) {
// retry decryption with the new sessions
if (newSessions.length) {
console.log(`I have ${newSessions.length} new inbound group sessions`, newSessions)
}
}
}