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,9 @@
1
- import { toHex as e } from "../asn1/asn1.js";
2
- import { canonicalDnKey as t, compareDistinguishedNames as n } from "../shared/dn.js";
3
- import { parseCertificatesFromSource as r } from "../../x509/parse.js";
4
- import { verifySignedDataDetailed as i } from "../crypto/sig-verify.js";
5
- //#region src/internal/verify/verify-path.ts
1
+ import { DECODE_LIMIT_CODES as e, decodeRefusalOf as t } from "../asn1/decode-refusal.js";
2
+ import { toHex as n } from "../asn1/asn1.js";
3
+ import { verifySignedDataDetailed as r } from "../crypto/sig-verify.js";
4
+ import { canonicalDnKey as i, compareDistinguishedNames as a } from "../shared/dn.js";
5
+ import { parseCertificatesFromSource as o } from "../../x509/parse.js";
6
+ // src/internal/verify/verify-path.ts
6
7
  /**
7
8
  * Internal certificate path-building and signature-check helpers.
8
9
  *
@@ -12,13 +13,19 @@ import { verifySignedDataDetailed as i } from "../crypto/sig-verify.js";
12
13
  *
13
14
  * @module
14
15
  */
16
+ /** Default upper bound on issuer candidates and trust anchors one path search may try. */
17
+ const s = 1e5;
18
+ /** Throws when a caller-supplied path-building limit is not a positive safe integer. */
19
+ function assertPathBuildingChecks(e) {
20
+ if (e !== void 0 && (!Number.isSafeInteger(e) || e < 1)) throw RangeError(`Invalid maxPathBuildingChecks: must be an integer >= 1, got ${e}`);
21
+ }
15
22
  /** Returns `true` if `at` falls within the certificate's notBefore/notAfter window (inclusive). */
16
23
  function isWithinValidity(e, t) {
17
24
  return e.notBefore.getTime() <= t.getTime() && t.getTime() <= e.notAfter.getTime();
18
25
  }
19
26
  /** Returns `true` if the certificate's subject and issuer DNs are semantically equal (RFC 5280 §7.1). */
20
27
  function isSelfIssued(e) {
21
- return n(e.subject, e.issuer);
28
+ return a(e.subject, e.issuer);
22
29
  }
23
30
  /** Counts non-self-issued CA certificates in positions 0..(index-1) for pathLength checking. */
