Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/CertificationPath/Policy/PolicyTree.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\CertificationPath\Policy;
6
7 use function count;
8 use function in_array;
9 use LogicException;
10 use SpomkyLabs\Pki\X509\Certificate\Certificate;
11 use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy\PolicyInformation;
12 use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\ValidatorState;
13
14 final class PolicyTree
15 {
16 /**
17 * @param PolicyNode $root Initial root node
18 */
19 private function __construct(
20 private ?PolicyNode $root
21 ) {
22 }
23
24 public static function create(?PolicyNode $root): self
25 {
26 return new self($root);
27 }
28
29 /**
30 * Process policy information from the certificate.
31 *
32 * Certificate policies extension must be present.
33 */
34 public function processPolicies(ValidatorState $state, Certificate $cert): ValidatorState
35 {
36 $policies = $cert->tbsCertificate()
37 ->extensions()
38 ->certificatePolicies();
39 $tree = clone $this;
40 // (d.1) for each policy P not equal to anyPolicy
41 foreach ($policies as $policy) {
42 /** @var PolicyInformation $policy */
43 if ($policy->isAnyPolicy()) {
44 $tree->processAnyPolicy($policy, $cert, $state);
45 } else {
46 $tree->processPolicy($policy, $state);
47 }
48 }
49 // if whole tree is pruned
50 if ($tree->pruneTree($state->index() - 1) === 0) {
51 return $state->withoutValidPolicyTree();
52 }
53 return $state->withValidPolicyTree($tree);
54 }
55
56 /**
57 * Process policy mappings from the certificate.
58 */
59 public function processMappings(ValidatorState $state, Certificate $cert): ValidatorState
60 {
61 $tree = clone $this;
62 if ($state->policyMapping() > 0) {
63 $tree->_applyMappings($cert, $state);
64 } elseif ($state->policyMapping() === 0) {
65 $tree->_deleteMappings($cert, $state);
66 }
67 // if whole tree is pruned
68 if ($tree->root === null) {
69 return $state->withoutValidPolicyTree();
70 }
71 return $state->withValidPolicyTree($tree);
72 }
73
74 /**
75 * Calculate policy intersection as specified in Wrap-Up Procedure 6.1.5.g.
76 *
77 * @param array<string> $policies
78 */
79 public function calculateIntersection(ValidatorState $state, array $policies): ValidatorState
80 {
81 $tree = clone $this;
82 $valid_policy_node_set = $tree->validPolicyNodeSet();
83 // 2. If the valid_policy of any node in the valid_policy_node_set
84 // is not in the user-initial-policy-set and is not anyPolicy,
85 // delete this node and all its children.
86 $valid_policy_node_set = array_filter(
87 $valid_policy_node_set,
88 function (PolicyNode $node) use ($policies) {
89 if ($node->isAnyPolicy()) {
90 return true;
91 }
92 if (in_array($node->validPolicy(), $policies, true)) {
93 return true;
94 }
95 $node->remove();
96 return false;
97 }
98 );
99 // array of valid policy OIDs
100 $valid_policy_set = array_map(static fn (PolicyNode $node) => $node->validPolicy(), $valid_policy_node_set);
101 // 3. If the valid_policy_tree includes a node of depth n with
102 // the valid_policy anyPolicy and the user-initial-policy-set
103 // is not any-policy
104 foreach ($tree->nodesAtDepth($state->index()) as $node) {
105 if ($node->hasParent() && $node->isAnyPolicy()) {
106 // a. Set P-Q to the qualifier_set in the node of depth n
107 // with valid_policy anyPolicy.
108 $pq = $node->qualifiers();
109 // b. For each P-OID in the user-initial-policy-set that is not
110 // the valid_policy of a node in the valid_policy_node_set,
111 // create a child node whose parent is the node of depth n-1
112 // with the valid_policy anyPolicy.
113 $poids = array_diff($policies, $valid_policy_set);
114 foreach ($tree->nodesAtDepth($state->index() - 1) as $parent) {
115 if ($parent->isAnyPolicy()) {
116 // Set the values in the child node as follows:
117 // set the valid_policy to P-OID, set the qualifier_set
118 // to P-Q, and set the expected_policy_set to {P-OID}.
119 foreach ($poids as $poid) {
120 $parent->addChild(PolicyNode::create($poid, $pq, [$poid]));
121 }
122 break;
123 }
124 }
125 // c. Delete the node of depth n with the
126 // valid_policy anyPolicy.
127 $node->remove();
128 }
129 }
130 // 4. If there is a node in the valid_policy_tree of depth n-1 or less
131 // without any child nodes, delete that node. Repeat this step until
132 // there are no nodes of depth n-1 or less without children.
133 if ($tree->pruneTree($state->index() - 1) === 0) {
134 return $state->withoutValidPolicyTree();
135 }
136 return $state->withValidPolicyTree($tree);
137 }
138
139 /**
140 * Get policies at given policy tree depth.
141 *
142 * @param int $i Depth in range 1..n
143 *
144 * @return PolicyInformation[]
145 */
146 public function policiesAtDepth(int $i): array
147 {
148 $policies = [];
149 foreach ($this->nodesAtDepth($i) as $node) {
150 $policies[] = PolicyInformation::create($node->validPolicy(), ...$node->qualifiers());
151 }
152 return $policies;
153 }
154
155 /**
156 * Process single policy information.
157 */
158 private function processPolicy(PolicyInformation $policy, ValidatorState $state): void
159 {
160 $p_oid = $policy->oid();
161 $i = $state->index();
162 $match_count = 0;
163 // (d.1.i) for each node of depth i-1 in the valid_policy_tree...
164 foreach ($this->nodesAtDepth($i - 1) as $node) {
165 // ...where P-OID is in the expected_policy_set
166 if ($node->hasExpectedPolicy($p_oid)) {
167 $node->addChild(PolicyNode::create($p_oid, $policy->qualifiers(), [$p_oid]));
168 ++$match_count;
169 }
170 }
171 // (d.1.ii) if there was no match in step (i)...
172 if ($match_count === 0) {
173 // ...and the valid_policy_tree includes a node of depth i-1 with
174 // the valid_policy anyPolicy
175 foreach ($this->nodesAtDepth($i - 1) as $node) {
176 if ($node->isAnyPolicy()) {
177 $node->addChild(PolicyNode::create($p_oid, $policy->qualifiers(), [$p_oid]));
178 }
179 }
180 }
181 }
182
183 /**
184 * Process anyPolicy policy information.
185 */
186 private function processAnyPolicy(PolicyInformation $policy, Certificate $cert, ValidatorState $state): void
187 {
188 $i = $state->index();
189 // if (a) inhibit_anyPolicy is greater than 0 or
190 // (b) i<n and the certificate is self-issued
191 if (! ($state->inhibitAnyPolicy() > 0 ||
192 ($i < $state->pathLength() && $cert->isSelfIssued()))) {
193 return;
194 }
195 // for each node in the valid_policy_tree of depth i-1
196 foreach ($this->nodesAtDepth($i - 1) as $node) {
197 // for each value in the expected_policy_set
198 foreach ($node->expectedPolicies() as $p_oid) {
199 // that does not appear in a child node
200 if (! $node->hasChildWithValidPolicy($p_oid)) {
201 $node->addChild(PolicyNode::create($p_oid, $policy->qualifiers(), [$p_oid]));
202 }
203 }
204 }
205 }
206
207 /**
208 * Apply policy mappings to the policy tree.
209 */
210 private function _applyMappings(Certificate $cert, ValidatorState $state): void
211 {
212 $policy_mappings = $cert->tbsCertificate()
213 ->extensions()
214 ->policyMappings();
215 // (6.1.4. b.1.) for each node in the valid_policy_tree of depth i...
216 foreach ($policy_mappings->flattenedMappings() as $idp => $sdps) {
217 $match_count = 0;
218 foreach ($this->nodesAtDepth($state->index()) as $node) {
219 // ...where ID-P is the valid_policy
220 if ($node->validPolicy() === $idp) {
221 // set expected_policy_set to the set of subjectDomainPolicy
222 // values that are specified as equivalent to ID-P by
223 // the policy mappings extension
224 $node->setExpectedPolicies(...$sdps);
225 ++$match_count;
226 }
227 }
228 // if no node of depth i in the valid_policy_tree has
229 // a valid_policy of ID-P...
230 if ($match_count === 0) {
231 $this->_applyAnyPolicyMapping($cert, $state, $idp, $sdps);
232 }
233 }
234 }
235
236 /**
237 * Apply anyPolicy mapping to the policy tree as specified in 6.1.4 (b)(1).
238 *
239 * @param string $idp OID of the issuer domain policy
240 * @param array<string> $sdps Array of subject domain policy OIDs
241 */
242 private function _applyAnyPolicyMapping(
243 Certificate $cert,
244 ValidatorState $state,
245 string $idp,
246 array $sdps
247 ): void {
248 // (6.1.4. b.1.) ...but there is a node of depth i with
249 // a valid_policy of anyPolicy
250 foreach ($this->nodesAtDepth($state->index()) as $node) {
251 if ($node->isAnyPolicy()) {
252 // then generate a child node of the node of depth i-1
253 // that has a valid_policy of anyPolicy as follows...
254 foreach ($this->nodesAtDepth($state->index() - 1) as $subnode) {
255 if ($subnode->isAnyPolicy()) {
256 // try to fetch qualifiers of anyPolicy certificate policy
257 try {
258 $qualifiers = $cert->tbsCertificate()
259 ->extensions()
260 ->certificatePolicies()
261 ->anyPolicy()
262 ->qualifiers();
263 } catch (LogicException) {
264 // if there's no policies or no qualifiers
265 $qualifiers = [];
266 }
267 $subnode->addChild(PolicyNode::create($idp, $qualifiers, $sdps));
268 // bail after first anyPolicy has been processed
269 break;
270 }
271 }
272 // bail after first anyPolicy has been processed
273 break;
274 }
275 }
276 }
277
278 /**
279 * Delete nodes as specified in 6.1.4 (b)(2).
280 */
281 private function _deleteMappings(Certificate $cert, ValidatorState $state): void
282 {
283 $idps = $cert->tbsCertificate()
284 ->extensions()
285 ->policyMappings()
286 ->issuerDomainPolicies();
287 // delete each node of depth i in the valid_policy_tree
288 // where ID-P is the valid_policy
289 foreach ($this->nodesAtDepth($state->index()) as $node) {
290 if (in_array($node->validPolicy(), $idps, true)) {
291 $node->remove();
292 }
293 }
294 $this->pruneTree($state->index() - 1);
295 }
296
297 /**
298 * Prune tree starting from given depth.
299 *
300 * @return int The number of nodes left in a tree
301 */
302 private function pruneTree(int $depth): int
303 {
304 if ($this->root === null) {
305 return 0;
306 }
307 for ($i = $depth; $i > 0; --$i) {
308 foreach ($this->nodesAtDepth($i) as $node) {
309 if (count($node) === 0) {
310 $node->remove();
311 }
312 }
313 }
314 // if root has no children left
315 if (count($this->root) === 0) {
316 $this->root = null;
317 return 0;
318 }
319 return $this->root->nodeCount();
320 }
321
322 /**
323 * Get all nodes at given depth.
324 *
325 * @return PolicyNode[]
326 */
327 private function nodesAtDepth(int $i): array
328 {
329 if ($this->root === null) {
330 return [];
331 }
332 $depth = 0;
333 $nodes = [$this->root];
334 while ($depth < $i) {
335 $nodes = self::gatherChildren(...$nodes);
336 if (count($nodes) === 0) {
337 break;
338 }
339 ++$depth;
340 }
341 return $nodes;
342 }
343
344 /**
345 * Get the valid policy node set as specified in spec 6.1.5.(g)(iii)1.
346 *
347 * @return PolicyNode[]
348 */
349 private function validPolicyNodeSet(): array
350 {
351 // 1. Determine the set of policy nodes whose parent nodes have
352 // a valid_policy of anyPolicy. This is the valid_policy_node_set.
353 $set = [];
354 if ($this->root === null) {
355 return $set;
356 }
357 // for each node in a tree
358 $this->root->walkNodes(
359 function (PolicyNode $node) use (&$set) {
360 $parents = $node->parents();
361 // node has parents
362 if (count($parents) !== 0) {
363 // check that each ancestor is an anyPolicy node
364 foreach ($parents as $ancestor) {
365 if (! $ancestor->isAnyPolicy()) {
366 return;
367 }
368 }
369 $set[] = $node;
370 }
371 }
372 );
373 return $set;
374 }
375
376 /**
377 * Gather all children of given nodes to a flattened array.
378 *
379 * @return PolicyNode[]
380 */
381 private static function gatherChildren(PolicyNode ...$nodes): array
382 {
383 $children = [];
384 foreach ($nodes as $node) {
385 $children = array_merge($children, $node->children());
386 }
387 return $children;
388 }
389 }