@ac-kit/format-tsp 0.0.0-stage → 0.1.1

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/LICENSE +28 -0
  3. package/README.md +202 -2
  4. package/dist/_decode.d.ts +17 -0
  5. package/dist/_decode.d.ts.map +1 -0
  6. package/dist/_decode.js +41 -0
  7. package/dist/_decode.js.map +1 -0
  8. package/dist/_generalized-time.d.ts +12 -0
  9. package/dist/_generalized-time.d.ts.map +1 -0
  10. package/dist/_generalized-time.js +40 -0
  11. package/dist/_generalized-time.js.map +1 -0
  12. package/dist/_hash-algorithms.d.ts +41 -0
  13. package/dist/_hash-algorithms.d.ts.map +1 -0
  14. package/dist/_hash-algorithms.js +66 -0
  15. package/dist/_hash-algorithms.js.map +1 -0
  16. package/dist/_pkix.d.ts +37 -0
  17. package/dist/_pkix.d.ts.map +1 -0
  18. package/dist/_pkix.js +48 -0
  19. package/dist/_pkix.js.map +1 -0
  20. package/dist/consistency.d.ts +101 -0
  21. package/dist/consistency.d.ts.map +1 -0
  22. package/dist/consistency.js +61 -0
  23. package/dist/consistency.js.map +1 -0
  24. package/dist/constants.d.ts +32 -0
  25. package/dist/constants.d.ts.map +1 -0
  26. package/dist/constants.js +39 -0
  27. package/dist/constants.js.map +1 -0
  28. package/dist/errors.d.ts +17 -0
  29. package/dist/errors.d.ts.map +1 -0
  30. package/dist/errors.js +21 -0
  31. package/dist/errors.js.map +1 -0
  32. package/dist/index.d.ts +10 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +10 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/message-imprint.d.ts +72 -0
  37. package/dist/message-imprint.d.ts.map +1 -0
  38. package/dist/message-imprint.js +97 -0
  39. package/dist/message-imprint.js.map +1 -0
  40. package/dist/request.d.ts +94 -0
  41. package/dist/request.d.ts.map +1 -0
  42. package/dist/request.js +123 -0
  43. package/dist/request.js.map +1 -0
  44. package/dist/response.d.ts +56 -0
  45. package/dist/response.d.ts.map +1 -0
  46. package/dist/response.js +92 -0
  47. package/dist/response.js.map +1 -0
  48. package/dist/schema.d.ts +39 -0
  49. package/dist/schema.d.ts.map +1 -0
  50. package/dist/schema.js +107 -0
  51. package/dist/schema.js.map +1 -0
  52. package/dist/token.d.ts +51 -0
  53. package/dist/token.d.ts.map +1 -0
  54. package/dist/token.js +137 -0
  55. package/dist/token.js.map +1 -0
  56. package/dist/tst-info.d.ts +94 -0
  57. package/dist/tst-info.d.ts.map +1 -0
  58. package/dist/tst-info.js +140 -0
  59. package/dist/tst-info.js.map +1 -0
  60. package/dist/verify.d.ts +110 -0
  61. package/dist/verify.d.ts.map +1 -0
  62. package/dist/verify.js +160 -0
  63. package/dist/verify.js.map +1 -0
  64. package/package.json +78 -3
