|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace X509\CertificationPath\PathValidation; |
|
6
|
|
|
|
|
7
|
|
|
use X509\Certificate\Certificate; |
|
8
|
|
|
use X509\Certificate\Extension\CertificatePolicy\PolicyInformation; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Configuration for the certification path validation process. |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-6.1.1 |
|
14
|
|
|
*/ |
|
15
|
|
|
class PathValidationConfig |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Maximum allowed certification path length. |
|
19
|
|
|
* |
|
20
|
|
|
* @var int $_maxLength |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $_maxLength; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Reference time. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \DateTimeImmutable $_dateTime |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $_dateTime; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* List of acceptable policy identifiers. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string[] $_policySet |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_policySet; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Trust anchor certificate. |
|
40
|
|
|
* |
|
41
|
|
|
* If not set, path validation uses the first certificate of the path. |
|
42
|
|
|
* |
|
43
|
|
|
* @var Certificate|null $_trustAnchor |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $_trustAnchor; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Whether policy mapping in inhibited. |
|
49
|
|
|
* |
|
50
|
|
|
* Setting this to true disallows policy mapping. |
|
51
|
|
|
* |
|
52
|
|
|
* @var bool $_policyMappingInhibit |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $_policyMappingInhibit; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Whether the path must be valid for at least one policy in the |
|
58
|
|
|
* initial policy set. |
|
59
|
|
|
* |
|
60
|
|
|
* @var bool $_explicitPolicy |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $_explicitPolicy; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Whether anyPolicy OID processing should be inhibited. |
|
66
|
|
|
* |
|
67
|
|
|
* Setting this to true disallows the usage of anyPolicy. |
|
68
|
|
|
* |
|
69
|
|
|
* @var bool $_anyPolicyInhibit |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $_anyPolicyInhibit; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
* @todo Implement |
|
76
|
|
|
* @var mixed $_permittedSubtrees |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $_permittedSubtrees; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* |
|
82
|
|
|
* @todo Implement |
|
83
|
|
|
* @var mixed $_excludedSubtrees |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $_excludedSubtrees; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Constructor. |
|
89
|
|
|
* |
|
90
|
|
|
* @param \DateTimeImmutable $dt Reference date and time |
|
91
|
|
|
* @param int $max_length Maximum certification path length |
|
92
|
|
|
*/ |
|
93
|
49 |
|
public function __construct(\DateTimeImmutable $dt, int $max_length) |
|
94
|
|
|
{ |
|
95
|
49 |
|
$this->_dateTime = $dt; |
|
96
|
49 |
|
$this->_maxLength = (int) $max_length; |
|
97
|
49 |
|
$this->_policySet = array((string) PolicyInformation::OID_ANY_POLICY); |
|
98
|
49 |
|
$this->_policyMappingInhibit = false; |
|
99
|
49 |
|
$this->_explicitPolicy = false; |
|
100
|
49 |
|
$this->_anyPolicyInhibit = false; |
|
101
|
49 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get default configuration. |
|
105
|
|
|
* |
|
106
|
|
|
* @return self |
|
107
|
|
|
*/ |
|
108
|
24 |
|
public static function defaultConfig(): self |
|
109
|
|
|
{ |
|
110
|
24 |
|
return new self(new \DateTimeImmutable(), 3); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get self with maximum path length. |
|
115
|
|
|
* |
|
116
|
|
|
* @param int $length |
|
117
|
|
|
* @return self |
|
118
|
|
|
*/ |
|
119
|
14 |
|
public function withMaxLength(int $length): self |
|
120
|
|
|
{ |
|
121
|
14 |
|
$obj = clone $this; |
|
122
|
14 |
|
$obj->_maxLength = $length; |
|
123
|
14 |
|
return $obj; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get self with reference date and time. |
|
128
|
|
|
* |
|
129
|
|
|
* @param \DateTimeImmutable $dt |
|
130
|
|
|
* @return self |
|
131
|
|
|
*/ |
|
132
|
16 |
|
public function withDateTime(\DateTimeImmutable $dt): self |
|
133
|
|
|
{ |
|
134
|
16 |
|
$obj = clone $this; |
|
135
|
16 |
|
$obj->_dateTime = $dt; |
|
136
|
16 |
|
return $obj; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get self with trust anchor certificate. |
|
141
|
|
|
* |
|
142
|
|
|
* @param Certificate $ca |
|
143
|
|
|
* @return self |
|
144
|
|
|
*/ |
|
145
|
2 |
|
public function withTrustAnchor(Certificate $ca): self |
|
146
|
|
|
{ |
|
147
|
2 |
|
$obj = clone $this; |
|
148
|
2 |
|
$obj->_trustAnchor = $ca; |
|
149
|
2 |
|
return $obj; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get self with initial-policy-mapping-inhibit set. |
|
154
|
|
|
* |
|
155
|
|
|
* @param bool $flag |
|
156
|
|
|
* @return self |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function withPolicyMappingInhibit(bool $flag): self |
|
159
|
|
|
{ |
|
160
|
1 |
|
$obj = clone $this; |
|
161
|
1 |
|
$obj->_policyMappingInhibit = $flag; |
|
162
|
1 |
|
return $obj; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get self with initial-explicit-policy set. |
|
167
|
|
|
* |
|
168
|
|
|
* @param bool $flag |
|
169
|
|
|
* @return self |
|
170
|
|
|
*/ |
|
171
|
7 |
|
public function withExplicitPolicy(bool $flag): self |
|
172
|
|
|
{ |
|
173
|
7 |
|
$obj = clone $this; |
|
174
|
7 |
|
$obj->_explicitPolicy = $flag; |
|
175
|
7 |
|
return $obj; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get self with initial-any-policy-inhibit set. |
|
180
|
|
|
* |
|
181
|
|
|
* @param bool $flag |
|
182
|
|
|
* @return self |
|
183
|
|
|
*/ |
|
184
|
1 |
|
public function withAnyPolicyInhibit(bool $flag): self |
|
185
|
|
|
{ |
|
186
|
1 |
|
$obj = clone $this; |
|
187
|
1 |
|
$obj->_anyPolicyInhibit = $flag; |
|
188
|
1 |
|
return $obj; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Get self with user-initial-policy-set set to policy OIDs. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string ...$policies List of policy OIDs |
|
195
|
|
|
* @return self |
|
196
|
|
|
*/ |
|
197
|
6 |
|
public function withPolicySet(string ...$policies): self |
|
198
|
|
|
{ |
|
199
|
6 |
|
$obj = clone $this; |
|
200
|
6 |
|
$obj->_policySet = $policies; |
|
201
|
6 |
|
return $obj; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Get maximum certification path length. |
|
206
|
|
|
* |
|
207
|
|
|
* @return int |
|
208
|
|
|
*/ |
|
209
|
48 |
|
public function maxLength(): int |
|
210
|
|
|
{ |
|
211
|
48 |
|
return $this->_maxLength; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get reference date and time. |
|
216
|
|
|
* |
|
217
|
|
|
* @return \DateTimeImmutable |
|
218
|
|
|
*/ |
|
219
|
45 |
|
public function dateTime(): \DateTimeImmutable |
|
220
|
|
|
{ |
|
221
|
45 |
|
return $this->_dateTime; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Get user-initial-policy-set. |
|
226
|
|
|
* |
|
227
|
|
|
* @return string[] Array of OID's |
|
228
|
|
|
*/ |
|
229
|
10 |
|
public function policySet(): array |
|
230
|
|
|
{ |
|
231
|
10 |
|
return $this->_policySet; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Check whether trust anchor certificate is set. |
|
236
|
|
|
* |
|
237
|
|
|
* @return bool |
|
238
|
|
|
*/ |
|
239
|
47 |
|
public function hasTrustAnchor(): bool |
|
240
|
|
|
{ |
|
241
|
47 |
|
return isset($this->_trustAnchor); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get trust anchor certificate. |
|
246
|
|
|
* |
|
247
|
|
|
* @throws \LogicException |
|
248
|
|
|
* @return Certificate |
|
249
|
|
|
*/ |
|
250
|
3 |
|
public function trustAnchor(): Certificate |
|
251
|
|
|
{ |
|
252
|
3 |
|
if (!$this->hasTrustAnchor()) { |
|
253
|
1 |
|
throw new \LogicException("No trust anchor."); |
|
254
|
|
|
} |
|
255
|
2 |
|
return $this->_trustAnchor; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Get initial-policy-mapping-inhibit. |
|
260
|
|
|
* |
|
261
|
|
|
* @return bool |
|
262
|
|
|
*/ |
|
263
|
48 |
|
public function policyMappingInhibit(): bool |
|
264
|
|
|
{ |
|
265
|
48 |
|
return $this->_policyMappingInhibit; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Get initial-explicit-policy. |
|
270
|
|
|
* |
|
271
|
|
|
* @return bool |
|
272
|
|
|
*/ |
|
273
|
48 |
|
public function explicitPolicy(): bool |
|
274
|
|
|
{ |
|
275
|
48 |
|
return $this->_explicitPolicy; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Get initial-any-policy-inhibit. |
|
280
|
|
|
* |
|
281
|
|
|
* @return bool |
|
282
|
|
|
*/ |
|
283
|
48 |
|
public function anyPolicyInhibit(): bool |
|
284
|
|
|
{ |
|
285
|
48 |
|
return $this->_anyPolicyInhibit; |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|