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.
|
|
|
|
*/
|
|
|
|
|
2020-09-04 15:36:26 +05:30
|
|
|
import {DecryptionError} from "../common.js";
|
2020-09-09 19:58:43 +05:30
|
|
|
import {groupBy} from "../../../utils/groupBy.js";
|
2020-09-04 15:36:26 +05:30
|
|
|
|
2020-09-09 19:58:43 +05:30
|
|
|
import {SessionInfo} from "./decryption/SessionInfo.js";
|
|
|
|
import {DecryptionPreparation} from "./decryption/DecryptionPreparation.js";
|
|
|
|
import {SessionDecryption} from "./decryption/SessionDecryption.js";
|
|
|
|
import {SessionCache} from "./decryption/SessionCache.js";
|
2020-09-10 22:11:23 +05:30
|
|
|
import {DecryptionWorker} from "./decryption/DecryptionWorker.js";
|
2020-09-09 19:58:43 +05:30
|
|
|
|
|
|
|
function getSenderKey(event) {
|
|
|
|
return event.content?.["sender_key"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSessionId(event) {
|
|
|
|
return event.content?.["session_id"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCiphertext(event) {
|
|
|
|
return event.content?.ciphertext;
|
|
|
|
}
|
2020-09-04 15:36:26 +05:30
|
|
|
|
2020-09-02 17:54:38 +05:30
|
|
|
export class Decryption {
|
2020-09-10 22:11:23 +05:30
|
|
|
constructor({pickleKey, olm, workerPool}) {
|
2020-09-02 17:54:38 +05:30
|
|
|
this._pickleKey = pickleKey;
|
2020-09-02 18:22:19 +05:30
|
|
|
this._olm = olm;
|
2020-09-10 22:11:23 +05:30
|
|
|
this._decryptor = workerPool ? new DecryptionWorker(workerPool) : null;
|
2020-09-02 17:54:38 +05:30
|
|
|
}
|
|
|
|
|
2020-09-09 19:58:43 +05:30
|
|
|
createSessionCache(fallback) {
|
|
|
|
return new SessionCache(fallback);
|
2020-09-04 15:36:26 +05:30
|
|
|
}
|
|
|
|
|
2020-09-08 14:18:11 +05:30
|
|
|
/**
|
2020-09-09 19:58:43 +05:30
|
|
|
* Reads all the state from storage to be able to decrypt the given events.
|
|
|
|
* Decryption can then happen outside of a storage transaction.
|
2020-09-08 14:18:11 +05:30
|
|
|
* @param {[type]} roomId [description]
|
2020-09-09 19:58:43 +05:30
|
|
|
* @param {[type]} events [description]
|
2020-09-08 14:18:11 +05:30
|
|
|
* @param {[type]} sessionCache [description]
|
|
|
|
* @param {[type]} txn [description]
|
2020-09-09 19:58:43 +05:30
|
|
|
* @return {DecryptionPreparation}
|
2020-09-08 14:18:11 +05:30
|
|
|
*/
|
2020-09-09 19:58:43 +05:30
|
|
|
async prepareDecryptAll(roomId, events, sessionCache, txn) {
|
|
|
|
const errors = new Map();
|
|
|
|
const validEvents = [];
|
|
|
|
|
|
|
|
for (const event of events) {
|
|
|
|
const isValid = typeof getSenderKey(event) === "string" &&
|
|
|
|
typeof getSessionId(event) === "string" &&
|
|
|
|
typeof getCiphertext(event) === "string";
|
|
|
|
if (isValid) {
|
|
|
|
validEvents.push(event);
|
|
|
|
} else {
|
|
|
|
errors.set(event.event_id, new DecryptionError("MEGOLM_INVALID_EVENT", event))
|
|
|
|
}
|
2020-09-04 15:36:26 +05:30
|
|
|
}
|
|
|
|
|
2020-09-09 19:58:43 +05:30
|
|
|
const eventsBySession = groupBy(validEvents, event => {
|
|
|
|
return `${getSenderKey(event)}|${getSessionId(event)}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
const sessionDecryptions = [];
|
|
|
|
|
|
|
|
await Promise.all(Array.from(eventsBySession.values()).map(async eventsForSession => {
|
|
|
|
const first = eventsForSession[0];
|
|
|
|
const senderKey = getSenderKey(first);
|
|
|
|
const sessionId = getSessionId(first);
|
|
|
|
const sessionInfo = await this._getSessionInfo(roomId, senderKey, sessionId, sessionCache, txn);
|
|
|
|
if (!sessionInfo) {
|
|
|
|
for (const event of eventsForSession) {
|
|
|
|
errors.set(event.event_id, new DecryptionError("MEGOLM_NO_SESSION", event));
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-10 16:30:11 +05:30
|
|
|
sessionDecryptions.push(new SessionDecryption(sessionInfo, eventsForSession, this._decryptor));
|
2020-09-09 19:58:43 +05:30
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
return new DecryptionPreparation(roomId, sessionDecryptions, errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getSessionInfo(roomId, senderKey, sessionId, sessionCache, txn) {
|
|
|
|
let sessionInfo;
|
|
|
|
sessionInfo = sessionCache.get(roomId, senderKey, sessionId);
|
|
|
|
if (!sessionInfo) {
|
2020-09-04 15:36:26 +05:30
|
|
|
const sessionEntry = await txn.inboundGroupSessions.get(roomId, senderKey, sessionId);
|
|
|
|
if (sessionEntry) {
|
2020-09-09 19:58:43 +05:30
|
|
|
let session = new this._olm.InboundGroupSession();
|
2020-09-04 15:36:26 +05:30
|
|
|
try {
|
|
|
|
session.unpickle(this._pickleKey, sessionEntry.session);
|
2020-09-09 19:58:43 +05:30
|
|
|
sessionInfo = new SessionInfo(roomId, senderKey, session, sessionEntry.claimedKeys);
|
2020-09-04 15:36:26 +05:30
|
|
|
} catch (err) {
|
|
|
|
session.free();
|
|
|
|
throw err;
|
|
|
|
}
|
2020-09-09 19:58:43 +05:30
|
|
|
sessionCache.add(sessionInfo);
|
2020-09-04 15:36:26 +05:30
|
|
|
}
|
|
|
|
}
|
2020-09-09 19:58:43 +05:30
|
|
|
return sessionInfo;
|
2020-09-04 15:36:26 +05:30
|
|
|
}
|
|
|
|
|
2020-09-08 14:22:02 +05:30
|
|
|
/**
|
|
|
|
* @type {MegolmInboundSessionDescription}
|
|
|
|
* @property {string} senderKey the sender key of the session
|
|
|
|
* @property {string} sessionId the session identifier
|
|
|
|
*
|
|
|
|
* Adds room keys as inbound group sessions
|
|
|
|
* @param {Array<OlmDecryptionResult>} decryptionResults an array of m.room_key decryption results.
|
|
|
|
* @param {[type]} txn a storage transaction with read/write on inboundGroupSessions
|
|
|
|
* @return {Promise<Array<MegolmInboundSessionDescription>>} an array with the newly added sessions
|
|
|
|
*/
|
|
|
|
async addRoomKeys(decryptionResults, txn) {
|
2020-09-02 17:54:38 +05:30
|
|
|
const newSessions = [];
|
2020-09-08 14:22:02 +05:30
|
|
|
for (const {senderCurve25519Key: senderKey, event, claimedEd25519Key} of decryptionResults) {
|
2020-09-02 17:54:38 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:16:13 +05:30
|
|
|
// TODO: compare first_known_index to see which session to keep
|
2020-09-02 17:54:38 +05:30
|
|
|
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),
|
2020-09-08 14:22:02 +05:30
|
|
|
claimedKeys: {ed25519: claimedEd25519Key},
|
2020-09-02 17:54:38 +05:30
|
|
|
};
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-09-04 15:36:26 +05:30
|
|
|
// this will be passed to the Room in notifyRoomKeys
|
2020-09-02 17:54:38 +05:30
|
|
|
return newSessions;
|
|
|
|
}
|
2020-09-04 15:36:26 +05:30
|
|
|
}
|
|
|
|
|