Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\CertificationPath\PathValidation;
6
7 use DateTimeImmutable;
8 use LogicException;
9 use Psr\Clock\ClockInterface;
10 use SpomkyLabs\Pki\X509\Certificate\Certificate;
11 use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy\PolicyInformation;
12
13 /**
14 * Configuration for the certification path validation process.
15 *
16 * @see https://tools.ietf.org/html/rfc5280#section-6.1.1
17 */
18 final class PathValidationConfig
19 {
20 /**
21 * List of acceptable policy identifiers.
22 *
23 * @var string[]
24 */
25 private array $policySet;
26
27 /**
28 * Trust anchor certificate.
29 *
30 * If not set, path validation uses the first certificate of the path.
31 */
32 private ?Certificate $trustAnchor = null;
33
34 /**
35 * Whether policy mapping in inhibited.
36 *
37 * Setting this to true disallows policy mapping.
38 */
39 private bool $policyMappingInhibit;
40
41 /**
42 * Whether the path must be valid for at least one policy in the initial policy set.
43 */
44 private bool $explicitPolicy;
45
46 /**
47 * Whether anyPolicy OID processing should be inhibited.
48 *
49 * Setting this to true disallows the usage of anyPolicy.
50 */
51 private bool $anyPolicyInhibit;
52
53 /**
54 * @param DateTimeImmutable $dateTime Reference date and time
55 * @param int $maxLength Maximum certification path length
56 */
57 private function __construct(
58 private DateTimeImmutable $dateTime,
59 private int $maxLength
60 ) {
61 $this->policySet = [PolicyInformation::OID_ANY_POLICY];
62 $this->policyMappingInhibit = false;
63 $this->explicitPolicy = false;
64 $this->anyPolicyInhibit = false;
65 }
66
67 public static function create(DateTimeImmutable $dateTime, int $maxLength): self
68 {
69 return new self($dateTime, $maxLength);
70 }
71
72 /**
73 * Get default configuration.
74 */
75 public static function defaultConfig(?ClockInterface $clock = null): self
76 {
77 $dateTime = $clock !== null ? $clock->now() : new DateTimeImmutable();
78 return self::create($dateTime, 3);
79 }
80
81 /**
82 * Get self with maximum path length.
83 */
84 public function withMaxLength(int $length): self
85 {
86 $obj = clone $this;
87 $obj->maxLength = $length;
88 return $obj;
89 }
90
91 /**
92 * Get self with reference date and time.
93 */
94 public function withDateTime(DateTimeImmutable $dt): self
95 {
96 $obj = clone $this;
97 $obj->dateTime = $dt;
98 return $obj;
99 }
100
101 /**
102 * Get self with trust anchor certificate.
103 */
104 public function withTrustAnchor(Certificate $ca): self
105 {
106 $obj = clone $this;
107 $obj->trustAnchor = $ca;
108 return $obj;
109 }
110
111 /**
112 * Get self with initial-policy-mapping-inhibit set.
113 */
114 public function withPolicyMappingInhibit(bool $flag): self
115 {
116 $obj = clone $this;
117 $obj->policyMappingInhibit = $flag;
118 return $obj;
119 }
120
121 /**
122 * Get self with initial-explicit-policy set.
123 */
124 public function withExplicitPolicy(bool $flag): self
125 {
126 $obj = clone $this;
127 $obj->explicitPolicy = $flag;
128 return $obj;
129 }
130
131 /**
132 * Get self with initial-any-policy-inhibit set.
133 */
134 public function withAnyPolicyInhibit(bool $flag): self
135 {
136 $obj = clone $this;
137 $obj->anyPolicyInhibit = $flag;
138 return $obj;
139 }
140
141 /**
142 * Get self with user-initial-policy-set set to policy OIDs.
143 *
144 * @param string ...$policies List of policy OIDs
145 */
146 public function withPolicySet(string ...$policies): self
147 {
148 $obj = clone $this;
149 $obj->policySet = $policies;
150 return $obj;
151 }
152
153 /**
154 * Get maximum certification path length.
155 */
156 public function maxLength(): int
157 {
158 return $this->maxLength;
159 }
160
161 /**
162 * Get reference date and time.
163 */
164 public function dateTime(): DateTimeImmutable
165 {
166 return $this->dateTime;
167 }
168
169 /**
170 * Get user-initial-policy-set.
171 *
172 * @return string[] Array of OID's
173 */
174 public function policySet(): array
175 {
176 return $this->policySet;
177 }
178
179 /**
180 * Check whether trust anchor certificate is set.
181 */
182 public function hasTrustAnchor(): bool
183 {
184 return isset($this->trustAnchor);
185 }
186
187 /**
188 * Get trust anchor certificate.
189 */
190 public function trustAnchor(): Certificate
191 {
192 if (! $this->hasTrustAnchor()) {
193 throw new LogicException('No trust anchor.');
194 }
195 return $this->trustAnchor;
196 }
197
198 public function policyMappingInhibit(): bool
199 {
200 return $this->policyMappingInhibit;
201 }
202
203 public function explicitPolicy(): bool
204 {
205 return $this->explicitPolicy;
206 }
207
208 public function anyPolicyInhibit(): bool
209 {
210 return $this->anyPolicyInhibit;
211 }
212 }