Skip to content

Commit

Permalink
Move to CryptoApi
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Jul 26, 2023
1 parent bba218c commit 5435a30
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 37 deletions.
15 changes: 9 additions & 6 deletions spec/integ/crypto/megolm-backup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { E2EKeyResponder } from "../../test-utils/E2EKeyResponder";
import { mockInitialApiRequests } from "../../test-utils/mockEndpoints";
import { awaitDecryption, CRYPTO_BACKENDS, InitCrypto, syncPromise } from "../../test-utils/test-utils";
import * as testData from "../../test-utils/test-data";
import { SecureKeyBackup } from "../../../src/common-crypto/SecureKeyBackup";

const ROOM_ID = "!ROOM:ID";

Expand Down Expand Up @@ -173,16 +174,18 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm-keys backup (%s)", (backe

aliceClient = await initTestClient();
const aliceCrypto = aliceClient.getCrypto()!;
// @ts-ignore backupManager is an internal property
const aliceBackupManager: SecureKeyBackup = aliceCrypto.backupManager;
await aliceClient.startClient();

// tell Alice to trust the dummy device that signed the backup
await waitForDeviceList();
await aliceCrypto.setDeviceVerified(testData.TEST_USER_ID, testData.TEST_DEVICE_ID);
await aliceCrypto.backupManager.checkAndStart();
await aliceBackupManager.checkAndStart();

// At this point there is no backup
let backupStatus: string | null;
backupStatus = await aliceCrypto.backupManager.getActiveBackupVersion();
backupStatus = await aliceCrypto.getActiveSessionBackupVersion();
expect(backupStatus).toBeNull();

// Serve a backup with no trusted signature
Expand All @@ -192,11 +195,11 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm-keys backup (%s)", (backe
overwriteRoutes: true,
});

const checked = await aliceCrypto.backupManager.checkAndStart();
const checked = await aliceBackupManager.checkAndStart();
expect(checked?.backupInfo?.version).toStrictEqual(unsignedBackup.version);
expect(checked?.trustInfo?.usable).toBeFalsy();

backupStatus = await aliceCrypto.backupManager.getActiveBackupVersion();
backupStatus = await aliceCrypto.getActiveSessionBackupVersion();
expect(backupStatus).toBeNull();

// Add a valid signature to the backup
Expand All @@ -213,12 +216,12 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm-keys backup (%s)", (backe
});
});

const validCheck = await aliceCrypto.backupManager.checkAndStart();
const validCheck = await aliceBackupManager.checkAndStart();
expect(validCheck?.trustInfo?.usable).toStrictEqual(true);

await backupPromise;

backupStatus = await aliceCrypto.backupManager.getActiveBackupVersion();
backupStatus = await aliceCrypto.getActiveSessionBackupVersion();
expect(backupStatus).toStrictEqual(testData.SIGNED_BACKUP_DATA.version);
});

Expand Down
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3300,10 +3300,10 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* the server, otherwise false. If we haven't completed a successful check
* of key backup status yet, returns null.
*
* @deprecated Prefer direct access to {@link Crypto.SecureBackup.getKeyBackupStatus}:
* @deprecated Prefer direct access to {@link CryptoApi.getActiveSessionBackupVersion}:
*
* ```javascript
* let enabled = (await client.getCrypto().backupManager.getKeyBackupStatus()) !== null;
* let enabled = (await client.getCrypto().getActiveSessionBackupVersion()) !== null;
* ```
*/
public getKeyBackupEnabled(): boolean | null {
Expand Down Expand Up @@ -3447,7 +3447,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
// If we're currently backing up to this backup... stop.
// (We start using it automatically in createKeyBackupVersion
// so this is symmetrical).
if ((await this.getCrypto()?.backupManager.getActiveBackupVersion()) === version) {
if ((await this.getCrypto()?.getActiveSessionBackupVersion()) === version) {
this.crypto.backupManager.disableKeyBackup();
}

Expand Down
7 changes: 0 additions & 7 deletions src/common-crypto/SecureKeyBackup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ export interface SecureKeyBackup {
* (or lack thereof).
*/
checkAndStart(): Promise<KeyBackupCheck | null>;

/**
* Get the current status of key backup.
*
* @returns If automatic key backups are enabled, the `version` of the active backup. Otherwise, `null`.
*/
getActiveBackupVersion(): Promise<string | null>;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/crypto-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ export interface CryptoApi {
* @param key - the backup decryption key
*/
storeSessionBackupPrivateKey(key: Uint8Array): Promise<void>;

/**
* Get the current status of key backup.
*
* @returns If automatic key backups are enabled, the `version` of the active backup. Otherwise, `null`.
*/
getActiveSessionBackupVersion(): Promise<string | null>;
}

/**
Expand Down
18 changes: 0 additions & 18 deletions src/crypto/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export class BackupManager implements SecureKeyBackup {
this.clientRunning = false;
}

/** @deprecated use {@link getActiveBackupVersion} instead */
public get version(): string | undefined {
return this.backupInfo && this.backupInfo.version;
}
Expand Down Expand Up @@ -196,30 +195,13 @@ export class BackupManager implements SecureKeyBackup {
this.baseApis.emit(CryptoEvent.KeyBackupStatus, false);
}

/** @deprecated use {@link getActiveBackupVersion} instead */
public getKeyBackupEnabled(): boolean | null {
if (!this.checkedForBackup) {
return null;
}
return Boolean(this.algorithm);
}

/**
* Get the current status of key backup.
*
* Implementation of {@link SecureKeyBackup.getActiveBackupVersion}.
*/
public async getActiveBackupVersion(): Promise<string | null> {
if (!this.checkedForBackup) {
return null;
}
if (this.algorithm && this.version) {
return this.version;
} else {
return null;
}
}

public async prepareKeyBackupVersion(
key?: string | Uint8Array | null,
algorithm?: string | undefined,
Expand Down
12 changes: 12 additions & 0 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,18 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
});
}

/**
* Get the current status of key backup.
*
* Implementation of {@link CryptoApi.getActiveSessionBackupVersion}.
*/
public async getActiveSessionBackupVersion(): Promise<string | null> {
if (this.backupManager.getKeyBackupEnabled()) {
return this.backupManager.version ?? null;
}
return null;
}

/**
* Checks that a given cross-signing private key matches a given public key.
* This can be used by the getCrossSigningKey callback to verify that the
Expand Down
4 changes: 1 addition & 3 deletions src/rust-crypto/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export class RustBackupManager implements SecureKeyBackup {
}

/**
* Get the current status of key backup.
*
* Implementation of {@link SecureKeyBackup.getActiveBackupVersion}.
* Get the backup version we are currently backing up to, if any
*/
public async getActiveBackupVersion(): Promise<string | null> {
// TODO stub
Expand Down
9 changes: 9 additions & 0 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,15 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
await this.olmMachine.saveBackupDecryptionKey(RustSdkCryptoJs.BackupDecryptionKey.fromBase64(base64Key), "");
}

/**
* Get the current status of key backup.
*
* Implementation of {@link CryptoApi#getActiveSessionBackupVersion}.
*/
public async getActiveSessionBackupVersion(): Promise<string | null> {
return await this.backupManager.getActiveBackupVersion();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// SyncCryptoCallbacks implementation
Expand Down

0 comments on commit 5435a30

Please sign in to comment.