1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-13 10:24:20 +01:00
bitwarden-browser/src/background/nativeMessaging.background.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

351 lines
12 KiB
TypeScript
Raw Normal View History

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";
import { MessagingService } from "jslib-common/abstractions/messaging.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { StorageService } from "jslib-common/abstractions/storage.service";
import { UserService } from "jslib-common/abstractions/user.service";
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
import { ConstantsService } from "jslib-common/services/constants.service";
2021-12-21 15:43:35 +01:00
import { Utils } from "jslib-common/misc/utils";
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";
import RuntimeBackground from "./runtime.background";
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
export class NativeMessagingBackground {
private connected = false;
private connecting: boolean;
private port: browser.runtime.Port | chrome.runtime.Port;
2021-12-21 15:43:35 +01: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;
private appId: string;
private validatingFingerprint: boolean;
2021-12-21 15:43:35 +01:00
constructor(
private storageService: StorageService,
private cryptoService: CryptoService,
2020-10-16 17:08:53 +02:00
private cryptoFunctionService: CryptoFunctionService,
private vaultTimeoutService: VaultTimeoutService,
2020-10-19 16:50:25 +02:00
private runtimeBackground: RuntimeBackground,
private i18nService: I18nService,
private userService: UserService,
private messagingService: MessagingService,
private appIdService: AppIdService,
private platformUtilsService: PlatformUtilsService
) {
this.storageService.save(ConstantsService.biometricFingerprintValidated, false);
2021-12-21 15:43:35 +01:00
2021-03-15 15:44:10 +01:00
if (chrome?.permissions?.onAdded) {
// Reload extension to activate nativeMessaging
2021-02-10 16:40:15 +01:00
chrome.permissions.onAdded.addListener((permissions) => {
BrowserApi.reloadExtension(null);
});
}
}
async connect() {
this.appId = await this.appIdService.getAppId();
this.storageService.save(ConstantsService.biometricFingerprintValidated, false);
return new Promise<void>((resolve, reject) => {
this.port = BrowserApi.connectNative("com.8bit.bitwarden");
this.connecting = true;
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
}
2020-10-21 17:18:04 +02:00
this.port.onMessage.addListener(async (message: any) => {
switch (message.command) {
case "connected":
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;
2020-10-21 17:18:04 +02:00
case "setupEncryption":
// Ignore since it belongs to another device
if (message.appId !== this.appId) {
2021-12-21 15:43:35 +01:00
return;
}
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-10-21 19:23:27 +02:00
if (this.validatingFingerprint) {
this.validatingFingerprint = false;
this.storageService.save(ConstantsService.biometricFingerprintValidated, true);
}
2020-10-23 14:40:50 +02:00
this.sharedSecret = new SymmetricCryptoKey(decrypted);
this.secureSetupResolve();
2021-12-21 15:43:35 +01:00
break;
case "invalidateEncryption":
// Ignore since it belongs to another device
if (message.appId !== this.appId) {
return;
}
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": {
if (this.sharedSecret == null) {
this.validatingFingerprint = true;
2020-12-18 16:06:36 +01:00
this.showFingerprintDialog();
2021-12-21 15:43:35 +01:00
}
break;
}
case "wrongUserId":
this.showWrongUserDialog();
2021-12-21 15:43:35 +01:00
default:
// Ignore since it belongs to another device
if (!this.platformUtilsService.isSafari() && message.appId !== this.appId) {
2021-12-21 15:43:35 +01:00
return;
}
this.onMessage(message.message);
}
2021-12-21 15:43:35 +01:00
});
this.port.onDisconnect.addListener((p: any) => {
2020-10-21 19:23:27 +02:00
let error;
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"),
type: "error",
2020-10-16 17:08:53 +02:00
});
}
this.sharedSecret = null;
2020-10-23 14:40:50 +02:00
this.privateKey = null;
this.connected = false;
2020-10-21 17:18:04 +02:00
reject();
2021-12-21 15:43:35 +01:00
});
});
}
showWrongUserDialog() {
this.messagingService.send("showDialog", {
text: this.i18nService.t("nativeMessagingWrongUserDesc"),
title: this.i18nService.t("nativeMessagingWrongUserTitle"),
confirmText: this.i18nService.t("ok"),
type: "error",
2021-12-21 15:43:35 +01:00
});
}
2020-10-16 17:08:53 +02:00
async send(message: any) {
if (!this.connected) {
await this.connect();
2021-12-21 15:43:35 +01:00
}
2020-10-16 17:08:53 +02:00
2020-10-12 21:18:47 +02:00
if (this.platformUtilsService.isSafari()) {
this.postMessage(message);
} else {
this.postMessage({ appId: this.appId, message: await this.encryptMessage(message) });
}
2021-12-21 15:43:35 +01:00
}
2020-10-21 17:18:04 +02:00
async encryptMessage(message: any) {
if (this.sharedSecret == null) {
await this.secureCommunication();
}
2020-10-16 17:08:53 +02:00
message.timestamp = Date.now();
2021-12-21 15:43:35 +01: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
});
}
private postMessage(message: any) {
// Wrap in try-catch to when the port disconnected without triggering `onDisconnect`.
try {
this.port.postMessage(message);
} catch (e) {
// tslint:disable-next-line
console.error("NativeMessaging port disconnected, disconnecting.");
2021-12-21 15:43:35 +01:00
this.sharedSecret = null;
this.privateKey = null;
this.connected = false;
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",
2021-12-21 15:43:35 +01:00
});
}
}
2020-10-12 21:18:47 +02:00
private async onMessage(rawMessage: any) {
let message = rawMessage;
if (!this.platformUtilsService.isSafari()) {
message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage, this.sharedSecret));
2021-12-21 15:43:35 +01:00
}
if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) {
// tslint:disable-next-line
console.error("NativeMessage is to old, ignoring.");
return;
}
2020-10-12 21:18:47 +02:00
switch (message.command) {
case "biometricUnlock":
await this.storageService.remove(ConstantsService.biometricAwaitingAcceptance);
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",
2021-12-21 15:43:35 +01:00
});
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",
2021-12-21 15:43:35 +01:00
});
break;
}
2020-10-12 21:18:47 +02:00
2020-10-16 11:09:49 +02:00
const enabled = await this.storageService.get(ConstantsService.biometricUnlockKey);
if (enabled === null || enabled === false) {
if (message.response === "unlocked") {
await this.storageService.save(ConstantsService.biometricUnlockKey, true);
2021-12-21 15:43:35 +01:00
}
2020-10-12 21:18:47 +02:00
break;
}
// Ignore unlock if already unlockeded
2020-10-21 17:18:04 +02:00
if (!this.vaultTimeoutService.biometricLocked) {
break;
}
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 {
2020-10-21 17:18:04 +02:00
await this.cryptoService.getFingerprint(await this.userService.getUserId());
} catch (e) {
// tslint:disable-next-line
console.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
2021-02-10 16:40:15 +01:00
message = false;
break;
}
2020-10-16 17:08:53 +02:00
this.vaultTimeoutService.biometricLocked = false;
this.runtimeBackground.processMessage({ command: "unlocked" }, null, null);
2021-12-21 15:43:35 +01:00
}
2020-10-16 17:08:53 +02:00
break;
2021-12-21 15:43:35 +01:00
default:
2020-10-16 17:08:53 +02:00
// tslint:disable-next-line
console.error("NativeMessage, got unknown command: ", message.command);
}
if (this.resolver) {
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
this.sendUnencrypted({
command: "setupEncryption",
publicKey: Utils.fromBufferToB64(publicKey),
userId: await this.userService.getUserId(),
});
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
private async sendUnencrypted(message: any) {
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
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 = (
await this.cryptoService.getFingerprint(await this.userService.getUserId(), this.publicKey)
2021-12-21 15:43:35 +01:00
).join(" ");
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"),
confirmText: this.i18nService.t("ok"),
2020-12-18 16:06:36 +01:00
type: "warning",
2021-12-21 15:43:35 +01:00
});
}
}