2020-10-12 21:18:28 +02:00
|
|
|
import { ipcRenderer } from 'electron';
|
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-12 21:34:41 +02:00
|
|
|
|
|
|
|
const MessageValidTimeout = 10 * 1000;
|
2020-10-16 17:09:17 +02:00
|
|
|
const EncryptionAlgorithm = 'sha256';
|
2020-10-12 21:34:41 +02:00
|
|
|
|
2020-10-05 15:11:37 +02:00
|
|
|
export class NativeMessagingService {
|
2020-10-16 17:09:17 +02:00
|
|
|
private publicKey: ArrayBuffer;
|
|
|
|
private privateKey: ArrayBuffer;
|
|
|
|
private remotePublicKey: ArrayBuffer;
|
2020-10-12 21:34:41 +02:00
|
|
|
|
2020-10-16 17:09:17 +02:00
|
|
|
constructor(private cryptoFunctionService: CryptoFunctionService, private cryptoService: CryptoService,
|
|
|
|
private platformUtilService: PlatformUtilsService, private logService: LogService) {
|
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-16 17:09:17 +02:00
|
|
|
if (rawMessage.command == 'setupEncryption') {
|
|
|
|
this.remotePublicKey = Utils.fromB64ToArray(rawMessage.publicKey).buffer;
|
|
|
|
this.secureCommunication();
|
|
|
|
return;
|
|
|
|
}
|
2020-10-05 20:05:48 +02:00
|
|
|
|
2020-10-16 17:09:17 +02:00
|
|
|
debugger;
|
|
|
|
const message = JSON.parse(Utils.fromBufferToUtf8(await this.cryptoFunctionService.rsaDecrypt(rawMessage, this.privateKey, EncryptionAlgorithm)));
|
|
|
|
console.log(message);
|
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()) {
|
|
|
|
ipcRenderer.send('nativeMessagingSync', )
|
|
|
|
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-12 21:18:28 +02:00
|
|
|
this.send({command: 'biometricUnlock', response: 'unlocked'});
|
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-16 17:09:17 +02:00
|
|
|
const encrypted = await this.cryptoFunctionService.rsaEncrypt(Utils.fromUtf8ToArray(JSON.stringify(message)), this.remotePublicKey, EncryptionAlgorithm);
|
2020-10-12 21:18:28 +02:00
|
|
|
|
|
|
|
ipcRenderer.send('nativeMessagingReply', encrypted);
|
|
|
|
}
|
2020-10-16 17:09:17 +02:00
|
|
|
|
|
|
|
private async secureCommunication() {
|
|
|
|
[this.publicKey, this.privateKey] = await this.cryptoFunctionService.rsaGenerateKeyPair(2048);
|
|
|
|
|
|
|
|
this.send({command: 'setupEncryption', publicKey: Utils.fromBufferToB64(this.publicKey)});
|
|
|
|
}
|
2020-10-05 15:11:37 +02:00
|
|
|
}
|