24
31
  function countCaCertificatesBelowParsed(e, t) {
@@ -43,13 +50,76 @@ function loadSingleCertificate(e) {
43
50
  return n;
44
51
  }
45
52
  /** Verifies that `certificate` was signed by `issuer`'s public key. */
46
- async function verifyCertificateSignature(e, t) {
47
- let n = await i(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signatureValue, e.tbsCertificateDer);
48
- return !n.ok && n.code === "verification_error" ? {
53
+ function verifyCertificateSignature(e, t) {
54
+ return signatureCheck(() => r(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signatureValue, e.tbsCertificateDer));
55
+ }
56
+ /** Runs `verify` as a path check, reporting a runtime failure as `signature_invalid` and a decoding limit as `limit_exceeded`. */
57
+ async function signatureCheck(n) {
58
+ let r;
59
+ try {
60
+ r = await n();
61
+ } catch (n) {
62
+ let r = t(n, e);
63
+ if (r === void 0) throw n;
64
+ return {
65
+ ok: !1,
66
+ code: r.code,
67
+ reason: r.message
68
+ };
69
+ }
70
+ return !r.ok && r.code === "verification_error" ? {
49
71
  ok: !1,
50
72
  code: "signature_invalid",
51
- reason: n.reason
52
- } : n;
73
+ reason: r.reason
74
+ } : r;
75
+ }
76
+ /** The real signature checks, used unless a caller injects its own. */
77
+ const c = {
78
+ certificate: verifyCertificateSignature,
79
+ trustAnchor: verifyTrustAnchorSignature
80
+ }, l = {
81
+ "specific-failure": 3,
82
+ "untrusted-anchor": 2,
83
+ "missing-issuer": 1,
84
+ none: 0
85
+ };
86
+ function isMoreSignificant(e, t) {
87
+ let n = l[e.kind], r = l[t.kind];
88
+ return n === r ? e.path.length > t.path.length : n > r;
89
+ }
90
+ /**
91
+ * Moves a diagnostic recorded below the certificate at `nodeIndex` onto a new
92
+ * prefix ending at that same certificate. A suffix that repeats a certificate
93
+ * of the new prefix cannot form a path, so only the prefix is reported.
94
+ */
95
+ function rebaseDiagnostic(e, t, n) {
96
+ let r = e.path.slice(t + 1), i = new Set(n.map((e) => fingerprint(e)));
97
+ if (r.some((e) => i.has(fingerprint(e)))) return {
98
+ kind: "none",
99
+ path: n
100
+ };
101
+ let a = [...n, ...r], o = n.length - 1 - t;
102
+ switch (e.kind) {
103
+ case "none":
104
+ case "untrusted-anchor": return {
105
+ kind: e.kind,
106
+ path: a
107
+ };
108
+ case "missing-issuer": return {
109
+ kind: "missing-issuer",
110
+ path: a,
111
+ missingIssuerAt: e.missingIssuerAt + o
112
+ };
113
+ case "specific-failure": return {
114
+ kind: "specific-failure",
115
+ path: a,
116
+ failure: e.failure.index === void 0 ? e.failure : {
117
+ ...e.failure,
118
+ index: e.failure.index + o
119
+ }
120
+ };
121
+ default: throw Error(`unreachable diagnostic ${String(e)}`);
122
+ }
53
123
  }
54
124
  /**
55
125
  * Depth-first chain search from leaf to root. Tries all issuer candidates,
@@ -57,93 +127,167 @@ async function verifyCertificateSignature(e, t) {
57
127
  * step. Returns the first fully anchored path or the deepest partial path
58
128
  * with the best failure encountered.
59
129
  */
60
- async function buildChainInternal(e, n, r, i, a, o) {
61
- let s = [...n, ...r], c = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), u = new Set(r.map((e) => fingerprint(e))), d = /* @__PURE__ */ new Map();
62
- for (let e of i) {
63
- let n = t(e.subject), r = d.get(n);
64
- r === void 0 ? d.set(n, [e]) : r.push(e);
130
+ async function buildChainInternal(e, t, r, a, o, l, u = c, d = s) {
131
+ let f = [...t, ...r], p = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new Map(), h = new Set(r.map((e) => fingerprint(e))), g = /* @__PURE__ */ new Map();
132
+ for (let e of a) {
133
+ let t = i(e.subject), n = g.get(t);
134
+ n === void 0 ? g.set(t, [e]) : n.push(e);
65
135
  }
66
- let f = !1, p = [e], m, h, g = /* @__PURE__ */ new Set();
67
- s.forEach((e, n) => {
68
- let r = t(e.subject), i = c.get(r);
69
- i === void 0 ? c.set(r, [e]) : i.push(e), l.set(fingerprint(e), n);
136
+ let _ = { current: {
137
+ kind: "none",
138
+ path: [e]
139
+ } }, v = /* @__PURE__ */ new Map(), y = [], b = /* @__PURE__ */ new Map(), x = {
140
+ remaining: d,
141
+ exhausted: !1
142
+ }, S = /* @__PURE__ */ new Map();
143
+ f.forEach((e, t) => {
144
+ let n = i(e.subject), r = p.get(n);
145
+ r === void 0 ? p.set(n, [e]) : r.push(e), m.set(fingerprint(e), t);
70
146
  });
71
- let _ = s.length + 1, v = fingerprint(e), y = await search(e, [e], /* @__PURE__ */ new Set([v]), 0);
72
- if (y !== void 0) {
73
- let e = y[y.length - 1];
147
+ let C = f.length + 1, w = fingerprint(e), T = await search(e, [e], /* @__PURE__ */ new Set([w]), 0);
148
+ if (T !== void 0) {
149
+ let e = T[T.length - 1];
74
150
  return {
75
- chain: y,
151
+ chain: T,
76
152
  foundTrustedRoot: !0,
77
- anchorCertificateInChain: e !== void 0 && u.has(fingerprint(e))
153
+ anchorCertificateInChain: e !== void 0 && h.has(fingerprint(e))
78
154
  };
79
155
  }
80
- if (h !== void 0) return {
81
- chain: p,
82
- foundTrustedRoot: !1,
83
- failure: h
84
- };
85
- if (f) return {
86
- chain: p,
87
- foundTrustedRoot: !1
88
- };
89
- return m === void 0 ? {
90
- chain: p,
91
- foundTrustedRoot: !1
92
- } : {
93
- chain: p,
156
+ let E = _.current;
157
+ if (x.exhausted) return {
158
+ chain: E.path,
94
159
  foundTrustedRoot: !1,
95
- missingIssuerAt: m
160
+ failure: l.failure("path_building_limit_exceeded", "path building stopped at its work limit", void 0, l.detail({
161
+ expected: `at most ${d} issuer and trust-anchor checks`,
162
+ actual: "limit reached before a trusted path was found"
163
+ }))
96
164
  };
97
- async function search(e, n, r, i) {
98
- if (u.has(fingerprint(e))) return n;
99
- let a = await matchTrustAnchor(e, d, o, n.length - 1);
100
- if (a.failure !== void 0 && recordFailure(a.failure, n), a.matched) return n;
101
- if (n.length > _) return;
102
- let s = [...r].sort().join(","), f = `${fingerprint(e)}:${i}:${s}`;
103
- if (g.has(f)) return;
104
- let p = rankIssuerCandidates(e, c.get(t(e.issuer)) ?? [], l, u);
105
- if (p.length === 0) {
106
- recordMissingIssuers(e, n), g.add(f);
165
+ switch (E.kind) {
166
+ case "specific-failure": return {
167
+ chain: E.path,
168
+ foundTrustedRoot: !1,
169
+ failure: E.failure
170
+ };
171
+ case "missing-issuer": return {
172
+ chain: E.path,
173
+ foundTrustedRoot: !1,
174
+ missingIssuerAt: E.missingIssuerAt
175
+ };
176
+ case "untrusted-anchor":
177
+ case "none": return {
178
+ chain: E.path,
179
+ foundTrustedRoot: !1
180
+ };
181
+ default: throw Error(`unreachable diagnostic ${String(E)}`);
182
+ }
183
+ async function search(e, t, n, r) {
184
+ if (x.exhausted) return;
185
+ if (h.has(fingerprint(e))) return t;
186
+ let i = await matchTrustAnchor(e, g, l, t.length - 1, verifyAnchorOnce, chargeBudget);
187
+ if (x.exhausted) return;
188
+ if (i.failure !== void 0 && recordFailure(i.failure, t), i.matched) return t;
189
+ if (t.length > C) return;
190
+ let a = `${fingerprint(e)}:${r}`, o = v.get(a);
191
+ if (o !== void 0) {
192
+ consider(rebaseDiagnostic(o.diagnostic, o.nodeIndex, t));
107
193
  return;
108
194
  }
109
- let m = await searchIssuerCandidates(e, p, n, r, i);
110
- if (m !== void 0) return m;
111
- g.add(f), updateDeepest(n);
195
+ let s = { best: {
196
+ kind: "none",
197
+ path: t
198
+ } };
199
+ y.push(s);
200
+ try {
201
+ let i = await searchBelow(e, t, n, r);
202
+ if (i !== void 0) return i;
203
+ } finally {
204
+ y.pop();
205
+ }
206
+ v.set(a, {
207
+ diagnostic: s.best,
208
+ nodeIndex: t.length - 1
209
+ });
112
210
  }
113
- function recordMissingIssuers(e, t) {
114
- isSelfIssued(e) ? (updateDeepest(t), f = !0) : updateDeepest(t) && (m = t.length - 1);
211
+ async function searchBelow(e, t, n, r) {
212
+ let i = !1;
213
+ for (let a of issuerCandidates(e)) {
214
+ if (!chargeBudget()) return;
215
+ if (!isIssuerOf(a, e)) continue;
216
+ i = !0;
217
+ let o = fingerprint(a);
218
+ if (n.has(o)) continue;
219
+ let s = await searchIssuerCandidate(e, a, o, t, n, r);
220
+ if (s !== void 0) return s;
221
+ if (x.exhausted) return;
222
+ }
223
+ i ? consider({
224
+ kind: "none",
225
+ path: t
226
+ }) : recordMissingIssuers(e, t);
115
227
  }
116
- /** Searches ranked issuer candidates until one produces a trusted path. */
117
- async function searchIssuerCandidates(e, t, n, r, i) {
118
- for (let a of t) {
119
- let t = await searchIssuerCandidate(e, a, n, r, i);
120
- if (t !== void 0) return t;
228
+ /** Yields the issuer bucket for `current`: AKI matches first, then roots, then input order. */
229
+ function* issuerCandidates(e) {
230
+ let t = i(e.issuer), n = S.get(t);
231
+ n === void 0 && (n = rankBucket(p.get(t) ?? [], m, h), S.set(t, n));
232
+ let r = e.authorityKeyIdentifier;
233
+ if (r === void 0) yield* n.ordered;
234
+ else {
235
+ yield* n.bySubjectKeyIdentifier.get(r) ?? [];
236
+ for (let e of n.ordered) e.subjectKeyIdentifier !== r && (yield e);
121
237
  }
122
238
  }
123
- async function searchIssuerCandidate(e, t, n, r, i) {
124
- let s = fingerprint(t);
125
- if (r.has(s)) return;
126
- let c = await evaluateIssuerCandidate(e, t, n, i, a, o);
127
- if (!c.ok) {
128
- recordFailure(c.failure, n);
239
+ function recordMissingIssuers(e, t) {
240
+ isSelfIssued(e) ? consider({
241
+ kind: "untrusted-anchor",
242
+ path: t
243
+ }) : t.length > 1 && consider({
244
+ kind: "missing-issuer",
245
+ path: t,
246
+ missingIssuerAt: t.length - 1
247
+ });
248
+ }
249
+ async function searchIssuerCandidate(e, t, n, r, i, a) {
250
+ let s = await evaluateIssuerCandidate(e, t, r, a, o, l, verifySignatureOnce);
251
+ if (!s.ok) {
252
+ recordFailure(s.failure, r);
129
253
  return;
130
254
  }
131
- let l = new Set(r);
132
- return l.add(s), await search(t, [...n, t], l, c.nextCaBelowCount);
255
+ let c = new Set(i);
256
+ return c.add(n), await search(t, [...r, t], c, s.nextCaBelowCount);
257
+ }
258
+ function chargeBudget() {
259
+ return x.remaining === 0 ? (x.exhausted = !0, !1) : (--x.remaining, !0);
260
+ }
261
+ function verifySignatureOnce(e, t) {
262
+ return verifyOnce(`c:${fingerprint(e)}:${n(t.subjectPublicKeyInfoDer)}`, () => u.certificate(e, t));
263
+ }
264
+ function verifyAnchorOnce(e, t) {
265
+ let r = `${t.publicKeyAlgorithmOid}:${t.publicKeyParametersOid ?? ""}:${n(t.subjectPublicKeyInfoDer)}`;
266
+ return verifyOnce(`a:${fingerprint(e)}:${r}`, () => u.trustAnchor(e, t));
267
+ }
268
+ /** One signature check per distinct certificate-and-key pair for the whole search. */
269
+ function verifyOnce(e, t) {
270
+ let n = b.get(e);
271
+ return n === void 0 && (n = t(), b.set(e, n)), n;
133
272
  }
134
- function updateDeepest(e) {
135
- return e.length > p.length ? (p = e, !0) : !1;
273
+ function consider(e) {
274
+ isMoreSignificant(e, _.current) && (_.current = e);
275
+ for (let t of y) isMoreSignificant(e, t.best) && (t.best = e);
136
276
  }
137
277
  function recordFailure(e, t) {
138
- (h === void 0 || t.length > p.length) && (h = e, p = t);
278
+ consider({
279
+ kind: "specific-failure",
280
+ path: t,
281
+ failure: e
282
+ });
139
283
  }
140
284
  }
141
285
  /** Applies issuer constraints and signature verification to one path candidate. */
142
- async function evaluateIssuerCandidate(e, t, n, r, i, a) {
143
- let o = evaluateIssuerConstraints(e, t, n, r, i, a);
144
- if (!o.ok) return o;
145
- let s = await verifyCertificateSignature(e, t);
146
- return s.ok ? s.valid ? o : {
286
+ async function evaluateIssuerCandidate(e, t, n, r, i, a, o) {
287
+ let s = evaluateIssuerConstraints(e, t, n, r, i, a);
288
+ if (!s.ok) return s;
289
+ let c = await o(e, t);
290
+ return c.ok ? c.valid ? s : {
147
291
  ok: !1,
148
292
  failure: a.failure("signature_invalid", "certificate signature does not verify", n.length - 1, a.detail({
149
293
  subjectCommonName: e.subject.values.commonName,
@@ -151,10 +295,10 @@ async function evaluateIssuerCandidate(e, t, n, r, i, a) {
151
295
  }))
152
296
  } : {
153
297
  ok: !1,
154
- failure: a.failure(s.code, s.reason, n.length - 1, a.detail({
298
+ failure: a.failure(c.code, c.reason, n.length - 1, a.detail({
155
299
  subjectCommonName: e.subject.values.commonName,
156
300
  issuerCommonName: t.subject.values.commonName,
157
- actual: s.reason
301
+ actual: c.reason
158
302
  }))
159
303
  };
160
304
  }
@@ -203,22 +347,24 @@ function authorityKeyIdentifierMismatch(e, t) {
203
347
  }
204
348
  /** Expands a PEM (possibly multi-block) or DER source into parsed certificates. */
205
349
  function expandSource(e) {
206
- return r(e);
350
+ return o(e);
207
351
  }
208
- /** Filters and sorts issuer candidates: AKI match first, then roots, then input order. */
209
- function rankIssuerCandidates(e, t, n, r) {
210
- let i = e.authorityKeyIdentifier, a = [...t].filter((t) => isIssuerOf(t, e)), o = /* @__PURE__ */ new Map();
211
- for (let e of a) o.set(e, fingerprint(e));
212
- return a.sort((e, t) => {
213
- let a = compareBooleans(matchesAki(e, i), matchesAki(t, i));
214
- if (a !== 0) return a;
215
- let s = o.get(e) ?? "", c = o.get(t) ?? "", l = compareBooleans(r.has(s), r.has(c));
216
- return l === 0 ? (n.get(s) ?? 2 ** 53 - 1) - (n.get(c) ?? 2 ** 53 - 1) : l;
217
- });
218
- }
219
- /** Returns `true` if the candidate's SKI matches the given authority key identifier. */
220
- function matchesAki(e, t) {
221
- return t !== void 0 && e.subjectKeyIdentifier !== void 0 && e.subjectKeyIdentifier === t;
352
+ /** Orders one subject bucket once, roots first and then input order, and indexes it by SKI. */
353
+ function rankBucket(e, t, n) {
354
+ let r = [...e].sort((e, r) => {
355
+ let i = fingerprint(e), a = fingerprint(r), o = compareBooleans(n.has(i), n.has(a));
356
+ return o === 0 ? (t.get(i) ?? 2 ** 53 - 1) - (t.get(a) ?? 2 ** 53 - 1) : o;
357
+ }), i = /* @__PURE__ */ new Map();
358
+ for (let e of r) {
359
+ let t = e.subjectKeyIdentifier;
360
+ if (t === void 0) continue;
361
+ let n = i.get(t);
362
+ n === void 0 ? i.set(t, [e]) : n.push(e);
363
+ }
364
+ return {
365
+ ordered: r,
366
+ bySubjectKeyIdentifier: i
367
+ };
222
368
  }
223
369
  /** Sort comparator: `true` sorts before `false`. Returns -1, 0, or 1. */
224
370
  function compareBooleans(e, t) {
@@ -226,47 +372,38 @@ function compareBooleans(e, t) {
226
372
  }
227
373
  /** Returns `true` if `issuer`'s subject DN semantically matches `child`'s issuer DN (RFC 5280 §7.1). */
228
374
  function isIssuerOf(e, t) {
229
- return n(t.issuer, e.subject);
375
+ return a(t.issuer, e.subject);
230
376
  }
231
377
  /** Attempts to verify `certificate` against each bare trust anchor whose subject DN matches. */
232
- async function matchTrustAnchor(e, r, i, a) {
233
- let o = r.get(t(e.issuer));
234
- if (o === void 0) return { matched: !1 };
235
- let s;
236
- for (let t of o) {
237
- if (trustAnchorAkiMismatch(e, t) || !n(e.issuer, t.subject)) continue;
238
- let r = await verifyTrustAnchorSignature(e, t);
239
- if (!r.ok) {
240
- s === void 0 && (s = i.failure(r.code, r.reason, a, i.detail({
241
- subjectCommonName: e.subject.values.commonName,
242
- actual: r.reason
243
- })));
244
- continue;
245
- }
246
- if (!r.valid) {
247
- s === void 0 && (s = i.failure("signature_invalid", "certificate signature does not verify", a, i.detail({
248
- subjectCommonName: e.subject.values.commonName,
249
- actual: "certificate signature does not verify"
250
- })));
251
- continue;
252
- }
253
- return { matched: !0 };
378
+ async function matchTrustAnchor(e, t, n, r, o, s) {
379
+ let c = t.get(i(e.issuer));
380
+ if (c === void 0) return { matched: !1 };
381
+ let l;
382
+ for (let t of c) {
383
+ if (!s()) return { matched: !1 };
384
+ if (trustAnchorAkiMismatch(e, t) || !a(e.issuer, t.subject)) continue;
385
+ let i = await o(e, t);
386
+ if (!i.ok) l === void 0 && (l = n.failure(i.code, i.reason, r, n.detail({
387
+ subjectCommonName: e.subject.values.commonName,
388
+ actual: i.reason
389
+ })));
390
+ else if (!i.valid) l === void 0 && (l = n.failure("signature_invalid", "certificate signature does not verify", r, n.detail({
391
+ subjectCommonName: e.subject.values.commonName,
392
+ actual: "certificate signature does not verify"
393
+ })));
394
+ else return { matched: !0 };
254
395
  }
255
- return s === void 0 ? { matched: !1 } : {
396
+ return l === void 0 ? { matched: !1 } : {
256
397
  matched: !1,
257
- failure: s
398
+ failure: l
258
399
  };
259
400
  }
260
401
  function trustAnchorAkiMismatch(e, t) {
261
402
  return t.subjectKeyIdentifier !== void 0 && e.authorityKeyIdentifier !== void 0 && t.subjectKeyIdentifier !== e.authorityKeyIdentifier;
262
403
  }
263
- async function verifyTrustAnchorSignature(e, t) {
264
- let n = await i(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signatureValue, e.tbsCertificateDer);
265
- return !n.ok && n.code === "verification_error" ? {
266
- ok: !1,
267
- code: "signature_invalid",
268
- reason: n.reason
269
- } : n;
404
+ /** Verifies that `certificate` was signed by a bare trust anchor's key. */
405
+ function verifyTrustAnchorSignature(e, t) {
406
+ return signatureCheck(() => r(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signatureValue, e.tbsCertificateDer));
270
407
  }
271
408
  function describeDateTime(e) {
272
409
  return Number.isNaN(e.getTime()) ? "<invalid date>" : e.toISOString();
@@ -275,13 +412,12 @@ function isNonNegativeInteger(e) {
275
412
  return e !== void 0 && Number.isInteger(e) && e >= 0;
276
413
  }
277
414
  /** Cache for computed fingerprints to avoid repeated toHex() calls. */
278
- const a = /* @__PURE__ */ new WeakMap();
415
+ const u = /* @__PURE__ */ new WeakMap();
279
416
  /** Returns a hex fingerprint of the certificate's raw DER, used for cycle detection and dedup. */
280
- function fingerprint(t) {
281
- let n = a.get(t);
282
- return n === void 0 && (n = e(t.der), a.set(t, n)), n;
417
+ function fingerprint(e) {
418
+ let t = u.get(e);
419
+ return t === void 0 && (t = n(e.der), u.set(e, t)), t;
283
420
  }
284
- //#endregion
285
- export { authorityKeyIdentifierMismatch, buildChainInternal, countCaCertificatesBelowParsed, isSelfIssued, isWithinValidity, loadCertificates, loadSingleCertificate, verifyCertificateSignature };
421
+ export { s as DEFAULT_MAX_PATH_BUILDING_CHECKS, assertPathBuildingChecks, authorityKeyIdentifierMismatch, buildChainInternal, countCaCertificatesBelowParsed, isSelfIssued, isWithinValidity, loadCertificates, loadSingleCertificate, rebaseDiagnostic, verifyCertificateSignature, verifyTrustAnchorSignature };
286
422
 
287
423
  //# sourceMappingURL=verify-path.js.map