@@ -0,0 +1,123 @@
1
+ import { bigIntFromBytesBe, describeValue } from "@ac-kit/core";
2
+ import { getRandomBytes } from "@ac-kit/crypto-random";
3
+ import { derEncode } from "@ac-kit/format-asn1/der";
4
+ import { oidEqual } from "@ac-kit/format-oid";
5
+ import { decodeDer_ } from "./_decode.js";
6
+ import { classifyImprint_, hashAlgorithms_, md5Oid_, } from "./_hash-algorithms.js";
7
+ import { algorithmIdentifierFromDer_, algorithmIdentifierToDer_, extensionsFromDer_, } from "./_pkix.js";
8
+ import { TspFormatError } from "./errors.js";
9
+ import { timeStampReqSchema } from "./schema.js";
10
+ /** Fewest random nonce bytes: 64 bits, as RFC 3161 §2.4.1 suggests. */
11
+ const MIN_NONCE_BYTE_LENGTH = 8;
12
+ /** Most random nonce bytes: 256 bits is past any collision concern. */
13
+ const MAX_NONCE_BYTE_LENGTH = 32;
14
+ /**
15
+ * A v1 request for a token over `messageImprint`. Synchronous: the imprint is
16
+ * already hashed, and the nonce comes from `crypto.getRandomValues` through
17
+ * `@ac-kit/crypto-random`, read as an unsigned integer.
18
+ *
19
+ * An imprint whose algorithm this package does not know is let through, so a
20
+ * caller may request a hash it hashed itself.
21
+ *
22
+ * Complexity: O(digest + extensions).
23
+ *
24
+ * @throws {RangeError} When `nonceByteLength` is not an integer from 8 to 32;
25
+ * the imprint is SHA-1 or MD5 (too weak to request); a known hash has the
26
+ * wrong digest length or parameters other than absent or `NULL`; or
27
+ * `extensions` is empty.
28
+ */
29
+ export function createTimeStampRequest(messageImprint, options = {}) {
30
+ const { reqPolicy, nonce, nonceByteLength = MIN_NONCE_BYTE_LENGTH, certReq = false, extensions, } = options;
31
+ if (!Number.isInteger(nonceByteLength) ||
32
+ nonceByteLength < MIN_NONCE_BYTE_LENGTH ||
33
+ nonceByteLength > MAX_NONCE_BYTE_LENGTH) {
34
+ throw new RangeError(`createTimeStampRequest: nonceByteLength must be an integer from ${MIN_NONCE_BYTE_LENGTH} to ${MAX_NONCE_BYTE_LENGTH}, got ${describeValue(nonceByteLength)}`);
35
+ }
36
+ checkRequestImprint_(messageImprint);
37
+ checkExtensionsNotEmpty_(extensions, "createTimeStampRequest");
38
+ const requestNonce = nonce === undefined ? randomNonce_(nonceByteLength) : nonce;
39
+ return {
40
+ messageImprint,
41
+ ...(reqPolicy === undefined ? {} : { reqPolicy }),
42
+ ...(requestNonce === null ? {} : { nonce: requestNonce }),
43
+ certReq,
44
+ ...(extensions === undefined ? {} : { extensions }),
45
+ };
46
+ }
47
+ /**
48
+ * The DER `TimeStampReq`, the body of an HTTP request of media type
49
+ * `application/timestamp-query`. `certReq` FALSE is left out, as DER requires
50
+ * of a DEFAULT value.
51
+ *
52
+ * Complexity: O(encoded size).
53
+ *
54
+ * @throws {RangeError} When `extensions` is empty.
55
+ */
56
+ export function encodeTimeStampRequest(request) {
57
+ checkExtensionsNotEmpty_(request.extensions, "encodeTimeStampRequest");
58
+ return derEncode(timeStampReqSchema, {
59
+ version: 1n,
60
+ messageImprint: {
61
+ hashAlgorithm: algorithmIdentifierToDer_(request.messageImprint.hashAlgorithm),
62
+ hashedMessage: request.messageImprint.hashedMessage,
63
+ },
64
+ ...(request.reqPolicy === undefined
65
+ ? {}
66
+ : { reqPolicy: request.reqPolicy }),
67
+ ...(request.nonce === undefined ? {} : { nonce: request.nonce }),
68
+ certReq: request.certReq,
69
+ ...(request.extensions === undefined
70
+ ? {}
71
+ : { extensions: request.extensions }),
72
+ });
73
+ }
74
+ /**
75
+ * Decodes a DER `TimeStampReq`. Hash parameters and extension values are kept
76
+ * byte for byte, so re-encoding a request from another encoder gives the same
77
+ * bytes when it was DER.
78
+ *
79
+ * Complexity: O(bytes).
80
+ *
81
+ * @throws {TspFormatError} For malformed DER (canonical-form violations too,
82
+ * unless `strict` is `false`), a version other than 1, or empty
83
+ * `extensions`.
84
+ */
85
+ export function decodeTimeStampRequest(bytes, options = {}) {
86
+ const part = "TimeStampReq";
87
+ const value = decodeDer_(timeStampReqSchema, bytes, options.strict ?? true, part);
88
+ if (value.version !== 1n) {
89
+ throw new TspFormatError(`${part}: version ${value.version}, expected 1`, undefined);
90
+ }
91
+ return {
92
+ messageImprint: {
93
+ hashAlgorithm: algorithmIdentifierFromDer_(value.messageImprint.hashAlgorithm),
94
+ hashedMessage: new Uint8Array(value.messageImprint.hashedMessage),
95
+ },
96
+ ...(value.reqPolicy === undefined ? {} : { reqPolicy: value.reqPolicy }),
97
+ ...(value.nonce === undefined ? {} : { nonce: value.nonce }),
98
+ certReq: value.certReq ?? false,
99
+ ...(value.extensions === undefined
100
+ ? {}
101
+ : { extensions: extensionsFromDer_(value.extensions, part) }),
102
+ };
103
+ }
104
+ function checkRequestImprint_(messageImprint) {
105
+ const { algorithm } = messageImprint.hashAlgorithm;
106
+ if (oidEqual(algorithm, hashAlgorithms_.sha1.oid) ||
107
+ oidEqual(algorithm, md5Oid_)) {
108
+ throw new RangeError("createTimeStampRequest: SHA-1 and MD5 are too weak to request a token over");
109
+ }
110
+ const classified = classifyImprint_(messageImprint);
111
+ if (classified.kind === "malformed") {
112
+ throw new RangeError(`createTimeStampRequest: ${classified.reason}`);
113
+ }
114
+ }
115
+ function checkExtensionsNotEmpty_(extensions, caller) {
116
+ if (extensions?.length === 0) {
117
+ throw new RangeError(`${caller}: extensions must be absent or hold at least one extension`);
118
+ }
119
+ }
120
+ function randomNonce_(byteLength) {
121
+ return BigInt.asUintN(byteLength * 8, bigIntFromBytesBe(getRandomBytes(byteLength)));
122
+ }
123
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.js","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAyB,MAAM,oBAAoB,CAAC;AAErE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACN,gBAAgB,EAChB,eAAe,EACf,OAAO,GACP,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACN,2BAA2B,EAC3B,yBAAyB,EACzB,kBAAkB,GAClB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AA2DjD,uEAAuE;AACvE,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAChC,uEAAuE;AACvE,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAEjC;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,sBAAsB,CACrC,cAA8B,EAC9B,OAAO,GAAkC,EAAE;IAE3C,MAAM,EACL,SAAS,EACT,KAAK,EACL,eAAe,GAAG,qBAAqB,EACvC,OAAO,GAAG,KAAK,EACf,UAAU,GACV,GAAG,OAAO,CAAC;IACZ,IACC,CAAC,MAAM,CAAC,SAAS,CAAC,eAAe,CAAC;QAClC,eAAe,GAAG,qBAAqB;QACvC,eAA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AAU,KAAK,SAAS;YACjC,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,EAAE,UAAU,EAAE,kBAAkB,CAAC,KAAK,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE,CAAC;KAC9D,CAAC;AACH,CAAC;AAED,SAAS,oBAAoB,CAAC,cAA8B;IAC3D,MAAM,EAAE,SAAS,EAAE,GAAG,cAAc,CAAC,aAAa,CAAC;IACnD,IACC,QAAQ,CAAC,SAAS,EAAE,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC;QAC7C,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC,EAC3B,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,4EAA4E,CAC5E,CAAC;IACH,CAAC;IACD,MAAM,UAAU,GAAG,gBAAgB,CAAC,cAAc,CAAC,CAAC;IACpD,IAAI,UAAU,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QACrC,MAAM,IAAI,UAAU,CAAC,2BAA2B,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;IACtE,CAAC;AACF,CAAC;AAED,SAAS,wBAAwB,CAChC,UAA+C,EAC/C,MAAc;IAEd,IAAI,UAAU,EAAE,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,UAAU,CACnB,GAAG,MAAM,4DAA4D,CACrE,CAAC;IACH,CAAC;AACF,CAAC;AAED,SAAS,YAAY,CAAC,UAAkB;IACvC,OAAO,MAAM,CAAC,OAAO,CACpB,UAAU,GAAG,CAAC,EACd,iBAAiB,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,CAC7C,CAAC;AACH,CAAC"}
@@ -0,0 +1,56 @@
1
+ import { type TimeStampToken } from "./token.js";
2
+ /** The `PKIStatus` values RFC 3161 §2.4.2 names, codes 0 to 5. */
3
+ export type PkiStatus = "granted" | "grantedWithMods" | "rejection" | "waiting" | "revocationWarning" | "revocationNotification";
4
+ /** The `PKIFailureInfo` bits RFC 3161 §2.4.2 names. */
5
+ export type PkiFailureInfoName = "badAlg" | "badRequest" | "badDataFormat" | "timeNotAvailable" | "unacceptedPolicy" | "unacceptedExtension" | "addInfoNotAvailable" | "systemFailure";
6
+ /** The set bits of a `PKIFailureInfo`. */
7
+ export interface PkiFailureInfo {
8
+ /** The named bits that are set, in bit order. */
9
+ readonly names: readonly PkiFailureInfoName[];
10
+ /**
11
+ * Set bits RFC 3161 does not name (CMP, RFC 4210, shares the type),
12
+ * ascending.
13
+ */
14
+ readonly otherBits: readonly number[];
15
+ }
16
+ /** A `PKIStatusInfo`. */
17
+ export interface PkiStatusInfo {
18
+ /** The status name, or `"unknown"` for a code RFC 3161 does not name. */
19
+ readonly status: PkiStatus | "unknown";
20
+ /** The status code exactly as received. */
21
+ readonly statusCode: bigint;
22
+ /** The TSA's free text, as received (possibly empty). */
23
+ readonly statusString?: readonly string[];
24
+ readonly failInfo?: PkiFailureInfo;
25
+ }
26
+ /** A response carrying a token: status `granted` or `grantedWithMods`. */
27
+ export interface GrantedTimeStampResponse extends PkiStatusInfo {
28
+ readonly status: "granted" | "grantedWithMods";
29
+ readonly timeStampToken: TimeStampToken;
30
+ }
31
+ /** A response without a token: any other status, unknown codes included. */
32
+ export interface NotGrantedTimeStampResponse extends PkiStatusInfo {
33
+ readonly status: Exclude<PkiStatus, "granted" | "grantedWithMods"> | "unknown";
34
+ readonly timeStampToken?: undefined;
35
+ }
36
+ /** A decoded `TimeStampResp`; narrow on `status` to reach the token. */
37
+ export type TimeStampResponse = GrantedTimeStampResponse | NotGrantedTimeStampResponse;
38
+ /**
39
+ * Decodes a `TimeStampResp`, the body of an `application/timestamp-reply`.
40
+ *
41
+ * The response wrapper is unsigned, so it is read leniently: BER canonical-form
42
+ * violations are accepted, a status code RFC 3161 does not name becomes
43
+ * `"unknown"` with its `statusCode` kept, `failInfo` may carry trailing zero
44
+ * bits or bits only CMP names, and an empty `statusString` is kept. Malformed
45
+ * framing and trailing bytes are still refused, and so is a contradiction a
46
+ * caller could act on wrongly: status 0 or 1 without a token, or a token with
47
+ * any other status (RFC 3161 §2.4.2). The token itself is only framed and
48
+ * type-checked (see {@link decodeTimeStampToken}); nothing in it is verified.
49
+ *
50
+ * Complexity: O(bytes).
51
+ *
52
+ * @throws {TspFormatError} For malformed framing, trailing bytes, a missing or
53
+ * unexpected token, or a token that is not a `SignedData` `ContentInfo`.
54
+ */
55
+ export declare function decodeTimeStampResponse(bytes: Uint8Array): TimeStampResponse;
56
+ //# sourceMappingURL=response.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAKA,OAAO,EAAuB,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAEtE,kEAAkE;AAClE,MAAM,MAAM,SAAS,GAClB,SAAS,GACT,iBAAiB,GACjB,WAAW,GACX,SAAS,GACT,mBAAmB,GACnB,wBAAwB,CAAC;AAE5B,uDAAuD;AACvD,MAAM,MAAM,kBAAkB,GAC3B,QAAQ,GACR,YAAY,GACZ,eAAe,GACf,kBAAkB,GAClB,kBAAkB,GAClB,qBAAqB,GACrB,qBAAqB,GACrB,eAAe,CAAC;AAEnB,0CAA0C;AAC1C,MAAM,WAAW,cAAc;IAC9B,iDAAiD;IACjD,QAAQ,CAAC,KAAK,EAAE,SAAS,kBAAkB,EAAE,CAAC;IAC9C;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC;AAED,yBAAyB;AACzB,MAAM,WAAW,aAAa;IAC7B,yEAAyE;IACzE,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,CAAC;IACvC,2CAA2C;IAC3C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,yDAAyD;IACzD,QAAQ,CAAC,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1C,QAAQ,CAAC,QAAQ,CAAC,EAAE,cAAc,CAAC;CACnC;AAED,0EAA0E;AAC1E,MAAM,WAAW,wBAAyB,SAAQ,aAAa;IAC9D,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,iBAAiB,CAAC;IAC/C,QAAQ,CAAC,cAAc,EAAE,cAAc,CAAC;CACxC;AAED,4EAA4E;AAC5E,MAAM,WAAW,2BAA4B,SAAQ,aAAa;IACjE,QAAQ,CAAC,MAAM,EACZ,OAAO,CAAC,SAAS,EAAE,SAAS,GAAG,iBAAiB,CAAC,GACjD,SAAS,CAAC;IACb,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,CAAC;CACpC;AAED,wEAAwE;AACxE,MAAM,MAAM,iBAAiB,GAC1B,wBAAwB,GACxB,2BAA2B,CAAC;AAsB/B;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,UAAU,GAAG,iBAAiB,CAuC5E"}
@@ -0,0 +1,92 @@
1
+ import { decodeDer_ } from "./_decode.js";
2
+ import { TspFormatError } from "./errors.js";
3
+ import { timeStampRespSchema } from "./schema.js";
4
+ import { checkTokenEnvelope_ } from "./token.js";
5
+ const STATUS_NAMES = [
6
+ "granted",
7
+ "grantedWithMods",
8
+ "rejection",
9
+ "waiting",
10
+ "revocationWarning",
11
+ "revocationNotification",
12
+ ];
13
+ const FAILURE_NAMES = new Map([
14
+ [0, "badAlg"],
15
+ [2, "badRequest"],
16
+ [5, "badDataFormat"],
17
+ [14, "timeNotAvailable"],
18
+ [15, "unacceptedPolicy"],
19
+ [16, "unacceptedExtension"],
20
+ [17, "addInfoNotAvailable"],
21
+ [25, "systemFailure"],
22
+ ]);
23
+ /**
24
+ * Decodes a `TimeStampResp`, the body of an `application/timestamp-reply`.
25
+ *
26
+ * The response wrapper is unsigned, so it is read leniently: BER canonical-form
27
+ * violations are accepted, a status code RFC 3161 does not name becomes
28
+ * `"unknown"` with its `statusCode` kept, `failInfo` may carry trailing zero
29
+ * bits or bits only CMP names, and an empty `statusString` is kept. Malformed
30
+ * framing and trailing bytes are still refused, and so is a contradiction a
31
+ * caller could act on wrongly: status 0 or 1 without a token, or a token with
32
+ * any other status (RFC 3161 §2.4.2). The token itself is only framed and
33
+ * type-checked (see {@link decodeTimeStampToken}); nothing in it is verified.
34
+ *
35
+ * Complexity: O(bytes).
36
+ *
37
+ * @throws {TspFormatError} For malformed framing, trailing bytes, a missing or
38
+ * unexpected token, or a token that is not a `SignedData` `ContentInfo`.
39
+ */
40
+ export function decodeTimeStampResponse(bytes) {
41
+ const part = "TimeStampResp";
42
+ const value = decodeDer_(timeStampRespSchema, bytes, false, part);
43
+ const code = value.status.status;
44
+ const name = code >= 0n && code < BigInt(STATUS_NAMES.length)
45
+ ? STATUS_NAMES[Number(code)]
46
+ : "unknown";
47
+ const statusInfo = {
48
+ statusCode: code,
49
+ ...(value.status.statusString === undefined
50
+ ? {}
51
+ : { statusString: value.status.statusString }),
52
+ ...(value.status.failInfo === undefined
53
+ ? {}
54
+ : { failInfo: failureInfoFromBits_(value.status.failInfo) }),
55
+ };
56
+ const encodedToken = value.timeStampToken?.encoded;
57
+ if (name === "granted" || name === "grantedWithMods") {
58
+ if (encodedToken === undefined) {
59
+ throw new TspFormatError(`${part}: status ${name} without a timeStampToken`, undefined);
60
+ }
61
+ checkTokenEnvelope_(encodedToken, bytes.length - encodedToken.length);
62
+ return {
63
+ status: name,
64
+ ...statusInfo,
65
+ timeStampToken: { encoded: new Uint8Array(encodedToken) },
66
+ };
67
+ }
68
+ if (encodedToken !== undefined) {
69
+ throw new TspFormatError(`${part}: status ${name} (${code}) with a timeStampToken`, bytes.length - encodedToken.length);
70
+ }
71
+ return { status: name, ...statusInfo };
72
+ }
73
+ /** Complexity: O(bits). */
74
+ function failureInfoFromBits_(value) {
75
+ const names = [];
76
+ const otherBits = [];
77
+ const bitCount = value.bytes.length * 8 - value.unusedBits;
78
+ for (let index = 0; index < bitCount; index++) {
79
+ if ((value.bytes[index >> 3] & (0x80 >> (index & 7))) === 0) {
80
+ continue;
81
+ }
82
+ const name = FAILURE_NAMES.get(index);
83
+ if (name === undefined) {
84
+ otherBits.push(index);
85
+ }
86
+ else {
87
+ names.push(name);
88
+ }
89
+ }
90
+ return { names, otherBits };
91
+ }
92
+ //# sourceMappingURL=response.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAuB,MAAM,YAAY,CAAC;AA+DtE,MAAM,YAAY,GAAyB;IAC1C,SAAS;IACT,iBAAiB;IACjB,WAAW;IACX,SAAS;IACT,mBAAmB;IACnB,wBAAwB;CACxB,CAAC;AAEF,MAAM,aAAa,GAA4C,IAAI,GAAG,CAAC;IACtE,CAAC,CAAC,EAAE,QAAQ,CAAC;IACb,CAAC,CAAC,EAAE,YAAY,CAAC;IACjB,CAAC,CAAC,EAAE,eAAe,CAAC;IACpB,CAAC,EAAE,EAAE,kBAAkB,CAAC;IACxB,CAAC,EAAE,EAAE,kBAAkB,CAAC;IACxB,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAC3B,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAC3B,CAAC,EAAE,EAAE,eAAe,CAAC;CACrB,CAAC,CAAC;AAEH;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAAiB;IACxD,MAAM,IAAI,GAAG,eAAe,CAAC;IAC7B,MAAM,KAAK,GAAG,UAAU,CAAC,mBAAmB,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IAClE,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;IACjC,MAAM,IAAI,GACT,IAAI,IAAI,EAAE,IAAI,IAAI,GAAG,MAAM,CAAC,YAAY,CAAC,MAAM,CA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,GAAG,UAAU,EAAE,CAAC;AACxC,CAAC;AAED,2BAA2B;AAC3B,SAAS,oBAAoB,CAAC,KAAqB;IAClD,MAAM,KAAK,GAAyB,EAAE,CAAC;IACvC,MAAM,SAAS,GAAa,EAAE,CAAC;IAC/B,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,GAAG,KAAK,CAAC,UAAU,CAAC;IAC3D,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC;QAC/C,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,CAAE,GAAG,CAAC,IAAI,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;YAC9D,SAAS;QACV,CAAC;QACD,MAAM,IAAI,GAAG,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACxB,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC;aAAM,CAAC;YACP,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAC7B,CAAC"}
@@ -0,0 +1,39 @@
1
+ /** `MessageImprint ::= SEQUENCE { hashAlgorithm, hashedMessage }`. */
2
+ export declare const messageImprintSchema: import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"hashAlgorithm", import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"algorithm", import("@ac-kit/format-asn1").Asn1ObjectIdentifierTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"parameters", import("@ac-kit/format-asn1").Asn1AnyTypeDef, true>]>, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"hashedMessage", import("@ac-kit/format-asn1").Asn1OctetStringTypeDef, false>]>;
3
+ /**
4
+ * `TimeStampReq`: `version INTEGER { v1(1) }, messageImprint, reqPolicy
5
+ * TSAPolicyId OPTIONAL, nonce INTEGER OPTIONAL, certReq BOOLEAN DEFAULT FALSE,
6
+ * extensions [0] IMPLICIT Extensions OPTIONAL`.
7
+ */
8
+ export declare const timeStampReqSchema: import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"version", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"messageImprint", import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"hashAlgorithm", import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"algorithm", import("@ac-kit/format-asn1").Asn1ObjectIdentifierTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"parameters", import("@ac-kit/format-asn1").Asn1AnyTypeDef, true>]>, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"hashedMessage", import("@ac-kit/format-asn1").Asn1OctetStringTypeDef, false>]>, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"reqPolicy", import("@ac-kit/format-asn1").Asn1ObjectIdentifierTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"nonce", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"certReq", import("@ac-kit/format-asn1").Asn1BooleanTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"extensions", import("@ac-kit/format-asn1").Asn1TaggedTypeDef<import("@ac-kit/format-asn1").Asn1SequenceOfTypeDef<import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"extnID", import("@ac-kit/format-asn1").Asn1ObjectIdentifierTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"critical", import("@ac-kit/format-asn1").Asn1BooleanTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"extnValue", import("@ac-kit/format-asn1").Asn1OctetStringTypeDef, false>]>>>, true>]>;
9
+ /** `PKIStatus ::= INTEGER`, named values 0 (`granted`) to 5. */
10
+ export declare const pkiStatusSchema: import("@ac-kit/format-asn1").Asn1IntegerTypeDef;
11
+ /** `PKIFailureInfo ::= BIT STRING`, with the eight bits RFC 3161 names. */
12
+ export declare const pkiFailureInfoSchema: import("@ac-kit/format-asn1").Asn1BitStringTypeDef;
13
+ /** `PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String`. */
14
+ export declare const pkiFreeTextSchema: import("@ac-kit/format-asn1").Asn1SequenceOfTypeDef<import("@ac-kit/format-asn1").Asn1Utf8StringTypeDef>;
15
+ /**
16
+ * `PKIStatusInfo ::= SEQUENCE { status, statusString OPTIONAL, failInfo
17
+ * OPTIONAL }`.
18
+ */
19
+ export declare const pkiStatusInfoSchema: import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"status", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"statusString", import("@ac-kit/format-asn1").Asn1SequenceOfTypeDef<import("@ac-kit/format-asn1").Asn1Utf8StringTypeDef>, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"failInfo", import("@ac-kit/format-asn1").Asn1BitStringTypeDef, true>]>;
20
+ /**
21
+ * `TimeStampResp ::= SEQUENCE { status PKIStatusInfo, timeStampToken
22
+ * TimeStampToken OPTIONAL }`, the token as `ANY` (the last component, so an
23
+ * untagged `ANY` is unambiguous).
24
+ */
25
+ export declare const timeStampRespSchema: import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"status", import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"status", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"statusString", import("@ac-kit/format-asn1").Asn1SequenceOfTypeDef<import("@ac-kit/format-asn1").Asn1Utf8StringTypeDef>, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"failInfo", import("@ac-kit/format-asn1").Asn1BitStringTypeDef, true>]>, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"timeStampToken", import("@ac-kit/format-asn1").Asn1AnyTypeDef, true>]>;
26
+ /**
27
+ * `Accuracy ::= SEQUENCE { seconds INTEGER OPTIONAL, millis [0] INTEGER
28
+ * (1..999) OPTIONAL, micros [1] INTEGER (1..999) OPTIONAL }`.
29
+ */
30
+ export declare const accuracySchema: import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"seconds", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"millis", import("@ac-kit/format-asn1").Asn1TaggedTypeDef<import("@ac-kit/format-asn1").Asn1IntegerTypeDef>, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"micros", import("@ac-kit/format-asn1").Asn1TaggedTypeDef<import("@ac-kit/format-asn1").Asn1IntegerTypeDef>, true>]>;
31
+ /**
32
+ * `TSTInfo`: `version, policy, messageImprint, serialNumber, genTime
33
+ * GeneralizedTime, accuracy OPTIONAL, ordering BOOLEAN DEFAULT FALSE, nonce
34
+ * OPTIONAL, tsa [0] GeneralName OPTIONAL, extensions [1] IMPLICIT Extensions
35
+ * OPTIONAL`. `GeneralName` is a CHOICE, so its `[0]` is explicit and the name
36
+ * itself is kept as `ANY`.
37
+ */
38
+ export declare const tstInfoSchema: import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"version", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"policy", import("@ac-kit/format-asn1").Asn1ObjectIdentifierTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"messageImprint", import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"hashAlgorithm", import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"algorithm", import("@ac-kit/format-asn1").Asn1ObjectIdentifierTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"parameters", import("@ac-kit/format-asn1").Asn1AnyTypeDef, true>]>, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"hashedMessage", import("@ac-kit/format-asn1").Asn1OctetStringTypeDef, false>]>, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"serialNumber", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"genTime", import("@ac-kit/format-asn1").Asn1GeneralizedTimeTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"accuracy", import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"seconds", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"millis", import("@ac-kit/format-asn1").Asn1TaggedTypeDef<import("@ac-kit/format-asn1").Asn1IntegerTypeDef>, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"micros", import("@ac-kit/format-asn1").Asn1TaggedTypeDef<import("@ac-kit/format-asn1").Asn1IntegerTypeDef>, true>]>, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"ordering", import("@ac-kit/format-asn1").Asn1BooleanTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"nonce", import("@ac-kit/format-asn1").Asn1IntegerTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"tsa", import("@ac-kit/format-asn1").Asn1TaggedTypeDef<import("@ac-kit/format-asn1").Asn1AnyTypeDef>, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"extensions", import("@ac-kit/format-asn1").Asn1TaggedTypeDef<import("@ac-kit/format-asn1").Asn1SequenceOfTypeDef<import("@ac-kit/format-asn1").Asn1SequenceTypeDef<readonly [import("@ac-kit/format-asn1").Asn1ComponentDef<"extnID", import("@ac-kit/format-asn1").Asn1ObjectIdentifierTypeDef, false>, import("@ac-kit/format-asn1").Asn1ComponentDef<"critical", import("@ac-kit/format-asn1").Asn1BooleanTypeDef, true>, import("@ac-kit/format-asn1").Asn1ComponentDef<"extnValue", import("@ac-kit/format-asn1").Asn1OctetStringTypeDef, false>]>>>, true>]>;
39
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AA4EA,sEAAsE;AACtE,eAAO,MAAM,oBAAoB,mjBAAuB,CAAC;AAEzD;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,kuDAS9B,CAAC;AAEF,gEAAgE;AAChE,eAAO,MAAM,eAAe,kDAAkB,CAAC;AAE/C,2EAA2E;AAC3E,eAAO,MAAM,oBAAoB,oDAAuB,CAAC;AAEzD,8DAA8D;AAC9D,eAAO,MAAM,iBAAiB,0GAAoB,CAAC;AAEnD;;;GAGG;AACH,eAAO,MAAM,mBAAmB,udAAsB,CAAC;AAEvD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,4sBAK/B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,ufAAiB,CAAC;AAE7C;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,2qFAazB,CAAC"}
package/dist/schema.js ADDED
@@ -0,0 +1,107 @@
1
+ /**
2
+ * The RFC 3161 Appendix C ASN.1 types (module `PKIXTSP`, `IMPLICIT TAGS`) as
3
+ * `@ac-kit/format-asn1` definitions, for `derDecode`/`derEncode` or for
4
+ * embedding in a larger schema. The root entry point is the usual way in; these
5
+ * are for callers who compose the time-stamp types into their own.
6
+ *
7
+ * `AlgorithmIdentifier` parameters, the token, and the `tsa` GeneralName are
8
+ * `ANY` (their bytes kept as received); `Extensions` has `SIZE (1..MAX)`, which
9
+ * the DER decoder does not enforce.
10
+ *
11
+ * @module
12
+ */
13
+ import { any, bitString, boolean, component, contextImplicitTag, contextTag, generalizedTime, integer, objectIdentifier, octetString, ref, sequence, sequenceOf, utf8String, } from "@ac-kit/format-asn1";
14
+ import { algorithmIdentifier_, extensions_ } from "./_pkix.js";
15
+ const messageImprint_ = sequence([
16
+ component("hashAlgorithm", algorithmIdentifier_),
17
+ component("hashedMessage", octetString()),
18
+ ]);
19
+ const pkiStatus_ = integer().namedNumbers({
20
+ granted: 0,
21
+ grantedWithMods: 1,
22
+ rejection: 2,
23
+ waiting: 3,
24
+ revocationWarning: 4,
25
+ revocationNotification: 5,
26
+ });
27
+ const pkiFailureInfo_ = bitString().namedBits({
28
+ badAlg: 0,
29
+ badRequest: 2,
30
+ badDataFormat: 5,
31
+ timeNotAvailable: 14,
32
+ unacceptedPolicy: 15,
33
+ unacceptedExtension: 16,
34
+ addInfoNotAvailable: 17,
35
+ systemFailure: 25,
36
+ });
37
+ const pkiFreeText_ = sequenceOf(utf8String()).size(1, "MAX");
38
+ const pkiStatusInfo_ = sequence([
39
+ component("status", pkiStatus_),
40
+ component("statusString", pkiFreeText_).optional(),
41
+ component("failInfo", pkiFailureInfo_).optional(),
42
+ ]);
43
+ const accuracy_ = sequence([
44
+ component("seconds", integer()).optional(),
45
+ component("millis", contextImplicitTag(0, integer().range(1, 999))).optional(),
46
+ component("micros", contextImplicitTag(1, integer().range(1, 999))).optional(),
47
+ ]);
48
+ /** `MessageImprint ::= SEQUENCE { hashAlgorithm, hashedMessage }`. */
49
+ export const messageImprintSchema = ref(messageImprint_);
50
+ /**
51
+ * `TimeStampReq`: `version INTEGER { v1(1) }, messageImprint, reqPolicy
52
+ * TSAPolicyId OPTIONAL, nonce INTEGER OPTIONAL, certReq BOOLEAN DEFAULT FALSE,
53
+ * extensions [0] IMPLICIT Extensions OPTIONAL`.
54
+ */
55
+ export const timeStampReqSchema = ref(sequence([
56
+ component("version", integer()),
57
+ component("messageImprint", messageImprint_),
58
+ component("reqPolicy", objectIdentifier()).optional(),
59
+ component("nonce", integer()).optional(),
60
+ component("certReq", boolean()).default(false),
61
+ component("extensions", contextImplicitTag(0, extensions_)).optional(),
62
+ ]));
63
+ /** `PKIStatus ::= INTEGER`, named values 0 (`granted`) to 5. */
64
+ export const pkiStatusSchema = ref(pkiStatus_);
65
+ /** `PKIFailureInfo ::= BIT STRING`, with the eight bits RFC 3161 names. */
66
+ export const pkiFailureInfoSchema = ref(pkiFailureInfo_);
67
+ /** `PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String`. */
68
+ export const pkiFreeTextSchema = ref(pkiFreeText_);
69
+ /**
70
+ * `PKIStatusInfo ::= SEQUENCE { status, statusString OPTIONAL, failInfo
71
+ * OPTIONAL }`.
72
+ */
73
+ export const pkiStatusInfoSchema = ref(pkiStatusInfo_);
74
+ /**
75
+ * `TimeStampResp ::= SEQUENCE { status PKIStatusInfo, timeStampToken
76
+ * TimeStampToken OPTIONAL }`, the token as `ANY` (the last component, so an
77
+ * untagged `ANY` is unambiguous).
78
+ */
79
+ export const timeStampRespSchema = ref(sequence([
80
+ component("status", pkiStatusInfo_),
81
+ component("timeStampToken", any()).optional(),
82
+ ]));
83
+ /**
84
+ * `Accuracy ::= SEQUENCE { seconds INTEGER OPTIONAL, millis [0] INTEGER
85
+ * (1..999) OPTIONAL, micros [1] INTEGER (1..999) OPTIONAL }`.
86
+ */
87
+ export const accuracySchema = ref(accuracy_);
88
+ /**
89
+ * `TSTInfo`: `version, policy, messageImprint, serialNumber, genTime
90
+ * GeneralizedTime, accuracy OPTIONAL, ordering BOOLEAN DEFAULT FALSE, nonce
91
+ * OPTIONAL, tsa [0] GeneralName OPTIONAL, extensions [1] IMPLICIT Extensions
92
+ * OPTIONAL`. `GeneralName` is a CHOICE, so its `[0]` is explicit and the name
93
+ * itself is kept as `ANY`.
94
+ */
95
+ export const tstInfoSchema = ref(sequence([
96
+ component("version", integer()),
97
+ component("policy", objectIdentifier()),
98
+ component("messageImprint", messageImprint_),
99
+ component("serialNumber", integer()),
100
+ component("genTime", generalizedTime()),
101
+ component("accuracy", accuracy_).optional(),
102
+ component("ordering", boolean()).default(false),
103
+ component("nonce", integer()).optional(),
104
+ component("tsa", contextTag(0, any())).optional(),
105
+ component("extensions", contextImplicitTag(1, extensions_)).optional(),
106
+ ]));
107
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EACN,GAAG,EACH,SAAS,EACT,OAAO,EACP,SAAS,EACT,kBAAkB,EAClB,UAAU,EACV,eAAe,EACf,OAAO,EACP,gBAAgB,EAChB,WAAW,EACX,GAAG,EACH,QAAQ,EACR,UAAU,EACV,UAAU,GACV,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE/D,MAAM,eAAe,GAAG,QAAQ,CAAC;IAChC,SAAS,CAAC,eAAe,EAAE,oBAAoB,CAAC;IAChD,SAAS,CAAC,eAAe,EAAE,WAAW,EAAE,CAAC;CAChC,CAAC,CAAC;AAEZ,MAAM,UAAU,GAAG,OAAO,EAAE,CAAC,YAAY,CAAC;IACzC,OAAO,EAAE,CAAC;IACV,eAAe,EAAE,CAAC;IAClB,SAAS,EAAE,CAAC;IACZ,OAAO,EAAE,CAAC;IACV,iBAAiB,EAAE,CAAC;IACpB,sBAAsB,EAAE,CAAC;CACzB,CAAC,CAAC;AAEH,MAAM,eAAe,GAAG,SAAS,EAAE,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AC;IACpC,SAAS,CAAC,SAAS,EAAE,eAAe,EAAE,CAAC;IACvC,SAAS,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC,QAAQ,EAAE;IAC3C,SAAS,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC;IAC/C,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,QAAQ,EAAE;IACxC,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE;IACjD,SAAS,CAAC,YAAY,EAAE,kBAAkB,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,QAAQ,EAAE;CAC7D,CAAC,CACX,CAAC"}
@@ -0,0 +1,51 @@
1
+ import type { TspDecodeOptions } from "./request.js";
2
+ import { type TstInfo } from "./tst-info.js";
3
+ /**
4
+ * A time-stamp token: the complete CMS `ContentInfo` (a `SignedData` over a
5
+ * `TSTInfo`), exactly as received. Not verified: holding one proves nothing
6
+ * until {@link verifyTimeStampToken} has checked its signature and the TSA
7
+ * certificate.
8
+ */
9
+ export interface TimeStampToken {
10
+ readonly encoded: Uint8Array<ArrayBuffer>;
11
+ }
12
+ /**
13
+ * Accepts a token stored on its own (a `.tst` file, an evidence package):
14
+ * checks that it is one DER-framed `ContentInfo` of type `id-signedData` whose
15
+ * content is a SEQUENCE, and copies it. Nothing inside the `SignedData` is
16
+ * checked beyond its framing.
17
+ *
18
+ * Complexity: O(bytes).
19
+ *
20
+ * @throws {TspFormatError} For malformed framing, trailing bytes, a content
21
+ * type other than `id-signedData`, or content that is not a SEQUENCE.
22
+ */
23
+ export declare function decodeTimeStampToken(bytes: Uint8Array): TimeStampToken;
24
+ /**
25
+ * Reads the `TSTInfo` a token carries **without verifying anything**: not the
26
+ * signature, not the signer, not a certificate. The result has no authenticity
27
+ * at all — anyone can build a token saying anything. Use it to display what a
28
+ * token claims, or to check consistency with a request before or after a CMS
29
+ * verifier has authenticated the same bytes.
30
+ *
31
+ * Walks `ContentInfo → SignedData → encapContentInfo` with a permissive
32
+ * envelope (non-strict DER: CMS encoders often leave certificate sets
33
+ * unsorted), reading only the `encapContentInfo`: the certificates, CRLs and
34
+ * signer infos are skipped undecoded, so no set in them costs memory past its
35
+ * bytes. Requires `eContentType` to be `id-ct-TSTInfo`, then decodes the
36
+ * `eContent` with {@link decodeTstInfo} and `options`.
37
+ *
38
+ * Complexity: O(token).
39
+ *
40
+ * @throws {TspFormatError} With a position relative to the token for an
41
+ * envelope fault, or relative to the `eContent` octets (the message starts
42
+ * with `TSTInfo:`) for a `TSTInfo` fault. A BER indefinite-length token is
43
+ * refused, with a message saying so.
44
+ */
45
+ export declare function readTstInfoUnverified(token: TimeStampToken, options?: TspDecodeOptions): TstInfo;
46
+ /**
47
+ * Checks `bytes` is one `ContentInfo` of type `id-signedData`, positions
48
+ * shifted by `offset` (where the token sits in a larger message).
49
+ */
50
+ export declare function checkTokenEnvelope_(bytes: Uint8Array, offset: number): void;
51
+ //# sourceMappingURL=token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAiB,KAAK,OAAO,EAAE,MAAM,eAAe,CAAC;AAQ5D;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC9B,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;CAC1C;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,UAAU,GAAG,cAAc,CAGtE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,cAAc,EACrB,OAAO,GAAE,gBAAqB,GAC5B,OAAO,CAoBT;AA4DD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAqB3E"}