@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/dist/index.d.cts CHANGED
@@ -372,6 +372,16 @@ declare class GigaPdfEngine {
372
372
  /** Raise a model to an EPUB e-book (`.epub`). */
373
373
  modelToEpub(model: GigaDocument): Uint8Array;
374
374
  }
375
+ /**
376
+ * Default TSA round trip for {@link GigaPdfDoc.signTimestamped}: POST the
377
+ * RFC 3161 `TimeStampReq` to `url` as `application/timestamp-query` and return
378
+ * the raw `TimeStampResp` (`application/timestamp-reply`) body. Works in both
379
+ * Node and the browser via the global `fetch`.
380
+ *
381
+ * No SSRF allow-listing is performed here — the URL is host-supplied. Consumers
382
+ * that need to restrict it should pass their own `tsaFetch`.
383
+ */
384
+ declare function defaultTsaPost(url: string, req: Uint8Array): Promise<Uint8Array>;
375
385
  /** A Google font the HTML engine needs (resolved from the catalogue). */
376
386
  interface HtmlFontRequest {
377
387
  family: string;
@@ -1178,6 +1188,87 @@ interface SignP12Options {
1178
1188
  /** `/ContactInfo` — how to reach the signer. */
1179
1189
  contactInfo?: string;
1180
1190
  }
1191
+ /**
1192
+ * Options for {@link GigaPdfDoc.signTimestamped} — a PAdES-B-T signature with an
1193
+ * RFC 3161 trusted timestamp embedded in the SignerInfo (`ETSI.CAdES.detached`
1194
+ * subfilter, `signing-certificate-v2` signed attribute, `id-aa-timeStampToken`
1195
+ * unsigned attribute).
1196
+ *
1197
+ * The signing identity is either an imported PKCS#12 (`p12` + `password`) or, if
1198
+ * `p12` is omitted, a freshly generated self-signed digital ID (`random` +
1199
+ * `notBefore`/`notAfter`).
1200
+ */
1201
+ interface SignTsaOptions extends SignP12Options {
1202
+ /** TSA endpoint URL, e.g. `"https://freetsa.org/tsr"`. */
1203
+ tsaUrl: string;
1204
+ /**
1205
+ * Optional override for the TSA round trip — lets the host add auth headers,
1206
+ * proxies, retries, **and apply its own SSRF allow-list** (the engine only
1207
+ * emits the request; the URL is host-supplied). Receives the `TimeStampReq`
1208
+ * DER and the URL, must resolve to the raw `TimeStampResp` bytes. When omitted,
1209
+ * {@link defaultTsaPost} POSTs `application/timestamp-query` via `fetch`.
1210
+ */
1211
+ tsaFetch?: (req: Uint8Array, url: string) => Promise<Uint8Array>;
1212
+ /** PKCS#12 identity bytes. Omit to sign with a generated self-signed ID. */
1213
+ p12?: Uint8Array;
1214
+ /** PKCS#12 passphrase. */
1215
+ password?: string;
1216
+ /** Self-signed path: ≥ 256 bytes from `crypto.getRandomValues`. */
1217
+ random?: Uint8Array;
1218
+ /** Self-signed path: RSA modulus size in bits (default 2048). */
1219
+ keyBits?: number;
1220
+ /** Self-signed path: certificate `notBefore`, UTCTime `YYMMDDHHMMSSZ`. */
1221
+ notBefore?: string;
1222
+ /** Self-signed path: certificate `notAfter`, UTCTime `YYMMDDHHMMSSZ`. */
1223
+ notAfter?: string;
1224
+ /** Optional 8–16 random bytes echoed by the TSA (request/response correlation). */
1225
+ nonce?: Uint8Array;
1226
+ }
1227
+ /**
1228
+ * Options for {@link GigaPdfDoc.signLtv} — a PAdES long-term validation
1229
+ * signature: a B-T signature, then a `/DSS` (Document Security Store) carrying
1230
+ * the certificate chain + OCSP/CRL revocation material the host fetched (B-LT),
1231
+ * optionally finished with a document timestamp over the whole file (B-LTA).
1232
+ *
1233
+ * Extends {@link SignTsaOptions}: the B-T signature is produced exactly as
1234
+ * {@link GigaPdfDoc.signTimestamped}, then the LTV material is added. The OCSP/CRL
1235
+ * URLs come from the **certificates**, so the host fetches them — supply
1236
+ * `revocationFetch`/`crlFetch` to add auth, proxies, or an SSRF allow-list (the
1237
+ * engine only computes which URLs to fetch).
1238
+ */
1239
+ interface SignLtvOptions extends SignTsaOptions {
1240
+ /**
1241
+ * Add a B-LTA **document timestamp** over the whole file (DSS included) after
1242
+ * the DSS. Requires a second TSA round trip. Default `false` (B-LT only).
1243
+ */
1244
+ archiveTimestamp?: boolean;
1245
+ /**
1246
+ * Override the OCSP round trip — receives the DER `OCSPRequest` and the
1247
+ * responder URL, must resolve to the raw `OCSPResponse` bytes. When omitted,
1248
+ * {@link defaultOcspPost} POSTs `application/ocsp-request` via `fetch`. Throw to
1249
+ * skip an unreachable responder (the DSS is built from whatever succeeds).
1250
+ */
1251
+ revocationFetch?: (req: Uint8Array, url: string) => Promise<Uint8Array>;
1252
+ /**
1253
+ * Override the CRL fetch — receives the CRL distribution-point URL, must
1254
+ * resolve to the raw `CertificateList` (CRL) bytes. When omitted,
1255
+ * {@link defaultCrlGet} GETs the URL. Throw to skip an unreachable CRL.
1256
+ */
1257
+ crlFetch?: (url: string) => Promise<Uint8Array>;
1258
+ }
1259
+ /**
1260
+ * Default OCSP round trip for {@link GigaPdfDoc.signLtv}: POST the DER
1261
+ * `OCSPRequest` to `url` as `application/ocsp-request` and return the raw
1262
+ * `OCSPResponse` body. No SSRF allow-listing — the URL comes from the
1263
+ * certificate's AIA extension; pass `revocationFetch` to restrict it.
1264
+ */
1265
+ declare function defaultOcspPost(req: Uint8Array, url: string): Promise<Uint8Array>;
1266
+ /**
1267
+ * Default CRL fetch for {@link GigaPdfDoc.signLtv}: GET the `CertificateList`
1268
+ * (CRL) from `url`. No SSRF allow-listing — the URL comes from the certificate's
1269
+ * CRL-DP extension; pass `crlFetch` to restrict it.
1270
+ */
1271
+ declare function defaultCrlGet(url: string): Promise<Uint8Array>;
1181
1272
  /**
1182
1273
  * A markup annotation (rect corners in PDF user space, origin bottom-left).
1183
1274
  * Carries the common metadata plus the type-specific geometry a host editor
@@ -1832,6 +1923,46 @@ declare class GigaPdfDoc {
1832
1923
  * missing certificate.
1833
1924
  */
1834
1925
  signP12(p12: Uint8Array, password: string, opts?: SignP12Options): Uint8Array;
1926
+ /**
1927
+ * Sign with an embedded **RFC 3161 trusted timestamp** (PAdES-B-T). Unlike
1928
+ * {@link sign}/{@link signP12} this is `async`: the timestamp requires a
1929
+ * network round trip to a TSA, so the method runs the engine's two-phase flow
1930
+ * — build the signature → POST the `TimeStampReq` to the TSA → embed the
1931
+ * returned token — with the HTTP in between.
1932
+ *
1933
+ * The signing identity is `opts.p12` (+ `opts.password`) when supplied, else a
1934
+ * generated self-signed digital ID from `opts.random` (+ `notBefore`/
1935
+ * `notAfter`). `opts.tsaUrl` is the TSA endpoint (e.g. FreeTSA
1936
+ * `https://freetsa.org/tsr`); pass `opts.tsaFetch` to customise the request
1937
+ * (auth, proxy, SSRF allow-list). Throws a single generic error on any failure
1938
+ * (bad identity, TSA HTTP error, malformed response, or signature too large).
1939
+ */
1940
+ signTimestamped(opts: SignTsaOptions): Promise<Uint8Array>;
1941
+ /**
1942
+ * Sign with **long-term validation** material embedded (PAdES-B-LT, or B-LTA
1943
+ * with `opts.archiveTimestamp`). Builds a B-T signature ({@link signTimestamped}),
1944
+ * then fetches the certificate chain's revocation material (OCSP responses /
1945
+ * CRLs, by URL **from the certificates** — the engine computes which URLs, the
1946
+ * host fetches) and stores it in a `/DSS`, so the signature validates long after
1947
+ * the certificates expire or are revoked. With `archiveTimestamp` a document
1948
+ * timestamp over the whole file (DSS included) is added for renewable archival.
1949
+ *
1950
+ * `async`, multi-round-trip: one TSA POST for the B-T timestamp, one OCSP/CRL
1951
+ * fetch per chain certificate, and (if `archiveTimestamp`) a second TSA POST.
1952
+ * Unreachable responders are skipped — the DSS is built from whatever resolves;
1953
+ * a self-signed identity (no AIA/CRL-DP) simply yields a `/DSS/Certs`-only store.
1954
+ * Override `tsaFetch`/`revocationFetch`/`crlFetch` to add auth, proxies, or an
1955
+ * SSRF allow-list. Throws on a fatal failure (bad identity, B-T signature, or a
1956
+ * malformed PDF the DSS can't chain to).
1957
+ */
1958
+ signLtv(opts: SignLtvOptions): Promise<Uint8Array>;
1959
+ /** Run `fn` with `pdf` copied into wasm memory, freeing it afterwards. */
1960
+ private withSignedPdf;
1961
+ /** Add the `/DSS` to `signed` as an incremental update (B-LT). */
1962
+ private applyDss;
1963
+ /** Append a document timestamp over the whole `pdf` (B-LTA), TSA round trip
1964
+ * included. */
1965
+ private appendDocumentTimestamp;
1835
1966
  getMetadata(key: string): string;
1836
1967
  /** Set an Info-dictionary entry (e.g. "Title", "Author"). */
1837
1968
  setMetadata(key: string, value: string): boolean;
@@ -1979,4 +2110,4 @@ declare class GigaPdfDoc {
1979
2110
  }): boolean;
