micro509 0.7.2 → 0.8.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 (109) hide show
  1. package/dist/index.d.ts +5 -5
  2. package/dist/index.js +15 -1
  3. package/dist/internal/asn1/asn1.js +280 -2
  4. package/dist/internal/asn1/der.js +378 -2
  5. package/dist/internal/asn1/oids.js +103 -2
  6. package/dist/internal/crypto/algorithm-names.js +58 -2
  7. package/dist/internal/crypto/ecdsa.js +68 -2
  8. package/dist/internal/crypto/hash.js +286 -2
  9. package/dist/internal/crypto/pbes2.js +213 -2
  10. package/dist/internal/crypto/rsa-pss.js +184 -2
  11. package/dist/internal/crypto/sig-verify.js +192 -2
  12. package/dist/internal/crypto/signing.d.ts +1 -2
  13. package/dist/internal/crypto/signing.js +123 -2
  14. package/dist/internal/crypto/webcrypto.js +14 -2
  15. package/dist/internal/shared/base64.js +16 -2
  16. package/dist/internal/shared/dn.js +84 -2
  17. package/dist/internal/shared/ip.js +164 -2
  18. package/dist/internal/verify/name-constraints-engine.js +450 -2
  19. package/dist/internal/verify/policy-engine.js +382 -2
  20. package/dist/internal/verify/verify-path.js +298 -2
  21. package/dist/internal/x509/extension-bits.js +132 -2
  22. package/dist/internal/x509/extension-registry.js +229 -2
  23. package/dist/internal/x509/name-fields.js +93 -2
  24. package/dist/keys/keys.d.ts +231 -7
  25. package/dist/keys/keys.js +1087 -5
  26. package/dist/keys.d.ts +2 -2
  27. package/dist/keys.js +2 -1
  28. package/dist/pem/pem.d.ts +2 -2
  29. package/dist/pem/pem.js +172 -6
  30. package/dist/pem.js +2 -1
  31. package/dist/pkcs/pfx.d.ts +4 -5
  32. package/dist/pkcs/pfx.js +395 -2
  33. package/dist/pkcs/pkcs12-mac.d.ts +2 -2
  34. package/dist/pkcs/pkcs12-mac.js +175 -2
  35. package/dist/pkcs/pkcs7.d.ts +1 -2
  36. package/dist/pkcs/pkcs7.js +653 -2
  37. package/dist/pkcs.d.ts +2 -2
  38. package/dist/pkcs.js +4 -1
  39. package/dist/result/result.d.ts +1 -2
  40. package/dist/result/result.js +111 -2
  41. package/dist/result.js +2 -1
  42. package/dist/revocation/chain.d.ts +4 -5
  43. package/dist/revocation/chain.js +536 -2
  44. package/dist/revocation/crl.d.ts +2 -2
  45. package/dist/revocation/crl.js +1119 -2
  46. package/dist/revocation/ocsp.d.ts +3 -2
  47. package/dist/revocation/ocsp.js +958 -2
  48. package/dist/revocation/revocation.d.ts +1 -2
  49. package/dist/revocation/revocation.js +261 -2
  50. package/dist/revocation.d.ts +2 -2
  51. package/dist/revocation.js +5 -1
  52. package/dist/verify/identity.d.ts +1 -2
  53. package/dist/verify/identity.js +270 -2
  54. package/dist/verify/name-constraints.d.ts +1 -2
  55. package/dist/verify/policy.d.ts +1 -2
  56. package/dist/verify/verify.d.ts +1 -2
  57. package/dist/verify/verify.js +764 -2
  58. package/dist/verify.js +3 -1
  59. package/dist/x509/certificate.d.ts +1 -2
  60. package/dist/x509/certificate.js +151 -2
  61. package/dist/x509/csr.d.ts +1 -2
  62. package/dist/x509/csr.js +71 -2
  63. package/dist/x509/extensions.d.ts +1 -2
  64. package/dist/x509/extensions.js +387 -2
  65. package/dist/x509/name.d.ts +1 -2
  66. package/dist/x509/name.js +173 -2
  67. package/dist/x509/parse.d.ts +84 -2
  68. package/dist/x509/parse.js +1090 -2
  69. package/dist/x509.d.ts +2 -2
  70. package/dist/x509.js +6 -1
  71. package/package.json +74 -34
  72. package/dist/internal/asn1/asn1.js.map +0 -1
  73. package/dist/internal/asn1/der.js.map +0 -1
  74. package/dist/internal/asn1/oids.js.map +0 -1
  75. package/dist/internal/crypto/algorithm-names.js.map +0 -1
  76. package/dist/internal/crypto/ecdsa.js.map +0 -1
  77. package/dist/internal/crypto/hash.js.map +0 -1
  78. package/dist/internal/crypto/pbes2.d.ts +0 -23
  79. package/dist/internal/crypto/pbes2.js.map +0 -1
  80. package/dist/internal/crypto/rsa-pss.js.map +0 -1
  81. package/dist/internal/crypto/sig-verify.js.map +0 -1
  82. package/dist/internal/crypto/signing.js.map +0 -1
  83. package/dist/internal/crypto/webcrypto.js.map +0 -1
  84. package/dist/internal/shared/base64.js.map +0 -1
  85. package/dist/internal/shared/dn.js.map +0 -1
  86. package/dist/internal/shared/ip.js.map +0 -1
  87. package/dist/internal/verify/name-constraints-engine.js.map +0 -1
  88. package/dist/internal/verify/policy-engine.js.map +0 -1
  89. package/dist/internal/verify/verify-path.js.map +0 -1
  90. package/dist/internal/x509/extension-bits.js.map +0 -1
  91. package/dist/internal/x509/extension-registry.js.map +0 -1
  92. package/dist/internal/x509/name-fields.js.map +0 -1
  93. package/dist/keys/keys.js.map +0 -1
  94. package/dist/pem/pem.js.map +0 -1
  95. package/dist/pkcs/pfx.js.map +0 -1
  96. package/dist/pkcs/pkcs12-mac.js.map +0 -1
  97. package/dist/pkcs/pkcs7.js.map +0 -1
  98. package/dist/result/result.js.map +0 -1
  99. package/dist/revocation/chain.js.map +0 -1
  100. package/dist/revocation/crl.js.map +0 -1
  101. package/dist/revocation/ocsp.js.map +0 -1
  102. package/dist/revocation/revocation.js.map +0 -1
  103. package/dist/verify/identity.js.map +0 -1
  104. package/dist/verify/verify.js.map +0 -1
  105. package/dist/x509/certificate.js.map +0 -1
  106. package/dist/x509/csr.js.map +0 -1
  107. package/dist/x509/extensions.js.map +0 -1
  108. package/dist/x509/name.js.map +0 -1
  109. package/dist/x509/parse.js.map +0 -1
