From fc0638a7d95e9e678cc125d5186c1dcd8fdad7e6 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 17 Feb 2022 11:36:00 -0500 Subject: [PATCH] fix infite recursion on getEncKey (#687) --- common/src/services/crypto.service.ts | 75 ++++++++++++++------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/common/src/services/crypto.service.ts b/common/src/services/crypto.service.ts index 5a97ac7621..654af3bb77 100644 --- a/common/src/services/crypto.service.ts +++ b/common/src/services/crypto.service.ts @@ -158,41 +158,8 @@ export class CryptoService implements CryptoServiceAbstraction { } @sequentialize(() => "getEncKey") - async getEncKey(key: SymmetricCryptoKey = null): Promise { - const inMemoryKey = await this.stateService.getDecryptedCryptoSymmetricKey(); - if (inMemoryKey != null) { - return inMemoryKey; - } - - const encKey = await this.stateService.getEncryptedCryptoSymmetricKey(); - if (encKey == null) { - return null; - } - - if (key == null) { - key = await this.getKey(); - } - if (key == null) { - return null; - } - - let decEncKey: ArrayBuffer; - const encKeyCipher = new EncString(encKey); - if (encKeyCipher.encryptionType === EncryptionType.AesCbc256_B64) { - decEncKey = await this.decryptToBytes(encKeyCipher, key); - } else if (encKeyCipher.encryptionType === EncryptionType.AesCbc256_HmacSha256_B64) { - const newKey = await this.stretchKey(key); - decEncKey = await this.decryptToBytes(encKeyCipher, newKey); - } else { - throw new Error("Unsupported encKey type."); - } - - if (decEncKey == null) { - return null; - } - const symmetricCryptoKey = new SymmetricCryptoKey(decEncKey); - await this.stateService.setDecryptedCryptoSymmetricKey(symmetricCryptoKey); - return symmetricCryptoKey; + getEncKey(key: SymmetricCryptoKey = null): Promise { + return this.getEncKeyHelper(key); } async getPublicKey(): Promise { @@ -747,7 +714,7 @@ export class CryptoService implements CryptoServiceAbstraction { async validateKey(key: SymmetricCryptoKey) { try { const encPrivateKey = await this.stateService.getEncryptedPrivateKey(); - const encKey = await this.getEncKey(key); + const encKey = await this.getEncKeyHelper(key); if (encPrivateKey == null || encKey == null) { return false; } @@ -967,4 +934,40 @@ export class CryptoService implements CryptoServiceAbstraction { await this.stateService.setCryptoMasterKeyAuto(null, { userId: userId }); await this.stateService.setCryptoMasterKeyBiometric(null, { userId: userId }); } + + private async getEncKeyHelper(key: SymmetricCryptoKey = null): Promise { + const inMemoryKey = await this.stateService.getDecryptedCryptoSymmetricKey(); + if (inMemoryKey != null) { + return inMemoryKey; + } + + const encKey = await this.stateService.getEncryptedCryptoSymmetricKey(); + if (encKey == null) { + return null; + } + + if (key == null) { + key = await this.getKey(); + } + if (key == null) { + return null; + } + + let decEncKey: ArrayBuffer; + const encKeyCipher = new EncString(encKey); + if (encKeyCipher.encryptionType === EncryptionType.AesCbc256_B64) { + decEncKey = await this.decryptToBytes(encKeyCipher, key); + } else if (encKeyCipher.encryptionType === EncryptionType.AesCbc256_HmacSha256_B64) { + const newKey = await this.stretchKey(key); + decEncKey = await this.decryptToBytes(encKeyCipher, newKey); + } else { + throw new Error("Unsupported encKey type."); + } + if (decEncKey == null) { + return null; + } + const symmetricCryptoKey = new SymmetricCryptoKey(decEncKey); + await this.stateService.setDecryptedCryptoSymmetricKey(symmetricCryptoKey); + return symmetricCryptoKey; + } }