1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 08:43:33 +00:00

remove provided key from getKeyForUserEncryption

This commit is contained in:
Jacob Fink
2023-05-25 13:12:52 -04:00
parent 4ce2a17ea4
commit acd3ef2204
2 changed files with 15 additions and 17 deletions

View File

@@ -13,7 +13,7 @@ import {
} from "../models/domain/symmetric-crypto-key"; } from "../models/domain/symmetric-crypto-key";
export abstract class CryptoService { export abstract class CryptoService {
// TODO: Update logic for this method // TODO: This works right?
getKeyForUserEncryption: (key?: SymmetricCryptoKey) => Promise<SymmetricCryptoKey>; getKeyForUserEncryption: (key?: SymmetricCryptoKey) => Promise<SymmetricCryptoKey>;
setUserKey: (key: SymmetricCryptoKey) => Promise<void>; setUserKey: (key: SymmetricCryptoKey) => Promise<void>;

View File

@@ -43,20 +43,18 @@ export class CryptoService implements CryptoServiceAbstraction {
) {} ) {}
/** /**
* TODO: We probably can't remove this. We need to have logic to choose the correct key. * Use for encryption/decryption of data in order to support legacy
* encryption models. It will return the user symmetric key if available,
* if not it will return the master key.
*/ */
async getKeyForUserEncryption(key?: SymmetricCryptoKey): Promise<SymmetricCryptoKey> { async getKeyForUserEncryption(): Promise<SymmetricCryptoKey> {
if (key != null) { const userKey = await this.getUserKey();
return key; if (userKey != null) {
} return userKey;
const encKey = await this.getEncKey();
if (encKey != null) {
return encKey;
} }
// Legacy support: encryption used to be done with the master key (derived from master password). // Legacy support: encryption used to be done with the master key (derived from master password).
// Users who have not migrated will have a null encKey and must use the master key instead. // Users who have not migrated will have a null user key and must use the master key instead.
return await this.stateService.getCryptoMasterKey(); return await this.stateService.getCryptoMasterKey();
} }
@@ -996,7 +994,7 @@ export class CryptoService implements CryptoServiceAbstraction {
* and then call encryptService.encrypt * and then call encryptService.encrypt
*/ */
async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<EncString> { async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<EncString> {
key = await this.getKeyForUserEncryption(key); key ||= await this.getKeyForUserEncryption();
return await this.encryptService.encrypt(plainValue, key); return await this.encryptService.encrypt(plainValue, key);
} }
@@ -1005,7 +1003,7 @@ export class CryptoService implements CryptoServiceAbstraction {
* and then call encryptService.encryptToBytes * and then call encryptService.encryptToBytes
*/ */
async encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise<EncArrayBuffer> { async encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise<EncArrayBuffer> {
key = await this.getKeyForUserEncryption(key); key ||= await this.getKeyForUserEncryption();
return this.encryptService.encryptToBytes(plainValue, key); return this.encryptService.encryptToBytes(plainValue, key);
} }
@@ -1014,8 +1012,8 @@ export class CryptoService implements CryptoServiceAbstraction {
* and then call encryptService.decryptToBytes * and then call encryptService.decryptToBytes
*/ */
async decryptToBytes(encString: EncString, key?: SymmetricCryptoKey): Promise<ArrayBuffer> { async decryptToBytes(encString: EncString, key?: SymmetricCryptoKey): Promise<ArrayBuffer> {
const keyForEnc = await this.getKeyForUserEncryption(key); key ||= await this.getKeyForUserEncryption();
return this.encryptService.decryptToBytes(encString, keyForEnc); return this.encryptService.decryptToBytes(encString, key);
} }
/** /**
@@ -1023,7 +1021,7 @@ export class CryptoService implements CryptoServiceAbstraction {
* and then call encryptService.decryptToUtf8 * and then call encryptService.decryptToUtf8
*/ */
async decryptToUtf8(encString: EncString, key?: SymmetricCryptoKey): Promise<string> { async decryptToUtf8(encString: EncString, key?: SymmetricCryptoKey): Promise<string> {
key = await this.getKeyForUserEncryption(key); key ||= await this.getKeyForUserEncryption();
return await this.encryptService.decryptToUtf8(encString, key); return await this.encryptService.decryptToUtf8(encString, key);
} }
@@ -1036,7 +1034,7 @@ export class CryptoService implements CryptoServiceAbstraction {
throw new Error("No buffer provided for decryption."); throw new Error("No buffer provided for decryption.");
} }
key = await this.getKeyForUserEncryption(key); key ||= await this.getKeyForUserEncryption();
return this.encryptService.decryptToBytes(encBuffer, key); return this.encryptService.decryptToBytes(encBuffer, key);
} }