mirror of
https://github.com/bitwarden/browser
synced 2025-12-19 01:33:33 +00:00
* Rename all importer related files Renamed all files based on our naming convention which we decided on with https://github.com/bitwarden/adr/blob/master/decisions/0012-angular-filename-convention.md * Removed entries from whitelist-capital-letters.txt * Rename missing safeInCloud test data * Fix broken import * Renamed folders (removed capital letters) * Fix filename of BitwardenCsvImporter * Fix imports of onepassword mac/win importer tests * Remove already renamed folders from whitelist * Rename dashlaneImporters to dashlane Rename the folder Fix all the imports Remove dashlaneImporters from white-list * Rename keeperImporters to keeper Rename the folder Fix all the imports Remove keeperImporters from white-list * Rename onepasswordImporters to onepassword Rename the folder Fix all the imports Remove onepasswordImporters from white-list * Rename safeinCloud test data folder * Fix onepassword importer type imports
82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
import { CryptoService } from "../abstractions/crypto.service";
|
|
import { I18nService } from "../abstractions/i18n.service";
|
|
import { KdfType } from "../enums/kdfType";
|
|
import { EncString } from "../models/domain/enc-string";
|
|
import { ImportResult } from "../models/domain/import-result";
|
|
import { SymmetricCryptoKey } from "../models/domain/symmetric-crypto-key";
|
|
|
|
import { BitwardenJsonImporter } from "./bitwarden-json-importer";
|
|
import { Importer } from "./importer";
|
|
|
|
interface BitwardenPasswordProtectedFileFormat {
|
|
encrypted: boolean;
|
|
passwordProtected: boolean;
|
|
salt: string;
|
|
kdfIterations: number;
|
|
kdfType: number;
|
|
encKeyValidation_DO_NOT_EDIT: string;
|
|
data: string;
|
|
}
|
|
|
|
export class BitwardenPasswordProtectedImporter extends BitwardenJsonImporter implements Importer {
|
|
private key: SymmetricCryptoKey;
|
|
|
|
constructor(cryptoService: CryptoService, i18nService: I18nService, private password: string) {
|
|
super(cryptoService, i18nService);
|
|
}
|
|
|
|
async parse(data: string): Promise<ImportResult> {
|
|
const result = new ImportResult();
|
|
const parsedData = JSON.parse(data);
|
|
if (this.cannotParseFile(parsedData)) {
|
|
result.success = false;
|
|
return result;
|
|
}
|
|
|
|
if (!(await this.checkPassword(parsedData))) {
|
|
result.success = false;
|
|
result.errorMessage = this.i18nService.t("invalidFilePassword");
|
|
return result;
|
|
}
|
|
|
|
const encData = new EncString(parsedData.data);
|
|
const clearTextData = await this.cryptoService.decryptToUtf8(encData, this.key);
|
|
return await super.parse(clearTextData);
|
|
}
|
|
|
|
private async checkPassword(jdoc: BitwardenPasswordProtectedFileFormat): Promise<boolean> {
|
|
this.key = await this.cryptoService.makePinKey(
|
|
this.password,
|
|
jdoc.salt,
|
|
KdfType.PBKDF2_SHA256,
|
|
jdoc.kdfIterations
|
|
);
|
|
|
|
const encKeyValidation = new EncString(jdoc.encKeyValidation_DO_NOT_EDIT);
|
|
|
|
const encKeyValidationDecrypt = await this.cryptoService.decryptToUtf8(
|
|
encKeyValidation,
|
|
this.key
|
|
);
|
|
if (encKeyValidationDecrypt === null) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private cannotParseFile(jdoc: BitwardenPasswordProtectedFileFormat): boolean {
|
|
return (
|
|
!jdoc ||
|
|
!jdoc.encrypted ||
|
|
!jdoc.passwordProtected ||
|
|
!jdoc.salt ||
|
|
!jdoc.kdfIterations ||
|
|
typeof jdoc.kdfIterations !== "number" ||
|
|
jdoc.kdfType == null ||
|
|
KdfType[jdoc.kdfType] == null ||
|
|
!jdoc.encKeyValidation_DO_NOT_EDIT ||
|
|
!jdoc.data
|
|
);
|
|
}
|
|
}
|