Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/CertificationPath/PathValidation/ValidatorState.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\CertificationPath\PathValidation;
6
7 use LogicException;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;
10 use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier;
11 use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PublicKeyInfo;
12 use SpomkyLabs\Pki\X501\ASN1\Name;
13 use SpomkyLabs\Pki\X509\Certificate\Certificate;
14 use SpomkyLabs\Pki\X509\CertificationPath\Policy\PolicyNode;
15 use SpomkyLabs\Pki\X509\CertificationPath\Policy\PolicyTree;
16
17 /**
18 * State class for the certification path validation process.
19 *
20 * @see https://tools.ietf.org/html/rfc5280#section-6.1.1
21 * @see https://tools.ietf.org/html/rfc5280#section-6.1.2
22 */
23 final class ValidatorState
24 {
25 /**
26 * Length of the certification path (n).
27 */
28 private ?int $_pathLength = null;
29
30 /**
31 * Current index in the certification path in the range of 1..n (i).
32 */
33 private ?int $_index = null;
34
35 /**
36 * Valid policy tree (valid_policy_tree).
37 *
38 * A tree of certificate policies with their optional qualifiers. Each of the leaves of the tree represents a valid
39 * policy at this stage in the certification path validation. Once the tree is set to NULL, policy processing
40 * ceases.
41 */
42 private ?PolicyTree $_validPolicyTree = null;
43
44 /**
45 * Permitted subtrees (permitted_subtrees).
46 *
47 * A set of root names for each name type defining a set of subtrees within which all subject names in subsequent
48 * certificates in the certification path must fall.
49 */
50 private mixed $_permittedSubtrees = null;
51
52 /**
53 * Excluded subtrees (excluded_subtrees).
54 *
55 * A set of root names for each name type defining a set of subtrees within which no subject name in subsequent
56 * certificates in the certification path may fall.
57 */
58 private mixed $_excludedSubtrees = null;
59
60 /**
61 * Explicit policy (explicit_policy).
62 *
63 * An integer that indicates if a non-NULL valid_policy_tree is required.
64 */
65 private ?int $_explicitPolicy = null;
66
67 /**
68 * Inhibit anyPolicy (inhibit_anyPolicy).
69 *
70 * An integer that indicates whether the anyPolicy policy identifier is considered a match.
71 */
72 private ?int $_inhibitAnyPolicy = null;
73
74 /**
75 * Policy mapping (policy_mapping).
76 *
77 * An integer that indicates if policy mapping is permitted.
78 */
79 private ?int $_policyMapping = null;
80
81 /**
82 * Working public key algorithm (working_public_key_algorithm).
83 *
84 * The digital signature algorithm used to verify the signature of a certificate.
85 */
86 private SignatureAlgorithmIdentifier|AlgorithmIdentifierType|null $_workingPublicKeyAlgorithm = null;
87
88 /**
89 * Working public key (working_public_key).
90 *
91 * The public key used to verify the signature of a certificate.
92 */
93 private ?PublicKeyInfo $_workingPublicKey = null;
94
95 /**
96 * Working public key parameters (working_public_key_parameters).
97 *
98 * Parameters associated with the current public key that may be required to verify a signature.
99 */
100 private ?Element $_workingPublicKeyParameters = null;
101
102 /**
103 * Working issuer name (working_issuer_name).
104 *
105 * The issuer distinguished name expected in the next certificate in the chain.
106 */
107 private ?Name $_workingIssuerName = null;
108
109 /**
110 * Maximum certification path length (max_path_length).
111 */
112 private ?int $_maxPathLength = null;
113
114 private function __construct()
115 {
116 }
117
118 /**
119 * Initialize variables according to RFC 5280 6.1.2.
120 *
121 * @see https://tools.ietf.org/html/rfc5280#section-6.1.2
122 *
123 * @param Certificate $trust_anchor Trust anchor certificate
124 * @param int $n Number of certificates
125 * in the certification path
126 */
127 public static function initialize(PathValidationConfig $config, Certificate $trust_anchor, int $n): self
128 {
129 $state = new self();
130 $state->_pathLength = $n;
131 $state->_index = 1;
132 $state->_validPolicyTree = PolicyTree::create(PolicyNode::anyPolicyNode());
133 $state->_permittedSubtrees = null;
134 $state->_excludedSubtrees = null;
135 $state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1;
136 $state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1;
137 $state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1;
138 $state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm();
139 $tbsCert = $trust_anchor->tbsCertificate();
140 $state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo();
141 $state->_workingPublicKeyParameters = self::getAlgorithmParameters(
142 $state->_workingPublicKey->algorithmIdentifier()
143 );
144 $state->_workingIssuerName = $tbsCert->subject();
145 $state->_maxPathLength = $config->maxLength();
146 return $state;
147 }
148
149 /**
150 * Get self with current certification path index set.
151 */
152 public function withIndex(int $index): self
153 {
154 $state = clone $this;
155 $state->_index = $index;
156 return $state;
157 }
158
159 /**
160 * Get self with valid_policy_tree.
161 */
162 public function withValidPolicyTree(PolicyTree $policy_tree): self
163 {
164 $state = clone $this;
165 $state->_validPolicyTree = $policy_tree;
166 return $state;
167 }
168
169 /**
170 * Get self with valid_policy_tree set to null.
171 */
172 public function withoutValidPolicyTree(): self
173 {
174 $state = clone $this;
175 $state->_validPolicyTree = null;
176 return $state;
177 }
178
179 /**
180 * Get self with explicit_policy.
181 */
182 public function withExplicitPolicy(int $num): self
183 {
184 $state = clone $this;
185 $state->_explicitPolicy = $num;
186 return $state;
187 }
188
189 /**
190 * Get self with inhibit_anyPolicy.
191 */
192 public function withInhibitAnyPolicy(int $num): self
193 {
194 $state = clone $this;
195 $state->_inhibitAnyPolicy = $num;
196 return $state;
197 }
198
199 /**
200 * Get self with policy_mapping.
201 */
202 public function withPolicyMapping(int $num): self
203 {
204 $state = clone $this;
205 $state->_policyMapping = $num;
206 return $state;
207 }
208
209 /**
210 * Get self with working_public_key_algorithm.
211 */
212 public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo): self
213 {
214 $state = clone $this;
215 $state->_workingPublicKeyAlgorithm = $algo;
216 return $state;
217 }
218
219 /**
220 * Get self with working_public_key.
221 */
222 public function withWorkingPublicKey(PublicKeyInfo $pubkey_info): self
223 {
224 $state = clone $this;
225 $state->_workingPublicKey = $pubkey_info;
226 return $state;
227 }
228
229 /**
230 * Get self with working_public_key_parameters.
231 */
232 public function withWorkingPublicKeyParameters(?Element $params = null): self
233 {
234 $state = clone $this;
235 $state->_workingPublicKeyParameters = $params;
236 return $state;
237 }
238
239 /**
240 * Get self with working_issuer_name.
241 */
242 public function withWorkingIssuerName(Name $issuer): self
243 {
244 $state = clone $this;
245 $state->_workingIssuerName = $issuer;
246 return $state;
247 }
248
249 /**
250 * Get self with max_path_length.
251 */
252 public function withMaxPathLength(int $length): self
253 {
254 $state = clone $this;
255 $state->_maxPathLength = $length;
256 return $state;
257 }
258
259 /**
260 * Get the certification path length (n).
261 */
262 public function pathLength(): int
263 {
264 return $this->_pathLength;
265 }
266
267 /**
268 * Get the current index in certification path in the range of 1..n.
269 */
270 public function index(): int
271 {
272 return $this->_index;
273 }
274
275 /**
276 * Check whether valid_policy_tree is present.
277 */
278 public function hasValidPolicyTree(): bool
279 {
280 return isset($this->_validPolicyTree);
281 }
282
283 public function validPolicyTree(): PolicyTree
284 {
285 if (! $this->hasValidPolicyTree()) {
286 throw new LogicException('valid_policy_tree not set.');
287 }
288 return $this->_validPolicyTree;
289 }
290
291 public function permittedSubtrees(): mixed
292 {
293 return $this->_permittedSubtrees;
294 }
295
296 public function excludedSubtrees(): mixed
297 {
298 return $this->_excludedSubtrees;
299 }
300
301 public function explicitPolicy(): int
302 {
303 return $this->_explicitPolicy;
304 }
305
306 public function inhibitAnyPolicy(): int
307 {
308 return $this->_inhibitAnyPolicy;
309 }
310
311 public function policyMapping(): int
312 {
313 return $this->_policyMapping;
314 }
315
316 public function workingPublicKeyAlgorithm(): AlgorithmIdentifierType
317 {
318 return $this->_workingPublicKeyAlgorithm;
319 }
320
321 public function workingPublicKey(): PublicKeyInfo
322 {
323 return $this->_workingPublicKey;
324 }
325
326 public function workingPublicKeyParameters(): ?Element
327 {
328 return $this->_workingPublicKeyParameters;
329 }
330
331 public function workingIssuerName(): Name
332 {
333 return $this->_workingIssuerName;
334 }
335
336 /**
337 * Get maximum certification path length.
338 */
339 public function maxPathLength(): int
340 {
341 return $this->_maxPathLength;
342 }
343
344 /**
345 * Check whether processing the final certificate of the certification path.
346 */
347 public function isFinal(): bool
348 {
349 return $this->_index === $this->_pathLength;
350 }
351
352 /**
353 * Get the path validation result.
354 *
355 * @param Certificate[] $certificates Certificates in a certification path
356 */
357 public function getResult(array $certificates): PathValidationResult
358 {
359 return PathValidationResult::create(
360 $certificates,
361 $this->_validPolicyTree,
362 $this->_workingPublicKey,
363 $this->_workingPublicKeyAlgorithm,
364 $this->_workingPublicKeyParameters
365 );
366 }
367
368 /**
369 * Get ASN.1 parameters from algorithm identifier.
370 *
371 * @return null|Element ASN.1 element or null if parameters are omitted
372 */
373 public static function getAlgorithmParameters(AlgorithmIdentifierType $algo): ?Element
374 {
375 $seq = $algo->toASN1();
376 return $seq->has(1) ? $seq->at(1)
377 ->asElement() : null;
378 }
379 }