micro509 0.12.0 → 0.13.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.
Files changed (36) hide show
  1. package/dist/der/der.d.ts +127 -0
  2. package/dist/der/der.js +171 -0
  3. package/dist/der/der.js.map +1 -0
  4. package/dist/der.d.ts +4 -0
  5. package/dist/der.js +4 -0
  6. package/dist/internal/asn1/asn1.d.ts +20 -0
  7. package/dist/internal/asn1/asn1.js +60 -28
  8. package/dist/internal/asn1/asn1.js.map +1 -1
  9. package/dist/internal/asn1/der.d.ts +171 -0
  10. package/dist/internal/asn1/der.js +101 -36
  11. package/dist/internal/asn1/der.js.map +1 -1
  12. package/dist/internal/asn1/oids2.js +9 -2
  13. package/dist/internal/asn1/oids2.js.map +1 -1
  14. package/dist/internal/shared/dn.js +142 -12
  15. package/dist/internal/shared/dn.js.map +1 -1
  16. package/dist/internal/shared/rfc3454-tables.js +1782 -0
  17. package/dist/internal/shared/rfc3454-tables.js.map +1 -0
  18. package/dist/internal/verify/name-constraints-engine.js +47 -15
  19. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  20. package/dist/internal/verify/policy-engine.js +85 -94
  21. package/dist/internal/verify/policy-engine.js.map +1 -1
  22. package/dist/internal/verify/verify-path.js +18 -14
  23. package/dist/internal/verify/verify-path.js.map +1 -1
  24. package/dist/internal/x509/extension-bits.js +1 -1
  25. package/dist/internal/x509/extension-bits.js.map +1 -1
  26. package/dist/revocation/chain.js +110 -55
  27. package/dist/revocation/chain.js.map +1 -1
  28. package/dist/revocation/crl.js.map +1 -1
  29. package/dist/revocation/ocsp.js +1 -1
  30. package/dist/revocation/ocsp.js.map +1 -1
  31. package/dist/verify/verify.d.ts +14 -1
  32. package/dist/verify/verify.js +39 -32
  33. package/dist/verify/verify.js.map +1 -1
  34. package/dist/x509/extensions.d.ts +1 -1
  35. package/dist/x509/extensions.js.map +1 -1
  36. package/package.json +32 -22
@@ -0,0 +1,171 @@
1
+ //#region src/internal/asn1/der.d.ts
2
+ /**
3
+ * Low-level DER encoding and reading helpers shared across the library.
4
+ *
5
+ * These utilities build and traverse ASN.1 TLV (tag-length-value) structures.
6
+ *
7
+ * @module
8
+ */
9
+ /**
10
+ * Maximum nesting depth allowed when recursively walking a DER structure.
11
+ *
12
+ * Guards against stack exhaustion from pathologically nested input.
13
+ */
14
+ declare const DEFAULT_MAX_DER_DEPTH = 64;
15
+ /** Concatenates multiple byte arrays into a single {@linkcode Uint8Array}. */
16
+ declare function concatBytes(parts: readonly Uint8Array[]): Uint8Array;
17
+ /**
18
+ * Builds a complete DER TLV (tag-length-value) element:
19
+ *
20
+ * - one tag octet,
21
+ * - the DER-encoded length, then
22
+ * - the raw value bytes.
23
+ */
24
+ declare function tlv(tag: number, value: Uint8Array): Uint8Array;
25
+ /** Wraps concatenated children in a SEQUENCE (tag `0x30`). */
26
+ declare function sequence(parts: readonly Uint8Array[]): Uint8Array;
27
+ /**
28
+ * Wraps children in a SET (tag `0x31`) after DER-sorting them lexicographically by encoded bytes,
29
+ * as required by {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690} DER.
30
+ */
31
+ declare function setOf(parts: readonly Uint8Array[]): Uint8Array;
32
+ /**
33
+ * Wraps a value in an explicit context-specific constructed tag (`0xa0 + tag`).
34
+ *
35
+ * Used for optional SEQUENCE fields tagged with `[tag] EXPLICIT`.
36
+ */
37
+ declare function explicitContext(tag: number, value: Uint8Array): Uint8Array;
38
+ /**
39
+ * Wraps a value in an implicit context-specific constructed tag (`0xa0 + tag`).
40
+ *
41
+ * Used for `[tag] IMPLICIT` fields whose underlying type is constructed (e.g. SEQUENCE).
42
+ */
43
+ declare function implicitConstructedContext(tag: number, value: Uint8Array): Uint8Array;
44
+ /**
45
+ * Wraps a value in an implicit context-specific primitive tag (`0x80 + tag`).
46
+ *
47
+ * Used for `[tag] IMPLICIT` fields whose underlying type is primitive (e.g. OCTET STRING).
48
+ */
49
+ declare function implicitPrimitiveContext(tag: number, value: Uint8Array): Uint8Array;
50
+ /**
51
+ * Encodes raw big-endian bytes as a DER INTEGER (tag `0x02`).
52
+ *
53
+ * Strips leading zero bytes for minimal encoding and prepends a zero byte when the high bit is set
54
+ * to keep the value non-negative.
55
+ */
56
+ declare function integer(bytes: Uint8Array): Uint8Array;
57
+ /**
58
+ * Encodes a non-negative JavaScript `number` as a DER INTEGER.
59
+ *
60
+ * @throws if the value is not a non-negative safe integer.
61
+ */
62
+ declare function integerFromNumber(value: number): Uint8Array;
63
+ /** Encodes a DER BOOLEAN (tag `0x01`): `true` → `0xff`, `false` → `0x00`. */
64
+ declare function bool(value: boolean): Uint8Array;
65
+ /** Produces a DER NULL element (tag `0x05`, zero-length value). */
66
+ declare function nullValue(): Uint8Array;
67
+ /** Wraps raw bytes in an OCTET STRING element (tag `0x04`). */
68
+ declare function octetString(value: Uint8Array): Uint8Array;
69
+ /**
70
+ * Encodes a DER BIT STRING (tag `0x03`).
71
+ *
72
+ * The value is prefixed with a single octet indicating how many trailing bits in the last byte are unused.
73
+ *
74
+ * @param unusedBits Number of unused trailing bits (0–7). Defaults to 0.
75
+ */
76
+ declare function bitString(value: Uint8Array, unusedBits?: number): Uint8Array;
77
+ /** Encodes a DER UTF8String (tag `0x0c`). */
78
+ declare function utf8String(value: string): Uint8Array;
79
+ /**
80
+ * Encodes a DER PrintableString (tag `0x13`).
81
+ *
82
+ * @throws if the input contains characters outside the X.520 PrintableString set.
83
+ */
84
+ declare function printableString(value: string): Uint8Array;
85
+ /**
86
+ * Encodes a DER IA5String (tag `0x16`).
87
+ *
88
+ * @throws if the input contains any non-ASCII character (code point > 0x7f).
89
+ */
90
+ declare function ia5String(value: string): Uint8Array;
91
+ /**
92
+ * Encodes a DER BMPString (tag `0x1e`) as big-endian UTF-16.
93
+ *
94
+ * @throws on lone surrogates and on code points above the Basic Multilingual Plane.
95
+ */
96
+ declare function bmpString(value: string): Uint8Array;
97
+ /**
98
+ * Encodes a DER UniversalString (tag `0x1c`) as big-endian UTF-32.
99
+ *
100
+ * @throws on lone surrogates.
101
+ */
102
+ declare function universalString(value: string): Uint8Array;
103
+ /**
104
+ * Encodes a dotted-decimal OID string as a DER OBJECT IDENTIFIER (tag `0x06`).
105
+ *
106
+ * Validates arc constraints per X.660: first arc must be 0–2, second < 40 for arcs 0 and 1.\
107
+ * Sub-identifiers are encoded with base-128 continuation.
108
+ */
109
+ declare function objectIdentifier(oid: string): Uint8Array;
110
+ /**
111
+ * Encodes a `Date` as a DER UTCTime (tag `0x17`), format `YYMMDDHHMMSSZ`.
112
+ *
113
+ * Only the two-digit year is stored; suitable for dates in 1950–2049.
114
+ */
115
+ declare function utcTime(date: Date): Uint8Array;
116
+ /**
117
+ * Encodes a {@linkcode Date} as a DER GeneralizedTime (tag `0x18`), format `YYYYMMDDHHMMSSZ`.
118
+ *
119
+ * Uses a four-digit year; required for dates outside the 1950–2049 range.
120
+ */
121
+ declare function generalizedTime(date: Date): Uint8Array;
122
+ /**
123
+ * Encodes a {@linkcode Date} as the appropriate DER time type per RFC 5280.
124
+ *
125
+ * - {@linkcode utcTime} for 1950–2049
126
+ * - {@linkcode generalizedTime} otherwise
127
+ */
128
+ declare function time(date: Date): Uint8Array;
129
+ /** A single parsed ASN.1 TLV element with byte-range metadata. */
130
+ interface DerElement {
131
+ /** ASN.1 tag byte (e.g. `0x30` for SEQUENCE, `0x02` for INTEGER). */
132
+ readonly tag: number;
133
+ /** Number of bytes occupied by the tag + length octets. */
134
+ readonly headerLength: number;
135
+ /** Byte length of the value portion (excluding tag and length octets). */
136
+ readonly length: number;
137
+ /** Byte offset where the value portion begins in the source buffer. */
138
+ readonly start: number;
139
+ /** Byte offset one past the last value byte. Equals the next element's header offset. */
140
+ readonly end: number;
141
+ /** The raw value bytes (slice of the source buffer). */
142
+ readonly value: Uint8Array;
143
+ }
144
+ /** Options for {@linkcode readSequenceChildren}. */
145
+ interface ReadSequenceChildrenOptions {
146
+ /** Maximum nesting depth for the DER depth check. @default {@linkcode DEFAULT_MAX_DER_DEPTH}. */
147
+ readonly maxDepth?: number;
148
+ /** Constructed tags whose inner bytes may not parse as valid TLV children (e.g. opaque extension values). */
149
+ readonly allowOpaqueConstructedTags?: readonly number[];
150
+ }
151
+ /** Options for {@linkcode readRootElement}. */
152
+ interface ReadRootElementOptions {
153
+ /** Maximum nesting depth for the DER depth check. @default {@linkcode DEFAULT_MAX_DER_DEPTH}. */
154
+ readonly maxDepth?: number;
155
+ /** Constructed tags whose inner bytes may not parse as valid TLV children (e.g. opaque extension values). */
156
+ readonly allowOpaqueConstructedTags?: readonly number[];
157
+ }
158
+ /**
159
+ * Walks the full DER tree rooted in {@linkcode bytes}.
160
+ *
161
+ * Constructed tags with content that cannot be parsed as valid children are tolerated when listed in {@linkcode options | allowOpaqueConstructedTags}.
162
+ *
163
+ * @throws if nesting exceeds {@linkcode maxDepth}.
164
+ */
165
+ declare function assertDerMaxDepth(bytes: Uint8Array, maxDepth?: number, options?: {
166
+ /** Constructed tags whose inner bytes may not parse as valid TLV children. */
167
+ readonly allowOpaqueConstructedTags?: readonly number[];
168
+ }): void;
169
+ //#endregion
170
+ export { DEFAULT_MAX_DER_DEPTH, DerElement, ReadRootElementOptions, ReadSequenceChildrenOptions, assertDerMaxDepth, bitString, bmpString, bool, concatBytes, explicitContext, generalizedTime, ia5String, implicitConstructedContext, implicitPrimitiveContext, integer, integerFromNumber, nullValue, objectIdentifier, octetString, printableString, sequence, setOf, time, tlv, universalString, utcTime, utf8String };
171
+ //# sourceMappingURL=der.d.ts.map
@@ -1,22 +1,40 @@
1
+ //#region src/internal/asn1/der.ts
2
+ /**
3
+ * Low-level DER encoding and reading helpers shared across the library.
4
+ *
5
+ * These utilities build and traverse ASN.1 TLV (tag-length-value) structures.
6
+ *
7
+ * @module
8
+ */
9
+ /**
10
+ * Maximum nesting depth allowed when recursively walking a DER structure.
11
+ *
12
+ * Guards against stack exhaustion from pathologically nested input.
13
+ */
14
+ const e = 64;
1
15
  /**
2
16
  * Produces the DER length octets for a given byte count.
3
- * Values < 128 use the short form (one octet); larger values use the
4
- * long form (leading octet encodes the number of subsequent length bytes).
17
+ *
18
+ * Values < 128 use the short form (one octet);
19
+ * larger values use the long form (leading octet encodes the number of subsequent length bytes).
5
20
  */
