2020-09-02 17:58:01 +02:00
|
|
|
/*
|
|
|
|
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";
|
2020-09-03 15:29:09 +02:00
|
|
|
import {verifyEd25519Signature, OLM_ALGORITHM} from "../common.js";
|
2020-09-02 17:58:01 +02:00
|
|
|
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 {
|
2020-09-03 15:29:35 +02:00
|
|
|
constructor({account, olm, olmUtil, ownUserId, storage, now, pickleKey, senderKeyLock}) {
|
2020-09-02 17:58:01 +02:00
|
|
|
this._account = account;
|
|
|
|
this._olm = olm;
|
|
|
|
this._olmUtil = olmUtil;
|
2020-09-03 15:29:35 +02:00
|
|
|
this._ownUserId = ownUserId;
|
2020-09-02 17:58:01 +02:00
|
|
|
this._storage = storage;
|
|
|
|
this._now = now;
|
|
|
|
this._pickleKey = pickleKey;
|
2020-09-03 12:12:33 +02:00
|
|
|
this._senderKeyLock = senderKeyLock;
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async encrypt(type, content, devices, hsApi) {
|
2020-09-03 12:12:33 +02:00
|
|
|
// 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 09:53:16 +02:00
|
|
|
try {
|
2020-09-03 12:12:33 +02:00
|
|
|
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 09:53:16 +02:00
|
|
|
}
|
|
|
|
} finally {
|
2020-09-03 12:12:33 +02:00
|
|
|
for (const lock of locks) {
|
|
|
|
lock.release();
|
2020-09-03 09:53:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _findExistingSessions(devices) {
|
2020-09-03 15:30:01 +02:00
|
|
|
const txn = await this._storage.readTxn([this._storage.storeNames.olmSessions]);
|
2020-09-02 17:58:01 +02:00
|
|
|
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 09:53:16 +02:00
|
|
|
const existingEncryptionTargets = devices.map((device, i) => {
|
2020-09-02 17:58:01 +02:00
|
|
|
const sessionIds = sessionIdsForDevice[i];
|
|
|
|
if (sessionIds?.length > 0) {
|
|
|
|
const sessionId = findFirstSessionId(sessionIds);
|
2020-09-03 09:53:16 +02:00
|
|
|
return EncryptionTarget.fromSessionId(device, sessionId);
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
2020-09-03 09:53:16 +02:00
|
|
|
}).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 17:58:01 +02:00
|
|
|
}
|
|
|
|
};
|
2020-09-03 09:53:16 +02:00
|
|
|
return encryptedContent;
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_buildPlainTextMessageForDevice(type, content, device) {
|
|
|
|
return {
|
|
|
|
keys: {
|
|
|
|
"ed25519": this._account.identityKeys.ed25519
|
|
|
|
},
|
|
|
|
recipient_keys: {
|
|
|
|
"ed25519": device.ed25519Key
|
|
|
|
},
|
|
|
|
recipient: device.userId,
|
2020-09-03 15:29:35 +02:00
|
|
|
sender: this._ownUserId,
|
2020-09-02 17:58:01 +02:00
|
|
|
content,
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:53:16 +02:00
|
|
|
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 17:58:01 +02:00
|
|
|
}
|
2020-09-03 15:30:09 +02:00
|
|
|
throw err;
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
2020-09-03 09:53:16 +02:00
|
|
|
return newEncryptionTargets;
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 09:53:16 +02:00
|
|
|
return this._verifyAndCreateOTKTargets(userKeyMap, devicesByUser);
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 09:53:16 +02:00
|
|
|
_verifyAndCreateOTKTargets(userKeyMap, devicesByUser) {
|
|
|
|
const verifiedEncryptionTargets = [];
|
2020-09-02 17:58:01 +02:00
|
|
|
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 09:53:16 +02:00
|
|
|
const target = EncryptionTarget.fromOTK(device, keySection.key);
|
|
|
|
verifiedEncryptionTargets.push(target);
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-03 09:53:16 +02:00
|
|
|
return verifiedEncryptionTargets;
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 09:53:16 +02:00
|
|
|
async _loadSessions(encryptionTargets) {
|
2020-09-03 15:30:01 +02:00
|
|
|
const txn = await this._storage.readTxn([this._storage.storeNames.olmSessions]);
|
2020-09-03 09:53:16 +02:00
|
|
|
// 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 17:58:01 +02:00
|
|
|
}
|
2020-09-03 09:53:16 +02:00
|
|
|
throw err;
|
|
|
|
}
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 09:53:16 +02:00
|
|
|
async _storeSessions(encryptionTargets, timestamp) {
|
2020-09-03 15:30:01 +02:00
|
|
|
const txn = await this._storage.readWriteTxn([this._storage.storeNames.olmSessions]);
|
2020-09-02 17:58:01 +02:00
|
|
|
try {
|
2020-09-03 09:53:16 +02:00
|
|
|
for (const target of encryptionTargets) {
|
2020-09-02 17:58:01 +02:00
|
|
|
const sessionEntry = createSessionEntry(
|
2020-09-03 09:53:16 +02:00
|
|
|
target.session, target.device.curve25519Key, timestamp, this._pickleKey);
|
2020-09-02 17:58:01 +02:00
|
|
|
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 09:53:16 +02:00
|
|
|
class EncryptionTarget {
|
2020-09-02 17:58:01 +02:00
|
|
|
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 09:53:16 +02:00
|
|
|
return new EncryptionTarget(device, oneTimeKey, null);
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromSessionId(device, sessionId) {
|
2020-09-03 09:53:16 +02:00
|
|
|
return new EncryptionTarget(device, null, sessionId);
|
2020-09-02 17:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
if (this.session) {
|
|
|
|
this.session.free();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-03 09:53:16 +02:00
|
|
|
|
|
|
|
class EncryptedMessage {
|
|
|
|
constructor(content, device) {
|
|
|
|
this.content = content;
|
|
|
|
this.device = device;
|
|
|
|
}
|
|
|
|
}
|