Skip to content

Commit

Permalink
Get rid of CrossSigningInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Aug 23, 2023
1 parent 0a69f6e commit 36a4f64
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 294 deletions.
44 changes: 27 additions & 17 deletions spec/integ/crypto/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ import type { IDeviceKeys } from "../../../src/@types/crypto";
import * as testUtils from "../../test-utils/test-utils";
import { CRYPTO_BACKENDS, getSyncResponse, InitCrypto, syncPromise } from "../../test-utils/test-utils";
import {
BOB_MASTER_CROSS_SIGNING_PUBLIC_KEY_BASE64,
BOB_SELF_CROSS_SIGNING_PUBLIC_KEY_BASE64,
BOB_SIGNED_CROSS_SIGNING_KEYS_DATA,
BOB_SIGNED_TEST_DEVICE_DATA,
BOB_TEST_USER_ID,
BOB_USER_CROSS_SIGNING_PUBLIC_KEY_BASE64,
SIGNED_CROSS_SIGNING_KEYS_DATA,
SIGNED_TEST_DEVICE_DATA,
TEST_ROOM_ID,
TEST_ROOM_ID as ROOM_ID,
TEST_USER_ID,
Expand Down Expand Up @@ -64,7 +63,7 @@ import { downloadDeviceToJsDevice } from "../../../src/rust-crypto/device-conver
import { flushPromises } from "../../test-utils/flushPromises";
import { mockInitialApiRequests, mockSetupCrossSigningRequests } from "../../test-utils/mockEndpoints";
import { AddSecretStorageKeyOpts, SECRET_STORAGE_ALGORITHM_V1_AES } from "../../../src/secret-storage";
import { CrossSigningKey, CryptoCallbacks } from "../../../src/crypto-api";
import { CryptoCallbacks } from "../../../src/crypto-api";
import { E2EKeyResponder } from "../../test-utils/E2EKeyResponder";

afterEach(() => {
Expand Down Expand Up @@ -2577,12 +2576,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
);
});

