From 772d91f9241cf4da81f6ea9e55190b885ebdeb53 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 16 Feb 2023 11:27:43 +0530 Subject: [PATCH] WIP --- src/domain/session/room/RoomViewModel.js | 10 ++++ .../verification/SAS/SASVerification.ts | 33 +++++++++++ .../SAS/stages/BaseSASVerificationStage.ts | 35 ++++++++++++ .../SAS/stages/StartVerificationStage.ts | 55 +++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 src/matrix/verification/SAS/SASVerification.ts create mode 100644 src/matrix/verification/SAS/stages/BaseSASVerificationStage.ts create mode 100644 src/matrix/verification/SAS/stages/StartVerificationStage.ts diff --git a/src/domain/session/room/RoomViewModel.js b/src/domain/session/room/RoomViewModel.js index 31608a62..2130bc7f 100644 --- a/src/domain/session/room/RoomViewModel.js +++ b/src/domain/session/room/RoomViewModel.js @@ -28,6 +28,7 @@ import {LocalMedia} from "../../../matrix/calls/LocalMedia"; // this is a breaking SDK change though to make this option mandatory import {tileClassForEntry as defaultTileClassForEntry} from "./timeline/tiles/index"; import {joinRoom} from "../../../matrix/room/joinRoom"; +import {SASVerification} from "../../../matrix/verification/SAS/SASVerification"; export class RoomViewModel extends ErrorReportViewModel { constructor(options) { @@ -49,6 +50,15 @@ export class RoomViewModel extends ErrorReportViewModel { this._setupCallViewModel(); } + async _startCrossSigning(otherUserId) { + await this.logAndCatch("startCrossSigning", async log => { + const session = this.getOption("session"); + const { userId, deviceId } = session; + const sas = new SASVerification(this.room, { userId, deviceId }, otherUserId, log); + await sas.start(); + }); + } + _setupCallViewModel() { if (!this.features.calls) { return; diff --git a/src/matrix/verification/SAS/SASVerification.ts b/src/matrix/verification/SAS/SASVerification.ts new file mode 100644 index 00000000..e2882fd4 --- /dev/null +++ b/src/matrix/verification/SAS/SASVerification.ts @@ -0,0 +1,33 @@ +/* +Copyright 2023 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 {StartVerificationStage} from "./stages/StartVerificationStage"; +import type {ILogItem} from "../../../logging/types"; +import type {Room} from "../../room/Room.js"; +import type {BaseSASVerificationStage, UserData} from "./stages/BaseSASVerificationStage"; + +export class SASVerification { + private stages: BaseSASVerificationStage[] = []; + + constructor(private room: Room, private ourUser: UserData, otherUserId: string, log: ILogItem) { + this.stages.push(new StartVerificationStage(room, ourUser, otherUserId, log)); + } + + async start() { + for (const stage of this.stages) { + await stage.completeStage(); + } + } +} diff --git a/src/matrix/verification/SAS/stages/BaseSASVerificationStage.ts b/src/matrix/verification/SAS/stages/BaseSASVerificationStage.ts new file mode 100644 index 00000000..9d86d08b --- /dev/null +++ b/src/matrix/verification/SAS/stages/BaseSASVerificationStage.ts @@ -0,0 +1,35 @@ +/* +Copyright 2023 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 type {ILogItem} from "../../../../lib.js"; +import type {Room} from "../../../room/Room.js"; + +export type UserData = { + userId: string; + deviceId: string; +} + +export abstract class BaseSASVerificationStage { + constructor(protected room: Room, + protected ourUser: UserData, + protected otherUserId: string, + protected log: ILogItem) { + + } + + abstract get type(): string; + abstract completeStage(): boolean | Promise; + abstract get nextStage(): BaseSASVerificationStage; +} diff --git a/src/matrix/verification/SAS/stages/StartVerificationStage.ts b/src/matrix/verification/SAS/stages/StartVerificationStage.ts new file mode 100644 index 00000000..1f92afc5 --- /dev/null +++ b/src/matrix/verification/SAS/stages/StartVerificationStage.ts @@ -0,0 +1,55 @@ +/* +Copyright 2023 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 {BaseSASVerificationStage} from "./BaseSASVerificationStage"; + +// From element-web +// type KeyAgreement = "curve25519-hkdf-sha256" | "curve25519"; +// type MacMethod = "hkdf-hmac-sha256.v2" | "org.matrix.msc3783.hkdf-hmac-sha256" | "hkdf-hmac-sha256" | "hmac-sha256"; + +// const KEY_AGREEMENT_LIST: KeyAgreement[] = ["curve25519-hkdf-sha256", "curve25519"]; +// const HASHES_LIST = ["sha256"]; +// const MAC_LIST: MacMethod[] = [ +// "hkdf-hmac-sha256.v2", +// "org.matrix.msc3783.hkdf-hmac-sha256", +// "hkdf-hmac-sha256", +// "hmac-sha256", +// ]; + +// const SAS_LIST = Object.keys(sasGenerators); +export class StartVerificationStage extends BaseSASVerificationStage { + + async completeStage() { + await this.log.wrap("StartVerificationStage.completeStage", async (log) => { + const content = { + "body": `${this.ourUser.userId} is requesting to verify your device, but your client does not support verification, so you may need to use a different verification method.`, + "from_device": this.ourUser.deviceId, + "methods": ["m.sas.v1"], + "msgtype": "m.key.verification.request", + "to": this.otherUserId, + }; + await this.room.sendEvent("m.room.message", content, null, log); + }); + return true; + } + + get type() { + return "m.key.verification.request"; + } + + get nextStage(): BaseSASVerificationStage { + return this; + } +}