micro509 0.8.0 → 0.10.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 (108) hide show
  1. package/dist/index.d.ts +4 -4
  2. package/dist/index.js +15 -15
  3. package/dist/internal/asn1/asn1.js +145 -186
  4. package/dist/internal/asn1/asn1.js.map +1 -0
  5. package/dist/internal/asn1/der.js +210 -227
  6. package/dist/internal/asn1/der.js.map +1 -0
  7. package/dist/internal/asn1/oids.js +297 -101
  8. package/dist/internal/asn1/oids.js.map +1 -0
  9. package/dist/internal/asn1/oids2.js +69 -0
  10. package/dist/internal/asn1/oids2.js.map +1 -0
  11. package/dist/internal/crypto/algorithm-names.js +37 -35
  12. package/dist/internal/crypto/algorithm-names.js.map +1 -0
  13. package/dist/internal/crypto/ecdsa.js +25 -28
  14. package/dist/internal/crypto/ecdsa.js.map +1 -0
  15. package/dist/internal/crypto/hash.js +56 -117
  16. package/dist/internal/crypto/hash.js.map +1 -0
  17. package/dist/internal/crypto/pbes2.js +122 -133
  18. package/dist/internal/crypto/pbes2.js.map +1 -0
  19. package/dist/internal/crypto/rsa-pss.js +118 -124
  20. package/dist/internal/crypto/rsa-pss.js.map +1 -0
  21. package/dist/internal/crypto/sig-verify.js +87 -99
  22. package/dist/internal/crypto/sig-verify.js.map +1 -0
  23. package/dist/internal/crypto/signing.d.ts +12 -8
  24. package/dist/internal/crypto/signing.js +53 -55
  25. package/dist/internal/crypto/signing.js.map +1 -0
  26. package/dist/internal/crypto/webcrypto.js +5 -3
  27. package/dist/internal/crypto/webcrypto.js.map +1 -0
  28. package/dist/internal/shared/base64.js +10 -9
  29. package/dist/internal/shared/base64.js.map +1 -0
  30. package/dist/internal/shared/dn.js +50 -57
  31. package/dist/internal/shared/dn.js.map +1 -0
  32. package/dist/internal/shared/ip.js +43 -53
  33. package/dist/internal/shared/ip.js.map +1 -0
  34. package/dist/internal/verify/name-constraints-engine.js +211 -239
  35. package/dist/internal/verify/name-constraints-engine.js.map +1 -0
  36. package/dist/internal/verify/policy-engine.js +228 -247
  37. package/dist/internal/verify/policy-engine.js.map +1 -0
  38. package/dist/internal/verify/verify-path.js +213 -228
  39. package/dist/internal/verify/verify-path.js.map +1 -0
  40. package/dist/internal/x509/extension-bits.js +62 -59
  41. package/dist/internal/x509/extension-bits.js.map +1 -0
  42. package/dist/internal/x509/extension-registry.js +133 -164
  43. package/dist/internal/x509/extension-registry.js.map +1 -0
  44. package/dist/internal/x509/name-fields.js +35 -41
  45. package/dist/internal/x509/name-fields.js.map +1 -0
  46. package/dist/keys/keys.d.ts +626 -447
  47. package/dist/keys/keys.js +652 -404
  48. package/dist/keys/keys.js.map +1 -0
  49. package/dist/keys.d.ts +2 -2
  50. package/dist/keys.js +2 -2
  51. package/dist/pem/pem.d.ts +46 -46
  52. package/dist/pem/pem.js +62 -81
  53. package/dist/pem/pem.js.map +1 -0
  54. package/dist/pem.js +2 -2
  55. package/dist/pkcs/pfx.d.ts +96 -80
  56. package/dist/pkcs/pfx.js +191 -231
  57. package/dist/pkcs/pfx.js.map +1 -0
  58. package/dist/pkcs/pkcs12-mac.d.ts +20 -18
  59. package/dist/pkcs/pkcs12-mac.js +90 -111
  60. package/dist/pkcs/pkcs12-mac.js.map +1 -0
  61. package/dist/pkcs/pkcs7.d.ts +118 -69
  62. package/dist/pkcs/pkcs7.js +401 -443
  63. package/dist/pkcs/pkcs7.js.map +1 -0
  64. package/dist/pkcs.d.ts +2 -2
  65. package/dist/pkcs.js +4 -4
  66. package/dist/result/result.d.ts +37 -32
  67. package/dist/result/result.js +46 -44
  68. package/dist/result/result.js.map +1 -0
  69. package/dist/result.js +2 -2
  70. package/dist/revocation/chain.d.ts +138 -119
  71. package/dist/revocation/chain.js +248 -281
  72. package/dist/revocation/chain.js.map +1 -0
  73. package/dist/revocation/crl.d.ts +125 -123
  74. package/dist/revocation/crl.js +653 -745
  75. package/dist/revocation/crl.js.map +1 -0
  76. package/dist/revocation/ocsp.d.ts +183 -177
  77. package/dist/revocation/ocsp.js +581 -638
  78. package/dist/revocation/ocsp.js.map +1 -0
  79. package/dist/revocation/revocation.d.ts +26 -26
  80. package/dist/revocation/revocation.js +104 -125
  81. package/dist/revocation/revocation.js.map +1 -0
  82. package/dist/revocation.js +5 -5
  83. package/dist/verify/identity.d.ts +42 -42
  84. package/dist/verify/identity.js +158 -167
  85. package/dist/verify/identity.js.map +1 -0
  86. package/dist/verify/name-constraints.d.ts +7 -7
  87. package/dist/verify/policy.d.ts +10 -10
  88. package/dist/verify/verify.d.ts +163 -163
  89. package/dist/verify/verify.js +396 -392
  90. package/dist/verify/verify.js.map +1 -0
  91. package/dist/verify.js +3 -3
  92. package/dist/x509/certificate.d.ts +117 -117
  93. package/dist/x509/certificate.js +51 -60
  94. package/dist/x509/certificate.js.map +1 -0
  95. package/dist/x509/csr.d.ts +25 -25
  96. package/dist/x509/csr.js +26 -30
  97. package/dist/x509/csr.js.map +1 -0
  98. package/dist/x509/extensions.d.ts +228 -184
  99. package/dist/x509/extensions.js +175 -200
  100. package/dist/x509/extensions.js.map +1 -0
  101. package/dist/x509/name.d.ts +94 -93
  102. package/dist/x509/name.js +30 -29
  103. package/dist/x509/name.js.map +1 -0
  104. package/dist/x509/parse.d.ts +185 -185
  105. package/dist/x509/parse.js +638 -676
  106. package/dist/x509/parse.js.map +1 -0
  107. package/dist/x509.js +6 -6
  108. package/package.json +79 -41
