diff --git a/src/app/services.module.ts b/src/app/services.module.ts index d222a4a169..783eea73e7 100644 --- a/src/app/services.module.ts +++ b/src/app/services.module.ts @@ -129,7 +129,7 @@ const environmentService = new EnvironmentService(apiService, storageService, no const eventService = new EventService(storageService, apiService, userService, cipherService); const systemService = new SystemService(storageService, vaultTimeoutService, messagingService, platformUtilsService, null); -const nativeMessagingService = new NativeMessagingService(cryptoService, platformUtilsService, logService); +const nativeMessagingService = new NativeMessagingService(cryptoFunctionService, cryptoService, platformUtilsService, logService); const analytics = new Analytics(window, () => isDev(), platformUtilsService, storageService, appIdService); containerService.attachToGlobal(window); diff --git a/src/services/nativeMessaging.service.ts b/src/services/nativeMessaging.service.ts index e490fc3334..07cdb12af4 100644 --- a/src/services/nativeMessaging.service.ts +++ b/src/services/nativeMessaging.service.ts @@ -1,22 +1,36 @@ import { ipcRenderer } from 'electron'; import { CryptoService } from 'jslib/abstractions/crypto.service'; +import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { LogService } from 'jslib/abstractions/log.service'; +import { Utils } from 'jslib/misc/utils'; const MessageValidTimeout = 10 * 1000; +const EncryptionAlgorithm = 'sha256'; export class NativeMessagingService { + private publicKey: ArrayBuffer; + private privateKey: ArrayBuffer; + private remotePublicKey: ArrayBuffer; - constructor(private cryptoService: CryptoService, private platformUtilService: PlatformUtilsService, private logService: LogService) { + constructor(private cryptoFunctionService: CryptoFunctionService, private cryptoService: CryptoService, + private platformUtilService: PlatformUtilsService, private logService: LogService) { ipcRenderer.on('nativeMessaging', async (event: any, message: any) => { this.messageHandler(message); }); } private async messageHandler(rawMessage: any) { - const message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage)); + if (rawMessage.command == 'setupEncryption') { + this.remotePublicKey = Utils.fromB64ToArray(rawMessage.publicKey).buffer; + this.secureCommunication(); + return; + } + debugger; + const message = JSON.parse(Utils.fromBufferToUtf8(await this.cryptoFunctionService.rsaDecrypt(rawMessage, this.privateKey, EncryptionAlgorithm))); + console.log(message); if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) { this.logService.error('NativeMessage is to old, ignoring.'); return; @@ -44,8 +58,14 @@ export class NativeMessagingService { private async send(message: any) { message.timestamp = Date.now(); - const encrypted = await this.cryptoService.encrypt(JSON.stringify(message)); + const encrypted = await this.cryptoFunctionService.rsaEncrypt(Utils.fromUtf8ToArray(JSON.stringify(message)), this.remotePublicKey, EncryptionAlgorithm); ipcRenderer.send('nativeMessagingReply', encrypted); } + + private async secureCommunication() { + [this.publicKey, this.privateKey] = await this.cryptoFunctionService.rsaGenerateKeyPair(2048); + + this.send({command: 'setupEncryption', publicKey: Utils.fromBufferToB64(this.publicKey)}); + } }