micro509 0.14.0 → 0.15.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 (161) hide show
  1. package/CHANGELOG.md +2064 -0
  2. package/README.md +105 -225
  3. package/dist/crypto/crypto.d.ts +42 -11
  4. package/dist/crypto/crypto.js +53 -22
  5. package/dist/crypto/crypto.js.map +1 -1
  6. package/dist/crypto.d.ts +2 -2
  7. package/dist/crypto.js +1 -2
  8. package/dist/der/der.d.ts +41 -33
  9. package/dist/der/der.js +26 -24
  10. package/dist/der/der.js.map +1 -1
  11. package/dist/index.d.ts +10 -10
  12. package/dist/index.js +19 -19
  13. package/dist/internal/asn1/asn1.d.ts +3 -6
  14. package/dist/internal/asn1/asn1.js +343 -43
  15. package/dist/internal/asn1/asn1.js.map +1 -1
  16. package/dist/internal/asn1/ber.js +146 -0
  17. package/dist/internal/asn1/ber.js.map +1 -0
  18. package/dist/internal/asn1/decode-refusal.js +29 -0
  19. package/dist/internal/asn1/decode-refusal.js.map +1 -0
  20. package/dist/internal/asn1/der.d.ts +38 -41
  21. package/dist/internal/asn1/der.js +131 -52
  22. package/dist/internal/asn1/der.js.map +1 -1
  23. package/dist/internal/asn1/oids.js +83 -53
  24. package/dist/internal/asn1/oids.js.map +1 -1
  25. package/dist/internal/asn1/oids2.js +1 -2
  26. package/dist/internal/asn1/oids2.js.map +1 -1
  27. package/dist/internal/crypto/algorithm-names.js +1 -2
  28. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  29. package/dist/internal/crypto/ecdsa.js +13 -8
  30. package/dist/internal/crypto/ecdsa.js.map +1 -1
  31. package/dist/internal/crypto/hash.js +1 -2
  32. package/dist/internal/crypto/hash.js.map +1 -1
  33. package/dist/internal/crypto/pbes2.d.ts +24 -11
  34. package/dist/internal/crypto/pbes2.js +79 -26
  35. package/dist/internal/crypto/pbes2.js.map +1 -1
  36. package/dist/internal/crypto/rsa-pss.js +46 -44
  37. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  38. package/dist/internal/crypto/sig-verify.d.ts +4 -7
  39. package/dist/internal/crypto/sig-verify.js +107 -60
  40. package/dist/internal/crypto/sig-verify.js.map +1 -1
  41. package/dist/internal/crypto/signing.d.ts +1 -4
  42. package/dist/internal/crypto/signing.js +28 -29
  43. package/dist/internal/crypto/signing.js.map +1 -1
  44. package/dist/internal/crypto/webcrypto.js +1 -2
  45. package/dist/internal/crypto/webcrypto.js.map +1 -1
  46. package/dist/internal/shared/base64.js +1 -2
  47. package/dist/internal/shared/base64.js.map +1 -1
  48. package/dist/internal/shared/dn.d.ts +3 -6
  49. package/dist/internal/shared/dn.js +99 -53
  50. package/dist/internal/shared/dn.js.map +1 -1
  51. package/dist/internal/shared/idna-tables.js +7217 -0
  52. package/dist/internal/shared/idna-tables.js.map +1 -0
  53. package/dist/internal/shared/idna.js +296 -0
  54. package/dist/internal/shared/idna.js.map +1 -0
  55. package/dist/internal/shared/ip.d.ts +4 -7
  56. package/dist/internal/shared/ip.js +1 -2
  57. package/dist/internal/shared/ip.js.map +1 -1
  58. package/dist/internal/shared/mailbox.js +26 -0
  59. package/dist/internal/shared/mailbox.js.map +1 -0
  60. package/dist/internal/shared/punycode.js +101 -0
  61. package/dist/internal/shared/punycode.js.map +1 -0
  62. package/dist/internal/shared/rfc3454-tables.js +1 -2
  63. package/dist/internal/shared/rfc3454-tables.js.map +1 -1
  64. package/dist/internal/shared/rfc4518-tables.js +119 -0
  65. package/dist/internal/shared/rfc4518-tables.js.map +1 -0
  66. package/dist/internal/shared/uri-host.js +102 -0
  67. package/dist/internal/shared/uri-host.js.map +1 -0
  68. package/dist/internal/verify/name-constraints-engine.js +246 -131
  69. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  70. package/dist/internal/verify/policy-engine.js +6 -21
  71. package/dist/internal/verify/policy-engine.js.map +1 -1
  72. package/dist/internal/verify/verify-path.d.ts +1 -4
  73. package/dist/internal/verify/verify-path.js +267 -131
  74. package/dist/internal/verify/verify-path.js.map +1 -1
  75. package/dist/internal/x509/directory-name.js +1 -2
  76. package/dist/internal/x509/directory-name.js.map +1 -1
  77. package/dist/internal/x509/extension-bits.js +2 -3
  78. package/dist/internal/x509/extension-bits.js.map +1 -1
  79. package/dist/internal/x509/extension-errors.d.ts +1 -4
  80. package/dist/internal/x509/extension-errors.js +1 -2
  81. package/dist/internal/x509/extension-errors.js.map +1 -1
  82. package/dist/internal/x509/extension-registry.js +107 -89
  83. package/dist/internal/x509/extension-registry.js.map +1 -1
  84. package/dist/internal/x509/general-name-profile.js +129 -0
  85. package/dist/internal/x509/general-name-profile.js.map +1 -0
  86. package/dist/internal/x509/general-name-tags.js +1 -2
  87. package/dist/internal/x509/general-name-tags.js.map +1 -1
  88. package/dist/internal/x509/general-name.js +128 -42
  89. package/dist/internal/x509/general-name.js.map +1 -1
  90. package/dist/internal/x509/name-fields.js +1 -2
  91. package/dist/internal/x509/name-fields.js.map +1 -1
  92. package/dist/internal/x509/or-address.js +217 -0
  93. package/dist/internal/x509/or-address.js.map +1 -0
  94. package/dist/keys/keys.d.ts +112 -97
  95. package/dist/keys/keys.js +211 -204
  96. package/dist/keys/keys.js.map +1 -1
  97. package/dist/keys.d.ts +3 -3
  98. package/dist/pem/pem.d.ts +14 -17
  99. package/dist/pem/pem.js +16 -17
  100. package/dist/pem/pem.js.map +1 -1
  101. package/dist/pem.js +1 -2
  102. package/dist/pkcs/pfx.d.ts +57 -32
  103. package/dist/pkcs/pfx.js +209 -173
  104. package/dist/pkcs/pfx.js.map +1 -1
  105. package/dist/pkcs/pkcs12-mac.d.ts +104 -27
  106. package/dist/pkcs/pkcs12-mac.js +266 -79
  107. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  108. package/dist/pkcs/pkcs7.d.ts +43 -37
  109. package/dist/pkcs/pkcs7.js +218 -217
  110. package/dist/pkcs/pkcs7.js.map +1 -1
  111. package/dist/pkcs.d.ts +4 -3
  112. package/dist/result/result.d.ts +27 -20
  113. package/dist/result/result.js +8 -3
  114. package/dist/result/result.js.map +1 -1
  115. package/dist/result.d.ts +2 -2
  116. package/dist/result.js +1 -2
  117. package/dist/revocation/chain.d.ts +101 -23
  118. package/dist/revocation/chain.js +436 -182
  119. package/dist/revocation/chain.js.map +1 -1
  120. package/dist/revocation/crl.d.ts +131 -67
  121. package/dist/revocation/crl.js +444 -323
  122. package/dist/revocation/crl.js.map +1 -1
  123. package/dist/revocation/ocsp.d.ts +94 -59
  124. package/dist/revocation/ocsp.js +331 -293
  125. package/dist/revocation/ocsp.js.map +1 -1
  126. package/dist/revocation/revocation.d.ts +47 -35
  127. package/dist/revocation/revocation.js +88 -57
  128. package/dist/revocation/revocation.js.map +1 -1
  129. package/dist/revocation.d.ts +3 -3
  130. package/dist/verify/identity.d.ts +16 -19
  131. package/dist/verify/identity.js +118 -89
  132. package/dist/verify/identity.js.map +1 -1
  133. package/dist/verify/name-constraints.d.ts +1 -4
  134. package/dist/verify/policy.d.ts +9 -7
  135. package/dist/verify/verify.d.ts +84 -46
  136. package/dist/verify/verify.js +300 -152
  137. package/dist/verify/verify.js.map +1 -1
  138. package/dist/verify.d.ts +2 -2
  139. package/dist/x509/certificate.d.ts +20 -21
  140. package/dist/x509/certificate.js +62 -46
  141. package/dist/x509/certificate.js.map +1 -1
  142. package/dist/x509/csr.d.ts +5 -7
  143. package/dist/x509/csr.js +10 -11
  144. package/dist/x509/csr.js.map +1 -1
  145. package/dist/x509/extensions.d.ts +154 -66
  146. package/dist/x509/extensions.js +317 -151
  147. package/dist/x509/extensions.js.map +1 -1
  148. package/dist/x509/fingerprint.d.ts +4 -7
  149. package/dist/x509/fingerprint.js +1 -2
  150. package/dist/x509/fingerprint.js.map +1 -1
  151. package/dist/x509/name-text.d.ts +7 -9
  152. package/dist/x509/name-text.js +18 -4
  153. package/dist/x509/name-text.js.map +1 -1
  154. package/dist/x509/name.d.ts +9 -11
  155. package/dist/x509/name.js +18 -19
  156. package/dist/x509/name.js.map +1 -1
  157. package/dist/x509/parse.d.ts +84 -69
  158. package/dist/x509/parse.js +421 -400
  159. package/dist/x509/parse.js.map +1 -1
  160. package/dist/x509.d.ts +2 -2
  161. package/package.json +76 -59
