From 83e0db516314a1fb181e5531ee9bc36d71012d09 Mon Sep 17 00:00:00 2001 From: Thomas Rittson Date: Tue, 28 Jun 2022 11:59:37 +1000 Subject: [PATCH] Resolve merge conflicts and fix cryptoKey handling --- .../localBackedSessionStorage.service.ts | 2 +- .../models/domain/symmetricCryptoKey.spec.ts | 2 +- .../src/models/domain/symmetricCryptoKey.ts | 24 ++++++++----------- libs/common/src/services/state.service.ts | 10 ++++---- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/apps/browser/src/services/localBackedSessionStorage.service.ts b/apps/browser/src/services/localBackedSessionStorage.service.ts index 9b507b7df5..0f5305366a 100644 --- a/apps/browser/src/services/localBackedSessionStorage.service.ts +++ b/apps/browser/src/services/localBackedSessionStorage.service.ts @@ -92,7 +92,7 @@ export class LocalBackedSessionStorageService extends AbstractStorageService { storedKey = await this.keyGenerationService.makeEphemeralKey(); await this.setSessionEncKey(storedKey); } - return SymmetricCryptoKey.initFromJson( + return SymmetricCryptoKey.fromJSON( Object.create(SymmetricCryptoKey.prototype, Object.getOwnPropertyDescriptors(storedKey)) ); } diff --git a/libs/common/spec/models/domain/symmetricCryptoKey.spec.ts b/libs/common/spec/models/domain/symmetricCryptoKey.spec.ts index 65f84a21c1..e0c147dfc9 100644 --- a/libs/common/spec/models/domain/symmetricCryptoKey.spec.ts +++ b/libs/common/spec/models/domain/symmetricCryptoKey.spec.ts @@ -68,7 +68,7 @@ describe("SymmetricCryptoKey", () => { }); it("serializes and deserializes", () => { - const key = new SymmetricCryptoKey(makeStaticByteArray(64)); + const key = new SymmetricCryptoKey(makeStaticByteArray(64).buffer); const serialized = JSON.stringify(key); const newKey = SymmetricCryptoKey.fromJSON(JSON.parse(serialized)); diff --git a/libs/common/src/models/domain/symmetricCryptoKey.ts b/libs/common/src/models/domain/symmetricCryptoKey.ts index cecb24942f..f03915e170 100644 --- a/libs/common/src/models/domain/symmetricCryptoKey.ts +++ b/libs/common/src/models/domain/symmetricCryptoKey.ts @@ -56,21 +56,17 @@ export class SymmetricCryptoKey { } } - static initFromJson(jsonResult: SymmetricCryptoKey): SymmetricCryptoKey { - if (jsonResult == null) { - return jsonResult; + toJSON(): any { + // The whole object is constructed from the initial key, so just store the B64 key + return { keyB64: this.keyB64 }; + } + + static fromJSON(obj: any): SymmetricCryptoKey { + if (obj == null) { + return null; } - if (jsonResult.keyB64 != null) { - jsonResult.key = Utils.fromB64ToArray(jsonResult.keyB64).buffer; - } - if (jsonResult.encKeyB64 != null) { - jsonResult.encKey = Utils.fromB64ToArray(jsonResult.encKeyB64).buffer; - } - if (jsonResult.macKeyB64 != null) { - jsonResult.macKey = Utils.fromB64ToArray(jsonResult.macKeyB64).buffer; - } - - return jsonResult; + const arrayBuffer = Utils.fromB64ToArray(obj.keyB64).buffer; + return new SymmetricCryptoKey(arrayBuffer); } } diff --git a/libs/common/src/services/state.service.ts b/libs/common/src/services/state.service.ts index c2ae787482..156914680d 100644 --- a/libs/common/src/services/state.service.ts +++ b/libs/common/src/services/state.service.ts @@ -483,7 +483,7 @@ export class StateService< ); } - @withPrototype(SymmetricCryptoKey, SymmetricCryptoKey.initFromJson) + @withPrototype(SymmetricCryptoKey, SymmetricCryptoKey.fromJSON) async getCryptoMasterKey(options?: StorageOptions): Promise { return ( await this.getAccount(this.reconcileOptions(options, await this.defaultInMemoryOptions())) @@ -637,7 +637,7 @@ export class StateService< ); } - @withPrototype(SymmetricCryptoKey, SymmetricCryptoKey.initFromJson) + @withPrototype(SymmetricCryptoKey, SymmetricCryptoKey.fromJSON) async getDecryptedCryptoSymmetricKey(options?: StorageOptions): Promise { return ( await this.getAccount(this.reconcileOptions(options, await this.defaultInMemoryOptions())) @@ -676,7 +676,7 @@ export class StateService< ); } - @withPrototypeForMap(SymmetricCryptoKey, SymmetricCryptoKey.initFromJson) + @withPrototypeForMap(SymmetricCryptoKey, SymmetricCryptoKey.fromJSON) async getDecryptedOrganizationKeys( options?: StorageOptions ): Promise> { @@ -783,7 +783,7 @@ export class StateService< ); } - @withPrototypeForMap(SymmetricCryptoKey, SymmetricCryptoKey.initFromJson) + @withPrototypeForMap(SymmetricCryptoKey, SymmetricCryptoKey.fromJSON) async getDecryptedProviderKeys( options?: StorageOptions ): Promise> { @@ -1745,7 +1745,7 @@ export class StateService< ); } - @withPrototype(SymmetricCryptoKey, SymmetricCryptoKey.initFromJson) + @withPrototype(SymmetricCryptoKey, SymmetricCryptoKey.fromJSON) async getLegacyEtmKey(options?: StorageOptions): Promise { return ( await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions()))