1980
2111
  }
1981
2112
 
1982
- export { type AnnotationInfo, type Attachment, type Box, type DecodedImage, type DocumentLanguage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, type GigaBlock, type GigaBlockAddr, type GigaBlockKind, type GigaBorderStyle, type GigaCellValue, type GigaCharStyle, type GigaDocument, type GigaGeneric, type GigaHeading, type GigaImageRef, type GigaInline, type GigaInlineRun, type GigaLinkTarget, type GigaList, type GigaListItem, type GigaListMarker, type GigaMergeRange, type GigaOutlineNode, type GigaPage, type GigaPageGeometry, type GigaParaPatch, type GigaParagraph, type GigaParagraphStyle, type GigaPathSeg, GigaPdfDoc, GigaPdfEngine, type GigaPlaceholder, type GigaPlaceholderRole, type GigaRect, type GigaRotation, type GigaSection, type GigaShape, type GigaSheet, type GigaSheetBlock, type GigaSheetCell, type GigaSheetRow, type GigaSlide, type GigaSlideBlock, type GigaStylePatch, type GigaTable, type GigaTableCell, type GigaTableRow, type GigaTextBox, type HeaderFooterAlign, type HeaderFooterSpec, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type ModelOp, type NamedDest, type OutlineEntry, type PageInfo, type PageMargins, type PathSegment, type PdfPermissions, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };
2113
+ export { type AnnotationInfo, type Attachment, type Box, type DecodedImage, type DocumentLanguage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, type GigaBlock, type GigaBlockAddr, type GigaBlockKind, type GigaBorderStyle, type GigaCellValue, type GigaCharStyle, type GigaDocument, type GigaGeneric, type GigaHeading, type GigaImageRef, type GigaInline, type GigaInlineRun, type GigaLinkTarget, type GigaList, type GigaListItem, type GigaListMarker, type GigaMergeRange, type GigaOutlineNode, type GigaPage, type GigaPageGeometry, type GigaParaPatch, type GigaParagraph, type GigaParagraphStyle, type GigaPathSeg, GigaPdfDoc, GigaPdfEngine, type GigaPlaceholder, type GigaPlaceholderRole, type GigaRect, type GigaRotation, type GigaSection, type GigaShape, type GigaSheet, type GigaSheetBlock, type GigaSheetCell, type GigaSheetRow, type GigaSlide, type GigaSlideBlock, type GigaStylePatch, type GigaTable, type GigaTableCell, type GigaTableRow, type GigaTextBox, type HeaderFooterAlign, type HeaderFooterSpec, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type ModelOp, type NamedDest, type OutlineEntry, type PageInfo, type PageMargins, type PathSegment, type PdfPermissions, type RadioOption, type SearchHit, type SignLtvOptions, type SignP12Options, type SignTsaOptions, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet, defaultCrlGet, defaultOcspPost, defaultTsaPost };
package/dist/index.d.ts CHANGED
@@ -372,6 +372,16 @@ declare class GigaPdfEngine {
372
372
  /** Raise a model to an EPUB e-book (`.epub`). */
373
373
  modelToEpub(model: GigaDocument): Uint8Array;
374
374
  }
