This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/matrix/e2ee/olm/Encryption.js

273 lines
10 KiB
JavaScript
Raw Normal View History

2020-09-02 21:28:01 +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.
*/
import {groupByWithCreator} from "../../../utils/groupBy.js";
import {verifyEd25519Signature, OLM_ALGORITHM} from "./common.js";
import {createSessionEntry} from "./Session.js";
function findFirstSessionId(sessionIds) {
return sessionIds.reduce((first, sessionId) => {
if (!first || sessionId < first) {
return sessionId;
} else {
return first;
}
}, null);
}
const OTK_ALGORITHM = "signed_curve25519";
export class Encryption {
constructor({account, olm, olmUtil, userId, storage, now, pickleKey, senderKeyLock}) {
2020-09-02 21:28:01 +05:30
this._account = account;
this._olm = olm;
this._olmUtil = olmUtil;
this._userId = userId;
this._storage = storage;
this._now = now;
this._pickleKey = pickleKey;
this._senderKeyLock = senderKeyLock;
2020-09-02 21:28:01 +05:30
}
async encrypt(type, content, devices, hsApi) {
// TODO: see if we can only hold some of the locks until after the /keys/claim call (if needed)
// take a lock on all senderKeys so decryption and other calls to encrypt (should not happen)
// don't modify the sessions at the same time
const locks = await Promise.all(devices.map(device => {
return this._senderKeyLock.takeLock(device.curve25519Key);
}));
2020-09-03 13:23:16 +05:30
try {
const {
devicesWithoutSession,
existingEncryptionTargets,
} = await this._findExistingSessions(devices);
const timestamp = this._now();
let encryptionTargets = [];
try {
if (devicesWithoutSession.length) {
const newEncryptionTargets = await this._createNewSessions(
devicesWithoutSession, hsApi, timestamp);
encryptionTargets = encryptionTargets.concat(newEncryptionTargets);
}
await this._loadSessions(existingEncryptionTargets);
encryptionTargets = encryptionTargets.concat(existingEncryptionTargets);
const messages = encryptionTargets.map(target => {
const content = this._encryptForDevice(type, content, target);
return new EncryptedMessage(content, target.device);
});
await this._storeSessions(encryptionTargets, timestamp);
return messages;
} finally {
for (const target of encryptionTargets) {
target.dispose();
}
2020-09-03 13:23:16 +05:30
}
} finally {
for (const lock of locks) {
lock.release();
2020-09-03 13:23:16 +05:30
}
}
}
async _findExistingSessions(devices) {
2020-09-02 21:28:01 +05:30
const txn = this._storage.readTxn([this._storage.storeNames.olmSessions]);
const sessionIdsForDevice = await Promise.all(devices.map(async device => {
return await txn.olmSessions.getSessionIds(device.curve25519Key);
}));
const devicesWithoutSession = devices.filter((_, i) => {
const sessionIds = sessionIdsForDevice[i];
return !(sessionIds?.length);
});
2020-09-03 13:23:16 +05:30
const existingEncryptionTargets = devices.map((device, i) => {
2020-09-02 21:28:01 +05:30
const sessionIds = sessionIdsForDevice[i];
if (sessionIds?.length > 0) {
const sessionId = findFirstSessionId(sessionIds);
2020-09-03 13:23:16 +05:30
return EncryptionTarget.fromSessionId(device, sessionId);
2020-09-02 21:28:01 +05:30
}
2020-09-03 13:23:16 +05:30
}).filter(target => !!target);
return {devicesWithoutSession, existingEncryptionTargets};
}
_encryptForDevice(type, content, target) {
const {session, device} = target;
const message = session.encrypt(this._buildPlainTextMessageForDevice(type, content, device));
const encryptedContent = {
algorithm: OLM_ALGORITHM,
sender_key: this._account.identityKeys.curve25519,
ciphertext: {
[device.curve25519Key]: message
2020-09-02 21:28:01 +05:30
}
};
2020-09-03 13:23:16 +05:30
return encryptedContent;
2020-09-02 21:28:01 +05:30
}
_buildPlainTextMessageForDevice(type, content, device) {
return {
keys: {
"ed25519": this._account.identityKeys.ed25519
},
recipient_keys: {
"ed25519": device.ed25519Key
},
recipient: device.userId,
sender: this._userId,
content,
type
}
}
2020-09-03 13:23:16 +05:30
async _createNewSessions(devicesWithoutSession, hsApi, timestamp) {
const newEncryptionTargets = await this._claimOneTimeKeys(hsApi, devicesWithoutSession);
try {
for (const target of newEncryptionTargets) {
const {device, oneTimeKey} = target;
target.session = this._account.createOutboundOlmSession(device.curve25519Key, oneTimeKey);
}
this._storeSessions(newEncryptionTargets, timestamp);
} catch (err) {
for (const target of newEncryptionTargets) {
target.dispose();
2020-09-02 21:28:01 +05:30
}
}
2020-09-03 13:23:16 +05:30
return newEncryptionTargets;
2020-09-02 21:28:01 +05:30
}
async _claimOneTimeKeys(hsApi, deviceIdentities) {
// create a Map<userId, Map<deviceId, deviceIdentity>>
const devicesByUser = groupByWithCreator(deviceIdentities,
device => device.userId,
() => new Map(),
(deviceMap, device) => deviceMap.set(device.deviceId, device)
);
const oneTimeKeys = Array.from(devicesByUser.entries()).reduce((usersObj, [userId, deviceMap]) => {
usersObj[userId] = Array.from(deviceMap.values()).reduce((devicesObj, device) => {
devicesObj[device.deviceId] = OTK_ALGORITHM;
return devicesObj;
}, {});
return usersObj;
}, {});
const claimResponse = await hsApi.claimKeys({
timeout: 10000,
one_time_keys: oneTimeKeys
}).response();
// TODO: log claimResponse.failures
const userKeyMap = claimResponse?.["one_time_keys"];
2020-09-03 13:23:16 +05:30
return this._verifyAndCreateOTKTargets(userKeyMap, devicesByUser);
2020-09-02 21:28:01 +05:30
}
2020-09-03 13:23:16 +05:30
_verifyAndCreateOTKTargets(userKeyMap, devicesByUser) {
const verifiedEncryptionTargets = [];
2020-09-02 21:28:01 +05:30
for (const [userId, userSection] of Object.entries(userKeyMap)) {
for (const [deviceId, deviceSection] of Object.entries(userSection)) {
const [firstPropName, keySection] = Object.entries(deviceSection)[0];
const [keyAlgorithm] = firstPropName.split(":");
if (keyAlgorithm === OTK_ALGORITHM) {
const device = devicesByUser.get(userId)?.get(deviceId);
if (device) {
const isValidSignature = verifyEd25519Signature(
this._olmUtil, userId, deviceId, device.ed25519Key, keySection);
if (isValidSignature) {
2020-09-03 13:23:16 +05:30
const target = EncryptionTarget.fromOTK(device, keySection.key);
verifiedEncryptionTargets.push(target);
2020-09-02 21:28:01 +05:30
}
}
}
}
}
2020-09-03 13:23:16 +05:30
return verifiedEncryptionTargets;
2020-09-02 21:28:01 +05:30
}
2020-09-03 13:23:16 +05:30
async _loadSessions(encryptionTargets) {
2020-09-02 21:28:01 +05:30
const txn = this._storage.readTxn([this._storage.storeNames.olmSessions]);
2020-09-03 13:23:16 +05:30
// given we run loading in parallel, there might still be some
// storage requests that will finish later once one has failed.
// those should not allocate a session anymore.
let failed = false;
try {
await Promise.all(encryptionTargets.map(async encryptionTarget => {
const sessionEntry = await txn.olmSessions.get(
encryptionTarget.device.curve25519Key, encryptionTarget.sessionId);
if (sessionEntry && !failed) {
const olmSession = new this._olm.Session();
olmSession.unpickle(this._pickleKey, sessionEntry.session);
encryptionTarget.session = olmSession;
}
}));
} catch (err) {
failed = true;
// clean up the sessions that did load
for (const target of encryptionTargets) {
target.dispose();
2020-09-02 21:28:01 +05:30
}
2020-09-03 13:23:16 +05:30
throw err;
}
2020-09-02 21:28:01 +05:30
}
2020-09-03 13:23:16 +05:30
async _storeSessions(encryptionTargets, timestamp) {
2020-09-02 21:28:01 +05:30
const txn = this._storage.readWriteTxn([this._storage.storeNames.olmSessions]);
try {
2020-09-03 13:23:16 +05:30
for (const target of encryptionTargets) {
2020-09-02 21:28:01 +05:30
const sessionEntry = createSessionEntry(
2020-09-03 13:23:16 +05:30
target.session, target.device.curve25519Key, timestamp, this._pickleKey);
2020-09-02 21:28:01 +05:30
txn.olmSessions.set(sessionEntry);
}
} catch (err) {
txn.abort();
throw err;
}
await txn.complete();
}
}
// just a container needed to encrypt a message for a recipient device
// it is constructed with either a oneTimeKey
// (and later converted to a session) in case of a new session
// or an existing session
2020-09-03 13:23:16 +05:30
class EncryptionTarget {
2020-09-02 21:28:01 +05:30
constructor(device, oneTimeKey, sessionId) {
this.device = device;
this.oneTimeKey = oneTimeKey;
this.sessionId = sessionId;
// an olmSession, should probably be called olmSession
this.session = null;
}
static fromOTK(device, oneTimeKey) {
2020-09-03 13:23:16 +05:30
return new EncryptionTarget(device, oneTimeKey, null);
2020-09-02 21:28:01 +05:30
}
static fromSessionId(device, sessionId) {
2020-09-03 13:23:16 +05:30
return new EncryptionTarget(device, null, sessionId);
2020-09-02 21:28:01 +05:30
}
dispose() {
if (this.session) {
this.session.free();
}
}
}
2020-09-03 13:23:16 +05:30
class EncryptedMessage {
constructor(content, device) {
this.content = content;
this.device = device;
}
}