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,5 +1,5 @@
1
- import { OIDS } from "../asn1/oids.js";
2
- import { compareDistinguishedNames } from "../shared/dn.js";
1
+ import { OIDS as e } from "../asn1/oids2.js";
2
+ import { compareDistinguishedNames as t } from "../shared/dn.js";
3
3
  //#region src/internal/verify/policy-engine.ts
4
4
  /**
5
5
  * Internal RFC 9618 policy-validation engine.
@@ -15,13 +15,13 @@ import { compareDistinguishedNames } from "../shared/dn.js";
15
15
  * Counter values default to `chainLength + 1` (effectively disabled) unless
16
16
  * the corresponding input flag is `true`.
17
17
  */
18
- function createPolicyValidationState(input, chainLength) {
19
- const disabledCounter = chainLength + 1;
18
+ function createPolicyValidationState(e, t) {
19
+ let n = t + 1;
20
20
  return {
21
- initialPolicySet: input.initialPolicySet ?? "any",
22
- explicitPolicy: input.requireExplicitPolicy === true ? 0 : disabledCounter,
23
- inhibitPolicyMapping: input.inhibitPolicyMapping === true ? 0 : disabledCounter,
24
- inhibitAnyPolicy: input.inhibitAnyPolicy === true ? 0 : disabledCounter,
21
+ initialPolicySet: e.initialPolicySet ?? "any",
22
+ explicitPolicy: e.requireExplicitPolicy === !0 ? 0 : n,
23
+ inhibitPolicyMapping: e.inhibitPolicyMapping === !0 ? 0 : n,
24
+ inhibitAnyPolicy: e.inhibitAnyPolicy === !0 ? 0 : n,
25
25
  validPolicyGraph: createInitialPolicyGraph()
26
26
  };
27
27
  }
@@ -29,354 +29,335 @@ function createPolicyValidationState(input, chainLength) {
29
29
  * Walks the chain root-to-leaf, updating policy state per certificate,
30
30
  * then checks whether the resulting policy set satisfies caller requirements.
31
31
  */
32
- function evaluatePolicyChain(chain, state) {
33
- if (chain.length === 0) throw new RangeError("policy validation requires at least one certificate");
34
- processPolicyState(chain, state);
35
- const outcome = derivePolicyValidationOutcome(chain, state);
36
- if (state.explicitPolicy === 0 && outcome.userConstrainedPolicies.length === 0) return {
37
- ok: false,
32
+ function evaluatePolicyChain(e, t) {
33
+ if (e.length === 0) throw RangeError("policy validation requires at least one certificate");
34
+ processPolicyState(e, t);
35
+ let n = derivePolicyValidationOutcome(e, t);
36
+ return t.explicitPolicy === 0 && n.userConstrainedPolicies.length === 0 ? {
37
+ ok: !1,
38
38
  error: {
39
39
  code: "explicit_policy_required",
40
40
  message: "policy validation requires an explicit permitted policy",
41
41
  details: {
42
- expected: state.initialPolicySet === "any" ? "explicit policy" : state.initialPolicySet.join(","),
43
- actual: describeFinalPolicies(outcome.userConstrainedPolicies)
42
+ expected: t.initialPolicySet === "any" ? "explicit policy" : t.initialPolicySet.join(","),
43
+ actual: describeFinalPolicies(n.userConstrainedPolicies)
44
44
  }
45
45
  }
46
- };
47
- if (state.initialPolicySet !== "any" && outcome.userConstrainedPolicies.length === 0) return {
48
- ok: false,
46
+ } : t.initialPolicySet !== "any" && n.userConstrainedPolicies.length === 0 ? {
47
+ ok: !1,
49
48
  error: {
50
49
  code: "initial_policy_set_not_satisfied",
51
50
  message: "certificate chain does not satisfy the requested initial policy set",
52
51
  details: {
53
- expected: state.initialPolicySet.join(","),
54
- actual: describeFinalPolicies(outcome.userConstrainedPolicies)
52
+ expected: t.initialPolicySet.join(","),
53
+ actual: describeFinalPolicies(n.userConstrainedPolicies)
55
54
  }
56
55
  }
57
- };
58
- return {
59
- ok: true,
60
- value: outcome
56
+ } : {
57
+ ok: !0,
58
+ value: n
61
59
  };
62
60
  }
63
61
  /** Builds the depth-0 graph with a single anyPolicy root node. */
64
62
  function createInitialPolicyGraph() {
65
- const rootNode = createPolicyGraphNode(0, OIDS.anyPolicy, void 0, [OIDS.anyPolicy], []);
66
- return { nodesByDepth: [/* @__PURE__ */ new Map([[policyNodeKey(0, OIDS.anyPolicy), rootNode]])] };
63
+ let t = createPolicyGraphNode(0, e.anyPolicy, void 0, [e.anyPolicy], []);
64
+ return { nodesByDepth: [/* @__PURE__ */ new Map([[policyNodeKey(0, e.anyPolicy), t]])] };
67
65
  }
68
66
  /** Constructs a new {@linkcode PolicyGraphNode} with empty child-key set. */
69
- function createPolicyGraphNode(depth, validPolicy, qualifierSet, expectedPolicySet, parentKeys) {
67
+ function createPolicyGraphNode(e, t, n, r, i) {
70
68
  return {
71
- depth,
72
- validPolicy,
73
- ...qualifierSet === void 0 ? {} : { qualifierSet },
74
- expectedPolicySet: new Set(expectedPolicySet),
75
- parentKeys: new Set(parentKeys),
69
+ depth: e,
70
+ validPolicy: t,
71
+ ...n === void 0 ? {} : { qualifierSet: n },
72
+ expectedPolicySet: new Set(r),
73
+ parentKeys: new Set(i),
76
74
  childKeys: /* @__PURE__ */ new Set()
77
75
  };
78
76
  }
79
77
  /** Canonical map key for a graph node: `"depth:policyOID"`. */
80
- function policyNodeKey(depth, validPolicy) {
81
- return `${String(depth)}:${validPolicy}`;
78
+ function policyNodeKey(e, t) {
79
+ return `${String(e)}:${t}`;
82
80
  }
83
81
  /** Iterates root-to-leaf (skipping root), applying each certificate's policies to the state. */
84
- function processPolicyState(chain, state) {
85
- if (chain.length === 1) {
86
- const certificate = chain[0];
87
- if (certificate === void 0) throw new Error(`missing certificate at chain index 0 (chain length ${String(chain.length)})`);
88
- processPolicyCertificate(state, certificate, 1, true);
82
+ function processPolicyState(e, t) {
83
+ if (e.length === 1) {
84
+ let n = e[0];
85
+ if (n === void 0) throw Error(`missing certificate at chain index 0 (chain length ${String(e.length)})`);
86
+ processPolicyCertificate(t, n, 1, !0);
89
87
  return;
90
88
  }
91
- const leafDepth = chain.length - 1;
92
- for (let index = chain.length - 2; index >= 0; index -= 1) {
93
- const certificate = chain[index];
94
- if (certificate === void 0) throw new Error(`missing certificate at chain index ${String(index)} (chain length ${String(chain.length)})`);
95
- const depth = leafDepth - index;
96
- processPolicyCertificate(state, certificate, depth, depth === leafDepth);
89
+ let n = e.length - 1;
90
+ for (let r = e.length - 2; r >= 0; --r) {
91
+ let i = e[r];
92
+ if (i === void 0) throw Error(`missing certificate at chain index ${String(r)} (chain length ${String(e.length)})`);
93
+ let a = n - r;
94
+ processPolicyCertificate(t, i, a, a === n);
97
95
  }
98
96
  }
99
97
  /** Extracts the final authority- and user-constrained policy sets from the completed graph. */
100
- function derivePolicyValidationOutcome(chain, state) {
101
- const authorityConstrainedPolicies = collectAuthorityConstrainedPolicies(chain, state.validPolicyGraph);
102
- const rootDomainPolicies = collectRootDomainPolicies(chain, state.validPolicyGraph);
98
+ function derivePolicyValidationOutcome(e, t) {
99
+ let n = collectAuthorityConstrainedPolicies(e, t.validPolicyGraph), r = collectRootDomainPolicies(e, t.validPolicyGraph);
103
100
  return {
104
- authorityConstrainedPolicies: [...authorityConstrainedPolicies.values()].sort(comparePolicies),
105
- userConstrainedPolicies: deriveUserConstrainedPolicies(authorityConstrainedPolicies, rootDomainPolicies, state.initialPolicySet)
101
+ authorityConstrainedPolicies: [...n.values()].sort(comparePolicies),
102
+ userConstrainedPolicies: deriveUserConstrainedPolicies(n, r, t.initialPolicySet)
106
103
  };
107
104
  }
108
105
  /** Collects leaf-depth policies whose graph paths trace back to the anyPolicy root. */
109
- function collectAuthorityConstrainedPolicies(chain, graph) {
110
- if (graph === null) return /* @__PURE__ */ new Map();
111
- const leafDepth = Math.max(1, chain.length - 1);
112
- const finalDepth = graph.nodesByDepth[leafDepth];
113
- if (finalDepth === void 0) return /* @__PURE__ */ new Map();
114
- const authorityPolicies = /* @__PURE__ */ new Map();
115
- for (const [key, node] of finalDepth) {
116
- if (node.validPolicy === OIDS.anyPolicy) {
117
- authorityPolicies.set(OIDS.anyPolicy, buildConstrainedPolicy(OIDS.anyPolicy, node.qualifierSet));
106
+ function collectAuthorityConstrainedPolicies(t, n) {
107
+ if (n === null) return /* @__PURE__ */ new Map();
108
+ let r = Math.max(1, t.length - 1), i = n.nodesByDepth[r];
109
+ if (i === void 0) return /* @__PURE__ */ new Map();
110
+ let a = /* @__PURE__ */ new Map();
111
+ for (let [t, r] of i) {
112
+ if (r.validPolicy === e.anyPolicy) {
113
+ a.set(e.anyPolicy, buildConstrainedPolicy(e.anyPolicy, r.qualifierSet));
118
114
  continue;
119
115
  }
120
- if (reachesAuthorityRoot(graph, key)) authorityPolicies.set(node.validPolicy, buildConstrainedPolicy(node.validPolicy, node.qualifierSet));
116
+ reachesAuthorityRoot(n, t) && a.set(r.validPolicy, buildConstrainedPolicy(r.validPolicy, r.qualifierSet));
121
117
  }
122
- return authorityPolicies;
118
+ return a;
123
119
  }
124
120
  /** DFS upward through parent links to check if the node ultimately connects to the depth-0 anyPolicy root. */
125
- function reachesAuthorityRoot(graph, nodeKey) {
126
- const pending = [nodeKey];
127
- const visited = /* @__PURE__ */ new Set();
128
- while (pending.length > 0) {
129
- const currentKey = pending.pop();
130
- if (currentKey === void 0 || visited.has(currentKey)) continue;
131
- visited.add(currentKey);
132
- const node = getPolicyGraphNode(graph, currentKey);
133
- if (node === void 0) continue;
134
- for (const parentKey of node.parentKeys) {
135
- const parent = getPolicyGraphNode(graph, parentKey);
136
- if (parent === void 0) continue;
137
- if (parent.depth === 0 && parent.validPolicy === OIDS.anyPolicy) return true;
138
- pending.push(parentKey);
121
+ function reachesAuthorityRoot(t, n) {
122
+ let r = [n], i = /* @__PURE__ */ new Set();
123
+ for (; r.length > 0;) {
124
+ let n = r.pop();
125
+ if (n === void 0 || i.has(n)) continue;
126
+ i.add(n);
127
+ let a = getPolicyGraphNode(t, n);
128
+ if (a !== void 0) for (let n of a.parentKeys) {
129
+ let i = getPolicyGraphNode(t, n);
130
+ if (i !== void 0) {
131
+ if (i.depth === 0 && i.validPolicy === e.anyPolicy) return !0;
132
+ r.push(n);
133
+ }
139
134
  }
140
135
  }
141
- return false;
136
+ return !1;
142
137
  }
143
138
  /** Like authority-constrained, but records the OID at the depth-1 node that connects to root. */
144
- function collectRootDomainPolicies(chain, graph) {
145
- if (graph === null) return /* @__PURE__ */ new Map();
146
- const leafDepth = Math.max(1, chain.length - 1);
147
- const finalDepth = graph.nodesByDepth[leafDepth];
148
- if (finalDepth === void 0) return /* @__PURE__ */ new Map();
149
- const rootPolicies = /* @__PURE__ */ new Map();
150
- for (const [key, node] of finalDepth) {
151
- if (node.validPolicy === OIDS.anyPolicy) {
152
- rootPolicies.set(OIDS.anyPolicy, buildConstrainedPolicy(OIDS.anyPolicy, node.qualifierSet));
139
+ function collectRootDomainPolicies(t, n) {
140
+ if (n === null) return /* @__PURE__ */ new Map();
141
+ let r = Math.max(1, t.length - 1), i = n.nodesByDepth[r];
142
+ if (i === void 0) return /* @__PURE__ */ new Map();
143
+ let a = /* @__PURE__ */ new Map();
144
+ for (let [t, r] of i) {
145
+ if (r.validPolicy === e.anyPolicy) {
146
+ a.set(e.anyPolicy, buildConstrainedPolicy(e.anyPolicy, r.qualifierSet));
153
147
  continue;
154
148
  }
155
- collectAuthorityConstrainedPolicyRoots(graph, key, rootPolicies);
149
+ collectAuthorityConstrainedPolicyRoots(n, t, a);
156
150
  }
157
- return rootPolicies;
151
+ return a;
158
152
  }
159
153
  /** Walks parent links upward, collecting the depth-1 OID that first connects to the root anyPolicy. */
160
- function collectAuthorityConstrainedPolicyRoots(graph, nodeKey, authorityPolicies) {
161
- const pending = [nodeKey];
162
- const visited = /* @__PURE__ */ new Set();
163
- while (pending.length > 0) {
164
- const currentKey = pending.pop();
165
- if (currentKey === void 0 || visited.has(currentKey)) continue;
166
- visited.add(currentKey);
167
- const node = getPolicyGraphNode(graph, currentKey);
168
- if (node === void 0) continue;
169
- for (const parentKey of node.parentKeys) {
170
- const parent = getPolicyGraphNode(graph, parentKey);
171
- if (parent === void 0) continue;
172
- if (parent.depth === 0 && parent.validPolicy === OIDS.anyPolicy) {
173
- authorityPolicies.set(node.validPolicy, buildConstrainedPolicy(node.validPolicy, currentKey === nodeKey ? node.qualifierSet : void 0));
174
- continue;
154
+ function collectAuthorityConstrainedPolicyRoots(t, n, r) {
155
+ let i = [n], a = /* @__PURE__ */ new Set();
156
+ for (; i.length > 0;) {
157
+ let o = i.pop();
158
+ if (o === void 0 || a.has(o)) continue;
159
+ a.add(o);
160
+ let s = getPolicyGraphNode(t, o);
161
+ if (s !== void 0) for (let a of s.parentKeys) {
162
+ let c = getPolicyGraphNode(t, a);
163
+ if (c !== void 0) {
164
+ if (c.depth === 0 && c.validPolicy === e.anyPolicy) {
165
+ r.set(s.validPolicy, buildConstrainedPolicy(s.validPolicy, o === n ? s.qualifierSet : void 0));
166
+ continue;
167
+ }
168
+ i.push(a);
175
169
  }
176
- pending.push(parentKey);
177
170
  }
178
171
  }
179
172
  }
180
173
  /** Intersects root-domain policies with the caller's initial-policy-set. */
181
- function deriveUserConstrainedPolicies(finalAuthorityConstrainedPolicies, rootDomainPolicies, initialPolicySet) {
182
- if (initialPolicySet === "any") return [...finalAuthorityConstrainedPolicies.values()].sort(comparePolicies);
183
- const anyPolicy = rootDomainPolicies.get(OIDS.anyPolicy);
184
- const eeHasAnyPolicy = finalAuthorityConstrainedPolicies.has(OIDS.anyPolicy);
185
- const constrained = /* @__PURE__ */ new Map();
186
- for (const policyIdentifier of initialPolicySet) {
187
- const direct = rootDomainPolicies.get(policyIdentifier);
188
- if (direct !== void 0) {
189
- constrained.set(policyIdentifier, direct);
174
+ function deriveUserConstrainedPolicies(t, n, r) {
175
+ if (r === "any") return [...t.values()].sort(comparePolicies);
176
+ let i = n.get(e.anyPolicy), a = t.has(e.anyPolicy), o = /* @__PURE__ */ new Map();
177
+ for (let e of r) {
178
+ let r = n.get(e);
179
+ if (r !== void 0) {
180
+ o.set(e, r);
190
181
  continue;
191
182
  }
192
- if (anyPolicy !== void 0 && (finalAuthorityConstrainedPolicies.has(policyIdentifier) || eeHasAnyPolicy)) constrained.set(policyIdentifier, buildConstrainedPolicy(policyIdentifier, anyPolicy.policyQualifiers));
183
+ i !== void 0 && (t.has(e) || a) && o.set(e, buildConstrainedPolicy(e, i.policyQualifiers));
193
184
  }
194
- return [...constrained.values()].sort(comparePolicies);
185
+ return [...o.values()].sort(comparePolicies);
195
186
  }
196
187
  /** Constructs a {@linkcode ConstrainedPolicy}, omitting qualifiers when absent. */
197
- function buildConstrainedPolicy(policyIdentifier, policyQualifiers) {
188
+ function buildConstrainedPolicy(e, t) {
198
189
  return {
199
- policyIdentifier,
200
- ...policyQualifiers === void 0 ? {} : { policyQualifiers }
190
+ policyIdentifier: e,
191
+ ...t === void 0 ? {} : { policyQualifiers: t }
201
192
  };
202
193
  }
203
194
  /** Lexicographic comparator for sorting policies by OID string. */
204
- function comparePolicies(left, right) {
205
- return left.policyIdentifier.localeCompare(right.policyIdentifier);
195
+ function comparePolicies(e, t) {
196
+ return e.policyIdentifier.localeCompare(t.policyIdentifier);
206
197
  }
207
198
  /** Human-readable comma-joined OID list for error messages; `"<none>"` when empty. */
208
- function describeFinalPolicies(policies) {
209
- return policies.length === 0 ? "<none>" : policies.map((policy) => policy.policyIdentifier).join(",");
199
+ function describeFinalPolicies(e) {
200
+ return e.length === 0 ? "<none>" : e.map((e) => e.policyIdentifier).join(",");
210
201
  }
211
202
  /** Applies one certificate's policies, mappings, and constraints to the running state. */
212
- function processPolicyCertificate(state, certificate, depth, isLeaf) {
213
- const certificatePolicies = normalizeCertificatePolicies(certificate.certificatePolicies);
214
- if (state.validPolicyGraph !== null && certificatePolicies === void 0) state.validPolicyGraph = null;
215
- else if (state.validPolicyGraph !== null && certificatePolicies !== void 0) {
216
- applyCertificatePolicyStep(state.validPolicyGraph, certificatePolicies, depth, state.inhibitAnyPolicy > 0 || !isLeaf && isSelfIssued(certificate));
217
- if (certificate.policyMappings !== void 0) applyPolicyMappingsStep(state.validPolicyGraph, depth, certificate.policyMappings, state.inhibitPolicyMapping > 0);
218
- }
219
- updatePolicyCounters(state, certificate, isLeaf);
203
+ function processPolicyCertificate(e, t, n, r) {
204
+ let i = normalizeCertificatePolicies(t.certificatePolicies);
205
+ e.validPolicyGraph !== null && i === void 0 ? e.validPolicyGraph = null : e.validPolicyGraph !== null && i !== void 0 && (applyCertificatePolicyStep(e.validPolicyGraph, i, n, e.inhibitAnyPolicy > 0 || !r && isSelfIssued(t)), t.policyMappings !== void 0 && applyPolicyMappingsStep(e.validPolicyGraph, n, t.policyMappings, e.inhibitPolicyMapping > 0)), updatePolicyCounters(e, t, r);
220
206
  }
221
207
  /** Deduplicates certificate policies by OID, keeping the first occurrence. */
222
- function normalizeCertificatePolicies(policies) {
223
- if (policies === void 0) return;
224
- const byOid = /* @__PURE__ */ new Map();
225
- for (const policy of policies) if (!byOid.has(policy.policyIdentifier)) byOid.set(policy.policyIdentifier, policy);
226
- return byOid;
208
+ function normalizeCertificatePolicies(e) {
209
+ if (e === void 0) return;
210
+ let t = /* @__PURE__ */ new Map();
211
+ for (let n of e) t.has(n.policyIdentifier) || t.set(n.policyIdentifier, n);
212
+ return t;
227
213
  }
228
214
  /** Extends the policy graph by one depth level using a certificate's policy extension. */
229
- function applyCertificatePolicyStep(graph, certificatePolicies, depth, allowAnyPolicyExpansion) {
230
- const previousDepth = graph.nodesByDepth[depth - 1] ?? /* @__PURE__ */ new Map();
231
- const currentDepth = /* @__PURE__ */ new Map();
232
- graph.nodesByDepth[depth] = currentDepth;
233
- const anyPolicyInfo = certificatePolicies.get(OIDS.anyPolicy);
234
- const previousAnyPolicy = previousDepth.get(policyNodeKey(depth - 1, OIDS.anyPolicy));
235
- for (const policy of certificatePolicies.values()) {
236
- if (policy.policyIdentifier === OIDS.anyPolicy) continue;
237
- const matchingParents = collectParentsForExpectedPolicy(previousDepth, policy.policyIdentifier);
238
- if (matchingParents.length > 0) addOrMergePolicyNode(graph, currentDepth, depth, policy.policyIdentifier, policy.policyQualifiers, matchingParents, [policy.policyIdentifier]);
239
- }
240
- if (previousAnyPolicy !== void 0) {
241
- const previousAnyPolicyKey = policyNodeKey(depth - 1, OIDS.anyPolicy);
242
- for (const policy of certificatePolicies.values()) {
243
- if (policy.policyIdentifier === OIDS.anyPolicy) continue;
244
- if (currentDepth.has(policyNodeKey(depth, policy.policyIdentifier))) continue;
245
- addOrMergePolicyNode(graph, currentDepth, depth, policy.policyIdentifier, policy.policyQualifiers, [previousAnyPolicyKey], [policy.policyIdentifier]);
246
- }
215
+ function applyCertificatePolicyStep(t, n, r, i) {
216
+ let a = t.nodesByDepth[r - 1] ?? /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map();
217
+ t.nodesByDepth[r] = o;
218
+ let s = n.get(e.anyPolicy), c = a.get(policyNodeKey(r - 1, e.anyPolicy)), l = policyNodeKey(r - 1, e.anyPolicy);
219
+ addPolicyNodesFromExpectedParents(t, o, a, n, r), c !== void 0 && addPolicyNodesFromPreviousAnyPolicy(t, o, n, r, l), s !== void 0 && i && addAnyPolicyExpansionNodes(t, o, a, r, s, l, c !== void 0), prunePolicyGraph(t, r - 1);
220
+ }
221
+ /** Adds explicit certificate-policy nodes whose expected policies match parent nodes. */
222
+ function addPolicyNodesFromExpectedParents(t, n, r, i, a) {
223
+ for (let o of i.values()) {
224
+ if (o.policyIdentifier === e.anyPolicy) continue;
225
+ let i = collectParentsForExpectedPolicy(r, o.policyIdentifier);
226
+ i.length !== 0 && addOrMergePolicyNode(t, n, a, o.policyIdentifier, o.policyQualifiers, i, [o.policyIdentifier]);
247
227
  }
248
- if (anyPolicyInfo !== void 0 && allowAnyPolicyExpansion) for (const [validPolicy, parentKeys] of collectExpectedPolicyParents(previousDepth)) {
249
- if (currentDepth.has(policyNodeKey(depth, validPolicy))) continue;
250
- const expansionParents = validPolicy === OIDS.anyPolicy ? previousAnyPolicy === void 0 ? [] : [policyNodeKey(depth - 1, OIDS.anyPolicy)] : parentKeys;
251
- if (expansionParents.length === 0) continue;
252
- addOrMergePolicyNode(graph, currentDepth, depth, validPolicy, anyPolicyInfo.policyQualifiers, expansionParents, [validPolicy]);
228
+ }
229
+ function addPolicyNodesFromPreviousAnyPolicy(t, n, r, i, a) {
230
+ for (let o of r.values()) o.policyIdentifier !== e.anyPolicy && (n.has(policyNodeKey(i, o.policyIdentifier)) || addOrMergePolicyNode(t, n, i, o.policyIdentifier, o.policyQualifiers, [a], [o.policyIdentifier]));
231
+ }
232
+ /** Expands an accepted anyPolicy entry into the policies expected by the previous depth. */
233
+ function addAnyPolicyExpansionNodes(e, t, n, r, i, a, o) {
234
+ for (let [s, c] of collectExpectedPolicyParents(n)) {
235
+ if (t.has(policyNodeKey(r, s))) continue;
236
+ let n = collectAnyPolicyExpansionParents(s, c, a, o);
237
+ n.length !== 0 && addOrMergePolicyNode(e, t, r, s, i.policyQualifiers, n, [s]);
253
238
  }
254
- prunePolicyGraph(graph, depth - 1);
239
+ }
240
+ function collectAnyPolicyExpansionParents(t, n, r, i) {
241
+ return t === e.anyPolicy ? i ? [r] : [] : n;
255
242
  }
256
243
  /** Finds all previous-depth nodes whose expectedPolicySet contains the given OID. */
257
- function collectParentsForExpectedPolicy(nodes, policyIdentifier) {
258
- const parents = [];
259
- for (const [key, node] of nodes) if (node.expectedPolicySet.has(policyIdentifier)) parents.push(key);
260
- return parents;
244
+ function collectParentsForExpectedPolicy(e, t) {
245
+ let n = [];
246
+ for (let [r, i] of e) i.expectedPolicySet.has(t) && n.push(r);
247
+ return n;
261
248
  }
262
249
  /** Groups all previous-depth nodes by each OID in their expectedPolicySet. */
263
- function collectExpectedPolicyParents(nodes) {
264
- const parentsByPolicy = /* @__PURE__ */ new Map();
265
- for (const [key, node] of nodes) for (const expectedPolicy of node.expectedPolicySet) {
266
- const parents = parentsByPolicy.get(expectedPolicy);
267
- if (parents === void 0) {
268
- parentsByPolicy.set(expectedPolicy, [key]);
250
+ function collectExpectedPolicyParents(e) {
251
+ let t = /* @__PURE__ */ new Map();
252
+ for (let [n, r] of e) for (let e of r.expectedPolicySet) {
253
+ let r = t.get(e);
254
+ if (r === void 0) {
255
+ t.set(e, [n]);
269
256
  continue;
270
257
  }
271
- parents.push(key);
258
+ r.push(n);
272
259
  }
273
- return parentsByPolicy;
260
+ return t;
274
261
  }
275
262
  /** Inserts a node at the current depth, or merges parents/expected-policies into an existing one. */
276
- function addOrMergePolicyNode(graph, currentDepth, depth, validPolicy, qualifierSet, parentKeys, expectedPolicySet) {
277
- const key = policyNodeKey(depth, validPolicy);
278
- const existing = currentDepth.get(key);
279
- if (existing !== void 0) {
280
- for (const parentKey of parentKeys) {
281
- existing.parentKeys.add(parentKey);
282
- getPolicyGraphNode(graph, parentKey)?.childKeys.add(key);
283
- }
284
- for (const expectedPolicy of expectedPolicySet) existing.expectedPolicySet.add(expectedPolicy);
285
- if (existing.qualifierSet === void 0 && qualifierSet !== void 0) existing.qualifierSet = qualifierSet;
263
+ function addOrMergePolicyNode(e, t, n, r, i, a, o) {
264
+ let s = policyNodeKey(n, r), c = t.get(s);
265
+ if (c !== void 0) {
266
+ for (let t of a) c.parentKeys.add(t), getPolicyGraphNode(e, t)?.childKeys.add(s);
267
+ for (let e of o) c.expectedPolicySet.add(e);
268
+ c.qualifierSet === void 0 && i !== void 0 && (c.qualifierSet = i);
286
269
  return;
287
270
  }
288
- const node = createPolicyGraphNode(depth, validPolicy, qualifierSet, expectedPolicySet, parentKeys);
289
- currentDepth.set(key, node);
290
- for (const parentKey of parentKeys) getPolicyGraphNode(graph, parentKey)?.childKeys.add(key);
271
+ let l = createPolicyGraphNode(n, r, i, o, a);
272
+ t.set(s, l);
273
+ for (let t of a) getPolicyGraphNode(e, t)?.childKeys.add(s);
291
274
  }
292
275
  /** Looks up a node by its `"depth:oid"` key, parsing the depth prefix. */
293
- function getPolicyGraphNode(graph, key) {
294
- const separator = key.indexOf(":");
295
- if (separator <= 0) return;
296
- const depthString = key.slice(0, separator);
297
- const depth = Number.parseInt(depthString, 10);
298
- if (Number.isNaN(depth)) return;
299
- return graph.nodesByDepth[depth]?.get(key);
276
+ function getPolicyGraphNode(e, t) {
277
+ let n = t.indexOf(":");
278
+ if (n <= 0) return;
279
+ let r = t.slice(0, n), i = Number.parseInt(r, 10);
280
+ if (!Number.isNaN(i)) return e.nodesByDepth[i]?.get(t);
300
281
  }
301
282
  /** Removes childless leaf nodes bottom-up, cascading deletions through parent links. */
302
- function prunePolicyGraph(graph, maxDepth) {
303
- for (let depth = maxDepth; depth >= 0; depth -= 1) {
304
- const nodes = graph.nodesByDepth[depth];
305
- if (nodes === void 0) continue;
306
- for (const key of [...nodes.keys()]) {
307
- const node = nodes.get(key);
308
- if (node === void 0 || node.childKeys.size > 0) continue;
309
- deletePolicyGraphNode(graph, key);
283
+ function prunePolicyGraph(e, t) {
284
+ for (let n = t; n >= 0; --n) {
285
+ let t = e.nodesByDepth[n];
286
+ if (t !== void 0) for (let n of [...t.keys()]) {
287
+ let r = t.get(n);
288
+ r === void 0 || r.childKeys.size > 0 || deletePolicyGraphNode(e, n);
310
289
  }
311
290
  }
312
291
  }
313
292
  /** Removes a node and recursively prunes its parents if they become childless. */
314
- function deletePolicyGraphNode(graph, key) {
315
- const node = getPolicyGraphNode(graph, key);
316
- if (node === void 0) return;
317
- graph.nodesByDepth[node.depth]?.delete(key);
318
- for (const parentKey of node.parentKeys) {
319
- const parent = getPolicyGraphNode(graph, parentKey);
320
- if (parent === void 0) continue;
321
- parent.childKeys.delete(key);
322
- if (parent.childKeys.size === 0) deletePolicyGraphNode(graph, parentKey);
293
+ function deletePolicyGraphNode(e, t) {
294
+ let n = getPolicyGraphNode(e, t);
295
+ if (n !== void 0) {
296
+ e.nodesByDepth[n.depth]?.delete(t);
297
+ for (let r of n.parentKeys) {
298
+ let n = getPolicyGraphNode(e, r);
299
+ n !== void 0 && (n.childKeys.delete(t), n.childKeys.size === 0 && deletePolicyGraphNode(e, r));
300
+ }
323
301
  }
324
302
  }
325
303
  /** Applies issuer→subject policy mappings at the current depth, or deletes mapped nodes when disallowed. */
326
- function applyPolicyMappingsStep(graph, depth, mappings, mappingAllowed) {
327
- const currentDepth = graph.nodesByDepth[depth];
328
- if (currentDepth === void 0) return;
329
- const groupedMappings = /* @__PURE__ */ new Map();
330
- for (const mapping of mappings) {
331
- if (mapping.issuerDomainPolicy === OIDS.anyPolicy || mapping.subjectDomainPolicy === OIDS.anyPolicy) continue;
332
- const subjectPolicies = groupedMappings.get(mapping.issuerDomainPolicy);
333
- if (subjectPolicies === void 0) {
334
- groupedMappings.set(mapping.issuerDomainPolicy, [mapping.subjectDomainPolicy]);
304
+ function applyPolicyMappingsStep(t, n, r, i) {
305
+ let a = t.nodesByDepth[n];
306
+ if (a === void 0) return;
307
+ let o = /* @__PURE__ */ new Map();
308
+ for (let t of r) {
309
+ if (t.issuerDomainPolicy === e.anyPolicy || t.subjectDomainPolicy === e.anyPolicy) continue;
310
+ let n = o.get(t.issuerDomainPolicy);
311
+ if (n === void 0) {
312
+ o.set(t.issuerDomainPolicy, [t.subjectDomainPolicy]);
335
313
  continue;
336
314
  }
337
- subjectPolicies.push(mapping.subjectDomainPolicy);
315
+ n.push(t.subjectDomainPolicy);
338
316
  }
339
- const anyPolicyNode = currentDepth.get(policyNodeKey(depth, OIDS.anyPolicy));
340
- for (const [issuerDomainPolicy, subjectDomainPolicies] of groupedMappings) {
341
- const nodeKey = policyNodeKey(depth, issuerDomainPolicy);
342
- const node = currentDepth.get(nodeKey);
343
- if (mappingAllowed) {
344
- if (node !== void 0) {
345
- node.expectedPolicySet = new Set(subjectDomainPolicies);
317
+ let s = a.get(policyNodeKey(n, e.anyPolicy));
318
+ for (let [e, r] of o) {
319
+ let o = policyNodeKey(n, e), c = a.get(o);
320
+ if (i) {
321
+ if (c !== void 0) {
322
+ c.expectedPolicySet = new Set(r);
346
323
  continue;
347
324
  }
348
- if (anyPolicyNode !== void 0) addOrMergePolicyNode(graph, currentDepth, depth, issuerDomainPolicy, anyPolicyNode.qualifierSet, [...anyPolicyNode.parentKeys], subjectDomainPolicies);
325
+ s !== void 0 && addOrMergePolicyNode(t, a, n, e, s.qualifierSet, [...s.parentKeys], r);
349
326
  continue;
350
327
  }
351
- if (node !== void 0) deletePolicyGraphNode(graph, nodeKey);
328
+ c !== void 0 && deletePolicyGraphNode(t, o);
352
329
  }
353
- prunePolicyGraph(graph, depth - 1);
330
+ prunePolicyGraph(t, n - 1);
354
331
  }
355
332
  /** Decrements explicitPolicy / inhibitPolicyMapping / inhibitAnyPolicy and applies policyConstraints overrides. */
356
- function updatePolicyCounters(state, certificate, isLeaf) {
357
- if (isLeaf) {
358
- if (state.explicitPolicy > 0) state.explicitPolicy -= 1;
359
- if (isNonNegativeInteger(certificate.policyConstraints?.requireExplicitPolicy)) {
360
- if (certificate.policyConstraints.requireExplicitPolicy === 0) state.explicitPolicy = 0;
361
- }
333
+ function updatePolicyCounters(e, t, n) {
334
+ if (n) {
335
+ updateLeafPolicyCounter(e, t);
362
336
  return;
363
337
  }
364
- if (!isSelfIssued(certificate)) {
365
- if (state.explicitPolicy > 0) state.explicitPolicy -= 1;
366
- if (state.inhibitPolicyMapping > 0) state.inhibitPolicyMapping -= 1;
367
- if (state.inhibitAnyPolicy > 0) state.inhibitAnyPolicy -= 1;
368
- }
369
- const policyConstraints = certificate.policyConstraints;
370
- if (isNonNegativeInteger(policyConstraints?.requireExplicitPolicy) && policyConstraints.requireExplicitPolicy < state.explicitPolicy) state.explicitPolicy = policyConstraints.requireExplicitPolicy;
371
- if (isNonNegativeInteger(policyConstraints?.inhibitPolicyMapping) && policyConstraints.inhibitPolicyMapping < state.inhibitPolicyMapping) state.inhibitPolicyMapping = policyConstraints.inhibitPolicyMapping;
372
- if (isNonNegativeInteger(certificate.inhibitAnyPolicy?.skipCerts) && certificate.inhibitAnyPolicy.skipCerts < state.inhibitAnyPolicy) state.inhibitAnyPolicy = certificate.inhibitAnyPolicy.skipCerts;
338
+ isSelfIssued(t) || decrementPolicyCounters(e), applyPolicyCounterOverrides(e, t);
339
+ }
340
+ function updateLeafPolicyCounter(e, t) {
341
+ decrementPositiveCounter(e, "explicitPolicy"), t.policyConstraints?.requireExplicitPolicy === 0 && (e.explicitPolicy = 0);
342
+ }
343
+ function decrementPolicyCounters(e) {
344
+ decrementPositiveCounter(e, "explicitPolicy"), decrementPositiveCounter(e, "inhibitPolicyMapping"), decrementPositiveCounter(e, "inhibitAnyPolicy");
345
+ }
346
+ function decrementPositiveCounter(e, t) {
347
+ e[t] > 0 && --e[t];
348
+ }
349
+ function applyPolicyCounterOverrides(e, t) {
350
+ let n = t.policyConstraints;
351
+ isNonNegativeInteger(n?.requireExplicitPolicy) && n.requireExplicitPolicy < e.explicitPolicy && (e.explicitPolicy = n.requireExplicitPolicy), isNonNegativeInteger(n?.inhibitPolicyMapping) && n.inhibitPolicyMapping < e.inhibitPolicyMapping && (e.inhibitPolicyMapping = n.inhibitPolicyMapping), isNonNegativeInteger(t.inhibitAnyPolicy?.skipCerts) && t.inhibitAnyPolicy.skipCerts < e.inhibitAnyPolicy && (e.inhibitAnyPolicy = t.inhibitAnyPolicy.skipCerts);
373
352
  }
374
- function isNonNegativeInteger(value) {
375
- return value !== void 0 && Number.isInteger(value) && value >= 0;
353
+ function isNonNegativeInteger(e) {
354
+ return e !== void 0 && Number.isInteger(e) && e >= 0;
376
355
  }
377
356
  /** A certificate is self-issued when subject and issuer DNs are semantically equal (RFC 5280 §7.1). */
378
- function isSelfIssued(certificate) {
379
- return compareDistinguishedNames(certificate.subject, certificate.issuer);
357
+ function isSelfIssued(e) {
358
+ return t(e.subject, e.issuer);
380
359
  }
381
360
  //#endregion
382
361
  export { createPolicyValidationState, evaluatePolicyChain };
362
+
363
+ //# sourceMappingURL=policy-engine.js.map