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