1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-03 13:33:32 +01:00
bitwarden-browser/src/services/nativeMessaging.service.ts

120 lines
5.3 KiB
TypeScript
Raw Normal View History

import { ipcRenderer } from 'electron';
2020-10-19 16:50:33 +02:00
import Swal from 'sweetalert2';
2020-10-05 20:05:48 +02:00
import { CryptoService } from 'jslib/abstractions/crypto.service';
2020-10-16 17:09:17 +02:00
import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { LogService } from 'jslib/abstractions/log.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { UserService } from 'jslib/abstractions/user.service';
import { VaultTimeoutService } from 'jslib/abstractions/vaultTimeout.service';
2020-10-16 17:09:17 +02:00
import { Utils } from 'jslib/misc/utils';
2020-10-19 12:21:07 +02:00
import { SymmetricCryptoKey } from 'jslib/models/domain/symmetricCryptoKey';
const MessageValidTimeout = 10 * 1000;
2020-10-19 12:21:07 +02:00
const EncryptionAlgorithm = 'sha1';
export class NativeMessagingService {
private sharedSecrets = new Map<string, SymmetricCryptoKey>();
2020-10-16 17:09:17 +02:00
constructor(private cryptoFunctionService: CryptoFunctionService, private cryptoService: CryptoService,
private platformUtilService: PlatformUtilsService, private logService: LogService, private i18nService: I18nService,
private userService: UserService, private messagingService: MessagingService, private vaultTimeoutService: VaultTimeoutService) {
ipcRenderer.on('nativeMessaging', async (event: any, message: any) => {
this.messageHandler(message);
});
}
private async messageHandler(msg: any) {
const appId = msg.appId;
const rawMessage = msg.message;
2020-10-21 16:48:40 +02:00
// Request to setup secure encryption
2020-11-30 15:10:57 +01:00
if (rawMessage.command === 'setupEncryption') {
2020-10-19 16:50:33 +02:00
const remotePublicKey = Utils.fromB64ToArray(rawMessage.publicKey).buffer;
const fingerprint = (await this.cryptoService.getFingerprint(await this.userService.getUserId(), remotePublicKey)).join(' ');
2020-11-30 15:10:57 +01:00
this.messagingService.send('setFocus');
2020-10-21 16:48:40 +02:00
// Await confirmation that fingerprint is correct
2020-10-19 16:50:33 +02:00
const submitted = await Swal.fire({
title: this.i18nService.t('verifyBrowserTitle'),
html: `${this.i18nService.t('verifyBrowserDesc')}<br><br><strong>${fingerprint}</strong>`,
2020-10-19 16:50:33 +02:00
showCancelButton: true,
cancelButtonText: this.i18nService.t('cancel'),
showConfirmButton: true,
confirmButtonText: this.i18nService.t('approve'),
allowOutsideClick: false,
});
2020-11-30 15:10:57 +01:00
2020-10-19 16:50:33 +02:00
if (submitted.value !== true) {
return;
}
2020-11-30 15:10:57 +01:00
this.secureCommunication(remotePublicKey, appId);
2020-10-16 17:09:17 +02:00
return;
}
2020-10-05 20:05:48 +02:00
const message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage, this.sharedSecrets.get(appId)));
2020-10-19 12:21:07 +02:00
// Shared secret is invalidated, force re-authentication
if (message == null) {
ipcRenderer.send('nativeMessagingReply', {command: 'invalidateEncryption', appId: appId});
return;
}
if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) {
this.logService.error('NativeMessage is to old, ignoring.');
return;
}
switch (message.command) {
case 'biometricUnlock':
if (! this.platformUtilService.supportsBiometric()) {
return this.send({command: 'biometricUnlock', response: 'not supported'}, appId);
}
if (! await this.vaultTimeoutService.isBiometricLockSet()) {
this.send({command: 'biometricUnlock', response: 'not enabled'}, appId);
return await Swal.fire({
title: this.i18nService.t('biometricsNotEnabledTitle'),
text: this.i18nService.t('biometricsNotEnabledDesc'),
showCancelButton: true,
cancelButtonText: this.i18nService.t('cancel'),
showConfirmButton: false,
});
}
const response = await this.platformUtilService.authenticateBiometric();
if (response) {
this.send({command: 'biometricUnlock', response: 'unlocked', keyB64: (await this.cryptoService.getKey()).keyB64}, appId);
} else {
this.send({command: 'biometricUnlock', response: 'canceled'}, appId);
}
2020-10-12 18:03:16 +02:00
break;
default:
this.logService.error('NativeMessage, got unknown command.');
}
}
private async send(message: any, appId: string) {
message.timestamp = Date.now();
2020-10-19 12:21:07 +02:00
const encrypted = await this.cryptoService.encrypt(JSON.stringify(message), this.sharedSecrets.get(appId));
ipcRenderer.send('nativeMessagingReply', {appId: appId, message: encrypted});
}
2020-10-16 17:09:17 +02:00
private async secureCommunication(remotePublicKey: ArrayBuffer, appId: string) {
2020-10-19 12:21:07 +02:00
const secret = await this.cryptoFunctionService.randomBytes(64);
this.sharedSecrets.set(appId, new SymmetricCryptoKey(secret));
2020-10-16 17:09:17 +02:00
2020-10-19 16:50:33 +02:00
const encryptedSecret = await this.cryptoFunctionService.rsaEncrypt(secret, remotePublicKey, EncryptionAlgorithm);
ipcRenderer.send('nativeMessagingReply', {appId: appId, command: 'setupEncryption', sharedSecret: Utils.fromBufferToB64(encryptedSecret)});
2020-10-16 17:09:17 +02:00
}
}