diff --git a/spec/unit/rust-crypto.spec.ts b/spec/unit/rust-crypto.spec.ts index 69dde7123..50e2c856c 100644 --- a/spec/unit/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto.spec.ts @@ -32,6 +32,9 @@ import { RustCrypto } from "../../src/rust-crypto/rust-crypto"; import { initRustCrypto } from "../../src/rust-crypto"; import { HttpApiEvent, HttpApiEventHandlerMap, IToDeviceEvent, MatrixClient, MatrixHttpApi } from "../../src"; import { TypedEventEmitter } from "../../src/models/typed-event-emitter"; +import { mkEvent } from "../test-utils/test-utils"; +import { CryptoBackend } from "../../src/common-crypto/CryptoBackend"; +import { IEventDecryptionResult } from "../../src/@types/crypto"; afterEach(() => { // reset fake-indexeddb after each test, to make sure we don't leak connections @@ -245,4 +248,33 @@ describe("RustCrypto", () => { expect(outgoingRequestQueue.length).toEqual(2); }); }); + + describe(".getEventEncryptionInfo", () => { + let rustCrypto: RustCrypto; + + beforeEach(async () => { + const mockHttpApi = {} as MatrixClient["http"]; + rustCrypto = (await initRustCrypto(mockHttpApi, TEST_USER, TEST_DEVICE_ID)) as RustCrypto; + }); + + it("should handle unencrypted events", () => { + const event = mkEvent({ event: true, type: "m.room.message", content: { body: "xyz" } }); + const res = rustCrypto.getEventEncryptionInfo(event); + expect(res.encrypted).toBeFalsy(); + }); + + it("should handle encrypted events", async () => { + const event = mkEvent({ event: true, type: "m.room.encrypted", content: { algorithm: "fake_alg" } }); + const mockCryptoBackend = { + decryptEvent: () => + ({ + senderCurve25519Key: "1234", + } as IEventDecryptionResult), + } as unknown as CryptoBackend; + await event.attemptDecryption(mockCryptoBackend); + + const res = rustCrypto.getEventEncryptionInfo(event); + expect(res.encrypted).toBeTruthy(); + }); + }); }); diff --git a/src/client.ts b/src/client.ts index c64795bd8..2a5797771 100644 --- a/src/client.ts +++ b/src/client.ts @@ -2507,10 +2507,10 @@ export class MatrixClient extends TypedEventEmitter; + /** + * Get the verification level for a given user + * + * TODO: define this better + * + * @param userId - user to be checked + */ + checkUserTrust(userId: string): UserTrustLevel; + + /** + * Get the verification level for a given device + * + * TODO: define this better + * + * @param userId - user to be checked + * @param deviceId - device to be checked + */ + checkDeviceTrust(userId: string, deviceId: string): DeviceTrustLevel; + /** * Decrypt a received event * @@ -62,6 +83,13 @@ export interface CryptoBackend extends SyncCryptoCallbacks { */ decryptEvent(event: MatrixEvent): Promise; + /** + * Get information about the encryption of an event + * + * @param event - event to be checked + */ + getEventEncryptionInfo(event: MatrixEvent): IEncryptedEventInfo; + /** * Get a list containing all of the room keys * diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index b48de4697..6023288b6 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -25,11 +25,13 @@ import { import type { IEventDecryptionResult, IMegolmSessionData } from "../@types/crypto"; import type { IToDeviceEvent } from "../sync-accumulator"; +import type { IEncryptedEventInfo } from "../crypto/api"; import { MatrixEvent } from "../models/event"; import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend"; import { logger } from "../logger"; import { IHttpOpts, MatrixHttpApi, Method } from "../http-api"; import { QueryDict } from "../utils"; +import { DeviceTrustLevel, UserTrustLevel } from "../crypto/CrossSigning"; /** * Common interface for all the request types returned by `OlmMachine.outgoingRequests`. @@ -78,6 +80,24 @@ export class RustCrypto implements CryptoBackend { throw new Error("not implemented"); } + public getEventEncryptionInfo(event: MatrixEvent): IEncryptedEventInfo { + // TODO: make this work properly. Or better, replace it. + + const ret: Partial = {}; + + ret.senderKey = event.getSenderKey() ?? undefined; + ret.algorithm = event.getWireContent().algorithm; + + if (!ret.senderKey || !ret.algorithm) { + ret.encrypted = false; + return ret as IEncryptedEventInfo; + } + ret.encrypted = true; + ret.authenticated = true; + ret.mismatchedSender = true; + return ret as IEncryptedEventInfo; + } + public async userHasCrossSigningKeys(): Promise { // TODO return false; @@ -88,6 +108,16 @@ export class RustCrypto implements CryptoBackend { return []; } + public checkUserTrust(userId: string): UserTrustLevel { + // TODO + return new UserTrustLevel(false, false, false); + } + + public checkDeviceTrust(userId: string, deviceId: string): DeviceTrustLevel { + // TODO + return new DeviceTrustLevel(false, false, false, false); + } + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // SyncCryptoCallbacks implementation