From 10c92c56f59cb5193f235eb128816dcb4373fd95 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 28 Mar 2023 12:58:23 +0530 Subject: [PATCH] Fix tests and code to use new data structure --- .../verification/SAS/SASVerification.ts | 20 ++++++++++++++----- .../verification/SAS/channel/Channel.ts | 4 ++-- .../verification/SAS/channel/MockChannel.ts | 8 +++++--- .../verification/SAS/stages/SendMacStage.ts | 8 +++++++- .../verification/SAS/stages/VerifyMacStage.ts | 11 ++++++++-- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/matrix/verification/SAS/SASVerification.ts b/src/matrix/verification/SAS/SASVerification.ts index 551d196f..5850ba5d 100644 --- a/src/matrix/verification/SAS/SASVerification.ts +++ b/src/matrix/verification/SAS/SASVerification.ts @@ -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, diff --git a/src/matrix/verification/SAS/channel/Channel.ts b/src/matrix/verification/SAS/channel/Channel.ts index 8b45613d..6492a6ae 100644 --- a/src/matrix/verification/SAS/channel/Channel.ts +++ b/src/matrix/verification/SAS/channel/Channel.ts @@ -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 diff --git a/src/matrix/verification/SAS/channel/MockChannel.ts b/src/matrix/verification/SAS/channel/MockChannel.ts index 6f707790..50197ba4 100644 --- a/src/matrix/verification/SAS/channel/MockChannel.ts +++ b/src/matrix/verification/SAS/channel/MockChannel.ts @@ -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 { - console.log("MockChannel.cancelVerification()"); this.isCancelled = true; } diff --git a/src/matrix/verification/SAS/stages/SendMacStage.ts b/src/matrix/verification/SAS/stages/SendMacStage.ts index b8b39780..14384d3a 100644 --- a/src/matrix/verification/SAS/stages/SendMacStage.ts +++ b/src/matrix/verification/SAS/stages/SendMacStage.ts @@ -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); diff --git a/src/matrix/verification/SAS/stages/VerifyMacStage.ts b/src/matrix/verification/SAS/stages/VerifyMacStage.ts index e19f695a..40e908c6 100644 --- a/src/matrix/verification/SAS/stages/VerifyMacStage.ts +++ b/src/matrix/verification/SAS/stages/VerifyMacStage.ts @@ -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 }