Skip to content

Commit 09a461b

Browse files
committed
Store cross signing keys and user signing keys
1 parent 86b8d03 commit 09a461b

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

spec/integ/crypto/crypto.spec.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,11 +2235,11 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
22352235
* Resolved when the cross signing master key is uploaded
22362236
* https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3useruseridaccount_datatype
22372237
*/
2238-
function awaitCrossSigningMasterKeyUpload(): Promise<Record<string, {}>> {
2238+
function awaitCrossSigningKeyUpload(key: string): Promise<Record<string, {}>> {
22392239
return new Promise((resolve) => {
2240-
// Called when the cross signing key master key is uploaded
2240+
// Called when the cross signing key is uploaded
22412241
fetchMock.put(
2242-
"express:/_matrix/client/r0/user/:userId/account_data/m.cross_signing.master",
2242+
`express:/_matrix/client/r0/user/:userId/account_data/m.cross_signing.${key}`,
22432243
(url: string, options: RequestInit) => {
22442244
const content = JSON.parse(options.body as string);
22452245
resolve(content.encrypted);
@@ -2374,11 +2374,13 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
23742374
},
23752375
);
23762376

2377-
newBackendOnly("should upload cross signing master key", async () => {
2377+
newBackendOnly("should upload cross signing keys", async () => {
23782378
mockSetupCrossSigningRequests();
23792379

2380+
// Before setting up secret-storage, bootstrap cross-signing, so that the client has cross-signing keys.
23802381
await aliceClient.getCrypto()?.bootstrapCrossSigning({});
23812382

2383+
// Now, when we bootstrap secret-storage, the cross-signing keys should be uploaded.
23822384
const bootstrapPromise = aliceClient
23832385
.getCrypto()!
23842386
.bootstrapSecretStorage({ setupNewSecretStorage: true, createSecretStorageKey });
@@ -2389,14 +2391,20 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
23892391
// Return the newly created key in the sync response
23902392
sendSyncResponse(secretStorageKey);
23912393

2392-
// Wait for the cross signing key to be uploaded
2393-
const crossSigningKey = await awaitCrossSigningMasterKeyUpload();
2394+
// Wait for the cross signing keys to be uploaded
2395+
const [masterKey, userSigningKey, selfSigningKey] = await Promise.all([
2396+
awaitCrossSigningKeyUpload("master"),
2397+
awaitCrossSigningKeyUpload("user_signing"),
2398+
awaitCrossSigningKeyUpload("self_signing"),
2399+
]);
23942400

23952401
// Finally, wait for bootstrapSecretStorage to finished
23962402
await bootstrapPromise;
23972403

23982404
// Expect the cross signing master key to be uploaded and to be encrypted with `secretStorageKey`
2399-
expect(crossSigningKey[secretStorageKey]).toBeDefined();
2405+
expect(masterKey[secretStorageKey]).toBeDefined();
2406+
expect(userSigningKey[secretStorageKey]).toBeDefined();
2407+
expect(selfSigningKey[secretStorageKey]).toBeDefined();
24002408
});
24012409
});
24022410
});

src/rust-crypto/rust-crypto.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,14 @@ export class RustCrypto implements CryptoBackend {
401401
await this.addSecretStorageKeyToSecretStorage(recoveryKey);
402402
}
403403

404+
const crossSigningStatus: RustSdkCryptoJs.CrossSigningStatus = await this.olmMachine.crossSigningStatus();
405+
const hasPrivateKeys =
406+
crossSigningStatus.hasMaster && crossSigningStatus.hasSelfSigning && crossSigningStatus.hasUserSigning;
407+
404408
// If we have cross-signing private keys cached, store them in secret
405409
// storage if they are not there already.
406410
if (
407-
(await this.isCrossSigningReady()) &&
411+
hasPrivateKeys &&
408412
(isNewSecretStorageKeyNeeded || !(await secretStorageContainsCrossSigningKeys(this.secretStorage)))
409413
) {
410414
const crossSigningPrivateKeys: RustSdkCryptoJs.CrossSigningKeyExport =
@@ -414,7 +418,17 @@ export class RustCrypto implements CryptoBackend {
414418
throw new Error("missing master key in cross signing private keys");
415419
}
416420

421+
if (!crossSigningPrivateKeys.userSigningKey) {
422+
throw new Error("missing user signing key in cross signing private keys");
423+
}
424+
425+
if (!crossSigningPrivateKeys.self_signing_key) {
426+
throw new Error("missing self signing key in cross signing private keys");
427+
}
428+
417429
await this.secretStorage.store("m.cross_signing.master", crossSigningPrivateKeys.masterKey);
430+
await this.secretStorage.store("m.cross_signing.user_signing", crossSigningPrivateKeys.userSigningKey);
431+
await this.secretStorage.store("m.cross_signing.self_signing", crossSigningPrivateKeys.self_signing_key);
418432
}
419433
}
420434

0 commit comments

Comments
 (0)