micro509 0.17.1 → 0.19.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 (183) hide show
  1. package/CHANGELOG.md +165 -6
  2. package/dist/crypto/crypto.d.ts +12 -4
  3. package/dist/crypto/crypto.js +30 -17
  4. package/dist/crypto/crypto.js.map +1 -1
  5. package/dist/crypto.d.ts +8 -2
  6. package/dist/der/der.d.ts +14 -4
  7. package/dist/der/der.js +19 -19
  8. package/dist/der/der.js.map +1 -1
  9. package/dist/der.d.ts +26 -2
  10. package/dist/der.js +4 -4
  11. package/dist/index.d.ts +84 -16
  12. package/dist/index.js +19 -19
  13. package/dist/internal/asn1/asn1.d.ts +1 -2
  14. package/dist/internal/asn1/asn1.js +10 -2
  15. package/dist/internal/asn1/asn1.js.map +1 -1
  16. package/dist/internal/asn1/ber.js.map +1 -1
  17. package/dist/internal/asn1/decode-refusal.js.map +1 -1
  18. package/dist/internal/asn1/der.d.ts +1 -2
  19. package/dist/internal/asn1/der.js.map +1 -1
  20. package/dist/internal/asn1/oids.js +71 -353
  21. package/dist/internal/asn1/oids.js.map +1 -1
  22. package/dist/internal/asn1/oids.json.js +357 -0
  23. package/dist/internal/asn1/oids.json.js.map +1 -0
  24. package/dist/internal/crypto/algorithm-names.js +1 -1
  25. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  26. package/dist/internal/crypto/ecdsa.js.map +1 -1
  27. package/dist/internal/crypto/hash.js.map +1 -1
  28. package/dist/internal/crypto/pbes2.d.ts +1 -2
  29. package/dist/internal/crypto/pbes2.js +70 -70
  30. package/dist/internal/crypto/pbes2.js.map +1 -1
  31. package/dist/internal/crypto/prime-ec.js +37 -0
  32. package/dist/internal/crypto/prime-ec.js.map +1 -0
  33. package/dist/internal/crypto/rsa-pss.js +50 -50
  34. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  35. package/dist/internal/crypto/sig-verify.d.ts +41 -2
  36. package/dist/internal/crypto/sig-verify.js +125 -38
  37. package/dist/internal/crypto/sig-verify.js.map +1 -1
  38. package/dist/internal/crypto/signing.d.ts +1 -2
  39. package/dist/internal/crypto/signing.js +26 -26
  40. package/dist/internal/crypto/signing.js.map +1 -1
  41. package/dist/internal/jose/base64url.js.map +1 -1
  42. package/dist/internal/jose/json-data.js +108 -0
  43. package/dist/internal/jose/json-data.js.map +1 -0
  44. package/dist/internal/jose/strict-json.d.ts +1 -2
  45. package/dist/internal/jose/strict-json.js.map +1 -1
  46. package/dist/internal/shared/base64.js.map +1 -1
  47. package/dist/internal/shared/dn.d.ts +1 -2
  48. package/dist/internal/shared/dn.js +1 -1
  49. package/dist/internal/shared/dn.js.map +1 -1
  50. package/dist/internal/shared/idna.js.map +1 -1
  51. package/dist/internal/shared/ip.d.ts +1 -2
  52. package/dist/internal/shared/ip.js.map +1 -1
  53. package/dist/internal/shared/mailbox.js.map +1 -1
  54. package/dist/internal/shared/punycode.js.map +1 -1
  55. package/dist/internal/shared/uri-host.js.map +1 -1
  56. package/dist/internal/verify/name-constraints-engine.js +64 -64
  57. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  58. package/dist/internal/verify/policy-engine.js +1 -1
  59. package/dist/internal/verify/policy-engine.js.map +1 -1
  60. package/dist/internal/verify/spki-distrust.js.map +1 -1
  61. package/dist/internal/verify/verify-path.d.ts +1 -2
  62. package/dist/internal/verify/verify-path.js +234 -128
  63. package/dist/internal/verify/verify-path.js.map +1 -1
  64. package/dist/internal/x509/directory-name.js +10 -10
  65. package/dist/internal/x509/directory-name.js.map +1 -1
  66. package/dist/internal/x509/extension-bits.js.map +1 -1
  67. package/dist/internal/x509/extension-errors.d.ts +1 -2
  68. package/dist/internal/x509/extension-errors.js.map +1 -1
  69. package/dist/internal/x509/extension-registry.js +18 -18
  70. package/dist/internal/x509/extension-registry.js.map +1 -1
  71. package/dist/internal/x509/general-name-profile.js +9 -9
  72. package/dist/internal/x509/general-name-profile.js.map +1 -1
  73. package/dist/internal/x509/general-name.js +32 -32
  74. package/dist/internal/x509/general-name.js.map +1 -1
  75. package/dist/internal/x509/name-fields.js +1 -1
  76. package/dist/internal/x509/name-fields.js.map +1 -1
  77. package/dist/internal/x509/or-address.js.map +1 -1
  78. package/dist/jose/base64url.d.ts +4 -0
  79. package/dist/jose/base64url.js +14 -0
  80. package/dist/jose/base64url.js.map +1 -0
  81. package/dist/jose/errors.d.ts +9 -3
  82. package/dist/jose/errors.js.map +1 -1
  83. package/dist/jose/jwe.d.ts +64 -0
  84. package/dist/jose/jwe.js +188 -0
  85. package/dist/jose/jwe.js.map +1 -0
  86. package/dist/jose/jwks.d.ts +38 -10
  87. package/dist/jose/jwks.js +156 -96
  88. package/dist/jose/jwks.js.map +1 -1
  89. package/dist/jose/jws.d.ts +8 -6
  90. package/dist/jose/jws.js +216 -170
  91. package/dist/jose/jws.js.map +1 -1
  92. package/dist/jose/jwt.d.ts +8 -2
  93. package/dist/jose/jwt.js +98 -98
  94. package/dist/jose/jwt.js.map +1 -1
  95. package/dist/jose/remote-jwks.d.ts +1 -2
  96. package/dist/jose/remote-jwks.js +106 -108
  97. package/dist/jose/remote-jwks.js.map +1 -1
  98. package/dist/jose.d.ts +15 -4
  99. package/dist/jose.js +7 -5
  100. package/dist/kdf/argon2-arithmetic.js.map +1 -1
  101. package/dist/kdf/argon2-core.js.map +1 -1
  102. package/dist/kdf/argon2-memory.js.map +1 -1
  103. package/dist/kdf/argon2-portable.js.map +1 -1
  104. package/dist/kdf/argon2.d.ts +7 -2
  105. package/dist/kdf/argon2.js.map +1 -1
  106. package/dist/kdf.d.ts +6 -0
  107. package/dist/keys/keys.d.ts +31 -6
  108. package/dist/keys/keys.js +315 -233
  109. package/dist/keys/keys.js.map +1 -1
  110. package/dist/keys.d.ts +11 -2
  111. package/dist/keys.js +2 -2
  112. package/dist/pem/pem.d.ts +1 -2
  113. package/dist/pem/pem.js +40 -25
  114. package/dist/pem/pem.js.map +1 -1
  115. package/dist/pem.d.ts +6 -0
  116. package/dist/pkcs/pfx.d.ts +2 -3
  117. package/dist/pkcs/pfx.js +142 -142
  118. package/dist/pkcs/pfx.js.map +1 -1
  119. package/dist/pkcs/pkcs12-mac.d.ts +1 -2
  120. package/dist/pkcs/pkcs12-mac.js +122 -122
  121. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  122. package/dist/pkcs/pkcs7.d.ts +24 -3
  123. package/dist/pkcs/pkcs7.js +294 -256
  124. package/dist/pkcs/pkcs7.js.map +1 -1
  125. package/dist/pkcs.d.ts +10 -1
  126. package/dist/pkcs.js +4 -4
  127. package/dist/result/result.d.ts +1 -2
  128. package/dist/result/result.js.map +1 -1
  129. package/dist/result.d.ts +6 -0
  130. package/dist/revocation/chain.d.ts +69 -16
  131. package/dist/revocation/chain.js +431 -303
  132. package/dist/revocation/chain.js.map +1 -1
  133. package/dist/revocation/crl.d.ts +67 -14
  134. package/dist/revocation/crl.js +656 -366
  135. package/dist/revocation/crl.js.map +1 -1
  136. package/dist/revocation/ocsp.d.ts +78 -24
  137. package/dist/revocation/ocsp.js +622 -436
  138. package/dist/revocation/ocsp.js.map +1 -1
  139. package/dist/revocation/revocation.d.ts +68 -14
  140. package/dist/revocation/revocation.js +195 -74
  141. package/dist/revocation/revocation.js.map +1 -1
  142. package/dist/revocation.d.ts +14 -5
  143. package/dist/revocation.js +4 -4
  144. package/dist/ssh/ssh.d.ts +2 -3
  145. package/dist/ssh/ssh.js +165 -170
  146. package/dist/ssh/ssh.js.map +1 -1
  147. package/dist/ssh/wire.d.ts +1 -2
  148. package/dist/ssh/wire.js +10 -10
  149. package/dist/ssh/wire.js.map +1 -1
  150. package/dist/ssh.d.ts +8 -0
  151. package/dist/verify/identity.d.ts +3 -4
  152. package/dist/verify/identity.js +33 -33
  153. package/dist/verify/identity.js.map +1 -1
  154. package/dist/verify/name-constraints.d.ts +9 -2
  155. package/dist/verify/policy.d.ts +9 -2
  156. package/dist/verify/verify.d.ts +85 -34
  157. package/dist/verify/verify.js +276 -226
  158. package/dist/verify/verify.js.map +1 -1
  159. package/dist/verify.d.ts +12 -2
  160. package/dist/x509/certificate.d.ts +4 -5
  161. package/dist/x509/certificate.js +40 -40
  162. package/dist/x509/certificate.js.map +1 -1
  163. package/dist/x509/csr.d.ts +3 -4
  164. package/dist/x509/csr.js +15 -15
  165. package/dist/x509/csr.js.map +1 -1
  166. package/dist/x509/extensions.d.ts +1 -2
  167. package/dist/x509/extensions.js +165 -165
  168. package/dist/x509/extensions.js.map +1 -1
  169. package/dist/x509/fingerprint.d.ts +1 -2
  170. package/dist/x509/fingerprint.js.map +1 -1
  171. package/dist/x509/name-text.d.ts +1 -2
  172. package/dist/x509/name-text.js.map +1 -1
  173. package/dist/x509/name.d.ts +1 -2
  174. package/dist/x509/name.js +15 -15
  175. package/dist/x509/name.js.map +1 -1
  176. package/dist/x509/parse.d.ts +9 -6
  177. package/dist/x509/parse.js +394 -394
  178. package/dist/x509/parse.js.map +1 -1
  179. package/dist/x509.d.ts +17 -4
  180. package/dist/x509.js +8 -8
  181. package/package.json +32 -33
  182. package/dist/internal/asn1/oids2.js +0 -75
  183. package/dist/internal/asn1/oids2.js.map +0 -1
