Path: blob/main/src/vs/platform/encryption/electron-main/encryptionMainService.ts
3294 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { safeStorage as safeStorageElectron, app } from 'electron';6import { isMacintosh, isWindows } from '../../../base/common/platform.js';7import { KnownStorageProvider, IEncryptionMainService, PasswordStoreCLIOption } from '../common/encryptionService.js';8import { ILogService } from '../../log/common/log.js';910// These APIs are currently only supported in our custom build of electron so11// we need to guard against them not being available.12interface ISafeStorageAdditionalAPIs {13setUsePlainTextEncryption(usePlainText: boolean): void;14getSelectedStorageBackend(): string;15}1617const safeStorage: typeof import('electron').safeStorage & Partial<ISafeStorageAdditionalAPIs> = safeStorageElectron;1819export class EncryptionMainService implements IEncryptionMainService {20_serviceBrand: undefined;2122constructor(23@ILogService private readonly logService: ILogService24) {25// if this commandLine switch is set, the user has opted in to using basic text encryption26if (app.commandLine.getSwitchValue('password-store') === PasswordStoreCLIOption.basic) {27this.logService.trace('[EncryptionMainService] setting usePlainTextEncryption to true...');28safeStorage.setUsePlainTextEncryption?.(true);29this.logService.trace('[EncryptionMainService] set usePlainTextEncryption to true');30}31}3233async encrypt(value: string): Promise<string> {34this.logService.trace('[EncryptionMainService] Encrypting value...');35try {36const result = JSON.stringify(safeStorage.encryptString(value));37this.logService.trace('[EncryptionMainService] Encrypted value.');38return result;39} catch (e) {40this.logService.error(e);41throw e;42}43}4445async decrypt(value: string): Promise<string> {46let parsedValue: { data: string };47try {48parsedValue = JSON.parse(value);49if (!parsedValue.data) {50throw new Error(`[EncryptionMainService] Invalid encrypted value: ${value}`);51}52const bufferToDecrypt = Buffer.from(parsedValue.data);5354this.logService.trace('[EncryptionMainService] Decrypting value...');55const result = safeStorage.decryptString(bufferToDecrypt);56this.logService.trace('[EncryptionMainService] Decrypted value.');57return result;58} catch (e) {59this.logService.error(e);60throw e;61}62}6364isEncryptionAvailable(): Promise<boolean> {65this.logService.trace('[EncryptionMainService] Checking if encryption is available...');66const result = safeStorage.isEncryptionAvailable();67this.logService.trace('[EncryptionMainService] Encryption is available: ', result);68return Promise.resolve(result);69}7071getKeyStorageProvider(): Promise<KnownStorageProvider> {72if (isWindows) {73return Promise.resolve(KnownStorageProvider.dplib);74}75if (isMacintosh) {76return Promise.resolve(KnownStorageProvider.keychainAccess);77}78if (safeStorage.getSelectedStorageBackend) {79try {80this.logService.trace('[EncryptionMainService] Getting selected storage backend...');81const result = safeStorage.getSelectedStorageBackend() as KnownStorageProvider;82this.logService.trace('[EncryptionMainService] Selected storage backend: ', result);83return Promise.resolve(result);84} catch (e) {85this.logService.error(e);86}87}88return Promise.resolve(KnownStorageProvider.unknown);89}9091async setUsePlainTextEncryption(): Promise<void> {92if (isWindows) {93throw new Error('Setting plain text encryption is not supported on Windows.');94}9596if (isMacintosh) {97throw new Error('Setting plain text encryption is not supported on macOS.');98}99100if (!safeStorage.setUsePlainTextEncryption) {101throw new Error('Setting plain text encryption is not supported.');102}103104this.logService.trace('[EncryptionMainService] Setting usePlainTextEncryption to true...');105safeStorage.setUsePlainTextEncryption(true);106this.logService.trace('[EncryptionMainService] Set usePlainTextEncryption to true');107}108}109110111