فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

test(A06): prove ECDSA signature, device binding, offline lease and tamper rejection

davoodmehraban89 committed Oct 15, 2026 at 20:46 UTC e9c8b5882be12fdf71d5d9cd3a1115d76229f0cd
1 file changed +68
tests/a06-signed-license-integration.test.mjs new
+68
@@ -0,0 +1,68 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { canonical } from '../src/core/EntitlementVerifier.mjs';
4 +import { loadProductionLicensePublicKey, validateOfflineActivation }
5 + from '../src/core/LicenseActivationCore.mjs';
6 +
7 +const NOW = Date.UTC(2026, 9, 15);
8 +async function newSignedExample() {
9 + const keys = await crypto.subtle.generateKey({
10 + name: 'ECDSA', namedCurve: 'P-256'
11 + }, true, ['sign', 'verify']);
12 + const pub = await crypto.subtle.exportKey('jwk', keys.publicKey);
13 + const payload = {
14 + product: 'Factor-easyPro',
15 + installationId: 'device-hardware-1', perpetual: true,
16 + issuedAt: '2026-10-15T00:00:00.000Z',
17 + lastOnlineCheckAt: '2026-10-15T00:00:00.000Z',
18 + maxCompanies: 5, maxUsers: 4, maxDevices: 1
19 + };
20 + const sign = async value => {
21 + const sig = await crypto.subtle.sign({
22 + name: 'ECDSA', hash: 'SHA-256'
23 + }, keys.privateKey, new TextEncoder().encode(canonical(value)));
24 + return { payload: value, signature: Buffer.from(sig).toString('base64url') };
25 + };
26 + return { pub, payload, sign };
27 +}
28 +
29 +test('A06: only a trusted public P-256 key without private scalar is accepted', async () => {
30 + const { pub } = await newSignedExample();
31 + assert.deepEqual(loadProductionLicensePublicKey(JSON.stringify(pub)), pub);
32 + assert.equal(loadProductionLicensePublicKey(null), null);
33 + assert.equal(loadProductionLicensePublicKey('{}'), null);
34 + assert.equal(loadProductionLicensePublicKey('{invalid-json'), null);
35 + assert.equal(loadProductionLicensePublicKey(JSON.stringify({ ...pub, d: 'secret' })), null);
36 +});
37 +
38 +test('A06: locally generated signed license verifies when bound to this exact installation', async () => {
39 + const { pub, payload, sign } = await newSignedExample();
40 + const token = await sign(payload);
41 + assert.equal(await validateOfflineActivation(token, pub, 'device-hardware-1', NOW), true);
42 + assert.equal(await validateOfflineActivation(token, pub, 'another-device', NOW), false);
43 + assert.equal(await validateOfflineActivation(
44 + { ...token, payload: { ...payload, maxUsers: 8 } }, pub, 'device-hardware-1', NOW
45 + ), false);
46 + assert.equal(await validateOfflineActivation(
47 + { ...token, signature: 'tampered' }, pub, 'device-hardware-1', NOW
48 + ), false);
49 +});
50 +
51 +test('A06: signed lease fails closed after 30 days, before activation, or on future timestamps', async () => {
52 + const { pub, payload, sign } = await newSignedExample();
53 + const token = await sign(payload);
54 + const DAY = 86400000;
55 + assert.equal(await validateOfflineActivation(token, pub, payload.installationId, NOW + 31 * DAY), false);
56 + assert.equal(await validateOfflineActivation(token, pub, payload.installationId, NOW - DAY), false);
57 + const future = await sign({
58 + ...payload, issuedAt: '2026-10-20T00:00:00.000Z',
59 + lastOnlineCheckAt: '2026-10-20T00:00:00.000Z'
60 + });
61 + assert.equal(await validateOfflineActivation(future, pub, payload.installationId, NOW), false);
62 +});
63 +
64 +test('A06: a caller-supplied forged public key is not automatically trusted by the application', async () => {
65 + const { payload, sign } = await newSignedExample();
66 + const attackerToken = await sign(payload);
67 + assert.equal(await validateOfflineActivation(attackerToken, null, payload.installationId, NOW), false);
68 +});