Fix tests and code to use new data structure

This commit is contained in:
RMidhunSuresh 2023-03-28 12:58:23 +05:30
parent 3e7a4d95c3
commit 10c92c56f5
5 changed files with 38 additions and 13 deletions

View File

@ -145,16 +145,25 @@ export function tests() {
},
};
const deviceTracker = {
getCrossSigningKeysForUser: (userId, _hsApi, _) => {
getCrossSigningKeyForUser: (userId, __, _hsApi, _) => {
let masterKey =
userId === ourUserId
? "5HIrEawRiiQioViNfezPDWfPWH2pdaw3pbQNHEVN2jM"
: "Ot8Y58PueQ7hJVpYWAJkg2qaREJAY/UhGZYOrsd52oo";
return { masterKey };
},
deviceForId: (_userId, _deviceId, _hsApi, _log) => {
return {
ed25519Key: "D8w9mrokGdEZPdPgrU0kQkYi4vZyzKEBfvGyZsGK7+Q",
user_id: userId,
usage: ["master"],
keys: {
[`ed25519:${masterKey}`]: masterKey,
}
};
},
deviceForId: (_userId, deviceId, _hsApi, _log) => {
return {
device_id: deviceId,
keys: {
[`ed25519:${deviceId}`]: "D8w9mrokGdEZPdPgrU0kQkYi4vZyzKEBfvGyZsGK7+Q",
}
};
},
};
@ -177,6 +186,7 @@ export function tests() {
channel,
clock,
hsApi,
// @ts-ignore
deviceTracker,
e2eeAccount,
olm,

View File

@ -211,13 +211,13 @@ export class ToDeviceChannel extends Disposables implements IChannel {
this.otherUserDeviceId = fromDevice;
// We need to send cancel messages to all other devices
const devices = await this.deviceTracker.devicesForUsers([this.otherUserId], this.hsApi, log);
const otherDevices = devices.filter(device => device.deviceId !== fromDevice && device.deviceId !== this.ourDeviceId);
const otherDevices = devices.filter(device => device.device_id !== fromDevice && device.device_id !== this.ourDeviceId);
const cancelMessage = {
code: CancelReason.OtherDeviceAccepted,
reason: messageFromErrorType[CancelReason.OtherDeviceAccepted],
transaction_id: this.id,
};
const deviceMessages = otherDevices.reduce((acc, device) => { acc[device.deviceId] = cancelMessage; return acc; }, {});
const deviceMessages = otherDevices.reduce((acc, device) => { acc[device.device_id] = cancelMessage; return acc; }, {});
const payload = {
messages: {
[this.otherUserId]: deviceMessages

View File

@ -3,6 +3,8 @@ import {createCalculateMAC} from "../mac";
import {VerificationCancelledError} from "../VerificationCancelledError";
import {IChannel} from "./Channel";
import {CancelReason, VerificationEventType} from "./types";
import {getKeyEd25519Key} from "../../CrossSigning";
import {getDeviceEd25519Key} from "../../../e2ee/common";
import anotherjson from "another-json";
interface ITestChannel extends IChannel {
@ -96,10 +98,11 @@ export class MockChannel implements ITestChannel {
const deviceId = keyId.split(":", 2)[1];
const device = await this.deviceTracker.deviceForId(this.otherUserDeviceId, deviceId);
if (device) {
macContent.mac[keyId] = calculateMac(device.ed25519Key, baseInfo + keyId);
macContent.mac[keyId] = calculateMac(getDeviceEd25519Key(device), baseInfo + keyId);
}
else {
const {masterKey} = await this.deviceTracker.getCrossSigningKeysForUser(this.otherUserId);
const key = await this.deviceTracker.getCrossSigningKeyForUser(this.otherUserId);
const masterKey = getKeyEd25519Key(key)!;
macContent.mac[keyId] = calculateMac(masterKey, baseInfo + keyId);
}
}
@ -112,7 +115,6 @@ export class MockChannel implements ITestChannel {
}
async cancelVerification(_: CancelReason): Promise<void> {
console.log("MockChannel.cancelVerification()");
this.isCancelled = true;
}

View File

@ -18,6 +18,7 @@ import {ILogItem} from "../../../../logging/types";
import {VerificationEventType} from "../channel/types";
import {createCalculateMAC} from "../mac";
import {VerifyMacStage} from "./VerifyMacStage";
import {getKeyEd25519Key, KeyUsage} from "../../CrossSigning";
export class SendMacStage extends BaseSASVerificationStage {
async completeStage() {
@ -47,7 +48,12 @@ export class SendMacStage extends BaseSASVerificationStage {
mac[deviceKeyId] = calculateMAC(deviceKeys.keys[deviceKeyId], baseInfo + deviceKeyId);
keyList.push(deviceKeyId);
const {masterKey: crossSigningKey} = await this.deviceTracker.getCrossSigningKeysForUser(this.ourUserId, this.hsApi, log);
const key = await this.deviceTracker.getCrossSigningKeyForUser(this.ourUserId, KeyUsage.Master, this.hsApi, log);
if (!key) {
log.log({ l: "Fetching msk failed", userId: this.ourUserId });
throw new Error("Fetching MSK for user failed!");
}
const crossSigningKey = getKeyEd25519Key(key);
if (crossSigningKey) {
const crossSigningKeyId = `ed25519:${crossSigningKey}`;
mac[crossSigningKeyId] = calculateMAC(crossSigningKey, baseInfo + crossSigningKeyId);

View File

@ -18,6 +18,8 @@ import {ILogItem} from "../../../../logging/types";
import {CancelReason, VerificationEventType} from "../channel/types";
import {createCalculateMAC} from "../mac";
import {SendDoneStage} from "./SendDoneStage";
import {KeyUsage, getKeyEd25519Key} from "../../CrossSigning";
import {getDeviceEd25519Key} from "../../../e2ee/common";
export type KeyVerifier = (keyId: string, device: any, keyInfo: string) => void;
@ -66,11 +68,16 @@ export class VerifyMacStage extends BaseSASVerificationStage {
const deviceIdOrMSK = keyId.split(":", 2)[1];
const device = await this.deviceTracker.deviceForId(userId, deviceIdOrMSK, this.hsApi, log);
if (device) {
verifier(keyId, device.ed25519Key, keyInfo);
verifier(keyId, getDeviceEd25519Key(device), keyInfo);
// todo: mark device as verified here
} else {
// If we were not able to find the device, then deviceIdOrMSK is actually the MSK!
const {masterKey} = await this.deviceTracker.getCrossSigningKeysForUser(userId, this.hsApi, log);
const key = await this.deviceTracker.getCrossSigningKeyForUser(userId, KeyUsage.Master, this.hsApi, log);
if (!key) {
log.log({ l: "Fetching msk failed", userId });
throw new Error("Fetching MSK for user failed!");
}
const masterKey = getKeyEd25519Key(key);
verifier(keyId, masterKey, keyInfo);
// todo: mark user as verified here
}