micro509 0.14.0 → 0.16.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 (164) hide show
  1. package/CHANGELOG.md +2119 -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 +11 -11
  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/spki-distrust.js +23 -0
  73. package/dist/internal/verify/spki-distrust.js.map +1 -0
  74. package/dist/internal/verify/verify-path.d.ts +1 -4
  75. package/dist/internal/verify/verify-path.js +282 -129
  76. package/dist/internal/verify/verify-path.js.map +1 -1
  77. package/dist/internal/x509/directory-name.js +1 -2
  78. package/dist/internal/x509/directory-name.js.map +1 -1
  79. package/dist/internal/x509/extension-bits.js +2 -3
  80. package/dist/internal/x509/extension-bits.js.map +1 -1
  81. package/dist/internal/x509/extension-errors.d.ts +1 -4
  82. package/dist/internal/x509/extension-errors.js +1 -2
  83. package/dist/internal/x509/extension-errors.js.map +1 -1
  84. package/dist/internal/x509/extension-registry.js +107 -89
  85. package/dist/internal/x509/extension-registry.js.map +1 -1
  86. package/dist/internal/x509/general-name-profile.js +129 -0
  87. package/dist/internal/x509/general-name-profile.js.map +1 -0
  88. package/dist/internal/x509/general-name-tags.js +1 -2
  89. package/dist/internal/x509/general-name-tags.js.map +1 -1
  90. package/dist/internal/x509/general-name.js +128 -42
  91. package/dist/internal/x509/general-name.js.map +1 -1
  92. package/dist/internal/x509/name-fields.js +1 -2
  93. package/dist/internal/x509/name-fields.js.map +1 -1
  94. package/dist/internal/x509/or-address.js +217 -0
  95. package/dist/internal/x509/or-address.js.map +1 -0
  96. package/dist/keys/keys.d.ts +122 -104
  97. package/dist/keys/keys.js +211 -204
  98. package/dist/keys/keys.js.map +1 -1
  99. package/dist/keys.d.ts +3 -3
  100. package/dist/pem/pem.d.ts +14 -17
  101. package/dist/pem/pem.js +16 -17
  102. package/dist/pem/pem.js.map +1 -1
  103. package/dist/pem.js +1 -2
  104. package/dist/pkcs/pfx.d.ts +57 -32
  105. package/dist/pkcs/pfx.js +209 -173
  106. package/dist/pkcs/pfx.js.map +1 -1
  107. package/dist/pkcs/pkcs12-mac.d.ts +104 -27
  108. package/dist/pkcs/pkcs12-mac.js +266 -79
  109. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  110. package/dist/pkcs/pkcs7.d.ts +43 -37
  111. package/dist/pkcs/pkcs7.js +218 -217
  112. package/dist/pkcs/pkcs7.js.map +1 -1
  113. package/dist/pkcs.d.ts +4 -3
  114. package/dist/result/result.d.ts +27 -20
  115. package/dist/result/result.js +8 -3
  116. package/dist/result/result.js.map +1 -1
  117. package/dist/result.d.ts +2 -2
  118. package/dist/result.js +1 -2
  119. package/dist/revocation/chain.d.ts +101 -23
  120. package/dist/revocation/chain.js +436 -182
  121. package/dist/revocation/chain.js.map +1 -1
  122. package/dist/revocation/crl.d.ts +131 -67
  123. package/dist/revocation/crl.js +443 -327
  124. package/dist/revocation/crl.js.map +1 -1
  125. package/dist/revocation/ocsp.d.ts +94 -59
  126. package/dist/revocation/ocsp.js +331 -293
  127. package/dist/revocation/ocsp.js.map +1 -1
  128. package/dist/revocation/revocation.d.ts +47 -35
  129. package/dist/revocation/revocation.js +88 -57
  130. package/dist/revocation/revocation.js.map +1 -1
  131. package/dist/revocation.d.ts +3 -3
  132. package/dist/verify/identity.d.ts +16 -19
  133. package/dist/verify/identity.js +118 -89
  134. package/dist/verify/identity.js.map +1 -1
  135. package/dist/verify/name-constraints.d.ts +1 -4
  136. package/dist/verify/policy.d.ts +9 -7
  137. package/dist/verify/verify.d.ts +115 -46
  138. package/dist/verify/verify.js +373 -183
  139. package/dist/verify/verify.js.map +1 -1
  140. package/dist/verify.d.ts +2 -2
  141. package/dist/x509/certificate.d.ts +20 -21
  142. package/dist/x509/certificate.js +62 -46
  143. package/dist/x509/certificate.js.map +1 -1
  144. package/dist/x509/csr.d.ts +5 -7
  145. package/dist/x509/csr.js +10 -11
  146. package/dist/x509/csr.js.map +1 -1
  147. package/dist/x509/extensions.d.ts +154 -66
  148. package/dist/x509/extensions.js +317 -151
  149. package/dist/x509/extensions.js.map +1 -1
  150. package/dist/x509/fingerprint.d.ts +15 -7
  151. package/dist/x509/fingerprint.js +20 -3
  152. package/dist/x509/fingerprint.js.map +1 -1
  153. package/dist/x509/name-text.d.ts +7 -9
  154. package/dist/x509/name-text.js +18 -4
  155. package/dist/x509/name-text.js.map +1 -1
  156. package/dist/x509/name.d.ts +9 -11
  157. package/dist/x509/name.js +18 -19
  158. package/dist/x509/name.js.map +1 -1
  159. package/dist/x509/parse.d.ts +84 -69
  160. package/dist/x509/parse.js +421 -400
  161. package/dist/x509/parse.js.map +1 -1
  162. package/dist/x509.d.ts +3 -3
  163. package/dist/x509.js +3 -3
  164. 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,176 @@ 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, f = () => Promise.resolve(void 0)) {
131
+ let p, m = [...t, ...r], h = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Map(), _ = new Set(r.map((e) => fingerprint(e))), v = /* @__PURE__ */ new Map();
132
+ for (let e of a) {
133
+ let t = i(e.subject), n = v.get(t);
134
+ n === void 0 ? v.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 y = { current: {
137
+ kind: "none",
138
+ path: [e]
139
+ } }, b = /* @__PURE__ */ new Map(), x = [], S = /* @__PURE__ */ new Map(), C = {
140
+ remaining: d,
141
+ exhausted: !1
142
+ }, w = /* @__PURE__ */ new Map();
143
+ m.forEach((e, t) => {
144
+ let n = i(e.subject), r = h.get(n);
145
+ r === void 0 ? h.set(n, [e]) : r.push(e), g.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 T = m.length + 1, E = fingerprint(e), D = await search(e, [e], /* @__PURE__ */ new Set([E]), 0);
148
+ if (D !== void 0) {
149
+ let e = D[D.length - 1];
74
150
  return {
75
- chain: y,
151
+ chain: D,
76
152
  foundTrustedRoot: !0,
77
- anchorCertificateInChain: e !== void 0 && u.has(fingerprint(e))
153
+ ...p !== void 0 && { trustAnchor: p },
154
+ anchorCertificateInChain: e !== void 0 && _.has(fingerprint(e))
78
155
  };
79
156
  }
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,
157
+ let O = y.current;
158
+ if (C.exhausted) return {
159
+ chain: O.path,
94
160
  foundTrustedRoot: !1,
95
- missingIssuerAt: m
161
+ failure: l.failure("path_building_limit_exceeded", "path building stopped at its work limit", void 0, l.detail({
162
+ expected: `at most ${d} issuer and trust-anchor checks`,
163
+ actual: "limit reached before a trusted path was found"
164
+ }))
96
165
  };
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);
166
+ switch (O.kind) {
167
+ case "specific-failure": return {
168
+ chain: O.path,
169
+ foundTrustedRoot: !1,
170
+ failure: O.failure
171
+ };
172
+ case "missing-issuer": return {
173
+ chain: O.path,
174
+ foundTrustedRoot: !1,
175
+ missingIssuerAt: O.missingIssuerAt
176
+ };
177
+ case "untrusted-anchor":
178
+ case "none": return {
179
+ chain: O.path,
180
+ foundTrustedRoot: !1
181
+ };
182
+ default: throw Error(`unreachable diagnostic ${String(O)}`);
183
+ }
184
+ async function search(e, t, n, r) {
185
+ if (C.exhausted) return;
186
+ let i = await f(e.subjectPublicKeyInfoDer);
187
+ if (i !== void 0) {
188
+ recordFailure(l.failure("public_key_distrusted", "certificate public key is explicitly distrusted", t.length - 1, l.detail({
189
+ subjectCommonName: e.subject.values.commonName,
190
+ actual: i
191
+ })), t);
107
192
  return;
108
193
  }
109
- let m = await searchIssuerCandidates(e, p, n, r, i);
110
- if (m !== void 0) return m;
111
- g.add(f), updateDeepest(n);
194
+ if (_.has(fingerprint(e))) return t;
195
+ let a = await matchTrustAnchor(e, v, l, t.length - 1, verifyAnchorOnce, chargeBudget, f);
196
+ if (C.exhausted) return;
197
+ if (a.failure !== void 0 && recordFailure(a.failure, t), a.matched) return p = a.anchor, t;
198
+ if (t.length > T) return;
199
+ let o = `${fingerprint(e)}:${r}`, s = b.get(o);
200
+ if (s !== void 0) {
201
+ consider(rebaseDiagnostic(s.diagnostic, s.nodeIndex, t));
202
+ return;
203
+ }
204
+ let c = { best: {
205
+ kind: "none",
206
+ path: t
207
+ } };
208
+ x.push(c);
209
+ try {
210
+ let i = await searchBelow(e, t, n, r);
211
+ if (i !== void 0) return i;
212
+ } finally {
213
+ x.pop();
214
+ }
215
+ b.set(o, {
216
+ diagnostic: c.best,
217
+ nodeIndex: t.length - 1
218
+ });
112
219
  }
113
- function recordMissingIssuers(e, t) {
114
- isSelfIssued(e) ? (updateDeepest(t), f = !0) : updateDeepest(t) && (m = t.length - 1);
220
+ async function searchBelow(e, t, n, r) {
221
+ let i = !1;
222
+ for (let a of issuerCandidates(e)) {
223
+ if (!chargeBudget()) return;
224
+ if (!isIssuerOf(a, e)) continue;
225
+ i = !0;
226
+ let o = fingerprint(a);
227
+ if (n.has(o)) continue;
228
+ let s = await searchIssuerCandidate(e, a, o, t, n, r);
229
+ if (s !== void 0) return s;
230
+ if (C.exhausted) return;
231
+ }
232
+ i ? consider({
233
+ kind: "none",
234
+ path: t
235
+ }) : recordMissingIssuers(e, t);
115
236
  }
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;
237
+ /** Yields the issuer bucket for `current`: AKI matches first, then roots, then input order. */
238
+ function* issuerCandidates(e) {
239
+ let t = i(e.issuer), n = w.get(t);
240
+ n === void 0 && (n = rankBucket(h.get(t) ?? [], g, _), w.set(t, n));
241
+ let r = e.authorityKeyIdentifier;
242
+ if (r === void 0) yield* n.ordered;
243
+ else {
244
+ yield* n.bySubjectKeyIdentifier.get(r) ?? [];
245
+ for (let e of n.ordered) e.subjectKeyIdentifier !== r && (yield e);
121
246
  }
122
247
  }
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);
248
+ function recordMissingIssuers(e, t) {
249
+ isSelfIssued(e) ? consider({
250
+ kind: "untrusted-anchor",
251
+ path: t
252
+ }) : t.length > 1 && consider({
253
+ kind: "missing-issuer",
254
+ path: t,
255
+ missingIssuerAt: t.length - 1
256
+ });
257
+ }
258
+ async function searchIssuerCandidate(e, t, n, r, i, a) {
259
+ let s = await evaluateIssuerCandidate(e, t, r, a, o, l, verifySignatureOnce);
260
+ if (!s.ok) {
261
+ recordFailure(s.failure, r);
129
262
  return;
130
263
  }
131
- let l = new Set(r);
132
- return l.add(s), await search(t, [...n, t], l, c.nextCaBelowCount);
264
+ let c = new Set(i);
265
+ return c.add(n), await search(t, [...r, t], c, s.nextCaBelowCount);
266
+ }
267
+ function chargeBudget() {
268
+ return C.remaining === 0 ? (C.exhausted = !0, !1) : (--C.remaining, !0);
269
+ }
270
+ function verifySignatureOnce(e, t) {
271
+ return verifyOnce(`c:${fingerprint(e)}:${n(t.subjectPublicKeyInfoDer)}`, () => u.certificate(e, t));
133
272
  }
134
- function updateDeepest(e) {
135
- return e.length > p.length ? (p = e, !0) : !1;
273
+ function verifyAnchorOnce(e, t) {
274
+ let r = `${t.publicKeyAlgorithmOid}:${t.publicKeyParametersOid ?? ""}:${n(t.subjectPublicKeyInfoDer)}`;
275
+ return verifyOnce(`a:${fingerprint(e)}:${r}`, () => u.trustAnchor(e, t));
276
+ }
277
+ /** One signature check per distinct certificate-and-key pair for the whole search. */
278
+ function verifyOnce(e, t) {
279
+ let n = S.get(e);
280
+ return n === void 0 && (n = t(), S.set(e, n)), n;
281
+ }
282
+ function consider(e) {
283
+ isMoreSignificant(e, y.current) && (y.current = e);
284
+ for (let t of x) isMoreSignificant(e, t.best) && (t.best = e);
136
285
  }
137
286
  function recordFailure(e, t) {
138
- (h === void 0 || t.length > p.length) && (h = e, p = t);
287
+ consider({
288
+ kind: "specific-failure",
289
+ path: t,
290
+ failure: e
291
+ });
139
292
  }
140
293
  }
141
294
  /** 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 : {
295
+ async function evaluateIssuerCandidate(e, t, n, r, i, a, o) {
296
+ let s = evaluateIssuerConstraints(e, t, n, r, i, a);
297
+ if (!s.ok) return s;
298
+ let c = await o(e, t);
299
+ return c.ok ? c.valid ? s : {
147
300
  ok: !1,
148
301
  failure: a.failure("signature_invalid", "certificate signature does not verify", n.length - 1, a.detail({
149
302
  subjectCommonName: e.subject.values.commonName,
@@ -151,10 +304,10 @@ async function evaluateIssuerCandidate(e, t, n, r, i, a) {
151
304
  }))
152
305
  } : {
153
306
  ok: !1,
154
- failure: a.failure(s.code, s.reason, n.length - 1, a.detail({
307
+ failure: a.failure(c.code, c.reason, n.length - 1, a.detail({
155
308
  subjectCommonName: e.subject.values.commonName,
156
309
  issuerCommonName: t.subject.values.commonName,
157
- actual: s.reason
310
+ actual: c.reason
158
311
  }))
159
312
  };
160
313
  }
@@ -203,22 +356,24 @@ function authorityKeyIdentifierMismatch(e, t) {
203
356
  }
204
357
  /** Expands a PEM (possibly multi-block) or DER source into parsed certificates. */
205
358
  function expandSource(e) {
206
- return r(e);
207
- }
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
- });
359
+ return o(e);
218
360
  }
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;
361
+ /** Orders one subject bucket once, roots first and then input order, and indexes it by SKI. */
362
+ function rankBucket(e, t, n) {
363
+ let r = [...e].sort((e, r) => {
364
+ let i = fingerprint(e), a = fingerprint(r), o = compareBooleans(n.has(i), n.has(a));
365
+ return o === 0 ? (t.get(i) ?? 2 ** 53 - 1) - (t.get(a) ?? 2 ** 53 - 1) : o;
366
+ }), i = /* @__PURE__ */ new Map();
367
+ for (let e of r) {
368
+ let t = e.subjectKeyIdentifier;
369
+ if (t === void 0) continue;
370
+ let n = i.get(t);
371
+ n === void 0 ? i.set(t, [e]) : n.push(e);
372
+ }
373
+ return {
374
+ ordered: r,
375
+ bySubjectKeyIdentifier: i
376
+ };
222
377
  }
