2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
import { existsSync } from 'fs';
|
|
|
|
import { join } from 'path';
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import { copy } from 'fs-extra';
|
|
|
|
|
|
|
|
import {
|
|
|
|
_getFileHash,
|
|
|
|
getSignaturePath,
|
|
|
|
loadHexFromPath,
|
|
|
|
verifySignature,
|
|
|
|
writeHexToPath,
|
|
|
|
writeSignature,
|
|
|
|
} from '../../updater/signature';
|
|
|
|
import { createTempDir, deleteTempDir } from '../../updater/common';
|
|
|
|
import { keyPair } from '../../updater/curve';
|
|
|
|
|
|
|
|
describe('updater/signatures', () => {
|
|
|
|
it('_getFileHash returns correct hash', async () => {
|
|
|
|
const filePath = join(__dirname, '../../../fixtures/ghost-kitty.mp4');
|
|
|
|
const expected =
|
|
|
|
'7bc77f27d92d00b4a1d57c480ca86dacc43d57bc318339c92119d1fbf6b557a5';
|
|
|
|
|
|
|
|
const hash = await _getFileHash(filePath);
|
|
|
|
|
|
|
|
assert.strictEqual(expected, Buffer.from(hash).toString('hex'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('roundtrips binary file writes', async () => {
|
|
|
|
let tempDir;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tempDir = await createTempDir();
|
|
|
|
|
|
|
|
const path = join(tempDir, 'something.bin');
|
|
|
|
const { publicKey } = keyPair();
|
|
|
|
|
|
|
|
await writeHexToPath(path, publicKey);
|
|
|
|
|
|
|
|
const fromDisk = await loadHexFromPath(path);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
Buffer.from(fromDisk).compare(Buffer.from(publicKey)),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
if (tempDir) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('roundtrips signature', async () => {
|
|
|
|
let tempDir;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tempDir = await createTempDir();
|
|
|
|
|
|
|
|
const version = 'v1.23.2';
|
|
|
|
const sourcePath = join(__dirname, '../../../fixtures/ghost-kitty.mp4');
|
|
|
|
const updatePath = join(tempDir, 'ghost-kitty.mp4');
|
|
|
|
await copy(sourcePath, updatePath);
|
|
|
|
|
|
|
|
const privateKeyPath = join(tempDir, 'private.key');
|
|
|
|
const { publicKey, privateKey } = keyPair();
|
|
|
|
await writeHexToPath(privateKeyPath, privateKey);
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const signature = await writeSignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
privateKeyPath
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
const signaturePath = getSignaturePath(updatePath);
|
|
|
|
assert.strictEqual(existsSync(signaturePath), true);
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const verified = await verifySignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
signature,
|
|
|
|
publicKey
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
assert.strictEqual(verified, true);
|
|
|
|
} finally {
|
|
|
|
if (tempDir) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails signature verification if version changes', async () => {
|
|
|
|
let tempDir;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tempDir = await createTempDir();
|
|
|
|
|
|
|
|
const version = 'v1.23.2';
|
|
|
|
const brokenVersion = 'v1.23.3';
|
|
|
|
|
|
|
|
const sourcePath = join(__dirname, '../../../fixtures/ghost-kitty.mp4');
|
|
|
|
const updatePath = join(tempDir, 'ghost-kitty.mp4');
|
|
|
|
await copy(sourcePath, updatePath);
|
|
|
|
|
|
|
|
const privateKeyPath = join(tempDir, 'private.key');
|
|
|
|
const { publicKey, privateKey } = keyPair();
|
|
|
|
await writeHexToPath(privateKeyPath, privateKey);
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const signature = await writeSignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
privateKeyPath
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
const verified = await verifySignature(
|
|
|
|
updatePath,
|
|
|
|
brokenVersion,
|
2022-03-03 22:34:51 +00:00
|
|
|
signature,
|
2019-03-28 17:09:26 +00:00
|
|
|
publicKey
|
|
|
|
);
|
|
|
|
assert.strictEqual(verified, false);
|
|
|
|
} finally {
|
|
|
|
if (tempDir) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails signature verification if signature tampered with', async () => {
|
|
|
|
let tempDir;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tempDir = await createTempDir();
|
|
|
|
|
|
|
|
const version = 'v1.23.2';
|
|
|
|
|
|
|
|
const sourcePath = join(__dirname, '../../../fixtures/ghost-kitty.mp4');
|
|
|
|
const updatePath = join(tempDir, 'ghost-kitty.mp4');
|
|
|
|
await copy(sourcePath, updatePath);
|
|
|
|
|
|
|
|
const privateKeyPath = join(tempDir, 'private.key');
|
|
|
|
const { publicKey, privateKey } = keyPair();
|
|
|
|
await writeHexToPath(privateKeyPath, privateKey);
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const signature = await writeSignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
privateKeyPath
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
signature[4] += 3;
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const verified = await verifySignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
signature,
|
|
|
|
publicKey
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
assert.strictEqual(verified, false);
|
|
|
|
} finally {
|
|
|
|
if (tempDir) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails signature verification if binary file tampered with', async () => {
|
|
|
|
let tempDir;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tempDir = await createTempDir();
|
|
|
|
|
|
|
|
const version = 'v1.23.2';
|
|
|
|
|
|
|
|
const sourcePath = join(__dirname, '../../../fixtures/ghost-kitty.mp4');
|
|
|
|
const updatePath = join(tempDir, 'ghost-kitty.mp4');
|
|
|
|
await copy(sourcePath, updatePath);
|
|
|
|
|
|
|
|
const privateKeyPath = join(tempDir, 'private.key');
|
|
|
|
const { publicKey, privateKey } = keyPair();
|
|
|
|
await writeHexToPath(privateKeyPath, privateKey);
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const signature = await writeSignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
privateKeyPath
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
const brokenSourcePath = join(
|
|
|
|
__dirname,
|
|
|
|
'../../../fixtures/pixabay-Soap-Bubble-7141.mp4'
|
|
|
|
);
|
|
|
|
await copy(brokenSourcePath, updatePath);
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const verified = await verifySignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
signature,
|
|
|
|
publicKey
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
assert.strictEqual(verified, false);
|
|
|
|
} finally {
|
|
|
|
if (tempDir) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails signature verification if signed by different key', async () => {
|
|
|
|
let tempDir;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tempDir = await createTempDir();
|
|
|
|
|
|
|
|
const version = 'v1.23.2';
|
|
|
|
|
|
|
|
const sourcePath = join(__dirname, '../../../fixtures/ghost-kitty.mp4');
|
|
|
|
const updatePath = join(tempDir, 'ghost-kitty.mp4');
|
|
|
|
await copy(sourcePath, updatePath);
|
|
|
|
|
|
|
|
const privateKeyPath = join(tempDir, 'private.key');
|
|
|
|
const { publicKey } = keyPair();
|
|
|
|
const { privateKey } = keyPair();
|
|
|
|
await writeHexToPath(privateKeyPath, privateKey);
|
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const signature = await writeSignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
privateKeyPath
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2022-03-03 22:34:51 +00:00
|
|
|
const verified = await verifySignature(
|
|
|
|
updatePath,
|
|
|
|
version,
|
|
|
|
signature,
|
|
|
|
publicKey
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
assert.strictEqual(verified, false);
|
|
|
|
} finally {
|
|
|
|
if (tempDir) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|