375
+ /**
376
+ * Default TSA round trip for {@link GigaPdfDoc.signTimestamped}: POST the
377
+ * RFC 3161 `TimeStampReq` to `url` as `application/timestamp-query` and return
378
+ * the raw `TimeStampResp` (`application/timestamp-reply`) body. Works in both
379
+ * Node and the browser via the global `fetch`.
380
+ *
381
+ * No SSRF allow-listing is performed here — the URL is host-supplied. Consumers
382
+ * that need to restrict it should pass their own `tsaFetch`.
383
+ */
384
+ declare function defaultTsaPost(url: string, req: Uint8Array): Promise<Uint8Array>;
375
385
  /** A Google font the HTML engine needs (resolved from the catalogue). */
376
386
  interface HtmlFontRequest {
377
387
  family: string;
@@ -1178,6 +1188,87 @@ interface SignP12Options {
1178
1188
  /** `/ContactInfo` — how to reach the signer. */
1179
1189
  contactInfo?: string;
1180
1190
  }
1191
+ /**
1192
+ * Options for {@link GigaPdfDoc.signTimestamped} — a PAdES-B-T signature with an
1193
+ * RFC 3161 trusted timestamp embedded in the SignerInfo (`ETSI.CAdES.detached`
1194
+ * subfilter, `signing-certificate-v2` signed attribute, `id-aa-timeStampToken`
1195
+ * unsigned attribute).
1196
+ *
1197
+ * The signing identity is either an imported PKCS#12 (`p12` + `password`) or, if
1198
+ * `p12` is omitted, a freshly generated self-signed digital ID (`random` +
1199
+ * `notBefore`/`notAfter`).
1200
+ */
1201
+ interface SignTsaOptions extends SignP12Options {
1202
+ /** TSA endpoint URL, e.g. `"https://freetsa.org/tsr"`. */
1203
+ tsaUrl: string;
1204
+ /**
1205
+ * Optional override for the TSA round trip — lets the host add auth headers,
1206
+ * proxies, retries, **and apply its own SSRF allow-list** (the engine only
1207
+ * emits the request; the URL is host-supplied). Receives the `TimeStampReq`
1208
+ * DER and the URL, must resolve to the raw `TimeStampResp` bytes. When omitted,
1209
+ * {@link defaultTsaPost} POSTs `application/timestamp-query` via `fetch`.
1210
+ */
1211
+ tsaFetch?: (req: Uint8Array, url: string) => Promise<Uint8Array>;
1212
+ /** PKCS#12 identity bytes. Omit to sign with a generated self-signed ID. */
1213
+ p12?: Uint8Array;
1214
+ /** PKCS#12 passphrase. */
1215
+ password?: string;
1216
+ /** Self-signed path: ≥ 256 bytes from `crypto.getRandomValues`. */
1217
+ random?: Uint8Array;
1218
+ /** Self-signed path: RSA modulus size in bits (default 2048). */
1219
+ keyBits?: number;
1220
+ /** Self-signed path: certificate `notBefore`, UTCTime `YYMMDDHHMMSSZ`. */
1221
+ notBefore?: string;
1222
+ /** Self-signed path: certificate `notAfter`, UTCTime `YYMMDDHHMMSSZ`. */
1223
+ notAfter?: string;
1224
+ /** Optional 8–16 random bytes echoed by the TSA (request/response correlation). */
1225
+ nonce?: Uint8Array;
1226
+ }
1227
+ /**
1228
+ * Options for {@link GigaPdfDoc.signLtv} — a PAdES long-term validation
1229
+ * signature: a B-T signature, then a `/DSS` (Document Security Store) carrying
1230
+ * the certificate chain + OCSP/CRL revocation material the host fetched (B-LT),
1231
+ * optionally finished with a document timestamp over the whole file (B-LTA).
1232
+ *
1233
+ * Extends {@link SignTsaOptions}: the B-T signature is produced exactly as
1234
+ * {@link GigaPdfDoc.signTimestamped}, then the LTV material is added. The OCSP/CRL
1235
+ * URLs come from the **certificates**, so the host fetches them — supply
1236
+ * `revocationFetch`/`crlFetch` to add auth, proxies, or an SSRF allow-list (the
1237
+ * engine only computes which URLs to fetch).
1238
+ */
1239
+ interface SignLtvOptions extends SignTsaOptions {
1240
+ /**
1241
+ * Add a B-LTA **document timestamp** over the whole file (DSS included) after
1242
+ * the DSS. Requires a second TSA round trip. Default `false` (B-LT only).
1243
+ */
1244
+ archiveTimestamp?: boolean;
1245
+ /**
1246
+ * Override the OCSP round trip — receives the DER `OCSPRequest` and the
1247
+ * responder URL, must resolve to the raw `OCSPResponse` bytes. When omitted,
1248
+ * {@link defaultOcspPost} POSTs `application/ocsp-request` via `fetch`. Throw to
1249
+ * skip an unreachable responder (the DSS is built from whatever succeeds).
1250
+ */
1251
+ revocationFetch?: (req: Uint8Array, url: string) => Promise<Uint8Array>;
1252
+ /**
1253
+ * Override the CRL fetch — receives the CRL distribution-point URL, must
1254
+ * resolve to the raw `CertificateList` (CRL) bytes. When omitted,
1255
+ * {@link defaultCrlGet} GETs the URL. Throw to skip an unreachable CRL.
1256
+ */
1257
+ crlFetch?: (url: string) => Promise<Uint8Array>;
1258
+ }
1259
+ /**
1260
+ * Default OCSP round trip for {@link GigaPdfDoc.signLtv}: POST the DER
1261
+ * `OCSPRequest` to `url` as `application/ocsp-request` and return the raw
1262
+ * `OCSPResponse` body. No SSRF allow-listing — the URL comes from the
1263
+ * certificate's AIA extension; pass `revocationFetch` to restrict it.
1264
+ */
1265
+ declare function defaultOcspPost(req: Uint8Array, url: string): Promise<Uint8Array>;
1266
+ /**
1267
+ * Default CRL fetch for {@link GigaPdfDoc.signLtv}: GET the `CertificateList`
1268
+ * (CRL) from `url`. No SSRF allow-listing — the URL comes from the certificate's
1269
+ * CRL-DP extension; pass `crlFetch` to restrict it.
1270
+ */
1271
+ declare function defaultCrlGet(url: string): Promise<Uint8Array>;
1181
1272
  /**
1182
1273
  * A markup annotation (rect corners in PDF user space, origin bottom-left).
1183
1274
  * Carries the common metadata plus the type-specific geometry a host editor
@@ -1832,6 +1923,46 @@ declare class GigaPdfDoc {
1832
1923
  * missing certificate.
1833
1924
  */
1834
1925
  signP12(p12: Uint8Array, password: string, opts?: SignP12Options): Uint8Array;
1926
+ /**
1927
+ * Sign with an embedded **RFC 3161 trusted timestamp** (PAdES-B-T). Unlike
1928
+ * {@link sign}/{@link signP12} this is `async`: the timestamp requires a
1929
+ * network round trip to a TSA, so the method runs the engine's two-phase flow
1930
+ * — build the signature → POST the `TimeStampReq` to the TSA → embed the
1931
+ * returned token — with the HTTP in between.
1932
+ *
1933
+ * The signing identity is `opts.p12` (+ `opts.password`) when supplied, else a
1934
+ * generated self-signed digital ID from `opts.random` (+ `notBefore`/
1935
+ * `notAfter`). `opts.tsaUrl` is the TSA endpoint (e.g. FreeTSA
1936
+ * `https://freetsa.org/tsr`); pass `opts.tsaFetch` to customise the request
1937
+ * (auth, proxy, SSRF allow-list). Throws a single generic error on any failure
1938
+ * (bad identity, TSA HTTP error, malformed response, or signature too large).
1939
+ */
1940
+ signTimestamped(opts: SignTsaOptions): Promise<Uint8Array>;
1941
+ /**
1942
+ * Sign with **long-term validation** material embedded (PAdES-B-LT, or B-LTA
1943
+ * with `opts.archiveTimestamp`). Builds a B-T signature ({@link signTimestamped}),
1944
+ * then fetches the certificate chain's revocation material (OCSP responses /
1945
+ * CRLs, by URL **from the certificates** — the engine computes which URLs, the
1946
+ * host fetches) and stores it in a `/DSS`, so the signature validates long after
1947
+ * the certificates expire or are revoked. With `archiveTimestamp` a document
1948
+ * timestamp over the whole file (DSS included) is added for renewable archival.
1949
+ *
1950
+ * `async`, multi-round-trip: one TSA POST for the B-T timestamp, one OCSP/CRL
1951
+ * fetch per chain certificate, and (if `archiveTimestamp`) a second TSA POST.
1952
+ * Unreachable responders are skipped — the DSS is built from whatever resolves;
1953
+ * a self-signed identity (no AIA/CRL-DP) simply yields a `/DSS/Certs`-only store.
1954
+ * Override `tsaFetch`/`revocationFetch`/`crlFetch` to add auth, proxies, or an
1955
+ * SSRF allow-list. Throws on a fatal failure (bad identity, B-T signature, or a
1956
+ * malformed PDF the DSS can't chain to).
1957
+ */
1958
+ signLtv(opts: SignLtvOptions): Promise<Uint8Array>;
1959
+ /** Run `fn` with `pdf` copied into wasm memory, freeing it afterwards. */
1960
+ private withSignedPdf;
1961
+ /** Add the `/DSS` to `signed` as an incremental update (B-LT). */
1962
+ private applyDss;
1963
+ /** Append a document timestamp over the whole `pdf` (B-LTA), TSA round trip
1964
+ * included. */
1965
+ private appendDocumentTimestamp;
1835
1966
  getMetadata(key: string): string;
1836
1967
  /** Set an Info-dictionary entry (e.g. "Title", "Author"). */
1837
1968
  setMetadata(key: string, value: string): boolean;
@@ -1979,4 +2110,4 @@ declare class GigaPdfDoc {
1979
2110
  }): boolean;
