2022-10-10 17:19:01 +02:00
|
|
|
// eslint-disable-next-line no-restricted-imports
|
2022-09-27 03:17:43 +02:00
|
|
|
import { Substitute, Arg } from "@fluffy-spoon/substitute";
|
2022-09-02 03:15:19 +02:00
|
|
|
import { mock, MockProxy } from "jest-mock-extended";
|
2022-04-16 17:18:12 +02:00
|
|
|
|
2022-09-02 03:15:19 +02:00
|
|
|
import { AbstractEncryptService } from "@bitwarden/common/abstractions/abstractEncrypt.service";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
|
|
import { EncryptionType } from "@bitwarden/common/enums/encryptionType";
|
|
|
|
import { EncString } from "@bitwarden/common/models/domain/encString";
|
|
|
|
import { SymmetricCryptoKey } from "@bitwarden/common/models/domain/symmetricCryptoKey";
|
|
|
|
import { ContainerService } from "@bitwarden/common/services/container.service";
|
2022-04-16 17:18:12 +02:00
|
|
|
|
|
|
|
describe("EncString", () => {
|
|
|
|
afterEach(() => {
|
|
|
|
(window as any).bitwardenContainerService = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Rsa2048_OaepSha256_B64", () => {
|
|
|
|
it("constructor", () => {
|
|
|
|
const encString = new EncString(EncryptionType.Rsa2048_OaepSha256_B64, "data");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
data: "data",
|
|
|
|
encryptedString: "3.data",
|
|
|
|
encryptionType: 3,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("parse existing", () => {
|
|
|
|
it("valid", () => {
|
|
|
|
const encString = new EncString("3.data");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
data: "data",
|
|
|
|
encryptedString: "3.data",
|
|
|
|
encryptionType: 3,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("invalid", () => {
|
|
|
|
const encString = new EncString("3.data|test");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
encryptedString: "3.data|test",
|
|
|
|
encryptionType: 3,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("decrypt", () => {
|
|
|
|
const encString = new EncString(EncryptionType.Rsa2048_OaepSha256_B64, "data");
|
|
|
|
|
|
|
|
const cryptoService = Substitute.for<CryptoService>();
|
|
|
|
cryptoService.getOrgKey(null).resolves(null);
|
2022-09-02 03:15:19 +02:00
|
|
|
|
|
|
|
const encryptService = Substitute.for<AbstractEncryptService>();
|
|
|
|
encryptService.decryptToUtf8(encString, Arg.any()).resolves("decrypted");
|
2022-04-16 17:18:12 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-09-02 03:15:19 +02:00
|
|
|
(window as any).bitwardenContainerService = new ContainerService(
|
|
|
|
cryptoService,
|
|
|
|
encryptService
|
|
|
|
);
|
2022-04-16 17:18:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("decrypts correctly", async () => {
|
|
|
|
const decrypted = await encString.decrypt(null);
|
|
|
|
|
|
|
|
expect(decrypted).toBe("decrypted");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("result should be cached", async () => {
|
|
|
|
const decrypted = await encString.decrypt(null);
|
2022-09-02 03:15:19 +02:00
|
|
|
encryptService.received(1).decryptToUtf8(Arg.any(), Arg.any());
|
2022-04-16 17:18:12 +02:00
|
|
|
|
|
|
|
expect(decrypted).toBe("decrypted");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("AesCbc256_B64", () => {
|
|
|
|
it("constructor", () => {
|
|
|
|
const encString = new EncString(EncryptionType.AesCbc256_B64, "data", "iv");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
data: "data",
|
|
|
|
encryptedString: "0.iv|data",
|
|
|
|
encryptionType: 0,
|
|
|
|
iv: "iv",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("parse existing", () => {
|
|
|
|
it("valid", () => {
|
|
|
|
const encString = new EncString("0.iv|data");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
data: "data",
|
|
|
|
encryptedString: "0.iv|data",
|
|
|
|
encryptionType: 0,
|
|
|
|
iv: "iv",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("invalid", () => {
|
|
|
|
const encString = new EncString("0.iv|data|mac");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
encryptedString: "0.iv|data|mac",
|
|
|
|
encryptionType: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("AesCbc256_HmacSha256_B64", () => {
|
|
|
|
it("constructor", () => {
|
|
|
|
const encString = new EncString(EncryptionType.AesCbc256_HmacSha256_B64, "data", "iv", "mac");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
data: "data",
|
|
|
|
encryptedString: "2.iv|data|mac",
|
|
|
|
encryptionType: 2,
|
|
|
|
iv: "iv",
|
|
|
|
mac: "mac",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("valid", () => {
|
|
|
|
const encString = new EncString("2.iv|data|mac");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
data: "data",
|
|
|
|
encryptedString: "2.iv|data|mac",
|
|
|
|
encryptionType: 2,
|
|
|
|
iv: "iv",
|
|
|
|
mac: "mac",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("invalid", () => {
|
|
|
|
const encString = new EncString("2.iv|data");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
encryptedString: "2.iv|data",
|
|
|
|
encryptionType: 2,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Exit early if null", () => {
|
|
|
|
const encString = new EncString(null);
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
encryptedString: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("decrypt", () => {
|
2022-09-02 03:15:19 +02:00
|
|
|
let cryptoService: MockProxy<CryptoService>;
|
|
|
|
let encryptService: MockProxy<AbstractEncryptService>;
|
|
|
|
let encString: EncString;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
cryptoService = mock<CryptoService>();
|
|
|
|
encryptService = mock<AbstractEncryptService>();
|
|
|
|
encString = new EncString(null);
|
|
|
|
|
|
|
|
(window as any).bitwardenContainerService = new ContainerService(
|
|
|
|
cryptoService,
|
|
|
|
encryptService
|
|
|
|
);
|
2022-04-16 17:18:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("handles value it can't decrypt", async () => {
|
2022-09-02 03:15:19 +02:00
|
|
|
encryptService.decryptToUtf8.mockRejectedValue("error");
|
2022-04-16 17:18:12 +02:00
|
|
|
|
2022-09-02 03:15:19 +02:00
|
|
|
(window as any).bitwardenContainerService = new ContainerService(
|
|
|
|
cryptoService,
|
|
|
|
encryptService
|
|
|
|
);
|
2022-04-16 17:18:12 +02:00
|
|
|
|
|
|
|
const decrypted = await encString.decrypt(null);
|
|
|
|
|
|
|
|
expect(decrypted).toBe("[error: cannot decrypt]");
|
|
|
|
|
|
|
|
expect(encString).toEqual({
|
|
|
|
decryptedValue: "[error: cannot decrypt]",
|
|
|
|
encryptedString: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-02 03:15:19 +02:00
|
|
|
it("uses provided key without depending on CryptoService", async () => {
|
|
|
|
const key = mock<SymmetricCryptoKey>();
|
2022-04-16 17:18:12 +02:00
|
|
|
|
2022-09-02 03:15:19 +02:00
|
|
|
await encString.decrypt(null, key);
|
|
|
|
|
|
|
|
expect(cryptoService.getKeyForUserEncryption).not.toHaveBeenCalled();
|
|
|
|
expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(encString, key);
|
|
|
|
});
|
2022-04-16 17:18:12 +02:00
|
|
|
|
2022-09-02 03:15:19 +02:00
|
|
|
it("gets an organization key if required", async () => {
|
|
|
|
const orgKey = mock<SymmetricCryptoKey>();
|
2022-04-16 17:18:12 +02:00
|
|
|
|
2022-09-02 03:15:19 +02:00
|
|
|
cryptoService.getOrgKey.calledWith("orgId").mockResolvedValue(orgKey);
|
|
|
|
|
|
|
|
await encString.decrypt("orgId", null);
|
|
|
|
|
|
|
|
expect(cryptoService.getOrgKey).toHaveBeenCalledWith("orgId");
|
|
|
|
expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(encString, orgKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("gets the user's decryption key if required", async () => {
|
|
|
|
const userKey = mock<SymmetricCryptoKey>();
|
|
|
|
|
|
|
|
cryptoService.getKeyForUserEncryption.mockResolvedValue(userKey);
|
|
|
|
|
|
|
|
await encString.decrypt(null, null);
|
2022-04-16 17:18:12 +02:00
|
|
|
|
2022-09-02 03:15:19 +02:00
|
|
|
expect(cryptoService.getKeyForUserEncryption).toHaveBeenCalledWith();
|
|
|
|
expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(encString, userKey);
|
2022-04-16 17:18:12 +02:00
|
|
|
});
|
|
|
|
});
|
2022-08-16 14:05:03 +02:00
|
|
|
|
|
|
|
describe("toJSON", () => {
|
|
|
|
it("Should be represented by the encrypted string", () => {
|
|
|
|
const encString = new EncString(EncryptionType.AesCbc256_B64, "data", "iv");
|
|
|
|
|
|
|
|
expect(encString.toJSON()).toBe(encString.encryptedString);
|
|
|
|
});
|
2022-10-03 22:50:43 +02:00
|
|
|
|
|
|
|
it("returns null if object is null", () => {
|
|
|
|
expect(EncString.fromJSON(null)).toBeNull();
|
|
|
|
});
|
2022-08-16 14:05:03 +02:00
|
|
|
});
|
2022-04-16 17:18:12 +02:00
|
|
|
});
|