@qrcommunication/gigapdf-lib 0.69.0 → 0.71.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,87 @@ All notable changes to `@qrcommunication/gigapdf-lib` are documented here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/) and the
5
5
  project adheres to [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## [0.71.0] - 2026-06-23
8
+
9
+ Long-term validation release: PAdES-LTV builds on the B-T timestamped signatures
10
+ from 0.70 by embedding the validation material (certificate chain + revocation
11
+ responses) so a signature keeps verifying long after its certificates expire or
12
+ are revoked. The public API is additive — existing behaviour is unchanged.
13
+
14
+ ### Added
15
+
16
+ - **PAdES-LTV (B-LT / B-LTA).** New SDK `GigaPdfDoc.signLtv()` (async) produces a
17
+ long-term-validation signature: it first builds a B-T signature
18
+ (`signTimestamped`), then embeds a Document Security Store (`/DSS` with
19
+ `/Certs`, `/OCSPs`, `/CRLs`, and per-signature `/VRI`) carrying the revocation
20
+ material for the certificate chain (B-LT). With `archiveTimestamp` it also adds
21
+ a `/DocTimeStamp` document timestamp (`ETSI.RFC3161` subfilter) over the whole
22
+ updated file for B-LTA, refreshing the long-term trust anchor. The engine
23
+ computes *which* OCSP/CRL endpoints to query from the certificates' AIA / CRL-DP
24
+ extensions; the host fetches them (the WASM core has no network stack, same
25
+ pure-data two-phase model as the TSA). OCSP requests follow RFC 6960; CRLs are
26
+ parsed as `CertificateList`. The exported `defaultOcspPost` and `defaultCrlGet`
27
+ perform the round trips via `fetch`, and the `revocationFetch` / `crlFetch`
28
+ hooks let the host add auth/proxy/retries **and apply its own SSRF allow-list**.
29
+
30
+ ### Fixed
31
+
32
+ - **B-T `id-aa-timeStampToken` now carries the bare `TimeStampToken`.**
33
+ `signFinishTimestamped` / `signTimestamped` previously embedded the TSA's raw
34
+ `TimeStampResp` (`SEQUENCE { PKIStatusInfo, TimeStampToken }`) verbatim in the
35
+ `id-aa-timeStampToken` unsigned attribute. The engine now unwraps the response
36
+ to the bare `TimeStampToken` (a CMS `ContentInfo`) before embedding it — as
37
+ required by RFC 3161 §3.3.2 / ETSI EN 319 122 — matching the B-LTA
38
+ document-timestamp path. Both a raw `TimeStampResp` and an already-unwrapped
39
+ token are accepted (the `PKIStatusInfo` gate is still enforced).
40
+
41
+ ## [0.70.0] - 2026-06-23
42
+
43
+ Fidelity + standards release: advanced (PAdES-B-T) timestamped signatures,
44
+ richer shading and JPEG decoding at the rasteriser, complex-script text shaping
45
+ for Indic writing systems, CFF flex curves, and RTF image import. The public API
46
+ is additive — existing behaviour is unchanged.
47
+
48
+ ### Added
49
+
50
+ - **PAdES-B-T trusted timestamps (RFC 3161).** New SDK
51
+ `GigaPdfDoc.signTimestamped()` (async) embeds an RFC 3161 timestamp token in
52
+ the SignerInfo for an *advanced*-level PAdES-B-T signature — `ETSI.CAdES.detached`
53
+ subfilter, `signing-certificate-v2` (ESS) signed attribute, and the
54
+ `id-aa-timeStampToken` unsigned attribute. Uses the engine's pure-data
55
+ two-phase TSA flow (core emits the `TimeStampReq`, host POSTs it, core embeds
56
+ the returned token) since the WASM core has no network stack; `tsaFetch` lets
57
+ the host add auth/proxy/retries **and apply its own SSRF allow-list**, and the
58
+ exported `defaultTsaPost` POSTs `application/timestamp-query` via `fetch`
59
+ (e.g. FreeTSA). Signs with an imported PKCS#12 or a freshly generated
60
+ self-signed identity.
61
+ - **Mesh shadings at the rasteriser.** Free-form (type 4), lattice (type 5),
62
+ Coons (type 6) and tensor (type 7) shadings are now rendered as Gouraud
63
+ triangles (pure, zero-dep decoder; Coons/tensor patches tessellated per
64
+ ISO 32000-1 §8.7.4.5.7), with per-vertex colour resolved through
65
+ `Separation`/`DeviceN`/`ICCBased`/`CMYK`/`Gray`. Axial (2) and radial (3)
66
+ shadings are unchanged.
67
+ - **Arithmetic-coded JPEG decoding.** SOF9 (sequential) and SOF10 (progressive)
68
+ JPEGs now decode via a hand-rolled ISO/IEC 10918-1 Annex MQ arithmetic decoder
69
+ with the F.1.4 DC/AC context models and `DAC` conditioning. Baseline/Huffman
70
+ paths are unchanged; lossless (SOF3/SOF11) and 12-bit Huffman (SOF1) remain
71
+ gracefully unsupported.
72
+ - **Indic complex-script shaping.** A syllabic reordering machine for the
73
+ Brahmi-derived scripts (Devanagari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil,
74
+ Telugu, Kannada, Malayalam) — reph and pre-base matra reordering — plus the
75
+ missing OpenType lookups: GSUB 2 (multiple), GSUB 3 (alternate), GSUB 8
76
+ (reverse chaining single) and GPOS 3 (cursive attachment). Latin and the
77
+ existing contextual paths are unchanged.
78
+ - **CFF/Type2 flex operators.** The Type2 charstring interpreter now implements
79
+ the four flex operators (`flex`, `flex1`, `hflex`, `hflex1`, Adobe TN #5177),
80
+ each emitting two cubic curves — CFF glyphs using flex no longer drop or
81
+ mis-render contour segments.
82
+ - **RTF image import.** RTF import parses the `\pict` group, extracting
83
+ `\pngblip`/`\jpegblip` payloads as `<img src="data:image/…;base64,…">`
84
+ (display size recovered from `\picwgoal`/`\pichgoal`), reusing the HTML
85
+ engine's image-embed pipeline. DIB/BMP, WMF/EMF and binary `\bin` payloads are
86
+ skipped (documented limits), guarded by a PNG/JPEG magic-byte check.
87
+
7
88
  ## [0.69.0] - 2026-06-23
8
89
 
9
90
  Image-watermark release: stamp a raster image across any range of pages, with
package/dist/index.cjs CHANGED
@@ -21,7 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  GigaPdfDoc: () => GigaPdfDoc,
24
- GigaPdfEngine: () => GigaPdfEngine
24
+ GigaPdfEngine: () => GigaPdfEngine,
25
+ defaultCrlGet: () => defaultCrlGet,
26
+ defaultOcspPost: () => defaultOcspPost,
27
+ defaultTsaPost: () => defaultTsaPost
25
28
  });