describe("Get cross signing information for a user", () => {
describe("Check if the cross signing keys are available for a user", () => {
beforeEach(async () => {
// anything that we don't have a specific matcher for silently returns a 404
fetchMock.catch(404);

keyResponder = new E2EKeyResponder(aliceClient.getHomeserverUrl());
keyResponder.addCrossSigningData(SIGNED_CROSS_SIGNING_KEYS_DATA);
keyResponder.addDeviceKeys(SIGNED_TEST_DEVICE_DATA);
keyResponder.addKeyReceiver(BOB_TEST_USER_ID, keyReceiver);
keyResponder.addCrossSigningData(BOB_SIGNED_CROSS_SIGNING_KEYS_DATA);
keyResponder.addDeviceKeys(BOB_SIGNED_TEST_DEVICE_DATA);
Expand All @@ -2591,22 +2592,31 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
await startClientAndAwaitFirstSync();
});

it("Get Bob cross signing info", async () => {
it("Cross signing keys are available for untracked user with cross signing keys on the homeserver", async () => {
// Needed for old crypto, download and cache locally the cross signing keys of Bob
await aliceClient.getCrypto()?.getUserDeviceInfo([BOB_TEST_USER_ID], true);

const crossSigningInfo = await aliceClient.getCrypto()!.getCrossSigningKeysForUser(BOB_TEST_USER_ID);
expect(crossSigningInfo).not.toBeNull();
const hasCrossSigningKeysForUser = await aliceClient
.getCrypto()!
.hasCrossSigningKeysForUser(BOB_TEST_USER_ID, true);
expect(hasCrossSigningKeysForUser).toBe(true);
});

expect(crossSigningInfo?.getPublicKey(CrossSigningKey.Master)).toStrictEqual(
BOB_MASTER_CROSS_SIGNING_PUBLIC_KEY_BASE64,
);
expect(crossSigningInfo?.getPublicKey(CrossSigningKey.SelfSigning)).toStrictEqual(
BOB_SELF_CROSS_SIGNING_PUBLIC_KEY_BASE64,
);
expect(crossSigningInfo?.getPublicKey(CrossSigningKey.UserSigning)).toStrictEqual(
BOB_USER_CROSS_SIGNING_PUBLIC_KEY_BASE64,
);
it("Cross signing keys are available for tracked users", async () => {
// Process Alice keys, old crypto has a sleep(5ms) during the process
await jest.advanceTimersByTimeAsync(5);
await flushPromises();

// Alice is the local user, it should be tracked !
const hasCrossSigningKeysForUser = await aliceClient.getCrypto()!.hasCrossSigningKeysForUser(TEST_USER_ID);
expect(hasCrossSigningKeysForUser).toBe(true);
});

it("Cross signing keys are not available for unknown user", async () => {
const hasCrossSigningKeysForUser = await aliceClient
.getCrypto()!
.hasCrossSigningKeysForUser("@unknown:xyz");
expect(hasCrossSigningKeysForUser).toBe(false);
});
});
});
114 changes: 0 additions & 114 deletions spec/unit/rust-crypto/CrossSigningInfo.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @param userId - the user ID to get the cross-signing info for.
*
* @returns the cross signing information for the user.
* @deprecated Prefer {@link CryptoApi#getCrossSigningKeysForUser}
* @deprecated Prefer {@link CryptoApi#hasCrossSigningKeysForUser}
*/
public getStoredCrossSigningForUser(userId: string): CrossSigningInfo | null {
if (!this.cryptoBackend) {
Expand Down
2 changes: 1 addition & 1 deletion src/common-crypto/CryptoBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export interface CryptoBackend extends SyncCryptoCallbacks, CryptoApi {
*
* @returns the cross signing information for the user.
*
* @deprecated Prefer {@link CryptoApi#getCrossSigningKeysForUser}
* @deprecated Prefer {@link CryptoApi#hasCrossSigningKeysForUser}
*/
getStoredCrossSigningForUser(userId: string): CrossSigningInfo | null;

Expand Down
13 changes: 8 additions & 5 deletions src/crypto-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { AddSecretStorageKeyOpts, SecretStorageCallbacks, SecretStorageKeyDescri
import { VerificationRequest } from "./crypto-api/verification";
import { BackupTrustInfo, KeyBackupCheck, KeyBackupInfo } from "./crypto-api/keybackup";
import { ISignatures } from "./@types/signed";
import { CrossSigningInfo } from "./crypto-api/CrossSigningInfo";

/**
* Public interface to the cryptography parts of the js-sdk
Expand Down Expand Up @@ -179,13 +178,17 @@ export interface CryptoApi {
getCrossSigningKeyId(type?: CrossSigningKey): Promise<string | null>;

/**
* Get the cross signing information for a given user.
* Check if the cross signing keys for the user are available.
*
* @param userId - the user ID to get the cross-signing info for.
* If the user is not tracked locally and downloadUncached is set at true,
* a `/keys/query` request is made to the server to retrieve the cross singing keys.
*
* @returns the cross signing information for the user.
* @param userId - the user ID to check
* @param downloadUncached - If true, download the cross signing keys.
*
* @returns true if the cross signing keys are available.
*/
getCrossSigningKeysForUser(userId: string): Promise<CrossSigningInfo | null>;
hasCrossSigningKeysForUser(userId: string, downloadUncached?: boolean): Promise<boolean>;

/**
* Bootstrap cross-signing by creating keys if needed.
Expand Down
31 changes: 0 additions & 31 deletions src/crypto-api/CrossSigningInfo.ts

This file was deleted.

16 changes: 1 addition & 15 deletions src/crypto/CrossSigning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { ISignatures } from "../@types/signed";
import { CryptoStore, SecretStorePrivateKeys } from "./store/base";
import { ServerSideSecretStorage, SecretStorageKeyDescription } from "../secret-storage";
import { DeviceVerificationStatus } from "../crypto-api";
import { CrossSigningInfo as NewCrossSigningInfo } from "../crypto-api/CrossSigningInfo";

const KEY_REQUEST_TIMEOUT_MS = 1000 * 60;

Expand All @@ -53,7 +52,7 @@ export interface ICrossSigningInfo {
crossSigningVerifiedBefore: boolean;
}

export class CrossSigningInfo implements NewCrossSigningInfo {
export class CrossSigningInfo {
public keys: Record<string, ICrossSigningKey> = {};
public firstUse = true;
// This tracks whether we've ever verified this user with any identity.
Expand Down Expand Up @@ -273,19 +272,6 @@ export class CrossSigningInfo implements NewCrossSigningInfo {
return publicKeyFromKeyInfo(keyInfo);
}

/**
* Get the public key of the user. This can also be used to test for
* the existence of a given key type.
*
* @param type - The type of key to get the ID of. One of "master",
* "self_signing", or "user_signing". Defaults to "master".
*
* @returns the public key
*/
public getPublicKey(type = "master"): string | null {
return this.getId(type);
}

/**
* Create new cross-signing keys for the given key types. The public keys
* will be held in this class, while the private keys are passed off to the
Expand Down
12 changes: 5 additions & 7 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1504,22 +1504,20 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
* @param userId - the user ID to get the cross-signing info for.
*
* @returns the cross signing information for the user.
*
* @deprecated Use {@link CryptoApi.getCrossSigningKeysForUser}.
*/
public getStoredCrossSigningForUser(userId: string): CrossSigningInfo | null {
return this.deviceList.getStoredCrossSigningForUser(userId);
}

/**
* Get the cross signing information for a given user.
* Check if we have locally the cross signing keys for a given user
*
* @param userId - the user ID to get the cross-signing info for.
* @param userId - the user ID to check.
*
* @returns the cross signing information for the user.
* @returns True if we have the cross signing keys.
*/
public getCrossSigningKeysForUser(userId: string): Promise<CrossSigningInfo | null> {
return Promise.resolve(this.getStoredCrossSigningForUser(userId));
public hasCrossSigningKeysForUser(userId: string): Promise<boolean> {
return Promise.resolve(Boolean(this.getStoredCrossSigningForUser(userId)?.getId()));
}

/**
Expand Down
Loading

0 comments on commit 36a4f64

Please sign in to comment.