2021-12-21 15:43:35 +01: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-12-21 15:43:35 +01: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";
|
2021-12-21 15:43:35 +01: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-12-21 15:43:35 +01:00
|
|
|
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
|
|
|
|
|
|
|
|
import { BrowserApi } from "../browser/browserApi";
|
|
|
|
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;
|
2021-12-21 15:43:35 +01: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 {
|
2021-12-21 15:43:35 +01:00
|
|
|
private connected = false;
|
|
|
|
private connecting: boolean;
|
|
|
|
private port: browser.runtime.Port | chrome.runtime.Port;
|
|
|
|
|
|
|
|
private resolver: any = null;
|
|
|
|
private privateKey: ArrayBuffer = null;
|
|
|
|
private publicKey: ArrayBuffer = null;
|
|
|
|
private secureSetupResolve: any = null;
|
|
|
|
private sharedSecret: SymmetricCryptoKey;
|
|
|
|
private appId: string;
|
|
|
|
private validatingFingerprint: boolean;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private cryptoService: CryptoService,
|
|
|
|
private cryptoFunctionService: CryptoFunctionService,
|
|
|
|
private runtimeBackground: RuntimeBackground,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private messagingService: MessagingService,
|
|
|
|
private appIdService: AppIdService,
|
2022-01-27 22:22:51 +01:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private stateService: StateService,
|
|
|
|
private logService: LogService
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
2022-01-27 22:22:51 +01:00
|
|
|
this.stateService.setBiometricFingerprintValidated(false);
|
2021-12-21 15:43:35 +01:00
|
|
|
|
|
|
|
if (chrome?.permissions?.onAdded) {
|
|
|
|
// Reload extension to activate nativeMessaging
|
|
|
|
chrome.permissions.onAdded.addListener((permissions) => {
|
|
|
|
BrowserApi.reloadExtension(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 18:01:34 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
async connect() {
|
|
|
|
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-12-21 15:43:35 +01:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
this.port = BrowserApi.connectNative("com.8bit.bitwarden");
|
2020-10-21 15:56:10 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
this.connecting = true;
|
2020-10-21 15:56:10 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
const connectedCallback = () => {
|
|
|
|
this.connected = true;
|
|
|
|
this.connecting = false;
|
|
|
|
resolve();
|
|
|
|
};
|
2021-04-07 17:39:59 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
this.port.onMessage.addListener(async (message: ReceiveMessageOuter) => {
|
2021-12-21 15:43:35 +01:00
|
|
|
switch (message.command) {
|
|
|
|
case "connected":
|
|
|
|
connectedCallback();
|
|
|
|
break;
|
|
|
|
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",
|
|
|
|
});
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
this.connected = false;
|
|
|
|
this.port.disconnect();
|
|
|
|
break;
|
|
|
|
case "setupEncryption":
|
|
|
|
// Ignore since it belongs to another device
|
|
|
|
if (message.appId !== this.appId) {
|
|
|
|
return;
|
2021-04-07 17:39:59 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
const encrypted = Utils.fromB64ToArray(message.sharedSecret);
|
|
|
|
const decrypted = await this.cryptoFunctionService.rsaDecrypt(
|
|
|
|
encrypted.buffer,
|
|
|
|
this.privateKey,
|
|
|
|
EncryptionAlgorithm
|
|
|
|
);
|
2020-10-21 15:56:10 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
if (this.validatingFingerprint) {
|
|
|
|
this.validatingFingerprint = false;
|
2022-01-27 22:22:51 +01:00
|
|
|
this.stateService.setBiometricFingerprintValidated(true);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
this.sharedSecret = new SymmetricCryptoKey(decrypted);
|
|
|
|
this.secureSetupResolve();
|
|
|
|
break;
|
|
|
|
case "invalidateEncryption":
|
|
|
|
// Ignore since it belongs to another device
|
|
|
|
if (message.appId !== this.appId) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-09 17:16:15 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
this.sharedSecret = null;
|
|
|
|
this.privateKey = null;
|
|
|
|
this.connected = false;
|
2021-04-07 17:39:59 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("nativeMessagingInvalidEncryptionDesc"),
|
|
|
|
title: this.i18nService.t("nativeMessagingInvalidEncryptionTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "verifyFingerprint": {
|
|
|
|
if (this.sharedSecret == null) {
|
|
|
|
this.validatingFingerprint = true;
|
|
|
|
this.showFingerprintDialog();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "wrongUserId":
|
|
|
|
this.showWrongUserDialog();
|
|
|
|
default:
|
|
|
|
// Ignore since it belongs to another device
|
|
|
|
if (!this.platformUtilsService.isSafari() && message.appId !== this.appId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12-21 15:43:35 +01:00
|
|
|
this.port.onDisconnect.addListener((p: any) => {
|
|
|
|
let error;
|
|
|
|
if (BrowserApi.isWebExtensionsApi) {
|
|
|
|
error = p.error.message;
|
2021-04-07 17:39:59 +02:00
|
|
|
} else {
|
2021-12-21 15:43:35 +01:00
|
|
|
error = chrome.runtime.lastError.message;
|
2021-04-07 17:39:59 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
if (error != null) {
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("desktopIntegrationDisabledDesc"),
|
|
|
|
title: this.i18nService.t("desktopIntegrationDisabledTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
|
|
|
});
|
2020-10-16 17:08:53 +02:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
this.sharedSecret = null;
|
|
|
|
this.privateKey = null;
|
|
|
|
this.connected = false;
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
showWrongUserDialog() {
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("nativeMessagingWrongUserDesc"),
|
|
|
|
title: this.i18nService.t("nativeMessagingWrongUserTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
async send(message: Message) {
|
2021-12-21 15:43:35 +01:00
|
|
|
if (!this.connected) {
|
|
|
|
await this.connect();
|
|
|
|
}
|
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();
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
if (this.platformUtilsService.isSafari()) {
|
2022-01-27 22:22:51 +01:00
|
|
|
this.postMessage(message as any);
|
2021-12-21 15:43:35 +01: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) {
|
2021-12-21 15:43:35 +01:00
|
|
|
if (this.sharedSecret == null) {
|
|
|
|
await this.secureCommunication();
|
2020-10-09 17:16:15 +02:00
|
|
|
}
|
2020-10-12 18:01:34 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
return await this.cryptoService.encrypt(JSON.stringify(message), this.sharedSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
getResponse(): Promise<any> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.resolver = resolve;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
private postMessage(message: OuterMessage) {
|
2021-12-21 15:43:35 +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
|
|
|
|
|
|
|
this.sharedSecret = null;
|
|
|
|
this.privateKey = null;
|
|
|
|
this.connected = false;
|
|
|
|
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("nativeMessagingInvalidEncryptionDesc"),
|
|
|
|
title: this.i18nService.t("nativeMessagingInvalidEncryptionTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
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-12-21 15:43:35 +01: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
|
|
|
|
2021-12-21 15:43:35 +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-12-21 15:43:35 +01:00
|
|
|
return;
|
2021-01-05 15:12:48 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
switch (message.command) {
|
|
|
|
case "biometricUnlock":
|
2022-01-27 22:22:51 +01:00
|
|
|
await this.stateService.setBiometricAwaitingAcceptance(null);
|
2021-12-21 15:43:35 +01:00
|
|
|
|
|
|
|
if (message.response === "not enabled") {
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("biometricsNotEnabledDesc"),
|
|
|
|
title: this.i18nService.t("biometricsNotEnabledTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
} else if (message.response === "not supported") {
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
text: this.i18nService.t("biometricsNotSupportedDesc"),
|
|
|
|
title: this.i18nService.t("biometricsNotSupportedTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
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();
|
2021-12-21 15:43:35 +01: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
|
|
|
}
|
|
|
|
break;
|
2020-10-12 21:18:47 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
// Ignore unlock if already unlocked
|
|
|
|
if (!(await this.stateService.getBiometricLocked())) {
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2020-10-12 18:01:34 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
if (message.response === "unlocked") {
|
|
|
|
await this.cryptoService.setKey(
|
|
|
|
new SymmetricCryptoKey(Utils.fromB64ToArray(message.keyB64).buffer)
|
|
|
|
);
|
2020-10-16 17:08:53 +02:00
|
|
|
|
2021-12-21 15:43:35 +01: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());
|
2021-12-21 15:43:35 +01:00
|
|
|
} catch (e) {
|
2022-01-27 22:22:51 +01:00
|
|
|
this.logService.error("Unable to verify key: " + e);
|
2021-12-21 15:43:35 +01:00
|
|
|
await this.cryptoService.clearKey();
|
|
|
|
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-12-21 15:43:35 +01:00
|
|
|
}
|
2020-10-16 17:08:53 +02:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
await this.stateService.setBiometricLocked(false);
|
2021-12-21 15:43:35 +01:00
|
|
|
this.runtimeBackground.processMessage({ command: "unlocked" }, null, null);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-01-27 22:22:51 +01:00
|
|
|
this.logService.error("NativeMessage, got unknown command: " + message.command);
|
2020-10-16 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
if (this.resolver) {
|
|
|
|
this.resolver(message);
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 17:08:53 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
private async secureCommunication() {
|
|
|
|
const [publicKey, privateKey] = await this.cryptoFunctionService.rsaGenerateKeyPair(2048);
|
|
|
|
this.publicKey = publicKey;
|
|
|
|
this.privateKey = privateKey;
|
2020-10-16 17:08:53 +02:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
this.sendUnencrypted({
|
|
|
|
command: "setupEncryption",
|
|
|
|
publicKey: Utils.fromBufferToB64(publicKey),
|
2022-01-27 22:22:51 +01:00
|
|
|
userId: await this.stateService.getUserId(),
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
2020-12-18 16:06:36 +01:00
|
|
|
|
2021-12-21 15:43:35 +01:00
|
|
|
return new Promise((resolve, reject) => (this.secureSetupResolve = resolve));
|
|
|
|
}
|
2020-12-18 16:06:36 +01:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
private async sendUnencrypted(message: Message) {
|
2021-12-21 15:43:35 +01:00
|
|
|
if (!this.connected) {
|
|
|
|
await this.connect();
|
2020-12-18 16:06:36 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
|
|
|
message.timestamp = Date.now();
|
|
|
|
|
|
|
|
this.postMessage({ appId: this.appId, message: message });
|
|
|
|
}
|
|
|
|
|
|
|
|
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(" ");
|
|
|
|
|
|
|
|
this.messagingService.send("showDialog", {
|
|
|
|
html: `${this.i18nService.t(
|
|
|
|
"desktopIntegrationVerificationText"
|
|
|
|
)}<br><br><strong>${fingerprint}</strong>`,
|
|
|
|
title: this.i18nService.t("desktopSyncVerificationTitle"),
|
|
|
|
confirmText: this.i18nService.t("ok"),
|
|
|
|
type: "warning",
|
|
|
|
});
|
|
|
|
}
|
2020-10-09 17:16:15 +02:00
|
|
|
}
|