26
29
  module.exports = __toCommonJS(index_exports);
27
30
  var import_node_fs = require("./node-fs.js");
@@ -853,6 +856,18 @@ var GigaPdfEngine = class _GigaPdfEngine {
853
856
  );
854
857
  }
855
858
  };
859
+ async function defaultTsaPost(url, req) {
860
+ const res = await fetch(url, {
861
+ method: "POST",
862
+ headers: { "Content-Type": "application/timestamp-query" },
863
+ body: req,
864
+ redirect: "error"
865
+ });
866
+ if (!res.ok) {
867
+ throw new Error(`TSA HTTP ${res.status}`);
868
+ }
869
+ return new Uint8Array(await res.arrayBuffer());
870
+ }
856
871
  function packHtmlResources(resources) {
857
872
  let size = 4;
858
873
  for (const r of resources) size += 4 + enc.encode(r.url).length + 4 + r.bytes.length;
@@ -899,6 +914,47 @@ function packHtmlFonts(fonts) {
899
914
  }
900
915
  return buf;
901
916
  }
917
+ async function defaultOcspPost(req, url) {
918
+ const res = await fetch(url, {
919
+ method: "POST",
920
+ headers: { "Content-Type": "application/ocsp-request" },
921
+ body: req,
922
+ redirect: "error"
923
+ });
924
+ if (!res.ok) {
925
+ throw new Error(`OCSP HTTP ${res.status}`);
926
+ }
927
+ return new Uint8Array(await res.arrayBuffer());
928
+ }
929
+ async function defaultCrlGet(url) {
930
+ const res = await fetch(url, { method: "GET", redirect: "error" });
931
+ if (!res.ok) {
932
+ throw new Error(`CRL HTTP ${res.status}`);
933
+ }
934
+ return new Uint8Array(await res.arrayBuffer());
935
+ }
936
+ function hexToBytes(hex) {
937
+ const out = new Uint8Array(hex.length >> 1);
938
+ for (let i = 0; i < out.length; i++) {
939
+ out[i] = parseInt(hex.substr(i * 2, 2), 16);
940
+ }
941
+ return out;
942
+ }
943
+ function frameBlobs(blobs) {
944
+ let total = 4;
945
+ for (const b of blobs) total += 4 + b.length;
946
+ const out = new Uint8Array(total);
947
+ const view = new DataView(out.buffer);
948
+ view.setUint32(0, blobs.length, true);
949
+ let pos = 4;
950
+ for (const b of blobs) {
951
+ view.setUint32(pos, b.length, true);
952
+ pos += 4;
953
+ out.set(b, pos);
954
+ pos += b.length;
955
+ }
956
+ return out;
957
+ }
902
958
  var RGB = (rgb) => rgb & 16777215;
