import { createHash } from "node:crypto"; import { createRequire } from "node:module"; import { serializeExportTableToPdf } from "@hivegrid/exports"; import { describe, expect, it } from "vitest"; import { buildRfc3161TimestampRequest, readTimestampResponseBytes, sealPdfWithPades } from "./padesSeal.server"; const requireNode = createRequire(import.meta.url); function createTestPkcs12(passphrase: string): Buffer { const forge = requireNode("node-forge"); const keys = forge.pki.rsa.generateKeyPair(1024); const certificate = forge.pki.createCertificate(); certificate.publicKey = keys.publicKey; certificate.serialNumber = "01"; certificate.validity.notBefore = new Date("2025-01-01T00:00:00.000Z"); certificate.validity.notAfter = new Date("2030-01-01T00:00:00.000Z"); const attributes = [{ name: "commonName", value: "HiveGrid test signer" }]; certificate.setSubject(attributes); certificate.setIssuer(attributes); certificate.sign(keys.privateKey, forge.md.sha256.create()); const p12 = forge.pkcs12.toPkcs12Asn1(keys.privateKey, [certificate], passphrase, { algorithm: "3des", }); return Buffer.from(forge.asn1.toDer(p12).getBytes(), "binary"); } describe("buildRfc3161TimestampRequest", () => { it("builds a DER encoded RFC3161 request for a SHA-256 message imprint", () => { const digest = createHash("sha256").update("pdf bytes").digest(); const request = buildRfc3161TimestampRequest(digest, Buffer.from("0102030405060708", "hex")); expect(request.byteLength).toBeGreaterThan(40); expect(request[0]).toBe(0x30); expect(request.includes(Buffer.from("608648016503040201", "hex"))).toBe(true); expect(request.includes(digest)).toBe(true); }); }); describe("readTimestampResponseBytes", () => { it("bounds both declared and actual response bytes", async () => { const valid = new Response(new Uint8Array([1, 2, 3]), { headers: { "content-length": "3" } }); await expect(readTimestampResponseBytes(valid, 3)).resolves.toEqual(new Uint8Array([1, 2, 3])); const declaredTooLarge = new Response(new Uint8Array([1]), { headers: { "content-length": "4" } }); await expect(readTimestampResponseBytes(declaredTooLarge, 3)).rejects.toThrow("size limit"); const understated = new Response(new Uint8Array([1, 2, 3, 4]), { headers: { "content-length": "2" } }); await expect(readTimestampResponseBytes(understated, 3)).rejects.toThrow("size limit"); }); }); describe("sealPdfWithPades", () => { it("signs a generated PDF with the supported PKCS#12 and PDF dependency graph", async () => { const passphrase = "test-passphrase"; const original = { p12: process.env.SIGNATURE_P12_BASE64, passphrase: process.env.SIGNATURE_P12_PASSPHRASE, requirePades: process.env.SIGNATURE_REQUIRE_PADES, tsaUrl: process.env.SIGNATURE_TSA_URL, }; process.env.SIGNATURE_P12_BASE64 = createTestPkcs12(passphrase).toString("base64"); process.env.SIGNATURE_P12_PASSPHRASE = passphrase; process.env.SIGNATURE_REQUIRE_PADES = "true"; delete process.env.SIGNATURE_TSA_URL; try { const pdfBytes = await serializeExportTableToPdf( { columns: [{ key: "value", label: "Value" }], rows: [{ value: "signed" }] }, { title: "Dependency compatibility" }, ); const result = await sealPdfWithPades({ pdfBytes, title: "Dependency compatibility", signerName: "HiveGrid test signer", contactInfo: "security@example.invalid", location: "Test", reason: "Dependency compatibility", signingTime: new Date("2026-01-01T00:00:00.000Z"), }); const signedPdf = Buffer.from(result.bytes).toString("latin1"); expect(result.mode).toBe("pades_cades_detached"); expect(result.certificateConfigured).toBe(true); expect(result.bytes.byteLength).toBeGreaterThan(pdfBytes.byteLength); expect(signedPdf).toContain("/ByteRange"); expect(signedPdf).toContain("/SubFilter /ETSI.CAdES.detached"); } finally { for (const [key, value] of Object.entries({ SIGNATURE_P12_BASE64: original.p12, SIGNATURE_P12_PASSPHRASE: original.passphrase, SIGNATURE_REQUIRE_PADES: original.requirePades, SIGNATURE_TSA_URL: original.tsaUrl, })) { if (value === undefined) delete process.env[key]; else process.env[key] = value; } } }); });