Skip to content

Digital Signing

SIDHARTH SINGH edited this page Dec 3, 2026 · 1 revision

Digital Signing (signPdf) & Verification (verifyPdfSignatures)

Government tenders, tax filings, and legal agreements require cryptographic signatures that preserve every byte signed earlier while letting downstream officers add co-signatures cleanly.

Why Incremental Saving Matters

Full-rewrite PDF libraries alter object numbering and cross-reference offsets every time they save a file—instantly breaking existing digital signatures. ONEX includes writePdfIncremental(), which keeps the original PDF prefix untouched down to the last byte, appends /AcroForm + /Widget + /Type /Sig objects, chains a new xref section back to the previous startxref via /Prev, and computes exact /ByteRange hashes over the updated bytes.

Supported Signing Backends

  • PKCS#12 / PFX containers: signer: { p12Path | p12Buffer, passphrase } parsed via pure TypeScript ASN.1 safe-bag extraction plus Node OpenSSL PKCS#8 decryption.
  • PEM key pairs: signer: { keyPem, certPem, chainPem?, passphrase? }.
  • External HSM / USB eToken / Aadhaar eSign callback: Pass signer: { certPem, chainPem?, sign(data, meta) } to sign CMS attributes asynchronously on external hardware.

Example

import { signPdf, verifyPdfSignatures } from 'onex-pdf';

const signed = await signPdf(pdfBuffer, {
  signer: { p12Path: 'officer.p12', passphrase: 'secret' },
  reason: 'Procurement Approval',
  location: 'New Delhi, IN',
  docMdp: 'formFillAndSign',
  tsaUrl: 'https://tsa.example.in',
  appearance: { position: 'bottomRight', includeDate: true },
});

const result = verifyPdfSignatures(signed, {
  trustedCaPem: rootCaPem,
  checkExpiry: true,
});

Inspecting result gives you per-signature digest status, hasTimestamp, docMdpPermission (1, 2, or 3), certificate validity at verification time, and trust-chain verification (chainValid). Note that tsaUrl passes through ONEX's SSRF validator (checkUrlAsync), blocking internal/loopback URLs by default unless explicitly permitted in security.

Clone this wiki locally