diff --git a/bitwarden_license/bit-web/src/app/billing/providers/setup/setup-business-unit.component.ts b/bitwarden_license/bit-web/src/app/billing/providers/setup/setup-business-unit.component.ts index 4c8d483a0c5..f262ba1abd0 100644 --- a/bitwarden_license/bit-web/src/app/billing/providers/setup/setup-business-unit.component.ts +++ b/bitwarden_license/bit-web/src/app/billing/providers/setup/setup-business-unit.component.ts @@ -84,10 +84,8 @@ export class SetupBusinessUnitComponent extends BaseAcceptComponent { const organizationKey = await firstValueFrom(organizationKey$); - const { encryptedString: encryptedOrganizationKey } = await this.encryptService.encrypt( - organizationKey.key, - providerKey, - ); + const { encryptedString: encryptedOrganizationKey } = + await this.encryptService.wrapSymmetricKey(organizationKey, providerKey); if (!encryptedProviderKey || !encryptedOrganizationKey) { return await fail(); diff --git a/libs/common/src/key-management/crypto/abstractions/encrypt.service.ts b/libs/common/src/key-management/crypto/abstractions/encrypt.service.ts index db25f415339..243c307f49f 100644 --- a/libs/common/src/key-management/crypto/abstractions/encrypt.service.ts +++ b/libs/common/src/key-management/crypto/abstractions/encrypt.service.ts @@ -13,6 +13,14 @@ export abstract class EncryptService { * @param key - The key to encrypt the value with */ abstract encrypt(plainValue: string, key: SymmetricCryptoKey): Promise; + /** + * Encrypts bytes to an EncString + * @param plainValue - The value to encrypt + * @param key - The key to encrypt the value with + * @deprecated Bytes are not the right abstraction to encrypt in. Use e.g. key wrapping or file encryption instead + */ + abstract encryptBytes(plainValue: Uint8Array, key: SymmetricCryptoKey): Promise; + /** * Encrypts a value to a Uint8Array * @param plainValue - The value to encrypt diff --git a/libs/common/src/key-management/crypto/services/encrypt.service.implementation.ts b/libs/common/src/key-management/crypto/services/encrypt.service.implementation.ts index 1838cec83c3..6d0bc0133c2 100644 --- a/libs/common/src/key-management/crypto/services/encrypt.service.implementation.ts +++ b/libs/common/src/key-management/crypto/services/encrypt.service.implementation.ts @@ -59,6 +59,24 @@ export class EncryptServiceImplementation implements EncryptService { return this.encryptUint8Array(Utils.fromUtf8ToArray(plainValue), key); } + async encryptBytes(plainValue: Uint8Array, key: SymmetricCryptoKey): Promise { + if (key == null) { + throw new Error("No encryption key provided."); + } + + if (this.blockType0) { + if (key.inner().type === EncryptionType.AesCbc256_B64 || key.key.byteLength < 64) { + throw new Error("Type 0 encryption is not supported."); + } + } + + if (plainValue == null) { + return null; + } + + return this.encryptUint8Array(plainValue, key); + } + async wrapDecapsulationKey( decapsulationKeyPkcs8: Uint8Array, wrappingKey: SymmetricCryptoKey, diff --git a/libs/common/src/key-management/device-trust/services/device-trust.service.implementation.ts b/libs/common/src/key-management/device-trust/services/device-trust.service.implementation.ts index 205f332d0f9..366e57245c7 100644 --- a/libs/common/src/key-management/device-trust/services/device-trust.service.implementation.ts +++ b/libs/common/src/key-management/device-trust/services/device-trust.service.implementation.ts @@ -220,9 +220,12 @@ export class DeviceTrustService implements DeviceTrustServiceAbstraction { return null; } - const newEncryptedPublicKey = await this.encryptService.encrypt(publicKey, newUserKey); - const newEncryptedUserKey = await this.encryptService.rsaEncrypt( - newUserKey.key, + const newEncryptedPublicKey = await this.encryptService.wrapEncapsulationKey( + publicKey, + newUserKey, + ); + const newEncryptedUserKey = await this.encryptService.encapsulateKeyUnsigned( + newUserKey, publicKey, ); diff --git a/libs/common/src/tools/send/services/send.service.ts b/libs/common/src/tools/send/services/send.service.ts index 8d6e62e3b8c..093e38a18b6 100644 --- a/libs/common/src/tools/send/services/send.service.ts +++ b/libs/common/src/tools/send/services/send.service.ts @@ -82,7 +82,7 @@ export class SendService implements InternalSendServiceAbstraction { key = await this.keyService.getUserKey(); } // Key is not a SymmetricCryptoKey, but key material used to derive the cryptoKey - send.key = await this.encryptService.encrypt(model.key, key); + send.key = await this.encryptService.encryptBytes(model.key, key); send.name = await this.encryptService.encrypt(model.name, model.cryptoKey); send.notes = await this.encryptService.encrypt(model.notes, model.cryptoKey); if (send.type === SendType.Text) {