@@ -1,2 +1,382 @@
1
- import{OIDS as e}from"../asn1/oids.js";import{compareDistinguishedNames as t}from"../shared/dn.js";function n(e,t){let n=t+1;return{initialPolicySet:e.initialPolicySet??`any`,explicitPolicy:e.requireExplicitPolicy===!0?0:n,inhibitPolicyMapping:e.inhibitPolicyMapping===!0?0:n,inhibitAnyPolicy:e.inhibitAnyPolicy===!0?0:n,validPolicyGraph:i()}}function r(e,t){if(e.length===0)throw RangeError(`policy validation requires at least one certificate`);s(e,t);let n=c(e,t);return t.explicitPolicy===0&&n.userConstrainedPolicies.length===0?{ok:!1,error:{code:`explicit_policy_required`,message:`policy validation requires an explicit permitted policy`,details:{expected:t.initialPolicySet===`any`?`explicit policy`:t.initialPolicySet.join(`,`),actual:g(n.userConstrainedPolicies)}}}:t.initialPolicySet!==`any`&&n.userConstrainedPolicies.length===0?{ok:!1,error:{code:`initial_policy_set_not_satisfied`,message:`certificate chain does not satisfy the requested initial policy set`,details:{expected:t.initialPolicySet.join(`,`),actual:g(n.userConstrainedPolicies)}}}:{ok:!0,value:n}}function i(){let t=a(0,e.anyPolicy,void 0,[e.anyPolicy],[]);return{nodesByDepth:[new Map([[o(0,e.anyPolicy),t]])]}}function a(e,t,n,r,i){return{depth:e,validPolicy:t,...n===void 0?{}:{qualifierSet:n},expectedPolicySet:new Set(r),parentKeys:new Set(i),childKeys:new Set}}function o(e,t){return`${String(e)}:${t}`}function s(e,t){if(e.length===1){let n=e[0];if(n===void 0)throw Error(`missing certificate at chain index 0 (chain length ${String(e.length)})`);_(t,n,1,!0);return}let n=e.length-1;for(let r=e.length-2;r>=0;--r){let i=e[r];if(i===void 0)throw Error(`missing certificate at chain index ${String(r)} (chain length ${String(e.length)})`);let a=n-r;_(t,i,a,a===n)}}function c(e,t){let n=l(e,t.validPolicyGraph),r=d(e,t.validPolicyGraph);return{authorityConstrainedPolicies:[...n.values()].sort(h),userConstrainedPolicies:p(n,r,t.initialPolicySet)}}function l(t,n){if(n===null)return new Map;let r=Math.max(1,t.length-1),i=n.nodesByDepth[r];if(i===void 0)return new Map;let a=new Map;for(let[t,r]of i){if(r.validPolicy===e.anyPolicy){a.set(e.anyPolicy,m(e.anyPolicy,r.qualifierSet));continue}u(n,t)&&a.set(r.validPolicy,m(r.validPolicy,r.qualifierSet))}return a}function u(t,n){let r=[n],i=new Set;for(;r.length>0;){let n=r.pop();if(n===void 0||i.has(n))continue;i.add(n);let a=C(t,n);if(a!==void 0)for(let n of a.parentKeys){let i=C(t,n);if(i!==void 0){if(i.depth===0&&i.validPolicy===e.anyPolicy)return!0;r.push(n)}}}return!1}function d(t,n){if(n===null)return new Map;let r=Math.max(1,t.length-1),i=n.nodesByDepth[r];if(i===void 0)return new Map;let a=new Map;for(let[t,r]of i){if(r.validPolicy===e.anyPolicy){a.set(e.anyPolicy,m(e.anyPolicy,r.qualifierSet));continue}f(n,t,a)}return a}function f(t,n,r){let i=[n],a=new Set;for(;i.length>0;){let o=i.pop();if(o===void 0||a.has(o))continue;a.add(o);let s=C(t,o);if(s!==void 0)for(let a of s.parentKeys){let c=C(t,a);if(c!==void 0){if(c.depth===0&&c.validPolicy===e.anyPolicy){r.set(s.validPolicy,m(s.validPolicy,o===n?s.qualifierSet:void 0));continue}i.push(a)}}}}function p(t,n,r){if(r===`any`)return[...t.values()].sort(h);let i=n.get(e.anyPolicy),a=t.has(e.anyPolicy),o=new Map;for(let e of r){let r=n.get(e);if(r!==void 0){o.set(e,r);continue}i!==void 0&&(t.has(e)||a)&&o.set(e,m(e,i.policyQualifiers))}return[...o.values()].sort(h)}function m(e,t){return{policyIdentifier:e,...t===void 0?{}:{policyQualifiers:t}}}function h(e,t){return e.policyIdentifier.localeCompare(t.policyIdentifier)}function g(e){return e.length===0?`<none>`:e.map(e=>e.policyIdentifier).join(`,`)}function _(e,t,n,r){let i=v(t.certificatePolicies);e.validPolicyGraph!==null&&i===void 0?e.validPolicyGraph=null:e.validPolicyGraph!==null&&i!==void 0&&(y(e.validPolicyGraph,i,n,e.inhibitAnyPolicy>0||!r&&k(t)),t.policyMappings!==void 0&&E(e.validPolicyGraph,n,t.policyMappings,e.inhibitPolicyMapping>0)),D(e,t,r)}function v(e){if(e===void 0)return;let t=new Map;for(let n of e)t.has(n.policyIdentifier)||t.set(n.policyIdentifier,n);return t}function y(t,n,r,i){let a=t.nodesByDepth[r-1]??new Map,s=new Map;t.nodesByDepth[r]=s;let c=n.get(e.anyPolicy),l=a.get(o(r-1,e.anyPolicy));for(let i of n.values()){if(i.policyIdentifier===e.anyPolicy)continue;let n=b(a,i.policyIdentifier);n.length>0&&S(t,s,r,i.policyIdentifier,i.policyQualifiers,n,[i.policyIdentifier])}if(l!==void 0){let i=o(r-1,e.anyPolicy);for(let a of n.values())a.policyIdentifier!==e.anyPolicy&&(s.has(o(r,a.policyIdentifier))||S(t,s,r,a.policyIdentifier,a.policyQualifiers,[i],[a.policyIdentifier]))}if(c!==void 0&&i)for(let[n,i]of x(a)){if(s.has(o(r,n)))continue;let a=n===e.anyPolicy?l===void 0?[]:[o(r-1,e.anyPolicy)]:i;a.length!==0&&S(t,s,r,n,c.policyQualifiers,a,[n])}w(t,r-1)}function b(e,t){let n=[];for(let[r,i]of e)i.expectedPolicySet.has(t)&&n.push(r);return n}function x(e){let t=new Map;for(let[n,r]of e)for(let e of r.expectedPolicySet){let r=t.get(e);if(r===void 0){t.set(e,[n]);continue}r.push(n)}return t}function S(e,t,n,r,i,s,c){let l=o(n,r),u=t.get(l);if(u!==void 0){for(let t of s)u.parentKeys.add(t),C(e,t)?.childKeys.add(l);for(let e of c)u.expectedPolicySet.add(e);u.qualifierSet===void 0&&i!==void 0&&(u.qualifierSet=i);return}let d=a(n,r,i,c,s);t.set(l,d);for(let t of s)C(e,t)?.childKeys.add(l)}function C(e,t){let n=t.indexOf(`:`);if(n<=0)return;let r=t.slice(0,n),i=Number.parseInt(r,10);if(!Number.isNaN(i))return e.nodesByDepth[i]?.get(t)}function w(e,t){for(let n=t;n>=0;--n){let t=e.nodesByDepth[n];if(t!==void 0)for(let n of[...t.keys()]){let r=t.get(n);r===void 0||r.childKeys.size>0||T(e,n)}}}function T(e,t){let n=C(e,t);if(n!==void 0){e.nodesByDepth[n.depth]?.delete(t);for(let r of n.parentKeys){let n=C(e,r);n!==void 0&&(n.childKeys.delete(t),n.childKeys.size===0&&T(e,r))}}}function E(t,n,r,i){let a=t.nodesByDepth[n];if(a===void 0)return;let s=new Map;for(let t of r){if(t.issuerDomainPolicy===e.anyPolicy||t.subjectDomainPolicy===e.anyPolicy)continue;let n=s.get(t.issuerDomainPolicy);if(n===void 0){s.set(t.issuerDomainPolicy,[t.subjectDomainPolicy]);continue}n.push(t.subjectDomainPolicy)}let c=a.get(o(n,e.anyPolicy));for(let[e,r]of s){let s=o(n,e),l=a.get(s);if(i){if(l!==void 0){l.expectedPolicySet=new Set(r);continue}c!==void 0&&S(t,a,n,e,c.qualifierSet,[...c.parentKeys],r);continue}l!==void 0&&T(t,s)}w(t,n-1)}function D(e,t,n){if(n){e.explicitPolicy>0&&--e.explicitPolicy,O(t.policyConstraints?.requireExplicitPolicy)&&t.policyConstraints.requireExplicitPolicy===0&&(e.explicitPolicy=0);return}k(t)||(e.explicitPolicy>0&&--e.explicitPolicy,e.inhibitPolicyMapping>0&&--e.inhibitPolicyMapping,e.inhibitAnyPolicy>0&&--e.inhibitAnyPolicy);let r=t.policyConstraints;O(r?.requireExplicitPolicy)&&r.requireExplicitPolicy<e.explicitPolicy&&(e.explicitPolicy=r.requireExplicitPolicy),O(r?.inhibitPolicyMapping)&&r.inhibitPolicyMapping<e.inhibitPolicyMapping&&(e.inhibitPolicyMapping=r.inhibitPolicyMapping),O(t.inhibitAnyPolicy?.skipCerts)&&t.inhibitAnyPolicy.skipCerts<e.inhibitAnyPolicy&&(e.inhibitAnyPolicy=t.inhibitAnyPolicy.skipCerts)}function O(e){return e!==void 0&&Number.isInteger(e)&&e>=0}function k(e){return t(e.subject,e.issuer)}export{n as createPolicyValidationState,r as evaluatePolicyChain};
2
- //# sourceMappingURL=policy-engine.js.map
1
+ import { OIDS } from "../asn1/oids.js";
2
+ import { compareDistinguishedNames } from "../shared/dn.js";
3
+ //#region src/internal/verify/policy-engine.ts
4
+ /**
5
+ * Internal RFC 9618 policy-validation engine.
6
+ *
7
+ * Tracks the policy graph and counter state through each certificate in a
8
+ * chain, then derives the {@linkcode PolicyValidationOutcome}.
9
+ *
10
+ * @module
11
+ */
12
+ /**
13
+ * Initializes mutable policy state from caller options.
14
+ *
15
+ * Counter values default to `chainLength + 1` (effectively disabled) unless
16
+ * the corresponding input flag is `true`.
17
+ */
18
+ function createPolicyValidationState(input, chainLength) {
19
+ const disabledCounter = chainLength + 1;
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,
25
+ validPolicyGraph: createInitialPolicyGraph()
26
+ };
27
+ }
28
+ /**
29
+ * Walks the chain root-to-leaf, updating policy state per certificate,
30
+ * then checks whether the resulting policy set satisfies caller requirements.
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,
38
+ error: {
39
+ code: "explicit_policy_required",
40
+ message: "policy validation requires an explicit permitted policy",
41
+ details: {
42
+ expected: state.initialPolicySet === "any" ? "explicit policy" : state.initialPolicySet.join(","),
43
+ actual: describeFinalPolicies(outcome.userConstrainedPolicies)
44
+ }
45
+ }
46
+ };
47
+ if (state.initialPolicySet !== "any" && outcome.userConstrainedPolicies.length === 0) return {
48
+ ok: false,
49
+ error: {
50
+ code: "initial_policy_set_not_satisfied",
51
+ message: "certificate chain does not satisfy the requested initial policy set",
52
+ details: {
53
+ expected: state.initialPolicySet.join(","),
54
+ actual: describeFinalPolicies(outcome.userConstrainedPolicies)
55
+ }
56
+ }
57
+ };
58
+ return {
59
+ ok: true,
60
+ value: outcome
61
+ };
62
+ }
63
+ /** Builds the depth-0 graph with a single anyPolicy root node. */
64
+ function createInitialPolicyGraph() {
65
+ const rootNode = createPolicyGraphNode(0, OIDS.anyPolicy, void 0, [OIDS.anyPolicy], []);
66
+ return { nodesByDepth: [/* @__PURE__ */ new Map([[policyNodeKey(0, OIDS.anyPolicy), rootNode]])] };
67
+ }
68
+ /** Constructs a new {@linkcode PolicyGraphNode} with empty child-key set. */
69
+ function createPolicyGraphNode(depth, validPolicy, qualifierSet, expectedPolicySet, parentKeys) {
70
+ return {
71
+ depth,
72
+ validPolicy,
73
+ ...qualifierSet === void 0 ? {} : { qualifierSet },
74
+ expectedPolicySet: new Set(expectedPolicySet),
75
+ parentKeys: new Set(parentKeys),
76
+ childKeys: /* @__PURE__ */ new Set()
77
+ };
78
+ }
79
+ /** Canonical map key for a graph node: `"depth:policyOID"`. */
80
+ function policyNodeKey(depth, validPolicy) {
81
+ return `${String(depth)}:${validPolicy}`;
82
+ }
83
+ /** 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);
89
+ return;
90
+ }
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);
97
+ }
98
+ }
99
+ /** 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);
103
+ return {
104
+ authorityConstrainedPolicies: [...authorityConstrainedPolicies.values()].sort(comparePolicies),
105
+ userConstrainedPolicies: deriveUserConstrainedPolicies(authorityConstrainedPolicies, rootDomainPolicies, state.initialPolicySet)
106
+ };
107
+ }
108
+ /** 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));
118
+ continue;
119
+ }
120
+ if (reachesAuthorityRoot(graph, key)) authorityPolicies.set(node.validPolicy, buildConstrainedPolicy(node.validPolicy, node.qualifierSet));
121
+ }
122
+ return authorityPolicies;
123
+ }
124
+ /** 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);
139
+ }
140
+ }
141
+ return false;
142
+ }
143
+ /** 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));
153
+ continue;
154
+ }
155
+ collectAuthorityConstrainedPolicyRoots(graph, key, rootPolicies);
156
+ }
157
+ return rootPolicies;
158
+ }
159
+ /** 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;
175
+ }
176
+ pending.push(parentKey);
177
+ }
178
+ }
179
+ }
180
+ /** 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);
190
+ continue;
191
+ }
192
+ if (anyPolicy !== void 0 && (finalAuthorityConstrainedPolicies.has(policyIdentifier) || eeHasAnyPolicy)) constrained.set(policyIdentifier, buildConstrainedPolicy(policyIdentifier, anyPolicy.policyQualifiers));
193
+ }
194
+ return [...constrained.values()].sort(comparePolicies);
195
+ }
196
+ /** Constructs a {@linkcode ConstrainedPolicy}, omitting qualifiers when absent. */
197
+ function buildConstrainedPolicy(policyIdentifier, policyQualifiers) {
198
+ return {
199
+ policyIdentifier,
200
+ ...policyQualifiers === void 0 ? {} : { policyQualifiers }
201
+ };
202
+ }
203
+ /** Lexicographic comparator for sorting policies by OID string. */
204
+ function comparePolicies(left, right) {
205
+ return left.policyIdentifier.localeCompare(right.policyIdentifier);
206
+ }
207
+ /** 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(",");
210
+ }
211
+ /** 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);
220
+ }
221
+ /** 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;
227
+ }
228
+ /** 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
+ }
247
+ }
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]);
253
+ }
254
+ prunePolicyGraph(graph, depth - 1);
255
+ }
256
+ /** 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;
261
+ }
262
+ /** 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]);
269
+ continue;
270
+ }
271
+ parents.push(key);
272
+ }
273
+ return parentsByPolicy;
274
+ }
275
+ /** 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;
286
+ return;
287
+ }
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);
291
+ }
292
+ /** 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);
300
+ }
301
+ /** 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);
310
+ }
311
+ }
312
+ }
313
+ /** 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);
323
+ }
324
+ }
325
+ /** 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]);
335
+ continue;
336
+ }
337
+ subjectPolicies.push(mapping.subjectDomainPolicy);
338
+ }
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);
346
+ continue;
347
+ }
348
+ if (anyPolicyNode !== void 0) addOrMergePolicyNode(graph, currentDepth, depth, issuerDomainPolicy, anyPolicyNode.qualifierSet, [...anyPolicyNode.parentKeys], subjectDomainPolicies);
349
+ continue;
350
+ }
351
+ if (node !== void 0) deletePolicyGraphNode(graph, nodeKey);
352
+ }
353
+ prunePolicyGraph(graph, depth - 1);
354
+ }
355
+ /** 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
+ }
362
+ return;
363
+ }
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;
373
+ }
374
+ function isNonNegativeInteger(value) {
375
+ return value !== void 0 && Number.isInteger(value) && value >= 0;
376
+ }
377
+ /** 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);
380
+ }
381
+ //#endregion
382
+ export { createPolicyValidationState, evaluatePolicyChain };