223
378
  /** Sort comparator: `true` sorts before `false`. Returns -1, 0, or 1. */
224
379
  function compareBooleans(e, t) {
@@ -226,47 +381,46 @@ function compareBooleans(e, t) {
226
381
  }
227
382
  /** Returns `true` if `issuer`'s subject DN semantically matches `child`'s issuer DN (RFC 5280 §7.1). */
228
383
  function isIssuerOf(e, t) {
229
- return n(t.issuer, e.subject);
384
+ return a(t.issuer, e.subject);
230
385
  }
231
386
  /** 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
- })));
387
+ async function matchTrustAnchor(e, t, n, r, o, s, c) {
388
+ let l = t.get(i(e.issuer));
389
+ if (l === void 0) return { matched: !1 };
390
+ let u;
391
+ for (let t of l) {
392
+ if (!s()) return { matched: !1 };
393
+ if (trustAnchorAkiMismatch(e, t) || !a(e.issuer, t.subject)) continue;
394
+ let i = await c(t.subjectPublicKeyInfoDer);
395
+ if (i !== void 0) {
396
+ u = n.failure("public_key_distrusted", "bare trust anchor public key is explicitly distrusted", void 0, n.detail({ actual: i }));
251
397
  continue;
252
398
  }
253
- return { matched: !0 };
399
+ let l = await o(e, t);
400
+ if (!l.ok) u ??= n.failure(l.code, l.reason, r, n.detail({
401
+ subjectCommonName: e.subject.values.commonName,
402
+ actual: l.reason
403
+ }));
404
+ else if (!l.valid) u === void 0 && (u = n.failure("signature_invalid", "certificate signature does not verify", r, n.detail({
405
+ subjectCommonName: e.subject.values.commonName,
406
+ actual: "certificate signature does not verify"
407
+ })));
408
+ else return {
409
+ matched: !0,
410
+ anchor: t
411
+ };
254
412
  }
255
- return s === void 0 ? { matched: !1 } : {
413
+ return u === void 0 ? { matched: !1 } : {
256
414
  matched: !1,
257
- failure: s
415
+ failure: u
258
416
  };
259
417
  }
260
418
  function trustAnchorAkiMismatch(e, t) {
261
419
  return t.subjectKeyIdentifier !== void 0 && e.authorityKeyIdentifier !== void 0 && t.subjectKeyIdentifier !== e.authorityKeyIdentifier;
262
420
  }
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;
421
+ /** Verifies that `certificate` was signed by a bare trust anchor's key. */
422
+ function verifyTrustAnchorSignature(e, t) {
423
+ return signatureCheck(() => r(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signatureValue, e.tbsCertificateDer));
270
424
  }
271
425
  function describeDateTime(e) {
272
426
  return Number.isNaN(e.getTime()) ? "<invalid date>" : e.toISOString();
@@ -275,13 +429,12 @@ function isNonNegativeInteger(e) {
275
429
  return e !== void 0 && Number.isInteger(e) && e >= 0;
276
430
  }
277
431
  /** Cache for computed fingerprints to avoid repeated toHex() calls. */
278
- const a = /* @__PURE__ */ new WeakMap();
432
+ const u = /* @__PURE__ */ new WeakMap();
279
433
  /** 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;
434
+ function fingerprint(e) {
435
+ let t = u.get(e);
436
+ return t === void 0 && (t = n(e.der), u.set(e, t)), t;
283
437
  }
284
- //#endregion
285
- export { authorityKeyIdentifierMismatch, buildChainInternal, countCaCertificatesBelowParsed, isSelfIssued, isWithinValidity, loadCertificates, loadSingleCertificate, verifyCertificateSignature };
438
+ export { s as DEFAULT_MAX_PATH_BUILDING_CHECKS, assertPathBuildingChecks, authorityKeyIdentifierMismatch, buildChainInternal, countCaCertificatesBelowParsed, isSelfIssued, isWithinValidity, loadCertificates, loadSingleCertificate, rebaseDiagnostic, verifyCertificateSignature, verifyTrustAnchorSignature };
286
439
 
287
440
  //# sourceMappingURL=verify-path.js.map