1980
2111
  }
1981
2112
 
1982
- export { type AnnotationInfo, type Attachment, type Box, type DecodedImage, type DocumentLanguage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, type GigaBlock, type GigaBlockAddr, type GigaBlockKind, type GigaBorderStyle, type GigaCellValue, type GigaCharStyle, type GigaDocument, type GigaGeneric, type GigaHeading, type GigaImageRef, type GigaInline, type GigaInlineRun, type GigaLinkTarget, type GigaList, type GigaListItem, type GigaListMarker, type GigaMergeRange, type GigaOutlineNode, type GigaPage, type GigaPageGeometry, type GigaParaPatch, type GigaParagraph, type GigaParagraphStyle, type GigaPathSeg, GigaPdfDoc, GigaPdfEngine, type GigaPlaceholder, type GigaPlaceholderRole, type GigaRect, type GigaRotation, type GigaSection, type GigaShape, type GigaSheet, type GigaSheetBlock, type GigaSheetCell, type GigaSheetRow, type GigaSlide, type GigaSlideBlock, type GigaStylePatch, type GigaTable, type GigaTableCell, type GigaTableRow, type GigaTextBox, type HeaderFooterAlign, type HeaderFooterSpec, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type ModelOp, type NamedDest, type OutlineEntry, type PageInfo, type PageMargins, type PathSegment, type PdfPermissions, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };
2113
+ export { type AnnotationInfo, type Attachment, type Box, type DecodedImage, type DocumentLanguage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, type GigaBlock, type GigaBlockAddr, type GigaBlockKind, type GigaBorderStyle, type GigaCellValue, type GigaCharStyle, type GigaDocument, type GigaGeneric, type GigaHeading, type GigaImageRef, type GigaInline, type GigaInlineRun, type GigaLinkTarget, type GigaList, type GigaListItem, type GigaListMarker, type GigaMergeRange, type GigaOutlineNode, type GigaPage, type GigaPageGeometry, type GigaParaPatch, type GigaParagraph, type GigaParagraphStyle, type GigaPathSeg, GigaPdfDoc, GigaPdfEngine, type GigaPlaceholder, type GigaPlaceholderRole, type GigaRect, type GigaRotation, type GigaSection, type GigaShape, type GigaSheet, type GigaSheetBlock, type GigaSheetCell, type GigaSheetRow, type GigaSlide, type GigaSlideBlock, type GigaStylePatch, type GigaTable, type GigaTableCell, type GigaTableRow, type GigaTextBox, type HeaderFooterAlign, type HeaderFooterSpec, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type ModelOp, type NamedDest, type OutlineEntry, type PageInfo, type PageMargins, type PathSegment, type PdfPermissions, type RadioOption, type SearchHit, type SignLtvOptions, type SignP12Options, type SignTsaOptions, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet, defaultCrlGet, defaultOcspPost, defaultTsaPost };
package/dist/index.js CHANGED
@@ -828,6 +828,18 @@ var GigaPdfEngine = class _GigaPdfEngine {
828
828
  );