@@ -1,6 +1,6 @@
1
- import { parseCertificateFromSource } from "../x509/parse.js";
2
- import { checkCertificateRevocationAgainstCrl, parseCertificateRevocationListDerOrThrow, parseCertificateRevocationListPemOrThrow, revocationReasonFromCode } from "./crl.js";
3
- import { parseOcspResponseDerOrThrow, parseOcspResponsePemOrThrow, validateOcspResponse } from "./ocsp.js";
1
+ import { parseCertificateFromSource as e } from "../x509/parse.js";
2
+ import { checkCertificateRevocationAgainstCrl as t, parseCertificateRevocationListDerOrThrow as n, parseCertificateRevocationListPemOrThrow as r, revocationReasonFromCode as i } from "./crl.js";
3
+ import { parseOcspResponseDerOrThrow as a, parseOcspResponsePemOrThrow as o, validateOcspResponse as s } from "./ocsp.js";
4
4
  //#region src/revocation/chain.ts
5
5
  /**
6
6
  * Granular reasons why revocation status could not be determined.
@@ -15,7 +15,7 @@ import { parseOcspResponseDerOrThrow, parseOcspResponsePemOrThrow, validateOcspR
15
15
  * `crl_signer_revoked`, `crl_signer_indeterminate`, and OCSP equivalents
16
16
  * - **Freshness**: `crl_expired`, `ocsp_response_expired`
17
17
  */
