@bilukit/seal
Electronic seals. A PAdES baseline (B-B) signature on a PDF, written as an incremental update: the original bytes
stay as they were, so an earlier signature stays valid. The check of every signature in a PDF. A detached CAdES
signature over any bytes (an XML invoice, an export), and its check. A library: no tables, no configuration.
import { sealKeyFromPkcs12, sealPdf, verifyPdf } from "@bilukit/seal";
// the certificate is a secret: keep it with @bilukit/connections, open it at call time
const key = sealKeyFromPkcs12(p12Bytes, password);
const sealed = await sealPdf(pdf, key, {
reason: "Issued by Example Ltd",
location: "Budapest",
visible: { page: 1, rect: { x: 360, y: 40, width: 190, height: 56 } }, // leave out for an invisible seal
});
for (const s of await verifyPdf(sealed)) {
// { field: "Seal1", intact: true, changedAfter: false, signer: { commonName: "Example Ltd", ... }, signingTime, ... }
}
Sealing parses the whole PDF: run it in a job.
No dependencies of its own: hashing and signing are node:crypto, and the CMS, X.509 and PKCS#12 structures are
read and written by a small ASN.1 reader in the package, which never re-encodes what it checks. The PDF functions
need pdf-lib, an optional peer (npm install pdf-lib), loaded on first use:
an app that only signs bytes with CAdES does not install it.
| export |
what |
sealKeyFromPkcs12(bytes, password) |
a SealKey from a .p12/.pfx file, the legacy RC2/3DES ones too; RSA or EC |
sealKeyFromPem({ key, certificate, password? }) |
a SealKey from PEM; the certificate text may carry the chain, which goes into every signature |
createSelfSignedSeal({ name, ... }) |
a self-signed certificate and its key, PEM - see below |
sealPdf(pdf, key, options?) |
the sealed PDF: /SubFilter /ETSI.CAdES.detached, signed content-type, message-digest and signing-certificate-v2; the claimed time in /M; reason, location, contactInfo, name, field, visible |
verifyPdf(pdf) |
every signed range once, in the order made: intact (digest and signature), changedAfter, signer, signingTime, digestAlgorithm, weakDigest, certificateBound, timestamped, problem |
cadesSign(bytes, key, { signingTime? }) |
a detached CAdES B-B signature (DER, a .p7s), signing-time included |
cadesVerify(signature, bytes) |
the same facts as for a PDF signature; a signature it cannot read throws SealError |
SealError |
what bad input throws: a key that does not open, a PDF that cannot be sealed, a signature that cannot be read |
A self-signed seal is not a qualified one. createSelfSignedSeal makes a certificate that nobody vouches for: a
reader shows the seal, says the document has not changed since, and says it does not know the signer. It is meant for
development, and for an organisation that wants tamper evidence before it buys a qualified electronic seal from a
trust service provider - which then replaces it with no change to the code.
The check reports facts, not a verdict. intact says the digest matches the signed byte range and the signature
verifies with the certificate it carries - nothing about who that is. Only a signature dictionary the signature covers
is reported with its reason, name, location and /M time: one a later update redefined, or one that borrows
another signature's range, is a problem without them. field is the form's current name for it. weakDigest
marks SHA-1 or MD5, whose collisions make intact worth little. changedAfter
says something was appended later (another signature, a form fill, an edit - which one is not judged);
signer.selfSigned says nobody else issued the certificate. Whether a certificate is qualified, trusted, revoked or
was valid at the time needs a trust list and revocation data: that is the reader's decision, made elsewhere.
What it does not do
- No time-stamp, no long-term validation (B-T, B-LT, B-LTA): the signing time is the sealer's claim. A
document time-stamp already in a PDF is checked (its token's signature and imprint) but not trusted.
- No certification (DocMDP) signatures, no field locking: a seal is an approval signature. A PDF certified with no
change allowed (DocMDP P=1), or one whose signed field locks the new field, is refused rather than broken.
- No encrypted PDFs for sealing. A PDF whose cross-reference is damaged is rewritten first - unless it already
carries a signature, which the rewrite would break: then it is refused.
- The visible seal's text is Helvetica (WinAnsi): a letter outside it loses its accent or becomes "?". Pass an
image for anything else. Rotated pages are not compensated.