bitwarden-estensione-browser/libs/importer/src/importers/bitwarden/bitwarden-password-protecte...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
3.1 KiB
TypeScript
Raw Normal View History

import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { KdfConfig } from "@bitwarden/common/auth/models/domain/kdf-config";
[AC-1266] Enums filename conventions (#5140) * refactor: update clientType enum * refactor: update deviceType filename * refactor: update encryptedExportType filename * refactor: update encryptionType filename * refactor: update eventType filename * refactor: update fieldType filename * refactor: update fileUploadType filename * refactor: update hashPurpose filename * refactor: update htmlStorageLocation filename * refactor: update kdfType filename * refactor: update keySuffixOptions filename * refactor: update linkedIdType filename * refactor: update logLevelType filename * refactor: update nativeMessagingVersion filename * refactor: update notificationType filename * refactor: update productType filename * refactor: update secureNoteType filename * refactor: update stateVersion filename * refactor: update storageLocation filename * refactor: update themeType filename * refactor: update uriMatchType filename * fix: update kdfType classes missed in initial pass, refs AC-1266 * fix: missing import update for device-type * refactor: add barrel file for enums and update pathed import statements, refs AC-1266 * fix: incorrect import statements for web, refs AC-1266 * fix: missed import statement updates (browser), refs AC-1266 * fix: missed import statement changes (cli), refs AC-1266 * fix: missed import statement changes (desktop), refs AC-1266 * fix: prettier, refs AC-1266 * refactor: (libs) update relative paths to use barrel file, refs AC-1266 * fix: missed find/replace import statements for SecureNoteType, refs AC-1266 * refactor: apply .enum suffix to enums folder and modify leftover relative paths, refs AC-1266 * fix: find/replace errors for native-messaging-version, refs AC-1266
2023-04-05 05:42:21 +02:00
import { KdfType } from "@bitwarden/common/enums";
import { EncString } from "@bitwarden/common/models/domain/enc-string";
import { SymmetricCryptoKey } from "@bitwarden/common/models/domain/symmetric-crypto-key";
import { BitwardenPasswordProtectedFileFormat } from "@bitwarden/exporter/vault-export/bitwarden-password-protected-types";
import { ImportResult } from "../../models/import-result";
import { Importer } from "../importer";
import { BitwardenJsonImporter } from "./bitwarden-json-importer";
export class BitwardenPasswordProtectedImporter extends BitwardenJsonImporter implements Importer {
private key: SymmetricCryptoKey;
constructor(
cryptoService: CryptoService,
i18nService: I18nService,
private promptForPassword_callback: () => Promise<string>
) {
super(cryptoService, i18nService);
}
async parse(data: string): Promise<ImportResult> {
const result = new ImportResult();
const parsedData: BitwardenPasswordProtectedFileFormat = JSON.parse(data);
if (!parsedData) {
result.success = false;
return result;
}
// File is unencrypted
if (!parsedData?.encrypted) {
return await super.parse(data);
}
// File is account-encrypted
if (!parsedData?.passwordProtected) {
return await super.parse(data);
}
if (this.cannotParseFile(parsedData)) {
result.success = false;
return result;
}
// File is password-protected
const password = await this.promptForPassword_callback();
if (!(await this.checkPassword(parsedData, password))) {
result.success = false;
[SM-89] Updates to encrypted export (#2963) * Rough draft of Export/Import changes w/ password encryption * fix for encrypted export changes * Create launch.json * Updates to export logic modal user secret prompt * Updates to error handling * renaming the component for checking the user secret to a name that is more clear about what it accomplishes * Fixing lint errors * Adding a comment * Suggested changes from CR * Suggested changes from CR * Making suggested changes * removing unnecessary properties * changes suggested * Fix * Updating error messages * Removing unecessary launch.json file commit * running lint, removing commented code * removing launch.json * Updates to remove the userVerificationPromptService * updates * Removing unused import, running npm prettier/lint * Changes to use Form Fields * Updates * updates requested by Matt * Update apps/web/src/app/tools/import-export/export.component.ts Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> * Suggested Changes from PR * Fix after merge from Master * changes to styling * Removing unused code and cleanup * Update libs/angular/src/components/user-verification-prompt.component.ts Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> * Update apps/web/src/locales/en/messages.json Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> * Changes suggested by Thomas R * Merging master into branch * Revert "Merging master into branch" This reverts commit eb2cdffe49955e5a902219bc23b1d79fd8c92c03. * Requested changes and improvements * merging master into feature branch * Revert "merging master into feature branch" This reverts commit e287715251880641f79f68e5e187fe29bbaca22f. * Suggested Changes * changes * requested changes * Requested changes * removing comments, fixing code * reducing copied code * fixing bug * fixing bug * changes * WIP * Thomas's requested changes * adding back missing spaces * change needed after the merge from master into feature branch * prettier + lint * Updating the EncryptedExportType Import * Fixing build errors Co-authored-by: Thomas Rittson <eliykat@users.noreply.github.com> * Move FilePasswordPrompt to ImportExportModule Also remove base class Also remove duplicate service providers * Run prettier * Suggested Changes from Thomas * only require filePassword and confirmFilePassword if it's type is FileEncrypted * Update to only enable the field when submitting a file password encrypted file * Requested changes, moving logic to web * undoing change to bit button * Refactor to process file-encrypted imports in main import.component *  Refactor confirm file password check * Remove UserVerificationPromptService * Address CodeScene feedback * Updates to disable the required file password field when needed * Subscribe to reactive form changes to adjust validators * style changes requested by suhkleen * Delete duplicate classes Co-authored-by: CarleyDiaz-Bitwarden <103955722+CarleyDiaz-Bitwarden@users.noreply.github.com> Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Co-authored-by: Thomas Rittson <trittson@bitwarden.com> Co-authored-by: Thomas Rittson <eliykat@users.noreply.github.com>
2022-08-29 16:11:44 +02:00
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,
password: string
): Promise<boolean> {
this.key = await this.cryptoService.makePinKey(
password,
jdoc.salt,
jdoc.kdfType,
new KdfConfig(jdoc.kdfIterations, jdoc.kdfMemory, jdoc.kdfParallelism)
);
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
);
}
}