| 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 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function processPolicies(ValidatorState $state, Certificate $cert): ValidatorState |
| 35 |
{ |
| 36 |
$policies = $cert->tbsCertificate() |
| 37 |
->extensions() |
| 38 |
->certificatePolicies(); |
| 39 |
$tree = clone $this; |
| 40 |
|
| 41 |
foreach ($policies as $policy) { |
| 42 |
|
| 43 |
if ($policy->isAnyPolicy()) { |
| 44 |
$tree->processAnyPolicy($policy, $cert, $state); |
| 45 |
} else { |
| 46 |
$tree->processPolicy($policy, $state); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if ($tree->pruneTree($state->index() - 1) === 0) { |
| 51 |
return $state->withoutValidPolicyTree(); |
| 52 |
} |
| 53 |
return $state->withValidPolicyTree($tree); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 68 |
if ($tree->root === null) { |
| 69 |
return $state->withoutValidPolicyTree(); |
| 70 |
} |
| 71 |
return $state->withValidPolicyTree($tree); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function calculateIntersection(ValidatorState $state, array $policies): ValidatorState |
| 80 |
{ |
| 81 |
$tree = clone $this; |
| 82 |
$valid_policy_node_set = $tree->validPolicyNodeSet(); |
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 100 |
$valid_policy_set = array_map(static fn (PolicyNode $node) => $node->validPolicy(), $valid_policy_node_set); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
foreach ($tree->nodesAtDepth($state->index()) as $node) { |
| 105 |
if ($node->hasParent() && $node->isAnyPolicy()) { |
| 106 |
|
| 107 |
|
| 108 |
$pq = $node->qualifiers(); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
$poids = array_diff($policies, $valid_policy_set); |
| 114 |
foreach ($tree->nodesAtDepth($state->index() - 1) as $parent) { |
| 115 |
if ($parent->isAnyPolicy()) { |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
foreach ($poids as $poid) { |
| 120 |
$parent->addChild(PolicyNode::create($poid, $pq, [$poid])); |
| 121 |
} |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
$node->remove(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if ($tree->pruneTree($state->index() - 1) === 0) { |
| 134 |
return $state->withoutValidPolicyTree(); |
| 135 |
} |
| 136 |
return $state->withValidPolicyTree($tree); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 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 |
|
| 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 |
|
| 164 |
foreach ($this->nodesAtDepth($i - 1) as $node) { |
| 165 |
|
| 166 |
if ($node->hasExpectedPolicy($p_oid)) { |
| 167 |
$node->addChild(PolicyNode::create($p_oid, $policy->qualifiers(), [$p_oid])); |
| 168 |
++$match_count; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if ($match_count === 0) { |
| 173 |
|
| 174 |
|
| 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 |
|
| 185 |
|
| 186 |
private function processAnyPolicy(PolicyInformation $policy, Certificate $cert, ValidatorState $state): void |
| 187 |
{ |
| 188 |
$i = $state->index(); |
| 189 |
|
| 190 |
|
| 191 |
if (! ($state->inhibitAnyPolicy() > 0 || |
| 192 |
($i < $state->pathLength() && $cert->isSelfIssued()))) { |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
foreach ($this->nodesAtDepth($i - 1) as $node) { |
| 197 |
|
| 198 |
foreach ($node->expectedPolicies() as $p_oid) { |
| 199 |
|
| 200 |
if (! $node->hasChildWithValidPolicy($p_oid)) { |
| 201 |
$node->addChild(PolicyNode::create($p_oid, $policy->qualifiers(), [$p_oid])); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private function _applyMappings(Certificate $cert, ValidatorState $state): void |
| 211 |
{ |
| 212 |
$policy_mappings = $cert->tbsCertificate() |
| 213 |
->extensions() |
| 214 |
->policyMappings(); |
| 215 |
|
| 216 |
foreach ($policy_mappings->flattenedMappings() as $idp => $sdps) { |
| 217 |
$match_count = 0; |
| 218 |
foreach ($this->nodesAtDepth($state->index()) as $node) { |
| 219 |
|
| 220 |
if ($node->validPolicy() === $idp) { |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
$node->setExpectedPolicies(...$sdps); |
| 225 |
++$match_count; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
if ($match_count === 0) { |
| 231 |
$this->_applyAnyPolicyMapping($cert, $state, $idp, $sdps); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private function _applyAnyPolicyMapping( |
| 243 |
Certificate $cert, |
| 244 |
ValidatorState $state, |
| 245 |
string $idp, |
| 246 |
array $sdps |
| 247 |
): void { |
| 248 |
|
| 249 |
|
| 250 |
foreach ($this->nodesAtDepth($state->index()) as $node) { |
| 251 |
if ($node->isAnyPolicy()) { |
| 252 |
|
| 253 |
|
| 254 |
foreach ($this->nodesAtDepth($state->index() - 1) as $subnode) { |
| 255 |
if ($subnode->isAnyPolicy()) { |
| 256 |
|
| 257 |
try { |
| 258 |
$qualifiers = $cert->tbsCertificate() |
| 259 |
->extensions() |
| 260 |
->certificatePolicies() |
| 261 |
->anyPolicy() |
| 262 |
->qualifiers(); |
| 263 |
} catch (LogicException) { |
| 264 |
|
| 265 |
$qualifiers = []; |
| 266 |
} |
| 267 |
$subnode->addChild(PolicyNode::create($idp, $qualifiers, $sdps)); |
| 268 |
|
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
break; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
private function _deleteMappings(Certificate $cert, ValidatorState $state): void |
| 282 |
{ |
| 283 |
$idps = $cert->tbsCertificate() |
| 284 |
->extensions() |
| 285 |
->policyMappings() |
| 286 |
->issuerDomainPolicies(); |
| 287 |
|
| 288 |
|
| 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 |
|
| 299 |
|
| 300 |
|
| 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 |
|
| 315 |
if (count($this->root) === 0) { |
| 316 |
$this->root = null; |
| 317 |
return 0; |
| 318 |
} |
| 319 |
return $this->root->nodeCount(); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 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 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
private function validPolicyNodeSet(): array |
| 350 |
{ |
| 351 |
|
| 352 |
|
| 353 |
$set = []; |
| 354 |
if ($this->root === null) { |
| 355 |
return $set; |
| 356 |
} |
| 357 |
|
| 358 |
$this->root->walkNodes( |
| 359 |
function (PolicyNode $node) use (&$set) { |
| 360 |
$parents = $node->parents(); |
| 361 |
|
| 362 |
if (count($parents) !== 0) { |
| 363 |
|
| 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 |
|
| 378 |
|
| 379 |
|
| 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 |
} |