2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2017 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-08-03 22:26:00 +00:00
|
|
|
import { assert } from 'chai';
|
2023-07-14 16:53:20 +00:00
|
|
|
import { range } from 'lodash';
|
2023-08-10 16:43:33 +00:00
|
|
|
import * as sinon from 'sinon';
|
2021-08-03 22:26:00 +00:00
|
|
|
|
|
|
|
import { getRandomBytes } from '../../Crypto';
|
|
|
|
import AccountManager from '../../textsecure/AccountManager';
|
2023-07-14 16:53:20 +00:00
|
|
|
import type {
|
|
|
|
KyberPreKeyType,
|
|
|
|
OuterSignedPrekeyType,
|
|
|
|
PreKeyType,
|
|
|
|
} from '../../textsecure/Types.d';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { ServiceIdKind, generateAci, generatePni } from '../../types/ServiceId';
|
2023-07-14 16:53:20 +00:00
|
|
|
import { DAY } from '../../util/durations';
|
2021-08-03 22:26:00 +00:00
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('AccountManager', () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
let sandbox: sinon.SinonSandbox;
|
2021-08-03 22:26:00 +00:00
|
|
|
let accountManager: AccountManager;
|
2017-12-01 21:35:39 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = generateAci();
|
|
|
|
const ourPni = generatePni();
|
2023-07-14 16:53:20 +00:00
|
|
|
const identityKey = window.Signal.Curve.generateKeyPair();
|
|
|
|
const pubKey = getRandomBytes(33);
|
|
|
|
const privKey = getRandomBytes(32);
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
beforeEach(() => {
|
2023-08-10 16:43:33 +00:00
|
|
|
sandbox = sinon.createSandbox();
|
|
|
|
|
2021-08-03 22:26:00 +00:00
|
|
|
const server: any = {};
|
|
|
|
accountManager = new AccountManager(server);
|
2023-07-14 16:53:20 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const { storage } = window.textsecure;
|
|
|
|
sandbox.stub(storage.protocol, 'getIdentityKeyPair').returns(identityKey);
|
|
|
|
const { user } = storage;
|
|
|
|
sandbox.stub(user, 'getAci').returns(ourAci);
|
|
|
|
sandbox.stub(user, 'getPni').returns(ourPni);
|
|
|
|
sandbox.stub(user, 'getServiceId').returns(ourAci);
|
|
|
|
sandbox.stub(user, 'getCheckedAci').returns(ourAci);
|
|
|
|
sandbox.stub(user, 'getCheckedPni').returns(ourPni);
|
|
|
|
sandbox.stub(user, 'getCheckedServiceId').returns(ourAci);
|
2023-07-14 16:53:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2023-08-10 16:43:33 +00:00
|
|
|
sandbox.restore();
|
2023-07-14 16:53:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('encrypted device name', () => {
|
|
|
|
it('roundtrips', async () => {
|
|
|
|
const deviceName = 'v2.5.0 on Ubunto 20.04';
|
|
|
|
const encrypted = accountManager.encryptDeviceName(
|
|
|
|
deviceName,
|
|
|
|
identityKey
|
|
|
|
);
|
|
|
|
if (!encrypted) {
|
|
|
|
throw new Error('failed to encrypt!');
|
|
|
|
}
|
|
|
|
assert.strictEqual(typeof encrypted, 'string');
|
|
|
|
const decrypted = await accountManager.decryptDeviceName(encrypted);
|
|
|
|
|
|
|
|
assert.strictEqual(decrypted, deviceName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles falsey deviceName', () => {
|
|
|
|
const encrypted = accountManager.encryptDeviceName('', identityKey);
|
|
|
|
assert.strictEqual(encrypted, null);
|
|
|
|
});
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
describe('#_cleanSignedPreKeys', () => {
|
2021-08-03 22:26:00 +00:00
|
|
|
let originalLoadSignedPreKeys: any;
|
|
|
|
let originalRemoveSignedPreKey: any;
|
|
|
|
let signedPreKeys: Array<OuterSignedPrekeyType>;
|
|
|
|
|
2018-12-13 19:12:33 +00:00
|
|
|
beforeEach(async () => {
|
2021-08-03 22:26:00 +00:00
|
|
|
originalLoadSignedPreKeys =
|
|
|
|
window.textsecure.storage.protocol.loadSignedPreKeys;
|
|
|
|
originalRemoveSignedPreKey =
|
2023-07-14 16:53:20 +00:00
|
|
|
window.textsecure.storage.protocol.removeSignedPreKeys;
|
2021-09-10 02:38:11 +00:00
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
window.textsecure.storage.protocol.loadSignedPreKeys = () =>
|
2021-08-03 22:26:00 +00:00
|
|
|
signedPreKeys;
|
2023-07-14 16:53:20 +00:00
|
|
|
// removeSignedPreKeys is updated per-test, below
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
afterEach(() => {
|
2021-11-11 22:43:05 +00:00
|
|
|
window.textsecure.storage.protocol.loadSignedPreKeys =
|
|
|
|
originalLoadSignedPreKeys;
|
2023-07-14 16:53:20 +00:00
|
|
|
window.textsecure.storage.protocol.removeSignedPreKeys =
|
2021-11-11 22:43:05 +00:00
|
|
|
originalRemoveSignedPreKey;
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
it('keeps no keys if five or less, even if over a month old', () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
const now = Date.now();
|
2018-05-02 16:51:22 +00:00
|
|
|
signedPreKeys = [
|
|
|
|
{
|
|
|
|
keyId: 1,
|
2020-12-17 23:29:20 +00:00
|
|
|
created_at: now - DAY * 32,
|
2018-05-02 16:51:22 +00:00
|
|
|
confirmed: true,
|
2021-08-03 22:26:00 +00:00
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 2,
|
2020-12-17 23:29:20 +00:00
|
|
|
created_at: now - DAY * 34,
|
2018-05-02 16:51:22 +00:00
|
|
|
confirmed: true,
|
2021-08-03 22:26:00 +00:00
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 3,
|
2020-12-17 23:29:20 +00:00
|
|
|
created_at: now - DAY * 38,
|
2018-05-02 16:51:22 +00:00
|
|
|
confirmed: true,
|
2021-08-03 22:26:00 +00:00
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
2023-07-14 16:53:20 +00:00
|
|
|
{
|
|
|
|
keyId: 4,
|
|
|
|
created_at: now - DAY * 39,
|
|
|
|
confirmed: true,
|
|
|
|
pubKey,
|
|
|
|
privKey,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 5,
|
|
|
|
created_at: now - DAY * 40,
|
|
|
|
confirmed: false,
|
|
|
|
pubKey,
|
|
|
|
privKey,
|
|
|
|
},
|
2018-05-02 16:51:22 +00:00
|
|
|
];
|
2017-12-01 21:35:39 +00:00
|
|
|
|
|
|
|
// should be no calls to store.removeSignedPreKey, would cause crash
|
2023-08-10 16:43:33 +00:00
|
|
|
return accountManager._cleanSignedPreKeys(ServiceIdKind.ACI);
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
|
|
|
|
2021-08-03 22:26:00 +00:00
|
|
|
it('eliminates oldest keys, even if recent key is unconfirmed', async () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
const now = Date.now();
|
2018-05-02 16:51:22 +00:00
|
|
|
signedPreKeys = [
|
|
|
|
{
|
|
|
|
keyId: 1,
|
2020-12-17 23:29:20 +00:00
|
|
|
created_at: now - DAY * 32,
|
2018-05-02 16:51:22 +00:00
|
|
|
confirmed: true,
|
2021-08-03 22:26:00 +00:00
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 2,
|
2020-12-17 23:29:20 +00:00
|
|
|
created_at: now - DAY * 31,
|
2021-08-03 22:26:00 +00:00
|
|
|
confirmed: false,
|
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 3,
|
2020-12-17 23:29:20 +00:00
|
|
|
created_at: now - DAY * 24,
|
2018-05-02 16:51:22 +00:00
|
|
|
confirmed: true,
|
2021-08-03 22:26:00 +00:00
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-03 22:26:00 +00:00
|
|
|
// Oldest, should be dropped
|
2018-05-02 16:51:22 +00:00
|
|
|
keyId: 4,
|
2020-12-17 23:29:20 +00:00
|
|
|
created_at: now - DAY * 38,
|
2018-05-02 16:51:22 +00:00
|
|
|
confirmed: true,
|
2021-08-03 22:26:00 +00:00
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 5,
|
|
|
|
created_at: now - DAY,
|
|
|
|
confirmed: true,
|
2021-08-03 22:26:00 +00:00
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-03 22:26:00 +00:00
|
|
|
keyId: 6,
|
|
|
|
created_at: now - DAY * 5,
|
|
|
|
confirmed: true,
|
|
|
|
pubKey,
|
|
|
|
privKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
];
|
2017-12-01 21:35:39 +00:00
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
let removedKeys: Array<number> = [];
|
|
|
|
window.textsecure.storage.protocol.removeSignedPreKeys = async (
|
2021-09-10 02:38:11 +00:00
|
|
|
_,
|
2023-07-14 16:53:20 +00:00
|
|
|
keyIds
|
2021-09-10 02:38:11 +00:00
|
|
|
) => {
|
2023-07-14 16:53:20 +00:00
|
|
|
removedKeys = removedKeys.concat(keyIds);
|
|
|
|
};
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
await accountManager._cleanSignedPreKeys(ServiceIdKind.ACI);
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.deepEqual(removedKeys, [4]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#_cleanLastResortKeys', () => {
|
|
|
|
let originalLoadKyberPreKeys: any;
|
|
|
|
let originalRemoveKyberPreKey: any;
|
|
|
|
let kyberPreKeys: Array<KyberPreKeyType>;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
originalLoadKyberPreKeys =
|
|
|
|
window.textsecure.storage.protocol.loadKyberPreKeys;
|
|
|
|
originalRemoveKyberPreKey =
|
|
|
|
window.textsecure.storage.protocol.removeKyberPreKeys;
|
|
|
|
|
|
|
|
window.textsecure.storage.protocol.loadKyberPreKeys = () => kyberPreKeys;
|
|
|
|
// removeKyberPreKeys is updated per-test, below
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
window.textsecure.storage.protocol.loadKyberPreKeys =
|
|
|
|
originalLoadKyberPreKeys;
|
|
|
|
window.textsecure.storage.protocol.removeKyberPreKeys =
|
|
|
|
originalRemoveKyberPreKey;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('keeps five keys even if over a month old', () => {
|
|
|
|
const now = Date.now();
|
|
|
|
kyberPreKeys = [
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:1`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 32,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: true,
|
|
|
|
keyId: 1,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:2`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 34,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: true,
|
|
|
|
keyId: 2,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:3`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 38,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: true,
|
|
|
|
keyId: 3,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:4`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 39,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: false,
|
|
|
|
keyId: 4,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:5`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 40,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: false,
|
|
|
|
keyId: 5,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// should be no calls to store.removeKyberPreKey, would cause crash
|
2023-08-10 16:43:33 +00:00
|
|
|
return accountManager._cleanLastResortKeys(ServiceIdKind.ACI);
|
2023-07-14 16:53:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('eliminates oldest keys, even if recent key is unconfirmed', async () => {
|
|
|
|
const now = Date.now();
|
|
|
|
kyberPreKeys = [
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:1`,
|
2017-12-01 21:35:39 +00:00
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
createdAt: now - DAY * 32,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: true,
|
|
|
|
keyId: 1,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:2`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 31,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: false,
|
|
|
|
keyId: 2,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:3`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 24,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: true,
|
|
|
|
keyId: 3,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Oldest, should be dropped
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:4`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 38,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: true,
|
|
|
|
keyId: 4,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:5`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 5,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: true,
|
|
|
|
keyId: 5,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:6`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 5,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isLastResort: true,
|
|
|
|
isConfirmed: true,
|
|
|
|
keyId: 6,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let removedKeys: Array<number> = [];
|
|
|
|
window.textsecure.storage.protocol.removeKyberPreKeys = async (
|
|
|
|
_,
|
|
|
|
keyIds
|
|
|
|
) => {
|
|
|
|
removedKeys = removedKeys.concat(keyIds);
|
2017-12-01 21:35:39 +00:00
|
|
|
};
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
await accountManager._cleanLastResortKeys(ServiceIdKind.ACI);
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.deepEqual(removedKeys, [4]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#_cleanPreKeys', () => {
|
|
|
|
let originalLoadPreKeys: any;
|
|
|
|
let originalRemovePreKeys: any;
|
|
|
|
let preKeys: Array<PreKeyType>;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
originalLoadPreKeys = window.textsecure.storage.protocol.loadPreKeys;
|
|
|
|
originalRemovePreKeys = window.textsecure.storage.protocol.removePreKeys;
|
|
|
|
|
|
|
|
window.textsecure.storage.protocol.loadPreKeys = () => preKeys;
|
|
|
|
// removePreKeys is updated per-test, below
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
window.textsecure.storage.protocol.loadPreKeys = originalLoadPreKeys;
|
|
|
|
window.textsecure.storage.protocol.removePreKeys = originalRemovePreKeys;
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
it('keeps five keys even if over 90 days old, but all latest batch', () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
const now = Date.now();
|
2023-07-14 16:53:20 +00:00
|
|
|
preKeys = [
|
2018-05-02 16:51:22 +00:00
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:1`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 92,
|
2018-05-02 16:51:22 +00:00
|
|
|
keyId: 1,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:2`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
2018-05-02 16:51:22 +00:00
|
|
|
keyId: 2,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:3`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
2018-05-02 16:51:22 +00:00
|
|
|
keyId: 3,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:4`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
2018-05-02 16:51:22 +00:00
|
|
|
keyId: 4,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:5`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 94,
|
|
|
|
keyId: 5,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
2018-05-02 16:51:22 +00:00
|
|
|
},
|
|
|
|
];
|
2017-12-01 21:35:39 +00:00
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
// should be no calls to store.removeKyberPreKey, would cause crash
|
2023-08-10 16:43:33 +00:00
|
|
|
return accountManager._cleanPreKeys(ServiceIdKind.ACI);
|
2023-07-14 16:53:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('eliminates keys not in the 200 newest, over 90 days old', async () => {
|
|
|
|
const now = Date.now();
|
|
|
|
preKeys = [
|
|
|
|
// The latest batch
|
|
|
|
...range(0, 100).map(
|
|
|
|
(id): PreKeyType => ({
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:${id}`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY,
|
|
|
|
keyId: 1,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
// Second-oldest batch, won't be dropped
|
|
|
|
...range(100, 200).map(
|
|
|
|
(id): PreKeyType => ({
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:${id}`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 40,
|
|
|
|
keyId: 1,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
// Oldest batch, will be dropped
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:6`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 92,
|
|
|
|
keyId: 6,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
publicKey: pubKey,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let removedKeys: Array<number> = [];
|
|
|
|
window.textsecure.storage.protocol.removePreKeys = async (_, keyIds) => {
|
|
|
|
removedKeys = removedKeys.concat(keyIds);
|
|
|
|
};
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
await accountManager._cleanPreKeys(ServiceIdKind.ACI);
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.deepEqual(removedKeys, [6]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#_cleanKyberPreKeys', () => {
|
|
|
|
let originalLoadKyberPreKeys: any;
|
|
|
|
let originalRemoveKyberPreKeys: any;
|
|
|
|
let kyberPreKeys: Array<KyberPreKeyType>;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
originalLoadKyberPreKeys =
|
|
|
|
window.textsecure.storage.protocol.loadKyberPreKeys;
|
|
|
|
originalRemoveKyberPreKeys =
|
|
|
|
window.textsecure.storage.protocol.removeKyberPreKeys;
|
|
|
|
|
|
|
|
window.textsecure.storage.protocol.loadKyberPreKeys = () => kyberPreKeys;
|
|
|
|
// removeKyberPreKeys is updated per-test, below
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
window.textsecure.storage.protocol.loadKyberPreKeys =
|
|
|
|
originalLoadKyberPreKeys;
|
|
|
|
window.textsecure.storage.protocol.removeKyberPreKeys =
|
|
|
|
originalRemoveKyberPreKeys;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('keeps five keys even if over 90 days old', () => {
|
|
|
|
const now = Date.now();
|
|
|
|
kyberPreKeys = [
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:1`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isConfirmed: false,
|
|
|
|
isLastResort: false,
|
|
|
|
keyId: 1,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:2`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isConfirmed: false,
|
|
|
|
isLastResort: false,
|
|
|
|
keyId: 2,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:3`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isConfirmed: false,
|
|
|
|
isLastResort: false,
|
|
|
|
keyId: 3,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:4`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isConfirmed: false,
|
|
|
|
isLastResort: false,
|
|
|
|
keyId: 4,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:5`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isConfirmed: false,
|
|
|
|
isLastResort: false,
|
|
|
|
keyId: 5,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// should be no calls to store.removeKyberPreKey, would cause crash
|
2023-08-10 16:43:33 +00:00
|
|
|
return accountManager._cleanKyberPreKeys(ServiceIdKind.ACI);
|
2023-07-14 16:53:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('eliminates keys not in the newest 200, over 90 days old', async () => {
|
|
|
|
const now = Date.now();
|
|
|
|
kyberPreKeys = [
|
|
|
|
// The latest batch
|
|
|
|
...range(0, 100).map(
|
|
|
|
(id): KyberPreKeyType => ({
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:${id}`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isConfirmed: false,
|
|
|
|
isLastResort: false,
|
|
|
|
keyId: 1,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
})
|
|
|
|
),
|
|
|
|
// Second-oldest batch, won't be dropped
|
|
|
|
...range(100, 200).map(
|
|
|
|
(id): KyberPreKeyType => ({
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:${id}`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 45,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isConfirmed: false,
|
|
|
|
isLastResort: false,
|
|
|
|
keyId: 4,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
})
|
|
|
|
),
|
|
|
|
// Oldest batch, will be dropped
|
|
|
|
{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: `${ourAci}:6`,
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
createdAt: now - DAY * 93,
|
|
|
|
data: getRandomBytes(32),
|
|
|
|
isConfirmed: false,
|
|
|
|
isLastResort: false,
|
|
|
|
keyId: 6,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourUuid: ourAci,
|
2023-07-14 16:53:20 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let removedKeys: Array<number> = [];
|
|
|
|
window.textsecure.storage.protocol.removeKyberPreKeys = async (
|
|
|
|
_,
|
|
|
|
keyIds
|
|
|
|
) => {
|
|
|
|
removedKeys = removedKeys.concat(keyIds);
|
2017-12-01 21:35:39 +00:00
|
|
|
};
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
await accountManager._cleanKyberPreKeys(ServiceIdKind.ACI);
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.deepEqual(removedKeys, [6]);
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|