829
829
  }
830
830
  };
831
+ async function defaultTsaPost(url, req) {
832
+ const res = await fetch(url, {
833
+ method: "POST",
834
+ headers: { "Content-Type": "application/timestamp-query" },
835
+ body: req,
836
+ redirect: "error"
837
+ });
838
+ if (!res.ok) {
839
+ throw new Error(`TSA HTTP ${res.status}`);
840
+ }
841
+ return new Uint8Array(await res.arrayBuffer());
842
+ }
831
843
  function packHtmlResources(resources) {
832
844
  let size = 4;
833
845
  for (const r of resources) size += 4 + enc.encode(r.url).length + 4 + r.bytes.length;
@@ -874,6 +886,47 @@ function packHtmlFonts(fonts) {
874
886
  }
875
887
  return buf;
876
888
  }
889
+ async function defaultOcspPost(req, url) {
890
+ const res = await fetch(url, {
891
+ method: "POST",
892
+ headers: { "Content-Type": "application/ocsp-request" },
893
+ body: req,
894
+ redirect: "error"
895
+ });
896
+ if (!res.ok) {
897
+ throw new Error(`OCSP HTTP ${res.status}`);
898
+ }
899
+ return new Uint8Array(await res.arrayBuffer());
900
+ }
901
+ async function defaultCrlGet(url) {
902
+ const res = await fetch(url, { method: "GET", redirect: "error" });
903
+ if (!res.ok) {
904
+ throw new Error(`CRL HTTP ${res.status}`);
905
+ }
906
+ return new Uint8Array(await res.arrayBuffer());
907
+ }
908
+ function hexToBytes(hex) {
909
+ const out = new Uint8Array(hex.length >> 1);
910
+ for (let i = 0; i < out.length; i++) {
911
+ out[i] = parseInt(hex.substr(i * 2, 2), 16);
912
+ }
913
+ return out;
914
+ }
915
+ function frameBlobs(blobs) {
916
+ let total = 4;
917
+ for (const b of blobs) total += 4 + b.length;
918
+ const out = new Uint8Array(total);
919
+ const view = new DataView(out.buffer);
920
+ view.setUint32(0, blobs.length, true);
921
+ let pos = 4;
922
+ for (const b of blobs) {
923
+ view.setUint32(pos, b.length, true);
924
+ pos += 4;
925
+ out.set(b, pos);
926
+ pos += b.length;
927
+ }
928
+ return out;
929
+ }
877
930
  var RGB = (rgb) => rgb & 16777215;
