2021-06-07 19:25:37 +02:00
|
|
|
import { AppIdService } from "jslib-common/abstractions/appId.service";
|
|
|
|
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
|
|
|
import { CryptoFunctionService } from "jslib-common/abstractions/cryptoFunction.service";
|
|
|
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
2022-01-27 22:22:51 +01:00
|
|
|
import { LogService } from "jslib-common/abstractions/log.service";
|
2021-06-07 19:25:37 +02:00
|
|
|
import { MessagingService } from "jslib-common/abstractions/messaging.service";
|
|
|
|
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
2022-01-27 22:22:51 +01:00
|
|
|
import { StateService } from "jslib-common/abstractions/state.service";
|
2022-02-11 10:44:35 +01:00
|
|
|
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
2021-06-07 19:25:37 +02:00
|
|
|
import { Utils } from "jslib-common/misc/utils";
|
2022-01-27 22:22:51 +01:00
|
|
|
import { EncString } from "jslib-common/models/domain/encString";
|
2021-09-17 15:44:27 +02:00
|
|
|
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-10-11 20:45:25 +02:00
|
|
|
import { BrowserApi } from "../browser/browserApi";
|
2022-02-24 18:14:04 +01:00
|
|
|
|
2020-10-12 18:01:34 +02:00
|
|
|
import RuntimeBackground from "./runtime.background";
|
2020-10-09 17:16:15 +02:00
|
|
|
|
2020-10-16 11:09:49 +02:00
|
|
|
const MessageValidTimeout = 10 * 1000;
|
2020-10-19 12:20:45 +02:00
|
|
|
const EncryptionAlgorithm = "sha1";
|
2020-10-16 11:09:49 +02:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
type Message = {
|
|
|
|
command: string;
|
|
|
|
|
|
|
|
// Filled in by this service
|
|
|
|
userId?: string;
|
|
|
|
timestamp?: number;
|
|
|
|
|
|
|
|
// Used for sharing secret
|
|
|
|
publicKey?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type OuterMessage = {
|
|
|
|
message: Message | EncString;
|
|
|
|
appId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ReceiveMessage = {
|
|
|
|
timestamp: number;
|
|
|
|
command: string;
|
|
|
|
response?: any;
|
|
|
|
|
|
|
|
// Unlock key
|
|
|
|
keyB64?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ReceiveMessageOuter = {
|
|
|
|
command: string;
|
|
|
|
appId: string;
|
|
|
|
|
|
|
|
// Should only have one of these.
|
|
|
|
message?: EncString;
|
|
|
|
sharedSecret?: string;
|
|
|
|
};
|
|
|
|
|
2020-10-09 17:16:15 +02:00
|
|
|
export class NativeMessagingBackground {
|
|
|
|
private connected = false;
|
2020-10-21 15:56:10 +02:00
|
|
|
private connecting: boolean;
|
2020-10-09 17:16:15 +02:00
|
|
|
private port: browser.runtime.Port | chrome.runtime.Port;
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-10-09 17:16:15 +02:00
|
|
|
private resolver: any = null;
|
2020-10-19 12:20:45 +02:00
|
|
|
private privateKey: ArrayBuffer = null;
|
2020-12-18 16:06:36 +01:00
|
|
|
private publicKey: ArrayBuffer = null;
|
2020-10-16 17:08:53 +02:00
|
|
|
private secureSetupResolve: any = null;
|
2020-10-19 12:20:45 +02:00
|
|
|
private sharedSecret: SymmetricCryptoKey;
|
2020-12-16 15:47:30 +01:00
|
|
|
private appId: string;
|
2021-01-13 23:31:44 +01:00
|
|
|
private validatingFingerprint: boolean;
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-10-12 18:01:34 +02:00
|
|
|
constructor(
|
|
|
|
private cryptoService: CryptoService,
|
2020-10-16 17:08:53 +02:00
|
|
|
private cryptoFunctionService: CryptoFunctionService,
|
2020-10-19 16:50:25 +02:00
|
|
|
private runtimeBackground: RuntimeBackground,
|
|
|
|
private i18nService: I18nService,
|
2021-04-07 17:39:59 +02:00
|
|
|
private messagingService: MessagingService,
|
|
|
|
private appIdService: AppIdService,
|
2022-01-27 22:22:51 +01:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private stateService: StateService,
|
2022-02-11 10:44:35 +01:00
|
|
|
private logService: LogService,
|
|
|
|
private vaultTimeoutService: VaultTimeoutService
|
2021-04-07 17:39:59 +02:00
|
|
|
) {
|
2022-01-27 22:22:51 +01:00
|
|
|
this.stateService.setBiometricFingerprintValidated(false);
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-03-15 15:44:10 +01:00
|
|
|
if (chrome?.permissions?.onAdded) {
|
2021-01-29 23:31:36 +01:00
|
|
|
// Reload extension to activate nativeMessaging
|
2021-02-10 16:40:15 +01:00
|
|
|
chrome.permissions.onAdded.addListener((permissions) => {
|
2021-01-29 23:31:36 +01:00
|
|
|
BrowserApi.reloadExtension(null);
|
|
|
|
});
|
|
|
|
}
|
2021-01-13 23:31:44 +01:00
|
|
|
}
|
2020-10-12 18:01:34 +02:00
|
|
|
|
2020-10-21 15:56:10 +02:00
|
|
|
async connect() {
|
2020-12-16 15:47:30 +01:00
|
|
|
this.appId = await this.appIdService.getAppId();
|
2022-01-27 22:22:51 +01:00
|
|
|
this.stateService.setBiometricFingerprintValidated(false);
|
2020-12-16 15:47:30 +01:00
|
|
|
|
2021-04-07 17:39:59 +02:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2020-10-21 15:56:10 +02:00
|
|
|
this.port = BrowserApi.connectNative("com.8bit.bitwarden");
|
|
|
|
|
|
|
|
this.connecting = true;
|
|
|
|
|
2021-04-07 17:39:59 +02:00
|
|
|
const connectedCallback = () => {
|
|
|
|
this.connected = true;
|
|
|
|
this.connecting = false;
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Safari has a bundled native component which is always available, no need to
|
|
|
|
// check if the desktop app is running.
|
|
|
|
if (this.platformUtilsService.isSafari()) {
|
|
|
|
connectedCallback();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
this.port.onMessage.addListener(async (message: ReceiveMessageOuter) => {
|
2020-10-21 17:18:04 +02:00
|
|
|
switch (message.command) {
|
|
|
|
case "connected":
|
2021-04-07 17:39:59 +02:00
|
|
|
connectedCallback();
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2020-10-21 17:18:04 +02:00
|
|
|
case "disconnected":
|
|
|
|
if (this.connecting) {
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("startDesktopDesc"),
|
|
|
|
title: this.i18nService.t("startDesktopTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
2020-10-21 17:18:04 +02:00
|
|
|
reject();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-10-21 17:18:04 +02:00
|
|
|
this.connected = false;
|
|
|
|
this.port.disconnect();
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
case "setupEncryption": {
|
2020-12-16 15:47:30 +01:00
|
|
|
// Ignore since it belongs to another device
|
|
|
|
if (message.appId !== this.appId) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2021-04-07 17:39:59 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 17:18:04 +02:00
|
|
|
const encrypted = Utils.fromB64ToArray(message.sharedSecret);
|
|
|
|
const decrypted = await this.cryptoFunctionService.rsaDecrypt(
|
|
|
|
encrypted.buffer,
|
2020-10-23 14:40:50 +02:00
|
|
|
this.privateKey,
|
|
|
|
EncryptionAlgorithm
|
2020-12-16 15:47:30 +01:00
|
|
|
);
|
2020-10-21 15:56:10 +02:00
|
|
|
|
2020-10-21 19:23:27 +02:00
|
|
|
if (this.validatingFingerprint) {
|
2020-10-21 15:56:10 +02:00
|
|
|
this.validatingFingerprint = false;
|
2022-01-27 22:22:51 +01:00
|
|
|
this.stateService.setBiometricFingerprintValidated(true);
|
2020-10-21 15:56:10 +02:00
|
|
|
}
|
2020-10-23 14:40:50 +02:00
|
|
|
this.sharedSecret = new SymmetricCryptoKey(decrypted);
|
2020-10-21 15:56:10 +02:00
|
|
|
this.secureSetupResolve();
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
}
|
2020-10-21 15:56:10 +02:00
|
|
|
case "invalidateEncryption":
|
2020-12-16 15:47:30 +01:00
|
|
|
// Ignore since it belongs to another device
|
2020-10-21 15:56:10 +02:00
|
|
|
if (message.appId !== this.appId) {
|
|
|
|
return;
|
2020-10-09 17:16:15 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:39:59 +02:00
|
|
|
this.sharedSecret = null;
|
|
|
|
this.privateKey = null;
|
|
|
|
this.connected = false;
|
|
|
|
|
2020-10-12 21:18:47 +02:00
|
|
|
this.messagingService.send("showDialog", {
|
2020-10-23 14:40:50 +02:00
|
|
|
text: this.i18nService.t("nativeMessagingInvalidEncryptionDesc"),
|
2020-10-12 21:18:47 +02:00
|
|
|
title: this.i18nService.t("nativeMessagingInvalidEncryptionTitle"),
|
2020-10-23 14:40:50 +02:00
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
break;
|
2020-10-12 21:18:47 +02:00
|
|
|
case "verifyFingerprint": {
|
2020-10-09 17:16:15 +02:00
|
|
|
if (this.sharedSecret == null) {
|
2021-01-13 23:31:44 +01:00
|
|
|
this.validatingFingerprint = true;
|
2020-12-18 16:06:36 +01:00
|
|
|
this.showFingerprintDialog();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-10-09 17:16:15 +02:00
|
|
|
case "wrongUserId":
|
2020-10-21 15:56:10 +02:00
|
|
|
this.showWrongUserDialog();
|
2022-02-24 18:14:04 +01:00
|
|
|
break;
|
2021-12-21 15:43:35 +01:00
|
|
|
default:
|
2020-10-21 15:56:10 +02:00
|
|
|
// Ignore since it belongs to another device
|
2021-04-07 17:39:59 +02:00
|
|
|
if (!this.platformUtilsService.isSafari() && message.appId !== this.appId) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:47:30 +01:00
|
|
|
this.onMessage(message.message);
|
2020-10-09 17:16:15 +02:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
2020-10-09 17:16:15 +02:00
|
|
|
|
2021-04-07 17:39:59 +02:00
|
|
|
this.port.onDisconnect.addListener((p: any) => {
|
2020-10-21 19:23:27 +02:00
|
|
|
let error;
|
2021-04-07 17:39:59 +02:00
|
|
|
if (BrowserApi.isWebExtensionsApi) {
|
|
|
|
error = p.error.message;
|
|
|
|
} else {
|
|
|
|
error = chrome.runtime.lastError.message;
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:20:45 +02:00
|
|
|
if (error != null) {
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("desktopIntegrationDisabledDesc"),
|
2020-10-16 17:08:53 +02:00
|
|
|
title: this.i18nService.t("desktopIntegrationDisabledTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
2021-04-07 17:39:59 +02:00
|
|
|
type: "error",
|
2020-10-16 17:08:53 +02:00
|
|
|
});
|
|
|
|
}
|
2021-01-05 15:12:48 +01:00
|
|
|
this.sharedSecret = null;
|
2020-10-23 14:40:50 +02:00
|
|
|
this.privateKey = null;
|
2021-01-05 15:12:48 +01:00
|
|
|
this.connected = false;
|
2020-10-21 17:18:04 +02:00
|
|
|
reject();
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:39:59 +02:00
|
|
|
showWrongUserDialog() {
|
2021-01-05 15:12:48 +01:00
|
|
|
this.messagingService.send("showDialog", {
|
2021-04-07 17:39:59 +02:00
|
|
|
text: this.i18nService.t("nativeMessagingWrongUserDesc"),
|
|
|
|
title: this.i18nService.t("nativeMessagingWrongUserTitle"),
|
2020-10-21 15:56:10 +02:00
|
|
|
confirmText: this.i18nService.t("ok"),
|
2021-04-07 17:39:59 +02:00
|
|
|
type: "error",
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
async send(message: Message) {
|
2020-10-16 17:08:53 +02:00
|
|
|
if (!this.connected) {
|
2020-10-21 15:56:10 +02:00
|
|
|
await this.connect();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-10-16 17:08:53 +02:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
message.userId = await this.stateService.getUserId();
|
|
|
|
message.timestamp = Date.now();
|
|
|
|
|
2020-10-12 21:18:47 +02:00
|
|
|
if (this.platformUtilsService.isSafari()) {
|
2022-01-27 22:22:51 +01:00
|
|
|
this.postMessage(message as any);
|
2021-04-07 17:39:59 +02:00
|
|
|
} else {
|
|
|
|
this.postMessage({ appId: this.appId, message: await this.encryptMessage(message) });
|
2020-10-09 17:16:15 +02:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-10-09 17:16:15 +02:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
async encryptMessage(message: Message) {
|
2020-10-09 17:16:15 +02:00
|
|
|
if (this.sharedSecret == null) {
|
|
|
|
await this.secureCommunication();
|
|
|
|
}
|
2020-10-12 18:01:34 +02:00
|
|
|
|
2021-04-07 17:39:59 +02:00
|
|
|
return await this.cryptoService.encrypt(JSON.stringify(message), this.sharedSecret);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-10-21 17:18:04 +02:00
|
|
|
getResponse(): Promise<any> {
|
2020-10-16 17:08:53 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.resolver = resolve;
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
private postMessage(message: OuterMessage) {
|
2021-01-05 15:12:48 +01:00
|
|
|
// Wrap in try-catch to when the port disconnected without triggering `onDisconnect`.
|
|
|
|
try {
|
|
|
|
this.port.postMessage(message);
|
|
|
|
} catch (e) {
|
2022-01-27 22:22:51 +01:00
|
|
|
this.logService.error("NativeMessaging port disconnected, disconnecting.");
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-01-05 15:12:48 +01:00
|
|
|
this.sharedSecret = null;
|
|
|
|
this.privateKey = null;
|
|
|
|
this.connected = false;
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-01-05 15:12:48 +01:00
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("nativeMessagingInvalidEncryptionDesc"),
|
|
|
|
title: this.i18nService.t("nativeMessagingInvalidEncryptionTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 15:12:48 +01:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
private async onMessage(rawMessage: ReceiveMessage | EncString) {
|
|
|
|
let message = rawMessage as ReceiveMessage;
|
2021-04-07 17:39:59 +02:00
|
|
|
if (!this.platformUtilsService.isSafari()) {
|
2022-01-27 22:22:51 +01:00
|
|
|
message = JSON.parse(
|
|
|
|
await this.cryptoService.decryptToUtf8(rawMessage as EncString, this.sharedSecret)
|
|
|
|
);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-01-05 15:12:48 +01:00
|
|
|
|
|
|
|
if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) {
|
2022-01-27 22:22:51 +01:00
|
|
|
this.logService.error("NativeMessage is to old, ignoring.");
|
2021-01-05 15:12:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-12 21:18:47 +02:00
|
|
|
switch (message.command) {
|
2022-02-24 18:14:04 +01:00
|
|
|
case "biometricUnlock": {
|
2022-01-27 22:22:51 +01:00
|
|
|
await this.stateService.setBiometricAwaitingAcceptance(null);
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-12-16 17:32:51 +01:00
|
|
|
if (message.response === "not enabled") {
|
|
|
|
this.messagingService.send("showDialog", {
|
2021-04-07 17:39:59 +02:00
|
|
|
text: this.i18nService.t("biometricsNotEnabledDesc"),
|
2020-12-16 17:32:51 +01:00
|
|
|
title: this.i18nService.t("biometricsNotEnabledTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
2021-04-07 17:39:59 +02:00
|
|
|
type: "error",
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
break;
|
2021-04-07 17:39:59 +02:00
|
|
|
} else if (message.response === "not supported") {
|
|
|
|
this.messagingService.send("showDialog", {
|
2020-12-16 17:32:51 +01:00
|
|
|
text: this.i18nService.t("biometricsNotSupportedDesc"),
|
|
|
|
title: this.i18nService.t("biometricsNotSupportedTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
2021-04-07 17:39:59 +02:00
|
|
|
type: "error",
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
break;
|
2021-04-07 17:39:59 +02:00
|
|
|
}
|
2020-10-12 21:18:47 +02:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
const enabled = await this.stateService.getBiometricUnlock();
|
2020-10-16 11:09:49 +02:00
|
|
|
if (enabled === null || enabled === false) {
|
|
|
|
if (message.response === "unlocked") {
|
2022-01-27 22:22:51 +01:00
|
|
|
await this.stateService.setBiometricUnlock(true);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-10-12 21:18:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
// Ignore unlock if already unlocked
|
2022-02-11 10:44:35 +01:00
|
|
|
if (!(await this.vaultTimeoutService.isLocked())) {
|
2021-04-07 17:39:59 +02:00
|
|
|
break;
|
2020-10-12 18:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message.response === "unlocked") {
|
|
|
|
await this.cryptoService.setKey(
|
2020-10-12 21:18:47 +02:00
|
|
|
new SymmetricCryptoKey(Utils.fromB64ToArray(message.keyB64).buffer)
|
|
|
|
);
|
2020-10-16 17:08:53 +02:00
|
|
|
|
|
|
|
// Verify key is correct by attempting to decrypt a secret
|
|
|
|
try {
|
2022-01-27 22:22:51 +01:00
|
|
|
await this.cryptoService.getFingerprint(await this.stateService.getUserId());
|
2020-10-21 17:18:04 +02:00
|
|
|
} catch (e) {
|
2022-01-27 22:22:51 +01:00
|
|
|
this.logService.error("Unable to verify key: " + e);
|
2020-12-18 16:06:36 +01:00
|
|
|
await this.cryptoService.clearKey();
|
2020-10-21 17:18:04 +02:00
|
|
|
this.showWrongUserDialog();
|
2020-10-16 17:08:53 +02:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
// Exit early
|
|
|
|
if (this.resolver) {
|
|
|
|
this.resolver(message);
|
|
|
|
}
|
|
|
|
return;
|
2021-01-15 16:44:27 +01:00
|
|
|
}
|
2020-10-16 17:08:53 +02:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
await this.stateService.setBiometricLocked(false);
|
2020-10-16 17:08:53 +02:00
|
|
|
this.runtimeBackground.processMessage({ command: "unlocked" }, null, null);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-10-16 17:08:53 +02:00
|
|
|
break;
|
2022-02-24 18:14:04 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
default:
|
2022-01-27 22:22:51 +01:00
|
|
|
this.logService.error("NativeMessage, got unknown command: " + message.command);
|
2022-02-24 18:14:04 +01:00
|
|
|
break;
|
2020-10-16 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.resolver) {
|
2020-10-21 15:56:10 +02:00
|
|
|
this.resolver(message);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-10-16 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async secureCommunication() {
|
|
|
|
const [publicKey, privateKey] = await this.cryptoFunctionService.rsaGenerateKeyPair(2048);
|
2020-12-18 16:06:36 +01:00
|
|
|
this.publicKey = publicKey;
|
2020-10-21 17:18:04 +02:00
|
|
|
this.privateKey = privateKey;
|
2020-10-16 17:08:53 +02:00
|
|
|
|
2021-01-05 15:12:48 +01:00
|
|
|
this.sendUnencrypted({
|
|
|
|
command: "setupEncryption",
|
2021-01-15 16:44:27 +01:00
|
|
|
publicKey: Utils.fromBufferToB64(publicKey),
|
2022-01-27 22:22:51 +01:00
|
|
|
userId: await this.stateService.getUserId(),
|
2021-01-05 15:12:48 +01:00
|
|
|
});
|
2020-12-18 16:06:36 +01:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => (this.secureSetupResolve = resolve));
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-12-18 16:06:36 +01:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
private async sendUnencrypted(message: Message) {
|
2020-12-18 16:06:36 +01:00
|
|
|
if (!this.connected) {
|
|
|
|
await this.connect();
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-10-16 17:08:53 +02:00
|
|
|
message.timestamp = Date.now();
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-04-07 17:39:59 +02:00
|
|
|
this.postMessage({ appId: this.appId, message: message });
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:06:36 +01:00
|
|
|
private async showFingerprintDialog() {
|
|
|
|
const fingerprint = (
|
2022-01-27 22:22:51 +01:00
|
|
|
await this.cryptoService.getFingerprint(await this.stateService.getUserId(), this.publicKey)
|
2021-12-21 15:43:35 +01:00
|
|
|
).join(" ");
|
|
|
|
|
2020-12-16 17:32:51 +01:00
|
|
|
this.messagingService.send("showDialog", {
|
2020-12-18 16:06:36 +01:00
|
|
|
html: `${this.i18nService.t(
|
|
|
|
"desktopIntegrationVerificationText"
|
|
|
|
)}<br><br><strong>${fingerprint}</strong>`,
|
|
|
|
title: this.i18nService.t("desktopSyncVerificationTitle"),
|
2020-12-16 17:32:51 +01:00
|
|
|
confirmText: this.i18nService.t("ok"),
|
2020-12-18 16:06:36 +01:00
|
|
|
type: "warning",
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
}
|
2020-10-09 17:16:15 +02:00
|
|
|
}
|