import * as asn1js from 'asn1js'; import { describe, expect, test } from 'vitest'; import { OID } from '../src/cms/oids'; import { certificateFromDer, parseCmsInternal } from '../src/cms/parse'; import { x509Time } from '../src/cms/time'; import { buildDetachedCms, createTestSigner, parseDetachedCms, validateChain, verifyCmsSignature, verifyForCompletion, type RawSigner, } from '../src/index'; import { derProblem, signedAttributesAsSet } from './helpers/der'; import { createIdentity } from './helpers/pki'; const digest = new Uint8Array(32).map((_, i) => (i * 7 + 3) & 0xff); const prepared = { signingId: 'test', digest, algorithm: 'sha256' as const, byteRange: [0, 100, 200, 50] as [number, number, number, number], contentsSizeBytes: 8192, subFilter: 'ETSI.CAdES.detached' as const, expectedVersion: { baseSha256: 'x', editsVersion: 0 }, expiresAt: null, }; describe('detached CMS: build → parse → verify', () => { for (const algorithm of ['RSA-PKCS1-v1_5', 'RSA-PSS', 'ECDSA'] as const) { test(`${algorithm}: a CAdES-B CMS round-trips and verifies`, async () => { const signer = await createTestSigner({ algorithm }); const cms = await buildDetachedCms({ digest, hash: 'sha256', profile: 'cades-b', signer }); expect(cms[0]).toBe(0x30); const parsed = parseDetachedCms(cms); expect(parsed.digestAlgorithm).toBe('sha256'); expect(Array.from(parsed.messageDigest)).toEqual(Array.from(digest)); expect(parsed.signingTime).toBeNull(); expect(parsed.signingCertificateV2).toBe(true); expect(parsed.certificates).toHaveLength(1); expect(Array.from(parsed.signerCertificate)).toEqual(Array.from(signer.certificate)); expect(parsed.timestampToken).toBeNull(); expect(await verifyCmsSignature(cms)).toBe('valid'); // Flip a byte of the signature value (the tail of the CMS): invalid, never unsupported. const tampered = cms.slice(); tampered[tampered.byteLength - 1] ^= 0x01; expect(await verifyCmsSignature(tampered)).toBe('invalid'); expect(await verifyForCompletion({ cms, prepared, profile: 'cades-b' })).toEqual({ ok: true, }); }); } test('a PKCS#7 CMS carries signing-time and no ESS attribute', async () => { const signer = await createTestSigner(); const when = '2026-09-11T00:00:00.000Z'; const cms = await buildDetachedCms({ digest, hash: 'sha256', profile: 'pkcs7', signer, signingTime: when, }); const parsed = parseDetachedCms(cms); expect(parsed.signingTime).toBe(when); expect(parsed.signingCertificateV2).toBe(false); expect(parsed.signatureAlgorithm).toBe('1.2.840.113549.1.1.11'); expect(await verifyCmsSignature(cms)).toBe('valid'); expect(await verifyForCompletion({ cms, prepared, profile: 'pkcs7' })).toEqual({ ok: true }); // The same CMS does not satisfy the CAdES profile. const gate = await verifyForCompletion({ cms, prepared, profile: 'cades-b' }); expect(gate.ok).toBe(false); expect(!gate.ok && gate.reason).toBe('profile-violation'); }); test('the completion gate refuses what does not match the prepared digest', async () => { const signer = await createTestSigner(); const other = digest.slice(); other[0] ^= 0xff; const wrongDigest = await buildDetachedCms({ digest: other, hash: 'sha256', profile: 'cades-b', signer, }); expect( await verifyForCompletion({ cms: wrongDigest, prepared, profile: 'cades-b' }), ).toMatchObject({ ok: false, reason: 'digest-mismatch', }); const sha384 = await createTestSigner({ hash: 'sha384' }); const wrongAlgorithm = await buildDetachedCms({ digest: new Uint8Array(48), hash: 'sha384', profile: 'cades-b', signer: sha384, }); expect( await verifyForCompletion({ cms: wrongAlgorithm, prepared, profile: 'cades-b' }), ).toMatchObject({ ok: false, reason: 'algorithm-mismatch', }); const cms = await buildDetachedCms({ digest, hash: 'sha256', profile: 'cades-b', signer }); expect( await verifyForCompletion({ cms, prepared: { ...prepared, contentsSizeBytes: 16 }, profile: 'cades-b', }), ).toMatchObject({ ok: false, reason: 'too-large' }); expect( await verifyForCompletion({ cms: new Uint8Array([1, 2, 3]), prepared, profile: 'cades-b' }), ).toMatchObject({ ok: false, reason: 'malformed' }); const tampered = cms.slice(); tampered[tampered.byteLength - 2] ^= 0x10; expect( await verifyForCompletion({ cms: tampered, prepared, profile: 'cades-b' }), ).toMatchObject({ ok: false, reason: 'signature-invalid', }); }); test('a signer whose hash disagrees with the prepared digest is refused before signing', async () => { const signer = await createTestSigner({ hash: 'sha384' }); await expect( buildDetachedCms({ digest, hash: 'sha256', profile: 'cades-b', signer }), ).rejects.toThrow(/hashes with sha384/); const sha256: RawSigner = { ...(await createTestSigner()), hash: 'sha256' }; await expect( buildDetachedCms({ digest: new Uint8Array(20), hash: 'sha256', profile: 'cades-b', signer: sha256, }), ).rejects.toThrow(/20 bytes/); }); test('trust: the signer chains to its own certificate and to nothing else', async () => { const signer = await createTestSigner(); const stranger = await createTestSigner({ commonName: 'someone else' }); const cms = await buildDetachedCms({ digest, hash: 'sha256', profile: 'cades-b', signer }); expect(await validateChain(cms, null)).toMatchObject({ status: 'unknown' }); expect(await validateChain(cms, { anchors: async () => [] })).toMatchObject({ status: 'unknown', }); const trusted = await validateChain(cms, { anchors: async () => [signer.certificate] }); expect(trusted.status).toBe('trusted'); // PKI.js reports the anchor as the path's end even when the leaf is the anchor. expect((trusted.path?.length ?? 0) >= 1).toBe(true); expect(Array.from(trusted.path![trusted.path!.length - 1])).toEqual( Array.from(signer.certificate), ); const untrusted = await validateChain(cms, { anchors: async () => [stranger.certificate] }); expect(untrusted.status).toBe('untrusted'); // A chain judged before the certificate existed is untrusted. const early = await validateChain( cms, { anchors: async () => [signer.certificate] }, { kind: 'timestamp', stampedAt: '2000-01-01T00:00:00.000Z', tokenVerified: true, }, ); expect(early.status).toBe('untrusted'); }); }); describe('a time from 2050 on', () => { // UTCTime has two digits for the year and ends with 2049. RFC 5652 and RFC 5280 both have a // later time written as GeneralizedTime. test('a signing time is UTCTime through 2049 and GeneralizedTime after', async () => { const signer = await createTestSigner(); for (const [when, form] of [ ['2049-12-31T23:59:59.000Z', asn1js.UTCTime], ['2050-01-01T00:00:00.000Z', asn1js.GeneralizedTime], ['2071-06-15T08:30:00.000Z', asn1js.GeneralizedTime], ] as const) { const cms = await buildDetachedCms({ digest, hash: 'sha256', profile: 'pkcs7', signer, signingTime: when, }); const written = parseCmsInternal(cms).signerInfo.signedAttrs!.attributes.find( (attribute) => attribute.type === OID.signingTime, )!.values[0]; // A GeneralizedTime is a UTCTime to `instanceof`, so the class is compared itself. expect(written.constructor, when).toBe(form); expect(parseDetachedCms(cms).signingTime).toBe(when); expect(await verifyCmsSignature(cms)).toBe('valid'); expect(derProblem(cms), when).toBeNull(); expect(derProblem(signedAttributesAsSet(cms)), when).toBeNull(); } }); test('a signing time is written to the second', async () => { const cms = await buildDetachedCms({ digest, hash: 'sha256', profile: 'pkcs7', signer: await createTestSigner(), signingTime: '2050-03-01T10:00:00.750Z', }); expect(parseDetachedCms(cms).signingTime).toBe('2050-03-01T10:00:00.000Z'); expect(derProblem(cms)).toBeNull(); }); test("a certificate's dates follow the same rule", async () => { expect(x509Time(new Date('2049-12-31T23:59:59.000Z')).type).toBe(0); expect(x509Time(new Date('2050-01-01T00:00:00.000Z')).type).toBe(1); const notAfter = new Date('2061-05-04T12:00:00.000Z'); const identity = await createIdentity({ commonName: 'valid past 2050', notAfter }); expect(derProblem(identity.certificate)).toBeNull(); expect(certificateFromDer(identity.certificate).notAfter.value).toEqual(notAfter); }); });