From 80cdbe1058c15c2dff34f10e4657fbeb06e76dde Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:11:04 +0100 Subject: [PATCH] Element-R: implement `userHasCrossSigningKeys` (#3488) --- spec/unit/rust-crypto/rust-crypto.spec.ts | 32 ++++++++++++++++++++++- src/rust-crypto/rust-crypto.ts | 7 +++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index 1e617c22d11..85c18af1a40 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -26,9 +26,10 @@ import { IHttpOpts, IToDeviceEvent, MatrixClient, MatrixHttpApi } from "../../.. import { mkEvent } from "../../test-utils/test-utils"; import { CryptoBackend } from "../../../src/common-crypto/CryptoBackend"; import { IEventDecryptionResult } from "../../../src/@types/crypto"; -import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor"; +import { OutgoingRequest, OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor"; import { ServerSideSecretStorage } from "../../../src/secret-storage"; import { ImportRoomKeysOpts } from "../../../src/crypto-api"; +import * as testData from "../../test-utils/test-data"; afterEach(() => { // reset fake-indexeddb after each test, to make sure we don't leak connections @@ -357,6 +358,35 @@ describe("RustCrypto", () => { }); }); + describe("userHasCrossSigningKeys", () => { + let rustCrypto: RustCrypto; + + beforeEach(async () => { + rustCrypto = await makeTestRustCrypto(undefined, testData.TEST_USER_ID); + }); + + it("returns false if there is no cross-signing identity", async () => { + await expect(rustCrypto.userHasCrossSigningKeys()).resolves.toBe(false); + }); + + it("returns true if OlmMachine has a cross-signing identity", async () => { + // @ts-ignore private field + const olmMachine = rustCrypto.olmMachine; + + const outgoingRequests: OutgoingRequest[] = await olmMachine.outgoingRequests(); + // pick out the KeysQueryRequest, and respond to it with the cross-signing keys + const req = outgoingRequests.find((r) => r instanceof KeysQueryRequest)!; + await olmMachine.markRequestAsSent( + req.id!, + req.type, + JSON.stringify(testData.SIGNED_CROSS_SIGNING_KEYS_DATA), + ); + + // ... and we should now have cross-signing keys. + await expect(rustCrypto.userHasCrossSigningKeys()).resolves.toBe(true); + }); + }); + describe("createRecoveryKeyFromPassphrase", () => { let rustCrypto: RustCrypto; diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 84a21c548d3..0405ed42954 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -188,9 +188,12 @@ export class RustCrypto implements CryptoBackend { public globalBlacklistUnverifiedDevices = false; + /** + * Implementation of {@link CryptoApi.userHasCrossSigningKeys}. + */ public async userHasCrossSigningKeys(): Promise { - // TODO - return false; + const userIdentity = await this.olmMachine.getIdentity(new RustSdkCryptoJs.UserId(this.userId)); + return userIdentity !== undefined; } public prepareToEncrypt(room: Room): void {