6
21
  function encodeLength(e) {
7
22
  if (assertNonNegativeSafeInteger(e, "DER length"), e < 128) return Uint8Array.of(e);
8
23
  let t = encodeBase256(e);
9
24
  return Uint8Array.of(128 | t.length, ...t);
10
25
  }
11
- /** Concatenates multiple byte arrays into a single `Uint8Array`. */
26
+ /** Concatenates multiple byte arrays into a single {@linkcode Uint8Array}. */
12
27
  function concatBytes(e) {
13
28
  let t = e.reduce((e, t) => e + t.length, 0), n = new Uint8Array(t), r = 0;
14
29
  for (let t of e) n.set(t, r), r += t.length;
15
30
  return n;
16
31
  }
17
32
  /**
18
- * Builds a complete DER TLV (tag-length-value) element: one tag octet,
19
- * the DER-encoded length, then the raw value bytes.
33
+ * Builds a complete DER TLV (tag-length-value) element:
34
+ *
35
+ * - one tag octet,
36
+ * - the DER-encoded length, then
37
+ * - the raw value bytes.
20
38
  */
21
39
  function tlv(e, t) {
22
40
  return assertSingleOctetDerTag(e), concatBytes([
@@ -30,8 +48,8 @@ function sequence(e) {
30
48
  return tlv(48, concatBytes(e));
31
49
  }
32
50
  /**
33
- * Wraps children in a SET (tag `0x31`) after DER-sorting them
34
- * lexicographically by encoded bytes, as required by X.690 DER.
51
+ * Wraps children in a SET (tag `0x31`) after DER-sorting them lexicographically by encoded bytes,
52
+ * as required by {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690} DER.
35
53
  */
36
54
  function setOf(e) {
37
55
  return tlv(49, concatBytes(e.slice().sort((e, t) => {
@@ -45,6 +63,7 @@ function setOf(e) {
45
63
  }
46
64
  /**
47
65
  * Wraps a value in an explicit context-specific constructed tag (`0xa0 + tag`).
66
+ *
48
67
  * Used for optional SEQUENCE fields tagged with `[tag] EXPLICIT`.
49
68
  */
50
69
  function explicitContext(e, t) {
@@ -52,6 +71,7 @@ function explicitContext(e, t) {
52
71
  }
53
72
  /**
54
73
  * Wraps a value in an implicit context-specific constructed tag (`0xa0 + tag`).
74
+ *
55
75
  * Used for `[tag] IMPLICIT` fields whose underlying type is constructed (e.g. SEQUENCE).
56
76
  */
57
77
  function implicitConstructedContext(e, t) {
@@ -59,6 +79,7 @@ function implicitConstructedContext(e, t) {
59
79
  }
60
80
  /**
61
81
  * Wraps a value in an implicit context-specific primitive tag (`0x80 + tag`).
82
+ *
62
83
  * Used for `[tag] IMPLICIT` fields whose underlying type is primitive (e.g. OCTET STRING).
63
84
  */
64
85
  function implicitPrimitiveContext(e, t) {
@@ -66,8 +87,9 @@ function implicitPrimitiveContext(e, t) {
66
87
  }
67
88
  /**
68
89
  * Encodes raw big-endian bytes as a DER INTEGER (tag `0x02`).
69
- * Strips leading zero bytes for minimal encoding and prepends a zero
70
- * byte when the high bit is set to keep the value non-negative.
90
+ *
91
+ * Strips leading zero bytes for minimal encoding and prepends a zero byte when the high bit is set
92
+ * to keep the value non-negative.
71
93
  */
72
94
  function integer(e) {
73
95
  if (e.length === 0) return tlv(2, Uint8Array.of(0));
@@ -78,7 +100,8 @@ function integer(e) {
78
100
  }
79
101
  /**
80
102
  * Encodes a non-negative JavaScript `number` as a DER INTEGER.
81
- * Throws if the value is not a non-negative safe integer.
103
+ *
104
+ * @throws if the value is not a non-negative safe integer.
82
105
  */
83
106
  function integerFromNumber(e) {
84
107
  if (!Number.isSafeInteger(e) || e < 0) throw Error("INTEGER must be a non-negative safe integer");
@@ -97,8 +120,9 @@ function octetString(e) {
97
120
  return tlv(4, e);
98
121
  }
99
122
  /**
100
- * Encodes a DER BIT STRING (tag `0x03`). The value is prefixed with a
101
- * single octet indicating how many trailing bits in the last byte are unused.
123
+ * Encodes a DER BIT STRING (tag `0x03`).
124
+ *
125
+ * The value is prefixed with a single octet indicating how many trailing bits in the last byte are unused.
102
126
  *
103
127
  * @param unusedBits Number of unused trailing bits (0–7). Defaults to 0.
104
128
  */
@@ -114,7 +138,8 @@ function utf8String(e) {
114
138
  }
115
139
  /**
116
140
  * Encodes a DER PrintableString (tag `0x13`).
117
- * Throws if the input contains characters outside the X.520 PrintableString set.
141
+ *
142
+ * @throws if the input contains characters outside the X.520 PrintableString set.
118
143
  */
119
144
  function printableString(e) {
120
145
  if (!/^[A-Za-z0-9 '()+,\-./:=?]*$/.test(e)) throw Error("Invalid PrintableString: contains characters outside the allowed set");
@@ -122,13 +147,43 @@ function printableString(e) {
122
147
  }
123
148
  /**
124
149
  * Encodes a DER IA5String (tag `0x16`).
125
- * Throws if the input contains any non-ASCII character (code point > 0x7f).
150
+ *
151
+ * @throws if the input contains any non-ASCII character (code point > 0x7f).
126
152
  */
127
153
  function ia5String(e) {
128
154
  for (let t = 0; t < e.length; t++) if (e.charCodeAt(t) > 127) throw Error("Invalid IA5String: contains non-ASCII characters");
129
155
  return tlv(22, new TextEncoder().encode(e));
130
156
  }
131
- /** Encode a non-negative integer as a base-128 sub-identifier (X.690 §8.19.2). */
157
+ /**
158
+ * Encodes a DER BMPString (tag `0x1e`) as big-endian UTF-16.
159
+ *
160
+ * @throws on lone surrogates and on code points above the Basic Multilingual Plane.
161
+ */
162
+ function bmpString(e) {
163
+ let t = [];
164
+ for (let n of e) {
165
+ let e = n.codePointAt(0) ?? 0;
166
+ if (e > 65535) throw Error("Invalid BMPString: code point above the Basic Multilingual Plane");
167
+ if (e >= 55296 && e <= 57343) throw Error("Invalid BMPString: lone surrogate");
168
+ t.push(e >> 8 & 255, e & 255);
169
+ }
170
+ return tlv(30, Uint8Array.from(t));
171
+ }
172
+ /**
173
+ * Encodes a DER UniversalString (tag `0x1c`) as big-endian UTF-32.
174
+ *
175
+ * @throws on lone surrogates.
176
+ */
177
+ function universalString(e) {
178
+ let t = [];
179
+ for (let n of e) {
180
+ let e = n.codePointAt(0) ?? 0;
181
+ if (e >= 55296 && e <= 57343) throw Error("Invalid UniversalString: lone surrogate");
182
+ t.push(e >> 24 & 255, e >> 16 & 255, e >> 8 & 255, e & 255);
183
+ }
184
+ return tlv(28, Uint8Array.from(t));
185
+ }
186
+ /** Encode a non-negative integer as a base-128 sub-identifier ({@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §8.19.2}). */
132
187
  function encodeBase128(e) {
133
188
  let t = [Number(e & 127n)], n = e >> 7n;
134
189
  for (; n > 0n;) t.unshift(128 | Number(n & 127n)), n >>= 7n;
@@ -136,8 +191,9 @@ function encodeBase128(e) {
136
191
  }
137
192
  /**
138
193
  * Encodes a dotted-decimal OID string as a DER OBJECT IDENTIFIER (tag `0x06`).
139
- * Validates arc constraints per X.660: first arc must be 0–2, second < 40
140
- * for arcs 0 and 1. Sub-identifiers are encoded with base-128 continuation.
194
+ *
195
+ * Validates arc constraints per X.660: first arc must be 0–2, second < 40 for arcs 0 and 1.\
196
+ * Sub-identifiers are encoded with base-128 continuation.
141
197
  */
142
198
  function objectIdentifier(e) {
143
199
  let t = /^\d+$/, n = e.split(".").map((e) => {
@@ -155,6 +211,7 @@ function objectIdentifier(e) {
155
211
  }
156
212
  /**
157
213
  * Encodes a `Date` as a DER UTCTime (tag `0x17`), format `YYMMDDHHMMSSZ`.
214
+ *
158
215
  * Only the two-digit year is stored; suitable for dates in 1950–2049.
159
216
  */
160
217
  function utcTime(e) {
@@ -172,7 +229,8 @@ function utcTime(e) {
172
229
  return tlv(23, new TextEncoder().encode(n));
173
230
  }
174
231
  /**
175
- * Encodes a `Date` as a DER GeneralizedTime (tag `0x18`), format `YYYYMMDDHHMMSSZ`.
232
+ * Encodes a {@linkcode Date} as a DER GeneralizedTime (tag `0x18`), format `YYYYMMDDHHMMSSZ`.
233
+ *
176
234
  * Uses a four-digit year; required for dates outside the 1950–2049 range.
177
235
  */
178
236
  function generalizedTime(e) {
@@ -190,8 +248,10 @@ function generalizedTime(e) {
190
248
  return tlv(24, new TextEncoder().encode(n));
191
249
  }
192
250
  /**
193
- * Encodes a `Date` as the appropriate DER time type per RFC 5280:
194
- * {@linkcode utcTime} for 1950–2049, {@linkcode generalizedTime} otherwise.
251
+ * Encodes a {@linkcode Date} as the appropriate DER time type per RFC 5280.
252
+ *
253
+ * - {@linkcode utcTime} for 1950–2049
254
+ * - {@linkcode generalizedTime} otherwise
195
255
  */
196
256
  function time(e) {
197
257
  return e.getUTCFullYear() >= 2050 || e.getUTCFullYear() < 1950 ? generalizedTime(e) : utcTime(e);
@@ -208,11 +268,11 @@ function encodeBase256(e) {
208
268
  return t;
209
269
  }
210
270
  /**
211
- * Reads one TLV element from `bytes` starting at `offset`.
212
- * Parses the tag byte, decodes the DER length octets, and slices out the
213
- * value bytes. Throws on truncated input, indefinite lengths, and
214
- * non-minimal length encodings.
271
+ * Reads one TLV element from {@linkcode bytes} starting at {@linkcode offset}.
215
272
  *
273
+ * Parses the tag byte, decodes the DER length octets, and slices out the value bytes.
274
+ *
275
+ * @throws on truncated input, indefinite lengths, and non-minimal length encodings.
216
276
  * @param offset Byte position of the tag octet. Defaults to 0.
217
277
  */
218
278
  function readElement(e, t = 0) {
@@ -261,9 +321,11 @@ function readLongFormDerLength(e, t, n) {
261
321
  return r;
262
322
  }
263
323
  /**
264
- * Walks the full DER tree rooted in `bytes` and throws if nesting exceeds
265
- * `maxDepth`. Constructed tags with content that cannot be parsed as valid
266
- * children are tolerated when listed in `allowOpaqueConstructedTags`.
324
+ * Walks the full DER tree rooted in {@linkcode bytes}.
325
+ *
326
+ * Constructed tags with content that cannot be parsed as valid children are tolerated when listed in {@linkcode options | allowOpaqueConstructedTags}.
327
+ *
328
+ * @throws if nesting exceeds {@linkcode maxDepth}.
267
329
  */
268
330
  function assertDerMaxDepth(e, t = 64, n) {
269
331
  if (!Number.isSafeInteger(t) || t < 1) throw Error("DER max depth must be a positive safe integer");
@@ -308,8 +370,11 @@ function readDerChildOrOpaque(e, t, n, r) {
308
370
  if (!canTreatAsOpaqueLeaf(t, n, r)) throw Error("DER child exceeds parent length");
309
371
  }
310
372
  /**
311
- * Reads the single top-level TLV element from `bytes`, optionally
312
- * validating nesting depth. Throws if there is trailing data after the element.
373
+ * Reads the single top-level TLV element from {@linkcode bytes}.
374
+ *
375
+ * Optionally validates nesting depth.
376
+ *
377
+ * @throws if there is trailing data after the element.
313
378
  */
314
379
  function readRootElement(e, t) {
315
380
  assertDerMaxDepth(e, t?.maxDepth ?? 64, t);
@@ -318,8 +383,9 @@ function readRootElement(e, t) {
318
383
  return n;
319
384
  }
320
385
  /**
321
- * Reads a DER-encoded SEQUENCE from `bytes` and returns its direct children.
322
- * Throws if the root element is not a SEQUENCE or if child boundaries are inconsistent.
386
+ * Reads a DER-encoded SEQUENCE from {@linkcode bytes} and returns its direct children.
387
+ *
388
+ * @throws if the root element is not a SEQUENCE or if child boundaries are inconsistent.
323
389
  */
324
390
  function readSequenceChildren(e, t) {
325
391
  let n = readRootElement(e, t);
@@ -333,7 +399,7 @@ function readSequenceChildren(e, t) {
333
399
  if (i !== n.end) throw Error("Malformed DER sequence");
334
400
  return r;
335
401
  }
336
- /** Throws if `value` is not a non-negative safe integer. */
402
+ /** @throws if {@linkcode value} is not a non-negative safe integer. */
337
403
  function assertNonNegativeSafeInteger(e, t) {
338
404
  if (!Number.isSafeInteger(e) || e < 0) throw Error(`${t} must be a non-negative safe integer`);
339
405
  }
@@ -348,14 +414,13 @@ function assertValidDate(e, t) {
348
414
  if (Number.isNaN(e.getTime())) throw RangeError(`${t} requires a valid Date`);
349
415
  }
350
416
  /**
351
- * Returns `true` when a constructed element's content should be treated as
352
- * an opaque leaf (not recursed into) because its tag appears in the
353
- * `allowOpaqueConstructedTags` list and the offset is at the element start.
417
+ * @returns `true` when a constructed element's content should be treated as an opaque leaf (not recursed into)
418
+ * because its tag appears in the {@linkcode options | allowOpaqueConstructedTags} list and the offset is at the element start.
354
419
  */
355
420
  function canTreatAsOpaqueLeaf(e, t, n) {
356
421
  return t === e.start && n?.allowOpaqueConstructedTags?.includes(e.tag) === !0;
357
422
  }
358
423
  //#endregion
359
- export { assertDerMaxDepth, bitString, bool, concatBytes, encodeLength, explicitContext, generalizedTime, ia5String, implicitConstructedContext, implicitPrimitiveContext, integer, integerFromNumber, nullValue, objectIdentifier, octetString, printableString, readElement, readRootElement, readSequenceChildren, sequence, setOf, time, tlv, utcTime, utf8String };
424
+ export { e as DEFAULT_MAX_DER_DEPTH, assertDerMaxDepth, bitString, bmpString, bool, concatBytes, encodeLength, explicitContext, generalizedTime, ia5String, implicitConstructedContext, implicitPrimitiveContext, integer, integerFromNumber, nullValue, objectIdentifier, octetString, printableString, readElement, readRootElement, readSequenceChildren, sequence, setOf, time, tlv, universalString, utcTime, utf8String };
360
425
 
361
426
  //# sourceMappingURL=der.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"der.js","names":[],"sources":["../../../src/internal/asn1/der.ts"],"sourcesContent":["/**\n * Low-level DER encoding and reading helpers shared across the library.\n *\n * These utilities build and traverse ASN.1 TLV (tag-length-value) structures\n * without pulling in external dependencies.\n *\n * @module\n */\n\n/**\n * Maximum nesting depth allowed when recursively walking a DER structure.\n * Guards against stack exhaustion from pathologically nested input.\n */\nexport const DEFAULT_MAX_DER_DEPTH = 64;\n\n/**\n * Produces the DER length octets for a given byte count.\n * Values < 128 use the short form (one octet); larger values use the\n * long form (leading octet encodes the number of subsequent length bytes).\n */\nexport function encodeLength(length: number): Uint8Array {\n\tassertNonNegativeSafeInteger(length, 'DER length');\n\tif (length < 128) {\n\t\treturn Uint8Array.of(length);\n\t}\n\n\tconst parts = encodeBase256(length);\n\treturn Uint8Array.of(0x80 | parts.length, ...parts);\n}\n\n/** Concatenates multiple byte arrays into a single `Uint8Array`. */\nexport function concatBytes(parts: readonly Uint8Array[]): Uint8Array {\n\tconst length = parts.reduce((sum, part) => sum + part.length, 0);\n\tconst out = new Uint8Array(length);\n\tlet offset = 0;\n\tfor (const part of parts) {\n\t\tout.set(part, offset);\n\t\toffset += part.length;\n\t}\n\treturn out;\n}\n\n/**\n * Builds a complete DER TLV (tag-length-value) element: one tag octet,\n * the DER-encoded length, then the raw value bytes.\n */\nexport function tlv(tag: number, value: Uint8Array): Uint8Array {\n\tassertSingleOctetDerTag(tag);\n\treturn concatBytes([Uint8Array.of(tag), encodeLength(value.length), value]);\n}\n\n/** Wraps concatenated children in a SEQUENCE (tag `0x30`). */\nexport function sequence(parts: readonly Uint8Array[]): Uint8Array {\n\treturn tlv(0x30, concatBytes(parts));\n}\n\n/**\n * Wraps children in a SET (tag `0x31`) after DER-sorting them\n * lexicographically by encoded bytes, as required by X.690 DER.\n */\nexport function setOf(parts: readonly Uint8Array[]): Uint8Array {\n\tconst sorted = parts.slice().sort((a, b) => {\n\t\tconst len = Math.min(a.length, b.length);\n\t\tfor (let i = 0; i < len; i++) {\n\t\t\tconst diff = (a[i] ?? 0) - (b[i] ?? 0);\n\t\t\tif (diff !== 0) return diff;\n\t\t}\n\t\treturn a.length - b.length;\n\t});\n\treturn tlv(0x31, concatBytes(sorted));\n}\n\n/**\n * Wraps a value in an explicit context-specific constructed tag (`0xa0 + tag`).\n * Used for optional SEQUENCE fields tagged with `[tag] EXPLICIT`.\n */\nexport function explicitContext(tag: number, value: Uint8Array): Uint8Array {\n\tassertContextSpecificTagNumber(tag);\n\treturn tlv(0xa0 + tag, value);\n}\n\n/**\n * Wraps a value in an implicit context-specific constructed tag (`0xa0 + tag`).\n * Used for `[tag] IMPLICIT` fields whose underlying type is constructed (e.g. SEQUENCE).\n */\nexport function implicitConstructedContext(tag: number, value: Uint8Array): Uint8Array {\n\tassertContextSpecificTagNumber(tag);\n\treturn tlv(0xa0 + tag, value);\n}\n\n/**\n * Wraps a value in an implicit context-specific primitive tag (`0x80 + tag`).\n * Used for `[tag] IMPLICIT` fields whose underlying type is primitive (e.g. OCTET STRING).\n */\nexport function implicitPrimitiveContext(tag: number, value: Uint8Array): Uint8Array {\n\tassertContextSpecificTagNumber(tag);\n\treturn tlv(0x80 + tag, value);\n}\n\n/**\n * Encodes raw big-endian bytes as a DER INTEGER (tag `0x02`).\n * Strips leading zero bytes for minimal encoding and prepends a zero\n * byte when the high bit is set to keep the value non-negative.\n */\nexport function integer(bytes: Uint8Array): Uint8Array {\n\tif (bytes.length === 0) {\n\t\treturn tlv(0x02, Uint8Array.of(0));\n\t}\n\n\tlet start = 0;\n\twhile (start < bytes.length - 1 && bytes[start] === 0) {\n\t\tstart += 1;\n\t}\n\n\tconst value = bytes.slice(start);\n\tif ((value[0] ?? 0) >= 0x80) {\n\t\treturn tlv(0x02, concatBytes([Uint8Array.of(0), value]));\n\t}\n\n\treturn tlv(0x02, value);\n}\n\n/**\n * Encodes a non-negative JavaScript `number` as a DER INTEGER.\n * Throws if the value is not a non-negative safe integer.\n */\nexport function integerFromNumber(value: number): Uint8Array {\n\tif (!Number.isSafeInteger(value) || value < 0) {\n\t\tthrow new Error('INTEGER must be a non-negative safe integer');\n\t}\n\n\tif (value === 0) {\n\t\treturn integer(Uint8Array.of(0));\n\t}\n\n\treturn integer(Uint8Array.from(encodeBase256(value)));\n}\n\n/** Encodes a DER BOOLEAN (tag `0x01`): `true` → `0xff`, `false` → `0x00`. */\nexport function bool(value: boolean): Uint8Array {\n\treturn tlv(0x01, Uint8Array.of(value ? 0xff : 0x00));\n}\n\n/** Produces a DER NULL element (tag `0x05`, zero-length value). */\nexport function nullValue(): Uint8Array {\n\treturn tlv(0x05, new Uint8Array());\n}\n\n/** Wraps raw bytes in an OCTET STRING element (tag `0x04`). */\nexport function octetString(value: Uint8Array): Uint8Array {\n\treturn tlv(0x04, value);\n}\n\n/**\n * Encodes a DER BIT STRING (tag `0x03`). The value is prefixed with a\n * single octet indicating how many trailing bits in the last byte are unused.\n *\n * @param unusedBits Number of unused trailing bits (0–7). Defaults to 0.\n */\nexport function bitString(value: Uint8Array, unusedBits = 0): Uint8Array {\n\tif (unusedBits < 0 || unusedBits > 7) {\n\t\tthrow new Error('unusedBits must be between 0 and 7');\n\t}\n\tif (value.length === 0 && unusedBits !== 0) {\n\t\tthrow new Error('unusedBits must be 0 when value is empty');\n\t}\n\tif (value.length > 0 && unusedBits > 0) {\n\t\tconst lastByte = value[value.length - 1] ?? 0;\n\t\tif ((lastByte & ((1 << unusedBits) - 1)) !== 0) {\n\t\t\tthrow new Error('unused bits in the last byte must be zero');\n\t\t}\n\t}\n\treturn tlv(0x03, concatBytes([Uint8Array.of(unusedBits), value]));\n}\n\n/** Encodes a DER UTF8String (tag `0x0c`). */\nexport function utf8String(value: string): Uint8Array {\n\treturn tlv(0x0c, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a DER PrintableString (tag `0x13`).\n * Throws if the input contains characters outside the X.520 PrintableString set.\n */\nexport function printableString(value: string): Uint8Array {\n\tif (!/^[A-Za-z0-9 '()+,\\-./:=?]*$/.test(value)) {\n\t\tthrow new Error('Invalid PrintableString: contains characters outside the allowed set');\n\t}\n\treturn tlv(0x13, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a DER IA5String (tag `0x16`).\n * Throws if the input contains any non-ASCII character (code point > 0x7f).\n */\nexport function ia5String(value: string): Uint8Array {\n\tfor (let i = 0; i < value.length; i++) {\n\t\tif (value.charCodeAt(i) > 0x7f) {\n\t\t\tthrow new Error('Invalid IA5String: contains non-ASCII characters');\n\t\t}\n\t}\n\treturn tlv(0x16, new TextEncoder().encode(value));\n}\n\n/** Encode a non-negative integer as a base-128 sub-identifier (X.690 §8.19.2). */\nfunction encodeBase128(value: bigint): number[] {\n\tconst encoded: number[] = [Number(value & 0x7fn)];\n\tlet current = value >> 7n;\n\twhile (current > 0n) {\n\t\tencoded.unshift(0x80 | Number(current & 0x7fn));\n\t\tcurrent >>= 7n;\n\t}\n\treturn encoded;\n}\n\n/**\n * Encodes a dotted-decimal OID string as a DER OBJECT IDENTIFIER (tag `0x06`).\n * Validates arc constraints per X.660: first arc must be 0–2, second < 40\n * for arcs 0 and 1. Sub-identifiers are encoded with base-128 continuation.\n */\nexport function objectIdentifier(oid: string): Uint8Array {\n\tconst digitPattern = /^\\d+$/;\n\tconst segments = oid.split('.').map((segment) => {\n\t\tif (!digitPattern.test(segment)) {\n\t\t\tthrow new Error(`Invalid OID segment: ${segment}`);\n\t\t}\n\t\treturn BigInt(segment);\n\t});\n\tif (segments.length < 2) {\n\t\tthrow new Error(`Invalid OID: ${oid}`);\n\t}\n\n\tconst [first, second, ...rest] = segments;\n\tif (first === undefined || second === undefined) {\n\t\tthrow new Error(`Invalid OID: ${oid}`);\n\t}\n\tif (first !== 0n && first !== 1n && first !== 2n) {\n\t\tthrow new Error(`Invalid OID first arc: ${first}`);\n\t}\n\tif ((first === 0n || first === 1n) && second >= 40n) {\n\t\tthrow new Error(`Invalid OID second arc: ${second} (must be < 40 when first arc is ${first})`);\n\t}\n\tconst bytes: number[] = encodeBase128(first * 40n + second);\n\tfor (const segment of rest) {\n\t\tbytes.push(...encodeBase128(segment));\n\t}\n\n\treturn tlv(0x06, Uint8Array.from(bytes));\n}\n\n/**\n * Encodes a `Date` as a DER UTCTime (tag `0x17`), format `YYMMDDHHMMSSZ`.\n * Only the two-digit year is stored; suitable for dates in 1950–2049.\n */\nexport function utcTime(date: Date): Uint8Array {\n\tassertValidDate(date, 'UTCTime');\n\tconst year = date.getUTCFullYear();\n\tif (year < 1950 || year > 2049) {\n\t\tthrow new RangeError('UTCTime year must be between 1950 and 2049');\n\t}\n\tconst value = `${[\n\t\ttwoDigits(year % 100),\n\t\ttwoDigits(date.getUTCMonth() + 1),\n\t\ttwoDigits(date.getUTCDate()),\n\t\ttwoDigits(date.getUTCHours()),\n\t\ttwoDigits(date.getUTCMinutes()),\n\t\ttwoDigits(date.getUTCSeconds()),\n\t].join('')}Z`;\n\treturn tlv(0x17, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a `Date` as a DER GeneralizedTime (tag `0x18`), format `YYYYMMDDHHMMSSZ`.\n * Uses a four-digit year; required for dates outside the 1950–2049 range.\n */\nexport function generalizedTime(date: Date): Uint8Array {\n\tassertValidDate(date, 'GeneralizedTime');\n\tconst year = date.getUTCFullYear();\n\tif (year < 0 || year > 9999) {\n\t\tthrow new RangeError('GeneralizedTime year must be between 0 and 9999');\n\t}\n\tconst value = `${[\n\t\tString(year).padStart(4, '0'),\n\t\ttwoDigits(date.getUTCMonth() + 1),\n\t\ttwoDigits(date.getUTCDate()),\n\t\ttwoDigits(date.getUTCHours()),\n\t\ttwoDigits(date.getUTCMinutes()),\n\t\ttwoDigits(date.getUTCSeconds()),\n\t].join('')}Z`;\n\treturn tlv(0x18, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a `Date` as the appropriate DER time type per RFC 5280:\n * {@linkcode utcTime} for 1950–2049, {@linkcode generalizedTime} otherwise.\n */\nexport function time(date: Date): Uint8Array {\n\tif (date.getUTCFullYear() >= 2050 || date.getUTCFullYear() < 1950) {\n\t\treturn generalizedTime(date);\n\t}\n\treturn utcTime(date);\n}\n\n/** Zero-pads a number to two digits for time encoding. */\nfunction twoDigits(value: number): string {\n\treturn String(value).padStart(2, '0');\n}\n\n/** Encodes a non-negative integer as big-endian base-256 octets. */\nfunction encodeBase256(value: number): readonly number[] {\n\tassertNonNegativeSafeInteger(value, 'DER integer');\n\tconst parts: number[] = [];\n\tlet current = value;\n\twhile (current > 0) {\n\t\tparts.unshift(current & 0xff);\n\t\tcurrent = Math.floor(current / 256);\n\t}\n\treturn parts;\n}\n\n/** A single parsed ASN.1 TLV element with byte-range metadata. */\nexport interface DerElement {\n\t/** ASN.1 tag byte (e.g. `0x30` for SEQUENCE, `0x02` for INTEGER). */\n\treadonly tag: number;\n\t/** Number of bytes occupied by the tag + length octets. */\n\treadonly headerLength: number;\n\t/** Byte length of the value portion (excluding tag and length octets). */\n\treadonly length: number;\n\t/** Byte offset where the value portion begins in the source buffer. */\n\treadonly start: number;\n\t/** Byte offset one past the last value byte — equals the next element's header offset. */\n\treadonly end: number;\n\t/** The raw value bytes (slice of the source buffer). */\n\treadonly value: Uint8Array;\n}\n\n/** Options for {@linkcode readSequenceChildren}. */\nexport interface ReadSequenceChildrenOptions {\n\t/** Maximum nesting depth for the DER depth check. Default: {@linkcode DEFAULT_MAX_DER_DEPTH}. */\n\treadonly maxDepth?: number;\n\t/** Constructed tags whose inner bytes may not parse as valid TLV children (e.g. opaque extension values). */\n\treadonly allowOpaqueConstructedTags?: readonly number[];\n}\n\n/** Options for {@linkcode readRootElement}. */\nexport interface ReadRootElementOptions {\n\t/** Maximum nesting depth for the DER depth check. Default: {@linkcode DEFAULT_MAX_DER_DEPTH}. */\n\treadonly maxDepth?: number;\n\t/** Constructed tags whose inner bytes may not parse as valid TLV children (e.g. opaque extension values). */\n\treadonly allowOpaqueConstructedTags?: readonly number[];\n}\n\n/**\n * Reads one TLV element from `bytes` starting at `offset`.\n * Parses the tag byte, decodes the DER length octets, and slices out the\n * value bytes. Throws on truncated input, indefinite lengths, and\n * non-minimal length encodings.\n *\n * @param offset Byte position of the tag octet. Defaults to 0.\n */\nexport function readElement(bytes: Uint8Array, offset = 0): DerElement {\n\tconst tag = bytes[offset];\n\tif (tag === undefined) {\n\t\tthrow new Error('Unexpected end of DER input');\n\t}\n\tif ((tag & 0x1f) === 0x1f) {\n\t\tthrow new Error('High-tag-number DER form is not supported');\n\t}\n\tconst lengthByte = bytes[offset + 1];\n\tif (lengthByte === undefined) {\n\t\tthrow new Error('Unexpected end of DER input');\n\t}\n\n\tconst { headerLength, length } = readDerLength(bytes, offset, lengthByte);\n\n\tconst start = offset + headerLength;\n\tconst end = start + length;\n\tif (end > bytes.length) {\n\t\tthrow new Error('DER element exceeds input length');\n\t}\n\n\treturn {\n\t\ttag,\n\t\theaderLength,\n\t\tlength,\n\t\tstart,\n\t\tend,\n\t\tvalue: bytes.slice(start, end),\n\t};\n}\n\n/** Decodes and validates the definite-length field of a DER element. */\nfunction readDerLength(\n\tbytes: Uint8Array,\n\toffset: number,\n\tlengthByte: number,\n): { readonly headerLength: number; readonly length: number } {\n\tif ((lengthByte & 0x80) === 0) {\n\t\treturn { headerLength: 2, length: lengthByte };\n\t}\n\tconst octets = lengthByte & 0x7f;\n\tif (octets === 0) {\n\t\tthrow new Error('Indefinite lengths are not supported');\n\t}\n\tconst firstLengthOctet = bytes[offset + 2];\n\tif (firstLengthOctet === undefined) {\n\t\tthrow new Error('Unexpected end of DER input');\n\t}\n\tif (firstLengthOctet === 0) {\n\t\tthrow new Error('Non-minimal DER length encoding');\n\t}\n\tconst length = readLongFormDerLength(bytes, offset, octets);\n\tif (length < 128) {\n\t\tthrow new Error('Non-minimal DER length encoding');\n\t}\n\treturn { headerLength: 2 + octets, length };\n}\n\nfunction readLongFormDerLength(bytes: Uint8Array, offset: number, octets: number): number {\n\tlet length = 0;\n\tfor (let index = 0; index < octets; index += 1) {\n\t\tconst next = bytes[offset + 2 + index];\n\t\tif (next === undefined) {\n\t\t\tthrow new Error('Unexpected end of DER input');\n\t\t}\n\t\tif (length > Math.floor((Number.MAX_SAFE_INTEGER - next) / 256)) {\n\t\t\tthrow new Error('DER length exceeds safe integer range');\n\t\t}\n\t\tlength = length * 256 + next;\n\t}\n\treturn length;\n}\n\n/**\n * Walks the full DER tree rooted in `bytes` and throws if nesting exceeds\n * `maxDepth`. Constructed tags with content that cannot be parsed as valid\n * children are tolerated when listed in `allowOpaqueConstructedTags`.\n */\nexport function assertDerMaxDepth(\n\tbytes: Uint8Array,\n\tmaxDepth: number = DEFAULT_MAX_DER_DEPTH,\n\toptions?: {\n\t\t/** Constructed tags whose inner bytes may not parse as valid TLV children. */\n\t\treadonly allowOpaqueConstructedTags?: readonly number[];\n\t},\n): void {\n\tif (!Number.isSafeInteger(maxDepth) || maxDepth < 1) {\n\t\tthrow new Error('DER max depth must be a positive safe integer');\n\t}\n\tconst root = readElement(bytes, 0);\n\tif (root.end !== bytes.length) {\n\t\tthrow new Error('Trailing data after DER element');\n\t}\n\tconst stack: {\n\t\treadonly element: DerElement;\n\t\treadonly depth: number;\n\t}[] = [{ element: root, depth: 1 }];\n\twhile (stack.length > 0) {\n\t\tconst current = stack.pop();\n\t\tif (current === undefined) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (current.depth > maxDepth) {\n\t\t\tthrow new Error(`DER exceeds max depth of ${maxDepth}`);\n\t\t}\n\t\tif ((current.element.tag & 0x20) === 0) {\n\t\t\tcontinue;\n\t\t}\n\t\tpushDerChildren(bytes, current, stack, options);\n\t}\n}\n\nfunction pushDerChildren(\n\tbytes: Uint8Array,\n\tcurrent: { readonly element: DerElement; readonly depth: number },\n\tstack: { readonly element: DerElement; readonly depth: number }[],\n\toptions?: {\n\t\treadonly allowOpaqueConstructedTags?: readonly number[];\n\t},\n): void {\n\tlet offset = current.element.start;\n\tlet treatedAsOpaqueLeaf = false;\n\twhile (offset < current.element.end) {\n\t\tconst child = readDerChildOrOpaque(bytes, current.element, offset, options);\n\t\tif (child === undefined) {\n\t\t\ttreatedAsOpaqueLeaf = true;\n\t\t\toffset = current.element.end;\n\t\t\tbreak;\n\t\t}\n\t\tstack.push({ element: child, depth: current.depth + 1 });\n\t\toffset = child.end;\n\t}\n\tif (!treatedAsOpaqueLeaf && offset !== current.element.end) {\n\t\tthrow new Error('Malformed DER container');\n\t}\n}\n\nfunction readDerChildOrOpaque(\n\tbytes: Uint8Array,\n\tparent: DerElement,\n\toffset: number,\n\toptions?: {\n\t\treadonly allowOpaqueConstructedTags?: readonly number[];\n\t},\n): DerElement | undefined {\n\tlet child: DerElement;\n\ttry {\n\t\tchild = readElement(bytes, offset);\n\t} catch (error) {\n\t\tif (canTreatAsOpaqueLeaf(parent, offset, options)) return undefined;\n\t\tthrow error;\n\t}\n\tif (child.end <= parent.end) {\n\t\treturn child;\n\t}\n\tif (canTreatAsOpaqueLeaf(parent, offset, options)) return undefined;\n\tthrow new Error('DER child exceeds parent length');\n}\n\n/**\n * Reads the single top-level TLV element from `bytes`, optionally\n * validating nesting depth. Throws if there is trailing data after the element.\n */\nexport function readRootElement(bytes: Uint8Array, options?: ReadRootElementOptions): DerElement {\n\tconst maxDepth = options?.maxDepth ?? DEFAULT_MAX_DER_DEPTH;\n\tassertDerMaxDepth(bytes, maxDepth, options);\n\tconst element = readElement(bytes, 0);\n\tif (element.end !== bytes.length) {\n\t\tthrow new Error('Trailing data after DER element');\n\t}\n\treturn element;\n}\n\n/**\n * Reads a DER-encoded SEQUENCE from `bytes` and returns its direct children.\n * Throws if the root element is not a SEQUENCE or if child boundaries are inconsistent.\n */\nexport function readSequenceChildren(\n\tbytes: Uint8Array,\n\toptions?: ReadSequenceChildrenOptions,\n): DerElement[] {\n\tconst sequenceElement = readRootElement(bytes, options);\n\tif (sequenceElement.tag !== 0x30) {\n\t\tthrow new Error('Expected SEQUENCE');\n\t}\n\n\tconst children: DerElement[] = [];\n\tlet offset = sequenceElement.start;\n\twhile (offset < sequenceElement.end) {\n\t\tconst element = readElement(bytes, offset);\n\t\tif (element.end > sequenceElement.end) {\n\t\t\tthrow new Error('DER child exceeds parent length');\n\t\t}\n\t\tchildren.push(element);\n\t\toffset = element.end;\n\t}\n\tif (offset !== sequenceElement.end) {\n\t\tthrow new Error('Malformed DER sequence');\n\t}\n\treturn children;\n}\n\n/** Throws if `value` is not a non-negative safe integer. */\nfunction assertNonNegativeSafeInteger(value: number, label: string): void {\n\tif (!Number.isSafeInteger(value) || value < 0) {\n\t\tthrow new Error(`${label} must be a non-negative safe integer`);\n\t}\n}\n\nfunction assertSingleOctetDerTag(tag: number): void {\n\tif (!Number.isSafeInteger(tag) || tag < 0 || tag > 0xff) {\n\t\tthrow new RangeError('DER tag octet must be between 0 and 255');\n\t}\n\tif ((tag & 0x1f) === 0x1f) {\n\t\tthrow new Error('High-tag-number DER tags are not supported');\n\t}\n}\n\nfunction assertContextSpecificTagNumber(tag: number): void {\n\tif (!Number.isSafeInteger(tag) || tag < 0 || tag >= 31) {\n\t\tthrow new RangeError('Context-specific tag number must be between 0 and 30');\n\t}\n}\n\nfunction assertValidDate(date: Date, label: 'UTCTime' | 'GeneralizedTime'): void {\n\tif (Number.isNaN(date.getTime())) {\n\t\tthrow new RangeError(`${label} requires a valid Date`);\n\t}\n}\n\n/**\n * Returns `true` when a constructed element's content should be treated as\n * an opaque leaf (not recursed into) because its tag appears in the\n * `allowOpaqueConstructedTags` list and the offset is at the element start.\n */\nfunction canTreatAsOpaqueLeaf(\n\telement: DerElement,\n\toffset: number,\n\toptions?: {\n\t\t/** Constructed tags whose inner bytes may not parse as valid TLV children. */\n\t\treadonly allowOpaqueConstructedTags?: readonly number[];\n\t},\n): boolean {\n\treturn (\n\t\toffset === element.start && options?.allowOpaqueConstructedTags?.includes(element.tag) === true\n\t);\n}\n"],"mappings":";;;;;AAoBA,SAAgB,aAAa,GAA4B;CAExD,IADA,6BAA6B,GAAQ,YAAY,GAC7C,IAAS,KACZ,OAAO,WAAW,GAAG,CAAM;CAG5B,IAAM,IAAQ,cAAc,CAAM;CAClC,OAAO,WAAW,GAAG,MAAO,EAAM,QAAQ,GAAG,CAAK;AACnD;;AAGA,SAAgB,YAAY,GAA0C;CACrE,IAAM,IAAS,EAAM,QAAQ,GAAK,MAAS,IAAM,EAAK,QAAQ,CAAC,GACzD,IAAM,IAAI,WAAW,CAAM,GAC7B,IAAS;CACb,KAAK,IAAM,KAAQ,GAElB,AADA,EAAI,IAAI,GAAM,CAAM,GACpB,KAAU,EAAK;CAEhB,OAAO;AACR;;;;;AAMA,SAAgB,IAAI,GAAa,GAA+B;CAE/D,OADA,wBAAwB,CAAG,GACpB,YAAY;EAAC,WAAW,GAAG,CAAG;EAAG,aAAa,EAAM,MAAM;EAAG;CAAK,CAAC;AAC3E;;AAGA,SAAgB,SAAS,GAA0C;CAClE,OAAO,IAAI,IAAM,YAAY,CAAK,CAAC;AACpC;;;;;AAMA,SAAgB,MAAM,GAA0C;CAS/D,OAAO,IAAI,IAAM,YARF,EAAM,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM;EAC3C,IAAM,IAAM,KAAK,IAAI,EAAE,QAAQ,EAAE,MAAM;EACvC,KAAK,IAAI,IAAI,GAAG,IAAI,GAAK,KAAK;GAC7B,IAAM,KAAQ,EAAE,MAAM,M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AS,KAAK,CAAO,GACrB,IAAS,EAAQ;CAClB;CACA,IAAI,MAAW,EAAgB,KAC9B,MAAU,MAAM,wBAAwB;CAEzC,OAAO;AACR;;AAGA,SAAS,6BAA6B,GAAe,GAAqB;CACzE,IAAI,CAAC,OAAO,cAAc,CAAK,KAAK,IAAQ,GAC3C,MAAU,MAAM,GAAG,EAAM,qCAAqC;AAEhE;AAEA,SAAS,wBAAwB,GAAmB;CACnD,IAAI,CAAC,OAAO,cAAc,CAAG,KAAK,IAAM,KAAK,IAAM,KAClD,MAAU,WAAW,yCAAyC;CAE/D,KAAK,IAAM,OAAU,IACpB,MAAU,MAAM,4CAA4C;AAE9D;AAEA,SAAS,+BAA+B,GAAmB;CAC1D,IAAI,CAAC,OAAO,cAAc,CAAG,KAAK,IAAM,KAAK,KAAO,IACnD,MAAU,WAAW,sDAAsD;AAE7E;AAEA,SAAS,gBAAgB,GAAY,GAA4C;CAChF,IAAI,OAAO,MAAM,EAAK,QAAQ,CAAC,GAC9B,MAAU,WAAW,GAAG,EAAM,uBAAuB;AAEvD;;;;;;AAOA,SAAS,qBACR,GACA,GACA,GAIU;CACV,OACC,MAAW,EAAQ,SAAS,GAAS,4BAA4B,SAAS,EAAQ,GAAG,MAAM;AAE7F"}
1
+ {"version":3,"file":"der.js","names":[],"sources":["../../../src/internal/asn1/der.ts"],"sourcesContent":["/**\n * Low-level DER encoding and reading helpers shared across the library.\n *\n * These utilities build and traverse ASN.1 TLV (tag-length-value) structures.\n *\n * @module\n */\n\n/**\n * Maximum nesting depth allowed when recursively walking a DER structure.\n *\n * Guards against stack exhaustion from pathologically nested input.\n */\nexport const DEFAULT_MAX_DER_DEPTH = 64;\n\n/**\n * Produces the DER length octets for a given byte count.\n *\n * Values < 128 use the short form (one octet);\n * larger values use the long form (leading octet encodes the number of subsequent length bytes).\n */\nexport function encodeLength(length: number): Uint8Array {\n\tassertNonNegativeSafeInteger(length, 'DER length');\n\tif (length < 128) {\n\t\treturn Uint8Array.of(length);\n\t}\n\n\tconst parts = encodeBase256(length);\n\treturn Uint8Array.of(0x80 | parts.length, ...parts);\n}\n\n/** Concatenates multiple byte arrays into a single {@linkcode Uint8Array}. */\nexport function concatBytes(parts: readonly Uint8Array[]): Uint8Array {\n\tconst length = parts.reduce((sum, part) => sum + part.length, 0);\n\tconst out = new Uint8Array(length);\n\tlet offset = 0;\n\tfor (const part of parts) {\n\t\tout.set(part, offset);\n\t\toffset += part.length;\n\t}\n\treturn out;\n}\n\n/**\n * Builds a complete DER TLV (tag-length-value) element:\n *\n * - one tag octet,\n * - the DER-encoded length, then\n * - the raw value bytes.\n */\nexport function tlv(tag: number, value: Uint8Array): Uint8Array {\n\tassertSingleOctetDerTag(tag);\n\treturn concatBytes([Uint8Array.of(tag), encodeLength(value.length), value]);\n}\n\n/** Wraps concatenated children in a SEQUENCE (tag `0x30`). */\nexport function sequence(parts: readonly Uint8Array[]): Uint8Array {\n\treturn tlv(0x30, concatBytes(parts));\n}\n\n/**\n * Wraps children in a SET (tag `0x31`) after DER-sorting them lexicographically by encoded bytes,\n * as required by {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690} DER.\n */\nexport function setOf(parts: readonly Uint8Array[]): Uint8Array {\n\tconst sorted = parts.slice().sort((a, b) => {\n\t\tconst len = Math.min(a.length, b.length);\n\t\tfor (let i = 0; i < len; i++) {\n\t\t\tconst diff = (a[i] ?? 0) - (b[i] ?? 0);\n\t\t\tif (diff !== 0) return diff;\n\t\t}\n\t\treturn a.length - b.length;\n\t});\n\treturn tlv(0x31, concatBytes(sorted));\n}\n\n/**\n * Wraps a value in an explicit context-specific constructed tag (`0xa0 + tag`).\n *\n * Used for optional SEQUENCE fields tagged with `[tag] EXPLICIT`.\n */\nexport function explicitContext(tag: number, value: Uint8Array): Uint8Array {\n\tassertContextSpecificTagNumber(tag);\n\treturn tlv(0xa0 + tag, value);\n}\n\n/**\n * Wraps a value in an implicit context-specific constructed tag (`0xa0 + tag`).\n *\n * Used for `[tag] IMPLICIT` fields whose underlying type is constructed (e.g. SEQUENCE).\n */\nexport function implicitConstructedContext(tag: number, value: Uint8Array): Uint8Array {\n\tassertContextSpecificTagNumber(tag);\n\treturn tlv(0xa0 + tag, value);\n}\n\n/**\n * Wraps a value in an implicit context-specific primitive tag (`0x80 + tag`).\n *\n * Used for `[tag] IMPLICIT` fields whose underlying type is primitive (e.g. OCTET STRING).\n */\nexport function implicitPrimitiveContext(tag: number, value: Uint8Array): Uint8Array {\n\tassertContextSpecificTagNumber(tag);\n\treturn tlv(0x80 + tag, value);\n}\n\n/**\n * Encodes raw big-endian bytes as a DER INTEGER (tag `0x02`).\n *\n * Strips leading zero bytes for minimal encoding and prepends a zero byte when the high bit is set\n * to keep the value non-negative.\n */\nexport function integer(bytes: Uint8Array): Uint8Array {\n\tif (bytes.length === 0) {\n\t\treturn tlv(0x02, Uint8Array.of(0));\n\t}\n\n\tlet start = 0;\n\twhile (start < bytes.length - 1 && bytes[start] === 0) {\n\t\tstart += 1;\n\t}\n\n\tconst value = bytes.slice(start);\n\tif ((value[0] ?? 0) >= 0x80) {\n\t\treturn tlv(0x02, concatBytes([Uint8Array.of(0), value]));\n\t}\n\n\treturn tlv(0x02, value);\n}\n\n/**\n * Encodes a non-negative JavaScript `number` as a DER INTEGER.\n *\n * @throws if the value is not a non-negative safe integer.\n */\nexport function integerFromNumber(value: number): Uint8Array {\n\tif (!Number.isSafeInteger(value) || value < 0) {\n\t\tthrow new Error('INTEGER must be a non-negative safe integer');\n\t}\n\n\tif (value === 0) {\n\t\treturn integer(Uint8Array.of(0));\n\t}\n\n\treturn integer(Uint8Array.from(encodeBase256(value)));\n}\n\n/** Encodes a DER BOOLEAN (tag `0x01`): `true` → `0xff`, `false` → `0x00`. */\nexport function bool(value: boolean): Uint8Array {\n\treturn tlv(0x01, Uint8Array.of(value ? 0xff : 0x00));\n}\n\n/** Produces a DER NULL element (tag `0x05`, zero-length value). */\nexport function nullValue(): Uint8Array {\n\treturn tlv(0x05, new Uint8Array());\n}\n\n/** Wraps raw bytes in an OCTET STRING element (tag `0x04`). */\nexport function octetString(value: Uint8Array): Uint8Array {\n\treturn tlv(0x04, value);\n}\n\n/**\n * Encodes a DER BIT STRING (tag `0x03`).\n *\n * The value is prefixed with a single octet indicating how many trailing bits in the last byte are unused.\n *\n * @param unusedBits Number of unused trailing bits (0–7). Defaults to 0.\n */\nexport function bitString(value: Uint8Array, unusedBits = 0): Uint8Array {\n\tif (unusedBits < 0 || unusedBits > 7) {\n\t\tthrow new Error('unusedBits must be between 0 and 7');\n\t}\n\tif (value.length === 0 && unusedBits !== 0) {\n\t\tthrow new Error('unusedBits must be 0 when value is empty');\n\t}\n\tif (value.length > 0 && unusedBits > 0) {\n\t\tconst lastByte = value[value.length - 1] ?? 0;\n\t\tif ((lastByte & ((1 << unusedBits) - 1)) !== 0) {\n\t\t\tthrow new Error('unused bits in the last byte must be zero');\n\t\t}\n\t}\n\treturn tlv(0x03, concatBytes([Uint8Array.of(unusedBits), value]));\n}\n\n/** Encodes a DER UTF8String (tag `0x0c`). */\nexport function utf8String(value: string): Uint8Array {\n\treturn tlv(0x0c, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a DER PrintableString (tag `0x13`).\n *\n * @throws if the input contains characters outside the X.520 PrintableString set.\n */\nexport function printableString(value: string): Uint8Array {\n\tif (!/^[A-Za-z0-9 '()+,\\-./:=?]*$/.test(value)) {\n\t\tthrow new Error('Invalid PrintableString: contains characters outside the allowed set');\n\t}\n\treturn tlv(0x13, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a DER IA5String (tag `0x16`).\n *\n * @throws if the input contains any non-ASCII character (code point > 0x7f).\n */\nexport function ia5String(value: string): Uint8Array {\n\tfor (let i = 0; i < value.length; i++) {\n\t\tif (value.charCodeAt(i) > 0x7f) {\n\t\t\tthrow new Error('Invalid IA5String: contains non-ASCII characters');\n\t\t}\n\t}\n\treturn tlv(0x16, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a DER BMPString (tag `0x1e`) as big-endian UTF-16.\n *\n * @throws on lone surrogates and on code points above the Basic Multilingual Plane.\n */\nexport function bmpString(value: string): Uint8Array {\n\tconst units: number[] = [];\n\tfor (const character of value) {\n\t\tconst codePoint = character.codePointAt(0) ?? 0;\n\t\tif (codePoint > 0xffff) {\n\t\t\tthrow new Error('Invalid BMPString: code point above the Basic Multilingual Plane');\n\t\t}\n\t\tif (codePoint >= 0xd800 && codePoint <= 0xdfff) {\n\t\t\tthrow new Error('Invalid BMPString: lone surrogate');\n\t\t}\n\t\tunits.push((codePoint >> 8) & 0xff, codePoint & 0xff);\n\t}\n\treturn tlv(0x1e, Uint8Array.from(units));\n}\n\n/**\n * Encodes a DER UniversalString (tag `0x1c`) as big-endian UTF-32.\n *\n * @throws on lone surrogates.\n */\nexport function universalString(value: string): Uint8Array {\n\tconst bytes: number[] = [];\n\tfor (const character of value) {\n\t\tconst codePoint = character.codePointAt(0) ?? 0;\n\t\tif (codePoint >= 0xd800 && codePoint <= 0xdfff) {\n\t\t\tthrow new Error('Invalid UniversalString: lone surrogate');\n\t\t}\n\t\tbytes.push(\n\t\t\t(codePoint >> 24) & 0xff,\n\t\t\t(codePoint >> 16) & 0xff,\n\t\t\t(codePoint >> 8) & 0xff,\n\t\t\tcodePoint & 0xff,\n\t\t);\n\t}\n\treturn tlv(0x1c, Uint8Array.from(bytes));\n}\n\n/** Encode a non-negative integer as a base-128 sub-identifier ({@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §8.19.2}). */\nfunction encodeBase128(value: bigint): number[] {\n\tconst encoded: number[] = [Number(value & 0x7fn)];\n\tlet current = value >> 7n;\n\twhile (current > 0n) {\n\t\tencoded.unshift(0x80 | Number(current & 0x7fn));\n\t\tcurrent >>= 7n;\n\t}\n\treturn encoded;\n}\n\n/**\n * Encodes a dotted-decimal OID string as a DER OBJECT IDENTIFIER (tag `0x06`).\n *\n * Validates arc constraints per X.660: first arc must be 0–2, second < 40 for arcs 0 and 1.\\\n * Sub-identifiers are encoded with base-128 continuation.\n */\nexport function objectIdentifier(oid: string): Uint8Array {\n\tconst digitPattern = /^\\d+$/;\n\tconst segments = oid.split('.').map((segment) => {\n\t\tif (!digitPattern.test(segment)) {\n\t\t\tthrow new Error(`Invalid OID segment: ${segment}`);\n\t\t}\n\t\treturn BigInt(segment);\n\t});\n\tif (segments.length < 2) {\n\t\tthrow new Error(`Invalid OID: ${oid}`);\n\t}\n\n\tconst [first, second, ...rest] = segments;\n\tif (first === undefined || second === undefined) {\n\t\tthrow new Error(`Invalid OID: ${oid}`);\n\t}\n\tif (first !== 0n && first !== 1n && first !== 2n) {\n\t\tthrow new Error(`Invalid OID first arc: ${first}`);\n\t}\n\tif ((first === 0n || first === 1n) && second >= 40n) {\n\t\tthrow new Error(`Invalid OID second arc: ${second} (must be < 40 when first arc is ${first})`);\n\t}\n\tconst bytes: number[] = encodeBase128(first * 40n + second);\n\tfor (const segment of rest) {\n\t\tbytes.push(...encodeBase128(segment));\n\t}\n\n\treturn tlv(0x06, Uint8Array.from(bytes));\n}\n\n/**\n * Encodes a `Date` as a DER UTCTime (tag `0x17`), format `YYMMDDHHMMSSZ`.\n *\n * Only the two-digit year is stored; suitable for dates in 1950–2049.\n */\nexport function utcTime(date: Date): Uint8Array {\n\tassertValidDate(date, 'UTCTime');\n\tconst year = date.getUTCFullYear();\n\tif (year < 1950 || year > 2049) {\n\t\tthrow new RangeError('UTCTime year must be between 1950 and 2049');\n\t}\n\tconst value = `${[\n\t\ttwoDigits(year % 100),\n\t\ttwoDigits(date.getUTCMonth() + 1),\n\t\ttwoDigits(date.getUTCDate()),\n\t\ttwoDigits(date.getUTCHours()),\n\t\ttwoDigits(date.getUTCMinutes()),\n\t\ttwoDigits(date.getUTCSeconds()),\n\t].join('')}Z`;\n\treturn tlv(0x17, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a {@linkcode Date} as a DER GeneralizedTime (tag `0x18`), format `YYYYMMDDHHMMSSZ`.\n *\n * Uses a four-digit year; required for dates outside the 1950–2049 range.\n */\nexport function generalizedTime(date: Date): Uint8Array {\n\tassertValidDate(date, 'GeneralizedTime');\n\tconst year = date.getUTCFullYear();\n\tif (year < 0 || year > 9999) {\n\t\tthrow new RangeError('GeneralizedTime year must be between 0 and 9999');\n\t}\n\tconst value = `${[\n\t\tString(year).padStart(4, '0'),\n\t\ttwoDigits(date.getUTCMonth() + 1),\n\t\ttwoDigits(date.getUTCDate()),\n\t\ttwoDigits(date.getUTCHours()),\n\t\ttwoDigits(date.getUTCMinutes()),\n\t\ttwoDigits(date.getUTCSeconds()),\n\t].join('')}Z`;\n\treturn tlv(0x18, new TextEncoder().encode(value));\n}\n\n/**\n * Encodes a {@linkcode Date} as the appropriate DER time type per RFC 5280.\n *\n * - {@linkcode utcTime} for 1950–2049\n * - {@linkcode generalizedTime} otherwise\n */\nexport function time(date: Date): Uint8Array {\n\tif (date.getUTCFullYear() >= 2050 || date.getUTCFullYear() < 1950) {\n\t\treturn generalizedTime(date);\n\t}\n\treturn utcTime(date);\n}\n\n/** Zero-pads a number to two digits for time encoding. */\nfunction twoDigits(value: number): string {\n\treturn String(value).padStart(2, '0');\n}\n\n/** Encodes a non-negative integer as big-endian base-256 octets. */\nfunction encodeBase256(value: number): readonly number[] {\n\tassertNonNegativeSafeInteger(value, 'DER integer');\n\tconst parts: number[] = [];\n\tlet current = value;\n\twhile (current > 0) {\n\t\tparts.unshift(current & 0xff);\n\t\tcurrent = Math.floor(current / 256);\n\t}\n\treturn parts;\n}\n\n/** A single parsed ASN.1 TLV element with byte-range metadata. */\nexport interface DerElement {\n\t/** ASN.1 tag byte (e.g. `0x30` for SEQUENCE, `0x02` for INTEGER). */\n\treadonly tag: number;\n\t/** Number of bytes occupied by the tag + length octets. */\n\treadonly headerLength: number;\n\t/** Byte length of the value portion (excluding tag and length octets). */\n\treadonly length: number;\n\t/** Byte offset where the value portion begins in the source buffer. */\n\treadonly start: number;\n\t/** Byte offset one past the last value byte. Equals the next element's header offset. */\n\treadonly end: number;\n\t/** The raw value bytes (slice of the source buffer). */\n\treadonly value: Uint8Array;\n}\n\n/** Options for {@linkcode readSequenceChildren}. */\nexport interface ReadSequenceChildrenOptions {\n\t/** Maximum nesting depth for the DER depth check. @default {@linkcode DEFAULT_MAX_DER_DEPTH}. */\n\treadonly maxDepth?: number;\n\t/** Constructed tags whose inner bytes may not parse as valid TLV children (e.g. opaque extension values). */\n\treadonly allowOpaqueConstructedTags?: readonly number[];\n}\n\n/** Options for {@linkcode readRootElement}. */\nexport interface ReadRootElementOptions {\n\t/** Maximum nesting depth for the DER depth check. @default {@linkcode DEFAULT_MAX_DER_DEPTH}. */\n\treadonly maxDepth?: number;\n\t/** Constructed tags whose inner bytes may not parse as valid TLV children (e.g. opaque extension values). */\n\treadonly allowOpaqueConstructedTags?: readonly number[];\n}\n\n/**\n * Reads one TLV element from {@linkcode bytes} starting at {@linkcode offset}.\n *\n * Parses the tag byte, decodes the DER length octets, and slices out the value bytes.\n *\n * @throws on truncated input, indefinite lengths, and non-minimal length encodings.\n * @param offset Byte position of the tag octet. Defaults to 0.\n */\nexport function readElement(bytes: Uint8Array, offset = 0): DerElement {\n\tconst tag = bytes[offset];\n\tif (tag === undefined) {\n\t\tthrow new Error('Unexpected end of DER input');\n\t}\n\tif ((tag & 0x1f) === 0x1f) {\n\t\tthrow new Error('High-tag-number DER form is not supported');\n\t}\n\tconst lengthByte = bytes[offset + 1];\n\tif (lengthByte === undefined) {\n\t\tthrow new Error('Unexpected end of DER input');\n\t}\n\n\tconst { headerLength, length } = readDerLength(bytes, offset, lengthByte);\n\n\tconst start = offset + headerLength;\n\tconst end = start + length;\n\tif (end > bytes.length) {\n\t\tthrow new Error('DER element exceeds input length');\n\t}\n\n\treturn {\n\t\ttag,\n\t\theaderLength,\n\t\tlength,\n\t\tstart,\n\t\tend,\n\t\tvalue: bytes.slice(start, end),\n\t};\n}\n\n/** Decodes and validates the definite-length field of a DER element. */\nfunction readDerLength(\n\tbytes: Uint8Array,\n\toffset: number,\n\tlengthByte: number,\n): { readonly headerLength: number; readonly length: number } {\n\tif ((lengthByte & 0x80) === 0) {\n\t\treturn { headerLength: 2, length: lengthByte };\n\t}\n\tconst octets = lengthByte & 0x7f;\n\tif (octets === 0) {\n\t\tthrow new Error('Indefinite lengths are not supported');\n\t}\n\tconst firstLengthOctet = bytes[offset + 2];\n\tif (firstLengthOctet === undefined) {\n\t\tthrow new Error('Unexpected end of DER input');\n\t}\n\tif (firstLengthOctet === 0) {\n\t\tthrow new Error('Non-minimal DER length encoding');\n\t}\n\tconst length = readLongFormDerLength(bytes, offset, octets);\n\tif (length < 128) {\n\t\tthrow new Error('Non-minimal DER length encoding');\n\t}\n\treturn { headerLength: 2 + octets, length };\n}\n\nfunction readLongFormDerLength(bytes: Uint8Array, offset: number, octets: number): number {\n\tlet length = 0;\n\tfor (let index = 0; index < octets; index += 1) {\n\t\tconst next = bytes[offset + 2 + index];\n\t\tif (next === undefined) {\n\t\t\tthrow new Error('Unexpected end of DER input');\n\t\t}\n\t\tif (length > Math.floor((Number.MAX_SAFE_INTEGER - next) / 256)) {\n\t\t\tthrow new Error('DER length exceeds safe integer range');\n\t\t}\n\t\tlength = length * 256 + next;\n\t}\n\treturn length;\n}\n\n/**\n * Walks the full DER tree rooted in {@linkcode bytes}.\n *\n * Constructed tags with content that cannot be parsed as valid children are tolerated when listed in {@linkcode options | allowOpaqueConstructedTags}.\n *\n * @throws if nesting exceeds {@linkcode maxDepth}.\n */\nexport function assertDerMaxDepth(\n\tbytes: Uint8Array,\n\tmaxDepth: number = DEFAULT_MAX_DER_DEPTH,\n\toptions?: {\n\t\t/** Constructed tags whose inner bytes may not parse as valid TLV children. */\n\t\treadonly allowOpaqueConstructedTags?: readonly number[];\n\t},\n): void {\n\tif (!Number.isSafeInteger(maxDepth) || maxDepth < 1) {\n\t\tthrow new Error('DER max depth must be a positive safe integer');\n\t}\n\tconst root = readElement(bytes, 0);\n\tif (root.end !== bytes.length) {\n\t\tthrow new Error('Trailing data after DER element');\n\t}\n\tconst stack: {\n\t\treadonly element: DerElement;\n\t\treadonly depth: number;\n\t}[] = [{ element: root, depth: 1 }];\n\twhile (stack.length > 0) {\n\t\tconst current = stack.pop();\n\t\tif (current === undefined) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (current.depth > maxDepth) {\n\t\t\tthrow new Error(`DER exceeds max depth of ${maxDepth}`);\n\t\t}\n\t\tif ((current.element.tag & 0x20) === 0) {\n\t\t\tcontinue;\n\t\t}\n\t\tpushDerChildren(bytes, current, stack, options);\n\t}\n}\n\nfunction pushDerChildren(\n\tbytes: Uint8Array,\n\tcurrent: { readonly element: DerElement; readonly depth: number },\n\tstack: { readonly element: DerElement; readonly depth: number }[],\n\toptions?: {\n\t\treadonly allowOpaqueConstructedTags?: readonly number[];\n\t},\n): void {\n\tlet offset = current.element.start;\n\tlet treatedAsOpaqueLeaf = false;\n\twhile (offset < current.element.end) {\n\t\tconst child = readDerChildOrOpaque(bytes, current.element, offset, options);\n\t\tif (child === undefined) {\n\t\t\ttreatedAsOpaqueLeaf = true;\n\t\t\toffset = current.element.end;\n\t\t\tbreak;\n\t\t}\n\t\tstack.push({ element: child, depth: current.depth + 1 });\n\t\toffset = child.end;\n\t}\n\tif (!treatedAsOpaqueLeaf && offset !== current.element.end) {\n\t\tthrow new Error('Malformed DER container');\n\t}\n}\n\nfunction readDerChildOrOpaque(\n\tbytes: Uint8Array,\n\tparent: DerElement,\n\toffset: number,\n\toptions?: {\n\t\treadonly allowOpaqueConstructedTags?: readonly number[];\n\t},\n): DerElement | undefined {\n\tlet child: DerElement;\n\ttry {\n\t\tchild = readElement(bytes, offset);\n\t} catch (error) {\n\t\tif (canTreatAsOpaqueLeaf(parent, offset, options)) return undefined;\n\t\tthrow error;\n\t}\n\tif (child.end <= parent.end) {\n\t\treturn child;\n\t}\n\tif (canTreatAsOpaqueLeaf(parent, offset, options)) return undefined;\n\tthrow new Error('DER child exceeds parent length');\n}\n\n/**\n * Reads the single top-level TLV element from {@linkcode bytes}.\n *\n * Optionally validates nesting depth.\n *\n * @throws if there is trailing data after the element.\n */\nexport function readRootElement(bytes: Uint8Array, options?: ReadRootElementOptions): DerElement {\n\tconst maxDepth = options?.maxDepth ?? DEFAULT_MAX_DER_DEPTH;\n\tassertDerMaxDepth(bytes, maxDepth, options);\n\tconst element = readElement(bytes, 0);\n\tif (element.end !== bytes.length) {\n\t\tthrow new Error('Trailing data after DER element');\n\t}\n\treturn element;\n}\n\n/**\n * Reads a DER-encoded SEQUENCE from {@linkcode bytes} and returns its direct children.\n *\n * @throws if the root element is not a SEQUENCE or if child boundaries are inconsistent.\n */\nexport function readSequenceChildren(\n\tbytes: Uint8Array,\n\toptions?: ReadSequenceChildrenOptions,\n): DerElement[] {\n\tconst sequenceElement = readRootElement(bytes, options);\n\tif (sequenceElement.tag !== 0x30) {\n\t\tthrow new Error('Expected SEQUENCE');\n\t}\n\n\tconst children: DerElement[] = [];\n\tlet offset = sequenceElement.start;\n\twhile (offset < sequenceElement.end) {\n\t\tconst element = readElement(bytes, offset);\n\t\tif (element.end > sequenceElement.end) {\n\t\t\tthrow new Error('DER child exceeds parent length');\n\t\t}\n\t\tchildren.push(element);\n\t\toffset = element.end;\n\t}\n\tif (offset !== sequenceElement.end) {\n\t\tthrow new Error('Malformed DER sequence');\n\t}\n\treturn children;\n}\n\n/** @throws if {@linkcode value} is not a non-negative safe integer. */\nfunction assertNonNegativeSafeInteger(value: number, label: string): void {\n\tif (!Number.isSafeInteger(value) || value < 0) {\n\t\tthrow new Error(`${label} must be a non-negative safe integer`);\n\t}\n}\n\nfunction assertSingleOctetDerTag(tag: number): void {\n\tif (!Number.isSafeInteger(tag) || tag < 0 || tag > 0xff) {\n\t\tthrow new RangeError('DER tag octet must be between 0 and 255');\n\t}\n\tif ((tag & 0x1f) === 0x1f) {\n\t\tthrow new Error('High-tag-number DER tags are not supported');\n\t}\n}\n\nfunction assertContextSpecificTagNumber(tag: number): void {\n\tif (!Number.isSafeInteger(tag) || tag < 0 || tag >= 31) {\n\t\tthrow new RangeError('Context-specific tag number must be between 0 and 30');\n\t}\n}\n\nfunction assertValidDate(date: Date, label: 'UTCTime' | 'GeneralizedTime'): void {\n\tif (Number.isNaN(date.getTime())) {\n\t\tthrow new RangeError(`${label} requires a valid Date`);\n\t}\n}\n\n/**\n * @returns `true` when a constructed element's content should be treated as an opaque leaf (not recursed into)\n * because its tag appears in the {@linkcode options | allowOpaqueConstructedTags} list and the offset is at the element start.\n */\nfunction canTreatAsOpaqueLeaf(\n\telement: DerElement,\n\toffset: number,\n\toptions?: {\n\t\t/** Constructed tags whose inner bytes may not parse as valid TLV children. */\n\t\treadonly allowOpaqueConstructedTags?: readonly number[];\n\t},\n): boolean {\n\treturn (\n\t\toffset === element.start && options?.allowOpaqueConstructedTags?.includes(element.tag) === true\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAa,IAAwB;;;;;;;AAQrC,SAAgB,aAAa,GAA4B;CAExD,IADA,6BAA6B,GAAQ,YAAY,GAC7C,IAAS,KACZ,OAAO,WAAW,GAAG,CAAM;CAG5B,IAAM,IAAQ,cAAc,CAAM;CAClC,OAAO,WAAW,GAAG,MAAO,EAAM,QAAQ,GAAG,CAAK;AACnD;;AAGA,SAAgB,YAAY,GAA0C;CACrE,IAAM,IAAS,EAAM,QAAQ,GAAK,MAAS,IAAM,EAAK,QAAQ,CAAC,GACzD,IAAM,IAAI,WAAW,CAAM,GAC7B,IAAS;CACb,KAAK,IAAM,KAAQ,GAElB,AADA,EAAI,IAAI,GAAM,CAAM,GACpB,KAAU,EAAK;CAEhB,OAAO;AACR;;;;;;;;AASA,SAAgB,IAAI,GAAa,GAA+B;CAE/D,OADA,wBAAwB,CAAG,GACpB,YAAY;EAAC,WAAW,GAAG,CAAG;EAAG,aAAa,EAAM,MAAM;EAAG;CAAK,CAAC;AAC3E;;AAGA,SAAgB,SAAS,GAA0C;CAClE,OAAO,IAAI,IAAM,YAAY,CAAK,CAAC;A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AO,cAAc,CAAG,KAAK,IAAM,KAAK,IAAM,KAClD,MAAU,WAAW,yCAAyC;CAE/D,KAAK,IAAM,OAAU,IACpB,MAAU,MAAM,4CAA4C;AAE9D;AAEA,SAAS,+BAA+B,GAAmB;CAC1D,IAAI,CAAC,OAAO,cAAc,CAAG,KAAK,IAAM,KAAK,KAAO,IACnD,MAAU,WAAW,sDAAsD;AAE7E;AAEA,SAAS,gBAAgB,GAAY,GAA4C;CAChF,IAAI,OAAO,MAAM,EAAK,QAAQ,CAAC,GAC9B,MAAU,WAAW,GAAG,EAAM,uBAAuB;AAEvD;;;;;AAMA,SAAS,qBACR,GACA,GACA,GAIU;CACV,OACC,MAAW,EAAQ,SAAS,GAAS,4BAA4B,SAAS,EAAQ,GAAG,MAAM;AAE7F"}
@@ -59,8 +59,15 @@ function flattenOidTree(e) {
59
59
  return t;
60
60
  }
61
61
  /**
62
- * Dotted-decimal OID constants for X.500 attributes, PKCS algorithms,
63
- * X.509v3 extensions, EKU purposes, hash algorithms, and PKIX access methods.
62
+ * Dotted-decimal OID constants.
63
+ *
64
+ * Includes:
65
+ * - EKU purposes,
66
+ * - PKCS algorithms,
67
+ * - PKIX access methods,
68
+ * - X.500 attributes,
69
+ * - X.509v3 extensions, and
70
+ * - hash algorithms.
64
71
  */
65
72
  const t = flattenOidTree(e);
66
73
  //#endregion