Skip to content

Commit

Permalink
Disable legacy ciphers
Browse files Browse the repository at this point in the history
  • Loading branch information
quexten committed Jan 1, 2025
1 parent 894dd2c commit f7ff4da
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FormControl, FormGroup, Validators } from "@angular/forms";
import { firstValueFrom } from "rxjs";

import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
Expand Down Expand Up @@ -40,6 +41,7 @@ export class MigrateFromLegacyEncryptionComponent {
private toastService: ToastService,
private dialogService: DialogService,
private folderApiService: FolderApiServiceAbstraction,
private encryptService: EncryptService,
) {}

submit = async () => {
Expand All @@ -60,9 +62,10 @@ export class MigrateFromLegacyEncryptionComponent {
const masterPassword = this.formGroup.value.masterPassword;

try {
this.encryptService.setLegacyCiphersEnabled(true);
await this.syncService.fullSync(false, true);

await this.keyRotationService.rotateUserKeyAndEncryptedData(masterPassword, activeUser);
this.encryptService.setLegacyCiphersEnabled(false);

this.toastService.showToast({
variant: "success",
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/platform/abstractions/encrypt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export abstract class EncryptService {
value: string | Uint8Array,
algorithm: "sha1" | "sha256" | "sha512",
): Promise<string>;
abstract setLegacyCiphersEnabled(enabled: boolean): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { EncryptedObject } from "../../models/domain/encrypted-object";
import { SymmetricCryptoKey } from "../../models/domain/symmetric-crypto-key";

export class EncryptServiceImplementation implements EncryptService {
private legacyCiphersEnabled: boolean = false;

constructor(
protected cryptoFunctionService: CryptoFunctionService,
protected logService: LogService,
Expand All @@ -29,6 +31,14 @@ export class EncryptServiceImplementation implements EncryptService {
return Promise.resolve(null);
}

if (
!this.legacyCiphersEnabled &&
(key.encType === EncryptionType.AesCbc128_HmacSha256_B64 ||
key.encType === EncryptionType.AesCbc256_B64)
) {
throw new Error("Legacy ciphers are disabled.");
}

let plainBuf: Uint8Array;
if (typeof plainValue === "string") {
plainBuf = Utils.fromUtf8ToArray(plainValue);
Expand All @@ -48,6 +58,14 @@ export class EncryptServiceImplementation implements EncryptService {
throw new Error("No encryption key provided.");
}

if (
!this.legacyCiphersEnabled &&
(key.encType === EncryptionType.AesCbc128_HmacSha256_B64 ||
key.encType === EncryptionType.AesCbc256_B64)
) {
throw new Error("Legacy ciphers are disabled.");

Check failure on line 66 in libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts

View workflow job for this annotation

GitHub Actions / Test Results

EncryptService ► EncryptService encryptToBytes encrypts data using a key which supports mac ► EncryptService encryptToBytes encrypts data using a key which supports mac

Failed test found in: junit.xml Error: Error: Legacy ciphers are disabled.
Raw output
Error: Legacy ciphers are disabled.
    at EncryptServiceImplementation.<anonymous> (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:66:13)
    at Generator.next (<anonymous>)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4373:40
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4322:10)
    at EncryptServiceImplementation.encryptToBytes (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4505:12)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:54:45
    at Generator.next (<anonymous>)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:8:71
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (/home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:4:12)
    at Object.<anonymous> (/home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:45:55)
    at Promise.then.completed (/home/runner/work/clients/clients/node_modules/jest-circus/build/utils.js:298:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/home/runner/work/clients/clients/node_modules/jest-circus/build/utils.js:231:10)
    at _callCircusTest (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:316:40)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at _runTest (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:252:3)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:126:9)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:121:9)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:121:9)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:121:9)
    at run (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:71:3)
    at runAndTransformResultsToJestFormat (/home/runner/work/clients/clients/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
    at jestAdapter (/home/runner/work/clients/clients/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
    at runTestInternal (/home/runner/work/clients/clients/node_modules/jest-runner/build/runTest.js:367:16)
    at runTest (/home/runner/work/clients/clients/node_modules/jest-runner/build/runTest.js:444:34)
    at Object.worker (/home/runner/work/clients/clients/node_modules/jest-runner/build/testWorker.js:106:12)

Check failure on line 66 in libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts

View workflow job for this annotation

GitHub Actions / Test Results

EncryptService ► EncryptService encryptToBytes encrypts data using a key which doesn't support mac ► EncryptService encryptToBytes encrypts data using a key which doesn't support mac

Failed test found in: junit.xml Error: Error: Legacy ciphers are disabled.
Raw output
Error: Legacy ciphers are disabled.
    at EncryptServiceImplementation.<anonymous> (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:66:13)
    at Generator.next (<anonymous>)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4373:40
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4322:10)
    at EncryptServiceImplementation.encryptToBytes (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4505:12)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:72:45
    at Generator.next (<anonymous>)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:8:71
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (/home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:4:12)
    at Object.<anonymous> (/home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:65:62)
    at Promise.then.completed (/home/runner/work/clients/clients/node_modules/jest-circus/build/utils.js:298:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/home/runner/work/clients/clients/node_modules/jest-circus/build/utils.js:231:10)
    at _callCircusTest (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:316:40)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at _runTest (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:252:3)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:126:9)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:121:9)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:121:9)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:121:9)
    at run (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:71:3)
    at runAndTransformResultsToJestFormat (/home/runner/work/clients/clients/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
    at jestAdapter (/home/runner/work/clients/clients/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
    at runTestInternal (/home/runner/work/clients/clients/node_modules/jest-runner/build/runTest.js:367:16)
    at runTest (/home/runner/work/clients/clients/node_modules/jest-runner/build/runTest.js:444:34)
    at Object.worker (/home/runner/work/clients/clients/node_modules/jest-runner/build/testWorker.js:106:12)
}

const encValue = await this.aesEncrypt(plainValue, key);
let macLen = 0;
if (encValue.mac != null) {
Expand Down Expand Up @@ -76,6 +94,14 @@ export class EncryptServiceImplementation implements EncryptService {

key = this.resolveLegacyKey(key, encString);

if (
(!this.legacyCiphersEnabled || encString.mac == null) &&
(key.encType === EncryptionType.AesCbc128_HmacSha256_B64 ||
key.encType === EncryptionType.AesCbc256_B64)
) {
throw new Error("Legacy ciphers are disabled.");
}

// DO NOT REMOVE OR MOVE. This prevents downgrade to mac-less CBC, which would compromise integrity and confidentiality.
if (key.macKey != null && encString?.mac == null) {
this.logService.error(
Expand Down Expand Up @@ -139,6 +165,14 @@ export class EncryptServiceImplementation implements EncryptService {

key = this.resolveLegacyKey(key, encThing);

if (
(!this.legacyCiphersEnabled || encThing.macBytes == null) &&
(key.encType === EncryptionType.AesCbc128_HmacSha256_B64 ||
key.encType === EncryptionType.AesCbc256_B64)
) {
throw new Error("Legacy ciphers are disabled.");

Check failure on line 173 in libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts

View workflow job for this annotation

GitHub Actions / Test Results

EncryptService ► EncryptService decryptToBytes returns null if encTypes don't match ► EncryptService decryptToBytes returns null if encTypes don't match

Failed test found in: junit.xml Error: Error: Legacy ciphers are disabled.
Raw output
Error: Legacy ciphers are disabled.
    at EncryptServiceImplementation.<anonymous> (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:173:13)
    at Generator.next (<anonymous>)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4373:40
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4322:10)
    at EncryptServiceImplementation.decryptToBytes (/home/runner/work/clients/clients/libs/common/src/platform/services/cryptography/encrypt.service.implementation.ts:4711:12)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:160:43
    at Generator.next (<anonymous>)
    at /home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:8:71
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (/home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:4:12)
    at Object.<anonymous> (/home/runner/work/clients/clients/libs/common/src/platform/services/encrypt.service.spec.ts:156:59)
    at Promise.then.completed (/home/runner/work/clients/clients/node_modules/jest-circus/build/utils.js:298:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/home/runner/work/clients/clients/node_modules/jest-circus/build/utils.js:231:10)
    at _callCircusTest (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:316:40)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at _runTest (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:252:3)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:126:9)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:121:9)
    at _runTestsForDescribeBlock (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:121:9)
    at run (/home/runner/work/clients/clients/node_modules/jest-circus/build/run.js:71:3)
    at runAndTransformResultsToJestFormat (/home/runner/work/clients/clients/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
    at jestAdapter (/home/runner/work/clients/clients/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
    at runTestInternal (/home/runner/work/clients/clients/node_modules/jest-runner/build/runTest.js:367:16)
    at runTest (/home/runner/work/clients/clients/node_modules/jest-runner/build/runTest.js:444:34)
    at Object.worker (/home/runner/work/clients/clients/node_modules/jest-runner/build/testWorker.js:106:12)
}

// DO NOT REMOVE OR MOVE. This prevents downgrade to mac-less CBC, which would compromise integrity and confidentiality.
if (key.macKey != null && encThing.macBytes == null) {
this.logService.error(
Expand Down Expand Up @@ -297,4 +331,9 @@ export class EncryptServiceImplementation implements EncryptService {

return key;
}

setLegacyCiphersEnabled(enabled: boolean): void {
this.logService.info("[Encrypt service] Legacy ciphers enabled: " + enabled);
this.legacyCiphersEnabled = enabled;
}
}

0 comments on commit f7ff4da

Please sign in to comment.