1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-24 12:06:15 +01:00
This commit is contained in:
Hinton 2020-10-16 17:09:17 +02:00
parent d83e2bc117
commit 74b51c9a6c
2 changed files with 24 additions and 4 deletions

View File

@ -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);

View File

@ -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)});
}
}