1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace X509\Certificate; |
4
|
|
|
|
5
|
|
|
use ASN1\Type\Constructed\Sequence; |
6
|
|
|
use ASN1\Type\UnspecifiedType; |
7
|
|
|
use X509\Certificate\Extension\AuthorityKeyIdentifierExtension; |
8
|
|
|
use X509\Certificate\Extension\BasicConstraintsExtension; |
9
|
|
|
use X509\Certificate\Extension\CertificatePoliciesExtension; |
10
|
|
|
use X509\Certificate\Extension\CRLDistributionPointsExtension; |
11
|
|
|
use X509\Certificate\Extension\ExtendedKeyUsageExtension; |
12
|
|
|
use X509\Certificate\Extension\Extension; |
13
|
|
|
use X509\Certificate\Extension\InhibitAnyPolicyExtension; |
14
|
|
|
use X509\Certificate\Extension\IssuerAlternativeNameExtension; |
15
|
|
|
use X509\Certificate\Extension\KeyUsageExtension; |
16
|
|
|
use X509\Certificate\Extension\NameConstraintsExtension; |
17
|
|
|
use X509\Certificate\Extension\PolicyConstraintsExtension; |
18
|
|
|
use X509\Certificate\Extension\PolicyMappingsExtension; |
19
|
|
|
use X509\Certificate\Extension\SubjectAlternativeNameExtension; |
20
|
|
|
use X509\Certificate\Extension\SubjectKeyIdentifierExtension; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Implements <i>Extensions</i> ASN.1 type. |
25
|
|
|
* |
26
|
|
|
* Several convenience methods are provided to fetch commonly used |
27
|
|
|
* standard extensions. Others can be accessed using <code>get($oid)</code>. |
28
|
|
|
* |
29
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-4.1.2.9 |
30
|
|
|
*/ |
31
|
|
|
class Extensions implements \Countable, \IteratorAggregate |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Extensions |
35
|
|
|
* |
36
|
|
|
* @var Extension[] $_extensions |
37
|
|
|
*/ |
38
|
|
|
protected $_extensions; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* |
43
|
|
|
* @param Extension ...$extensions Extension objects |
44
|
|
|
*/ |
45
|
62 |
|
public function __construct(Extension ...$extensions) { |
46
|
62 |
|
$this->_extensions = array(); |
47
|
62 |
|
foreach ($extensions as $ext) { |
48
|
38 |
|
$this->_extensions[$ext->oid()] = $ext; |
49
|
62 |
|
} |
50
|
62 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize from ASN.1. |
54
|
|
|
* |
55
|
|
|
* @param Sequence $seq |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
24 |
|
public static function fromASN1(Sequence $seq) { |
59
|
24 |
|
$extensions = array_map( |
60
|
|
|
function (UnspecifiedType $el) { |
61
|
21 |
|
return Extension::fromASN1($el->asSequence()); |
62
|
24 |
|
}, $seq->elements()); |
63
|
24 |
|
return new self(...$extensions); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Generate ASN.1 structure. |
68
|
|
|
* |
69
|
|
|
* @return Sequence |
70
|
|
|
*/ |
71
|
56 |
|
public function toASN1() { |
72
|
56 |
|
$elements = array_values( |
73
|
56 |
|
array_map(function ($ext) { |
74
|
50 |
|
return $ext->toASN1(); |
75
|
56 |
|
}, $this->_extensions)); |
76
|
56 |
|
return new Sequence(...$elements); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get self with extensions added. |
81
|
|
|
* |
82
|
|
|
* @param Extension ...$ext One or more extensions to add |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
5 |
|
public function withExtensions(Extension ...$exts) { |
86
|
5 |
|
$obj = clone $this; |
87
|
5 |
|
foreach ($exts as $ext) { |
88
|
5 |
|
$obj->_extensions[$ext->oid()] = $ext; |
89
|
5 |
|
} |
90
|
5 |
|
return $obj; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check whether extension is present. |
95
|
|
|
* |
96
|
|
|
* @param string $oid Extensions OID |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
96 |
|
public function has($oid) { |
100
|
96 |
|
return isset($this->_extensions[$oid]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get extension by OID. |
105
|
|
|
* |
106
|
|
|
* @param string $oid |
107
|
|
|
* @throws \LogicException |
108
|
|
|
* @return Extension |
109
|
|
|
*/ |
110
|
75 |
|
public function get($oid) { |
111
|
75 |
|
if (!$this->has($oid)) { |
112
|
1 |
|
throw new \LogicException("No extension by OID $oid."); |
113
|
|
|
} |
114
|
74 |
|
return $this->_extensions[$oid]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check whether 'Authority Key Identifier' extension is present. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
10 |
|
public function hasAuthorityKeyIdentifier() { |
123
|
10 |
|
return $this->has(Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get 'Authority Key Identifier' extension. |
128
|
|
|
* |
129
|
|
|
* @return AuthorityKeyIdentifierExtension |
130
|
|
|
*/ |
131
|
11 |
|
public function authorityKeyIdentifier() { |
132
|
11 |
|
return $this->get(Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check whether 'Subject Key Identifier' extension is present. |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
12 |
|
public function hasSubjectKeyIdentifier() { |
141
|
12 |
|
return $this->has(Extension::OID_SUBJECT_KEY_IDENTIFIER); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get 'Subject Key Identifier' extension. |
146
|
|
|
* |
147
|
|
|
* @return SubjectKeyIdentifierExtension |
148
|
|
|
*/ |
149
|
11 |
|
public function subjectKeyIdentifier() { |
150
|
11 |
|
return $this->get(Extension::OID_SUBJECT_KEY_IDENTIFIER); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Check whether 'Key Usage' extension is present. |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
33 |
|
public function hasKeyUsage() { |
159
|
33 |
|
return $this->has(Extension::OID_KEY_USAGE); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get 'Key Usage' extension. |
164
|
|
|
* |
165
|
|
|
* @return KeyUsageExtension |
166
|
|
|
*/ |
167
|
21 |
|
public function keyUsage() { |
168
|
21 |
|
return $this->get(Extension::OID_KEY_USAGE); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check whether 'Certificate Policies' extension is present. |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
36 |
|
public function hasCertificatePolicies() { |
177
|
36 |
|
return $this->has(Extension::OID_CERTIFICATE_POLICIES); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get 'Certificate Policies' extension. |
182
|
|
|
* |
183
|
|
|
* @return CertificatePoliciesExtension |
184
|
|
|
*/ |
185
|
1 |
|
public function certificatePolicies() { |
186
|
1 |
|
return $this->get(Extension::OID_CERTIFICATE_POLICIES); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Check whether 'Policy Mappings' extension is present. |
191
|
|
|
* |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
36 |
|
public function hasPolicyMappings() { |
195
|
36 |
|
return $this->has(Extension::OID_POLICY_MAPPINGS); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get 'Policy Mappings' extension. |
200
|
|
|
* |
201
|
|
|
* @return PolicyMappingsExtension |
202
|
|
|
*/ |
203
|
4 |
|
public function policyMappings() { |
204
|
4 |
|
return $this->get(Extension::OID_POLICY_MAPPINGS); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Check whether 'Subject Alternative Name' extension is present. |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
3 |
|
public function hasSubjectAlternativeName() { |
213
|
3 |
|
return $this->has(Extension::OID_SUBJECT_ALT_NAME); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get 'Subject Alternative Name' extension. |
218
|
|
|
* |
219
|
|
|
* @return SubjectAlternativeNameExtension |
220
|
|
|
*/ |
221
|
4 |
|
public function subjectAlternativeName() { |
222
|
4 |
|
return $this->get(Extension::OID_SUBJECT_ALT_NAME); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Check whether 'Issuer Alternative Name' extension is present. |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
1 |
|
public function hasIssuerAlternativeName() { |
231
|
1 |
|
return $this->has(Extension::OID_ISSUER_ALT_NAME); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get 'Issuer Alternative Name' extension. |
236
|
|
|
* |
237
|
|
|
* @return IssuerAlternativeNameExtension |
238
|
|
|
*/ |
239
|
1 |
|
public function issuerAlternativeName() { |
240
|
1 |
|
return $this->get(Extension::OID_ISSUER_ALT_NAME); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Check whether 'Basic Constraints' extension is present. |
245
|
|
|
* |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
35 |
|
public function hasBasicConstraints() { |
249
|
35 |
|
return $this->has(Extension::OID_BASIC_CONSTRAINTS); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get 'Basic Constraints' extension. |
254
|
|
|
* |
255
|
|
|
* @return BasicConstraintsExtension |
256
|
|
|
*/ |
257
|
32 |
|
public function basicConstraints() { |
258
|
32 |
|
return $this->get(Extension::OID_BASIC_CONSTRAINTS); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Check whether 'Name Constraints' extension is present. |
263
|
|
|
* |
264
|
|
|
* @return bool |
265
|
|
|
*/ |
266
|
35 |
|
public function hasNameConstraints() { |
267
|
35 |
|
return $this->has(Extension::OID_NAME_CONSTRAINTS); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Get 'Name Constraints' extension. |
272
|
|
|
* |
273
|
|
|
* @return NameConstraintsExtension |
274
|
|
|
*/ |
275
|
1 |
|
public function nameConstraints() { |
276
|
1 |
|
return $this->get(Extension::OID_NAME_CONSTRAINTS); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Check whether 'Policy Constraints' extension is present. |
281
|
|
|
* |
282
|
|
|
* @return bool |
283
|
|
|
*/ |
284
|
35 |
|
public function hasPolicyConstraints() { |
285
|
35 |
|
return $this->has(Extension::OID_POLICY_CONSTRAINTS); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Get 'Policy Constraints' extension. |
290
|
|
|
* |
291
|
|
|
* @return PolicyConstraintsExtension |
292
|
|
|
*/ |
293
|
16 |
|
public function policyConstraints() { |
294
|
16 |
|
return $this->get(Extension::OID_POLICY_CONSTRAINTS); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Check whether 'Extended Key Usage' extension is present. |
299
|
|
|
* |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
1 |
|
public function hasExtendedKeyUsage() { |
303
|
1 |
|
return $this->has(Extension::OID_EXT_KEY_USAGE); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get 'Extended Key Usage' extension. |
308
|
|
|
* |
309
|
|
|
* @return ExtendedKeyUsageExtension |
310
|
|
|
*/ |
311
|
1 |
|
public function extendedKeyUsage() { |
312
|
1 |
|
return $this->get(Extension::OID_EXT_KEY_USAGE); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Check whether 'CRL Distribution Points' extension is present. |
317
|
|
|
* |
318
|
|
|
* @return bool |
319
|
|
|
*/ |
320
|
1 |
|
public function hasCRLDistributionPoints() { |
321
|
1 |
|
return $this->has(Extension::OID_CRL_DISTRIBUTION_POINTS); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get 'CRL Distribution Points' extension. |
326
|
|
|
* |
327
|
|
|
* @return CRLDistributionPointsExtension |
328
|
|
|
*/ |
329
|
1 |
|
public function crlDistributionPoints() { |
330
|
1 |
|
return $this->get(Extension::OID_CRL_DISTRIBUTION_POINTS); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Check whether 'Inhibit anyPolicy' extension is present. |
335
|
|
|
* |
336
|
|
|
* @return bool |
337
|
|
|
*/ |
338
|
35 |
|
public function hasInhibitAnyPolicy() { |
339
|
35 |
|
return $this->has(Extension::OID_INHIBIT_ANY_POLICY); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get 'Inhibit anyPolicy' extension. |
344
|
|
|
* |
345
|
|
|
* @return InhibitAnyPolicyExtension |
346
|
|
|
*/ |
347
|
2 |
|
public function inhibitAnyPolicy() { |
348
|
2 |
|
return $this->get(Extension::OID_INHIBIT_ANY_POLICY); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* |
353
|
|
|
* @see Countable::count() |
354
|
|
|
* @return int |
355
|
|
|
*/ |
356
|
67 |
|
public function count() { |
357
|
67 |
|
return count($this->_extensions); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Get iterator for extensions. |
362
|
|
|
* |
363
|
|
|
* @see IteratorAggregate::getIterator() |
364
|
|
|
* @return \Traversable |
365
|
|
|
*/ |
366
|
1 |
|
public function getIterator() { |
367
|
1 |
|
return new \ArrayIterator($this->_extensions); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|