2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2015 Signal Messenger, LLC
|
2021-09-10 02:38:11 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
import { constantTimeEqual } from '../../Crypto';
|
2021-09-10 02:38:11 +00:00
|
|
|
import { generateKeyPair } from '../../Curve';
|
2023-07-14 16:53:20 +00:00
|
|
|
import type { UploadKeysType } from '../../textsecure/WebAPI';
|
2021-10-26 19:15:33 +00:00
|
|
|
import AccountManager from '../../textsecure/AccountManager';
|
|
|
|
import type { PreKeyType, SignedPreKeyType } from '../../textsecure/Types.d';
|
2021-11-30 19:33:51 +00:00
|
|
|
import { UUID, UUIDKind } from '../../types/UUID';
|
2021-09-10 02:38:11 +00:00
|
|
|
|
|
|
|
const { textsecure } = window;
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const assertEqualBuffers = (a: Uint8Array, b: Uint8Array) => {
|
2021-09-10 02:38:11 +00:00
|
|
|
assert.isTrue(constantTimeEqual(a, b));
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Key generation', function thisNeeded() {
|
|
|
|
const count = 10;
|
|
|
|
const ourUuid = new UUID('aaaaaaaa-bbbb-4ccc-9ddd-eeeeeeeeeeee');
|
2023-07-14 16:53:20 +00:00
|
|
|
let result: UploadKeysType;
|
2021-09-10 02:38:11 +00:00
|
|
|
this.timeout(count * 2000);
|
|
|
|
|
|
|
|
function itStoresPreKey(keyId: number): void {
|
|
|
|
it(`prekey ${keyId} is valid`, async () => {
|
|
|
|
const keyPair = await textsecure.storage.protocol.loadPreKey(
|
|
|
|
ourUuid,
|
|
|
|
keyId
|
|
|
|
);
|
|
|
|
assert(keyPair, `PreKey ${keyId} not found`);
|
|
|
|
});
|
|
|
|
}
|
2023-07-14 16:53:20 +00:00
|
|
|
function itStoresKyberPreKey(keyId: number): void {
|
|
|
|
it(`kyber pre key ${keyId} is valid`, async () => {
|
|
|
|
const key = await textsecure.storage.protocol.loadKyberPreKey(
|
|
|
|
ourUuid,
|
|
|
|
keyId
|
|
|
|
);
|
|
|
|
assert(key, `kyber pre key ${keyId} not found`);
|
|
|
|
});
|
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
function itStoresSignedPreKey(keyId: number): void {
|
|
|
|
it(`signed prekey ${keyId} is valid`, async () => {
|
|
|
|
const keyPair = await textsecure.storage.protocol.loadSignedPreKey(
|
|
|
|
ourUuid,
|
|
|
|
keyId
|
|
|
|
);
|
|
|
|
assert(keyPair, `SignedPreKey ${keyId} not found`);
|
|
|
|
});
|
|
|
|
}
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
async function validateResultPreKey(
|
2021-09-10 02:38:11 +00:00
|
|
|
resultKey: Pick<PreKeyType, 'keyId' | 'publicKey'>
|
|
|
|
): Promise<void> {
|
|
|
|
const keyPair = await textsecure.storage.protocol.loadPreKey(
|
|
|
|
ourUuid,
|
|
|
|
resultKey.keyId
|
|
|
|
);
|
|
|
|
if (!keyPair) {
|
|
|
|
throw new Error(`PreKey ${resultKey.keyId} not found`);
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
assertEqualBuffers(resultKey.publicKey, keyPair.publicKey().serialize());
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
|
|
|
async function validateResultSignedKey(
|
2023-07-14 16:53:20 +00:00
|
|
|
resultSignedKey?: Pick<SignedPreKeyType, 'keyId' | 'publicKey'>
|
2021-09-10 02:38:11 +00:00
|
|
|
) {
|
2023-07-14 16:53:20 +00:00
|
|
|
if (!resultSignedKey) {
|
|
|
|
throw new Error('validateResultSignedKey: No signed prekey provided!');
|
|
|
|
}
|
2021-09-10 02:38:11 +00:00
|
|
|
const keyPair = await textsecure.storage.protocol.loadSignedPreKey(
|
|
|
|
ourUuid,
|
|
|
|
resultSignedKey.keyId
|
|
|
|
);
|
|
|
|
if (!keyPair) {
|
|
|
|
throw new Error(`SignedPreKey ${resultSignedKey.keyId} not found`);
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
assertEqualBuffers(
|
2021-09-10 02:38:11 +00:00
|
|
|
resultSignedKey.publicKey,
|
2021-09-24 00:49:05 +00:00
|
|
|
keyPair.publicKey().serialize()
|
2021-09-10 02:38:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
before(async () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
await textsecure.storage.protocol.clearPreKeyStore();
|
|
|
|
await textsecure.storage.protocol.clearKyberPreKeyStore();
|
|
|
|
await textsecure.storage.protocol.clearSignedPreKeysStore();
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
const keyPair = generateKeyPair();
|
|
|
|
await textsecure.storage.put('identityKeyMap', {
|
2022-07-28 16:35:29 +00:00
|
|
|
[ourUuid.toString()]: keyPair,
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
|
|
|
await textsecure.storage.user.setUuidAndDeviceId(ourUuid.toString(), 1);
|
2023-07-14 16:53:20 +00:00
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
await textsecure.storage.protocol.hydrateCaches();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await textsecure.storage.protocol.clearPreKeyStore();
|
2023-07-14 16:53:20 +00:00
|
|
|
await textsecure.storage.protocol.clearKyberPreKeyStore();
|
2021-09-10 02:38:11 +00:00
|
|
|
await textsecure.storage.protocol.clearSignedPreKeysStore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('the first time', () => {
|
|
|
|
before(async () => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const accountManager = new AccountManager({} as any);
|
2023-07-14 16:53:20 +00:00
|
|
|
result = await accountManager._generateKeys(count, UUIDKind.ACI);
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
|
|
|
|
2023-07-14 16:53:20 +00:00
|
|
|
describe('generates the basics', () => {
|
|
|
|
for (let i = 1; i <= count; i += 1) {
|
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
for (let i = 1; i <= count + 1; i += 1) {
|
|
|
|
itStoresKyberPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(1);
|
|
|
|
});
|
2021-09-10 02:38:11 +00:00
|
|
|
|
|
|
|
it(`result contains ${count} preKeys`, () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
|
|
|
assert.isArray(preKeys);
|
|
|
|
assert.lengthOf(preKeys, count);
|
2021-09-10 02:38:11 +00:00
|
|
|
for (let i = 0; i < count; i += 1) {
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.isObject(preKeys[i]);
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct keyIds', () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
2021-09-10 02:38:11 +00:00
|
|
|
for (let i = 0; i < count; i += 1) {
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.strictEqual(preKeys[i].keyId, i + 1);
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct public keys', async () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
|
|
|
await Promise.all(preKeys.map(validateResultPreKey));
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
|
|
|
it('returns a signed prekey', () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.strictEqual(result.signedPreKey?.keyId, 1);
|
|
|
|
assert.instanceOf(result.signedPreKey?.signature, Uint8Array);
|
2021-09-10 02:38:11 +00:00
|
|
|
return validateResultSignedKey(result.signedPreKey);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('the second time', () => {
|
|
|
|
before(async () => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const accountManager = new AccountManager({} as any);
|
2023-07-14 16:53:20 +00:00
|
|
|
result = await accountManager._generateKeys(count, UUIDKind.ACI);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('generates the basics', () => {
|
|
|
|
for (let i = 1; i <= 2 * count; i += 1) {
|
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
for (let i = 1; i <= 2 * count + 2; i += 1) {
|
|
|
|
itStoresKyberPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(1);
|
|
|
|
itStoresSignedPreKey(2);
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`result contains ${count} preKeys`, () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
|
|
|
assert.isArray(preKeys);
|
|
|
|
assert.lengthOf(preKeys, count);
|
2021-09-10 02:38:11 +00:00
|
|
|
for (let i = 0; i < count; i += 1) {
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.isObject(preKeys[i]);
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct keyIds', () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
2021-09-10 02:38:11 +00:00
|
|
|
for (let i = 1; i <= count; i += 1) {
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.strictEqual(preKeys[i - 1].keyId, i + count);
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct public keys', async () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
|
|
|
await Promise.all(preKeys.map(validateResultPreKey));
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
|
|
|
it('returns a signed prekey', () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.strictEqual(result.signedPreKey?.keyId, 2);
|
|
|
|
assert.instanceOf(result.signedPreKey?.signature, Uint8Array);
|
2021-09-10 02:38:11 +00:00
|
|
|
return validateResultSignedKey(result.signedPreKey);
|
|
|
|
});
|
|
|
|
});
|
2023-07-14 16:53:20 +00:00
|
|
|
describe('the third time, after keys are confirmed', () => {
|
2021-09-10 02:38:11 +00:00
|
|
|
before(async () => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const accountManager = new AccountManager({} as any);
|
2023-07-14 16:53:20 +00:00
|
|
|
|
|
|
|
await accountManager._confirmKeys(result, UUIDKind.ACI);
|
|
|
|
|
|
|
|
result = await accountManager._generateKeys(count, UUIDKind.ACI);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('generates the basics', () => {
|
|
|
|
for (let i = 1; i <= 3 * count; i += 1) {
|
|
|
|
itStoresPreKey(i);
|
|
|
|
}
|
|
|
|
// Note: no new last resort kyber key generated
|
|
|
|
for (let i = 1; i <= 3 * count + 2; i += 1) {
|
|
|
|
itStoresKyberPreKey(i);
|
|
|
|
}
|
|
|
|
itStoresSignedPreKey(1);
|
|
|
|
itStoresSignedPreKey(2);
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`result contains ${count} preKeys`, () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
|
|
|
assert.isArray(preKeys);
|
|
|
|
assert.lengthOf(preKeys, count);
|
2021-09-10 02:38:11 +00:00
|
|
|
for (let i = 0; i < count; i += 1) {
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.isObject(preKeys[i]);
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct keyIds', () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
2021-09-10 02:38:11 +00:00
|
|
|
for (let i = 1; i <= count; i += 1) {
|
2023-07-14 16:53:20 +00:00
|
|
|
assert.strictEqual(preKeys[i - 1].keyId, i + 2 * count);
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
it('result contains the correct public keys', async () => {
|
2023-07-14 16:53:20 +00:00
|
|
|
const preKeys = result.preKeys || [];
|
|
|
|
await Promise.all(preKeys.map(validateResultPreKey));
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
2023-07-14 16:53:20 +00:00
|
|
|
it('does not generate a third last resort prekey', async () => {
|
|
|
|
const keyId = 3 * count + 3;
|
|
|
|
const key = await textsecure.storage.protocol.loadKyberPreKey(
|
|
|
|
ourUuid,
|
|
|
|
keyId
|
|
|
|
);
|
|
|
|
assert.isUndefined(key, `kyber pre key ${keyId} was unexpectedly found`);
|
|
|
|
});
|
|
|
|
it('does not generate a third signed prekey', async () => {
|
|
|
|
const keyId = 3;
|
|
|
|
const keyPair = await textsecure.storage.protocol.loadSignedPreKey(
|
|
|
|
ourUuid,
|
|
|
|
keyId
|
|
|
|
);
|
|
|
|
assert.isUndefined(
|
|
|
|
keyPair,
|
|
|
|
`SignedPreKey ${keyId} was unexpectedly found`
|
|
|
|
);
|
2021-09-10 02:38:11 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|