@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.
- package/CHANGELOG.md +59 -0
- package/LICENSE +28 -0
- package/README.md +202 -2
- package/dist/_decode.d.ts +17 -0
- package/dist/_decode.d.ts.map +1 -0
- package/dist/_decode.js +41 -0
- package/dist/_decode.js.map +1 -0
- package/dist/_generalized-time.d.ts +12 -0
- package/dist/_generalized-time.d.ts.map +1 -0
- package/dist/_generalized-time.js +40 -0
- package/dist/_generalized-time.js.map +1 -0
- package/dist/_hash-algorithms.d.ts +41 -0
- package/dist/_hash-algorithms.d.ts.map +1 -0
- package/dist/_hash-algorithms.js +66 -0
- package/dist/_hash-algorithms.js.map +1 -0
- package/dist/_pkix.d.ts +37 -0
- package/dist/_pkix.d.ts.map +1 -0
- package/dist/_pkix.js +48 -0
- package/dist/_pkix.js.map +1 -0
- package/dist/consistency.d.ts +101 -0
- package/dist/consistency.d.ts.map +1 -0
- package/dist/consistency.js +61 -0
- package/dist/consistency.js.map +1 -0
- package/dist/constants.d.ts +32 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +39 -0
- package/dist/constants.js.map +1 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +21 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/message-imprint.d.ts +72 -0
- package/dist/message-imprint.d.ts.map +1 -0
- package/dist/message-imprint.js +97 -0
- package/dist/message-imprint.js.map +1 -0
- package/dist/request.d.ts +94 -0
- package/dist/request.d.ts.map +1 -0
- package/dist/request.js +123 -0
- package/dist/request.js.map +1 -0
- package/dist/response.d.ts +56 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +92 -0
- package/dist/response.js.map +1 -0
- package/dist/schema.d.ts +39 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +107 -0
- package/dist/schema.js.map +1 -0
- package/dist/token.d.ts +51 -0
- package/dist/token.d.ts.map +1 -0
- package/dist/token.js +137 -0
- package/dist/token.js.map +1 -0
- package/dist/tst-info.d.ts +94 -0
- package/dist/tst-info.d.ts.map +1 -0
- package/dist/tst-info.js +140 -0
- package/dist/tst-info.js.map +1 -0
- package/dist/verify.d.ts +110 -0
- package/dist/verify.d.ts.map +1 -0
- package/dist/verify.js +160 -0
- package/dist/verify.js.map +1 -0
- package/package.json +78 -3
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { type ObjectIdentifier } from "@ac-kit/format-oid";
|
|
2
|
+
import { type MessageImprint } from "./message-imprint.js";
|
|
3
|
+
import { type TimeWindow, type TstInfo, type TstInfoTimeInterval } from "./tst-info.js";
|
|
4
|
+
/**
|
|
5
|
+
* What a `TSTInfo` must agree with: the fields of the request it answers. A
|
|
6
|
+
* `TimeStampRequest` fits as it is.
|
|
7
|
+
*/
|
|
8
|
+
export interface ExpectedTstInfo {
|
|
9
|
+
readonly messageImprint: MessageImprint;
|
|
10
|
+
/** The request's nonce; the token must echo it when set. */
|
|
11
|
+
readonly nonce?: bigint;
|
|
12
|
+
/** The request's policy; the token must carry exactly it when set. */
|
|
13
|
+
readonly reqPolicy?: ObjectIdentifier;
|
|
14
|
+
}
|
|
15
|
+
/** Options of {@link checkTstInfoConsistency}. */
|
|
16
|
+
export interface TstInfoConsistencyOptions {
|
|
17
|
+
/**
|
|
18
|
+
* The TSA policies to accept when the request named none, or `"any"` to
|
|
19
|
+
* accept whatever policy the TSA chose. Required then, so accepting any
|
|
20
|
+
* policy is a decision written down rather than a default; not consulted when
|
|
21
|
+
* the request named a policy, which the token must then carry.
|
|
22
|
+
*/
|
|
23
|
+
readonly acceptedPolicies?: readonly ObjectIdentifier[] | "any";
|
|
24
|
+
/**
|
|
25
|
+
* When the request was sent and the reply received, by the local clock and
|
|
26
|
+
* already widened by its tolerance; `earliest` must not be after `latest`.
|
|
27
|
+
* The token's interval ({@link tstInfoTimeInterval}) must overlap it.
|
|
28
|
+
*/
|
|
29
|
+
readonly window?: TimeWindow;
|
|
30
|
+
/**
|
|
31
|
+
* Critical extensions the caller processes itself. Any other critical
|
|
32
|
+
* extension in the token is reported, since its meaning is unknown and might
|
|
33
|
+
* change the token's.
|
|
34
|
+
*/
|
|
35
|
+
readonly understoodExtensions?: readonly ObjectIdentifier[];
|
|
36
|
+
}
|
|
37
|
+
/** One way a `TSTInfo` disagrees with what was expected. */
|
|
38
|
+
export type TstInfoMismatch = {
|
|
39
|
+
readonly kind: "messageImprint";
|
|
40
|
+
readonly expected: MessageImprint;
|
|
41
|
+
readonly actual: MessageImprint;
|
|
42
|
+
} | {
|
|
43
|
+
readonly kind: "nonce";
|
|
44
|
+
readonly expected: bigint;
|
|
45
|
+
readonly actual: bigint | undefined;
|
|
46
|
+
} | {
|
|
47
|
+
readonly kind: "policy";
|
|
48
|
+
readonly accepted: readonly ObjectIdentifier[];
|
|
49
|
+
readonly actual: ObjectIdentifier;
|
|
50
|
+
} | {
|
|
51
|
+
readonly kind: "time";
|
|
52
|
+
readonly window: TimeWindow;
|
|
53
|
+
readonly interval: TstInfoTimeInterval;
|
|
54
|
+
} | {
|
|
55
|
+
readonly kind: "criticalExtension";
|
|
56
|
+
readonly extnID: ObjectIdentifier;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* A `TSTInfo` consistent with the request — and still unauthenticated: the
|
|
60
|
+
* consistency check verifies no signature, so the token may be forged. Only
|
|
61
|
+
* {@link verifyTimeStampToken} authenticates it.
|
|
62
|
+
*/
|
|
63
|
+
export interface ConsistentUnauthenticatedTstInfo {
|
|
64
|
+
readonly consistency: "consistentUnauthenticated";
|
|
65
|
+
readonly tstInfo: TstInfo;
|
|
66
|
+
readonly timeInterval: TstInfoTimeInterval;
|
|
67
|
+
}
|
|
68
|
+
/** A `TSTInfo` that disagrees with the request in at least one way. */
|
|
69
|
+
export interface InconsistentTstInfo {
|
|
70
|
+
readonly consistency: "inconsistent";
|
|
71
|
+
readonly tstInfo: TstInfo;
|
|
72
|
+
readonly timeInterval: TstInfoTimeInterval;
|
|
73
|
+
readonly mismatches: readonly [TstInfoMismatch, ...TstInfoMismatch[]];
|
|
74
|
+
}
|
|
75
|
+
/** The result of {@link checkTstInfoConsistency}. */
|
|
76
|
+
export type TstInfoConsistency = ConsistentUnauthenticatedTstInfo | InconsistentTstInfo;
|
|
77
|
+
/**
|
|
78
|
+
* Checks that a `TSTInfo` answers the request it is supposed to (RFC 3161
|
|
79
|
+
* §2.4.2): the same message imprint (absent and `NULL` hash parameters taken as
|
|
80
|
+
* one), the nonce echoed when the request had one, the policy the request named
|
|
81
|
+
* or one of `acceptedPolicies`, an interval overlapping `window` when given,
|
|
82
|
+
* and no critical extension outside `understoodExtensions`. Every mismatch is
|
|
83
|
+
* reported, not just the first.
|
|
84
|
+
*
|
|
85
|
+
* Consistency is not authenticity: the result says nothing about who made the
|
|
86
|
+
* token, which is why the success case is named "consistent, unauthenticated".
|
|
87
|
+
*
|
|
88
|
+
* Complexity: O(digest + policies + extensions × understood extensions).
|
|
89
|
+
*
|
|
90
|
+
* @throws {TypeError} When the request named no policy and `acceptedPolicies`
|
|
91
|
+
* is missing.
|
|
92
|
+
*/
|
|
93
|
+
export declare function checkTstInfoConsistency(tstInfo: TstInfo, expected: ExpectedTstInfo & {
|
|
94
|
+
readonly reqPolicy: ObjectIdentifier;
|
|
95
|
+
}, options?: TstInfoConsistencyOptions & {
|
|
96
|
+
readonly acceptedPolicies?: never;
|
|
97
|
+
}): TstInfoConsistency;
|
|
98
|
+
export declare function checkTstInfoConsistency(tstInfo: TstInfo, expected: ExpectedTstInfo, options: TstInfoConsistencyOptions & {
|
|
99
|
+
readonly acceptedPolicies: readonly ObjectIdentifier[] | "any";
|
|
100
|
+
}): TstInfoConsistency;
|
|
101
|
+
//# sourceMappingURL=consistency.d.ts.map
|
|
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G;IACpC,QAAQ,CAAC,gBAAgB,EAAE,SAAS,gBAAgB,EAAE,GAAG,KAAK,CAAC;CAC/D,GACC,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { oidEqual } from "@ac-kit/format-oid";
|
|
2
|
+
import { isSameMessageImprint, } from "./message-imprint.js";
|
|
3
|
+
import { tstInfoTimeInterval, } from "./tst-info.js";
|
|
4
|
+
export function checkTstInfoConsistency(tstInfo, expected, options = {}) {
|
|
5
|
+
const accepted = expected.reqPolicy === undefined
|
|
6
|
+
? options.acceptedPolicies
|
|
7
|
+
: [expected.reqPolicy];
|
|
8
|
+
if (accepted === undefined) {
|
|
9
|
+
throw new TypeError('checkTstInfoConsistency: the request named no policy, so acceptedPolicies is required (a list, or "any")');
|
|
10
|
+
}
|
|
11
|
+
const mismatches = [];
|
|
12
|
+
if (!isSameMessageImprint(expected.messageImprint, tstInfo.messageImprint)) {
|
|
13
|
+
mismatches.push({
|
|
14
|
+
kind: "messageImprint",
|
|
15
|
+
expected: expected.messageImprint,
|
|
16
|
+
actual: tstInfo.messageImprint,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
if (expected.nonce !== undefined && tstInfo.nonce !== expected.nonce) {
|
|
20
|
+
mismatches.push({
|
|
21
|
+
kind: "nonce",
|
|
22
|
+
expected: expected.nonce,
|
|
23
|
+
actual: tstInfo.nonce,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
if (accepted !== "any" &&
|
|
27
|
+
!accepted.some((policy) => oidEqual(policy, tstInfo.policy))) {
|
|
28
|
+
mismatches.push({ kind: "policy", accepted, actual: tstInfo.policy });
|
|
29
|
+
}
|
|
30
|
+
const window = options.window;
|
|
31
|
+
if (window !== undefined &&
|
|
32
|
+
Temporal.Instant.compare(window.earliest, window.latest) > 0) {
|
|
33
|
+
throw new RangeError(`checkTstInfoConsistency: window.earliest ${window.earliest.toString()} is after window.latest ${window.latest.toString()}`);
|
|
34
|
+
}
|
|
35
|
+
const timeInterval = tstInfoTimeInterval(tstInfo);
|
|
36
|
+
if (window !== undefined &&
|
|
37
|
+
(Temporal.Instant.compare(timeInterval.latest, window.earliest) < 0 ||
|
|
38
|
+
Temporal.Instant.compare(timeInterval.earliest, window.latest) > 0)) {
|
|
39
|
+
mismatches.push({ kind: "time", window, interval: timeInterval });
|
|
40
|
+
}
|
|
41
|
+
const understood = options.understoodExtensions ?? [];
|
|
42
|
+
for (const extension of tstInfo.extensions ?? []) {
|
|
43
|
+
if (extension.critical &&
|
|
44
|
+
!understood.some((extnID) => oidEqual(extnID, extension.extnID))) {
|
|
45
|
+
mismatches.push({
|
|
46
|
+
kind: "criticalExtension",
|
|
47
|
+
extnID: extension.extnID,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const [first, ...rest] = mismatches;
|
|
52
|
+
return first === undefined
|
|
53
|
+
? { consistency: "consistentUnauthenticated", tstInfo, timeInterval }
|
|
54
|
+
: {
|
|
55
|
+
consistency: "inconsistent",
|
|
56
|
+
tstInfo,
|
|
57
|
+
timeInterval,
|
|
58
|
+
mismatches: [first, ...rest],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=consistency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consistency.js","sourceRoot":"","sources":["../src/consistency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAyB,MAAM,oBAAoB,CAAC;AAErE,OAAO,EACN,oBAAoB,GAEpB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACN,mBAAmB,GAInB,MAAM,eAAe,CAAC;AAoHvB,MAAM,UAAU,uBAAuB,CACtC,OAAgB,EAChB,QAAyB,EACzB,OAAO,GAA8B,EAAE;IAEvC,MAAM,QAAQ,GACb,QAAQ,CAAC,SAAS,KAAK,SAAS;QAC/B,CAAC,CAAC,OAAO,CAAC,gBAAgB;QAC1B,CAAC,CAAC,CAAC,QA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AK,EAAE,GAAG,IAAI,CAAC;SAC5B,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ObjectIdentifier } from "@ac-kit/format-oid";
|
|
2
|
+
/**
|
|
3
|
+
* Media type of an HTTP request body holding a DER `TimeStampReq` (RFC 3161
|
|
4
|
+
* §3.4).
|
|
5
|
+
*/
|
|
6
|
+
export declare const timeStampQueryMediaType = "application/timestamp-query";
|
|
7
|
+
/**
|
|
8
|
+
* Media type of an HTTP response body holding a DER `TimeStampResp` (RFC 3161
|
|
9
|
+
* §3.4).
|
|
10
|
+
*/
|
|
11
|
+
export declare const timeStampReplyMediaType = "application/timestamp-reply";
|
|
12
|
+
/**
|
|
13
|
+
* `id-ct-TSTInfo`, 1.2.840.113549.1.9.16.1.4: the `eContentType` of a
|
|
14
|
+
* time-stamp token (RFC 3161 §2.4.2).
|
|
15
|
+
*/
|
|
16
|
+
export declare const idCtTstInfo: ObjectIdentifier;
|
|
17
|
+
/**
|
|
18
|
+
* `id-aa-timeStampToken`, 1.2.840.113549.1.9.16.2.14: the unsigned attribute
|
|
19
|
+
* carrying a time-stamp token over a signature (RFC 3161 Appendix A).
|
|
20
|
+
*/
|
|
21
|
+
export declare const idAaTimeStampToken: ObjectIdentifier;
|
|
22
|
+
/**
|
|
23
|
+
* `id-kp-timeStamping`, 1.3.6.1.5.5.7.3.8: the extended key usage a TSA
|
|
24
|
+
* certificate carries, as its only and critical one (RFC 3161 §2.3).
|
|
25
|
+
*/
|
|
26
|
+
export declare const idKpTimeStamping: ObjectIdentifier;
|
|
27
|
+
/**
|
|
28
|
+
* `id-signedData`, 1.2.840.113549.1.7.2: the `contentType` of the CMS
|
|
29
|
+
* `ContentInfo` a time-stamp token is (RFC 5652 §5.1).
|
|
30
|
+
*/
|
|
31
|
+
export declare const idSignedData: ObjectIdentifier;
|
|
32
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE3D;;;GAGG;AACH,eAAO,MAAM,uBAAuB,gCAAgC,CAAC;AAErE;;;GAGG;AACH,eAAO,MAAM,uBAAuB,gCAAgC,CAAC;AAErE;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,gBAExB,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,kBAAkB,EAAE,gBAE/B,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,gBAE7B,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE,gBAEzB,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Media type of an HTTP request body holding a DER `TimeStampReq` (RFC 3161
|
|
3
|
+
* §3.4).
|
|
4
|
+
*/
|
|
5
|
+
export const timeStampQueryMediaType = "application/timestamp-query";
|
|
6
|
+
/**
|
|
7
|
+
* Media type of an HTTP response body holding a DER `TimeStampResp` (RFC 3161
|
|
8
|
+
* §3.4).
|
|
9
|
+
*/
|
|
10
|
+
export const timeStampReplyMediaType = "application/timestamp-reply";
|
|
11
|
+
/**
|
|
12
|
+
* `id-ct-TSTInfo`, 1.2.840.113549.1.9.16.1.4: the `eContentType` of a
|
|
13
|
+
* time-stamp token (RFC 3161 §2.4.2).
|
|
14
|
+
*/
|
|
15
|
+
export const idCtTstInfo = Object.freeze([
|
|
16
|
+
1, 2, 840, 113549, 1, 9, 16, 1, 4,
|
|
17
|
+
]);
|
|
18
|
+
/**
|
|
19
|
+
* `id-aa-timeStampToken`, 1.2.840.113549.1.9.16.2.14: the unsigned attribute
|
|
20
|
+
* carrying a time-stamp token over a signature (RFC 3161 Appendix A).
|
|
21
|
+
*/
|
|
22
|
+
export const idAaTimeStampToken = Object.freeze([
|
|
23
|
+
1, 2, 840, 113549, 1, 9, 16, 2, 14,
|
|
24
|
+
]);
|
|
25
|
+
/**
|
|
26
|
+
* `id-kp-timeStamping`, 1.3.6.1.5.5.7.3.8: the extended key usage a TSA
|
|
27
|
+
* certificate carries, as its only and critical one (RFC 3161 §2.3).
|
|
28
|
+
*/
|
|
29
|
+
export const idKpTimeStamping = Object.freeze([
|
|
30
|
+
1, 3, 6, 1, 5, 5, 7, 3, 8,
|
|
31
|
+
]);
|
|
32
|
+
/**
|
|
33
|
+
* `id-signedData`, 1.2.840.113549.1.7.2: the `contentType` of the CMS
|
|
34
|
+
* `ContentInfo` a time-stamp token is (RFC 5652 §5.1).
|
|
35
|
+
*/
|
|
36
|
+
export const idSignedData = Object.freeze([
|
|
37
|
+
1, 2, 840, 113549, 1, 7, 2,
|
|
38
|
+
]);
|
|
39
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,6BAA6B,CAAC;AAErE;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,6BAA6B,CAAC;AAErE;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAqB,MAAM,CAAC,MAAM,CAAC;IAC1D,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC;CACjC,CAAC,CAAC;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAqB,MAAM,CAAC,MAAM,CAAC;IACjE,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;CAClC,CAAC,CAAC;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAqB,MAAM,CAAC,MAAM,CAAC;IAC/D,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;CACzB,CAAC,CAAC;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAqB,MAAM,CAAC,MAAM,CAAC;IAC3D,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;CAC1B,CAAC,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bytes that are not a well-formed time-stamp protocol message: malformed DER,
|
|
3
|
+
* a field outside its RFC 3161 range, or a contradiction between fields (a
|
|
4
|
+
* granted status without a token).
|
|
5
|
+
*
|
|
6
|
+
* `position` is the byte offset the decoder gave up at, relative to the bytes
|
|
7
|
+
* the failing function was given, or to the part named at the start of the
|
|
8
|
+
* message (`TSTInfo:` offsets are relative to the `eContent` octets).
|
|
9
|
+
* `undefined` when the fault is not at one place, such as two fields that
|
|
10
|
+
* disagree. A failure inside `@ac-kit/format-asn1` is kept as `cause`.
|
|
11
|
+
*/
|
|
12
|
+
export declare class TspFormatError extends SyntaxError {
|
|
13
|
+
/** Byte offset of the fault, or `undefined` when it has no single place. */
|
|
14
|
+
readonly position: number | undefined;
|
|
15
|
+
constructor(message: string, position: number | undefined, options?: ErrorOptions);
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,qBAAa,cAAe,SAAQ,WAAW;IAC9C,4EAA4E;IAC5E,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAEtC,YACC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,OAAO,CAAC,EAAE,YAAY,EAKtB;CACD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bytes that are not a well-formed time-stamp protocol message: malformed DER,
|
|
3
|
+
* a field outside its RFC 3161 range, or a contradiction between fields (a
|
|
4
|
+
* granted status without a token).
|
|
5
|
+
*
|
|
6
|
+
* `position` is the byte offset the decoder gave up at, relative to the bytes
|
|
7
|
+
* the failing function was given, or to the part named at the start of the
|
|
8
|
+
* message (`TSTInfo:` offsets are relative to the `eContent` octets).
|
|
9
|
+
* `undefined` when the fault is not at one place, such as two fields that
|
|
10
|
+
* disagree. A failure inside `@ac-kit/format-asn1` is kept as `cause`.
|
|
11
|
+
*/
|
|
12
|
+
export class TspFormatError extends SyntaxError {
|
|
13
|
+
/** Byte offset of the fault, or `undefined` when it has no single place. */
|
|
14
|
+
position;
|
|
15
|
+
constructor(message, position, options) {
|
|
16
|
+
super(message, options);
|
|
17
|
+
this.name = "TspFormatError";
|
|
18
|
+
this.position = position;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,OAAO,cAAe,SAAQ,WAAW;IAC9C,4EAA4E;IACnE,QAAQ,CAAqB;IAEtC,YACC,OAAe,EACf,QAA4B,EAC5B,OAAsB;QAEtB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC1B,CAAC;CACD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./consistency.js";
|
|
2
|
+
export * from "./constants.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export * from "./message-imprint.js";
|
|
5
|
+
export * from "./request.js";
|
|
6
|
+
export * from "./response.js";
|
|
7
|
+
export * from "./token.js";
|
|
8
|
+
export * from "./tst-info.js";
|
|
9
|
+
export * from "./verify.js";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./consistency.js";
|
|
2
|
+
export * from "./constants.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export * from "./message-imprint.js";
|
|
5
|
+
export * from "./request.js";
|
|
6
|
+
export * from "./response.js";
|
|
7
|
+
export * from "./token.js";
|
|
8
|
+
export * from "./tst-info.js";
|
|
9
|
+
export * from "./verify.js";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC;AACrC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type MaybePromiseLike } from "@ac-kit/core";
|
|
2
|
+
import { type ObjectIdentifier } from "@ac-kit/format-oid";
|
|
3
|
+
/**
|
|
4
|
+
* Hashes this package recognises in a `MessageImprint`. SHA-1 is read, to check
|
|
5
|
+
* tokens issued long ago, but never requested.
|
|
6
|
+
*/
|
|
7
|
+
export type TspHashAlgorithm = "sha1" | "sha256" | "sha384" | "sha512";
|
|
8
|
+
/** Hashes a new request may use. */
|
|
9
|
+
export type TspRequestHashAlgorithm = Exclude<TspHashAlgorithm, "sha1">;
|
|
10
|
+
/** An `AlgorithmIdentifier` (RFC 5280 §4.1.1.2). */
|
|
11
|
+
export interface AlgorithmIdentifierValue {
|
|
12
|
+
readonly algorithm: ObjectIdentifier;
|
|
13
|
+
/** The complete DER of the parameters as received; absent when omitted. */
|
|
14
|
+
readonly parameters?: Uint8Array<ArrayBuffer>;
|
|
15
|
+
}
|
|
16
|
+
/** The hash of the time-stamped data, with its algorithm (RFC 3161 §2.4.1). */
|
|
17
|
+
export interface MessageImprint {
|
|
18
|
+
readonly hashAlgorithm: AlgorithmIdentifierValue;
|
|
19
|
+
readonly hashedMessage: Uint8Array<ArrayBuffer>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The imprint of data already hashed with `algorithm`, written with absent hash
|
|
23
|
+
* parameters as RFC 5754 §2 requires of a generator. Use this when the data is
|
|
24
|
+
* not in memory: hash it through a `@ac-kit/crypto-hash` `*Sink`, then pass the
|
|
25
|
+
* digest here. SHA-1 is accepted so a caller can state what an old token should
|
|
26
|
+
* hold; {@link createTimeStampRequest} still refuses it.
|
|
27
|
+
*
|
|
28
|
+
* Complexity: O(digest); the digest is copied.
|
|
29
|
+
*
|
|
30
|
+
* @throws {RangeError} When `digest` is not the length `algorithm` produces
|
|
31
|
+
* (20, 32, 48 or 64 bytes).
|
|
32
|
+
*/
|
|
33
|
+
export declare function messageImprintFromDigest(algorithm: TspHashAlgorithm, digest: Uint8Array): MessageImprint;
|
|
34
|
+
/**
|
|
35
|
+
* Hashes `data` with `algorithm` and returns its imprint. Hashing goes through
|
|
36
|
+
* `@ac-kit/crypto-hash` — Web Crypto when the runtime has it, so the result is
|
|
37
|
+
* then a promise; always `await` it.
|
|
38
|
+
*
|
|
39
|
+
* Complexity: O(data).
|
|
40
|
+
*/
|
|
41
|
+
export declare function createMessageImprint(algorithm: TspRequestHashAlgorithm, data: Uint8Array<ArrayBuffer>): MaybePromiseLike<MessageImprint>;
|
|
42
|
+
/**
|
|
43
|
+
* The hash an imprint names, or `undefined` when its OID is not one of
|
|
44
|
+
* {@link TspHashAlgorithm} — or when it is, but the imprint is malformed:
|
|
45
|
+
* parameters other than absent or DER `NULL` (RFC 5754 §2, RFC 8017 Appendix
|
|
46
|
+
* B.1), or a digest of the wrong length for the algorithm.
|
|
47
|
+
*
|
|
48
|
+
* Complexity: O(1).
|
|
49
|
+
*/
|
|
50
|
+
export declare function messageImprintHashAlgorithm(imprint: MessageImprint): TspHashAlgorithm | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Whether two imprints are the same. Both must be well formed when their
|
|
53
|
+
* algorithm is known (see {@link messageImprintHashAlgorithm}): a malformed
|
|
54
|
+
* imprint is the same as nothing, itself included. Then: same algorithm OID,
|
|
55
|
+
* parameters equal once absent and DER `NULL` are taken as one (RFC 5754 §2 has
|
|
56
|
+
* a reader accept both), and the same hashed bytes. An unknown algorithm is
|
|
57
|
+
* compared as opaque bytes. Not constant-time; nothing here is secret.
|
|
58
|
+
*
|
|
59
|
+
* Complexity: O(digest).
|
|
60
|
+
*/
|
|
61
|
+
export declare function isSameMessageImprint(first: MessageImprint, second: MessageImprint): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Whether `imprint` is the `algorithm` hash `digest`: the caller names the
|
|
64
|
+
* algorithm it hashed with, so a token cannot choose a weaker one for it. SHA-1
|
|
65
|
+
* is allowed here, to check old tokens. A malformed imprint is never one.
|
|
66
|
+
*
|
|
67
|
+
* Complexity: O(digest).
|
|
68
|
+
*
|
|
69
|
+
* @throws {RangeError} When `digest` is not the length `algorithm` produces.
|
|
70
|
+
*/
|
|
71
|
+
export declare function isMessageImprintOfDigest(imprint: MessageImprint, algorithm: TspHashAlgorithm, digest: Uint8Array): boolean;
|
|
72
|
+
//# sourceMappingURL=message-imprint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-imprint.d.ts","sourceRoot":"","sources":["../src/message-imprint.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC/E,OAAO,EAAY,KAAK,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAQrE;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAEvE,oCAAoC;AACpC,MAAM,MAAM,uBAAuB,GAAG,OAAO,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;AAExE,oDAAoD;AACpD,MAAM,WAAW,wBAAwB;IACxC,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC;IACrC,2EAA2E;IAC3E,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;CAC9C;AAED,+EAA+E;AAC/E,MAAM,WAAW,cAAc;IAC9B,QAAQ,CAAC,aAAa,EAAE,wBAAwB,CAAC;IACjD,QAAQ,CAAC,aAAa,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;CAChD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,wBAAwB,CACvC,SAAS,EAAE,gBAAgB,EAC3B,MAAM,EAAE,UAAU,GAChB,cAAc,CAWhB;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CACnC,SAAS,EAAE,uBAAuB,EAClC,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,GAC3B,gBAAgB,CAAC,cAAc,CAAC,CAMlC;AAED;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CAC1C,OAAO,EAAE,cAAc,GACrB,gBAAgB,GAAG,SAAS,CAG9B;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,cAAc,EACrB,MAAM,EAAE,cAAc,GACpB,OAAO,CAsBT;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACvC,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,gBAAgB,EAC3B,MAAM,EAAE,UAAU,GAChB,OAAO,CAWT"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { bytesIsEqual, isThenable } from "@ac-kit/core";
|
|
2
|
+
import { oidEqual } from "@ac-kit/format-oid";
|
|
3
|
+
import { classifyImprint_, hashAlgorithms_, isAbsentOrNull_, } from "./_hash-algorithms.js";
|
|
4
|
+
/**
|
|
5
|
+
* The imprint of data already hashed with `algorithm`, written with absent hash
|
|
6
|
+
* parameters as RFC 5754 §2 requires of a generator. Use this when the data is
|
|
7
|
+
* not in memory: hash it through a `@ac-kit/crypto-hash` `*Sink`, then pass the
|
|
8
|
+
* digest here. SHA-1 is accepted so a caller can state what an old token should
|
|
9
|
+
* hold; {@link createTimeStampRequest} still refuses it.
|
|
10
|
+
*
|
|
11
|
+
* Complexity: O(digest); the digest is copied.
|
|
12
|
+
*
|
|
13
|
+
* @throws {RangeError} When `digest` is not the length `algorithm` produces
|
|
14
|
+
* (20, 32, 48 or 64 bytes).
|
|
15
|
+
*/
|
|
16
|
+
export function messageImprintFromDigest(algorithm, digest) {
|
|
17
|
+
const entry = hashAlgorithms_[algorithm];
|
|
18
|
+
if (digest.length !== entry.digestLength) {
|
|
19
|
+
throw new RangeError(`messageImprintFromDigest: a ${algorithm} digest is ${entry.digestLength} bytes, got ${digest.length}`);
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
hashAlgorithm: { algorithm: entry.oid },
|
|
23
|
+
hashedMessage: new Uint8Array(digest),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Hashes `data` with `algorithm` and returns its imprint. Hashing goes through
|
|
28
|
+
* `@ac-kit/crypto-hash` — Web Crypto when the runtime has it, so the result is
|
|
29
|
+
* then a promise; always `await` it.
|
|
30
|
+
*
|
|
31
|
+
* Complexity: O(data).
|
|
32
|
+
*/
|
|
33
|
+
export function createMessageImprint(algorithm, data) {
|
|
34
|
+
const digest = hashAlgorithms_[algorithm].hash(data);
|
|
35
|
+
if (isThenable(digest)) {
|
|
36
|
+
return digest.then((bytes) => messageImprintFromDigest(algorithm, bytes));
|
|
37
|
+
}
|
|
38
|
+
return messageImprintFromDigest(algorithm, digest);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The hash an imprint names, or `undefined` when its OID is not one of
|
|
42
|
+
* {@link TspHashAlgorithm} — or when it is, but the imprint is malformed:
|
|
43
|
+
* parameters other than absent or DER `NULL` (RFC 5754 §2, RFC 8017 Appendix
|
|
44
|
+
* B.1), or a digest of the wrong length for the algorithm.
|
|
45
|
+
*
|
|
46
|
+
* Complexity: O(1).
|
|
47
|
+
*/
|
|
48
|
+
export function messageImprintHashAlgorithm(imprint) {
|
|
49
|
+
const classified = classifyImprint_(imprint);
|
|
50
|
+
return classified.kind === "known" ? classified.entry.name : undefined;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Whether two imprints are the same. Both must be well formed when their
|
|
54
|
+
* algorithm is known (see {@link messageImprintHashAlgorithm}): a malformed
|
|
55
|
+
* imprint is the same as nothing, itself included. Then: same algorithm OID,
|
|
56
|
+
* parameters equal once absent and DER `NULL` are taken as one (RFC 5754 §2 has
|
|
57
|
+
* a reader accept both), and the same hashed bytes. An unknown algorithm is
|
|
58
|
+
* compared as opaque bytes. Not constant-time; nothing here is secret.
|
|
59
|
+
*
|
|
60
|
+
* Complexity: O(digest).
|
|
61
|
+
*/
|
|
62
|
+
export function isSameMessageImprint(first, second) {
|
|
63
|
+
const firstClass = classifyImprint_(first);
|
|
64
|
+
const secondClass = classifyImprint_(second);
|
|
65
|
+
if (firstClass.kind === "malformed" || secondClass.kind === "malformed") {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (!oidEqual(first.hashAlgorithm.algorithm, second.hashAlgorithm.algorithm)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
const firstParameters = first.hashAlgorithm.parameters;
|
|
72
|
+
const secondParameters = second.hashAlgorithm.parameters;
|
|
73
|
+
const sameParameters = isAbsentOrNull_(firstParameters) && isAbsentOrNull_(secondParameters)
|
|
74
|
+
? true
|
|
75
|
+
: firstParameters !== undefined &&
|
|
76
|
+
secondParameters !== undefined &&
|
|
77
|
+
bytesIsEqual(firstParameters, secondParameters);
|
|
78
|
+
return (sameParameters && bytesIsEqual(first.hashedMessage, second.hashedMessage));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Whether `imprint` is the `algorithm` hash `digest`: the caller names the
|
|
82
|
+
* algorithm it hashed with, so a token cannot choose a weaker one for it. SHA-1
|
|
83
|
+
* is allowed here, to check old tokens. A malformed imprint is never one.
|
|
84
|
+
*
|
|
85
|
+
* Complexity: O(digest).
|
|
86
|
+
*
|
|
87
|
+
* @throws {RangeError} When `digest` is not the length `algorithm` produces.
|
|
88
|
+
*/
|
|
89
|
+
export function isMessageImprintOfDigest(imprint, algorithm, digest) {
|
|
90
|
+
const entry = hashAlgorithms_[algorithm];
|
|
91
|
+
if (digest.length !== entry.digestLength) {
|
|
92
|
+
throw new RangeError(`isMessageImprintOfDigest: a ${algorithm} digest is ${entry.digestLength} bytes, got ${digest.length}`);
|
|
93
|
+
}
|
|
94
|
+
return (messageImprintHashAlgorithm(imprint) === algorithm &&
|
|
95
|
+
bytesIsEqual(imprint.hashedMessage, digest));
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=message-imprint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-imprint.js","sourceRoot":"","sources":["../src/message-imprint.ts"],"names":[],"mappings":"AAAA,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,KAAK,CAAC,YAAY,eAAe,MAAM,CAAC,MAAM,EAAE,CACtG,CAAC;IACH,CAAC;IACD,OAAO,CACN,2BAA2B,CAAC,OAAO,CAAC,KAAK,SAAS;QAClD,YAAY,CAAC,OAAO,CAAC,aAAa,EAAE,MAAM,CAAC,CAC3C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type ObjectIdentifier } from "@ac-kit/format-oid";
|
|
2
|
+
import type { MessageImprint } from "./message-imprint.js";
|
|
3
|
+
/** An X.509 `Extension` as RFC 3161 carries it in requests and `TSTInfo`. */
|
|
4
|
+
export interface TspExtension {
|
|
5
|
+
readonly extnID: ObjectIdentifier;
|
|
6
|
+
readonly critical: boolean;
|
|
7
|
+
/** The `extnValue` OCTET STRING contents: the DER of the extension value. */
|
|
8
|
+
readonly extnValue: Uint8Array<ArrayBuffer>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A `TimeStampReq` (RFC 3161 §2.4.1). The version is always v1, so it is not a
|
|
12
|
+
* field.
|
|
13
|
+
*/
|
|
14
|
+
export interface TimeStampRequest {
|
|
15
|
+
readonly messageImprint: MessageImprint;
|
|
16
|
+
/** The TSA policy the token should be issued under. */
|
|
17
|
+
readonly reqPolicy?: ObjectIdentifier;
|
|
18
|
+
readonly nonce?: bigint;
|
|
19
|
+
/** Whether the TSA must include its signing certificate in the token. */
|
|
20
|
+
readonly certReq: boolean;
|
|
21
|
+
/** Never empty when present. */
|
|
22
|
+
readonly extensions?: readonly TspExtension[];
|
|
23
|
+
}
|
|
24
|
+
/** Options of {@link createTimeStampRequest}. */
|
|
25
|
+
export interface CreateTimeStampRequestOptions {
|
|
26
|
+
readonly reqPolicy?: ObjectIdentifier;
|
|
27
|
+
/**
|
|
28
|
+
* `undefined` (the default): a random nonce; `null`: no nonce; a bigint: that
|
|
29
|
+
* value. A nonce ties the reply to this request, so leave it random unless
|
|
30
|
+
* replaying a stored request.
|
|
31
|
+
*/
|
|
32
|
+
readonly nonce?: bigint | null;
|
|
33
|
+
/**
|
|
34
|
+
* Bytes of randomness in a generated nonce, 8 to 32. Default 8: the 64 bits
|
|
35
|
+
* RFC 3161 suggests and `openssl ts` sends, which every TSA accepts; some cap
|
|
36
|
+
* the nonce they echo, so a longer one buys nothing a TSA will honour.
|
|
37
|
+
*/
|
|
38
|
+
readonly nonceByteLength?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Default `false` (the RFC's DEFAULT). Set `true` when the token must be
|
|
41
|
+
* verifiable offline, without fetching the TSA certificate.
|
|
42
|
+
*/
|
|
43
|
+
readonly certReq?: boolean;
|
|
44
|
+
/** Never empty when present. */
|
|
45
|
+
readonly extensions?: readonly TspExtension[];
|
|
46
|
+
}
|
|
47
|
+
/** Options of the decoders. */
|
|
48
|
+
export interface TspDecodeOptions {
|
|
49
|
+
/**
|
|
50
|
+
* Default `true`: strict DER, as RFC 3161 requires of these structures.
|
|
51
|
+
* `false` accepts canonical-form violations from BER encoders; malformed
|
|
52
|
+
* framing is refused either way.
|
|
53
|
+
*/
|
|
54
|
+
readonly strict?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A v1 request for a token over `messageImprint`. Synchronous: the imprint is
|
|
58
|
+
* already hashed, and the nonce comes from `crypto.getRandomValues` through
|
|
59
|
+
* `@ac-kit/crypto-random`, read as an unsigned integer.
|
|
60
|
+
*
|
|
61
|
+
* An imprint whose algorithm this package does not know is let through, so a
|
|
62
|
+
* caller may request a hash it hashed itself.
|
|
63
|
+
*
|
|
64
|
+
* Complexity: O(digest + extensions).
|
|
65
|
+
*
|
|
66
|
+
* @throws {RangeError} When `nonceByteLength` is not an integer from 8 to 32;
|
|
67
|
+
* the imprint is SHA-1 or MD5 (too weak to request); a known hash has the
|
|
68
|
+
* wrong digest length or parameters other than absent or `NULL`; or
|
|
69
|
+
* `extensions` is empty.
|
|
70
|
+
*/
|
|
71
|
+
export declare function createTimeStampRequest(messageImprint: MessageImprint, options?: CreateTimeStampRequestOptions): TimeStampRequest;
|
|
72
|
+
/**
|
|
73
|
+
* The DER `TimeStampReq`, the body of an HTTP request of media type
|
|
74
|
+
* `application/timestamp-query`. `certReq` FALSE is left out, as DER requires
|
|
75
|
+
* of a DEFAULT value.
|
|
76
|
+
*
|
|
77
|
+
* Complexity: O(encoded size).
|
|
78
|
+
*
|
|
79
|
+
* @throws {RangeError} When `extensions` is empty.
|
|
80
|
+
*/
|
|
81
|
+
export declare function encodeTimeStampRequest(request: TimeStampRequest): Uint8Array<ArrayBuffer>;
|
|
82
|
+
/**
|
|
83
|
+
* Decodes a DER `TimeStampReq`. Hash parameters and extension values are kept
|
|
84
|
+
* byte for byte, so re-encoding a request from another encoder gives the same
|
|
85
|
+
* bytes when it was DER.
|
|
86
|
+
*
|
|
87
|
+
* Complexity: O(bytes).
|
|
88
|
+
*
|
|
89
|
+
* @throws {TspFormatError} For malformed DER (canonical-form violations too,
|
|
90
|
+
* unless `strict` is `false`), a version other than 1, or empty
|
|
91
|
+
* `extensions`.
|
|
92
|
+
*/
|
|
93
|
+
export declare function decodeTimeStampRequest(bytes: Uint8Array, options?: TspDecodeOptions): TimeStampRequest;
|
|
94
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAGA,OAAO,EAAY,KAAK,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAcrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAG3D,6EAA6E;AAC7E,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC;IAClC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,6EAA6E;IAC7E,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;CAC5C;AAED;;;GAGG;AACH,MAAM,WAAW,gBA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