2020-08-31 17:41:08 +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-02 21:07:48 +05:30
|
|
|
import {verifyEd25519Signature, SIGNATURE_ALGORITHM} from "./common.js";
|
2020-08-31 17:41:08 +05:30
|
|
|
|
|
|
|
const TRACKING_STATUS_OUTDATED = 0;
|
|
|
|
const TRACKING_STATUS_UPTODATE = 1;
|
|
|
|
|
|
|
|
// map 1 device from /keys/query response to DeviceIdentity
|
|
|
|
function deviceKeysAsDeviceIdentity(deviceSection) {
|
|
|
|
const deviceId = deviceSection["device_id"];
|
2020-08-31 19:35:04 +05:30
|
|
|
const userId = deviceSection["user_id"];
|
2020-08-31 17:41:08 +05:30
|
|
|
return {
|
|
|
|
userId,
|
|
|
|
deviceId,
|
2020-09-08 14:21:01 +05:30
|
|
|
ed25519Key: deviceSection.keys[`ed25519:${deviceId}`],
|
|
|
|
curve25519Key: deviceSection.keys[`curve25519:${deviceId}`],
|
2020-08-31 17:41:08 +05:30
|
|
|
algorithms: deviceSection.algorithms,
|
|
|
|
displayName: deviceSection.unsigned?.device_display_name,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DeviceTracker {
|
2020-09-03 18:58:03 +05:30
|
|
|
constructor({storage, getSyncToken, olmUtil, ownUserId, ownDeviceId}) {
|
2020-08-31 17:41:08 +05:30
|
|
|
this._storage = storage;
|
|
|
|
this._getSyncToken = getSyncToken;
|
|
|
|
this._identityChangedForRoom = null;
|
2020-09-01 21:27:59 +05:30
|
|
|
this._olmUtil = olmUtil;
|
2020-09-03 18:58:03 +05:30
|
|
|
this._ownUserId = ownUserId;
|
|
|
|
this._ownDeviceId = ownDeviceId;
|
2020-08-31 17:41:08 +05:30
|
|
|
}
|
|
|
|
|
2021-02-19 00:26:10 +05:30
|
|
|
async writeDeviceChanges(changed, txn, log) {
|
2020-08-31 17:41:08 +05:30
|
|
|
const {userIdentities} = txn;
|
2021-02-12 01:38:06 +05:30
|
|
|
// TODO: should we also look at left here to handle this?:
|
|
|
|
// the usual problem here is that you share a room with a user,
|
|
|
|
// go offline, the remote user leaves the room, changes their devices,
|
|
|
|
// then rejoins the room you share (or another room).
|
|
|
|
// At which point you come online, all of this happens in the gap,
|
|
|
|
// and you don't notice that they ever left,
|
|
|
|
// and so the client doesn't invalidate their device cache for the user
|
2021-02-19 00:26:10 +05:30
|
|
|
log.set("changed", changed.length);
|
|
|
|
await Promise.all(changed.map(async userId => {
|
|
|
|
const user = await userIdentities.get(userId);
|
|
|
|
if (user) {
|
|
|
|
log.log({l: "outdated", id: userId});
|
|
|
|
user.deviceTrackingStatus = TRACKING_STATUS_OUTDATED;
|
|
|
|
userIdentities.set(user);
|
|
|
|
}
|
|
|
|
}));
|
2020-08-31 17:41:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
writeMemberChanges(room, memberChanges, txn) {
|
|
|
|
return Promise.all(Array.from(memberChanges.values()).map(async memberChange => {
|
|
|
|
return this._applyMemberChange(memberChange, txn);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-02-23 23:52:59 +05:30
|
|
|
async trackRoom(room, log) {
|
2020-09-08 17:54:48 +05:30
|
|
|
if (room.isTrackingMembers || !room.isEncrypted) {
|
2020-08-31 17:41:08 +05:30
|
|
|
return;
|
|
|
|
}
|
2021-02-23 23:52:59 +05:30
|
|
|
const memberList = await room.loadMemberList(log);
|
2020-08-31 17:41:08 +05:30
|
|
|
try {
|
2021-03-05 00:17:02 +05:30
|
|
|
const txn = await this._storage.readWriteTxn([
|
2020-08-31 17:41:08 +05:30
|
|
|
this._storage.storeNames.roomSummary,
|
|
|
|
this._storage.storeNames.userIdentities,
|
|
|
|
]);
|
|
|
|
let isTrackingChanges;
|
|
|
|
try {
|
|
|
|
isTrackingChanges = room.writeIsTrackingMembers(true, txn);
|
|
|
|
const members = Array.from(memberList.members.values());
|
2021-02-23 23:52:59 +05:30
|
|
|
log.set("members", members.length);
|
2020-08-31 17:41:08 +05:30
|
|
|
await this._writeJoinedMembers(members, txn);
|
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await txn.complete();
|
|
|
|
room.applyIsTrackingMembersChanges(isTrackingChanges);
|
|
|
|
} finally {
|
|
|
|
memberList.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _writeJoinedMembers(members, txn) {
|
|
|
|
await Promise.all(members.map(async member => {
|
|
|
|
if (member.membership === "join") {
|
|
|
|
await this._writeMember(member, txn);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
async _writeMember(member, txn) {
|
|
|
|
const {userIdentities} = txn;
|
|
|
|
const identity = await userIdentities.get(member.userId);
|
|
|
|
if (!identity) {
|
|
|
|
userIdentities.set({
|
|
|
|
userId: member.userId,
|
|
|
|
roomIds: [member.roomId],
|
|
|
|
deviceTrackingStatus: TRACKING_STATUS_OUTDATED,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (!identity.roomIds.includes(member.roomId)) {
|
|
|
|
identity.roomIds.push(member.roomId);
|
|
|
|
userIdentities.set(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _applyMemberChange(memberChange, txn) {
|
|
|
|
// TODO: depends whether we encrypt for invited users??
|
|
|
|
// add room
|
|
|
|
if (memberChange.previousMembership !== "join" && memberChange.membership === "join") {
|
|
|
|
await this._writeMember(memberChange.member, txn);
|
|
|
|
}
|
|
|
|
// remove room
|
|
|
|
else if (memberChange.previousMembership === "join" && memberChange.membership !== "join") {
|
|
|
|
const {userIdentities} = txn;
|
|
|
|
const identity = await userIdentities.get(memberChange.userId);
|
|
|
|
if (identity) {
|
|
|
|
identity.roomIds = identity.roomIds.filter(roomId => roomId !== memberChange.roomId);
|
|
|
|
// no more encrypted rooms with this user, remove
|
|
|
|
if (identity.roomIds.length === 0) {
|
|
|
|
userIdentities.remove(identity.userId);
|
|
|
|
} else {
|
|
|
|
userIdentities.set(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 23:52:59 +05:30
|
|
|
async _queryKeys(userIds, hsApi, log) {
|
2020-08-31 17:41:08 +05:30
|
|
|
// TODO: we need to handle the race here between /sync and /keys/query just like we need to do for the member list ...
|
|
|
|
// there are multiple requests going out for /keys/query though and only one for /members
|
|
|
|
|
2020-08-31 17:54:09 +05:30
|
|
|
const deviceKeyResponse = await hsApi.queryKeys({
|
2020-08-31 17:41:08 +05:30
|
|
|
"timeout": 10000,
|
|
|
|
"device_keys": userIds.reduce((deviceKeysMap, userId) => {
|
|
|
|
deviceKeysMap[userId] = [];
|
|
|
|
return deviceKeysMap;
|
|
|
|
}, {}),
|
|
|
|
"token": this._getSyncToken()
|
2021-02-23 23:52:59 +05:30
|
|
|
}, {log}).response();
|
2020-08-31 17:41:08 +05:30
|
|
|
|
2021-02-23 23:52:59 +05:30
|
|
|
const verifiedKeysPerUser = log.wrap("verify", log => this._filterVerifiedDeviceKeys(deviceKeyResponse["device_keys"], log));
|
2021-03-05 00:17:02 +05:30
|
|
|
const txn = await this._storage.readWriteTxn([
|
2020-08-31 17:41:08 +05:30
|
|
|
this._storage.storeNames.userIdentities,
|
|
|
|
this._storage.storeNames.deviceIdentities,
|
|
|
|
]);
|
|
|
|
let deviceIdentities;
|
|
|
|
try {
|
2020-09-14 19:14:47 +05:30
|
|
|
const devicesIdentitiesPerUser = await Promise.all(verifiedKeysPerUser.map(async ({userId, verifiedKeys}) => {
|
|
|
|
const deviceIdentities = verifiedKeys.map(deviceKeysAsDeviceIdentity);
|
|
|
|
return await this._storeQueriedDevicesForUserId(userId, deviceIdentities, txn);
|
2020-08-31 17:41:08 +05:30
|
|
|
}));
|
2020-09-14 19:14:47 +05:30
|
|
|
deviceIdentities = devicesIdentitiesPerUser.reduce((all, devices) => all.concat(devices), []);
|
2021-02-23 23:52:59 +05:30
|
|
|
log.set("devices", deviceIdentities.length);
|
2020-08-31 17:41:08 +05:30
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await txn.complete();
|
|
|
|
return deviceIdentities;
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:14:47 +05:30
|
|
|
async _storeQueriedDevicesForUserId(userId, deviceIdentities, txn) {
|
2020-09-14 22:01:54 +05:30
|
|
|
const knownDeviceIds = await txn.deviceIdentities.getAllDeviceIds(userId);
|
2020-09-14 19:14:47 +05:30
|
|
|
// delete any devices that we know off but are not in the response anymore.
|
|
|
|
// important this happens before checking if the ed25519 key changed,
|
|
|
|
// otherwise we would end up deleting existing devices with changed keys.
|
|
|
|
for (const deviceId of knownDeviceIds) {
|
|
|
|
if (deviceIdentities.every(di => di.deviceId !== deviceId)) {
|
|
|
|
txn.deviceIdentities.remove(userId, deviceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all the device identities as we will have them in storage
|
|
|
|
const allDeviceIdentities = [];
|
|
|
|
const deviceIdentitiesToStore = [];
|
|
|
|
// filter out devices that have changed their ed25519 key since last time we queried them
|
|
|
|
deviceIdentities = await Promise.all(deviceIdentities.map(async deviceIdentity => {
|
|
|
|
if (knownDeviceIds.includes(deviceIdentity.deviceId)) {
|
|
|
|
const existingDevice = await txn.deviceIdentities.get(deviceIdentity.userId, deviceIdentity.deviceId);
|
|
|
|
if (existingDevice.ed25519Key !== deviceIdentity.ed25519Key) {
|
|
|
|
allDeviceIdentities.push(existingDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allDeviceIdentities.push(deviceIdentity);
|
|
|
|
deviceIdentitiesToStore.push(deviceIdentity);
|
|
|
|
}));
|
|
|
|
// store devices
|
|
|
|
for (const deviceIdentity of deviceIdentitiesToStore) {
|
|
|
|
txn.deviceIdentities.set(deviceIdentity);
|
|
|
|
}
|
|
|
|
// mark user identities as up to date
|
|
|
|
const identity = await txn.userIdentities.get(userId);
|
|
|
|
identity.deviceTrackingStatus = TRACKING_STATUS_UPTODATE;
|
|
|
|
txn.userIdentities.set(identity);
|
|
|
|
|
|
|
|
return allDeviceIdentities;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {Array<{userId, verifiedKeys: Array<DeviceSection>>}
|
|
|
|
*/
|
2021-02-23 23:52:59 +05:30
|
|
|
_filterVerifiedDeviceKeys(keyQueryDeviceKeysResponse, parentLog) {
|
2020-09-08 20:44:23 +05:30
|
|
|
const curve25519Keys = new Set();
|
2020-08-31 19:35:57 +05:30
|
|
|
const verifiedKeys = Object.entries(keyQueryDeviceKeysResponse).map(([userId, keysByDevice]) => {
|
|
|
|
const verifiedEntries = Object.entries(keysByDevice).filter(([deviceId, deviceKeys]) => {
|
2020-08-31 17:41:08 +05:30
|
|
|
const deviceIdOnKeys = deviceKeys["device_id"];
|
|
|
|
const userIdOnKeys = deviceKeys["user_id"];
|
|
|
|
if (userIdOnKeys !== userId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (deviceIdOnKeys !== deviceId) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-08 14:21:01 +05:30
|
|
|
const ed25519Key = deviceKeys.keys?.[`ed25519:${deviceId}`];
|
|
|
|
const curve25519Key = deviceKeys.keys?.[`curve25519:${deviceId}`];
|
|
|
|
if (typeof ed25519Key !== "string" || typeof curve25519Key !== "string") {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-08 20:44:23 +05:30
|
|
|
if (curve25519Keys.has(curve25519Key)) {
|
2021-02-23 23:52:59 +05:30
|
|
|
parentLog.log({
|
|
|
|
l: "ignore device with duplicate curve25519 key",
|
|
|
|
keys: deviceKeys
|
|
|
|
}, parentLog.level.Warn);
|
2020-09-08 20:44:23 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
curve25519Keys.add(curve25519Key);
|
2021-02-23 23:52:59 +05:30
|
|
|
const isValid = this._hasValidSignature(deviceKeys);
|
|
|
|
if (!isValid) {
|
|
|
|
parentLog.log({
|
|
|
|
l: "ignore device with invalid signature",
|
|
|
|
keys: deviceKeys
|
|
|
|
}, parentLog.level.Warn);
|
|
|
|
}
|
|
|
|
return isValid;
|
2020-08-31 17:41:08 +05:30
|
|
|
});
|
2020-08-31 19:35:57 +05:30
|
|
|
const verifiedKeys = verifiedEntries.map(([, deviceKeys]) => deviceKeys);
|
2020-08-31 17:41:08 +05:30
|
|
|
return {userId, verifiedKeys};
|
|
|
|
});
|
|
|
|
return verifiedKeys;
|
|
|
|
}
|
|
|
|
|
2020-09-02 21:07:48 +05:30
|
|
|
_hasValidSignature(deviceSection) {
|
2020-08-31 17:41:08 +05:30
|
|
|
const deviceId = deviceSection["device_id"];
|
|
|
|
const userId = deviceSection["user_id"];
|
2020-09-02 21:07:48 +05:30
|
|
|
const ed25519Key = deviceSection?.keys?.[`${SIGNATURE_ALGORITHM}:${deviceId}`];
|
|
|
|
return verifyEd25519Signature(this._olmUtil, userId, deviceId, ed25519Key, deviceSection);
|
2020-08-31 17:41:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gives all the device identities for a room that is already tracked.
|
|
|
|
* Assumes room is already tracked. Call `trackRoom` first if unsure.
|
|
|
|
* @param {String} roomId [description]
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2021-02-23 23:52:59 +05:30
|
|
|
async devicesForTrackedRoom(roomId, hsApi, log) {
|
2021-03-05 00:17:02 +05:30
|
|
|
const txn = await this._storage.readTxn([
|
2020-08-31 17:41:08 +05:30
|
|
|
this._storage.storeNames.roomMembers,
|
|
|
|
this._storage.storeNames.userIdentities,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// because we don't have multiEntry support in IE11, we get a set of userIds that is pretty close to what we
|
|
|
|
// need as a good first filter (given that non-join memberships will be in there). After fetching the identities,
|
|
|
|
// we check which ones have the roomId for the room we're looking at.
|
|
|
|
|
|
|
|
// So, this will also contain non-joined memberships
|
2020-08-31 19:36:31 +05:30
|
|
|
const userIds = await txn.roomMembers.getAllUserIds(roomId);
|
2020-09-08 17:54:48 +05:30
|
|
|
|
2021-02-23 23:52:59 +05:30
|
|
|
return await this._devicesForUserIds(roomId, userIds, txn, hsApi, log);
|
2020-09-08 17:54:48 +05:30
|
|
|
}
|
|
|
|
|
2021-02-23 23:52:59 +05:30
|
|
|
async devicesForRoomMembers(roomId, userIds, hsApi, log) {
|
2021-03-05 00:17:02 +05:30
|
|
|
const txn = await this._storage.readTxn([
|
2020-09-08 17:54:48 +05:30
|
|
|
this._storage.storeNames.userIdentities,
|
|
|
|
]);
|
2021-02-23 23:52:59 +05:30
|
|
|
return await this._devicesForUserIds(roomId, userIds, txn, hsApi, log);
|
2020-09-08 17:54:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} roomId [description]
|
|
|
|
* @param {Array<string>} userIds a set of user ids to try and find the identity for. Will be check to belong to roomId.
|
|
|
|
* @param {Transaction} userIdentityTxn to read the user identities
|
|
|
|
* @param {HomeServerApi} hsApi
|
|
|
|
* @return {Array<DeviceIdentity>}
|
|
|
|
*/
|
2021-02-23 23:52:59 +05:30
|
|
|
async _devicesForUserIds(roomId, userIds, userIdentityTxn, hsApi, log) {
|
2020-09-08 17:54:48 +05:30
|
|
|
const allMemberIdentities = await Promise.all(userIds.map(userId => userIdentityTxn.userIdentities.get(userId)));
|
|
|
|
const identities = allMemberIdentities.filter(identity => {
|
2020-08-31 17:41:08 +05:30
|
|
|
// identity will be missing for any userIds that don't have
|
|
|
|
// membership join in any of your encrypted rooms
|
|
|
|
return identity && identity.roomIds.includes(roomId);
|
|
|
|
});
|
|
|
|
const upToDateIdentities = identities.filter(i => i.deviceTrackingStatus === TRACKING_STATUS_UPTODATE);
|
|
|
|
const outdatedIdentities = identities.filter(i => i.deviceTrackingStatus === TRACKING_STATUS_OUTDATED);
|
2021-02-23 23:52:59 +05:30
|
|
|
log.set("uptodate", upToDateIdentities.length);
|
|
|
|
log.set("outdated", outdatedIdentities.length);
|
2020-08-31 17:41:08 +05:30
|
|
|
let queriedDevices;
|
|
|
|
if (outdatedIdentities.length) {
|
|
|
|
// TODO: ignore the race between /sync and /keys/query for now,
|
|
|
|
// where users could get marked as outdated or added/removed from the room while
|
|
|
|
// querying keys
|
2021-02-23 23:52:59 +05:30
|
|
|
queriedDevices = await this._queryKeys(outdatedIdentities.map(i => i.userId), hsApi, log);
|
2020-08-31 17:41:08 +05:30
|
|
|
}
|
|
|
|
|
2021-03-05 00:17:02 +05:30
|
|
|
const deviceTxn = await this._storage.readTxn([
|
2020-08-31 17:41:08 +05:30
|
|
|
this._storage.storeNames.deviceIdentities,
|
|
|
|
]);
|
|
|
|
const devicesPerUser = await Promise.all(upToDateIdentities.map(identity => {
|
|
|
|
return deviceTxn.deviceIdentities.getAllForUserId(identity.userId);
|
|
|
|
}));
|
|
|
|
let flattenedDevices = devicesPerUser.reduce((all, devicesForUser) => all.concat(devicesForUser), []);
|
|
|
|
if (queriedDevices && queriedDevices.length) {
|
|
|
|
flattenedDevices = flattenedDevices.concat(queriedDevices);
|
|
|
|
}
|
2020-09-08 18:54:36 +05:30
|
|
|
// filter out our own device
|
2020-09-03 18:58:03 +05:30
|
|
|
const devices = flattenedDevices.filter(device => {
|
2020-09-08 21:57:35 +05:30
|
|
|
const isOwnDevice = device.userId === this._ownUserId && device.deviceId === this._ownDeviceId;
|
|
|
|
return !isOwnDevice;
|
2020-09-03 18:58:03 +05:30
|
|
|
});
|
|
|
|
return devices;
|
2020-08-31 17:41:08 +05:30
|
|
|
}
|
2020-09-08 14:20:39 +05:30
|
|
|
|
|
|
|
async getDeviceByCurve25519Key(curve25519Key, txn) {
|
|
|
|
return await txn.deviceIdentities.getByCurve25519Key(curve25519Key);
|
|
|
|
}
|
2020-08-31 17:41:08 +05:30
|
|
|
}
|