@@ -1,8 +1,7 @@
1
- //#region src/internal/asn1/asn1.d.ts
2
1
  /** Converts raw bytes to a lowercase hex string with no separator. */
3
- declare function toHex(bytes: Uint8Array): string;
2
+ export declare function toHex(bytes: Uint8Array): string;
4
3
  /** A BIT STRING payload and the number of unused trailing bits in its final byte. */
5
- interface DerBitString {
4
+ export interface DerBitString {
6
5
  /** Payload bytes, excluding the leading unused-bit count octet. */
7
6
  readonly bytes: Uint8Array;
8
7
  /** Unused trailing bits in the final byte, 0 through 7. */
@@ -14,7 +13,5 @@ interface DerBitString {
14
13
  * Converts a hex string (even or odd length) to a {@linkcode Uint8Array}.
15
14
  * Odd-length strings are left-padded with a zero nibble.
16
15
  */
17
- declare function hexToBytes(value: string): Uint8Array;
18
- //#endregion
19
- export { DerBitString, hexToBytes, toHex };
16
+ export declare function hexToBytes(value: string): Uint8Array;
20
17
  //# sourceMappingURL=asn1.d.ts.map
@@ -1,21 +1,35 @@
1
- import { objectIdentifier as e, readElement as t } from "./der.js";
2
- //#region src/internal/asn1/asn1.ts
1
+ import { DECODE_LIMIT_CODES as e, rethrowDecodeRefusal as t, throwDecodeRefusal as n } from "./decode-refusal.js";
2
+ import { objectIdentifier as r, readElement as i, walkDerTree as a } from "./der.js";
3
+ // src/internal/asn1/asn1.ts
4
+ /**
5
+ * ASN.1 decoding helpers used by the higher-level DER, parse, and crypto modules.
6
+ *
7
+ * These utilities stay intentionally small and strict so malformed input fails early and
8
+ * higher-level modules can build on one consistent decoding layer.
9
+ *
10
+ * @module
11
+ */
3
12
  /** Shared UTF-8 text decoder for ASN.1 string types. */
4
- const n = new TextDecoder("utf-8", { fatal: !0 }), r = /^[A-Za-z0-9 '()+,\-./:=?]*$/u;
13
+ const o = new TextDecoder("utf-8", {
14
+ fatal: !0,
15
+ ignoreBOM: !0
16
+ }), s = /^[A-Za-z0-9 '()+,\-./:=?]*$/u;
5
17
  /**
6
18
  * Decodes a DER-encoded OBJECT IDENTIFIER value into its dotted-decimal string form.
7
19
  *
8
20
  * @example "1.2.840.113549.1.1.1"
9
- * @throws on empty input, truncated multi-byte sub-identifiers, or incomplete continuation octets.
21
+ * @throws on empty input, truncated multi-byte sub-identifiers, or incomplete continuation octets,
22
+ * and `limit_exceeded` for a sub-identifier encoded in more than
23
+ * {@linkcode MAX_OID_SUBIDENTIFIER_OCTETS} octets.
10
24
  */
11
25
  function decodeObjectIdentifier(e) {
12
26
  if (e.length === 0) throw Error("OID is empty");
13
- let t = decodeOidSubidentifier(e, 0), n = t.nextOffset, r = t.value < 40 ? [0, t.value] : t.value < 80 ? [1, t.value - 40] : [2, t.value - 80];
27
+ let t = decodeOidSubidentifier(e, 0), n = t.nextOffset, r = t.value, i = r < 40n ? [0n, r] : r < 80n ? [1n, r - 40n] : [2n, r - 80n];
14
28
  for (; n < e.length;) {
15
29
  let t = decodeOidSubidentifier(e, n);
16
- r.push(t.value), n = t.nextOffset;
30
+ i.push(t.value), n = t.nextOffset;
17
31
  }
18
- return r.join(".");
32
+ return i.join(".");
19
33
  }
20
34
  /**
21
35
  * Reduces an OID to the dotted-decimal form its DER encoding decodes back to, so
@@ -25,8 +39,8 @@ function decodeObjectIdentifier(e) {
25
39
  * @example `canonicalizeOid('2.5.029.17')` returns `'2.5.29.17'`
26
40
  * @throws if the OID has a non-numeric segment or violates the X.660 arc constraints.
27
41
  */
28
- function canonicalizeOid(n) {
29
- return decodeObjectIdentifier(t(e(n), 0).value);
42
+ function canonicalizeOid(e) {
43
+ return decodeObjectIdentifier(i(r(e), 0).value);
30
44
  }
31
45
  /** Converts raw bytes to a lowercase hex string with no separator. */
32
46
  function toHex(e) {
@@ -46,15 +60,15 @@ function toArrayBuffer(e) {
46
60
  *
47
61
  * @throws if any child overflows the parent boundary or if there is leftover data between the last child and the parent end offset.
48
62
  */
49
- function childrenOf(e, n) {
50
- let r = [], i = n.start;
51
- for (; i < n.end;) {
52
- let a = t(e, i);
53
- if (a.end > n.end) throw Error("DER child exceeds parent length");
54
- r.push(a), i = a.end;
63
+ function childrenOf(e, t) {
64
+ let n = [], r = t.start;
65
+ for (; r < t.end;) {
66
+ let a = i(e, r);
67
+ if (a.end > t.end) throw Error("DER child exceeds parent length");
68
+ n.push(a), r = a.end;
55
69
  }
56
- if (i !== n.end) throw Error("Malformed DER container");
57
- return r;
70
+ if (r !== t.end) throw Error("Malformed DER container");
71
+ return n;
58
72
  }
59
73
  /**
60
74
  * Asserts that a value is not `undefined`, returning it narrowed.\
@@ -127,21 +141,46 @@ function parseTime(e) {
127
141
  throw Error(`Unsupported time tag: ${e.tag}`);
128
142
  }
129
143
  /**
130
- * Decodes a big-endian unsigned byte sequence into a JavaScript `number`.
144
+ * Decodes DER INTEGER content octets holding a non-negative value into a JavaScript `number`.
131
145
  *
132
- * @throws if the value exceeds {@linkcode Number.MAX_SAFE_INTEGER}, the boundary for lossless arithmetic.
146
+ * @throws if the value is empty, negative, non-minimally encoded, or exceeds {@linkcode Number.MAX_SAFE_INTEGER}.
133
147
  */
134
148
  function decodeIntegerNumber(e) {
135
- let t = e[0];
136
- if (t === void 0) throw Error("INTEGER is empty");
137
- if (t & 128) throw Error("INTEGER must be non-negative");
138
- if (e.length > 1 && t === 0 && !((e[1] ?? 0) & 128)) throw Error("INTEGER must use minimal encoding");
139
- let n = 0;
149
+ let t = decodeIntegerMagnitude(e);
150
+ if (t.type === "unsafe") throw Error(`Integer too large for safe number (${e.length} bytes)`);
151
+ return t.value;
152
+ }
153
+ /**
154
+ * Decodes DER INTEGER content octets holding a non-negative value, stopping at the first octet past {@linkcode Number.MAX_SAFE_INTEGER}.
155
+ *
156
+ * @param bytes DER INTEGER content octets to decode.
157
+ * @param label Field name for error messages (defaults to `"INTEGER"`).
158
+ * @throws if the value is empty, negative, or non-minimally encoded.
159
+ */
160
+ function decodeIntegerMagnitude(e, t = "INTEGER") {
161
+ let n = e[0];
162
+ if (n === void 0) throw Error(`${t} is empty`);
163
+ if (n & 128) throw Error(`${t} must be non-negative`);
164
+ if (e.length > 1 && n === 0 && !((e[1] ?? 0) & 128)) throw Error(`${t} must use minimal encoding`);
165
+ let r = 0;
140
166
  for (let t of e) {
141
- if (n > Math.floor((2 ** 53 - 1 - t) / 256)) throw Error(`Integer too large for safe number (${e.length} bytes)`);
142
- n = n * 256 + t;
167
+ if (r > Math.floor((2 ** 53 - 1 - t) / 256)) return { type: "unsafe" };
168
+ r = r * 256 + t;
143
169
  }
144
- return n;
170
+ return {
171
+ type: "safe",
172
+ value: r
173
+ };
174
+ }
175
+ /**
176
+ * Decodes DER INTEGER or ENUMERATED content octets holding a non-negative value of any length into a `bigint`.
177
+ *
178
+ * @param bytes DER content octets to decode.
179
+ * @param label Field name for error messages (defaults to `"INTEGER"`).
180
+ * @throws if the value is empty, negative, or non-minimally encoded.
181
+ */
182
+ function decodeNonNegativeIntegerBigInt(e, t = "INTEGER") {
183
+ return decodeIntegerMagnitude(e, t), BigInt(`0x${toHex(e)}`);
145
184
  }
146
185
  /**
147
186
  * Like {@linkcode decodeIntegerNumber}, but optionally rewrites thrown error messages with a caller-specific field label.
@@ -193,16 +232,18 @@ function decodeOidSubidentifier(e, t) {
193
232
  let n = e[t];
194
233
  if (n === void 0) throw Error("Malformed OID");
195
234
  if (n === 128) throw Error("Malformed OID: non-minimal base-128 encoding");
196
- let r = 0, i = t;
197
- for (; i < e.length; i += 1) {
198
- let t = e[i];
199
- if (t === void 0) throw Error("Malformed OID");
200
- let n = t & 127;
201
- if (r > Math.floor((2 ** 53 - 1 - n) / 128)) throw Error("Malformed OID: overflow/non-minimal or too-large subidentifier");
202
- if (r = r * 128 + n, !(t & 128)) return {
203
- value: r,
204
- nextOffset: i + 1
205
- };
235
+ let r = findOidSubidentifierEnd(e, t), i = 0n;
236
+ for (let n = t; n <= r; n += 1) i = i << 7n | BigInt((e[n] ?? 0) & 127);
237
+ return {
238
+ value: i,
239
+ nextOffset: r + 1
240
+ };
241
+ }
242
+ /** The offset of the sub-identifier's last octet, checked before any of it is accumulated. */
243
+ function findOidSubidentifierEnd(e, t) {
244
+ for (let r = t; r < e.length; r += 1) {
245
+ if (!((e[r] ?? 0) & 128)) return r;
246
+ r - t + 1 >= 64 && r + 1 < e.length && n("limit_exceeded", "OID sub-identifier exceeds 64 octets");
206
247
  }
207
248
  throw Error("Malformed OID: incomplete continuation");
208
249
  }
@@ -212,6 +253,7 @@ function decodeOidSubidentifier(e, t) {
212
253
  * Supports:
213
254
  * - UTF8String (`0x0c`),
214
255
  * - PrintableString (`0x13`),
256
+ * - TeletexString (`0x14`) in its X.690 §8.23.5.2 initial state,
215
257
  * - IA5String (`0x16`),
216
258
  * - UniversalString (`0x1c`), and
217
259
  * - BMPString (`0x1e`).
@@ -223,7 +265,7 @@ function decodeString(e, t) {
223
265
  case 12: return decodeUtf8Text(t, "UTF8String");
224
266
  case 19: return decodePrintableString(t);
225
267
  case 22: return decodeIa5String(t);
226
- case 20: throw Error("Unsupported string tag: 20 (TeletexString)");
268
+ case 20: return decodeTeletexString(t);
227
269
  case 28: return decodeUniversalString(t);
228
270
  case 30: return decodeBmpString(t);
229
271
  default: throw Error(`Unsupported string tag: ${e}`);
@@ -231,7 +273,7 @@ function decodeString(e, t) {
231
273
  }
232
274
  function decodeUtf8Text(e, t) {
233
275
  try {
234
- return n.decode(e);
276
+ return o.decode(e);
235
277
  } catch (e) {
236
278
  throw e instanceof Error ? Error(`Invalid ${t}: invalid UTF-8`, { cause: e }) : e;
237
279
  }
@@ -249,9 +291,44 @@ function decodeIa5String(e) {
249
291
  }
250
292
  function decodePrintableString(e) {
251
293
  let t = decodeAsciiString(e, "PrintableString");
252
- if (!r.test(t)) throw Error("Invalid PrintableString: contains characters outside the allowed set");
294
+ if (!s.test(t)) throw Error("Invalid PrintableString: contains characters outside the allowed set");
295
+ return t;
296
+ }
297
+ /**
298
+ * Decodes VisibleString contents, which X.680 §43.7 limits to 0x20 through 0x7E.
299
+ *
300
+ * @throws on an octet outside that range.
301
+ */
302
+ function decodeVisibleString(e) {
303
+ let t = "";
304
+ for (let n of e) {
305
+ if (n < 32 || n > 126) throw Error("Invalid VisibleString: contains octets outside 0x20 to 0x7e");
306
+ t += String.fromCharCode(n);
307
+ }
253
308
  return t;
254
309
  }
310
+ /** T.61 Table 1 leaves these positions of register entry 102 empty. */
311
+ const c = /* @__PURE__ */ new Set([
312
+ 92,
313
+ 94,
314
+ 96,
315
+ 123,
316
+ 125,
317
+ 126
318
+ ]);
319
+ /**
320
+ * Decodes TeletexString contents in the X.690 §8.23.5.2 initial state, reading
321
+ * register entry 102 by T.61 Table 1 and Figure 2 Note 4, with the SPACE and
322
+ * DELETE that X.680 Table 8 adds.
323
+ *
324
+ * @throws `unsupported` on any other octet.
325
+ */
326
+ function decodeTeletexString(e) {
327
+ let t = "";
328
+ for (let r of e) (r < 32 || r > 127 || c.has(r)) && n("unsupported", `Unsupported TeletexString octet: 0x${r.toString(16).padStart(2, "0")}`), t += r === 36 ? "¤" : String.fromCharCode(r);
329
+ return t;
330
+ }
331
+ /** X.680 §41.15: BMPString holds code units 0000 to D7FF and E000 to FFFD. */
255
332
  function decodeBmpString(e) {
256
333
  if (e.length % 2 != 0) throw Error("Invalid BMPString length");
257
334
  let t = "";
@@ -259,7 +336,7 @@ function decodeBmpString(e) {
259
336
  let r = e[n], i = e[n + 1];
260
337
  if (r === void 0 || i === void 0) throw Error("Invalid BMPString content");
261
338
  let a = r << 8 | i;
262
- if (a >= 55296 && a <= 57343) throw Error("Invalid BMPString code point");
339
+ if (a >= 55296 && a <= 57343 || a >= 65534) throw Error("Invalid BMPString code point");
263
340
  t += String.fromCharCode(a);
264
341
  }
265
342
  return t;
@@ -276,7 +353,230 @@ function decodeUniversalString(e) {
276
353
  }
277
354
  return t;
278
355
  }
279
- //#endregion
280
- export { canonicalizeOid, childrenOf, decodeBitString, decodeBoolean, decodeIntegerNumber, decodeNonNegativeIntegerNumber, decodeObjectIdentifier, decodeString, extractBitStringValue, hexToBytes, parseTime, requireElement, toArrayBuffer, toHex };
356
+ /**
357
+ * Walks {@linkcode bytes} as one DER element and checks each universal-class
358
+ * element against the X.690 rules its tag fixes: the identifier form, the
359
+ * contents, and the DER restrictions of clauses 10 and 11.
360
+ *
361
+ * Where those rules rest on ISO/IEC 2022, ISO 8601 or an implicitly tagged
362
+ * schema, the element is `unsupported`: TeletexString, VideotexString, TIME,
363
+ * EXTERNAL, EMBEDDED PDV, CHARACTER STRING, the types of UNIVERSAL 31 to 36, a
364
+ * REAL with a long-form exponent, a GeneralizedTime at second 60, and text
365
+ * holding an escape sequence or a code-extension control. A SET or SET OF
366
+ * whose children follow neither the DER SET order nor the DER SET OF order is
367
+ * `malformed`. Context-specific, application and private elements get framing
368
+ * checks only, as do the rules that depend on a schema: DEFAULT omission and
369
+ * NamedBitList trailing bits. No value is checked against an ASN.1 type, so
370
+ * ENUMERATED membership, INTEGER ranges, SIZE constraints, CHOICE alternatives
371
+ * and SEQUENCE or SET components stay unchecked.
372
+ *
373
+ * @throws `limit_exceeded` past {@linkcode maxDepth} levels of nesting or on a
374
+ * tag number of 2^53 or more.
375
+ */
376
+ function checkStrictDer(n, r = 64) {
377
+ let i = !1;
378
+ try {
379
+ a(n, r, void 0, (e) => {
380
+ let t = checkUniversalElement(e);
381
+ if (t === "malformed") throw Error("DER element breaks the rules of its universal type");
382
+ i ||= t === "unsupported";
383
+ });
384
+ } catch (n) {
385
+ return t(n, e), "malformed";
386
+ }
387
+ return i ? "unsupported" : "valid";
388
+ }
389
+ function verdictOf(e) {
390
+ return e ? "valid" : "malformed";
391
+ }
392
+ function decodes(e) {
393
+ try {
394
+ return e(), "valid";
395
+ } catch {
396
+ return "malformed";
397
+ }
398
+ }
399
+ function asciiText(e) {
400
+ try {
401
+ return decodeIa5String(e);
402
+ } catch {
403
+ return;
404
+ }
405
+ }
406
+ const l = /* @__PURE__ */ new Set([27]), u = /* @__PURE__ */ new Set([
407
+ 14,
408
+ 15,
409
+ 27,
410
+ 142,
411
+ 143,
412
+ 155
413
+ ]);
414
+ /** The verdict on text {@linkcode decode} validates, `unsupported` when it holds one of {@linkcode controls}. */
415
+ function checkText(e, t) {
416
+ let n;
417
+ try {
418
+ n = e();
419
+ } catch {
420
+ return "malformed";
421
+ }
422
+ for (let e of n) if (t.has(e.codePointAt(0) ?? 0)) return "unsupported";
423
+ return "valid";
424
+ }
425
+ /** X.690 Table 3: GraphicString and ObjectDescriptor open with register entry 6 as G0 and no C0 or C1 set. */
426
+ function checkGraphicString(e) {
427
+ return e.value.includes(27) ? "unsupported" : verdictOf(e.value.every((e) => e >= 32 && e <= 126));
428
+ }
429
+ /** X.690 Table 3: GeneralString opens with register entry 6 as G0 and entry 1 as C0, beside SPACE and DELETE. */
430
+ function checkGeneralString(e) {
431
+ return e.value.some((e) => e === 27 || e === 14 || e === 15) ? "unsupported" : verdictOf(e.value.every((e) => e <= 127));
432
+ }
433
+ /** X.690 §8.3.2: the first nine bits of a multi-octet INTEGER are neither all ones nor all zeros. */
434
+ function isMinimalInteger(e) {
435
+ let [t, n] = e;
436
+ return t === void 0 ? !1 : n === void 0 || (t !== 0 || n >= 128) && (t !== 255 || n < 128);
437
+ }
438
+ /** X.690 §8.19.2: every subidentifier ends on an octet with bit 8 clear and never opens with 0x80. */
439
+ function isSubidentifierList(e) {
440
+ let t = !0;
441
+ for (let n of e) {
442
+ if (t && n === 128) return !1;
443
+ t = !(n & 128);
444
+ }
445
+ return t;
446
+ }
447
+ function checkBitString(e) {
448
+ try {
449
+ return verdictOf(!decodeBitString(e).nonZeroPadding);
450
+ } catch {
451
+ return "malformed";
452
+ }
453
+ }
454
+ /** X.680 Table 9: NumericString holds the digits and SPACE. */
455
+ function isNumericStringContents(e) {
456
+ return e.every((e) => e === 32 || e >= 48 && e <= 57);
457
+ }
458
+ function isLeapYear(e) {
459
+ return e % 4 == 0 && e % 100 != 0 || e % 400 == 0;
460
+ }
461
+ const d = [
462
+ 31,
463
+ 28,
464
+ 31,
465
+ 30,
466
+ 31,
467
+ 30,
468
+ 31,
469
+ 31,
470
+ 30,
471
+ 31,
472
+ 30,
473
+ 31
474
+ ], f = {
475
+ pattern: /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/,
476
+ century: 2e3,
477
+ leapSecond: "malformed"
478
+ }, p = {
479
+ pattern: /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(?:\.\d*[1-9])?Z$/,
480
+ century: 0,
481
+ leapSecond: "unsupported"
482
+ };
483
+ function checkTime(e, t) {
484
+ let n = t.pattern.exec(asciiText(e.value) ?? "")?.slice(1).map(Number);
485
+ if (n === void 0) return "malformed";
486
+ let [r = 0, i = 0, a = 0, o = 0, s = 0, c = 0] = n, l = (d[i - 1] ?? 0) + (i === 2 && isLeapYear(t.century + r) ? 1 : 0);
487
+ return a < 1 || a > l || o > 23 || s > 59 || c > 60 ? "malformed" : c === 60 ? t.leapSecond : "valid";
488
+ }
489
+ /**
490
+ * X.690 §11.3.2: NR3 without SPACE, a MINUS SIGN only on a negative value, a
491
+ * mantissa that neither opens nor closes with 0 followed by `.E`, and an
492
+ * exponent written `+0` or with no leading 0 and no PLUS SIGN.
493
+ */
494
+ const m = /^-?[1-9](?:\d*[1-9])?\.E(?:\+0|-?[1-9]\d*)$/;
495
+ /**
496
+ * X.690 §8.5 and §11.3: an empty plus zero, a one-octet special value, a
497
+ * base-2 binary form with F = 0, the fewest exponent octets and an odd
498
+ * mantissa with no leading zero octet, or a decimal NR3 form. X.690 §8.5.7.4
499
+ * d) leaves the length of a long-form exponent ambiguous.
500
+ */
501
+ function checkReal(e) {
502
+ let [t] = e.value, n = e.value.subarray(1);
503
+ if (t === void 0) return "valid";
504
+ if (!(t & 128)) return t & 64 ? verdictOf(n.length === 0 && t <= 67) : verdictOf(t === 3 && m.test(asciiText(n) ?? ""));
505
+ let r = t & 3;
506
+ if (t & 60) return "malformed";
507
+ if (r === 3) return "unsupported";
508
+ let i = n.subarray(0, r + 1), a = n.subarray(r + 1);
509
+ return verdictOf(i.length === r + 1 && isMinimalInteger(i) && (a[0] ?? 0) !== 0 && ((a.at(-1) ?? 0) & 1) == 1);
510
+ }
511
+ /** X.690 §11.6: set-of encodings compare as octet strings, the shorter padded at its end with zero octets. */
512
+ function compareSetOfEncodings(e, t) {
513
+ for (let n = 0; n < Math.max(e.length, t.length); n += 1) {
514
+ let r = (e[n] ?? 0) - (t[n] ?? 0);
515
+ if (r !== 0) return r;
516
+ }
517
+ return 0;
518
+ }
519
+ /**
520
+ * X.690 §10.3 orders SET components by the canonical tag order of X.680 §8.6,
521
+ * which compares class and number and never the constructed bit, and X.690
522
+ * §11.6 orders SET OF components by encoding.
523
+ */
524
+ function checkSet(e) {
525
+ let t = [];
526
+ for (let n = 0; n < e.value.length;) {
527
+ let r = i(e.value, n);
528
+ t.push(r), n = r.end;
529
+ }
530
+ let encodingOf = (t) => e.value.subarray(t.start - t.headerLength, t.end), n = t.every((e, n) => {
531
+ let r = t[n - 1];
532
+ if (r === void 0) return !0;
533
+ let i = r.tag & 192, a = e.tag & 192;
534
+ return i < a || i === a && r.tagNumber < e.tagNumber;
535
+ }), r = t.every((e, n) => {
536
+ let r = t[n - 1];
537
+ return r === void 0 || compareSetOfEncodings(encodingOf(r), encodingOf(e)) <= 0;
538
+ });
539
+ return verdictOf(n || r);
540
+ }
541
+ /**
542
+ * The identifier octet DER uses for each universal type, mapped to the check
543
+ * of its contents. A universal identifier missing here is a reserved tag
544
+ * number or a form DER forbids.
545
+ */
546
+ const h = /* @__PURE__ */ new Map([
547
+ [1, (e) => decodes(() => decodeBoolean(e.value))],
548
+ [2, (e) => verdictOf(isMinimalInteger(e.value))],
549
+ [3, checkBitString],
550
+ [4, () => "valid"],
551
+ [5, (e) => verdictOf(e.value.length === 0)],
552
+ [6, (e) => verdictOf(e.value.length > 0 && isSubidentifierList(e.value))],
553
+ [7, checkGraphicString],
554
+ [9, checkReal],
555
+ [10, (e) => verdictOf(isMinimalInteger(e.value))],
556
+ [12, (e) => checkText(() => decodeUtf8Text(e.value, "UTF8String"), l)],
557
+ [13, (e) => verdictOf(e.value.length > 0 && isSubidentifierList(e.value))],
558
+ [14, () => "unsupported"],
559
+ [18, (e) => verdictOf(isNumericStringContents(e.value))],
560
+ [19, (e) => decodes(() => decodePrintableString(e.value))],
561
+ [20, () => "unsupported"],
562
+ [21, () => "unsupported"],
563
+ [22, (e) => checkText(() => decodeIa5String(e.value), l)],
564
+ [23, (e) => checkTime(e, f)],
565
+ [24, (e) => checkTime(e, p)],
566
+ [25, checkGraphicString],
567
+ [26, (e) => decodes(() => decodeVisibleString(e.value))],
568
+ [27, checkGeneralString],
569
+ [28, (e) => checkText(() => decodeUniversalString(e.value), u)],
570
+ [30, (e) => checkText(() => decodeBmpString(e.value), u)],
571
+ [40, () => "unsupported"],
572
+ [43, () => "unsupported"],
573
+ [48, () => "valid"],
574
+ [49, checkSet],
575
+ [61, () => "unsupported"]
576
+ ]);
577
+ function checkUniversalElement(e) {
578
+ return e.tag & 192 ? "valid" : (e.tag & 31) == 31 ? e.tagNumber <= 36 ? "unsupported" : "malformed" : h.get(e.tag)?.(e) ?? "malformed";
579
+ }
580
+ export { canonicalizeOid, checkStrictDer, childrenOf, decodeBitString, decodeBoolean, decodeIntegerMagnitude, decodeIntegerNumber, decodeNonNegativeIntegerBigInt, decodeNonNegativeIntegerNumber, decodeObjectIdentifier, decodeString, decodeVisibleString, extractBitStringValue, hexToBytes, isNumericStringContents, parseTime, requireElement, toArrayBuffer, toHex };
281
581
 
282
582
  //# sourceMappingURL=asn1.js.map