mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 22:03:36 +00:00
[PM-17667] Move key-generation service to KM ownership (#16015)
* Move key-generation service * Update comment * Add deprecation comments * Fix firefox build * Update comment * Update DI import * Update module imports
This commit is contained in:
@@ -69,6 +69,10 @@ import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abs
|
|||||||
import { DefaultBillingAccountProfileStateService } from "@bitwarden/common/billing/services/account/billing-account-profile-state.service";
|
import { DefaultBillingAccountProfileStateService } from "@bitwarden/common/billing/services/account/billing-account-profile-state.service";
|
||||||
import { ClientType } from "@bitwarden/common/enums";
|
import { ClientType } from "@bitwarden/common/enums";
|
||||||
import { ProcessReloadServiceAbstraction } from "@bitwarden/common/key-management/abstractions/process-reload.service";
|
import { ProcessReloadServiceAbstraction } from "@bitwarden/common/key-management/abstractions/process-reload.service";
|
||||||
|
import {
|
||||||
|
DefaultKeyGenerationService,
|
||||||
|
KeyGenerationService,
|
||||||
|
} from "@bitwarden/common/key-management/crypto";
|
||||||
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
||||||
import { EncryptServiceImplementation } from "@bitwarden/common/key-management/crypto/services/encrypt.service.implementation";
|
import { EncryptServiceImplementation } from "@bitwarden/common/key-management/crypto/services/encrypt.service.implementation";
|
||||||
@@ -97,7 +101,6 @@ import { Fido2ClientService as Fido2ClientServiceAbstraction } from "@bitwarden/
|
|||||||
import { Fido2UserInterfaceService as Fido2UserInterfaceServiceAbstraction } from "@bitwarden/common/platform/abstractions/fido2/fido2-user-interface.service.abstraction";
|
import { Fido2UserInterfaceService as Fido2UserInterfaceServiceAbstraction } from "@bitwarden/common/platform/abstractions/fido2/fido2-user-interface.service.abstraction";
|
||||||
import { FileUploadService as FileUploadServiceAbstraction } from "@bitwarden/common/platform/abstractions/file-upload/file-upload.service";
|
import { FileUploadService as FileUploadServiceAbstraction } from "@bitwarden/common/platform/abstractions/file-upload/file-upload.service";
|
||||||
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
|
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
import { KeyGenerationService as KeyGenerationServiceAbstraction } from "@bitwarden/common/platform/abstractions/key-generation.service";
|
|
||||||
import { LogService as LogServiceAbstraction } from "@bitwarden/common/platform/abstractions/log.service";
|
import { LogService as LogServiceAbstraction } from "@bitwarden/common/platform/abstractions/log.service";
|
||||||
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||||
import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service";
|
import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service";
|
||||||
@@ -135,7 +138,6 @@ import { Fido2ActiveRequestManager } from "@bitwarden/common/platform/services/f
|
|||||||
import { Fido2AuthenticatorService } from "@bitwarden/common/platform/services/fido2/fido2-authenticator.service";
|
import { Fido2AuthenticatorService } from "@bitwarden/common/platform/services/fido2/fido2-authenticator.service";
|
||||||
import { Fido2ClientService } from "@bitwarden/common/platform/services/fido2/fido2-client.service";
|
import { Fido2ClientService } from "@bitwarden/common/platform/services/fido2/fido2-client.service";
|
||||||
import { FileUploadService } from "@bitwarden/common/platform/services/file-upload/file-upload.service";
|
import { FileUploadService } from "@bitwarden/common/platform/services/file-upload/file-upload.service";
|
||||||
import { KeyGenerationService } from "@bitwarden/common/platform/services/key-generation.service";
|
|
||||||
import { MigrationBuilderService } from "@bitwarden/common/platform/services/migration-builder.service";
|
import { MigrationBuilderService } from "@bitwarden/common/platform/services/migration-builder.service";
|
||||||
import { MigrationRunner } from "@bitwarden/common/platform/services/migration-runner";
|
import { MigrationRunner } from "@bitwarden/common/platform/services/migration-runner";
|
||||||
import { DefaultSdkClientFactory } from "@bitwarden/common/platform/services/sdk/default-sdk-client-factory";
|
import { DefaultSdkClientFactory } from "@bitwarden/common/platform/services/sdk/default-sdk-client-factory";
|
||||||
@@ -311,7 +313,7 @@ export default class MainBackground {
|
|||||||
i18nService: I18nServiceAbstraction;
|
i18nService: I18nServiceAbstraction;
|
||||||
platformUtilsService: PlatformUtilsServiceAbstraction;
|
platformUtilsService: PlatformUtilsServiceAbstraction;
|
||||||
logService: LogServiceAbstraction;
|
logService: LogServiceAbstraction;
|
||||||
keyGenerationService: KeyGenerationServiceAbstraction;
|
keyGenerationService: KeyGenerationService;
|
||||||
keyService: KeyServiceAbstraction;
|
keyService: KeyServiceAbstraction;
|
||||||
cryptoFunctionService: CryptoFunctionServiceAbstraction;
|
cryptoFunctionService: CryptoFunctionServiceAbstraction;
|
||||||
masterPasswordService: InternalMasterPasswordServiceAbstraction;
|
masterPasswordService: InternalMasterPasswordServiceAbstraction;
|
||||||
@@ -472,7 +474,7 @@ export default class MainBackground {
|
|||||||
const isDev = process.env.ENV === "development";
|
const isDev = process.env.ENV === "development";
|
||||||
this.logService = new ConsoleLogService(isDev);
|
this.logService = new ConsoleLogService(isDev);
|
||||||
this.cryptoFunctionService = new WebCryptoFunctionService(self);
|
this.cryptoFunctionService = new WebCryptoFunctionService(self);
|
||||||
this.keyGenerationService = new KeyGenerationService(this.cryptoFunctionService);
|
this.keyGenerationService = new DefaultKeyGenerationService(this.cryptoFunctionService);
|
||||||
this.storageService = new BrowserLocalStorageService(this.logService);
|
this.storageService = new BrowserLocalStorageService(this.logService);
|
||||||
|
|
||||||
this.intraprocessMessagingSubject = new Subject<Message<Record<string, unknown>>>();
|
this.intraprocessMessagingSubject = new Subject<Message<Record<string, unknown>>>();
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ import {
|
|||||||
} from "@bitwarden/common/autofill/services/user-notification-settings.service";
|
} from "@bitwarden/common/autofill/services/user-notification-settings.service";
|
||||||
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
|
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
|
||||||
import { ClientType } from "@bitwarden/common/enums";
|
import { ClientType } from "@bitwarden/common/enums";
|
||||||
|
import { KeyGenerationService } from "@bitwarden/common/key-management/crypto";
|
||||||
import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
||||||
import { WebCryptoFunctionService } from "@bitwarden/common/key-management/crypto/services/web-crypto-function.service";
|
import { WebCryptoFunctionService } from "@bitwarden/common/key-management/crypto/services/web-crypto-function.service";
|
||||||
@@ -79,7 +80,6 @@ import { ConfigService } from "@bitwarden/common/platform/abstractions/config/co
|
|||||||
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
||||||
import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service";
|
import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service";
|
||||||
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
|
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
import { KeyGenerationService } from "@bitwarden/common/platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||||
import { MessagingService as MessagingServiceAbstraction } from "@bitwarden/common/platform/abstractions/messaging.service";
|
import { MessagingService as MessagingServiceAbstraction } from "@bitwarden/common/platform/abstractions/messaging.service";
|
||||||
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||||
|
|||||||
@@ -60,6 +60,10 @@ import {
|
|||||||
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
|
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
|
||||||
import { DefaultBillingAccountProfileStateService } from "@bitwarden/common/billing/services/account/billing-account-profile-state.service";
|
import { DefaultBillingAccountProfileStateService } from "@bitwarden/common/billing/services/account/billing-account-profile-state.service";
|
||||||
import { ClientType } from "@bitwarden/common/enums";
|
import { ClientType } from "@bitwarden/common/enums";
|
||||||
|
import {
|
||||||
|
DefaultKeyGenerationService,
|
||||||
|
KeyGenerationService,
|
||||||
|
} from "@bitwarden/common/key-management/crypto";
|
||||||
import { EncryptServiceImplementation } from "@bitwarden/common/key-management/crypto/services/encrypt.service.implementation";
|
import { EncryptServiceImplementation } from "@bitwarden/common/key-management/crypto/services/encrypt.service.implementation";
|
||||||
import { DeviceTrustServiceAbstraction } from "@bitwarden/common/key-management/device-trust/abstractions/device-trust.service.abstraction";
|
import { DeviceTrustServiceAbstraction } from "@bitwarden/common/key-management/device-trust/abstractions/device-trust.service.abstraction";
|
||||||
import { DeviceTrustService } from "@bitwarden/common/key-management/device-trust/services/device-trust.service.implementation";
|
import { DeviceTrustService } from "@bitwarden/common/key-management/device-trust/services/device-trust.service.implementation";
|
||||||
@@ -81,7 +85,6 @@ import {
|
|||||||
EnvironmentService,
|
EnvironmentService,
|
||||||
RegionConfig,
|
RegionConfig,
|
||||||
} from "@bitwarden/common/platform/abstractions/environment.service";
|
} from "@bitwarden/common/platform/abstractions/environment.service";
|
||||||
import { KeyGenerationService as KeyGenerationServiceAbstraction } from "@bitwarden/common/platform/abstractions/key-generation.service";
|
|
||||||
import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service";
|
import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service";
|
||||||
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
|
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
|
||||||
import { KeySuffixOptions, LogLevelType } from "@bitwarden/common/platform/enums";
|
import { KeySuffixOptions, LogLevelType } from "@bitwarden/common/platform/enums";
|
||||||
@@ -99,7 +102,6 @@ import { DefaultConfigService } from "@bitwarden/common/platform/services/config
|
|||||||
import { ContainerService } from "@bitwarden/common/platform/services/container.service";
|
import { ContainerService } from "@bitwarden/common/platform/services/container.service";
|
||||||
import { DefaultEnvironmentService } from "@bitwarden/common/platform/services/default-environment.service";
|
import { DefaultEnvironmentService } from "@bitwarden/common/platform/services/default-environment.service";
|
||||||
import { FileUploadService } from "@bitwarden/common/platform/services/file-upload/file-upload.service";
|
import { FileUploadService } from "@bitwarden/common/platform/services/file-upload/file-upload.service";
|
||||||
import { KeyGenerationService } from "@bitwarden/common/platform/services/key-generation.service";
|
|
||||||
import { MemoryStorageService } from "@bitwarden/common/platform/services/memory-storage.service";
|
import { MemoryStorageService } from "@bitwarden/common/platform/services/memory-storage.service";
|
||||||
import { MigrationBuilderService } from "@bitwarden/common/platform/services/migration-builder.service";
|
import { MigrationBuilderService } from "@bitwarden/common/platform/services/migration-builder.service";
|
||||||
import { MigrationRunner } from "@bitwarden/common/platform/services/migration-runner";
|
import { MigrationRunner } from "@bitwarden/common/platform/services/migration-runner";
|
||||||
@@ -239,7 +241,7 @@ export class ServiceContainer {
|
|||||||
individualExportService: IndividualVaultExportServiceAbstraction;
|
individualExportService: IndividualVaultExportServiceAbstraction;
|
||||||
organizationExportService: OrganizationVaultExportServiceAbstraction;
|
organizationExportService: OrganizationVaultExportServiceAbstraction;
|
||||||
searchService: SearchService;
|
searchService: SearchService;
|
||||||
keyGenerationService: KeyGenerationServiceAbstraction;
|
keyGenerationService: KeyGenerationService;
|
||||||
cryptoFunctionService: NodeCryptoFunctionService;
|
cryptoFunctionService: NodeCryptoFunctionService;
|
||||||
encryptService: EncryptServiceImplementation;
|
encryptService: EncryptServiceImplementation;
|
||||||
authService: AuthService;
|
authService: AuthService;
|
||||||
@@ -397,7 +399,7 @@ export class ServiceContainer {
|
|||||||
process.env.ADDITIONAL_REGIONS as unknown as RegionConfig[],
|
process.env.ADDITIONAL_REGIONS as unknown as RegionConfig[],
|
||||||
);
|
);
|
||||||
|
|
||||||
this.keyGenerationService = new KeyGenerationService(this.cryptoFunctionService);
|
this.keyGenerationService = new DefaultKeyGenerationService(this.cryptoFunctionService);
|
||||||
|
|
||||||
this.tokenService = new TokenService(
|
this.tokenService = new TokenService(
|
||||||
this.singleUserStateProvider,
|
this.singleUserStateProvider,
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ import { SsoLoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/
|
|||||||
import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service";
|
import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service";
|
||||||
import { ClientType } from "@bitwarden/common/enums";
|
import { ClientType } from "@bitwarden/common/enums";
|
||||||
import { ProcessReloadServiceAbstraction } from "@bitwarden/common/key-management/abstractions/process-reload.service";
|
import { ProcessReloadServiceAbstraction } from "@bitwarden/common/key-management/abstractions/process-reload.service";
|
||||||
|
import { KeyGenerationService } from "@bitwarden/common/key-management/crypto";
|
||||||
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
||||||
import { WebCryptoFunctionService } from "@bitwarden/common/key-management/crypto/services/web-crypto-function.service";
|
import { WebCryptoFunctionService } from "@bitwarden/common/key-management/crypto/services/web-crypto-function.service";
|
||||||
@@ -67,7 +68,6 @@ import { Fido2AuthenticatorService as Fido2AuthenticatorServiceAbstraction } fro
|
|||||||
import { Fido2UserInterfaceService as Fido2UserInterfaceServiceAbstraction } from "@bitwarden/common/platform/abstractions/fido2/fido2-user-interface.service.abstraction";
|
import { Fido2UserInterfaceService as Fido2UserInterfaceServiceAbstraction } from "@bitwarden/common/platform/abstractions/fido2/fido2-user-interface.service.abstraction";
|
||||||
import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service";
|
import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service";
|
||||||
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
|
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
import { KeyGenerationService as KeyGenerationServiceAbstraction } from "@bitwarden/common/platform/abstractions/key-generation.service";
|
|
||||||
import {
|
import {
|
||||||
LogService,
|
LogService,
|
||||||
LogService as LogServiceAbstraction,
|
LogService as LogServiceAbstraction,
|
||||||
@@ -304,7 +304,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
deps: [
|
deps: [
|
||||||
PinServiceAbstraction,
|
PinServiceAbstraction,
|
||||||
InternalMasterPasswordServiceAbstraction,
|
InternalMasterPasswordServiceAbstraction,
|
||||||
KeyGenerationServiceAbstraction,
|
KeyGenerationService,
|
||||||
CryptoFunctionServiceAbstraction,
|
CryptoFunctionServiceAbstraction,
|
||||||
EncryptService,
|
EncryptService,
|
||||||
PlatformUtilsServiceAbstraction,
|
PlatformUtilsServiceAbstraction,
|
||||||
|
|||||||
@@ -149,6 +149,10 @@ import { OrganizationBillingApiService } from "@bitwarden/common/billing/service
|
|||||||
import { OrganizationSponsorshipApiService } from "@bitwarden/common/billing/services/organization/organization-sponsorship-api.service";
|
import { OrganizationSponsorshipApiService } from "@bitwarden/common/billing/services/organization/organization-sponsorship-api.service";
|
||||||
import { OrganizationBillingService } from "@bitwarden/common/billing/services/organization-billing.service";
|
import { OrganizationBillingService } from "@bitwarden/common/billing/services/organization-billing.service";
|
||||||
import { TaxService } from "@bitwarden/common/billing/services/tax.service";
|
import { TaxService } from "@bitwarden/common/billing/services/tax.service";
|
||||||
|
import {
|
||||||
|
DefaultKeyGenerationService,
|
||||||
|
KeyGenerationService,
|
||||||
|
} from "@bitwarden/common/key-management/crypto";
|
||||||
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
||||||
import { EncryptServiceImplementation } from "@bitwarden/common/key-management/crypto/services/encrypt.service.implementation";
|
import { EncryptServiceImplementation } from "@bitwarden/common/key-management/crypto/services/encrypt.service.implementation";
|
||||||
@@ -184,7 +188,6 @@ import {
|
|||||||
} from "@bitwarden/common/platform/abstractions/environment.service";
|
} from "@bitwarden/common/platform/abstractions/environment.service";
|
||||||
import { FileUploadService as FileUploadServiceAbstraction } from "@bitwarden/common/platform/abstractions/file-upload/file-upload.service";
|
import { FileUploadService as FileUploadServiceAbstraction } from "@bitwarden/common/platform/abstractions/file-upload/file-upload.service";
|
||||||
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
|
import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
import { KeyGenerationService as KeyGenerationServiceAbstraction } from "@bitwarden/common/platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||||
import { MessagingService as MessagingServiceAbstraction } from "@bitwarden/common/platform/abstractions/messaging.service";
|
import { MessagingService as MessagingServiceAbstraction } from "@bitwarden/common/platform/abstractions/messaging.service";
|
||||||
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||||
@@ -222,7 +225,6 @@ import { DefaultBroadcasterService } from "@bitwarden/common/platform/services/d
|
|||||||
import { DefaultEnvironmentService } from "@bitwarden/common/platform/services/default-environment.service";
|
import { DefaultEnvironmentService } from "@bitwarden/common/platform/services/default-environment.service";
|
||||||
import { DefaultServerSettingsService } from "@bitwarden/common/platform/services/default-server-settings.service";
|
import { DefaultServerSettingsService } from "@bitwarden/common/platform/services/default-server-settings.service";
|
||||||
import { FileUploadService } from "@bitwarden/common/platform/services/file-upload/file-upload.service";
|
import { FileUploadService } from "@bitwarden/common/platform/services/file-upload/file-upload.service";
|
||||||
import { KeyGenerationService } from "@bitwarden/common/platform/services/key-generation.service";
|
|
||||||
import { MigrationBuilderService } from "@bitwarden/common/platform/services/migration-builder.service";
|
import { MigrationBuilderService } from "@bitwarden/common/platform/services/migration-builder.service";
|
||||||
import { MigrationRunner } from "@bitwarden/common/platform/services/migration-runner";
|
import { MigrationRunner } from "@bitwarden/common/platform/services/migration-runner";
|
||||||
import { DefaultSdkService } from "@bitwarden/common/platform/services/sdk/default-sdk.service";
|
import { DefaultSdkService } from "@bitwarden/common/platform/services/sdk/default-sdk.service";
|
||||||
@@ -660,15 +662,15 @@ const safeProviders: SafeProvider[] = [
|
|||||||
GlobalStateProvider,
|
GlobalStateProvider,
|
||||||
SUPPORTS_SECURE_STORAGE,
|
SUPPORTS_SECURE_STORAGE,
|
||||||
SECURE_STORAGE,
|
SECURE_STORAGE,
|
||||||
KeyGenerationServiceAbstraction,
|
KeyGenerationService,
|
||||||
EncryptService,
|
EncryptService,
|
||||||
LogService,
|
LogService,
|
||||||
LOGOUT_CALLBACK,
|
LOGOUT_CALLBACK,
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
safeProvider({
|
safeProvider({
|
||||||
provide: KeyGenerationServiceAbstraction,
|
provide: KeyGenerationService,
|
||||||
useClass: KeyGenerationService,
|
useClass: DefaultKeyGenerationService,
|
||||||
deps: [CryptoFunctionServiceAbstraction],
|
deps: [CryptoFunctionServiceAbstraction],
|
||||||
}),
|
}),
|
||||||
safeProvider({
|
safeProvider({
|
||||||
@@ -677,7 +679,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
deps: [
|
deps: [
|
||||||
PinServiceAbstraction,
|
PinServiceAbstraction,
|
||||||
InternalMasterPasswordServiceAbstraction,
|
InternalMasterPasswordServiceAbstraction,
|
||||||
KeyGenerationServiceAbstraction,
|
KeyGenerationService,
|
||||||
CryptoFunctionServiceAbstraction,
|
CryptoFunctionServiceAbstraction,
|
||||||
EncryptService,
|
EncryptService,
|
||||||
PlatformUtilsServiceAbstraction,
|
PlatformUtilsServiceAbstraction,
|
||||||
@@ -767,7 +769,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
deps: [
|
deps: [
|
||||||
KeyService,
|
KeyService,
|
||||||
I18nServiceAbstraction,
|
I18nServiceAbstraction,
|
||||||
KeyGenerationServiceAbstraction,
|
KeyGenerationService,
|
||||||
SendStateProviderAbstraction,
|
SendStateProviderAbstraction,
|
||||||
EncryptService,
|
EncryptService,
|
||||||
],
|
],
|
||||||
@@ -1013,7 +1015,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
deps: [
|
deps: [
|
||||||
StateProvider,
|
StateProvider,
|
||||||
StateServiceAbstraction,
|
StateServiceAbstraction,
|
||||||
KeyGenerationServiceAbstraction,
|
KeyGenerationService,
|
||||||
EncryptService,
|
EncryptService,
|
||||||
LogService,
|
LogService,
|
||||||
CryptoFunctionServiceAbstraction,
|
CryptoFunctionServiceAbstraction,
|
||||||
@@ -1035,7 +1037,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
TokenServiceAbstraction,
|
TokenServiceAbstraction,
|
||||||
LogService,
|
LogService,
|
||||||
OrganizationServiceAbstraction,
|
OrganizationServiceAbstraction,
|
||||||
KeyGenerationServiceAbstraction,
|
KeyGenerationService,
|
||||||
LOGOUT_CALLBACK,
|
LOGOUT_CALLBACK,
|
||||||
StateProvider,
|
StateProvider,
|
||||||
],
|
],
|
||||||
@@ -1194,7 +1196,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
provide: DeviceTrustServiceAbstraction,
|
provide: DeviceTrustServiceAbstraction,
|
||||||
useClass: DeviceTrustService,
|
useClass: DeviceTrustService,
|
||||||
deps: [
|
deps: [
|
||||||
KeyGenerationServiceAbstraction,
|
KeyGenerationService,
|
||||||
CryptoFunctionServiceAbstraction,
|
CryptoFunctionServiceAbstraction,
|
||||||
KeyService,
|
KeyService,
|
||||||
EncryptService,
|
EncryptService,
|
||||||
@@ -1230,7 +1232,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
CryptoFunctionServiceAbstraction,
|
CryptoFunctionServiceAbstraction,
|
||||||
EncryptService,
|
EncryptService,
|
||||||
KdfConfigService,
|
KdfConfigService,
|
||||||
KeyGenerationServiceAbstraction,
|
KeyGenerationService,
|
||||||
LogService,
|
LogService,
|
||||||
StateProvider,
|
StateProvider,
|
||||||
],
|
],
|
||||||
|
|||||||
2
libs/common/src/key-management/crypto/index.ts
Normal file
2
libs/common/src/key-management/crypto/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export { KeyGenerationService } from "./key-generation/key-generation.service";
|
||||||
|
export { DefaultKeyGenerationService } from "./key-generation/default-key-generation.service";
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
// FIXME: Update this file to be type safe and remove this and next line
|
||||||
|
// @ts-strict-ignore
|
||||||
|
|
||||||
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||||
|
// eslint-disable-next-line no-restricted-imports
|
||||||
|
import { KdfConfig } from "@bitwarden/key-management";
|
||||||
|
import { PureCrypto } from "@bitwarden/sdk-internal";
|
||||||
|
|
||||||
|
import { SdkLoadService } from "../../../platform/abstractions/sdk/sdk-load.service";
|
||||||
|
import { EncryptionType } from "../../../platform/enums";
|
||||||
|
import { Utils } from "../../../platform/misc/utils";
|
||||||
|
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||||
|
import { CsprngArray } from "../../../types/csprng";
|
||||||
|
import { CryptoFunctionService } from "../abstractions/crypto-function.service";
|
||||||
|
|
||||||
|
import { KeyGenerationService } from "./key-generation.service";
|
||||||
|
|
||||||
|
export class DefaultKeyGenerationService implements KeyGenerationService {
|
||||||
|
constructor(private cryptoFunctionService: CryptoFunctionService) {}
|
||||||
|
|
||||||
|
async createKey(bitLength: 256 | 512): Promise<SymmetricCryptoKey> {
|
||||||
|
const key = await this.cryptoFunctionService.aesGenerateKey(bitLength);
|
||||||
|
return new SymmetricCryptoKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
async createKeyWithPurpose(
|
||||||
|
bitLength: 128 | 192 | 256 | 512,
|
||||||
|
purpose: string,
|
||||||
|
salt?: string,
|
||||||
|
): Promise<{ salt: string; material: CsprngArray; derivedKey: SymmetricCryptoKey }> {
|
||||||
|
if (salt == null) {
|
||||||
|
const bytes = await this.cryptoFunctionService.randomBytes(32);
|
||||||
|
salt = Utils.fromBufferToUtf8(bytes);
|
||||||
|
}
|
||||||
|
const material = await this.cryptoFunctionService.aesGenerateKey(bitLength);
|
||||||
|
const key = await this.cryptoFunctionService.hkdf(material, salt, purpose, 64, "sha256");
|
||||||
|
return { salt, material, derivedKey: new SymmetricCryptoKey(key) };
|
||||||
|
}
|
||||||
|
|
||||||
|
async deriveKeyFromMaterial(
|
||||||
|
material: CsprngArray,
|
||||||
|
salt: string,
|
||||||
|
purpose: string,
|
||||||
|
): Promise<SymmetricCryptoKey> {
|
||||||
|
const key = await this.cryptoFunctionService.hkdf(material, salt, purpose, 64, "sha256");
|
||||||
|
return new SymmetricCryptoKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
async deriveKeyFromPassword(
|
||||||
|
password: string | Uint8Array,
|
||||||
|
salt: string | Uint8Array,
|
||||||
|
kdfConfig: KdfConfig,
|
||||||
|
): Promise<SymmetricCryptoKey> {
|
||||||
|
if (typeof password === "string") {
|
||||||
|
password = new TextEncoder().encode(password);
|
||||||
|
}
|
||||||
|
if (typeof salt === "string") {
|
||||||
|
salt = new TextEncoder().encode(salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
await SdkLoadService.Ready;
|
||||||
|
return new SymmetricCryptoKey(
|
||||||
|
PureCrypto.derive_kdf_material(password, salt, kdfConfig.toSdkConfig()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async stretchKey(key: SymmetricCryptoKey): Promise<SymmetricCryptoKey> {
|
||||||
|
// The key to be stretched is actually usually the output of a KDF, and not actually meant for AesCbc256_B64 encryption,
|
||||||
|
// but has the same key length. Only 256-bit key materials should be stretched.
|
||||||
|
if (key.inner().type != EncryptionType.AesCbc256_B64) {
|
||||||
|
throw new Error("Key passed into stretchKey is not a 256-bit key.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const newKey = new Uint8Array(64);
|
||||||
|
// Master key and pin key are always 32 bytes
|
||||||
|
const encKey = await this.cryptoFunctionService.hkdfExpand(
|
||||||
|
key.inner().encryptionKey,
|
||||||
|
"enc",
|
||||||
|
32,
|
||||||
|
"sha256",
|
||||||
|
);
|
||||||
|
const macKey = await this.cryptoFunctionService.hkdfExpand(
|
||||||
|
key.inner().encryptionKey,
|
||||||
|
"mac",
|
||||||
|
32,
|
||||||
|
"sha256",
|
||||||
|
);
|
||||||
|
|
||||||
|
newKey.set(new Uint8Array(encKey));
|
||||||
|
newKey.set(new Uint8Array(macKey), 32);
|
||||||
|
|
||||||
|
return new SymmetricCryptoKey(newKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,21 +4,21 @@ import { mock } from "jest-mock-extended";
|
|||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line no-restricted-imports
|
||||||
import { PBKDF2KdfConfig, Argon2KdfConfig } from "@bitwarden/key-management";
|
import { PBKDF2KdfConfig, Argon2KdfConfig } from "@bitwarden/key-management";
|
||||||
|
|
||||||
import { CryptoFunctionService } from "../../key-management/crypto/abstractions/crypto-function.service";
|
import { SdkLoadService } from "../../../platform/abstractions/sdk/sdk-load.service";
|
||||||
import { CsprngArray } from "../../types/csprng";
|
import { EncryptionType } from "../../../platform/enums";
|
||||||
import { SdkLoadService } from "../abstractions/sdk/sdk-load.service";
|
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||||
import { EncryptionType } from "../enums";
|
import { CsprngArray } from "../../../types/csprng";
|
||||||
import { SymmetricCryptoKey } from "../models/domain/symmetric-crypto-key";
|
import { CryptoFunctionService } from "../abstractions/crypto-function.service";
|
||||||
|
|
||||||
import { KeyGenerationService } from "./key-generation.service";
|
import { DefaultKeyGenerationService } from "./default-key-generation.service";
|
||||||
|
|
||||||
describe("KeyGenerationService", () => {
|
describe("KeyGenerationService", () => {
|
||||||
let sut: KeyGenerationService;
|
let sut: DefaultKeyGenerationService;
|
||||||
|
|
||||||
const cryptoFunctionService = mock<CryptoFunctionService>();
|
const cryptoFunctionService = mock<CryptoFunctionService>();
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
sut = new KeyGenerationService(cryptoFunctionService);
|
sut = new DefaultKeyGenerationService(cryptoFunctionService);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("createKey", () => {
|
describe("createKey", () => {
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||||
|
// eslint-disable-next-line no-restricted-imports
|
||||||
|
import { KdfConfig } from "@bitwarden/key-management";
|
||||||
|
|
||||||
|
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||||
|
import { CsprngArray } from "../../../types/csprng";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated This is a low-level cryptographic service. New functionality should not be built
|
||||||
|
* on top of it, and instead should be built in the sdk.
|
||||||
|
*/
|
||||||
|
export abstract class KeyGenerationService {
|
||||||
|
/**
|
||||||
|
* Generates a key of the given length suitable for use in AES encryption
|
||||||
|
*
|
||||||
|
* @deprecated WARNING: DO NOT USE THIS FOR NEW CODE. Direct generation and handling of keys should only be done in the SDK,
|
||||||
|
* as memory safety cannot be ensured in a JS context.
|
||||||
|
*
|
||||||
|
* @param bitLength Length of key.
|
||||||
|
* 256 bits = 32 bytes
|
||||||
|
* 512 bits = 64 bytes
|
||||||
|
* @returns Generated key.
|
||||||
|
*/
|
||||||
|
abstract createKey(bitLength: 256 | 512): Promise<SymmetricCryptoKey>;
|
||||||
|
/**
|
||||||
|
* Generates key material from CSPRNG and derives a 64 byte key from it.
|
||||||
|
* Uses HKDF, see {@link https://datatracker.ietf.org/doc/html/rfc5869 RFC 5869}
|
||||||
|
* for details.
|
||||||
|
*
|
||||||
|
* @deprecated HAZMAT WARNING: DO NOT USE THIS FOR NEW CODE. This is a low-level cryptographic function.
|
||||||
|
* New functionality should not be built on top of it, and instead should be built in the sdk.
|
||||||
|
*
|
||||||
|
* @param bitLength Length of key material.
|
||||||
|
* @param purpose Purpose for the key derivation function.
|
||||||
|
* Different purposes results in different keys, even with the same material.
|
||||||
|
* @param salt Optional. If not provided will be generated from CSPRNG.
|
||||||
|
* @returns An object containing the salt, key material, and derived key.
|
||||||
|
*/
|
||||||
|
abstract createKeyWithPurpose(
|
||||||
|
bitLength: 128 | 192 | 256 | 512,
|
||||||
|
purpose: string,
|
||||||
|
salt?: string,
|
||||||
|
): Promise<{ salt: string; material: CsprngArray; derivedKey: SymmetricCryptoKey }>;
|
||||||
|
/**
|
||||||
|
* Derives a 64 byte key from key material.
|
||||||
|
*
|
||||||
|
* @deprecated HAZMAT WARNING: DO NOT USE THIS FOR NEW CODE. This is a low-level cryptographic function.
|
||||||
|
* New functionality should not be built on top of it, and instead should be built in the sdk.
|
||||||
|
*
|
||||||
|
* @remark The key material should be generated from {@link createKey}, or {@link createKeyWithPurpose}.
|
||||||
|
* Uses HKDF, see {@link https://datatracker.ietf.org/doc/html/rfc5869 RFC 5869} for details.
|
||||||
|
* @param material key material.
|
||||||
|
* @param salt Salt for the key derivation function.
|
||||||
|
* @param purpose Purpose for the key derivation function.
|
||||||
|
* Different purposes results in different keys, even with the same material.
|
||||||
|
* @returns 64 byte derived key.
|
||||||
|
*/
|
||||||
|
abstract deriveKeyFromMaterial(
|
||||||
|
material: CsprngArray,
|
||||||
|
salt: string,
|
||||||
|
purpose: string,
|
||||||
|
): Promise<SymmetricCryptoKey>;
|
||||||
|
/**
|
||||||
|
* Derives a 32 byte key from a password using a key derivation function.
|
||||||
|
*
|
||||||
|
* @deprecated HAZMAT WARNING: DO NOT USE THIS FOR NEW CODE. This is a low-level cryptographic function.
|
||||||
|
* New functionality should not be built on top of it, and instead should be built in the sdk.
|
||||||
|
*
|
||||||
|
* @param password Password to derive the key from.
|
||||||
|
* @param salt Salt for the key derivation function.
|
||||||
|
* @param kdfConfig Configuration for the key derivation function.
|
||||||
|
* @returns 32 byte derived key.
|
||||||
|
*/
|
||||||
|
abstract deriveKeyFromPassword(
|
||||||
|
password: string | Uint8Array,
|
||||||
|
salt: string | Uint8Array,
|
||||||
|
kdfConfig: KdfConfig,
|
||||||
|
): Promise<SymmetricCryptoKey>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derives a 64 byte key from a 32 byte key using a key derivation function.
|
||||||
|
*
|
||||||
|
* @deprecated HAZMAT WARNING: DO NOT USE THIS FOR NEW CODE. This is a low-level cryptographic function.
|
||||||
|
* New functionality should not be built on top of it, and instead should be built in the sdk.
|
||||||
|
*
|
||||||
|
* @param key 32 byte key.
|
||||||
|
* @returns 64 byte derived key.
|
||||||
|
*/
|
||||||
|
abstract stretchKey(key: SymmetricCryptoKey): Promise<SymmetricCryptoKey>;
|
||||||
|
}
|
||||||
@@ -20,7 +20,6 @@ import {
|
|||||||
import { AppIdService } from "../../../platform/abstractions/app-id.service";
|
import { AppIdService } from "../../../platform/abstractions/app-id.service";
|
||||||
import { ConfigService } from "../../../platform/abstractions/config/config.service";
|
import { ConfigService } from "../../../platform/abstractions/config/config.service";
|
||||||
import { I18nService } from "../../../platform/abstractions/i18n.service";
|
import { I18nService } from "../../../platform/abstractions/i18n.service";
|
||||||
import { KeyGenerationService } from "../../../platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "../../../platform/abstractions/log.service";
|
import { LogService } from "../../../platform/abstractions/log.service";
|
||||||
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
|
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
|
||||||
import { AbstractStorageService } from "../../../platform/abstractions/storage.service";
|
import { AbstractStorageService } from "../../../platform/abstractions/storage.service";
|
||||||
@@ -30,6 +29,7 @@ import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-cr
|
|||||||
import { DEVICE_TRUST_DISK_LOCAL, StateProvider, UserKeyDefinition } from "../../../platform/state";
|
import { DEVICE_TRUST_DISK_LOCAL, StateProvider, UserKeyDefinition } from "../../../platform/state";
|
||||||
import { UserId } from "../../../types/guid";
|
import { UserId } from "../../../types/guid";
|
||||||
import { UserKey, DeviceKey } from "../../../types/key";
|
import { UserKey, DeviceKey } from "../../../types/key";
|
||||||
|
import { KeyGenerationService } from "../../crypto";
|
||||||
import { CryptoFunctionService } from "../../crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "../../crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "../../crypto/abstractions/encrypt.service";
|
import { EncryptService } from "../../crypto/abstractions/encrypt.service";
|
||||||
import { EncString } from "../../crypto/models/enc-string";
|
import { EncString } from "../../crypto/models/enc-string";
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import { DeviceType } from "../../../enums";
|
|||||||
import { AppIdService } from "../../../platform/abstractions/app-id.service";
|
import { AppIdService } from "../../../platform/abstractions/app-id.service";
|
||||||
import { ConfigService } from "../../../platform/abstractions/config/config.service";
|
import { ConfigService } from "../../../platform/abstractions/config/config.service";
|
||||||
import { I18nService } from "../../../platform/abstractions/i18n.service";
|
import { I18nService } from "../../../platform/abstractions/i18n.service";
|
||||||
import { KeyGenerationService } from "../../../platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "../../../platform/abstractions/log.service";
|
import { LogService } from "../../../platform/abstractions/log.service";
|
||||||
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
|
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
|
||||||
import { AbstractStorageService } from "../../../platform/abstractions/storage.service";
|
import { AbstractStorageService } from "../../../platform/abstractions/storage.service";
|
||||||
@@ -37,6 +36,7 @@ import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-cr
|
|||||||
import { CsprngArray } from "../../../types/csprng";
|
import { CsprngArray } from "../../../types/csprng";
|
||||||
import { UserId } from "../../../types/guid";
|
import { UserId } from "../../../types/guid";
|
||||||
import { DeviceKey, UserKey } from "../../../types/key";
|
import { DeviceKey, UserKey } from "../../../types/key";
|
||||||
|
import { KeyGenerationService } from "../../crypto";
|
||||||
import { CryptoFunctionService } from "../../crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "../../crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "../../crypto/abstractions/encrypt.service";
|
import { EncryptService } from "../../crypto/abstractions/encrypt.service";
|
||||||
import { EncString } from "../../crypto/models/enc-string";
|
import { EncString } from "../../crypto/models/enc-string";
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ import { TokenService } from "../../../auth/services/token.service";
|
|||||||
import { LogService } from "../../../platform/abstractions/log.service";
|
import { LogService } from "../../../platform/abstractions/log.service";
|
||||||
import { Utils } from "../../../platform/misc/utils";
|
import { Utils } from "../../../platform/misc/utils";
|
||||||
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||||
import { KeyGenerationService } from "../../../platform/services/key-generation.service";
|
|
||||||
import { OrganizationId, UserId } from "../../../types/guid";
|
import { OrganizationId, UserId } from "../../../types/guid";
|
||||||
import { MasterKey, UserKey } from "../../../types/key";
|
import { MasterKey, UserKey } from "../../../types/key";
|
||||||
|
import { KeyGenerationService } from "../../crypto";
|
||||||
import { EncString } from "../../crypto/models/enc-string";
|
import { EncString } from "../../crypto/models/enc-string";
|
||||||
import { FakeMasterPasswordService } from "../../master-password/services/fake-master-password.service";
|
import { FakeMasterPasswordService } from "../../master-password/services/fake-master-password.service";
|
||||||
import { KeyConnectorUserKeyRequest } from "../models/key-connector-user-key.request";
|
import { KeyConnectorUserKeyRequest } from "../models/key-connector-user-key.request";
|
||||||
|
|||||||
@@ -23,13 +23,13 @@ import { Organization } from "../../../admin-console/models/domain/organization"
|
|||||||
import { TokenService } from "../../../auth/abstractions/token.service";
|
import { TokenService } from "../../../auth/abstractions/token.service";
|
||||||
import { IdentityTokenResponse } from "../../../auth/models/response/identity-token.response";
|
import { IdentityTokenResponse } from "../../../auth/models/response/identity-token.response";
|
||||||
import { KeysRequest } from "../../../models/request/keys.request";
|
import { KeysRequest } from "../../../models/request/keys.request";
|
||||||
import { KeyGenerationService } from "../../../platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "../../../platform/abstractions/log.service";
|
import { LogService } from "../../../platform/abstractions/log.service";
|
||||||
import { Utils } from "../../../platform/misc/utils";
|
import { Utils } from "../../../platform/misc/utils";
|
||||||
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||||
import { KEY_CONNECTOR_DISK, StateProvider, UserKeyDefinition } from "../../../platform/state";
|
import { KEY_CONNECTOR_DISK, StateProvider, UserKeyDefinition } from "../../../platform/state";
|
||||||
import { UserId } from "../../../types/guid";
|
import { UserId } from "../../../types/guid";
|
||||||
import { MasterKey } from "../../../types/key";
|
import { MasterKey } from "../../../types/key";
|
||||||
|
import { KeyGenerationService } from "../../crypto";
|
||||||
import { InternalMasterPasswordServiceAbstraction } from "../../master-password/abstractions/master-password.service.abstraction";
|
import { InternalMasterPasswordServiceAbstraction } from "../../master-password/abstractions/master-password.service.abstraction";
|
||||||
import { KeyConnectorService as KeyConnectorServiceAbstraction } from "../abstractions/key-connector.service";
|
import { KeyConnectorService as KeyConnectorServiceAbstraction } from "../abstractions/key-connector.service";
|
||||||
import { KeyConnectorUserKeyRequest } from "../models/key-connector-user-key.request";
|
import { KeyConnectorUserKeyRequest } from "../models/key-connector-user-key.request";
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ import {
|
|||||||
mockAccountServiceWith,
|
mockAccountServiceWith,
|
||||||
} from "../../../../spec";
|
} from "../../../../spec";
|
||||||
import { ForceSetPasswordReason } from "../../../auth/models/domain/force-set-password-reason";
|
import { ForceSetPasswordReason } from "../../../auth/models/domain/force-set-password-reason";
|
||||||
import { KeyGenerationService } from "../../../platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "../../../platform/abstractions/log.service";
|
import { LogService } from "../../../platform/abstractions/log.service";
|
||||||
import { StateService } from "../../../platform/abstractions/state.service";
|
import { StateService } from "../../../platform/abstractions/state.service";
|
||||||
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||||
import { StateProvider } from "../../../platform/state";
|
import { StateProvider } from "../../../platform/state";
|
||||||
import { UserId } from "../../../types/guid";
|
import { UserId } from "../../../types/guid";
|
||||||
import { MasterKey, UserKey } from "../../../types/key";
|
import { MasterKey, UserKey } from "../../../types/key";
|
||||||
|
import { KeyGenerationService } from "../../crypto";
|
||||||
import { CryptoFunctionService } from "../../crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "../../crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "../../crypto/abstractions/encrypt.service";
|
import { EncryptService } from "../../crypto/abstractions/encrypt.service";
|
||||||
import { EncString } from "../../crypto/models/enc-string";
|
import { EncString } from "../../crypto/models/enc-string";
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import { KdfConfig } from "@bitwarden/key-management";
|
|||||||
import { PureCrypto } from "@bitwarden/sdk-internal";
|
import { PureCrypto } from "@bitwarden/sdk-internal";
|
||||||
|
|
||||||
import { ForceSetPasswordReason } from "../../../auth/models/domain/force-set-password-reason";
|
import { ForceSetPasswordReason } from "../../../auth/models/domain/force-set-password-reason";
|
||||||
import { KeyGenerationService } from "../../../platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "../../../platform/abstractions/log.service";
|
import { LogService } from "../../../platform/abstractions/log.service";
|
||||||
import { StateService } from "../../../platform/abstractions/state.service";
|
import { StateService } from "../../../platform/abstractions/state.service";
|
||||||
import { EncryptionType } from "../../../platform/enums";
|
import { EncryptionType } from "../../../platform/enums";
|
||||||
@@ -24,6 +23,7 @@ import {
|
|||||||
} from "../../../platform/state";
|
} from "../../../platform/state";
|
||||||
import { UserId } from "../../../types/guid";
|
import { UserId } from "../../../types/guid";
|
||||||
import { MasterKey, UserKey } from "../../../types/key";
|
import { MasterKey, UserKey } from "../../../types/key";
|
||||||
|
import { KeyGenerationService } from "../../crypto";
|
||||||
import { CryptoFunctionService } from "../../crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "../../crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "../../crypto/abstractions/encrypt.service";
|
import { EncryptService } from "../../crypto/abstractions/encrypt.service";
|
||||||
import { EncryptedString, EncString } from "../../crypto/models/enc-string";
|
import { EncryptedString, EncString } from "../../crypto/models/enc-string";
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ import { AccountService } from "../../auth/abstractions/account.service";
|
|||||||
import { CryptoFunctionService } from "../../key-management/crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "../../key-management/crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "../../key-management/crypto/abstractions/encrypt.service";
|
import { EncryptService } from "../../key-management/crypto/abstractions/encrypt.service";
|
||||||
import { EncString, EncryptedString } from "../../key-management/crypto/models/enc-string";
|
import { EncString, EncryptedString } from "../../key-management/crypto/models/enc-string";
|
||||||
import { KeyGenerationService } from "../../platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "../../platform/abstractions/log.service";
|
import { LogService } from "../../platform/abstractions/log.service";
|
||||||
import { PIN_DISK, PIN_MEMORY, StateProvider, UserKeyDefinition } from "../../platform/state";
|
import { PIN_DISK, PIN_MEMORY, StateProvider, UserKeyDefinition } from "../../platform/state";
|
||||||
import { UserId } from "../../types/guid";
|
import { UserId } from "../../types/guid";
|
||||||
import { PinKey, UserKey } from "../../types/key";
|
import { PinKey, UserKey } from "../../types/key";
|
||||||
|
import { KeyGenerationService } from "../crypto";
|
||||||
|
|
||||||
import { PinServiceAbstraction } from "./pin.service.abstraction";
|
import { PinServiceAbstraction } from "./pin.service.abstraction";
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ import { mock } from "jest-mock-extended";
|
|||||||
import { DEFAULT_KDF_CONFIG, KdfConfigService } from "@bitwarden/key-management";
|
import { DEFAULT_KDF_CONFIG, KdfConfigService } from "@bitwarden/key-management";
|
||||||
|
|
||||||
import { FakeAccountService, FakeStateProvider, mockAccountServiceWith } from "../../../spec";
|
import { FakeAccountService, FakeStateProvider, mockAccountServiceWith } from "../../../spec";
|
||||||
import { KeyGenerationService } from "../../platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "../../platform/abstractions/log.service";
|
import { LogService } from "../../platform/abstractions/log.service";
|
||||||
import { Utils } from "../../platform/misc/utils";
|
import { Utils } from "../../platform/misc/utils";
|
||||||
import { SymmetricCryptoKey } from "../../platform/models/domain/symmetric-crypto-key";
|
import { SymmetricCryptoKey } from "../../platform/models/domain/symmetric-crypto-key";
|
||||||
import { UserId } from "../../types/guid";
|
import { UserId } from "../../types/guid";
|
||||||
import { PinKey, UserKey } from "../../types/key";
|
import { PinKey, UserKey } from "../../types/key";
|
||||||
|
import { KeyGenerationService } from "../crypto";
|
||||||
import { CryptoFunctionService } from "../crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "../crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "../crypto/abstractions/encrypt.service";
|
import { EncryptService } from "../crypto/abstractions/encrypt.service";
|
||||||
import { EncString } from "../crypto/models/enc-string";
|
import { EncString } from "../crypto/models/enc-string";
|
||||||
|
|||||||
@@ -1,66 +1,2 @@
|
|||||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
/** Temporary re-export. This should not be used for new imports */
|
||||||
// eslint-disable-next-line no-restricted-imports
|
export { KeyGenerationService } from "../../key-management/crypto/key-generation/key-generation.service";
|
||||||
import { KdfConfig } from "@bitwarden/key-management";
|
|
||||||
|
|
||||||
import { CsprngArray } from "../../types/csprng";
|
|
||||||
import { SymmetricCryptoKey } from "../models/domain/symmetric-crypto-key";
|
|
||||||
|
|
||||||
export abstract class KeyGenerationService {
|
|
||||||
/**
|
|
||||||
* Generates a key of the given length suitable for use in AES encryption
|
|
||||||
* @param bitLength Length of key.
|
|
||||||
* 256 bits = 32 bytes
|
|
||||||
* 512 bits = 64 bytes
|
|
||||||
* @returns Generated key.
|
|
||||||
*/
|
|
||||||
abstract createKey(bitLength: 256 | 512): Promise<SymmetricCryptoKey>;
|
|
||||||
/**
|
|
||||||
* Generates key material from CSPRNG and derives a 64 byte key from it.
|
|
||||||
* Uses HKDF, see {@link https://datatracker.ietf.org/doc/html/rfc5869 RFC 5869}
|
|
||||||
* for details.
|
|
||||||
* @param bitLength Length of key material.
|
|
||||||
* @param purpose Purpose for the key derivation function.
|
|
||||||
* Different purposes results in different keys, even with the same material.
|
|
||||||
* @param salt Optional. If not provided will be generated from CSPRNG.
|
|
||||||
* @returns An object containing the salt, key material, and derived key.
|
|
||||||
*/
|
|
||||||
abstract createKeyWithPurpose(
|
|
||||||
bitLength: 128 | 192 | 256 | 512,
|
|
||||||
purpose: string,
|
|
||||||
salt?: string,
|
|
||||||
): Promise<{ salt: string; material: CsprngArray; derivedKey: SymmetricCryptoKey }>;
|
|
||||||
/**
|
|
||||||
* Derives a 64 byte key from key material.
|
|
||||||
* @remark The key material should be generated from {@link createKey}, or {@link createKeyWithPurpose}.
|
|
||||||
* Uses HKDF, see {@link https://datatracker.ietf.org/doc/html/rfc5869 RFC 5869} for details.
|
|
||||||
* @param material key material.
|
|
||||||
* @param salt Salt for the key derivation function.
|
|
||||||
* @param purpose Purpose for the key derivation function.
|
|
||||||
* Different purposes results in different keys, even with the same material.
|
|
||||||
* @returns 64 byte derived key.
|
|
||||||
*/
|
|
||||||
abstract deriveKeyFromMaterial(
|
|
||||||
material: CsprngArray,
|
|
||||||
salt: string,
|
|
||||||
purpose: string,
|
|
||||||
): Promise<SymmetricCryptoKey>;
|
|
||||||
/**
|
|
||||||
* Derives a 32 byte key from a password using a key derivation function.
|
|
||||||
* @param password Password to derive the key from.
|
|
||||||
* @param salt Salt for the key derivation function.
|
|
||||||
* @param kdfConfig Configuration for the key derivation function.
|
|
||||||
* @returns 32 byte derived key.
|
|
||||||
*/
|
|
||||||
abstract deriveKeyFromPassword(
|
|
||||||
password: string | Uint8Array,
|
|
||||||
salt: string | Uint8Array,
|
|
||||||
kdfConfig: KdfConfig,
|
|
||||||
): Promise<SymmetricCryptoKey>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Derives a 64 byte key from a 32 byte key using a key derivation function.
|
|
||||||
* @param key 32 byte key.
|
|
||||||
* @returns 64 byte derived key.
|
|
||||||
*/
|
|
||||||
abstract stretchKey(key: SymmetricCryptoKey): Promise<SymmetricCryptoKey>;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,92 +1,2 @@
|
|||||||
// FIXME: Update this file to be type safe and remove this and next line
|
/** Temporary re-export. This should not be used for new imports */
|
||||||
// @ts-strict-ignore
|
export { DefaultKeyGenerationService as KeyGenerationService } from "../../key-management/crypto/key-generation/default-key-generation.service";
|
||||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
|
||||||
// eslint-disable-next-line no-restricted-imports
|
|
||||||
import { KdfConfig } from "@bitwarden/key-management";
|
|
||||||
import { PureCrypto } from "@bitwarden/sdk-internal";
|
|
||||||
|
|
||||||
import { CryptoFunctionService } from "../../key-management/crypto/abstractions/crypto-function.service";
|
|
||||||
import { CsprngArray } from "../../types/csprng";
|
|
||||||
import { KeyGenerationService as KeyGenerationServiceAbstraction } from "../abstractions/key-generation.service";
|
|
||||||
import { SdkLoadService } from "../abstractions/sdk/sdk-load.service";
|
|
||||||
import { EncryptionType } from "../enums";
|
|
||||||
import { Utils } from "../misc/utils";
|
|
||||||
import { SymmetricCryptoKey } from "../models/domain/symmetric-crypto-key";
|
|
||||||
|
|
||||||
export class KeyGenerationService implements KeyGenerationServiceAbstraction {
|
|
||||||
constructor(private cryptoFunctionService: CryptoFunctionService) {}
|
|
||||||
|
|
||||||
async createKey(bitLength: 256 | 512): Promise<SymmetricCryptoKey> {
|
|
||||||
const key = await this.cryptoFunctionService.aesGenerateKey(bitLength);
|
|
||||||
return new SymmetricCryptoKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
async createKeyWithPurpose(
|
|
||||||
bitLength: 128 | 192 | 256 | 512,
|
|
||||||
purpose: string,
|
|
||||||
salt?: string,
|
|
||||||
): Promise<{ salt: string; material: CsprngArray; derivedKey: SymmetricCryptoKey }> {
|
|
||||||
if (salt == null) {
|
|
||||||
const bytes = await this.cryptoFunctionService.randomBytes(32);
|
|
||||||
salt = Utils.fromBufferToUtf8(bytes);
|
|
||||||
}
|
|
||||||
const material = await this.cryptoFunctionService.aesGenerateKey(bitLength);
|
|
||||||
const key = await this.cryptoFunctionService.hkdf(material, salt, purpose, 64, "sha256");
|
|
||||||
return { salt, material, derivedKey: new SymmetricCryptoKey(key) };
|
|
||||||
}
|
|
||||||
|
|
||||||
async deriveKeyFromMaterial(
|
|
||||||
material: CsprngArray,
|
|
||||||
salt: string,
|
|
||||||
purpose: string,
|
|
||||||
): Promise<SymmetricCryptoKey> {
|
|
||||||
const key = await this.cryptoFunctionService.hkdf(material, salt, purpose, 64, "sha256");
|
|
||||||
return new SymmetricCryptoKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
async deriveKeyFromPassword(
|
|
||||||
password: string | Uint8Array,
|
|
||||||
salt: string | Uint8Array,
|
|
||||||
kdfConfig: KdfConfig,
|
|
||||||
): Promise<SymmetricCryptoKey> {
|
|
||||||
if (typeof password === "string") {
|
|
||||||
password = new TextEncoder().encode(password);
|
|
||||||
}
|
|
||||||
if (typeof salt === "string") {
|
|
||||||
salt = new TextEncoder().encode(salt);
|
|
||||||
}
|
|
||||||
|
|
||||||
await SdkLoadService.Ready;
|
|
||||||
return new SymmetricCryptoKey(
|
|
||||||
PureCrypto.derive_kdf_material(password, salt, kdfConfig.toSdkConfig()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async stretchKey(key: SymmetricCryptoKey): Promise<SymmetricCryptoKey> {
|
|
||||||
// The key to be stretched is actually usually the output of a KDF, and not actually meant for AesCbc256_B64 encryption,
|
|
||||||
// but has the same key length. Only 256-bit key materials should be stretched.
|
|
||||||
if (key.inner().type != EncryptionType.AesCbc256_B64) {
|
|
||||||
throw new Error("Key passed into stretchKey is not a 256-bit key.");
|
|
||||||
}
|
|
||||||
|
|
||||||
const newKey = new Uint8Array(64);
|
|
||||||
// Master key and pin key are always 32 bytes
|
|
||||||
const encKey = await this.cryptoFunctionService.hkdfExpand(
|
|
||||||
key.inner().encryptionKey,
|
|
||||||
"enc",
|
|
||||||
32,
|
|
||||||
"sha256",
|
|
||||||
);
|
|
||||||
const macKey = await this.cryptoFunctionService.hkdfExpand(
|
|
||||||
key.inner().encryptionKey,
|
|
||||||
"mac",
|
|
||||||
32,
|
|
||||||
"sha256",
|
|
||||||
);
|
|
||||||
|
|
||||||
newKey.set(new Uint8Array(encKey));
|
|
||||||
newKey.set(new Uint8Array(macKey), 32);
|
|
||||||
|
|
||||||
return new SymmetricCryptoKey(newKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { mock } from "jest-mock-extended";
|
|||||||
import { BehaviorSubject, bufferCount, firstValueFrom, lastValueFrom, of, take } from "rxjs";
|
import { BehaviorSubject, bufferCount, firstValueFrom, lastValueFrom, of, take } from "rxjs";
|
||||||
|
|
||||||
import { EncryptedOrganizationKeyData } from "@bitwarden/common/admin-console/models/data/encrypted-organization-key.data";
|
import { EncryptedOrganizationKeyData } from "@bitwarden/common/admin-console/models/data/encrypted-organization-key.data";
|
||||||
|
import { KeyGenerationService } from "@bitwarden/common/key-management/crypto";
|
||||||
import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
||||||
import {
|
import {
|
||||||
@@ -12,7 +13,6 @@ import { FakeMasterPasswordService } from "@bitwarden/common/key-management/mast
|
|||||||
import { PinServiceAbstraction } from "@bitwarden/common/key-management/pin/pin.service.abstraction";
|
import { PinServiceAbstraction } from "@bitwarden/common/key-management/pin/pin.service.abstraction";
|
||||||
import { VaultTimeoutStringType } from "@bitwarden/common/key-management/vault-timeout";
|
import { VaultTimeoutStringType } from "@bitwarden/common/key-management/vault-timeout";
|
||||||
import { VAULT_TIMEOUT } from "@bitwarden/common/key-management/vault-timeout/services/vault-timeout-settings.state";
|
import { VAULT_TIMEOUT } from "@bitwarden/common/key-management/vault-timeout/services/vault-timeout-settings.state";
|
||||||
import { KeyGenerationService } from "@bitwarden/common/platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||||
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||||
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import { ProfileOrganizationResponse } from "@bitwarden/common/admin-console/mod
|
|||||||
import { ProfileProviderOrganizationResponse } from "@bitwarden/common/admin-console/models/response/profile-provider-organization.response";
|
import { ProfileProviderOrganizationResponse } from "@bitwarden/common/admin-console/models/response/profile-provider-organization.response";
|
||||||
import { ProfileProviderResponse } from "@bitwarden/common/admin-console/models/response/profile-provider.response";
|
import { ProfileProviderResponse } from "@bitwarden/common/admin-console/models/response/profile-provider.response";
|
||||||
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
||||||
|
import { KeyGenerationService } from "@bitwarden/common/key-management/crypto";
|
||||||
import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
||||||
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
||||||
import {
|
import {
|
||||||
@@ -29,7 +30,6 @@ import { InternalMasterPasswordServiceAbstraction } from "@bitwarden/common/key-
|
|||||||
import { PinServiceAbstraction } from "@bitwarden/common/key-management/pin/pin.service.abstraction";
|
import { PinServiceAbstraction } from "@bitwarden/common/key-management/pin/pin.service.abstraction";
|
||||||
import { VaultTimeoutStringType } from "@bitwarden/common/key-management/vault-timeout";
|
import { VaultTimeoutStringType } from "@bitwarden/common/key-management/vault-timeout";
|
||||||
import { VAULT_TIMEOUT } from "@bitwarden/common/key-management/vault-timeout/services/vault-timeout-settings.state";
|
import { VAULT_TIMEOUT } from "@bitwarden/common/key-management/vault-timeout/services/vault-timeout-settings.state";
|
||||||
import { KeyGenerationService } from "@bitwarden/common/platform/abstractions/key-generation.service";
|
|
||||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||||
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||||
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
||||||
|
|||||||
Reference in New Issue
Block a user