From 54752c28305bc311fd70a92e4ab1e5106fe7a57c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Gonc=CC=A7alves?= Date: Wed, 12 Jul 2023 18:06:34 +0100 Subject: [PATCH] pm-2582 Moved code to cipher service --- .../src/vault/components/attachments.component.ts | 14 -------------- libs/common/src/vault/services/cipher.service.ts | 13 ++++++++++++- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/libs/angular/src/vault/components/attachments.component.ts b/libs/angular/src/vault/components/attachments.component.ts index d9b2ab36ecf4..95fe67022a84 100644 --- a/libs/angular/src/vault/components/attachments.component.ts +++ b/libs/angular/src/vault/components/attachments.component.ts @@ -296,20 +296,6 @@ export class AttachmentsComponent implements OnInit { } protected async saveCipherAttachment(file: File) { - //if cipher key encryption is disabled but the item has an individual key, - //then we rollback to using the user key as the main key of encryption of the item - //in order to keep item and it's attachments with the same encryption level - if ( - this.cipherDomain.key != null && - !(await this.cipherService.getCipherKeyEncryptionEnabled()) - ) { - const model = await this.cipherDomain.decrypt( - await this.cipherService.getKeyForCipherKeyDecryption(this.cipherDomain) - ); - this.cipherDomain = await this.cipherService.encrypt(model); - await this.cipherService.updateWithServer(this.cipherDomain); - } - return this.cipherService.saveAttachmentWithServer(this.cipherDomain, file); } diff --git a/libs/common/src/vault/services/cipher.service.ts b/libs/common/src/vault/services/cipher.service.ts index c871e5ac9ee3..54cc5465ea50 100644 --- a/libs/common/src/vault/services/cipher.service.ts +++ b/libs/common/src/vault/services/cipher.service.ts @@ -626,11 +626,22 @@ export class CipherService implements CipherServiceAbstraction { admin = false ): Promise { const key = await this.getKeyForCipherKeyDecryption(cipher); + const cipherKeyEncryptionEnabled = await this.getCipherKeyEncryptionEnabled(); const cipherEncKey = - (await this.getCipherKeyEncryptionEnabled()) && cipher.key != null + cipherKeyEncryptionEnabled && cipher.key != null ? new SymmetricCryptoKey(await this.encryptService.decryptToBytes(cipher.key, key)) : key; + + //if cipher key encryption is disabled but the item has an individual key, + //then we rollback to using the user key as the main key of encryption of the item + //in order to keep item and it's attachments with the same encryption level + if (cipher.key != null && !cipherKeyEncryptionEnabled) { + const model = await cipher.decrypt(await this.getKeyForCipherKeyDecryption(cipher)); + cipher = await this.encrypt(model); + await this.updateWithServer(cipher); + } + const encFileName = await this.cryptoService.encrypt(filename, cipherEncKey); const dataEncKey = await this.cryptoService.makeEncKey(cipherEncKey);