@@ -22,450 +22,450 @@ |
||
22 | 22 | */ |
23 | 23 | class AttributeCertificateInfo |
24 | 24 | { |
25 | - const VERSION_2 = 1; |
|
26 | - |
|
27 | - /** |
|
28 | - * AC version. |
|
29 | - * |
|
30 | - * @var int |
|
31 | - */ |
|
32 | - protected $_version; |
|
33 | - |
|
34 | - /** |
|
35 | - * AC holder. |
|
36 | - * |
|
37 | - * @var Holder |
|
38 | - */ |
|
39 | - protected $_holder; |
|
40 | - |
|
41 | - /** |
|
42 | - * AC issuer. |
|
43 | - * |
|
44 | - * @var AttCertIssuer |
|
45 | - */ |
|
46 | - protected $_issuer; |
|
47 | - |
|
48 | - /** |
|
49 | - * Signature algorithm identifier. |
|
50 | - * |
|
51 | - * @var SignatureAlgorithmIdentifier |
|
52 | - */ |
|
53 | - protected $_signature; |
|
54 | - |
|
55 | - /** |
|
56 | - * AC serial number as a base 10 integer. |
|
57 | - * |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $_serialNumber; |
|
61 | - |
|
62 | - /** |
|
63 | - * Validity period. |
|
64 | - * |
|
65 | - * @var AttCertValidityPeriod |
|
66 | - */ |
|
67 | - protected $_attrCertValidityPeriod; |
|
68 | - |
|
69 | - /** |
|
70 | - * Attributes. |
|
71 | - * |
|
72 | - * @var Attributes |
|
73 | - */ |
|
74 | - protected $_attributes; |
|
75 | - |
|
76 | - /** |
|
77 | - * Issuer unique identifier. |
|
78 | - * |
|
79 | - * @var null|UniqueIdentifier |
|
80 | - */ |
|
81 | - protected $_issuerUniqueID; |
|
82 | - |
|
83 | - /** |
|
84 | - * Extensions. |
|
85 | - * |
|
86 | - * @var Extensions |
|
87 | - */ |
|
88 | - protected $_extensions; |
|
89 | - |
|
90 | - /** |
|
91 | - * Constructor. |
|
92 | - * |
|
93 | - * @param Holder $holder AC holder |
|
94 | - * @param AttCertIssuer $issuer AC issuer |
|
95 | - * @param AttCertValidityPeriod $validity Validity |
|
96 | - * @param Attributes $attribs Attributes |
|
97 | - */ |
|
98 | - public function __construct(Holder $holder, AttCertIssuer $issuer, |
|
99 | - AttCertValidityPeriod $validity, Attributes $attribs) |
|
100 | - { |
|
101 | - $this->_version = self::VERSION_2; |
|
102 | - $this->_holder = $holder; |
|
103 | - $this->_issuer = $issuer; |
|
104 | - $this->_attrCertValidityPeriod = $validity; |
|
105 | - $this->_attributes = $attribs; |
|
106 | - $this->_extensions = new Extensions(); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Initialize from ASN.1. |
|
111 | - * |
|
112 | - * @param Sequence $seq |
|
113 | - * |
|
114 | - * @throws \UnexpectedValueException |
|
115 | - * |
|
116 | - * @return self |
|
117 | - */ |
|
118 | - public static function fromASN1(Sequence $seq): self |
|
119 | - { |
|
120 | - $idx = 0; |
|
121 | - $version = $seq->at($idx++)->asInteger()->intNumber(); |
|
122 | - if (self::VERSION_2 !== $version) { |
|
123 | - throw new \UnexpectedValueException('Version must be 2.'); |
|
124 | - } |
|
125 | - $holder = Holder::fromASN1($seq->at($idx++)->asSequence()); |
|
126 | - $issuer = AttCertIssuer::fromASN1($seq->at($idx++)); |
|
127 | - $signature = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
128 | - if (!$signature instanceof SignatureAlgorithmIdentifier) { |
|
129 | - throw new \UnexpectedValueException( |
|
130 | - 'Unsupported signature algorithm ' . $signature->oid() . '.'); |
|
131 | - } |
|
132 | - $serial = $seq->at($idx++)->asInteger()->number(); |
|
133 | - $validity = AttCertValidityPeriod::fromASN1($seq->at($idx++)->asSequence()); |
|
134 | - $attribs = Attributes::fromASN1($seq->at($idx++)->asSequence()); |
|
135 | - $obj = new self($holder, $issuer, $validity, $attribs); |
|
136 | - $obj->_signature = $signature; |
|
137 | - $obj->_serialNumber = $serial; |
|
138 | - if ($seq->has($idx, Element::TYPE_BIT_STRING)) { |
|
139 | - $obj->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
140 | - $seq->at($idx++)->asBitString()); |
|
141 | - } |
|
142 | - if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
143 | - $obj->_extensions = Extensions::fromASN1( |
|
144 | - $seq->at($idx++)->asSequence()); |
|
145 | - } |
|
146 | - return $obj; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Get self with holder. |
|
151 | - * |
|
152 | - * @param Holder $holder |
|
153 | - * |
|
154 | - * @return self |
|
155 | - */ |
|
156 | - public function withHolder(Holder $holder): self |
|
157 | - { |
|
158 | - $obj = clone $this; |
|
159 | - $obj->_holder = $holder; |
|
160 | - return $obj; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get self with issuer. |
|
165 | - * |
|
166 | - * @param AttCertIssuer $issuer |
|
167 | - * |
|
168 | - * @return self |
|
169 | - */ |
|
170 | - public function withIssuer(AttCertIssuer $issuer): self |
|
171 | - { |
|
172 | - $obj = clone $this; |
|
173 | - $obj->_issuer = $issuer; |
|
174 | - return $obj; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Get self with signature algorithm identifier. |
|
179 | - * |
|
180 | - * @param SignatureAlgorithmIdentifier $algo |
|
181 | - * |
|
182 | - * @return self |
|
183 | - */ |
|
184 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
185 | - { |
|
186 | - $obj = clone $this; |
|
187 | - $obj->_signature = $algo; |
|
188 | - return $obj; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Get self with serial number. |
|
193 | - * |
|
194 | - * @param int|string $serial Base 10 serial number |
|
195 | - * |
|
196 | - * @return self |
|
197 | - */ |
|
198 | - public function withSerialNumber($serial): self |
|
199 | - { |
|
200 | - $obj = clone $this; |
|
201 | - $obj->_serialNumber = strval($serial); |
|
202 | - return $obj; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Get self with random positive serial number. |
|
207 | - * |
|
208 | - * @param int $size Number of random bytes |
|
209 | - * |
|
210 | - * @return self |
|
211 | - */ |
|
212 | - public function withRandomSerialNumber(int $size = 16): self |
|
213 | - { |
|
214 | - // ensure that first byte is always non-zero and having first bit unset |
|
215 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
216 | - for ($i = 1; $i < $size; ++$i) { |
|
217 | - $num <<= 8; |
|
218 | - $num += mt_rand(0, 0xff); |
|
219 | - } |
|
220 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Get self with validity period. |
|
225 | - * |
|
226 | - * @param AttCertValidityPeriod $validity |
|
227 | - * |
|
228 | - * @return self |
|
229 | - */ |
|
230 | - public function withValidity(AttCertValidityPeriod $validity): self |
|
231 | - { |
|
232 | - $obj = clone $this; |
|
233 | - $obj->_attrCertValidityPeriod = $validity; |
|
234 | - return $obj; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Get self with attributes. |
|
239 | - * |
|
240 | - * @param Attributes $attribs |
|
241 | - * |
|
242 | - * @return self |
|
243 | - */ |
|
244 | - public function withAttributes(Attributes $attribs): self |
|
245 | - { |
|
246 | - $obj = clone $this; |
|
247 | - $obj->_attributes = $attribs; |
|
248 | - return $obj; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Get self with issuer unique identifier. |
|
253 | - * |
|
254 | - * @param UniqueIdentifier $uid |
|
255 | - * |
|
256 | - * @return self |
|
257 | - */ |
|
258 | - public function withIssuerUniqueID(UniqueIdentifier $uid): self |
|
259 | - { |
|
260 | - $obj = clone $this; |
|
261 | - $obj->_issuerUniqueID = $uid; |
|
262 | - return $obj; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Get self with extensions. |
|
267 | - * |
|
268 | - * @param Extensions $extensions |
|
269 | - * |
|
270 | - * @return self |
|
271 | - */ |
|
272 | - public function withExtensions(Extensions $extensions): self |
|
273 | - { |
|
274 | - $obj = clone $this; |
|
275 | - $obj->_extensions = $extensions; |
|
276 | - return $obj; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Get self with extensions added. |
|
281 | - * |
|
282 | - * @param Extension ...$exts One or more Extension objects |
|
283 | - * |
|
284 | - * @return self |
|
285 | - */ |
|
286 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
287 | - { |
|
288 | - $obj = clone $this; |
|
289 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
290 | - return $obj; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * Get version. |
|
295 | - * |
|
296 | - * @return int |
|
297 | - */ |
|
298 | - public function version(): int |
|
299 | - { |
|
300 | - return $this->_version; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Get AC holder. |
|
305 | - * |
|
306 | - * @return Holder |
|
307 | - */ |
|
308 | - public function holder(): Holder |
|
309 | - { |
|
310 | - return $this->_holder; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Get AC issuer. |
|
315 | - * |
|
316 | - * @return AttCertIssuer |
|
317 | - */ |
|
318 | - public function issuer(): AttCertIssuer |
|
319 | - { |
|
320 | - return $this->_issuer; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Check whether signature is set. |
|
325 | - * |
|
326 | - * @return bool |
|
327 | - */ |
|
328 | - public function hasSignature(): bool |
|
329 | - { |
|
330 | - return isset($this->_signature); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Get signature algorithm identifier. |
|
335 | - * |
|
336 | - * @throws \LogicException If not set |
|
337 | - * |
|
338 | - * @return SignatureAlgorithmIdentifier |
|
339 | - */ |
|
340 | - public function signature(): SignatureAlgorithmIdentifier |
|
341 | - { |
|
342 | - if (!$this->hasSignature()) { |
|
343 | - throw new \LogicException('signature not set.'); |
|
344 | - } |
|
345 | - return $this->_signature; |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Check whether serial number is present. |
|
350 | - * |
|
351 | - * @return bool |
|
352 | - */ |
|
353 | - public function hasSerialNumber(): bool |
|
354 | - { |
|
355 | - return isset($this->_serialNumber); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Get AC serial number as a base 10 integer. |
|
360 | - * |
|
361 | - * @throws \LogicException If not set |
|
362 | - * |
|
363 | - * @return string |
|
364 | - */ |
|
365 | - public function serialNumber(): string |
|
366 | - { |
|
367 | - if (!$this->hasSerialNumber()) { |
|
368 | - throw new \LogicException('serialNumber not set.'); |
|
369 | - } |
|
370 | - return $this->_serialNumber; |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Get validity period. |
|
375 | - * |
|
376 | - * @return AttCertValidityPeriod |
|
377 | - */ |
|
378 | - public function validityPeriod(): AttCertValidityPeriod |
|
379 | - { |
|
380 | - return $this->_attrCertValidityPeriod; |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Get attributes. |
|
385 | - * |
|
386 | - * @return Attributes |
|
387 | - */ |
|
388 | - public function attributes(): Attributes |
|
389 | - { |
|
390 | - return $this->_attributes; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Check whether issuer unique identifier is present. |
|
395 | - * |
|
396 | - * @return bool |
|
397 | - */ |
|
398 | - public function hasIssuerUniqueID(): bool |
|
399 | - { |
|
400 | - return isset($this->_issuerUniqueID); |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Get issuer unique identifier. |
|
405 | - * |
|
406 | - * @throws \LogicException If not set |
|
407 | - * |
|
408 | - * @return UniqueIdentifier |
|
409 | - */ |
|
410 | - public function issuerUniqueID(): UniqueIdentifier |
|
411 | - { |
|
412 | - if (!$this->hasIssuerUniqueID()) { |
|
413 | - throw new \LogicException('issuerUniqueID not set.'); |
|
414 | - } |
|
415 | - return $this->_issuerUniqueID; |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Get extensions. |
|
420 | - * |
|
421 | - * @return Extensions |
|
422 | - */ |
|
423 | - public function extensions(): Extensions |
|
424 | - { |
|
425 | - return $this->_extensions; |
|
426 | - } |
|
427 | - |
|
428 | - /** |
|
429 | - * Get ASN.1 structure. |
|
430 | - * |
|
431 | - * @return Sequence |
|
432 | - */ |
|
433 | - public function toASN1(): Sequence |
|
434 | - { |
|
435 | - $elements = [new Integer($this->_version), $this->_holder->toASN1(), |
|
436 | - $this->_issuer->toASN1(), $this->signature()->toASN1(), |
|
437 | - new Integer($this->serialNumber()), |
|
438 | - $this->_attrCertValidityPeriod->toASN1(), |
|
439 | - $this->_attributes->toASN1(), ]; |
|
440 | - if (isset($this->_issuerUniqueID)) { |
|
441 | - $elements[] = $this->_issuerUniqueID->toASN1(); |
|
442 | - } |
|
443 | - if (count($this->_extensions)) { |
|
444 | - $elements[] = $this->_extensions->toASN1(); |
|
445 | - } |
|
446 | - return new Sequence(...$elements); |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Create signed attribute certificate. |
|
451 | - * |
|
452 | - * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
|
453 | - * @param PrivateKeyInfo $privkey_info Private key |
|
454 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
455 | - * |
|
456 | - * @return AttributeCertificate |
|
457 | - */ |
|
458 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
459 | - PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): AttributeCertificate |
|
460 | - { |
|
461 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
462 | - $aci = clone $this; |
|
463 | - if (!isset($aci->_serialNumber)) { |
|
464 | - $aci->_serialNumber = '0'; |
|
465 | - } |
|
466 | - $aci->_signature = $algo; |
|
467 | - $data = $aci->toASN1()->toDER(); |
|
468 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
469 | - return new AttributeCertificate($aci, $algo, $signature); |
|
470 | - } |
|
25 | + const VERSION_2 = 1; |
|
26 | + |
|
27 | + /** |
|
28 | + * AC version. |
|
29 | + * |
|
30 | + * @var int |
|
31 | + */ |
|
32 | + protected $_version; |
|
33 | + |
|
34 | + /** |
|
35 | + * AC holder. |
|
36 | + * |
|
37 | + * @var Holder |
|
38 | + */ |
|
39 | + protected $_holder; |
|
40 | + |
|
41 | + /** |
|
42 | + * AC issuer. |
|
43 | + * |
|
44 | + * @var AttCertIssuer |
|
45 | + */ |
|
46 | + protected $_issuer; |
|
47 | + |
|
48 | + /** |
|
49 | + * Signature algorithm identifier. |
|
50 | + * |
|
51 | + * @var SignatureAlgorithmIdentifier |
|
52 | + */ |
|
53 | + protected $_signature; |
|
54 | + |
|
55 | + /** |
|
56 | + * AC serial number as a base 10 integer. |
|
57 | + * |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $_serialNumber; |
|
61 | + |
|
62 | + /** |
|
63 | + * Validity period. |
|
64 | + * |
|
65 | + * @var AttCertValidityPeriod |
|
66 | + */ |
|
67 | + protected $_attrCertValidityPeriod; |
|
68 | + |
|
69 | + /** |
|
70 | + * Attributes. |
|
71 | + * |
|
72 | + * @var Attributes |
|
73 | + */ |
|
74 | + protected $_attributes; |
|
75 | + |
|
76 | + /** |
|
77 | + * Issuer unique identifier. |
|
78 | + * |
|
79 | + * @var null|UniqueIdentifier |
|
80 | + */ |
|
81 | + protected $_issuerUniqueID; |
|
82 | + |
|
83 | + /** |
|
84 | + * Extensions. |
|
85 | + * |
|
86 | + * @var Extensions |
|
87 | + */ |
|
88 | + protected $_extensions; |
|
89 | + |
|
90 | + /** |
|
91 | + * Constructor. |
|
92 | + * |
|
93 | + * @param Holder $holder AC holder |
|
94 | + * @param AttCertIssuer $issuer AC issuer |
|
95 | + * @param AttCertValidityPeriod $validity Validity |
|
96 | + * @param Attributes $attribs Attributes |
|
97 | + */ |
|
98 | + public function __construct(Holder $holder, AttCertIssuer $issuer, |
|
99 | + AttCertValidityPeriod $validity, Attributes $attribs) |
|
100 | + { |
|
101 | + $this->_version = self::VERSION_2; |
|
102 | + $this->_holder = $holder; |
|
103 | + $this->_issuer = $issuer; |
|
104 | + $this->_attrCertValidityPeriod = $validity; |
|
105 | + $this->_attributes = $attribs; |
|
106 | + $this->_extensions = new Extensions(); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Initialize from ASN.1. |
|
111 | + * |
|
112 | + * @param Sequence $seq |
|
113 | + * |
|
114 | + * @throws \UnexpectedValueException |
|
115 | + * |
|
116 | + * @return self |
|
117 | + */ |
|
118 | + public static function fromASN1(Sequence $seq): self |
|
119 | + { |
|
120 | + $idx = 0; |
|
121 | + $version = $seq->at($idx++)->asInteger()->intNumber(); |
|
122 | + if (self::VERSION_2 !== $version) { |
|
123 | + throw new \UnexpectedValueException('Version must be 2.'); |
|
124 | + } |
|
125 | + $holder = Holder::fromASN1($seq->at($idx++)->asSequence()); |
|
126 | + $issuer = AttCertIssuer::fromASN1($seq->at($idx++)); |
|
127 | + $signature = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
128 | + if (!$signature instanceof SignatureAlgorithmIdentifier) { |
|
129 | + throw new \UnexpectedValueException( |
|
130 | + 'Unsupported signature algorithm ' . $signature->oid() . '.'); |
|
131 | + } |
|
132 | + $serial = $seq->at($idx++)->asInteger()->number(); |
|
133 | + $validity = AttCertValidityPeriod::fromASN1($seq->at($idx++)->asSequence()); |
|
134 | + $attribs = Attributes::fromASN1($seq->at($idx++)->asSequence()); |
|
135 | + $obj = new self($holder, $issuer, $validity, $attribs); |
|
136 | + $obj->_signature = $signature; |
|
137 | + $obj->_serialNumber = $serial; |
|
138 | + if ($seq->has($idx, Element::TYPE_BIT_STRING)) { |
|
139 | + $obj->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
140 | + $seq->at($idx++)->asBitString()); |
|
141 | + } |
|
142 | + if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
143 | + $obj->_extensions = Extensions::fromASN1( |
|
144 | + $seq->at($idx++)->asSequence()); |
|
145 | + } |
|
146 | + return $obj; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Get self with holder. |
|
151 | + * |
|
152 | + * @param Holder $holder |
|
153 | + * |
|
154 | + * @return self |
|
155 | + */ |
|
156 | + public function withHolder(Holder $holder): self |
|
157 | + { |
|
158 | + $obj = clone $this; |
|
159 | + $obj->_holder = $holder; |
|
160 | + return $obj; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get self with issuer. |
|
165 | + * |
|
166 | + * @param AttCertIssuer $issuer |
|
167 | + * |
|
168 | + * @return self |
|
169 | + */ |
|
170 | + public function withIssuer(AttCertIssuer $issuer): self |
|
171 | + { |
|
172 | + $obj = clone $this; |
|
173 | + $obj->_issuer = $issuer; |
|
174 | + return $obj; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Get self with signature algorithm identifier. |
|
179 | + * |
|
180 | + * @param SignatureAlgorithmIdentifier $algo |
|
181 | + * |
|
182 | + * @return self |
|
183 | + */ |
|
184 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
185 | + { |
|
186 | + $obj = clone $this; |
|
187 | + $obj->_signature = $algo; |
|
188 | + return $obj; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Get self with serial number. |
|
193 | + * |
|
194 | + * @param int|string $serial Base 10 serial number |
|
195 | + * |
|
196 | + * @return self |
|
197 | + */ |
|
198 | + public function withSerialNumber($serial): self |
|
199 | + { |
|
200 | + $obj = clone $this; |
|
201 | + $obj->_serialNumber = strval($serial); |
|
202 | + return $obj; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Get self with random positive serial number. |
|
207 | + * |
|
208 | + * @param int $size Number of random bytes |
|
209 | + * |
|
210 | + * @return self |
|
211 | + */ |
|
212 | + public function withRandomSerialNumber(int $size = 16): self |
|
213 | + { |
|
214 | + // ensure that first byte is always non-zero and having first bit unset |
|
215 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
216 | + for ($i = 1; $i < $size; ++$i) { |
|
217 | + $num <<= 8; |
|
218 | + $num += mt_rand(0, 0xff); |
|
219 | + } |
|
220 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Get self with validity period. |
|
225 | + * |
|
226 | + * @param AttCertValidityPeriod $validity |
|
227 | + * |
|
228 | + * @return self |
|
229 | + */ |
|
230 | + public function withValidity(AttCertValidityPeriod $validity): self |
|
231 | + { |
|
232 | + $obj = clone $this; |
|
233 | + $obj->_attrCertValidityPeriod = $validity; |
|
234 | + return $obj; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Get self with attributes. |
|
239 | + * |
|
240 | + * @param Attributes $attribs |
|
241 | + * |
|
242 | + * @return self |
|
243 | + */ |
|
244 | + public function withAttributes(Attributes $attribs): self |
|
245 | + { |
|
246 | + $obj = clone $this; |
|
247 | + $obj->_attributes = $attribs; |
|
248 | + return $obj; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Get self with issuer unique identifier. |
|
253 | + * |
|
254 | + * @param UniqueIdentifier $uid |
|
255 | + * |
|
256 | + * @return self |
|
257 | + */ |
|
258 | + public function withIssuerUniqueID(UniqueIdentifier $uid): self |
|
259 | + { |
|
260 | + $obj = clone $this; |
|
261 | + $obj->_issuerUniqueID = $uid; |
|
262 | + return $obj; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Get self with extensions. |
|
267 | + * |
|
268 | + * @param Extensions $extensions |
|
269 | + * |
|
270 | + * @return self |
|
271 | + */ |
|
272 | + public function withExtensions(Extensions $extensions): self |
|
273 | + { |
|
274 | + $obj = clone $this; |
|
275 | + $obj->_extensions = $extensions; |
|
276 | + return $obj; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Get self with extensions added. |
|
281 | + * |
|
282 | + * @param Extension ...$exts One or more Extension objects |
|
283 | + * |
|
284 | + * @return self |
|
285 | + */ |
|
286 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
287 | + { |
|
288 | + $obj = clone $this; |
|
289 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
290 | + return $obj; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * Get version. |
|
295 | + * |
|
296 | + * @return int |
|
297 | + */ |
|
298 | + public function version(): int |
|
299 | + { |
|
300 | + return $this->_version; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Get AC holder. |
|
305 | + * |
|
306 | + * @return Holder |
|
307 | + */ |
|
308 | + public function holder(): Holder |
|
309 | + { |
|
310 | + return $this->_holder; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Get AC issuer. |
|
315 | + * |
|
316 | + * @return AttCertIssuer |
|
317 | + */ |
|
318 | + public function issuer(): AttCertIssuer |
|
319 | + { |
|
320 | + return $this->_issuer; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Check whether signature is set. |
|
325 | + * |
|
326 | + * @return bool |
|
327 | + */ |
|
328 | + public function hasSignature(): bool |
|
329 | + { |
|
330 | + return isset($this->_signature); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Get signature algorithm identifier. |
|
335 | + * |
|
336 | + * @throws \LogicException If not set |
|
337 | + * |
|
338 | + * @return SignatureAlgorithmIdentifier |
|
339 | + */ |
|
340 | + public function signature(): SignatureAlgorithmIdentifier |
|
341 | + { |
|
342 | + if (!$this->hasSignature()) { |
|
343 | + throw new \LogicException('signature not set.'); |
|
344 | + } |
|
345 | + return $this->_signature; |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Check whether serial number is present. |
|
350 | + * |
|
351 | + * @return bool |
|
352 | + */ |
|
353 | + public function hasSerialNumber(): bool |
|
354 | + { |
|
355 | + return isset($this->_serialNumber); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Get AC serial number as a base 10 integer. |
|
360 | + * |
|
361 | + * @throws \LogicException If not set |
|
362 | + * |
|
363 | + * @return string |
|
364 | + */ |
|
365 | + public function serialNumber(): string |
|
366 | + { |
|
367 | + if (!$this->hasSerialNumber()) { |
|
368 | + throw new \LogicException('serialNumber not set.'); |
|
369 | + } |
|
370 | + return $this->_serialNumber; |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Get validity period. |
|
375 | + * |
|
376 | + * @return AttCertValidityPeriod |
|
377 | + */ |
|
378 | + public function validityPeriod(): AttCertValidityPeriod |
|
379 | + { |
|
380 | + return $this->_attrCertValidityPeriod; |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Get attributes. |
|
385 | + * |
|
386 | + * @return Attributes |
|
387 | + */ |
|
388 | + public function attributes(): Attributes |
|
389 | + { |
|
390 | + return $this->_attributes; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Check whether issuer unique identifier is present. |
|
395 | + * |
|
396 | + * @return bool |
|
397 | + */ |
|
398 | + public function hasIssuerUniqueID(): bool |
|
399 | + { |
|
400 | + return isset($this->_issuerUniqueID); |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Get issuer unique identifier. |
|
405 | + * |
|
406 | + * @throws \LogicException If not set |
|
407 | + * |
|
408 | + * @return UniqueIdentifier |
|
409 | + */ |
|
410 | + public function issuerUniqueID(): UniqueIdentifier |
|
411 | + { |
|
412 | + if (!$this->hasIssuerUniqueID()) { |
|
413 | + throw new \LogicException('issuerUniqueID not set.'); |
|
414 | + } |
|
415 | + return $this->_issuerUniqueID; |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Get extensions. |
|
420 | + * |
|
421 | + * @return Extensions |
|
422 | + */ |
|
423 | + public function extensions(): Extensions |
|
424 | + { |
|
425 | + return $this->_extensions; |
|
426 | + } |
|
427 | + |
|
428 | + /** |
|
429 | + * Get ASN.1 structure. |
|
430 | + * |
|
431 | + * @return Sequence |
|
432 | + */ |
|
433 | + public function toASN1(): Sequence |
|
434 | + { |
|
435 | + $elements = [new Integer($this->_version), $this->_holder->toASN1(), |
|
436 | + $this->_issuer->toASN1(), $this->signature()->toASN1(), |
|
437 | + new Integer($this->serialNumber()), |
|
438 | + $this->_attrCertValidityPeriod->toASN1(), |
|
439 | + $this->_attributes->toASN1(), ]; |
|
440 | + if (isset($this->_issuerUniqueID)) { |
|
441 | + $elements[] = $this->_issuerUniqueID->toASN1(); |
|
442 | + } |
|
443 | + if (count($this->_extensions)) { |
|
444 | + $elements[] = $this->_extensions->toASN1(); |
|
445 | + } |
|
446 | + return new Sequence(...$elements); |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Create signed attribute certificate. |
|
451 | + * |
|
452 | + * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
|
453 | + * @param PrivateKeyInfo $privkey_info Private key |
|
454 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
455 | + * |
|
456 | + * @return AttributeCertificate |
|
457 | + */ |
|
458 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
459 | + PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): AttributeCertificate |
|
460 | + { |
|
461 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
462 | + $aci = clone $this; |
|
463 | + if (!isset($aci->_serialNumber)) { |
|
464 | + $aci->_serialNumber = '0'; |
|
465 | + } |
|
466 | + $aci->_signature = $algo; |
|
467 | + $data = $aci->toASN1()->toDER(); |
|
468 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
469 | + return new AttributeCertificate($aci, $algo, $signature); |
|
470 | + } |
|
471 | 471 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate; |
6 | 6 |
@@ -21,208 +21,208 @@ |
||
21 | 21 | */ |
22 | 22 | class AttributeCertificate |
23 | 23 | { |
24 | - /** |
|
25 | - * Attribute certificate info. |
|
26 | - * |
|
27 | - * @var AttributeCertificateInfo |
|
28 | - */ |
|
29 | - protected $_acinfo; |
|
30 | - |
|
31 | - /** |
|
32 | - * Signature algorithm identifier. |
|
33 | - * |
|
34 | - * @var SignatureAlgorithmIdentifier |
|
35 | - */ |
|
36 | - protected $_signatureAlgorithm; |
|
37 | - |
|
38 | - /** |
|
39 | - * Signature value. |
|
40 | - * |
|
41 | - * @var Signature |
|
42 | - */ |
|
43 | - protected $_signatureValue; |
|
44 | - |
|
45 | - /** |
|
46 | - * Constructor. |
|
47 | - * |
|
48 | - * @param AttributeCertificateInfo $acinfo |
|
49 | - * @param SignatureAlgorithmIdentifier $algo |
|
50 | - * @param Signature $signature |
|
51 | - */ |
|
52 | - public function __construct(AttributeCertificateInfo $acinfo, |
|
53 | - SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
54 | - { |
|
55 | - $this->_acinfo = $acinfo; |
|
56 | - $this->_signatureAlgorithm = $algo; |
|
57 | - $this->_signatureValue = $signature; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Get attribute certificate as a PEM formatted string. |
|
62 | - * |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function __toString(): string |
|
66 | - { |
|
67 | - return $this->toPEM()->string(); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Initialize from ASN.1. |
|
72 | - * |
|
73 | - * @param Sequence $seq |
|
74 | - * |
|
75 | - * @return self |
|
76 | - */ |
|
77 | - public static function fromASN1(Sequence $seq): self |
|
78 | - { |
|
79 | - $acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence()); |
|
80 | - $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
81 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
82 | - throw new \UnexpectedValueException( |
|
83 | - 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
84 | - } |
|
85 | - $signature = Signature::fromSignatureData( |
|
86 | - $seq->at(2)->asBitString()->string(), $algo); |
|
87 | - return new self($acinfo, $algo, $signature); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Initialize from DER data. |
|
92 | - * |
|
93 | - * @param string $data |
|
94 | - * |
|
95 | - * @return self |
|
96 | - */ |
|
97 | - public static function fromDER(string $data): self |
|
98 | - { |
|
99 | - return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Initialize from PEM. |
|
104 | - * |
|
105 | - * @param PEM $pem |
|
106 | - * |
|
107 | - * @throws \UnexpectedValueException |
|
108 | - * |
|
109 | - * @return self |
|
110 | - */ |
|
111 | - public static function fromPEM(PEM $pem): self |
|
112 | - { |
|
113 | - if (PEM::TYPE_ATTRIBUTE_CERTIFICATE !== $pem->type()) { |
|
114 | - throw new \UnexpectedValueException('Invalid PEM type.'); |
|
115 | - } |
|
116 | - return self::fromDER($pem->data()); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Get attribute certificate info. |
|
121 | - * |
|
122 | - * @return AttributeCertificateInfo |
|
123 | - */ |
|
124 | - public function acinfo(): AttributeCertificateInfo |
|
125 | - { |
|
126 | - return $this->_acinfo; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Get signature algorithm identifier. |
|
131 | - * |
|
132 | - * @return SignatureAlgorithmIdentifier |
|
133 | - */ |
|
134 | - public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
135 | - { |
|
136 | - return $this->_signatureAlgorithm; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Get signature value. |
|
141 | - * |
|
142 | - * @return Signature |
|
143 | - */ |
|
144 | - public function signatureValue(): Signature |
|
145 | - { |
|
146 | - return $this->_signatureValue; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Get ASN.1 structure. |
|
151 | - * |
|
152 | - * @return Sequence |
|
153 | - */ |
|
154 | - public function toASN1(): Sequence |
|
155 | - { |
|
156 | - return new Sequence($this->_acinfo->toASN1(), |
|
157 | - $this->_signatureAlgorithm->toASN1(), |
|
158 | - $this->_signatureValue->bitString()); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Get attribute certificate as a DER. |
|
163 | - * |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - public function toDER(): string |
|
167 | - { |
|
168 | - return $this->toASN1()->toDER(); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get attribute certificate as a PEM. |
|
173 | - * |
|
174 | - * @return PEM |
|
175 | - */ |
|
176 | - public function toPEM(): PEM |
|
177 | - { |
|
178 | - return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER()); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Check whether attribute certificate is issued to the subject identified |
|
183 | - * by given public key certificate. |
|
184 | - * |
|
185 | - * @param Certificate $cert Certificate |
|
186 | - * |
|
187 | - * @return bool |
|
188 | - */ |
|
189 | - public function isHeldBy(Certificate $cert): bool |
|
190 | - { |
|
191 | - if (!$this->_acinfo->holder()->identifiesPKC($cert)) { |
|
192 | - return false; |
|
193 | - } |
|
194 | - return true; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Check whether attribute certificate is issued by given public key |
|
199 | - * certificate. |
|
200 | - * |
|
201 | - * @param Certificate $cert Certificate |
|
202 | - * |
|
203 | - * @return bool |
|
204 | - */ |
|
205 | - public function isIssuedBy(Certificate $cert): bool |
|
206 | - { |
|
207 | - if (!$this->_acinfo->issuer()->identifiesPKC($cert)) { |
|
208 | - return false; |
|
209 | - } |
|
210 | - return true; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Verify signature. |
|
215 | - * |
|
216 | - * @param PublicKeyInfo $pubkey_info Signer's public key |
|
217 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
218 | - * |
|
219 | - * @return bool |
|
220 | - */ |
|
221 | - public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
222 | - { |
|
223 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
224 | - $data = $this->_acinfo->toASN1()->toDER(); |
|
225 | - return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
226 | - $this->_signatureAlgorithm); |
|
227 | - } |
|
24 | + /** |
|
25 | + * Attribute certificate info. |
|
26 | + * |
|
27 | + * @var AttributeCertificateInfo |
|
28 | + */ |
|
29 | + protected $_acinfo; |
|
30 | + |
|
31 | + /** |
|
32 | + * Signature algorithm identifier. |
|
33 | + * |
|
34 | + * @var SignatureAlgorithmIdentifier |
|
35 | + */ |
|
36 | + protected $_signatureAlgorithm; |
|
37 | + |
|
38 | + /** |
|
39 | + * Signature value. |
|
40 | + * |
|
41 | + * @var Signature |
|
42 | + */ |
|
43 | + protected $_signatureValue; |
|
44 | + |
|
45 | + /** |
|
46 | + * Constructor. |
|
47 | + * |
|
48 | + * @param AttributeCertificateInfo $acinfo |
|
49 | + * @param SignatureAlgorithmIdentifier $algo |
|
50 | + * @param Signature $signature |
|
51 | + */ |
|
52 | + public function __construct(AttributeCertificateInfo $acinfo, |
|
53 | + SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
54 | + { |
|
55 | + $this->_acinfo = $acinfo; |
|
56 | + $this->_signatureAlgorithm = $algo; |
|
57 | + $this->_signatureValue = $signature; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Get attribute certificate as a PEM formatted string. |
|
62 | + * |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function __toString(): string |
|
66 | + { |
|
67 | + return $this->toPEM()->string(); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Initialize from ASN.1. |
|
72 | + * |
|
73 | + * @param Sequence $seq |
|
74 | + * |
|
75 | + * @return self |
|
76 | + */ |
|
77 | + public static function fromASN1(Sequence $seq): self |
|
78 | + { |
|
79 | + $acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence()); |
|
80 | + $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
81 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
82 | + throw new \UnexpectedValueException( |
|
83 | + 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
84 | + } |
|
85 | + $signature = Signature::fromSignatureData( |
|
86 | + $seq->at(2)->asBitString()->string(), $algo); |
|
87 | + return new self($acinfo, $algo, $signature); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Initialize from DER data. |
|
92 | + * |
|
93 | + * @param string $data |
|
94 | + * |
|
95 | + * @return self |
|
96 | + */ |
|
97 | + public static function fromDER(string $data): self |
|
98 | + { |
|
99 | + return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Initialize from PEM. |
|
104 | + * |
|
105 | + * @param PEM $pem |
|
106 | + * |
|
107 | + * @throws \UnexpectedValueException |
|
108 | + * |
|
109 | + * @return self |
|
110 | + */ |
|
111 | + public static function fromPEM(PEM $pem): self |
|
112 | + { |
|
113 | + if (PEM::TYPE_ATTRIBUTE_CERTIFICATE !== $pem->type()) { |
|
114 | + throw new \UnexpectedValueException('Invalid PEM type.'); |
|
115 | + } |
|
116 | + return self::fromDER($pem->data()); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Get attribute certificate info. |
|
121 | + * |
|
122 | + * @return AttributeCertificateInfo |
|
123 | + */ |
|
124 | + public function acinfo(): AttributeCertificateInfo |
|
125 | + { |
|
126 | + return $this->_acinfo; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Get signature algorithm identifier. |
|
131 | + * |
|
132 | + * @return SignatureAlgorithmIdentifier |
|
133 | + */ |
|
134 | + public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
135 | + { |
|
136 | + return $this->_signatureAlgorithm; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Get signature value. |
|
141 | + * |
|
142 | + * @return Signature |
|
143 | + */ |
|
144 | + public function signatureValue(): Signature |
|
145 | + { |
|
146 | + return $this->_signatureValue; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Get ASN.1 structure. |
|
151 | + * |
|
152 | + * @return Sequence |
|
153 | + */ |
|
154 | + public function toASN1(): Sequence |
|
155 | + { |
|
156 | + return new Sequence($this->_acinfo->toASN1(), |
|
157 | + $this->_signatureAlgorithm->toASN1(), |
|
158 | + $this->_signatureValue->bitString()); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Get attribute certificate as a DER. |
|
163 | + * |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + public function toDER(): string |
|
167 | + { |
|
168 | + return $this->toASN1()->toDER(); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get attribute certificate as a PEM. |
|
173 | + * |
|
174 | + * @return PEM |
|
175 | + */ |
|
176 | + public function toPEM(): PEM |
|
177 | + { |
|
178 | + return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER()); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Check whether attribute certificate is issued to the subject identified |
|
183 | + * by given public key certificate. |
|
184 | + * |
|
185 | + * @param Certificate $cert Certificate |
|
186 | + * |
|
187 | + * @return bool |
|
188 | + */ |
|
189 | + public function isHeldBy(Certificate $cert): bool |
|
190 | + { |
|
191 | + if (!$this->_acinfo->holder()->identifiesPKC($cert)) { |
|
192 | + return false; |
|
193 | + } |
|
194 | + return true; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Check whether attribute certificate is issued by given public key |
|
199 | + * certificate. |
|
200 | + * |
|
201 | + * @param Certificate $cert Certificate |
|
202 | + * |
|
203 | + * @return bool |
|
204 | + */ |
|
205 | + public function isIssuedBy(Certificate $cert): bool |
|
206 | + { |
|
207 | + if (!$this->_acinfo->issuer()->identifiesPKC($cert)) { |
|
208 | + return false; |
|
209 | + } |
|
210 | + return true; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Verify signature. |
|
215 | + * |
|
216 | + * @param PublicKeyInfo $pubkey_info Signer's public key |
|
217 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
218 | + * |
|
219 | + * @return bool |
|
220 | + */ |
|
221 | + public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
222 | + { |
|
223 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
224 | + $data = $this->_acinfo->toASN1()->toDER(); |
|
225 | + return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
226 | + $this->_signatureAlgorithm); |
|
227 | + } |
|
228 | 228 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate; |
6 | 6 |
@@ -18,72 +18,72 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class AttCertIssuer |
20 | 20 | { |
21 | - /** |
|
22 | - * Generate ASN.1 element. |
|
23 | - * |
|
24 | - * @return Element |
|
25 | - */ |
|
26 | - abstract public function toASN1(): Element; |
|
21 | + /** |
|
22 | + * Generate ASN.1 element. |
|
23 | + * |
|
24 | + * @return Element |
|
25 | + */ |
|
26 | + abstract public function toASN1(): Element; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Check whether AttCertIssuer identifies given certificate. |
|
30 | - * |
|
31 | - * @param Certificate $cert |
|
32 | - * |
|
33 | - * @return bool |
|
34 | - */ |
|
35 | - abstract public function identifiesPKC(Certificate $cert): bool; |
|
28 | + /** |
|
29 | + * Check whether AttCertIssuer identifies given certificate. |
|
30 | + * |
|
31 | + * @param Certificate $cert |
|
32 | + * |
|
33 | + * @return bool |
|
34 | + */ |
|
35 | + abstract public function identifiesPKC(Certificate $cert): bool; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Initialize from distinguished name. |
|
39 | - * |
|
40 | - * This conforms to RFC 5755 which states that only v2Form must be used, |
|
41 | - * and issuerName must contain exactly one GeneralName of DirectoryName |
|
42 | - * type. |
|
43 | - * |
|
44 | - * @see https://tools.ietf.org/html/rfc5755#section-4.2.3 |
|
45 | - * |
|
46 | - * @param Name $name |
|
47 | - * |
|
48 | - * @return self |
|
49 | - */ |
|
50 | - public static function fromName(Name $name): self |
|
51 | - { |
|
52 | - return new V2Form(new GeneralNames(new DirectoryName($name))); |
|
53 | - } |
|
37 | + /** |
|
38 | + * Initialize from distinguished name. |
|
39 | + * |
|
40 | + * This conforms to RFC 5755 which states that only v2Form must be used, |
|
41 | + * and issuerName must contain exactly one GeneralName of DirectoryName |
|
42 | + * type. |
|
43 | + * |
|
44 | + * @see https://tools.ietf.org/html/rfc5755#section-4.2.3 |
|
45 | + * |
|
46 | + * @param Name $name |
|
47 | + * |
|
48 | + * @return self |
|
49 | + */ |
|
50 | + public static function fromName(Name $name): self |
|
51 | + { |
|
52 | + return new V2Form(new GeneralNames(new DirectoryName($name))); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Initialize from an issuer's public key certificate. |
|
57 | - * |
|
58 | - * @param Certificate $cert |
|
59 | - * |
|
60 | - * @return self |
|
61 | - */ |
|
62 | - public static function fromPKC(Certificate $cert): self |
|
63 | - { |
|
64 | - return self::fromName($cert->tbsCertificate()->subject()); |
|
65 | - } |
|
55 | + /** |
|
56 | + * Initialize from an issuer's public key certificate. |
|
57 | + * |
|
58 | + * @param Certificate $cert |
|
59 | + * |
|
60 | + * @return self |
|
61 | + */ |
|
62 | + public static function fromPKC(Certificate $cert): self |
|
63 | + { |
|
64 | + return self::fromName($cert->tbsCertificate()->subject()); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Initialize from ASN.1. |
|
69 | - * |
|
70 | - * @param UnspecifiedType $el CHOICE |
|
71 | - * |
|
72 | - * @throws \UnexpectedValueException |
|
73 | - * |
|
74 | - * @return self |
|
75 | - */ |
|
76 | - public static function fromASN1(UnspecifiedType $el): self |
|
77 | - { |
|
78 | - if (!$el->isTagged()) { |
|
79 | - throw new \UnexpectedValueException('v1Form issuer not supported.'); |
|
80 | - } |
|
81 | - $tagged = $el->asTagged(); |
|
82 | - switch ($tagged->tag()) { |
|
83 | - case 0: |
|
84 | - return V2Form::fromV2ASN1( |
|
85 | - $tagged->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
86 | - } |
|
87 | - throw new \UnexpectedValueException('Unsupported issuer type.'); |
|
88 | - } |
|
67 | + /** |
|
68 | + * Initialize from ASN.1. |
|
69 | + * |
|
70 | + * @param UnspecifiedType $el CHOICE |
|
71 | + * |
|
72 | + * @throws \UnexpectedValueException |
|
73 | + * |
|
74 | + * @return self |
|
75 | + */ |
|
76 | + public static function fromASN1(UnspecifiedType $el): self |
|
77 | + { |
|
78 | + if (!$el->isTagged()) { |
|
79 | + throw new \UnexpectedValueException('v1Form issuer not supported.'); |
|
80 | + } |
|
81 | + $tagged = $el->asTagged(); |
|
82 | + switch ($tagged->tag()) { |
|
83 | + case 0: |
|
84 | + return V2Form::fromV2ASN1( |
|
85 | + $tagged->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
86 | + } |
|
87 | + throw new \UnexpectedValueException('Unsupported issuer type.'); |
|
88 | + } |
|
89 | 89 | } |
@@ -80,9 +80,9 @@ |
||
80 | 80 | } |
81 | 81 | $tagged = $el->asTagged(); |
82 | 82 | switch ($tagged->tag()) { |
83 | - case 0: |
|
84 | - return V2Form::fromV2ASN1( |
|
85 | - $tagged->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
83 | + case 0: |
|
84 | + return V2Form::fromV2ASN1( |
|
85 | + $tagged->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
86 | 86 | } |
87 | 87 | throw new \UnexpectedValueException('Unsupported issuer type.'); |
88 | 88 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate; |
6 | 6 |
@@ -13,17 +13,17 @@ |
||
13 | 13 | */ |
14 | 14 | class AccessIdentityAttributeValue extends SvceAuthInfo |
15 | 15 | { |
16 | - const OID = '1.3.6.1.5.5.7.10.2'; |
|
16 | + const OID = '1.3.6.1.5.5.7.10.2'; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Constructor. |
|
20 | - * |
|
21 | - * @param GeneralName $service |
|
22 | - * @param GeneralName $ident |
|
23 | - */ |
|
24 | - public function __construct(GeneralName $service, GeneralName $ident) |
|
25 | - { |
|
26 | - parent::__construct($service, $ident, null); |
|
27 | - $this->_oid = self::OID; |
|
28 | - } |
|
18 | + /** |
|
19 | + * Constructor. |
|
20 | + * |
|
21 | + * @param GeneralName $service |
|
22 | + * @param GeneralName $ident |
|
23 | + */ |
|
24 | + public function __construct(GeneralName $service, GeneralName $ident) |
|
25 | + { |
|
26 | + parent::__construct($service, $ident, null); |
|
27 | + $this->_oid = self::OID; |
|
28 | + } |
|
29 | 29 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Attribute; |
6 | 6 |
@@ -21,145 +21,145 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class SvceAuthInfo extends AttributeValue |
23 | 23 | { |
24 | - /** |
|
25 | - * Service. |
|
26 | - * |
|
27 | - * @var GeneralName |
|
28 | - */ |
|
29 | - protected $_service; |
|
30 | - |
|
31 | - /** |
|
32 | - * Ident. |
|
33 | - * |
|
34 | - * @var GeneralName |
|
35 | - */ |
|
36 | - protected $_ident; |
|
37 | - |
|
38 | - /** |
|
39 | - * Auth info. |
|
40 | - * |
|
41 | - * @var null|string |
|
42 | - */ |
|
43 | - protected $_authInfo; |
|
44 | - |
|
45 | - /** |
|
46 | - * Constructor. |
|
47 | - * |
|
48 | - * @param GeneralName $service |
|
49 | - * @param GeneralName $ident |
|
50 | - * @param null|string $auth_info |
|
51 | - */ |
|
52 | - public function __construct(GeneralName $service, GeneralName $ident, |
|
53 | - ?string $auth_info = null) |
|
54 | - { |
|
55 | - $this->_service = $service; |
|
56 | - $this->_ident = $ident; |
|
57 | - $this->_authInfo = $auth_info; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - * |
|
63 | - * @return self |
|
64 | - */ |
|
65 | - public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
66 | - { |
|
67 | - $seq = $el->asSequence(); |
|
68 | - $service = GeneralName::fromASN1($seq->at(0)->asTagged()); |
|
69 | - $ident = GeneralName::fromASN1($seq->at(1)->asTagged()); |
|
70 | - $auth_info = null; |
|
71 | - if ($seq->has(2, Element::TYPE_OCTET_STRING)) { |
|
72 | - $auth_info = $seq->at(2)->asString()->string(); |
|
73 | - } |
|
74 | - return new static($service, $ident, $auth_info); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Get service name. |
|
79 | - * |
|
80 | - * @return GeneralName |
|
81 | - */ |
|
82 | - public function service(): GeneralName |
|
83 | - { |
|
84 | - return $this->_service; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Get ident. |
|
89 | - * |
|
90 | - * @return GeneralName |
|
91 | - */ |
|
92 | - public function ident(): GeneralName |
|
93 | - { |
|
94 | - return $this->_ident; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Check whether authentication info is present. |
|
99 | - * |
|
100 | - * @return bool |
|
101 | - */ |
|
102 | - public function hasAuthInfo(): bool |
|
103 | - { |
|
104 | - return isset($this->_authInfo); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Get authentication info. |
|
109 | - * |
|
110 | - * @throws \LogicException If not set |
|
111 | - * |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public function authInfo(): string |
|
115 | - { |
|
116 | - if (!$this->hasAuthInfo()) { |
|
117 | - throw new \LogicException('authInfo not set.'); |
|
118 | - } |
|
119 | - return $this->_authInfo; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * {@inheritdoc} |
|
124 | - */ |
|
125 | - public function toASN1(): Element |
|
126 | - { |
|
127 | - $elements = [$this->_service->toASN1(), $this->_ident->toASN1()]; |
|
128 | - if (isset($this->_authInfo)) { |
|
129 | - $elements[] = new OctetString($this->_authInfo); |
|
130 | - } |
|
131 | - return new Sequence(...$elements); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * {@inheritdoc} |
|
136 | - */ |
|
137 | - public function stringValue(): string |
|
138 | - { |
|
139 | - return '#' . bin2hex($this->toASN1()->toDER()); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * {@inheritdoc} |
|
144 | - */ |
|
145 | - public function equalityMatchingRule(): MatchingRule |
|
146 | - { |
|
147 | - return new BinaryMatch(); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * {@inheritdoc} |
|
152 | - */ |
|
153 | - public function rfc2253String(): string |
|
154 | - { |
|
155 | - return $this->stringValue(); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * {@inheritdoc} |
|
160 | - */ |
|
161 | - protected function _transcodedString(): string |
|
162 | - { |
|
163 | - return $this->stringValue(); |
|
164 | - } |
|
24 | + /** |
|
25 | + * Service. |
|
26 | + * |
|
27 | + * @var GeneralName |
|
28 | + */ |
|
29 | + protected $_service; |
|
30 | + |
|
31 | + /** |
|
32 | + * Ident. |
|
33 | + * |
|
34 | + * @var GeneralName |
|
35 | + */ |
|
36 | + protected $_ident; |
|
37 | + |
|
38 | + /** |
|
39 | + * Auth info. |
|
40 | + * |
|
41 | + * @var null|string |
|
42 | + */ |
|
43 | + protected $_authInfo; |
|
44 | + |
|
45 | + /** |
|
46 | + * Constructor. |
|
47 | + * |
|
48 | + * @param GeneralName $service |
|
49 | + * @param GeneralName $ident |
|
50 | + * @param null|string $auth_info |
|
51 | + */ |
|
52 | + public function __construct(GeneralName $service, GeneralName $ident, |
|
53 | + ?string $auth_info = null) |
|
54 | + { |
|
55 | + $this->_service = $service; |
|
56 | + $this->_ident = $ident; |
|
57 | + $this->_authInfo = $auth_info; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + * |
|
63 | + * @return self |
|
64 | + */ |
|
65 | + public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
66 | + { |
|
67 | + $seq = $el->asSequence(); |
|
68 | + $service = GeneralName::fromASN1($seq->at(0)->asTagged()); |
|
69 | + $ident = GeneralName::fromASN1($seq->at(1)->asTagged()); |
|
70 | + $auth_info = null; |
|
71 | + if ($seq->has(2, Element::TYPE_OCTET_STRING)) { |
|
72 | + $auth_info = $seq->at(2)->asString()->string(); |
|
73 | + } |
|
74 | + return new static($service, $ident, $auth_info); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Get service name. |
|
79 | + * |
|
80 | + * @return GeneralName |
|
81 | + */ |
|
82 | + public function service(): GeneralName |
|
83 | + { |
|
84 | + return $this->_service; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Get ident. |
|
89 | + * |
|
90 | + * @return GeneralName |
|
91 | + */ |
|
92 | + public function ident(): GeneralName |
|
93 | + { |
|
94 | + return $this->_ident; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Check whether authentication info is present. |
|
99 | + * |
|
100 | + * @return bool |
|
101 | + */ |
|
102 | + public function hasAuthInfo(): bool |
|
103 | + { |
|
104 | + return isset($this->_authInfo); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Get authentication info. |
|
109 | + * |
|
110 | + * @throws \LogicException If not set |
|
111 | + * |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public function authInfo(): string |
|
115 | + { |
|
116 | + if (!$this->hasAuthInfo()) { |
|
117 | + throw new \LogicException('authInfo not set.'); |
|
118 | + } |
|
119 | + return $this->_authInfo; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * {@inheritdoc} |
|
124 | + */ |
|
125 | + public function toASN1(): Element |
|
126 | + { |
|
127 | + $elements = [$this->_service->toASN1(), $this->_ident->toASN1()]; |
|
128 | + if (isset($this->_authInfo)) { |
|
129 | + $elements[] = new OctetString($this->_authInfo); |
|
130 | + } |
|
131 | + return new Sequence(...$elements); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * {@inheritdoc} |
|
136 | + */ |
|
137 | + public function stringValue(): string |
|
138 | + { |
|
139 | + return '#' . bin2hex($this->toASN1()->toDER()); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * {@inheritdoc} |
|
144 | + */ |
|
145 | + public function equalityMatchingRule(): MatchingRule |
|
146 | + { |
|
147 | + return new BinaryMatch(); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * {@inheritdoc} |
|
152 | + */ |
|
153 | + public function rfc2253String(): string |
|
154 | + { |
|
155 | + return $this->stringValue(); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * {@inheritdoc} |
|
160 | + */ |
|
161 | + protected function _transcodedString(): string |
|
162 | + { |
|
163 | + return $this->stringValue(); |
|
164 | + } |
|
165 | 165 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Attribute; |
6 | 6 |
@@ -24,146 +24,146 @@ |
||
24 | 24 | */ |
25 | 25 | class RoleAttributeValue extends AttributeValue |
26 | 26 | { |
27 | - /** |
|
28 | - * Issuing authority. |
|
29 | - * |
|
30 | - * @var null|GeneralNames |
|
31 | - */ |
|
32 | - protected $_roleAuthority; |
|
33 | - |
|
34 | - /** |
|
35 | - * Role name. |
|
36 | - * |
|
37 | - * @var GeneralName |
|
38 | - */ |
|
39 | - protected $_roleName; |
|
40 | - |
|
41 | - /** |
|
42 | - * Constructor. |
|
43 | - * |
|
44 | - * @param GeneralName $name Role name |
|
45 | - * @param null|GeneralNames $authority Issuing authority |
|
46 | - */ |
|
47 | - public function __construct(GeneralName $name, |
|
48 | - ?GeneralNames $authority = null) |
|
49 | - { |
|
50 | - $this->_roleAuthority = $authority; |
|
51 | - $this->_roleName = $name; |
|
52 | - $this->_oid = AttributeType::OID_ROLE; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Initialize from a role string. |
|
57 | - * |
|
58 | - * @param string $role_name Role name in URI format |
|
59 | - * @param null|GeneralNames $authority Issuing authority |
|
60 | - * |
|
61 | - * @return self |
|
62 | - */ |
|
63 | - public static function fromString(string $role_name, |
|
64 | - ?GeneralNames $authority = null): self |
|
65 | - { |
|
66 | - return new self(new UniformResourceIdentifier($role_name), $authority); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * {@inheritdoc} |
|
71 | - * |
|
72 | - * @return self |
|
73 | - */ |
|
74 | - public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
75 | - { |
|
76 | - $seq = $el->asSequence(); |
|
77 | - $authority = null; |
|
78 | - if ($seq->hasTagged(0)) { |
|
79 | - $authority = GeneralNames::fromASN1( |
|
80 | - $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
81 | - ->asSequence()); |
|
82 | - } |
|
83 | - $name = GeneralName::fromASN1($seq->getTagged(1) |
|
84 | - ->asExplicit()->asTagged()); |
|
85 | - return new self($name, $authority); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Check whether issuing authority is present. |
|
90 | - * |
|
91 | - * @return bool |
|
92 | - */ |
|
93 | - public function hasRoleAuthority(): bool |
|
94 | - { |
|
95 | - return isset($this->_roleAuthority); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Get issuing authority. |
|
100 | - * |
|
101 | - * @throws \LogicException If not set |
|
102 | - * |
|
103 | - * @return GeneralNames |
|
104 | - */ |
|
105 | - public function roleAuthority(): GeneralNames |
|
106 | - { |
|
107 | - if (!$this->hasRoleAuthority()) { |
|
108 | - throw new \LogicException('roleAuthority not set.'); |
|
109 | - } |
|
110 | - return $this->_roleAuthority; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get role name. |
|
115 | - * |
|
116 | - * @return GeneralName |
|
117 | - */ |
|
118 | - public function roleName(): GeneralName |
|
119 | - { |
|
120 | - return $this->_roleName; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * {@inheritdoc} |
|
125 | - */ |
|
126 | - public function toASN1(): Element |
|
127 | - { |
|
128 | - $elements = []; |
|
129 | - if (isset($this->_roleAuthority)) { |
|
130 | - $elements[] = new ImplicitlyTaggedType( |
|
131 | - 0, $this->_roleAuthority->toASN1()); |
|
132 | - } |
|
133 | - $elements[] = new ExplicitlyTaggedType( |
|
134 | - 1, $this->_roleName->toASN1()); |
|
135 | - return new Sequence(...$elements); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * {@inheritdoc} |
|
140 | - */ |
|
141 | - public function stringValue(): string |
|
142 | - { |
|
143 | - return '#' . bin2hex($this->toASN1()->toDER()); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * {@inheritdoc} |
|
148 | - */ |
|
149 | - public function equalityMatchingRule(): MatchingRule |
|
150 | - { |
|
151 | - return new BinaryMatch(); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * {@inheritdoc} |
|
156 | - */ |
|
157 | - public function rfc2253String(): string |
|
158 | - { |
|
159 | - return $this->stringValue(); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * {@inheritdoc} |
|
164 | - */ |
|
165 | - protected function _transcodedString(): string |
|
166 | - { |
|
167 | - return $this->stringValue(); |
|
168 | - } |
|
27 | + /** |
|
28 | + * Issuing authority. |
|
29 | + * |
|
30 | + * @var null|GeneralNames |
|
31 | + */ |
|
32 | + protected $_roleAuthority; |
|
33 | + |
|
34 | + /** |
|
35 | + * Role name. |
|
36 | + * |
|
37 | + * @var GeneralName |
|
38 | + */ |
|
39 | + protected $_roleName; |
|
40 | + |
|
41 | + /** |
|
42 | + * Constructor. |
|
43 | + * |
|
44 | + * @param GeneralName $name Role name |
|
45 | + * @param null|GeneralNames $authority Issuing authority |
|
46 | + */ |
|
47 | + public function __construct(GeneralName $name, |
|
48 | + ?GeneralNames $authority = null) |
|
49 | + { |
|
50 | + $this->_roleAuthority = $authority; |
|
51 | + $this->_roleName = $name; |
|
52 | + $this->_oid = AttributeType::OID_ROLE; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Initialize from a role string. |
|
57 | + * |
|
58 | + * @param string $role_name Role name in URI format |
|
59 | + * @param null|GeneralNames $authority Issuing authority |
|
60 | + * |
|
61 | + * @return self |
|
62 | + */ |
|
63 | + public static function fromString(string $role_name, |
|
64 | + ?GeneralNames $authority = null): self |
|
65 | + { |
|
66 | + return new self(new UniformResourceIdentifier($role_name), $authority); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * {@inheritdoc} |
|
71 | + * |
|
72 | + * @return self |
|
73 | + */ |
|
74 | + public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
75 | + { |
|
76 | + $seq = $el->asSequence(); |
|
77 | + $authority = null; |
|
78 | + if ($seq->hasTagged(0)) { |
|
79 | + $authority = GeneralNames::fromASN1( |
|
80 | + $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
81 | + ->asSequence()); |
|
82 | + } |
|
83 | + $name = GeneralName::fromASN1($seq->getTagged(1) |
|
84 | + ->asExplicit()->asTagged()); |
|
85 | + return new self($name, $authority); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Check whether issuing authority is present. |
|
90 | + * |
|
91 | + * @return bool |
|
92 | + */ |
|
93 | + public function hasRoleAuthority(): bool |
|
94 | + { |
|
95 | + return isset($this->_roleAuthority); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Get issuing authority. |
|
100 | + * |
|
101 | + * @throws \LogicException If not set |
|
102 | + * |
|
103 | + * @return GeneralNames |
|
104 | + */ |
|
105 | + public function roleAuthority(): GeneralNames |
|
106 | + { |
|
107 | + if (!$this->hasRoleAuthority()) { |
|
108 | + throw new \LogicException('roleAuthority not set.'); |
|
109 | + } |
|
110 | + return $this->_roleAuthority; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get role name. |
|
115 | + * |
|
116 | + * @return GeneralName |
|
117 | + */ |
|
118 | + public function roleName(): GeneralName |
|
119 | + { |
|
120 | + return $this->_roleName; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * {@inheritdoc} |
|
125 | + */ |
|
126 | + public function toASN1(): Element |
|
127 | + { |
|
128 | + $elements = []; |
|
129 | + if (isset($this->_roleAuthority)) { |
|
130 | + $elements[] = new ImplicitlyTaggedType( |
|
131 | + 0, $this->_roleAuthority->toASN1()); |
|
132 | + } |
|
133 | + $elements[] = new ExplicitlyTaggedType( |
|
134 | + 1, $this->_roleName->toASN1()); |
|
135 | + return new Sequence(...$elements); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * {@inheritdoc} |
|
140 | + */ |
|
141 | + public function stringValue(): string |
|
142 | + { |
|
143 | + return '#' . bin2hex($this->toASN1()->toDER()); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * {@inheritdoc} |
|
148 | + */ |
|
149 | + public function equalityMatchingRule(): MatchingRule |
|
150 | + { |
|
151 | + return new BinaryMatch(); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * {@inheritdoc} |
|
156 | + */ |
|
157 | + public function rfc2253String(): string |
|
158 | + { |
|
159 | + return $this->stringValue(); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * {@inheritdoc} |
|
164 | + */ |
|
165 | + protected function _transcodedString(): string |
|
166 | + { |
|
167 | + return $this->stringValue(); |
|
168 | + } |
|
169 | 169 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Attribute; |
6 | 6 |
@@ -11,16 +11,16 @@ |
||
11 | 11 | */ |
12 | 12 | class GroupAttributeValue extends IetfAttrSyntax |
13 | 13 | { |
14 | - const OID = '1.3.6.1.5.5.7.10.4'; |
|
14 | + const OID = '1.3.6.1.5.5.7.10.4'; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Constructor. |
|
18 | - * |
|
19 | - * @param IetfAttrValue ...$values |
|
20 | - */ |
|
21 | - public function __construct(IetfAttrValue ...$values) |
|
22 | - { |
|
23 | - parent::__construct(...$values); |
|
24 | - $this->_oid = self::OID; |
|
25 | - } |
|
16 | + /** |
|
17 | + * Constructor. |
|
18 | + * |
|
19 | + * @param IetfAttrValue ...$values |
|
20 | + */ |
|
21 | + public function __construct(IetfAttrValue ...$values) |
|
22 | + { |
|
23 | + parent::__construct(...$values); |
|
24 | + $this->_oid = self::OID; |
|
25 | + } |
|
26 | 26 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Attribute; |
6 | 6 |
@@ -17,166 +17,166 @@ |
||
17 | 17 | */ |
18 | 18 | class IetfAttrValue |
19 | 19 | { |
20 | - /** |
|
21 | - * Element type tag. |
|
22 | - * |
|
23 | - * @var int |
|
24 | - */ |
|
25 | - protected $_type; |
|
26 | - |
|
27 | - /** |
|
28 | - * Value. |
|
29 | - * |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected $_value; |
|
33 | - |
|
34 | - /** |
|
35 | - * Constructor. |
|
36 | - * |
|
37 | - * @param string $value |
|
38 | - * @param int $type |
|
39 | - */ |
|
40 | - public function __construct(string $value, int $type) |
|
41 | - { |
|
42 | - $this->_type = $type; |
|
43 | - $this->_value = $value; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function __toString(): string |
|
50 | - { |
|
51 | - return $this->_value; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Initialize from ASN.1. |
|
56 | - * |
|
57 | - * @param UnspecifiedType $el |
|
58 | - * |
|
59 | - * @throws \UnexpectedValueException |
|
60 | - * |
|
61 | - * @return self |
|
62 | - */ |
|
63 | - public static function fromASN1(UnspecifiedType $el): self |
|
64 | - { |
|
65 | - switch ($el->tag()) { |
|
66 | - case Element::TYPE_OCTET_STRING: |
|
67 | - case Element::TYPE_UTF8_STRING: |
|
68 | - return new self($el->asString()->string(), $el->tag()); |
|
69 | - case Element::TYPE_OBJECT_IDENTIFIER: |
|
70 | - return new self($el->asObjectIdentifier()->oid(), $el->tag()); |
|
71 | - } |
|
72 | - throw new \UnexpectedValueException( |
|
73 | - 'Type ' . Element::tagToName($el->tag()) . ' not supported.'); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Initialize from octet string. |
|
78 | - * |
|
79 | - * @param string $octets |
|
80 | - * |
|
81 | - * @return self |
|
82 | - */ |
|
83 | - public static function fromOctets(string $octets): self |
|
84 | - { |
|
85 | - return new self($octets, Element::TYPE_OCTET_STRING); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Initialize from UTF-8 string. |
|
90 | - * |
|
91 | - * @param string $str |
|
92 | - * |
|
93 | - * @return self |
|
94 | - */ |
|
95 | - public static function fromString(string $str): self |
|
96 | - { |
|
97 | - return new self($str, Element::TYPE_UTF8_STRING); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Initialize from OID. |
|
102 | - * |
|
103 | - * @param string $oid |
|
104 | - * |
|
105 | - * @return self |
|
106 | - */ |
|
107 | - public static function fromOID(string $oid): self |
|
108 | - { |
|
109 | - return new self($oid, Element::TYPE_OBJECT_IDENTIFIER); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Get type tag. |
|
114 | - * |
|
115 | - * @return int |
|
116 | - */ |
|
117 | - public function type(): int |
|
118 | - { |
|
119 | - return $this->_type; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Whether value type is octets. |
|
124 | - * |
|
125 | - * @return bool |
|
126 | - */ |
|
127 | - public function isOctets(): bool |
|
128 | - { |
|
129 | - return Element::TYPE_OCTET_STRING === $this->_type; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Whether value type is OID. |
|
134 | - * |
|
135 | - * @return bool |
|
136 | - */ |
|
137 | - public function isOID(): bool |
|
138 | - { |
|
139 | - return Element::TYPE_OBJECT_IDENTIFIER === $this->_type; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Whether value type is string. |
|
144 | - * |
|
145 | - * @return bool |
|
146 | - */ |
|
147 | - public function isString(): bool |
|
148 | - { |
|
149 | - return Element::TYPE_UTF8_STRING === $this->_type; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Get value. |
|
154 | - * |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - public function value(): string |
|
158 | - { |
|
159 | - return $this->_value; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Generate ASN.1 structure. |
|
164 | - * |
|
165 | - * @throws \LogicException |
|
166 | - * |
|
167 | - * @return Element |
|
168 | - */ |
|
169 | - public function toASN1(): Element |
|
170 | - { |
|
171 | - switch ($this->_type) { |
|
172 | - case Element::TYPE_OCTET_STRING: |
|
173 | - return new OctetString($this->_value); |
|
174 | - case Element::TYPE_UTF8_STRING: |
|
175 | - return new UTF8String($this->_value); |
|
176 | - case Element::TYPE_OBJECT_IDENTIFIER: |
|
177 | - return new ObjectIdentifier($this->_value); |
|
178 | - } |
|
179 | - throw new \LogicException( |
|
180 | - 'Type ' . Element::tagToName($this->_type) . ' not supported.'); |
|
181 | - } |
|
20 | + /** |
|
21 | + * Element type tag. |
|
22 | + * |
|
23 | + * @var int |
|
24 | + */ |
|
25 | + protected $_type; |
|
26 | + |
|
27 | + /** |
|
28 | + * Value. |
|
29 | + * |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected $_value; |
|
33 | + |
|
34 | + /** |
|
35 | + * Constructor. |
|
36 | + * |
|
37 | + * @param string $value |
|
38 | + * @param int $type |
|
39 | + */ |
|
40 | + public function __construct(string $value, int $type) |
|
41 | + { |
|
42 | + $this->_type = $type; |
|
43 | + $this->_value = $value; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function __toString(): string |
|
50 | + { |
|
51 | + return $this->_value; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Initialize from ASN.1. |
|
56 | + * |
|
57 | + * @param UnspecifiedType $el |
|
58 | + * |
|
59 | + * @throws \UnexpectedValueException |
|
60 | + * |
|
61 | + * @return self |
|
62 | + */ |
|
63 | + public static function fromASN1(UnspecifiedType $el): self |
|
64 | + { |
|
65 | + switch ($el->tag()) { |
|
66 | + case Element::TYPE_OCTET_STRING: |
|
67 | + case Element::TYPE_UTF8_STRING: |
|
68 | + return new self($el->asString()->string(), $el->tag()); |
|
69 | + case Element::TYPE_OBJECT_IDENTIFIER: |
|
70 | + return new self($el->asObjectIdentifier()->oid(), $el->tag()); |
|
71 | + } |
|
72 | + throw new \UnexpectedValueException( |
|
73 | + 'Type ' . Element::tagToName($el->tag()) . ' not supported.'); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Initialize from octet string. |
|
78 | + * |
|
79 | + * @param string $octets |
|
80 | + * |
|
81 | + * @return self |
|
82 | + */ |
|
83 | + public static function fromOctets(string $octets): self |
|
84 | + { |
|
85 | + return new self($octets, Element::TYPE_OCTET_STRING); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Initialize from UTF-8 string. |
|
90 | + * |
|
91 | + * @param string $str |
|
92 | + * |
|
93 | + * @return self |
|
94 | + */ |
|
95 | + public static function fromString(string $str): self |
|
96 | + { |
|
97 | + return new self($str, Element::TYPE_UTF8_STRING); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Initialize from OID. |
|
102 | + * |
|
103 | + * @param string $oid |
|
104 | + * |
|
105 | + * @return self |
|
106 | + */ |
|
107 | + public static function fromOID(string $oid): self |
|
108 | + { |
|
109 | + return new self($oid, Element::TYPE_OBJECT_IDENTIFIER); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Get type tag. |
|
114 | + * |
|
115 | + * @return int |
|
116 | + */ |
|
117 | + public function type(): int |
|
118 | + { |
|
119 | + return $this->_type; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Whether value type is octets. |
|
124 | + * |
|
125 | + * @return bool |
|
126 | + */ |
|
127 | + public function isOctets(): bool |
|
128 | + { |
|
129 | + return Element::TYPE_OCTET_STRING === $this->_type; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Whether value type is OID. |
|
134 | + * |
|
135 | + * @return bool |
|
136 | + */ |
|
137 | + public function isOID(): bool |
|
138 | + { |
|
139 | + return Element::TYPE_OBJECT_IDENTIFIER === $this->_type; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Whether value type is string. |
|
144 | + * |
|
145 | + * @return bool |
|
146 | + */ |
|
147 | + public function isString(): bool |
|
148 | + { |
|
149 | + return Element::TYPE_UTF8_STRING === $this->_type; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Get value. |
|
154 | + * |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + public function value(): string |
|
158 | + { |
|
159 | + return $this->_value; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Generate ASN.1 structure. |
|
164 | + * |
|
165 | + * @throws \LogicException |
|
166 | + * |
|
167 | + * @return Element |
|
168 | + */ |
|
169 | + public function toASN1(): Element |
|
170 | + { |
|
171 | + switch ($this->_type) { |
|
172 | + case Element::TYPE_OCTET_STRING: |
|
173 | + return new OctetString($this->_value); |
|
174 | + case Element::TYPE_UTF8_STRING: |
|
175 | + return new UTF8String($this->_value); |
|
176 | + case Element::TYPE_OBJECT_IDENTIFIER: |
|
177 | + return new ObjectIdentifier($this->_value); |
|
178 | + } |
|
179 | + throw new \LogicException( |
|
180 | + 'Type ' . Element::tagToName($this->_type) . ' not supported.'); |
|
181 | + } |
|
182 | 182 | } |
@@ -63,11 +63,11 @@ discard block |
||
63 | 63 | public static function fromASN1(UnspecifiedType $el): self |
64 | 64 | { |
65 | 65 | switch ($el->tag()) { |
66 | - case Element::TYPE_OCTET_STRING: |
|
67 | - case Element::TYPE_UTF8_STRING: |
|
68 | - return new self($el->asString()->string(), $el->tag()); |
|
69 | - case Element::TYPE_OBJECT_IDENTIFIER: |
|
70 | - return new self($el->asObjectIdentifier()->oid(), $el->tag()); |
|
66 | + case Element::TYPE_OCTET_STRING: |
|
67 | + case Element::TYPE_UTF8_STRING: |
|
68 | + return new self($el->asString()->string(), $el->tag()); |
|
69 | + case Element::TYPE_OBJECT_IDENTIFIER: |
|
70 | + return new self($el->asObjectIdentifier()->oid(), $el->tag()); |
|
71 | 71 | } |
72 | 72 | throw new \UnexpectedValueException( |
73 | 73 | 'Type ' . Element::tagToName($el->tag()) . ' not supported.'); |
@@ -169,12 +169,12 @@ discard block |
||
169 | 169 | public function toASN1(): Element |
170 | 170 | { |
171 | 171 | switch ($this->_type) { |
172 | - case Element::TYPE_OCTET_STRING: |
|
173 | - return new OctetString($this->_value); |
|
174 | - case Element::TYPE_UTF8_STRING: |
|
175 | - return new UTF8String($this->_value); |
|
176 | - case Element::TYPE_OBJECT_IDENTIFIER: |
|
177 | - return new ObjectIdentifier($this->_value); |
|
172 | + case Element::TYPE_OCTET_STRING: |
|
173 | + return new OctetString($this->_value); |
|
174 | + case Element::TYPE_UTF8_STRING: |
|
175 | + return new UTF8String($this->_value); |
|
176 | + case Element::TYPE_OBJECT_IDENTIFIER: |
|
177 | + return new ObjectIdentifier($this->_value); |
|
178 | 178 | } |
179 | 179 | throw new \LogicException( |
180 | 180 | 'Type ' . Element::tagToName($this->_type) . ' not supported.'); |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Attribute; |
6 | 6 |
@@ -13,19 +13,19 @@ |
||
13 | 13 | */ |
14 | 14 | class AuthenticationInfoAttributeValue extends SvceAuthInfo |
15 | 15 | { |
16 | - const OID = '1.3.6.1.5.5.7.10.1'; |
|
16 | + const OID = '1.3.6.1.5.5.7.10.1'; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Constructor. |
|
20 | - * |
|
21 | - * @param GeneralName $service |
|
22 | - * @param GeneralName $ident |
|
23 | - * @param null|string $auth_info |
|
24 | - */ |
|
25 | - public function __construct(GeneralName $service, GeneralName $ident, |
|
26 | - ?string $auth_info = null) |
|
27 | - { |
|
28 | - parent::__construct($service, $ident, $auth_info); |
|
29 | - $this->_oid = self::OID; |
|
30 | - } |
|
18 | + /** |
|
19 | + * Constructor. |
|
20 | + * |
|
21 | + * @param GeneralName $service |
|
22 | + * @param GeneralName $ident |
|
23 | + * @param null|string $auth_info |
|
24 | + */ |
|
25 | + public function __construct(GeneralName $service, GeneralName $ident, |
|
26 | + ?string $auth_info = null) |
|
27 | + { |
|
28 | + parent::__construct($service, $ident, $auth_info); |
|
29 | + $this->_oid = self::OID; |
|
30 | + } |
|
31 | 31 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Attribute; |
6 | 6 |