18
- const REVOCATION_INDETERMINATE_REASONS = [
18
+ const c = [
19
19
  "no_applicable_crl",
20
20
  "no_applicable_ocsp",
21
21
  "distribution_point_mismatch",
@@ -38,17 +38,15 @@ const REVOCATION_INDETERMINATE_REASONS = [
38
38
  /**
39
39
  * Parses a CRL from various source formats.
40
40
  */
41
- function parseCrlFromSource(source) {
42
- if (typeof source === "object" && "issuer" in source) return source;
43
- if (typeof source === "string") return parseCertificateRevocationListPemOrThrow(source);
44
- return parseCertificateRevocationListDerOrThrow(source);
41
+ function parseCrlFromSource(e) {
42
+ return typeof e == "object" && "issuer" in e ? e : typeof e == "string" ? r(e) : n(e);
45
43
  }
46
44
  /**
47
45
  * Parses a certificate from various source formats, returning undefined on failure.
48
46
  */
49
- function parseCertificateSafe(source) {
47
+ function parseCertificateSafe(t) {
50
48
  try {
51
- return parseCertificateFromSource(source);
49
+ return e(t);
52
50
  } catch {
53
51
  return;
54
52
  }
@@ -57,135 +55,116 @@ function parseCertificateSafe(source) {
57
55
  * Compares two certificates by DER bytes for identity.
58
56
  * Reference equality fails when same cert is parsed from different sources.
59
57
  */
60
- function sameCertificate(a, b) {
61
- if (a.der.length !== b.der.length) return false;
62
- for (let i = 0; i < a.der.length; i++) if (a.der[i] !== b.der[i]) return false;
63
- return true;
58
+ function sameCertificate(e, t) {
59
+ if (e.der.length !== t.der.length) return !1;
60
+ for (let n = 0; n < e.der.length; n++) if (e.der[n] !== t.der[n]) return !1;
61
+ return !0;
64
62
  }
65
63
  /**
66
64
  * Searches for a certificate that could have signed the given CRL.
67
65
  * Matches by AKI/SKI or issuer/subject DN.
68
66
  */
69
- function findIndirectCrlIssuer(crl, extraCertificates, chain) {
70
- const parsedExtras = [];
71
- for (const source of extraCertificates) {
72
- const parsed = parseCertificateSafe(source);
73
- if (parsed !== void 0) parsedExtras.push(parsed);
67
+ function findIndirectCrlIssuer(e, t, n) {
68
+ let r = [];
69
+ for (let e of t) {
70
+ let t = parseCertificateSafe(e);
71
+ t !== void 0 && r.push(t);
74
72
  }
75
- const candidates = [...parsedExtras, ...chain];
76
- if (crl.authorityKeyIdentifier !== void 0) {
77
- for (const candidate of candidates) if (candidate.subjectKeyIdentifier !== void 0 && normalizeHex(crl.authorityKeyIdentifier) === normalizeHex(candidate.subjectKeyIdentifier)) return candidate;
73
+ let i = [...r, ...n];
74
+ if (e.authorityKeyIdentifier !== void 0) {
75
+ for (let t of i) if (t.subjectKeyIdentifier !== void 0 && normalizeHex(e.authorityKeyIdentifier) === normalizeHex(t.subjectKeyIdentifier)) return t;
78
76
  }
79
- for (const candidate of candidates) if (crl.issuer.derHex === candidate.subject.derHex) return candidate;
77
+ for (let t of i) if (e.issuer.derHex === t.subject.derHex) return t;
80
78
  }
81
79
  /** Lowercases a hex string for bytewise comparison. */
82
- function normalizeHex(value) {
83
- return value.toLowerCase();
80
+ function normalizeHex(e) {
81
+ return e.toLowerCase();
84
82
  }
85
83
  /**
86
84
  * Builds a unique cache key for a certificate.
87
85
  * Uses issuer DN + serial number which uniquely identifies a cert.
88
86
  */
89
- function certCacheKey(cert) {
90
- return `${cert.issuer.derHex}:${cert.serialNumberHex}`;
87
+ function certCacheKey(e) {
88
+ return `${e.issuer.derHex}:${e.serialNumberHex}`;
91
89
  }
92
90
  /**
93
91
  * Validates that a CRL signer certificate is not revoked.
94
92
  * Uses memoization to avoid redundant checks and detect cycles.
95
93
  */
96
- async function validateCrlSigner(signer, ctx) {
97
- const key = certCacheKey(signer);
98
- const cached = ctx.cache.get(key);
99
- if (cached === "visiting") return "resolved-indeterminate";
100
- if (cached !== void 0) return cached;
101
- ctx.cache.set(key, "visiting");
102
- const trustAnchor = ctx.chain[ctx.chain.length - 1];
103
- if (trustAnchor !== void 0 && sameCertificate(signer, trustAnchor)) {
104
- ctx.cache.set(key, "resolved-valid");
105
- return "resolved-valid";
106
- }
107
- if (ctx.chain.some((c) => sameCertificate(c, signer))) {
108
- ctx.cache.set(key, "resolved-valid");
109
- return "resolved-valid";
110
- }
111
- const signerIssuer = findSignerIssuer(signer, ctx);
112
- if (signerIssuer === void 0) {
113
- ctx.cache.set(key, "resolved-indeterminate");
114
- return "resolved-indeterminate";
115
- }
116
- const signerRevocation = await checkSignerRevocation(signer, signerIssuer, ctx);
117
- ctx.cache.set(key, signerRevocation);
118
- return signerRevocation;
94
+ async function validateCrlSigner(e, t) {
95
+ let n = certCacheKey(e), r = t.cache.get(n);
96
+ if (r === "visiting") return "resolved-indeterminate";
97
+ if (r !== void 0) return r;
98
+ t.cache.set(n, "visiting");
99
+ let i = t.chain[t.chain.length - 1];
100
+ if (i !== void 0 && sameCertificate(e, i) || t.chain.some((t) => sameCertificate(t, e))) return t.cache.set(n, "resolved-valid"), "resolved-valid";
101
+ let a = findSignerIssuer(e, t);
102
+ if (a === void 0) return t.cache.set(n, "resolved-indeterminate"), "resolved-indeterminate";
103
+ let o = await checkSignerRevocation(e, a, t);
104
+ return t.cache.set(n, o), o;
119
105
  }
120
106
  /**
121
107
  * Finds the issuer certificate for a CRL signer.
122
108
  * Searches the chain and extraCertificates by AKI/SKI or DN matching.
123
109
  */
124
- function findSignerIssuer(signer, ctx) {
125
- const parsedExtras = [];
126
- for (const source of ctx.extraCertificates) {
127
- const parsed = parseCertificateSafe(source);
128
- if (parsed !== void 0) parsedExtras.push(parsed);
129
- }
130
- const candidates = [...ctx.chain, ...parsedExtras];
131
- for (const candidate of candidates) {
132
- if (signer.authorityKeyIdentifier !== void 0 && candidate.subjectKeyIdentifier !== void 0 && normalizeHex(signer.authorityKeyIdentifier) === normalizeHex(candidate.subjectKeyIdentifier)) return candidate;
133
- if (signer.issuer.derHex === candidate.subject.derHex) return candidate;
110
+ function findSignerIssuer(e, t) {
111
+ let n = [];
112
+ for (let e of t.extraCertificates) {
113
+ let t = parseCertificateSafe(e);
114
+ t !== void 0 && n.push(t);
134
115
  }
116
+ let r = [...t.chain, ...n];
117
+ for (let t of r) if (e.authorityKeyIdentifier !== void 0 && t.subjectKeyIdentifier !== void 0 && normalizeHex(e.authorityKeyIdentifier) === normalizeHex(t.subjectKeyIdentifier) || e.issuer.derHex === t.subject.derHex) return t;
135
118
  }
136
119
  /**
137
120
  * Checks if a CRL signer certificate is revoked by examining available CRLs.
138
121
  * Recursively validates the CRL signer of any CRL used to check revocation.
139
122
  */
140
- async function checkSignerRevocation(signer, issuer, ctx) {
141
- const issuerInChain = ctx.chain.some((c) => sameCertificate(c, issuer));
142
- for (const crlSource of ctx.crls) {
143
- let crl;
123
+ async function checkSignerRevocation(e, n, r) {
124
+ let i = r.chain.some((e) => sameCertificate(e, n));
125
+ for (let i of r.crls) {
126
+ let a;
144
127
  try {
145
- crl = parseCrlFromSource(crlSource);
128
+ a = parseCrlFromSource(i);
146
129
  } catch {
147
130
  continue;
148
131
  }
149
- const result = await checkCertificateRevocationAgainstCrl({
150
- certificate: signer,
151
- issuerCertificate: issuer,
152
- crl,
153
- at: ctx.at
132
+ let o = await t({
133
+ certificate: e,
134
+ issuerCertificate: n,
135
+ crl: a,
136
+ at: r.at
154
137
  });
155
- if (result.ok) {
156
- if (result.value.status === "revoked") return "resolved-revoked";
157
- if (result.value.status === "good") {
158
- if (await validateCrlSigner(issuer, ctx) === "resolved-valid") return "resolved-valid";
159
- }
138
+ if (o.ok) {
139
+ if (o.value.status === "revoked") return "resolved-revoked";
140
+ if (o.value.status === "good" && await validateCrlSigner(n, r) === "resolved-valid") return "resolved-valid";
160
141
  }
161
142
  }
162
- if (issuerInChain) return "resolved-valid";
163
- return "resolved-indeterminate";
143
+ return i ? "resolved-valid" : "resolved-indeterminate";
164
144
  }
165
145
  /** Builds the `revoked` CertificateRevocationStatus for a CRL hit. */
166
- function buildCrlRevokedStatus(cert, signer, thisUpdate, revocationDate, reasonCode) {
146
+ function buildCrlRevokedStatus(e, t, n, r, i) {
167
147
  return {
168
- certificate: cert,
148
+ certificate: e,
169
149
  status: "revoked",
170
150
  source: {
171
151
  kind: "crl",
172
- signerCertificate: signer,
173
- thisUpdate
152
+ signerCertificate: t,
153
+ thisUpdate: n
174
154
  },
175
155
  revocationInfo: {
176
- revocationDate,
177
- ...reasonCode !== void 0 ? { reason: reasonCode } : {}
156
+ revocationDate: r,
157
+ ...i === void 0 ? {} : { reason: i }
178
158
  }
179
159
  };
180
160
  }
181
161
  /** Parses an OCSP response from PEM string or DER bytes. */
182
- function parseOcspResponseFromSource(source) {
183
- if (typeof source === "string") return parseOcspResponsePemOrThrow(source);
184
- return parseOcspResponseDerOrThrow(source);
162
+ function parseOcspResponseFromSource(e) {
163
+ return typeof e == "string" ? o(e) : a(e);
185
164
  }
186
165
  /** Maps a {@linkcode validateOcspResponse} failure code to an indeterminate reason. */
187
- function ocspIndeterminateReasonFromFailure(code) {
188
- switch (code) {
166
+ function ocspIndeterminateReasonFromFailure(e) {
167
+ switch (e) {
189
168
  case "stale_response": return "ocsp_response_expired";
190
169
  case "responder_id_mismatch":
191
170
  case "responder_chain_invalid":
@@ -200,7 +179,7 @@ function ocspIndeterminateReasonFromFailure(code) {
200
179
  }
201
180
  }
202
181
  /** Validation failures worth retrying with an explicit responder certificate. */
203
- const OCSP_RESPONDER_FAILURE_CODES = /* @__PURE__ */ new Set([
182
+ const l = /* @__PURE__ */ new Set([
204
183
  "signature_invalid",
205
184
  "responder_id_mismatch",
206
185
  "responder_chain_invalid",
@@ -210,30 +189,29 @@ const OCSP_RESPONDER_FAILURE_CODES = /* @__PURE__ */ new Set([
210
189
  * Validates an OCSP response, retrying with caller-provided extra certificates
211
190
  * as explicit responder certificates when embedded discovery fails.
212
191
  */
213
- async function validateOcspResponseWithResponderFallback(response, issuer, input, at) {
214
- const shared = {
215
- issuerCertificate: issuer,
216
- at,
217
- ...input.trustedOcspResponders !== void 0 ? { trustedOcspResponders: input.trustedOcspResponders } : {},
218
- ...input.policy?.ocspResponderRevocation !== void 0 ? { responderRevocationPolicy: input.policy.ocspResponderRevocation } : {},
219
- ...input.crls !== void 0 ? { responderRevocationCrls: input.crls } : {}
220
- };
221
- const primary = await validateOcspResponse({
222
- response,
223
- ...shared
192
+ async function validateOcspResponseWithResponderFallback(e, t, n, r) {
193
+ let i = {
194
+ issuerCertificate: t,
195
+ at: r,
196
+ ...n.trustedOcspResponders === void 0 ? {} : { trustedOcspResponders: n.trustedOcspResponders },
197
+ ...n.policy?.ocspResponderRevocation === void 0 ? {} : { responderRevocationPolicy: n.policy.ocspResponderRevocation },
198
+ ...n.crls === void 0 ? {} : { responderRevocationCrls: n.crls }
199
+ }, a = await s({
200
+ response: e,
201
+ ...i
224
202
  });
225
- if (primary.ok || !OCSP_RESPONDER_FAILURE_CODES.has(primary.code)) return primary;
226
- for (const source of input.extraCertificates ?? []) {
227
- const responder = parseCertificateSafe(source);
228
- if (responder === void 0) continue;
229
- const retry = await validateOcspResponse({
230
- response,
231
- ...shared,
232
- responderCertificate: responder
203
+ if (a.ok || !l.has(a.code)) return a;
204
+ for (let t of n.extraCertificates ?? []) {
205
+ let n = parseCertificateSafe(t);
206
+ if (n === void 0) continue;
207
+ let r = await s({
208
+ response: e,
209
+ ...i,
210
+ responderCertificate: n
233
211
  });
234
- if (retry.ok) return retry;
212
+ if (r.ok) return r;
235
213
  }
236
- return primary;
214
+ return a;
237
215
  }
238
216
  /**
239
217
  * Evaluates OCSP evidence for a single certificate (RFC 6960).
@@ -244,67 +222,64 @@ async function validateOcspResponseWithResponderFallback(response, issuer, input
244
222
  * the validator, so a serial-number match on a validated response is
245
223
  * sufficient to attribute the entry to `cert`.
246
224
  */
247
- async function evaluateOcspEvidence(cert, issuer, input) {
248
- const { ocspResponses = [], at = /* @__PURE__ */ new Date() } = input;
249
- const executionErrors = [];
250
- const reasons = /* @__PURE__ */ new Set();
251
- for (const source of ocspResponses) {
252
- let parsed;
225
+ async function evaluateOcspEvidence(e, t, n) {
226
+ let { ocspResponses: r = [], at: a = /* @__PURE__ */ new Date() } = n, o = [], s = /* @__PURE__ */ new Set();
227
+ for (let c of r) {
228
+ let r;
253
229
  try {
254
- parsed = parseOcspResponseFromSource(source);
230
+ r = parseOcspResponseFromSource(c);
255
231
  } catch (e) {
256
- executionErrors.push({
232
+ o.push({
257
233
  kind: "parse_error",
258
234
  message: e instanceof Error ? e.message : "OCSP response parse failed"
259
235
  });
260
236
  continue;
261
237
  }
262
- const entry = (parsed.responses ?? []).find((single) => normalizeHex(single.certId.serialNumberHex) === normalizeHex(cert.serialNumberHex));
263
- if (entry === void 0) continue;
264
- const validation = await validateOcspResponseWithResponderFallback(parsed, issuer, input, at);
265
- if (!validation.ok) {
266
- reasons.add(ocspIndeterminateReasonFromFailure(validation.code));
238
+ let l = (r.responses ?? []).find((t) => normalizeHex(t.certId.serialNumberHex) === normalizeHex(e.serialNumberHex));
239
+ if (l === void 0) continue;
240
+ let u = await validateOcspResponseWithResponderFallback(r, t, n, a);
241
+ if (!u.ok) {
242
+ s.add(ocspIndeterminateReasonFromFailure(u.code));
267
243
  continue;
268
244
  }
269
- if (entry.certStatus === "revoked") {
270
- const reason = revocationReasonFromCode(entry.revocationReasonCode);
245
+ if (l.certStatus === "revoked") {
246
+ let t = i(l.revocationReasonCode);
271
247
  return {
272
248
  status: {
273
- certificate: cert,
249
+ certificate: e,
274
250
  status: "revoked",
275
251
  source: {
276
252
  kind: "ocsp",
277
- thisUpdate: entry.thisUpdate
253
+ thisUpdate: l.thisUpdate
278
254
  },
279
255
  revocationInfo: {
280
- revocationDate: entry.revokedAt ?? entry.thisUpdate,
281
- ...reason !== void 0 ? { reason } : {}
256
+ revocationDate: l.revokedAt ?? l.thisUpdate,
257
+ ...t === void 0 ? {} : { reason: t }
282
258
  }
283
259
  },
284
- executionErrors
260
+ executionErrors: o
285
261
  };
286
262
  }
287
- if (entry.certStatus === "good") return {
263
+ if (l.certStatus === "good") return {
288
264
  status: {
289
- certificate: cert,
265
+ certificate: e,
290
266
  status: "good",
291
267
  source: {
292
268
  kind: "ocsp",
293
- thisUpdate: entry.thisUpdate
269
+ thisUpdate: l.thisUpdate
294
270
  }
295
271
  },
296
- executionErrors
272
+ executionErrors: o
297
273
  };
298
- reasons.add("ocsp_status_unknown");
274
+ s.add("ocsp_status_unknown");
299
275
  }
300
- if (reasons.size === 0) reasons.add("no_applicable_ocsp");
301
- return {
276
+ return s.size === 0 && s.add("no_applicable_ocsp"), {
302
277
  status: {
303
- certificate: cert,
278
+ certificate: e,
304
279
  status: "indeterminate",
305
- indeterminateReasons: [...reasons]
280
+ indeterminateReasons: [...s]
306
281
  },
307
- executionErrors
282
+ executionErrors: o
308
283
  };
309
284
  }
310
285
  /**
@@ -316,7 +291,7 @@ async function evaluateOcspEvidence(cert, issuer, input) {
316
291
  *
317
292
  * Also validates that CRL signers are not revoked (RFC 5280 §6.3.3).
318
293
  */
319
- const ALL_REASON_FLAGS = [
294
+ const u = [
320
295
  "keyCompromise",
321
296
  "cACompromise",
322
297
  "affiliationChanged",
@@ -326,104 +301,110 @@ const ALL_REASON_FLAGS = [
326
301
  "privilegeWithdrawn",
327
302
  "aACompromise"
328
303
  ];
329
- async function evaluateCrlEvidence(cert, issuer, input, signerCtx) {
330
- const { crls = [], extraCertificates = [], chain = [], at = /* @__PURE__ */ new Date() } = input;
331
- const executionErrors = [];
332
- let sawCrlSignerRevoked = false;
333
- let sawCrlSignerIndeterminate = false;
334
- let sawGood = false;
335
- let freshestGood;
336
- const coveredReasons = /* @__PURE__ */ new Set();
337
- const parsedCrls = [];
338
- for (const crlSource of crls) try {
339
- parsedCrls.push(parseCrlFromSource(crlSource));
340
- } catch (e) {
341
- executionErrors.push({
342
- kind: "parse_error",
343
- message: e instanceof Error ? e.message : "CRL parse failed"
344
- });
345
- }
346
- const baseCrls = parsedCrls.filter((crl) => crl.baseCrlNumber === void 0);
347
- const deltaCrls = parsedCrls.filter((crl) => crl.baseCrlNumber !== void 0);
348
- const checkWithIssuer = (crl, deltaCrl, crlIssuer) => {
349
- return checkCertificateRevocationAgainstCrl({
350
- certificate: cert,
351
- issuerCertificate: crlIssuer,
352
- crl,
353
- ...deltaCrl !== void 0 ? { deltaCrl } : {},
354
- at
355
- });
356
- };
357
- for (const baseCrl of baseCrls) {
358
- const applicableDelta = deltaCrls.find((d) => d.issuer.derHex === baseCrl.issuer.derHex && d.baseCrlNumber !== void 0 && baseCrl.crlNumber !== void 0 && BigInt(d.baseCrlNumber) <= BigInt(baseCrl.crlNumber));
359
- let result = await checkWithIssuer(baseCrl, applicableDelta, issuer);
360
- let effectiveSigner = issuer;
361
- if (!result.ok) {
362
- const indirectIssuer = findIndirectCrlIssuer(baseCrl, extraCertificates, chain);
363
- if (indirectIssuer !== void 0 && !sameCertificate(indirectIssuer, issuer)) {
364
- result = await checkWithIssuer(baseCrl, applicableDelta, indirectIssuer);
365
- effectiveSigner = indirectIssuer;
366
- }
367
- }
368
- if (!result.ok) continue;
369
- const signerStatus = await validateCrlSigner(effectiveSigner, signerCtx);
370
- if (signerStatus === "resolved-revoked") {
371
- sawCrlSignerRevoked = true;
304
+ async function evaluateCrlEvidence(e, t, n, r) {
305
+ let { crls: i = [], extraCertificates: a = [], chain: o = [], at: s = /* @__PURE__ */ new Date() } = n, c = {
306
+ executionErrors: [],
307
+ coveredReasons: /* @__PURE__ */ new Set(),
308
+ sawCrlSignerRevoked: !1,
309
+ sawCrlSignerIndeterminate: !1,
310
+ sawGood: !1
311
+ }, l = parseCrlEvidenceSources(i, c.executionErrors), d = l.filter((e) => e.baseCrlNumber === void 0), f = l.filter((e) => e.baseCrlNumber !== void 0);
312
+ for (let n of d) {
313
+ let i = findApplicableDeltaCrl(n, f), l = await checkCrlWithAvailableIssuers(e, n, i, t, a, o, s);
314
+ if (!l.result.ok) continue;
315
+ let u = await validateCrlSigner(l.signer, r);
316
+ if (u === "resolved-revoked") {
317
+ c.sawCrlSignerRevoked = !0;
372
318
  continue;
373
319
  }
374
- if (signerStatus === "resolved-indeterminate") {
375
- sawCrlSignerIndeterminate = true;
320
+ if (u === "resolved-indeterminate") {
321
+ c.sawCrlSignerIndeterminate = !0;
376
322
  continue;
377
323
  }
378
- const crlThisUpdate = applicableDelta !== void 0 && applicableDelta.thisUpdate.getTime() > baseCrl.thisUpdate.getTime() ? applicableDelta.thisUpdate : baseCrl.thisUpdate;
379
- if (result.value.status === "revoked") return {
380
- status: buildCrlRevokedStatus(cert, effectiveSigner, crlThisUpdate, result.value.revocationDate, result.value.reasonCode),
381
- executionErrors
382
- };
383
- sawGood = true;
384
- if (freshestGood === void 0 || crlThisUpdate.getTime() > freshestGood.thisUpdate.getTime()) freshestGood = {
385
- signer: effectiveSigner,
386
- thisUpdate: crlThisUpdate
387
- };
388
- const crlReasons = baseCrl.issuingDistributionPoint?.onlySomeReasons?.flags;
389
- if (crlReasons === void 0) for (const reason of ALL_REASON_FLAGS) coveredReasons.add(reason);
390
- else for (const reason of crlReasons) coveredReasons.add(reason);
391
- }
392
- if (sawGood && freshestGood !== void 0) {
393
- if (ALL_REASON_FLAGS.every((r) => coveredReasons.has(r))) return {
394
- status: {
395
- certificate: cert,
396
- status: "good",
397
- source: {
398
- kind: "crl",
399
- signerCertificate: freshestGood.signer,
400
- thisUpdate: freshestGood.thisUpdate
401
- }
402
- },
403
- executionErrors
404
- };
405
- return {
406
- status: {
407
- certificate: cert,
408
- status: "indeterminate",
409
- indeterminateReasons: ["reason_coverage_incomplete"]
410
- },
411
- executionErrors
324
+ let d = i !== void 0 && i.thisUpdate.getTime() > n.thisUpdate.getTime() ? i.thisUpdate : n.thisUpdate;
325
+ if (l.result.value.status === "revoked") return {
326
+ status: buildCrlRevokedStatus(e, l.signer, d, l.result.value.revocationDate, l.result.value.reasonCode),
327
+ executionErrors: c.executionErrors
412
328
  };
329
+ recordGoodCrlEvidence(c, l.signer, d, n);
413
330
  }
414
- const reasons = [];
415
- if (sawCrlSignerRevoked) reasons.push("crl_signer_revoked");
416
- else if (sawCrlSignerIndeterminate) reasons.push("crl_signer_indeterminate");
417
- else reasons.push("no_applicable_crl");
418
- return {
331
+ if (c.sawGood && c.freshestGood !== void 0) return u.every((e) => c.coveredReasons.has(e)) ? {
419
332
  status: {
420
- certificate: cert,
333
+ certificate: e,
334
+ status: "good",
335
+ source: {
336
+ kind: "crl",
337
+ signerCertificate: c.freshestGood.signer,
338
+ thisUpdate: c.freshestGood.thisUpdate
339
+ }
340
+ },
341
+ executionErrors: c.executionErrors
342
+ } : {
343
+ status: {
344
+ certificate: e,
421
345
  status: "indeterminate",
422
- indeterminateReasons: reasons
346
+ indeterminateReasons: ["reason_coverage_incomplete"]
423
347
  },
424
- executionErrors
348
+ executionErrors: c.executionErrors
349
+ };
350
+ let p = [];
351
+ return c.sawCrlSignerRevoked ? p.push("crl_signer_revoked") : c.sawCrlSignerIndeterminate ? p.push("crl_signer_indeterminate") : p.push("no_applicable_crl"), {
352
+ status: {
353
+ certificate: e,
354
+ status: "indeterminate",
355
+ indeterminateReasons: p
356
+ },
357
+ executionErrors: c.executionErrors
425
358
  };
426
359
  }
360
+ /** Parses complete and delta CRL evidence while retaining source provenance. */
361
+ function parseCrlEvidenceSources(e, t) {
362
+ let n = [];
363
+ for (let r of e) try {
364
+ n.push(parseCrlFromSource(r));
365
+ } catch (e) {
366
+ t.push({
367
+ kind: "parse_error",
368
+ message: e instanceof Error ? e.message : "CRL parse failed"
369
+ });
370
+ }
371
+ return n;
372
+ }
373
+ function findApplicableDeltaCrl(e, t) {
374
+ return t.find((t) => t.issuer.derHex === e.issuer.derHex && t.baseCrlNumber !== void 0 && e.crlNumber !== void 0 && BigInt(t.baseCrlNumber) <= BigInt(e.crlNumber));
375
+ }
376
+ /** Evaluates a CRL against each available issuer until applicable evidence is found. */
377
+ async function checkCrlWithAvailableIssuers(e, t, n, r, i, a, o) {
378
+ let s = await checkCrlWithIssuer(e, t, n, r, o);
379
+ if (s.ok) return {
380
+ result: s,
381
+ signer: r
382
+ };
383
+ let c = findIndirectCrlIssuer(t, i, a);
384
+ return c === void 0 || sameCertificate(c, r) ? {
385
+ result: s,
386
+ signer: r
387
+ } : {
388
+ result: await checkCrlWithIssuer(e, t, n, c, o),
389
+ signer: c
390
+ };
391
+ }
392
+ function checkCrlWithIssuer(e, n, r, i, a) {
393
+ return t({
394
+ certificate: e,
395
+ issuerCertificate: i,
396
+ crl: n,
397
+ ...r === void 0 ? {} : { deltaCrl: r },
398
+ at: a
399
+ });
400
+ }
401
+ function recordGoodCrlEvidence(e, t, n, r) {
402
+ e.sawGood = !0, (e.freshestGood === void 0 || n.getTime() > e.freshestGood.thisUpdate.getTime()) && (e.freshestGood = {
403
+ signer: t,
404
+ thisUpdate: n
405
+ });
406
+ for (let t of r.issuingDistributionPoint?.onlySomeReasons?.flags ?? u) e.coveredReasons.add(t);
407
+ }
427
408
  /**
428
409
  * Evaluates revocation status for a single certificate by combining OCSP and
429
410
  * CRL evidence.
@@ -435,31 +416,26 @@ async function evaluateCrlEvidence(cert, issuer, input, signerCtx) {
435
416
  * ties favoring OCSP. If neither source is definitive, indeterminate reasons
436
417
  * from both are merged.
437
418
  */
438
- async function evaluateCertificateRevocation(cert, issuer, input, signerCtx) {
439
- const prefer = input.policy?.prefer ?? "best-available";
440
- const ocsp = await evaluateOcspEvidence(cert, issuer, input);
441
- const crl = await evaluateCrlEvidence(cert, issuer, input, signerCtx);
442
- const executionErrors = [...ocsp.executionErrors, ...crl.executionErrors];
443
- const ordered = prefer === "crl" ? [crl, ocsp] : prefer === "ocsp" ? [ocsp, crl] : [ocsp, crl].sort((a, b) => (b.status.source?.thisUpdate?.getTime() ?? 0) - (a.status.source?.thisUpdate?.getTime() ?? 0));
444
- const revoked = ordered.find((evaluation) => evaluation.status.status === "revoked");
445
- if (revoked !== void 0) return {
446
- status: revoked.status,
447
- executionErrors
419
+ async function evaluateCertificateRevocation(e, t, n, r) {
420
+ let i = n.policy?.prefer ?? "best-available", a = await evaluateOcspEvidence(e, t, n), o = await evaluateCrlEvidence(e, t, n, r), s = [...a.executionErrors, ...o.executionErrors], c = i === "crl" ? [o, a] : i === "ocsp" ? [a, o] : [a, o].sort((e, t) => (t.status.source?.thisUpdate?.getTime() ?? 0) - (e.status.source?.thisUpdate?.getTime() ?? 0)), l = c.find((e) => e.status.status === "revoked");
421
+ if (l !== void 0) return {
422
+ status: l.status,
423
+ executionErrors: s
448
424
  };
449
- const good = ordered.find((evaluation) => evaluation.status.status === "good");
450
- if (good !== void 0) return {
451
- status: good.status,
452
- executionErrors
425
+ let u = c.find((e) => e.status.status === "good");
426
+ if (u !== void 0) return {
427
+ status: u.status,
428
+ executionErrors: s
453
429
  };
454
- const reasons = /* @__PURE__ */ new Set();
455
- for (const evaluation of ordered) for (const reason of evaluation.status.indeterminateReasons ?? []) reasons.add(reason);
430
+ let d = /* @__PURE__ */ new Set();
431
+ for (let e of c) for (let t of e.status.indeterminateReasons ?? []) d.add(t);
456
432
  return {
457
433
  status: {
458
- certificate: cert,
434
+ certificate: e,
459
435
  status: "indeterminate",
460
- indeterminateReasons: [...reasons]
436
+ indeterminateReasons: [...d]
461
437
  },
462
- executionErrors
438
+ executionErrors: s
463
439
  };
464
440
  }
465
441
  /**
@@ -481,11 +457,10 @@ async function evaluateCertificateRevocation(cert, issuer, input, signerCtx) {
481
457
  * }
482
458
  * ```
483
459
  */
484
- async function checkChainRevocation(input) {
485
- const { chain, policy, crls = [], extraCertificates = [], at = /* @__PURE__ */ new Date() } = input;
486
- const mode = policy?.mode ?? "hard-fail";
487
- if (chain.length === 0) return {
488
- ok: true,
460
+ async function checkChainRevocation(e) {
461
+ let { chain: t, policy: n, crls: r = [], extraCertificates: i = [], at: a = /* @__PURE__ */ new Date() } = e, o = n?.mode ?? "hard-fail";
462
+ if (t.length === 0) return {
463
+ ok: !0,
489
464
  value: {
490
465
  decision: "allow",
491
466
  summary: {
@@ -495,42 +470,34 @@ async function checkChainRevocation(input) {
495
470
  certificates: []
496
471
  }
497
472
  };
498
- const signerCtx = {
473
+ let s = {
499
474
  cache: /* @__PURE__ */ new Map(),
500
- chain,
501
- crls,
502
- extraCertificates,
503
- at
504
- };
505
- const certsToCheck = chain.slice(0, -1);
506
- const certificates = [];
507
- const revokedCertificates = [];
508
- const indeterminateCertificates = [];
509
- const allExecutionErrors = [];
510
- for (let i = 0; i < certsToCheck.length; i++) {
511
- const cert = certsToCheck[i];
512
- const issuer = chain[i + 1];
513
- if (cert === void 0 || issuer === void 0) continue;
514
- const { status, executionErrors } = await evaluateCertificateRevocation(cert, issuer, input, signerCtx);
515
- certificates.push(status);
516
- allExecutionErrors.push(...executionErrors);
517
- if (status.status === "revoked") revokedCertificates.push(cert);
518
- else if (status.status === "indeterminate") indeterminateCertificates.push(cert);
475
+ chain: t,
476
+ crls: r,
477
+ extraCertificates: i,
478
+ at: a
479
+ }, c = t.slice(0, -1), l = [], u = [], d = [], f = [];
480
+ for (let n = 0; n < c.length; n++) {
481
+ let r = c[n], i = t[n + 1];
482
+ if (r === void 0 || i === void 0) continue;
483
+ let { status: a, executionErrors: o } = await evaluateCertificateRevocation(r, i, e, s);
484
+ l.push(a), f.push(...o), a.status === "revoked" ? u.push(r) : a.status === "indeterminate" && d.push(r);
519
485
  }
520
- const hasRevoked = revokedCertificates.length > 0;
521
- const hasIndeterminate = indeterminateCertificates.length > 0;
486
+ let p = u.length > 0, m = d.length > 0;
522
487
  return {
523
- ok: true,
488
+ ok: !0,
524
489
  value: {
525
- decision: hasRevoked ? "deny" : hasIndeterminate && mode === "hard-fail" ? "deny" : "allow",
490
+ decision: p || m && o === "hard-fail" ? "deny" : "allow",
526
491
  summary: {
527
- revokedCertificates,
528
- indeterminateCertificates
492
+ revokedCertificates: u,
493
+ indeterminateCertificates: d
529
494
  },
530
- certificates,
531
- ...allExecutionErrors.length > 0 ? { executionErrors: allExecutionErrors } : {}
495
+ certificates: l,
496
+ ...f.length > 0 ? { executionErrors: f } : {}
532
497
  }
533
498
  };
534
499
  }
535
500
  //#endregion
536
- export { REVOCATION_INDETERMINATE_REASONS, checkChainRevocation };
501
+ export { c as REVOCATION_INDETERMINATE_REASONS, checkChainRevocation };
502
+
503
+ //# sourceMappingURL=chain.js.map