878
931
  function styleArgs(s = {}) {
879
932
  const hasBorder = s.border === null ? 0 : 1;
@@ -1723,6 +1776,231 @@ var GigaPdfDoc = class {
1723
1776
  }
1724
1777
  return out;
1725
1778
  }
1779
+ /**
1780
+ * Sign with an embedded **RFC 3161 trusted timestamp** (PAdES-B-T). Unlike
1781
+ * {@link sign}/{@link signP12} this is `async`: the timestamp requires a
1782
+ * network round trip to a TSA, so the method runs the engine's two-phase flow
1783
+ * — build the signature → POST the `TimeStampReq` to the TSA → embed the
1784
+ * returned token — with the HTTP in between.
1785
+ *
1786
+ * The signing identity is `opts.p12` (+ `opts.password`) when supplied, else a
1787
+ * generated self-signed digital ID from `opts.random` (+ `notBefore`/
1788
+ * `notAfter`). `opts.tsaUrl` is the TSA endpoint (e.g. FreeTSA
1789
+ * `https://freetsa.org/tsr`); pass `opts.tsaFetch` to customise the request
1790
+ * (auth, proxy, SSRF allow-list). Throws a single generic error on any failure
1791
+ * (bad identity, TSA HTTP error, malformed response, or signature too large).
1792
+ */
1793
+ async signTimestamped(opts) {
1794
+ const usingP12 = opts.p12 != null && opts.p12.length > 0;
1795
+ if (!usingP12 && (opts.random == null || opts.random.length < 256)) {
1796
+ throw new Error(
1797
+ "signTimestamped: self-signed path needs `random` \u2265 256 bytes (or pass a `p12`)"
1798
+ );
1799
+ }
1800
+ const fields = [
1801
+ opts.name ?? "",
1802
+ opts.reason ?? "",
1803
+ opts.date ?? "",
1804
+ opts.location ?? "",
1805
+ opts.contactInfo ?? "",
1806
+ opts.notBefore ?? "",
1807
+ opts.notAfter ?? ""
1808
+ ].join(" ");
1809
+ const rand = opts.random ?? new Uint8Array(0);
1810
+ const p12 = opts.p12 ?? new Uint8Array(0);
1811
+ const nonce = opts.nonce ?? new Uint8Array(0);
1812
+ const keyBits = opts.keyBits ?? 2048;
1813
+ const rPtr = this.g._toWasm(rand);
1814
+ const p12Ptr = this.g._toWasm(p12);
1815
+ const noncePtr = this.g._toWasm(nonce);
1816
+ let request;
1817
+ try {
1818
+ request = this.g._withStr(
1819
+ opts.password ?? "",
1820
+ (pwP, pwL) => this.g._withStr(
1821
+ fields,
1822
+ (fP, fL) => this.g._buffer(
1823
+ (o) => this.ex().gp_sign_prepare_tsa(
1824
+ this.h,
1825
+ fP,
1826
+ fL,
1827
+ rPtr,
1828
+ rand.length,
1829
+ keyBits,
1830
+ p12Ptr,
1831
+ p12.length,
1832
+ pwP,
1833
+ pwL,
1834
+ noncePtr,
1835
+ nonce.length,
1836
+ o
1837
+ )
1838
+ )
1839
+ )
1840
+ );
1841
+ } finally {
1842
+ this.g._free(rPtr, rand.length);
1843
+ this.g._free(p12Ptr, p12.length);
1844
+ this.g._free(noncePtr, nonce.length);
1845
+ }
1846
+ if (request.length === 0) {
1847
+ throw new Error(
1848
+ "timestamped signing failed: invalid identity or could not build the timestamp request"
1849
+ );
1850
+ }
1851
+ const response = opts.tsaFetch ? await opts.tsaFetch(request, opts.tsaUrl) : await defaultTsaPost(opts.tsaUrl, request);
1852
+ const tokenPtr = this.g._toWasm(response);
1853
+ let out;
1854
+ try {
1855
+ out = this.g._buffer(
1856
+ (o) => this.ex().gp_sign_finish_tsa(this.h, tokenPtr, response.length, o)
1857
+ );
1858
+ } finally {
1859
+ this.g._free(tokenPtr, response.length);
1860
+ }
1861
+ if (out.length === 0) {
1862
+ throw new Error(
1863
+ "timestamped signing failed: TSA response rejected or signature too large for the reserved space"
1864
+ );
1865
+ }
1866
+ return out;
1867
+ }
1868
+ /**
1869
+ * Sign with **long-term validation** material embedded (PAdES-B-LT, or B-LTA
1870
+ * with `opts.archiveTimestamp`). Builds a B-T signature ({@link signTimestamped}),
1871
+ * then fetches the certificate chain's revocation material (OCSP responses /
1872
+ * CRLs, by URL **from the certificates** — the engine computes which URLs, the
1873
+ * host fetches) and stores it in a `/DSS`, so the signature validates long after
1874
+ * the certificates expire or are revoked. With `archiveTimestamp` a document
1875
+ * timestamp over the whole file (DSS included) is added for renewable archival.
1876
+ *
1877
+ * `async`, multi-round-trip: one TSA POST for the B-T timestamp, one OCSP/CRL
1878
+ * fetch per chain certificate, and (if `archiveTimestamp`) a second TSA POST.
1879
+ * Unreachable responders are skipped — the DSS is built from whatever resolves;
1880
+ * a self-signed identity (no AIA/CRL-DP) simply yields a `/DSS/Certs`-only store.
1881
+ * Override `tsaFetch`/`revocationFetch`/`crlFetch` to add auth, proxies, or an
1882
+ * SSRF allow-list. Throws on a fatal failure (bad identity, B-T signature, or a
1883
+ * malformed PDF the DSS can't chain to).
1884
+ */
1885
+ async signLtv(opts) {
1886
+ const signed = await this.signTimestamped(opts);
1887
+ const nonce = opts.nonce ?? new Uint8Array(0);
1888
+ const targetsJson = this.withSignedPdf(signed, (pdfPtr, pdfLen) => {
1889
+ const noncePtr = this.g._toWasm(nonce);
1890
+ try {
1891
+ return dec.decode(
1892
+ this.g._buffer(
1893
+ (o) => this.ex().gp_ltv_targets(pdfPtr, pdfLen, noncePtr, nonce.length, o)
1894
+ )
1895
+ );
1896
+ } finally {
1897
+ this.g._free(noncePtr, nonce.length);
1898
+ }
1899
+ });
1900
+ const targets = targetsJson ? JSON.parse(targetsJson) : [];
1901
+ const certs = [];
1902
+ const ocsps = [];
1903
+ const crls = [];
1904
+ const ocspPost = opts.revocationFetch ?? defaultOcspPost;
1905
+ const crlGet = opts.crlFetch ?? defaultCrlGet;
1906
+ for (const target of targets) {
1907
+ certs.push(hexToBytes(target.certHex));
1908
+ for (const source of target.sources) {
1909
+ try {
1910
+ if (source.kind === "ocsp") {
1911
+ ocsps.push(await ocspPost(hexToBytes(source.requestHex), source.url));
1912
+ } else {
1913
+ crls.push(await crlGet(source.url));
1914
+ }
1915
+ } catch {
1916
+ }
1917
+ }
1918
+ }
1919
+ let lt = this.applyDss(signed, certs, ocsps, crls);
1920
+ if (opts.archiveTimestamp) {
1921
+ lt = await this.appendDocumentTimestamp(lt, opts);
1922
+ }
1923
+ return lt;
1924
+ }
1925
+ /** Run `fn` with `pdf` copied into wasm memory, freeing it afterwards. */
1926
+ withSignedPdf(pdf, fn) {
1927
+ const ptr = this.g._toWasm(pdf);
1928
+ try {
1929
+ return fn(ptr, pdf.length);
1930
+ } finally {
1931
+ this.g._free(ptr, pdf.length);
1932
+ }
1933
+ }
1934
+ /** Add the `/DSS` to `signed` as an incremental update (B-LT). */
1935
+ applyDss(signed, certs, ocsps, crls) {
1936
+ const certsBuf = frameBlobs(certs);
1937
+ const ocspsBuf = frameBlobs(ocsps);
1938
+ const crlsBuf = frameBlobs(crls);
1939
+ const pdfPtr = this.g._toWasm(signed);
1940
+ const cPtr = this.g._toWasm(certsBuf);
1941
+ const oPtr = this.g._toWasm(ocspsBuf);
1942
+ const rPtr = this.g._toWasm(crlsBuf);
1943
+ let out;
1944
+ try {
1945
+ out = this.g._buffer(
1946
+ (res) => this.ex().gp_apply_dss(
1947
+ pdfPtr,
1948
+ signed.length,
1949
+ cPtr,
1950
+ certsBuf.length,
1951
+ oPtr,
1952
+ ocspsBuf.length,
1953
+ rPtr,
1954
+ crlsBuf.length,
1955
+ res
1956
+ )
1957
+ );
1958
+ } finally {
1959
+ this.g._free(pdfPtr, signed.length);
1960
+ this.g._free(cPtr, certsBuf.length);
1961
+ this.g._free(oPtr, ocspsBuf.length);
1962
+ this.g._free(rPtr, crlsBuf.length);
1963
+ }
1964
+ if (out.length === 0) {
1965
+ throw new Error("LTV failed: could not add the /DSS to the signed PDF");
1966
+ }
1967
+ return out;
1968
+ }
1969
+ /** Append a document timestamp over the whole `pdf` (B-LTA), TSA round trip
1970
+ * included. */
1971
+ async appendDocumentTimestamp(pdf, opts) {
1972
+ const nonce = opts.nonce ?? new Uint8Array(0);
1973
+ const pdfPtr = this.g._toWasm(pdf);
1974
+ const noncePtr = this.g._toWasm(nonce);
1975
+ let request;
1976
+ try {
1977
+ request = this.g._buffer(
1978
+ (o) => this.ex().gp_doc_timestamp_prepare(this.h, pdfPtr, pdf.length, noncePtr, nonce.length, o)
1979
+ );
1980
+ } finally {
1981
+ this.g._free(pdfPtr, pdf.length);
1982
+ this.g._free(noncePtr, nonce.length);
1983
+ }
1984
+ if (request.length === 0) {
1985
+ throw new Error("LTV archive timestamp failed: could not build the timestamp request");
1986
+ }
1987
+ const response = opts.tsaFetch ? await opts.tsaFetch(request, opts.tsaUrl) : await defaultTsaPost(opts.tsaUrl, request);
1988
+ const tokenPtr = this.g._toWasm(response);
1989
+ let out;
1990
+ try {
1991
+ out = this.g._buffer(
1992
+ (o) => this.ex().gp_doc_timestamp_finish(this.h, tokenPtr, response.length, o)
1993
+ );
1994
+ } finally {
1995
+ this.g._free(tokenPtr, response.length);
1996
+ }
1997
+ if (out.length === 0) {
1998
+ throw new Error(
1999
+ "LTV archive timestamp failed: TSA response rejected or token too large for the reserved space"
2000
+ );
2001
+ }
2002
+ return out;
2003
+ }
1726
2004
  // metadata
1727
2005
  getMetadata(key) {
1728
2006
  return this.g._withStr(
@@ -2121,6 +2399,9 @@ var GigaPdfDoc = class {
2121
2399
  };
2122
2400
  export {
2123
2401
  GigaPdfDoc,
2124
- GigaPdfEngine
2402
+ GigaPdfEngine,
2403
+ defaultCrlGet,
2404
+ defaultOcspPost,
2405
+ defaultTsaPost
2125
2406
  };
2126
2407
  //# sourceMappingURL=index.js.map