903
959
  function styleArgs(s = {}) {
904
960
  const hasBorder = s.border === null ? 0 : 1;
@@ -1748,6 +1804,231 @@ var GigaPdfDoc = class {
1748
1804
  }
1749
1805
  return out;
1750
1806
  }
1807
+ /**
1808
+ * Sign with an embedded **RFC 3161 trusted timestamp** (PAdES-B-T). Unlike
1809
+ * {@link sign}/{@link signP12} this is `async`: the timestamp requires a
1810
+ * network round trip to a TSA, so the method runs the engine's two-phase flow
1811
+ * — build the signature → POST the `TimeStampReq` to the TSA → embed the
1812
+ * returned token — with the HTTP in between.
1813
+ *
1814
+ * The signing identity is `opts.p12` (+ `opts.password`) when supplied, else a
1815
+ * generated self-signed digital ID from `opts.random` (+ `notBefore`/
1816
+ * `notAfter`). `opts.tsaUrl` is the TSA endpoint (e.g. FreeTSA
1817
+ * `https://freetsa.org/tsr`); pass `opts.tsaFetch` to customise the request
1818
+ * (auth, proxy, SSRF allow-list). Throws a single generic error on any failure
1819
+ * (bad identity, TSA HTTP error, malformed response, or signature too large).
1820
+ */
1821
+ async signTimestamped(opts) {
1822
+ const usingP12 = opts.p12 != null && opts.p12.length > 0;
1823
+ if (!usingP12 && (opts.random == null || opts.random.length < 256)) {
1824
+ throw new Error(
1825
+ "signTimestamped: self-signed path needs `random` \u2265 256 bytes (or pass a `p12`)"
1826
+ );
1827
+ }
1828
+ const fields = [
1829
+ opts.name ?? "",
1830
+ opts.reason ?? "",
1831
+ opts.date ?? "",
1832
+ opts.location ?? "",
1833
+ opts.contactInfo ?? "",
1834
+ opts.notBefore ?? "",
1835
+ opts.notAfter ?? ""
1836
+ ].join(" ");
1837
+ const rand = opts.random ?? new Uint8Array(0);
1838
+ const p12 = opts.p12 ?? new Uint8Array(0);
1839
+ const nonce = opts.nonce ?? new Uint8Array(0);
1840
+ const keyBits = opts.keyBits ?? 2048;
1841
+ const rPtr = this.g._toWasm(rand);
1842
+ const p12Ptr = this.g._toWasm(p12);
1843
+ const noncePtr = this.g._toWasm(nonce);
1844
+ let request;
1845
+ try {
1846
+ request = this.g._withStr(
1847
+ opts.password ?? "",
1848
+ (pwP, pwL) => this.g._withStr(
1849
+ fields,
1850
+ (fP, fL) => this.g._buffer(
1851
+ (o) => this.ex().gp_sign_prepare_tsa(
1852
+ this.h,
1853
+ fP,
1854
+ fL,
1855
+ rPtr,
1856
+ rand.length,
1857
+ keyBits,
1858
+ p12Ptr,
1859
+ p12.length,
1860
+ pwP,
1861
+ pwL,
1862
+ noncePtr,
1863
+ nonce.length,
1864
+ o
1865
+ )
1866
+ )
1867
+ )
1868
+ );
1869
+ } finally {
1870
+ this.g._free(rPtr, rand.length);
1871
+ this.g._free(p12Ptr, p12.length);
1872
+ this.g._free(noncePtr, nonce.length);
1873
+ }
1874
+ if (request.length === 0) {
1875
+ throw new Error(
1876
+ "timestamped signing failed: invalid identity or could not build the timestamp request"
1877
+ );
1878
+ }
1879
+ const response = opts.tsaFetch ? await opts.tsaFetch(request, opts.tsaUrl) : await defaultTsaPost(opts.tsaUrl, request);
1880
+ const tokenPtr = this.g._toWasm(response);
1881
+ let out;
1882
+ try {
1883
+ out = this.g._buffer(
1884
+ (o) => this.ex().gp_sign_finish_tsa(this.h, tokenPtr, response.length, o)
1885
+ );
1886
+ } finally {
1887
+ this.g._free(tokenPtr, response.length);
1888
+ }
1889
+ if (out.length === 0) {
1890
+ throw new Error(
1891
+ "timestamped signing failed: TSA response rejected or signature too large for the reserved space"
1892
+ );
1893
+ }
1894
+ return out;
1895
+ }
1896
+ /**
1897
+ * Sign with **long-term validation** material embedded (PAdES-B-LT, or B-LTA
1898
+ * with `opts.archiveTimestamp`). Builds a B-T signature ({@link signTimestamped}),
1899
+ * then fetches the certificate chain's revocation material (OCSP responses /
1900
+ * CRLs, by URL **from the certificates** — the engine computes which URLs, the
1901
+ * host fetches) and stores it in a `/DSS`, so the signature validates long after
1902
+ * the certificates expire or are revoked. With `archiveTimestamp` a document
1903
+ * timestamp over the whole file (DSS included) is added for renewable archival.
1904
+ *
1905
+ * `async`, multi-round-trip: one TSA POST for the B-T timestamp, one OCSP/CRL
1906
+ * fetch per chain certificate, and (if `archiveTimestamp`) a second TSA POST.
1907
+ * Unreachable responders are skipped — the DSS is built from whatever resolves;
1908
+ * a self-signed identity (no AIA/CRL-DP) simply yields a `/DSS/Certs`-only store.
1909
+ * Override `tsaFetch`/`revocationFetch`/`crlFetch` to add auth, proxies, or an
1910
+ * SSRF allow-list. Throws on a fatal failure (bad identity, B-T signature, or a
1911
+ * malformed PDF the DSS can't chain to).
1912
+ */
1913
+ async signLtv(opts) {
1914
+ const signed = await this.signTimestamped(opts);
1915
+ const nonce = opts.nonce ?? new Uint8Array(0);
1916
+ const targetsJson = this.withSignedPdf(signed, (pdfPtr, pdfLen) => {
1917
+ const noncePtr = this.g._toWasm(nonce);
1918
+ try {
1919
+ return dec.decode(
1920
+ this.g._buffer(
1921
+ (o) => this.ex().gp_ltv_targets(pdfPtr, pdfLen, noncePtr, nonce.length, o)
1922
+ )
1923
+ );
1924
+ } finally {
1925
+ this.g._free(noncePtr, nonce.length);
1926
+ }
1927
+ });
1928
+ const targets = targetsJson ? JSON.parse(targetsJson) : [];
1929
+ const certs = [];
1930
+ const ocsps = [];
1931
+ const crls = [];
1932
+ const ocspPost = opts.revocationFetch ?? defaultOcspPost;
1933
+ const crlGet = opts.crlFetch ?? defaultCrlGet;
1934
+ for (const target of targets) {
1935
+ certs.push(hexToBytes(target.certHex));
1936
+ for (const source of target.sources) {
1937
+ try {
1938
+ if (source.kind === "ocsp") {
1939
+ ocsps.push(await ocspPost(hexToBytes(source.requestHex), source.url));
1940
+ } else {
1941
+ crls.push(await crlGet(source.url));
1942
+ }
1943
+ } catch {
1944
+ }
1945
+ }
1946
+ }
1947
+ let lt = this.applyDss(signed, certs, ocsps, crls);
1948
+ if (opts.archiveTimestamp) {
1949
+ lt = await this.appendDocumentTimestamp(lt, opts);
1950
+ }
1951
+ return lt;
1952
+ }
1953
+ /** Run `fn` with `pdf` copied into wasm memory, freeing it afterwards. */
1954
+ withSignedPdf(pdf, fn) {
1955
+ const ptr = this.g._toWasm(pdf);
1956
+ try {
1957
+ return fn(ptr, pdf.length);
1958
+ } finally {
1959
+ this.g._free(ptr, pdf.length);
1960
+ }
1961
+ }
1962
+ /** Add the `/DSS` to `signed` as an incremental update (B-LT). */
1963
+ applyDss(signed, certs, ocsps, crls) {
1964
+ const certsBuf = frameBlobs(certs);
1965
+ const ocspsBuf = frameBlobs(ocsps);
1966
+ const crlsBuf = frameBlobs(crls);
1967
+ const pdfPtr = this.g._toWasm(signed);
1968
+ const cPtr = this.g._toWasm(certsBuf);
1969
+ const oPtr = this.g._toWasm(ocspsBuf);
1970
+ const rPtr = this.g._toWasm(crlsBuf);
1971
+ let out;
1972
+ try {
1973
+ out = this.g._buffer(
1974
+ (res) => this.ex().gp_apply_dss(
1975
+ pdfPtr,
1976
+ signed.length,
1977
+ cPtr,
1978
+ certsBuf.length,
1979
+ oPtr,
1980
+ ocspsBuf.length,
1981
+ rPtr,
1982
+ crlsBuf.length,
1983
+ res
1984
+ )
1985
+ );
1986
+ } finally {
1987
+ this.g._free(pdfPtr, signed.length);
1988
+ this.g._free(cPtr, certsBuf.length);
1989
+ this.g._free(oPtr, ocspsBuf.length);
1990
+ this.g._free(rPtr, crlsBuf.length);
1991
+ }
1992
+ if (out.length === 0) {
1993
+ throw new Error("LTV failed: could not add the /DSS to the signed PDF");
1994
+ }
1995
+ return out;
1996
+ }
1997
+ /** Append a document timestamp over the whole `pdf` (B-LTA), TSA round trip
1998
+ * included. */
1999
+ async appendDocumentTimestamp(pdf, opts) {
2000
+ const nonce = opts.nonce ?? new Uint8Array(0);
2001
+ const pdfPtr = this.g._toWasm(pdf);
2002
+ const noncePtr = this.g._toWasm(nonce);
2003
+ let request;
2004
+ try {
2005
+ request = this.g._buffer(
2006
+ (o) => this.ex().gp_doc_timestamp_prepare(this.h, pdfPtr, pdf.length, noncePtr, nonce.length, o)
2007
+ );
2008
+ } finally {
2009
+ this.g._free(pdfPtr, pdf.length);
2010
+ this.g._free(noncePtr, nonce.length);
2011
+ }
2012
+ if (request.length === 0) {
2013
+ throw new Error("LTV archive timestamp failed: could not build the timestamp request");
2014
+ }
2015
+ const response = opts.tsaFetch ? await opts.tsaFetch(request, opts.tsaUrl) : await defaultTsaPost(opts.tsaUrl, request);
2016
+ const tokenPtr = this.g._toWasm(response);
2017
+ let out;
2018
+ try {
2019
+ out = this.g._buffer(
2020
+ (o) => this.ex().gp_doc_timestamp_finish(this.h, tokenPtr, response.length, o)
2021
+ );
2022
+ } finally {
2023
+ this.g._free(tokenPtr, response.length);
2024
+ }
2025
+ if (out.length === 0) {
2026
+ throw new Error(
2027
+ "LTV archive timestamp failed: TSA response rejected or token too large for the reserved space"
2028
+ );
2029
+ }
2030
+ return out;
2031
+ }
1751
2032
  // metadata
1752
2033
  getMetadata(key) {
1753
2034
  return this.g._withStr(
@@ -2147,6 +2428,9 @@ var GigaPdfDoc = class {
2147
2428
  // Annotate the CommonJS export names for ESM import in node:
2148
2429
  0 && (module.exports = {
2149
2430
  GigaPdfDoc,
2150
- GigaPdfEngine
2431
+ GigaPdfEngine,
2432
+ defaultCrlGet,
2433
+ defaultOcspPost,
2434
+ defaultTsaPost
2151
2435
  });
2152
2436
  //# sourceMappingURL=index.cjs.map