2020-10-12 21:18:28 +02:00
|
|
|
import { ipcRenderer } from 'electron';
|
2020-10-19 16:50:33 +02:00
|
|
|
import Swal from 'sweetalert2';
|
2020-10-05 20:05:48 +02:00
|
|
|
|
2020-10-12 21:34:41 +02:00
|
|
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
2020-10-16 17:09:17 +02:00
|
|
|
import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service';
|
2020-10-12 21:34:41 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
|
|
|
import { LogService } from 'jslib/abstractions/log.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';
|
2020-10-19 16:50:33 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
2020-10-12 21:34:41 +02:00
|
|
|
|
|
|
|
const MessageValidTimeout = 10 * 1000;
|
2020-10-19 12:21:07 +02:00
|
|
|
const EncryptionAlgorithm = 'sha1';
|
2020-10-12 21:34:41 +02:00
|
|
|
|
2020-10-05 15:11:37 +02:00
|
|
|
export class NativeMessagingService {
|
2020-10-19 12:21:07 +02:00
|
|
|
private sharedSecret: any;
|
2020-10-12 21:34:41 +02:00
|
|
|
|
2020-10-16 17:09:17 +02:00
|
|
|
constructor(private cryptoFunctionService: CryptoFunctionService, private cryptoService: CryptoService,
|
2020-10-19 16:50:33 +02:00
|
|
|
private platformUtilService: PlatformUtilsService, private logService: LogService, private i18nService: I18nService, private userService: UserService) {
|
2020-10-12 21:18:28 +02:00
|
|
|
ipcRenderer.on('nativeMessaging', async (event: any, message: any) => {
|
|
|
|
this.messageHandler(message);
|
2020-10-05 15:11:37 +02:00
|
|
|
});
|
2020-10-05 19:48:51 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:18:28 +02:00
|
|
|
private async messageHandler(rawMessage: any) {
|
2020-10-21 16:48:40 +02:00
|
|
|
|
|
|
|
// Request to setup secure encryption
|
2020-10-16 17:09:17 +02: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-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('verifyBrowserDescription')}<br><br><strong>${fingerprint}</strong>`,
|
|
|
|
showCancelButton: true,
|
|
|
|
cancelButtonText: this.i18nService.t('cancel'),
|
|
|
|
showConfirmButton: true,
|
|
|
|
confirmButtonText: this.i18nService.t('approve'),
|
|
|
|
allowOutsideClick: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (submitted.value !== true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.secureCommunication(remotePublicKey);
|
2020-10-16 17:09:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-10-05 20:05:48 +02:00
|
|
|
|
2020-10-19 12:21:07 +02:00
|
|
|
const message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage, this.sharedSecret));
|
|
|
|
|
2020-10-23 14:40:31 +02:00
|
|
|
// Shared secret is invalidated, force re-authentication
|
|
|
|
if (message == null) {
|
|
|
|
ipcRenderer.send('nativeMessagingReply', {command: 'invalidateEncryption'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-12 21:34:41 +02:00
|
|
|
if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) {
|
|
|
|
this.logService.error('NativeMessage is to old, ignoring.');
|
2020-10-07 15:11:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-10-05 19:48:51 +02:00
|
|
|
|
2020-10-11 20:41:10 +02:00
|
|
|
switch (message.command) {
|
|
|
|
case 'biometricUnlock':
|
2020-10-12 21:18:28 +02:00
|
|
|
if (! this.platformUtilService.supportsBiometric()) {
|
2020-10-21 16:48:40 +02:00
|
|
|
return this.send({command: 'biometricUnlock', response: 'not supported'});
|
2020-10-11 20:41:10 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:18:28 +02:00
|
|
|
const response = await this.platformUtilService.authenticateBiometric();
|
2020-10-11 20:41:10 +02:00
|
|
|
if (response) {
|
2020-10-19 18:34:24 +02:00
|
|
|
this.send({command: 'biometricUnlock', response: 'unlocked', keyB64: (await this.cryptoService.getKey()).keyB64});
|
2020-10-11 20:41:10 +02:00
|
|
|
} else {
|
2020-10-12 21:18:28 +02:00
|
|
|
this.send({command: 'biometricUnlock', response: 'canceled'});
|
2020-10-11 20:41:10 +02:00
|
|
|
}
|
2020-10-12 18:03:16 +02:00
|
|
|
|
2020-10-11 20:41:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
2020-10-12 21:34:41 +02:00
|
|
|
this.logService.error('NativeMessage, got unknown command.');
|
2020-10-11 20:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 21:18:28 +02:00
|
|
|
|
|
|
|
private async send(message: any) {
|
|
|
|
message.timestamp = Date.now();
|
2020-10-19 12:21:07 +02:00
|
|
|
|
|
|
|
const encrypted = await this.cryptoService.encrypt(JSON.stringify(message), this.sharedSecret);
|
2020-10-12 21:18:28 +02:00
|
|
|
|
|
|
|
ipcRenderer.send('nativeMessagingReply', encrypted);
|
|
|
|
}
|
2020-10-16 17:09:17 +02:00
|
|
|
|
2020-10-19 16:50:33 +02:00
|
|
|
private async secureCommunication(remotePublicKey: ArrayBuffer) {
|
2020-10-19 12:21:07 +02:00
|
|
|
const secret = await this.cryptoFunctionService.randomBytes(64);
|
|
|
|
this.sharedSecret = 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);
|
2020-10-19 12:21:07 +02:00
|
|
|
ipcRenderer.send('nativeMessagingReply', {command: 'setupEncryption', sharedSecret: Utils.fromBufferToB64(encryptedSecret)});
|
2020-10-16 17:09:17 +02:00
|
|
|
}
|
2020-10-05 15:11:37 +02:00
|
|
|
}
|