@@ -1,8 +1,9 @@
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";
1
+ import { toHex as e } from "../asn1/asn1.js";
2
+ import { DECODE_LIMIT_CODES as t, decodeRefusalOf as n } from "../asn1/decode-refusal.js";
3
+ import { readSequenceChildren as r, sequence as i } from "../asn1/der.js";
4
+ import { NO_CALLER_VERIFICATION as a, explicitParameters as o, spkiAlgorithmParametersDer as s, verifySignedDataDetailed as c } from "../crypto/sig-verify.js";
5
+ import { canonicalDnKey as l, compareDistinguishedNames as u } from "../shared/dn.js";
6
+ import { parseCertificatesFromSource as d } from "../../x509/parse.js";
6
7
  // src/internal/verify/verify-path.ts
7
8
  /**
8
9
  * Internal certificate path-building and signature-check helpers.
@@ -14,7 +15,7 @@ import { parseCertificatesFromSource as o } from "../../x509/parse.js";
14
15
  * @module
15
16
  */
16
17
  /** Default upper bound on issuer candidates and trust anchors one path search may try. */
17
- const s = 1e5;
18
+ const f = 1e5;
18
19
  /** Throws when a caller-supplied path-building limit is not a positive safe integer. */
19
20
  function assertPathBuildingChecks(e) {
20
21
  if (e !== void 0 && (!Number.isSafeInteger(e) || e < 1)) throw RangeError(`Invalid maxPathBuildingChecks: must be an integer >= 1, got ${e}`);
@@ -25,7 +26,7 @@ function isWithinValidity(e, t) {
25
26
  }
26
27
  /** Returns `true` if the certificate's subject and issuer DNs are semantically equal (RFC 5280 §7.1). */
27
28
  function isSelfIssued(e) {
28
- return a(e.subject, e.issuer);
29
+ return u(e.subject, e.issuer);
29
30
  }
30
31
  /** Counts non-self-issued CA certificates in positions 0..(index-1) for pathLength checking. */
31
32
  function countCaCertificatesBelowParsed(e, t) {
@@ -50,17 +51,17 @@ function loadSingleCertificate(e) {
50
51
  return n;
51
52
  }
52
53
  /** Verifies that `certificate` was signed by `issuer`'s public key. */
53
- function verifyCertificateSignature(e, t) {
54
- return signatureCheck(() => r(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signatureValue, e.tbsCertificateDer));
54
+ function verifyCertificateSignature(e, t, n = a) {
55
+ return signatureCheck(() => c(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signatureValue, e.signatureValueUnusedBits, e.tbsCertificateDer, n));
55
56
  }
56
57
  /** 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
+ async function signatureCheck(e) {
58
59
  let r;
59
60
  try {
60
- r = await n();
61
- } catch (n) {
62
- let r = t(n, e);
63
- if (r === void 0) throw n;
61
+ r = await e();
62
+ } catch (e) {
63
+ let r = n(e, t);
64
+ if (r === void 0) throw e;
64
65
  return {
65
66
  ok: !1,
66
67
  code: r.code,
@@ -74,17 +75,88 @@ async function signatureCheck(n) {
74
75
  } : r;
75
76
  }
76
77
  /** The real signature checks, used unless a caller injects its own. */
77
- const c = {
78
- certificate: verifyCertificateSignature,
79
- trustAnchor: verifyTrustAnchorSignature
80
- }, l = {
78
+ const p = signatureChecksWith(/* @__PURE__ */ new Map());
79
+ /** The real signature checks, falling back to caller verifiers for algorithms WebCrypto cannot run. */
80
+ function signatureChecksWith(e) {
81
+ return {
82
+ certificate: (t, n, r) => verifyCertificateSignature(t, n, {
83
+ verifiers: e,
84
+ ...r !== void 0 && { workingParameters: r }
85
+ }),
86
+ trustAnchor: (t, n) => verifyTrustAnchorSignature(t, n, { verifiers: e }),
87
+ inheritsParameters: (t, n) => e.has(t.signatureAlgorithmOid) && o(n.publicKeyAlgorithmParametersDer) === void 0
88
+ };
89
+ }
90
+ /** The working key a trust anchor initializes (RFC 5280 §6.1.2 (g)-(i)). */
91
+ function anchorWorkingKey(e) {
92
+ return {
93
+ algorithmOid: e.publicKeyAlgorithmOid,
94
+ parametersDer: o(s(e.subjectPublicKeyInfoDer))
95
+ };
96
+ }
97
+ /** Whether `certificate` carries `anchor`'s name and public key: the anchor's own certificate. */
98
+ function carriesTrustAnchorKey(e, t) {
99
+ let n = t.subjectPublicKeyInfoDer;
100
+ return u(e.subject, t.subject) && e.subjectPublicKeyInfoDer.length === n.length && e.subjectPublicKeyInfoDer.every((e, t) => e === n[t]);
101
+ }
102
+ /**
103
+ * `anchor` with `parametersDer` in its SubjectPublicKeyInfo: the trust anchor
104
+ * information of RFC 5280 §6.1.1 (d) for a key whose path supplies its
105
+ * parameters (§6.1.4 (e)).
106
+ */
107
+ function withWorkingParameters(e, t) {
108
+ let n = e.subjectPublicKeyInfoDer, [a, o] = r(n);
109
+ if (a === void 0 || o === void 0) throw Error("SubjectPublicKeyInfo lacks its algorithm or key");
110
+ let s = n.subarray(a.start - a.headerLength, a.end), [c] = r(s);
111
+ if (c === void 0) throw Error("AlgorithmIdentifier lacks its OID");
112
+ return {
113
+ ...e,
114
+ subjectPublicKeyInfoDer: i([i([s.subarray(c.start - c.headerLength, c.end), t]), n.subarray(o.start - o.headerLength, o.end)])
115
+ };
116
+ }
117
+ /**
118
+ * The working key of `path.chain[index]` along a validated path: its own key
119
+ * parameters, or those it inherits from the root or bare anchor above it.
120
+ */
121
+ function pathWorkingKey(e, t) {
122
+ let n = e.chain[t], r = e.chain[e.chain.length - 1];
123
+ if (n === void 0 || r === void 0) throw Error(`no certificate at path index ${t}`);
124
+ return {
125
+ algorithmOid: n.publicKeyAlgorithmOid,
126
+ parametersDer: e.anchorCertificateInChain ? workingParametersAt(e.chain, t, e.chain.length - 2, certificateWorkingKey(r)) : workingParametersAt(e.chain, t, e.chain.length - 1, anchorWorkingKey(e.trustAnchor))
127
+ };
128
+ }
129
+ /** The working key a certificate used as trust anchor initializes. */
130
+ function certificateWorkingKey(e) {
131
+ return {
132
+ algorithmOid: e.publicKeyAlgorithmOid,
133
+ parametersDer: o(e.publicKeyAlgorithmParametersDer)
134
+ };
135
+ }
136
+ /**
137
+ * RFC 5280 §6.1.4 (e)-(f): the working parameters after processing `path` from
138
+ * index `firstProcessed` down to `issuerIndex`, starting from `start`. A
139
+ * certificate with its own parameters sets them. One without keeps the working
140
+ * parameters when its algorithm matches and clears them when it differs.
141
+ */
142
+ function workingParametersAt(e, t, n, r) {
143
+ let { algorithmOid: i, parametersDer: a } = r;
144
+ for (let r = n; r >= t; --r) {
145
+ let t = e[r];
146
+ if (t === void 0) return;
147
+ let n = o(t.publicKeyAlgorithmParametersDer);
148
+ n === void 0 ? t.publicKeyAlgorithmOid !== i && (a = void 0) : a = n, i = t.publicKeyAlgorithmOid;
149
+ }
150
+ return a;
151
+ }
152
+ const m = {
81
153
  "specific-failure": 3,
82
154
  "untrusted-anchor": 2,
83
155
  "missing-issuer": 1,
84
156
  none: 0
85
157
  };
86
158
  function isMoreSignificant(e, t) {
87
- let n = l[e.kind], r = l[t.kind];
159
+ let n = m[e.kind], r = m[t.kind];
88
160
  return n === r ? e.path.length > t.path.length : n > r;
89
161
  }
90
162
  /**
@@ -127,116 +199,143 @@ function rebaseDiagnostic(e, t, n) {
127
199
  * step. Returns the first fully anchored path or the deepest partial path
128
200
  * with the best failure encountered.
129
201
  */
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);
202
+ async function buildChainInternal(t, n, r, i, a, o, s = p, c = f, u = () => Promise.resolve(void 0)) {
203
+ let d, m = [...n, ...r], h = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Map(), _ = new Set(r.map((e) => fingerprint(e))), v = /* @__PURE__ */ new Map();
204
+ for (let e of i) {
205
+ let t = l(e.subject), n = v.get(t);
134
206
  n === void 0 ? v.set(t, [e]) : n.push(e);
135
207
  }
136
208
  let y = { current: {
137
209
  kind: "none",
138
- path: [e]
210
+ path: [t]
139
211
  } }, b = /* @__PURE__ */ new Map(), x = [], S = /* @__PURE__ */ new Map(), C = {
140
- remaining: d,
212
+ remaining: c,
141
213
  exhausted: !1
142
214
  }, w = /* @__PURE__ */ new Map();
143
215
  m.forEach((e, t) => {
144
- let n = i(e.subject), r = h.get(n);
216
+ let n = l(e.subject), r = h.get(n);
145
217
  r === void 0 ? h.set(n, [e]) : r.push(e), g.set(fingerprint(e), t);
146
218
  });
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];
150
- return {
151
- chain: D,
152
- foundTrustedRoot: !0,
153
- ...p !== void 0 && { trustAnchor: p },
154
- anchorCertificateInChain: e !== void 0 && _.has(fingerprint(e))
155
- };
156
- }
157
- let O = y.current;
219
+ let T = fingerprint(t), E = await search(t, [t], /* @__PURE__ */ new Set([T]), 0, []);
220
+ if (E !== void 0) return {
221
+ chain: E,
222
+ foundTrustedRoot: !0,
223
+ ...d === void 0 ? { anchorCertificateInChain: !0 } : {
224
+ anchorCertificateInChain: !1,
225
+ trustAnchor: d
226
+ }
227
+ };
228
+ let D = y.current;
158
229
  if (C.exhausted) return {
159
- chain: O.path,
230
+ chain: D.path,
160
231
  foundTrustedRoot: !1,
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`,
232
+ failure: o.failure("path_building_limit_exceeded", "path building stopped at its work limit", void 0, o.detail({
233
+ expected: `at most ${c} issuer and trust-anchor checks`,
163
234
  actual: "limit reached before a trusted path was found"
164
235
  }))
165
236
  };
166
- switch (O.kind) {
237
+ switch (D.kind) {
167
238
  case "specific-failure": return {
168
- chain: O.path,
239
+ chain: D.path,
169
240
  foundTrustedRoot: !1,
170
- failure: O.failure
241
+ failure: D.failure
171
242
  };
172
243
  case "missing-issuer": return {
173
- chain: O.path,
244
+ chain: D.path,
174
245
  foundTrustedRoot: !1,
175
- missingIssuerAt: O.missingIssuerAt
246
+ missingIssuerAt: D.missingIssuerAt
176
247
  };
177
248
  case "untrusted-anchor":
178
249
  case "none": return {
179
- chain: O.path,
250
+ chain: D.path,
180
251
  foundTrustedRoot: !1
181
252
  };
182
- default: throw Error(`unreachable diagnostic ${String(O)}`);
253
+ default: throw Error(`unreachable diagnostic ${String(D)}`);
183
254
  }
184
- async function search(e, t, n, r) {
255
+ async function search(e, t, n, r, i) {
185
256
  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({
257
+ let a = await u(e.subjectPublicKeyInfoDer);
258
+ if (a !== void 0) {
259
+ recordFailure(o.failure("public_key_distrusted", "certificate public key is explicitly distrusted", t.length - 1, o.detail({
189
260
  subjectCommonName: e.subject.values.commonName,
190
- actual: i
261
+ actual: a
191
262
  })), t);
192
263
  return;
193
264
  }
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));
265
+ let s = await terminateAtAnchor(e, t, i);
266
+ return s === "open" ? await searchIssuers(e, t, n, r, i) : s;
267
+ }
268
+ /** Ends the path at a root or a bare trust anchor, or reports `'open'` to keep searching. */
269
+ async function terminateAtAnchor(e, t, n) {
270
+ if (_.has(fingerprint(e)) && await verifyInheritedLinks(t, n, certificateWorkingKey(e), t.length - 2)) return t;
271
+ let r = await matchTrustAnchor(e, v, o, t.length - 1, {
272
+ verifyAnchor: verifyAnchorOnce,
273
+ charge: chargeBudget,
274
+ distrust: u,
275
+ completesPath: (e) => verifyInheritedLinks(t, n, anchorWorkingKey(e), t.length - 1)
276
+ });
277
+ if (!C.exhausted) return r.matched ? (d = r.anchor, t) : (r.failure !== void 0 && recordFailure(r.failure, t), "open");
278
+ }
279
+ /**
280
+ * Searches the issuers of `current`. A subtree that carries deferred links
281
+ * depends on the path below it, so it skips the dead-end memo.
282
+ */
283
+ async function searchIssuers(e, t, n, r, i) {
284
+ let a = i.length === 0 ? `${fingerprint(e)}:${r}` : void 0, o = a === void 0 ? void 0 : b.get(a);
285
+ if (o !== void 0) {
286
+ consider(rebaseDiagnostic(o.diagnostic, o.nodeIndex, t));
202
287
  return;
203
288
  }
204
- let c = { best: {
289
+ let s = { best: {
205
290
  kind: "none",
206
291
  path: t
207
292
  } };
208
- x.push(c);
293
+ x.push(s);
209
294
  try {
210
- let i = await searchBelow(e, t, n, r);
211
- if (i !== void 0) return i;
295
+ let a = await searchBelow(e, t, n, r, i);
296
+ if (a !== void 0) return a;
212
297
  } finally {
213
298
  x.pop();
214
299
  }
215
- b.set(o, {
216
- diagnostic: c.best,
300
+ a !== void 0 && b.set(a, {
301
+ diagnostic: s.best,
217
302
  nodeIndex: t.length - 1
218
303
  });
219
304
  }
220
- async function searchBelow(e, t, n, r) {
221
- let i = !1;
222
- for (let a of issuerCandidates(e)) {
305
+ /**
306
+ * Verifies the deferred links of a completed path once its trust anchor fixes
307
+ * the working key. `pending` holds the index of each certificate whose issuer
308
+ * key inherits its parameters.
309
+ */
310
+ async function verifyInheritedLinks(t, n, r, i) {
311
+ for (let a of n) {
312
+ let n = t[a], c = t[a + 1];
313
+ if (n === void 0 || c === void 0) return !1;
314
+ let l = workingParametersAt(t, a + 1, i, r), u = signatureResultFailure(await verifyOnce(`c:${fingerprint(n)}:${e(c.subjectPublicKeyInfoDer)}:${l === void 0 ? "-" : e(l)}`, () => s.certificate(n, c, { parametersDer: l })), n, c, a, o);
315
+ if (u !== void 0) return recordFailure(u, t), !1;
316
+ }
317
+ return !0;
318
+ }
319
+ async function searchBelow(e, t, n, r, i) {
320
+ let a = !1;
321
+ for (let o of issuerCandidates(e)) {
223
322
  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;
323
+ if (!isIssuerOf(o, e)) continue;
324
+ a = !0;
325
+ let s = fingerprint(o);
326
+ if (n.has(s)) continue;
327
+ let c = await searchIssuerCandidate(e, o, s, t, n, r, i);
328
+ if (c !== void 0) return c;
230
329
  if (C.exhausted) return;
231
330
  }
232
- i ? consider({
331
+ a ? consider({
233
332
  kind: "none",
234
333
  path: t
235
334
  }) : recordMissingIssuers(e, t);
236
335
  }
237
336
  /** Yields the issuer bucket for `current`: AKI matches first, then roots, then input order. */
238
337
  function* issuerCandidates(e) {
239
- let t = i(e.issuer), n = w.get(t);
338
+ let t = l(e.issuer), n = w.get(t);
240
339
  n === void 0 && (n = rankBucket(h.get(t) ?? [], g, _), w.set(t, n));
241
340
  let r = e.authorityKeyIdentifier;
242
341
  if (r === void 0) yield* n.ordered;
@@ -255,24 +354,30 @@ async function buildChainInternal(e, t, r, a, o, l, u = c, d = s, f = () => Prom
255
354
  missingIssuerAt: t.length - 1
256
355
  });
257
356
  }
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);
357
+ async function searchIssuerCandidate(e, t, n, r, i, c, l) {
358
+ let u = s.inheritsParameters(e, t), d = await evaluateIssuerCandidate(e, t, r, c, a, o, u ? deferSignature : verifySignatureOnce);
359
+ if (!d.ok) {
360
+ recordFailure(d.failure, r);
262
361
  return;
263
362
  }
264
- let c = new Set(i);
265
- return c.add(n), await search(t, [...r, t], c, s.nextCaBelowCount);
363
+ let f = new Set(i);
364
+ return f.add(n), await search(t, [...r, t], f, d.nextCaBelowCount, u ? [...l, r.length - 1] : l);
365
+ }
366
+ function deferSignature() {
367
+ return Promise.resolve({
368
+ ok: !0,
369
+ valid: !0
370
+ });
266
371
  }
267
372
  function chargeBudget() {
268
373
  return C.remaining === 0 ? (C.exhausted = !0, !1) : (--C.remaining, !0);
269
374
  }
270
- function verifySignatureOnce(e, t) {
271
- return verifyOnce(`c:${fingerprint(e)}:${n(t.subjectPublicKeyInfoDer)}`, () => u.certificate(e, t));
375
+ function verifySignatureOnce(t, n) {
376
+ return verifyOnce(`c:${fingerprint(t)}:${e(n.subjectPublicKeyInfoDer)}`, () => s.certificate(t, n));
272
377
  }
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));
378
+ function verifyAnchorOnce(t, n) {
379
+ let r = `${n.publicKeyAlgorithmOid}:${n.publicKeyParametersOid ?? ""}:${e(n.subjectPublicKeyInfoDer)}`;
380
+ return verifyOnce(`a:${fingerprint(t)}:${r}`, () => s.trustAnchor(t, n));
276
381
  }
277
382
  /** One signature check per distinct certificate-and-key pair for the whole search. */
278
383
  function verifyOnce(e, t) {
@@ -295,22 +400,24 @@ async function buildChainInternal(e, t, r, a, o, l, u = c, d = s, f = () => Prom
295
400
  async function evaluateIssuerCandidate(e, t, n, r, i, a, o) {
296
401
  let s = evaluateIssuerConstraints(e, t, n, r, i, a);
297
402
  if (!s.ok) return s;
298
- let c = await o(e, t);
299
- return c.ok ? c.valid ? s : {
300
- ok: !1,
301
- failure: a.failure("signature_invalid", "certificate signature does not verify", n.length - 1, a.detail({
302
- subjectCommonName: e.subject.values.commonName,
303
- issuerCommonName: t.subject.values.commonName
304
- }))
305
- } : {
403
+ let c = signatureResultFailure(await o(e, t), e, t, n.length - 1, a);
404
+ return c === void 0 ? s : {
306
405
  ok: !1,
307
- failure: a.failure(c.code, c.reason, n.length - 1, a.detail({
308
- subjectCommonName: e.subject.values.commonName,
309
- issuerCommonName: t.subject.values.commonName,
310
- actual: c.reason
311
- }))
406
+ failure: c
312
407
  };
313
408
  }
409
+ /** The path failure for a signature check of `current` under `issuer`, if it did not pass. */
410
+ function signatureResultFailure(e, t, n, r, i) {
411
+ if (!e.ok) return i.failure(e.code, e.reason, r, i.detail({
412
+ subjectCommonName: t.subject.values.commonName,
413
+ issuerCommonName: n.subject.values.commonName,
414
+ actual: e.reason
415
+ }));
416
+ if (!e.valid) return i.failure("signature_invalid", "certificate signature does not verify", r, i.detail({
417
+ subjectCommonName: t.subject.values.commonName,
418
+ issuerCommonName: n.subject.values.commonName
419
+ }));
420
+ }
314
421
  function evaluateIssuerConstraints(e, t, n, r, i, a) {
315
422
  return isWithinValidity(t, i) ? t.basicConstraints?.ca === !0 ? evaluateIssuerKeyConstraints(e, t, n, r, a) : {
316
423
  ok: !1,
@@ -356,7 +463,7 @@ function authorityKeyIdentifierMismatch(e, t) {
356
463
  }
357
464
  /** Expands a PEM (possibly multi-block) or DER source into parsed certificates. */
358
465
  function expandSource(e) {
359
- return o(e);
466
+ return d(e);
360
467
  }
361
468
  /** Orders one subject bucket once, roots first and then input order, and indexes it by SKI. */
362
469
  function rankBucket(e, t, n) {
@@ -381,46 +488,45 @@ function compareBooleans(e, t) {
381
488
  }
382
489
  /** Returns `true` if `issuer`'s subject DN semantically matches `child`'s issuer DN (RFC 5280 §7.1). */
383
490
  function isIssuerOf(e, t) {
384
- return a(t.issuer, e.subject);
385
- }
386
- /** Attempts to verify `certificate` against each bare trust anchor whose subject DN matches. */
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 }));
491
+ return u(t.issuer, e.subject);
492
+ }
493
+ async function matchTrustAnchor(e, t, n, r, { verifyAnchor: i, charge: a, distrust: o, completesPath: s }) {
494
+ let c = t.get(l(e.issuer));
495
+ if (c === void 0) return { matched: !1 };
496
+ let d;
497
+ for (let t of c) {
498
+ if (!a()) return { matched: !1 };
499
+ if (trustAnchorAkiMismatch(e, t) || !u(e.issuer, t.subject)) continue;
500
+ let c = await o(t.subjectPublicKeyInfoDer);
501
+ if (c !== void 0) {
502
+ d = n.failure("public_key_distrusted", "bare trust anchor public key is explicitly distrusted", void 0, n.detail({ actual: c }));
397
503
  continue;
398
504
  }
399
- let l = await o(e, t);
400
- if (!l.ok) u ??= n.failure(l.code, l.reason, r, n.detail({
505
+ let l = await i(e, t);
506
+ if (!l.ok) d ??= n.failure(l.code, l.reason, r, n.detail({
401
507
  subjectCommonName: e.subject.values.commonName,
402
508
  actual: l.reason
403
509
  }));
404
- else if (!l.valid) u === void 0 && (u = n.failure("signature_invalid", "certificate signature does not verify", r, n.detail({
510
+ else if (!l.valid) d === void 0 && (d = n.failure("signature_invalid", "certificate signature does not verify", r, n.detail({
405
511
  subjectCommonName: e.subject.values.commonName,
406
512
  actual: "certificate signature does not verify"
407
513
  })));
408
- else return {
514
+ else if (await s(t)) return {
409
515
  matched: !0,
410
516
  anchor: t
411
517
  };
412
518
  }
413
- return u === void 0 ? { matched: !1 } : {
519
+ return d === void 0 ? { matched: !1 } : {
414
520
  matched: !1,
415
- failure: u
521
+ failure: d
416
522
  };
417
523
  }
418
524
  function trustAnchorAkiMismatch(e, t) {
419
525
  return t.subjectKeyIdentifier !== void 0 && e.authorityKeyIdentifier !== void 0 && t.subjectKeyIdentifier !== e.authorityKeyIdentifier;
420
526
  }
421
527
  /** 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));
528
+ function verifyTrustAnchorSignature(e, t, n = a) {
529
+ return signatureCheck(() => c(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signatureValue, e.signatureValueUnusedBits, e.tbsCertificateDer, n));
424
530
  }
425
531
  function describeDateTime(e) {
426
532
  return Number.isNaN(e.getTime()) ? "<invalid date>" : e.toISOString();
@@ -429,12 +535,12 @@ function isNonNegativeInteger(e) {
429
535
  return e !== void 0 && Number.isInteger(e) && e >= 0;
430
536
  }
431
537
  /** Cache for computed fingerprints to avoid repeated toHex() calls. */
432
- const u = /* @__PURE__ */ new WeakMap();
538
+ const h = /* @__PURE__ */ new WeakMap();
433
539
  /** Returns a hex fingerprint of the certificate's raw DER, used for cycle detection and dedup. */
434
- function fingerprint(e) {
435
- let t = u.get(e);
436
- return t === void 0 && (t = n(e.der), u.set(e, t)), t;
540
+ function fingerprint(t) {
541
+ let n = h.get(t);
542
+ return n === void 0 && (n = e(t.der), h.set(t, n)), n;
437
543
  }
438
- export { s as DEFAULT_MAX_PATH_BUILDING_CHECKS, assertPathBuildingChecks, authorityKeyIdentifierMismatch, buildChainInternal, countCaCertificatesBelowParsed, isSelfIssued, isWithinValidity, loadCertificates, loadSingleCertificate, rebaseDiagnostic, verifyCertificateSignature, verifyTrustAnchorSignature };
544
+ export { f as DEFAULT_MAX_PATH_BUILDING_CHECKS, anchorWorkingKey, assertPathBuildingChecks, authorityKeyIdentifierMismatch, buildChainInternal, carriesTrustAnchorKey, certificateWorkingKey, countCaCertificatesBelowParsed, isSelfIssued, isWithinValidity, loadCertificates, loadSingleCertificate, pathWorkingKey, rebaseDiagnostic, signatureChecksWith, verifyCertificateSignature, verifyTrustAnchorSignature, withWorkingParameters, workingParametersAt };
439
545
 
440
546
  //# sourceMappingURL=verify-path.js.map