@@ -27,610 +27,610 @@ |
||
27 | 27 | */ |
28 | 28 | class TBSCertificate |
29 | 29 | { |
30 | - // Certificate version enumerations |
|
31 | - const VERSION_1 = 0; |
|
32 | - const VERSION_2 = 1; |
|
33 | - const VERSION_3 = 2; |
|
34 | - |
|
35 | - /** |
|
36 | - * Certificate version. |
|
37 | - * |
|
38 | - * @var int|null |
|
39 | - */ |
|
40 | - protected $_version; |
|
41 | - |
|
42 | - /** |
|
43 | - * Serial number. |
|
44 | - * |
|
45 | - * @var string|null |
|
46 | - */ |
|
47 | - protected $_serialNumber; |
|
48 | - |
|
49 | - /** |
|
50 | - * Signature algorithm. |
|
51 | - * |
|
52 | - * @var SignatureAlgorithmIdentifier|null |
|
53 | - */ |
|
54 | - protected $_signature; |
|
55 | - |
|
56 | - /** |
|
57 | - * Certificate issuer. |
|
58 | - * |
|
59 | - * @var Name $_issuer |
|
60 | - */ |
|
61 | - protected $_issuer; |
|
62 | - |
|
63 | - /** |
|
64 | - * Certificate validity period. |
|
65 | - * |
|
66 | - * @var Validity $_validity |
|
67 | - */ |
|
68 | - protected $_validity; |
|
69 | - |
|
70 | - /** |
|
71 | - * Certificate subject. |
|
72 | - * |
|
73 | - * @var Name $_subject |
|
74 | - */ |
|
75 | - protected $_subject; |
|
76 | - |
|
77 | - /** |
|
78 | - * Subject public key. |
|
79 | - * |
|
80 | - * @var PublicKeyInfo $_subjectPublicKeyInfo |
|
81 | - */ |
|
82 | - protected $_subjectPublicKeyInfo; |
|
83 | - |
|
84 | - /** |
|
85 | - * Issuer unique identifier. |
|
86 | - * |
|
87 | - * @var UniqueIdentifier|null $_issuerUniqueID |
|
88 | - */ |
|
89 | - protected $_issuerUniqueID; |
|
90 | - |
|
91 | - /** |
|
92 | - * Subject unique identifier. |
|
93 | - * |
|
94 | - * @var UniqueIdentifier|null $_subjectUniqueID |
|
95 | - */ |
|
96 | - protected $_subjectUniqueID; |
|
97 | - |
|
98 | - /** |
|
99 | - * Extensions. |
|
100 | - * |
|
101 | - * @var Extensions $_extensions |
|
102 | - */ |
|
103 | - protected $_extensions; |
|
104 | - |
|
105 | - /** |
|
106 | - * Constructor. |
|
107 | - * |
|
108 | - * @param Name $subject Certificate subject |
|
109 | - * @param PublicKeyInfo $pki Subject public key |
|
110 | - * @param Name $issuer Certificate issuer |
|
111 | - * @param Validity $validity Validity period |
|
112 | - */ |
|
113 | - public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
114 | - Validity $validity) |
|
115 | - { |
|
116 | - $this->_subject = $subject; |
|
117 | - $this->_subjectPublicKeyInfo = $pki; |
|
118 | - $this->_issuer = $issuer; |
|
119 | - $this->_validity = $validity; |
|
120 | - $this->_extensions = new Extensions(); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Initialize from ASN.1. |
|
125 | - * |
|
126 | - * @param Sequence $seq |
|
127 | - * @return self |
|
128 | - */ |
|
129 | - public static function fromASN1(Sequence $seq): self |
|
130 | - { |
|
131 | - $idx = 0; |
|
132 | - if ($seq->hasTagged(0)) { |
|
133 | - $idx++; |
|
134 | - $version = $seq->getTagged(0) |
|
135 | - ->asExplicit() |
|
136 | - ->asInteger() |
|
137 | - ->intNumber(); |
|
138 | - } else { |
|
139 | - $version = self::VERSION_1; |
|
140 | - } |
|
141 | - $serial = $seq->at($idx++) |
|
142 | - ->asInteger() |
|
143 | - ->number(); |
|
144 | - $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
145 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
146 | - throw new \UnexpectedValueException( |
|
147 | - "Unsupported signature algorithm " . $algo->name() . "."); |
|
148 | - } |
|
149 | - $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
150 | - $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
151 | - $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
152 | - $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
153 | - $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
154 | - $tbs_cert->_version = $version; |
|
155 | - $tbs_cert->_serialNumber = $serial; |
|
156 | - $tbs_cert->_signature = $algo; |
|
157 | - if ($seq->hasTagged(1)) { |
|
158 | - $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
159 | - $seq->getTagged(1) |
|
160 | - ->asImplicit(Element::TYPE_BIT_STRING) |
|
161 | - ->asBitString()); |
|
162 | - } |
|
163 | - if ($seq->hasTagged(2)) { |
|
164 | - $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
165 | - $seq->getTagged(2) |
|
166 | - ->asImplicit(Element::TYPE_BIT_STRING) |
|
167 | - ->asBitString()); |
|
168 | - } |
|
169 | - if ($seq->hasTagged(3)) { |
|
170 | - $tbs_cert->_extensions = Extensions::fromASN1( |
|
171 | - $seq->getTagged(3) |
|
172 | - ->asExplicit() |
|
173 | - ->asSequence()); |
|
174 | - } |
|
175 | - return $tbs_cert; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Initialize from certification request. |
|
180 | - * |
|
181 | - * Note that signature is not verified and must be done by the caller. |
|
182 | - * |
|
183 | - * @param CertificationRequest $cr |
|
184 | - * @return self |
|
185 | - */ |
|
186 | - public static function fromCSR(CertificationRequest $cr): self |
|
187 | - { |
|
188 | - $cri = $cr->certificationRequestInfo(); |
|
189 | - $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
190 | - Validity::fromStrings(null, null)); |
|
191 | - // if CSR has Extension Request attribute |
|
192 | - if ($cri->hasAttributes()) { |
|
193 | - $attribs = $cri->attributes(); |
|
194 | - if ($attribs->hasExtensionRequest()) { |
|
195 | - $tbs_cert = $tbs_cert->withExtensions( |
|
196 | - $attribs->extensionRequest() |
|
197 | - ->extensions()); |
|
198 | - } |
|
199 | - } |
|
200 | - // add Subject Key Identifier extension |
|
201 | - $tbs_cert = $tbs_cert->withAdditionalExtensions( |
|
202 | - new SubjectKeyIdentifierExtension(false, |
|
203 | - $cri->subjectPKInfo() |
|
204 | - ->keyIdentifier())); |
|
205 | - return $tbs_cert; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Get self with fields set from the issuer's certificate. |
|
210 | - * |
|
211 | - * Issuer shall be set to issuing certificate's subject. |
|
212 | - * Authority key identifier extensions shall be added with a key identifier |
|
213 | - * set to issuing certificate's public key identifier. |
|
214 | - * |
|
215 | - * @param Certificate $cert Issuing party's certificate |
|
216 | - * @return self |
|
217 | - */ |
|
218 | - public function withIssuerCertificate(Certificate $cert): self |
|
219 | - { |
|
220 | - $obj = clone $this; |
|
221 | - // set issuer DN from cert's subject |
|
222 | - $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
223 | - // add authority key identifier extension |
|
224 | - $key_id = $cert->tbsCertificate() |
|
225 | - ->subjectPublicKeyInfo() |
|
226 | - ->keyIdentifier(); |
|
227 | - $obj->_extensions = $obj->_extensions->withExtensions( |
|
228 | - new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
229 | - return $obj; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Get self with given version. |
|
234 | - * |
|
235 | - * If version is not set, appropriate version is automatically |
|
236 | - * determined during signing. |
|
237 | - * |
|
238 | - * @param int $version |
|
239 | - * @return self |
|
240 | - */ |
|
241 | - public function withVersion(int $version): self |
|
242 | - { |
|
243 | - $obj = clone $this; |
|
244 | - $obj->_version = $version; |
|
245 | - return $obj; |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Get self with given serial number. |
|
250 | - * |
|
251 | - * @param int|string $serial Base 10 number |
|
252 | - * @return self |
|
253 | - */ |
|
254 | - public function withSerialNumber($serial): self |
|
255 | - { |
|
256 | - $obj = clone $this; |
|
257 | - $obj->_serialNumber = strval($serial); |
|
258 | - return $obj; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Get self with random positive serial number. |
|
263 | - * |
|
264 | - * @param int $size Number of random bytes |
|
265 | - * @return self |
|
266 | - */ |
|
267 | - public function withRandomSerialNumber(int $size = 16): self |
|
268 | - { |
|
269 | - // ensure that first byte is always non-zero and having first bit unset |
|
270 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
271 | - for ($i = 1; $i < $size; ++$i) { |
|
272 | - $num <<= 8; |
|
273 | - $num += mt_rand(0, 0xff); |
|
274 | - } |
|
275 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Get self with given signature algorithm. |
|
280 | - * |
|
281 | - * @param SignatureAlgorithmIdentifier $algo |
|
282 | - * @return self |
|
283 | - */ |
|
284 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
285 | - { |
|
286 | - $obj = clone $this; |
|
287 | - $obj->_signature = $algo; |
|
288 | - return $obj; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Get self with given issuer. |
|
293 | - * |
|
294 | - * @param Name $issuer |
|
295 | - * @return self |
|
296 | - */ |
|
297 | - public function withIssuer(Name $issuer): self |
|
298 | - { |
|
299 | - $obj = clone $this; |
|
300 | - $obj->_issuer = $issuer; |
|
301 | - return $obj; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Get self with given validity. |
|
306 | - * |
|
307 | - * @param Validity $validity |
|
308 | - * @return self |
|
309 | - */ |
|
310 | - public function withValidity(Validity $validity): self |
|
311 | - { |
|
312 | - $obj = clone $this; |
|
313 | - $obj->_validity = $validity; |
|
314 | - return $obj; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * Get self with given subject. |
|
319 | - * |
|
320 | - * @param Name $subject |
|
321 | - * @return self |
|
322 | - */ |
|
323 | - public function withSubject(Name $subject): self |
|
324 | - { |
|
325 | - $obj = clone $this; |
|
326 | - $obj->_subject = $subject; |
|
327 | - return $obj; |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Get self with given subject public key info. |
|
332 | - * |
|
333 | - * @param PublicKeyInfo $pub_key_info |
|
334 | - * @return self |
|
335 | - */ |
|
336 | - public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
337 | - { |
|
338 | - $obj = clone $this; |
|
339 | - $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
340 | - return $obj; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Get self with issuer unique ID. |
|
345 | - * |
|
346 | - * @param UniqueIdentifier $id |
|
347 | - * @return self |
|
348 | - */ |
|
349 | - public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
350 | - { |
|
351 | - $obj = clone $this; |
|
352 | - $obj->_issuerUniqueID = $id; |
|
353 | - return $obj; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Get self with subject unique ID. |
|
358 | - * |
|
359 | - * @param UniqueIdentifier $id |
|
360 | - * @return self |
|
361 | - */ |
|
362 | - public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
363 | - { |
|
364 | - $obj = clone $this; |
|
365 | - $obj->_subjectUniqueID = $id; |
|
366 | - return $obj; |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Get self with given extensions. |
|
371 | - * |
|
372 | - * @param Extensions $extensions |
|
373 | - * @return self |
|
374 | - */ |
|
375 | - public function withExtensions(Extensions $extensions): self |
|
376 | - { |
|
377 | - $obj = clone $this; |
|
378 | - $obj->_extensions = $extensions; |
|
379 | - return $obj; |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Get self with extensions added. |
|
384 | - * |
|
385 | - * @param Extension ...$exts One or more Extension objects |
|
386 | - * @return self |
|
387 | - */ |
|
388 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
389 | - { |
|
390 | - $obj = clone $this; |
|
391 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
392 | - return $obj; |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Check whether version is set. |
|
397 | - * |
|
398 | - * @return bool |
|
399 | - */ |
|
400 | - public function hasVersion(): bool |
|
401 | - { |
|
402 | - return isset($this->_version); |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Get certificate version. |
|
407 | - * |
|
408 | - * @return int |
|
409 | - */ |
|
410 | - public function version(): int |
|
411 | - { |
|
412 | - if (!$this->hasVersion()) { |
|
413 | - throw new \LogicException("version not set."); |
|
414 | - } |
|
415 | - return $this->_version; |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Check whether serial number is set. |
|
420 | - * |
|
421 | - * @return bool |
|
422 | - */ |
|
423 | - public function hasSerialNumber(): bool |
|
424 | - { |
|
425 | - return isset($this->_serialNumber); |
|
426 | - } |
|
427 | - |
|
428 | - /** |
|
429 | - * Get serial number. |
|
430 | - * |
|
431 | - * @return string Base 10 integer |
|
432 | - */ |
|
433 | - public function serialNumber(): string |
|
434 | - { |
|
435 | - if (!$this->hasSerialNumber()) { |
|
436 | - throw new \LogicException("serialNumber not set."); |
|
437 | - } |
|
438 | - return $this->_serialNumber; |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Check whether signature algorithm is set. |
|
443 | - * |
|
444 | - * @return bool |
|
445 | - */ |
|
446 | - public function hasSignature(): bool |
|
447 | - { |
|
448 | - return isset($this->_signature); |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Get signature algorithm. |
|
453 | - * |
|
454 | - * @return SignatureAlgorithmIdentifier |
|
455 | - */ |
|
456 | - public function signature(): SignatureAlgorithmIdentifier |
|
457 | - { |
|
458 | - if (!$this->hasSignature()) { |
|
459 | - throw new \LogicException("signature not set."); |
|
460 | - } |
|
461 | - return $this->_signature; |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Get issuer. |
|
466 | - * |
|
467 | - * @return Name |
|
468 | - */ |
|
469 | - public function issuer(): Name |
|
470 | - { |
|
471 | - return $this->_issuer; |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * Get validity period. |
|
476 | - * |
|
477 | - * @return Validity |
|
478 | - */ |
|
479 | - public function validity(): Validity |
|
480 | - { |
|
481 | - return $this->_validity; |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Get subject. |
|
486 | - * |
|
487 | - * @return Name |
|
488 | - */ |
|
489 | - public function subject(): Name |
|
490 | - { |
|
491 | - return $this->_subject; |
|
492 | - } |
|
493 | - |
|
494 | - /** |
|
495 | - * Get subject public key. |
|
496 | - * |
|
497 | - * @return PublicKeyInfo |
|
498 | - */ |
|
499 | - public function subjectPublicKeyInfo(): PublicKeyInfo |
|
500 | - { |
|
501 | - return $this->_subjectPublicKeyInfo; |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * Whether issuer unique identifier is present. |
|
506 | - * |
|
507 | - * @return bool |
|
508 | - */ |
|
509 | - public function hasIssuerUniqueID(): bool |
|
510 | - { |
|
511 | - return isset($this->_issuerUniqueID); |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Get issuerUniqueID. |
|
516 | - * |
|
517 | - * @return UniqueIdentifier |
|
518 | - */ |
|
519 | - public function issuerUniqueID(): UniqueIdentifier |
|
520 | - { |
|
521 | - if (!$this->hasIssuerUniqueID()) { |
|
522 | - throw new \LogicException("issuerUniqueID not set."); |
|
523 | - } |
|
524 | - return $this->_issuerUniqueID; |
|
525 | - } |
|
526 | - |
|
527 | - /** |
|
528 | - * Whether subject unique identifier is present. |
|
529 | - * |
|
530 | - * @return bool |
|
531 | - */ |
|
532 | - public function hasSubjectUniqueID(): bool |
|
533 | - { |
|
534 | - return isset($this->_subjectUniqueID); |
|
535 | - } |
|
536 | - |
|
537 | - /** |
|
538 | - * Get subjectUniqueID. |
|
539 | - * |
|
540 | - * @return UniqueIdentifier |
|
541 | - */ |
|
542 | - public function subjectUniqueID(): UniqueIdentifier |
|
543 | - { |
|
544 | - if (!$this->hasSubjectUniqueID()) { |
|
545 | - throw new \LogicException("subjectUniqueID not set."); |
|
546 | - } |
|
547 | - return $this->_subjectUniqueID; |
|
548 | - } |
|
549 | - |
|
550 | - /** |
|
551 | - * Get extensions. |
|
552 | - * |
|
553 | - * @return Extensions |
|
554 | - */ |
|
555 | - public function extensions(): Extensions |
|
556 | - { |
|
557 | - return $this->_extensions; |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * Generate ASN.1 structure. |
|
562 | - * |
|
563 | - * @return Sequence |
|
564 | - */ |
|
565 | - public function toASN1(): Sequence |
|
566 | - { |
|
567 | - $elements = array(); |
|
568 | - $version = $this->version(); |
|
569 | - // if version is not default |
|
570 | - if ($version != self::VERSION_1) { |
|
571 | - $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
572 | - } |
|
573 | - $serial = $this->serialNumber(); |
|
574 | - $signature = $this->signature(); |
|
575 | - // add required elements |
|
576 | - array_push($elements, new Integer($serial), $signature->toASN1(), |
|
577 | - $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
578 | - $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
579 | - if (isset($this->_issuerUniqueID)) { |
|
580 | - $elements[] = new ImplicitlyTaggedType(1, |
|
581 | - $this->_issuerUniqueID->toASN1()); |
|
582 | - } |
|
583 | - if (isset($this->_subjectUniqueID)) { |
|
584 | - $elements[] = new ImplicitlyTaggedType(2, |
|
585 | - $this->_subjectUniqueID->toASN1()); |
|
586 | - } |
|
587 | - if (count($this->_extensions)) { |
|
588 | - $elements[] = new ExplicitlyTaggedType(3, |
|
589 | - $this->_extensions->toASN1()); |
|
590 | - } |
|
591 | - return new Sequence(...$elements); |
|
592 | - } |
|
593 | - |
|
594 | - /** |
|
595 | - * Create signed certificate. |
|
596 | - * |
|
597 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
598 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
599 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
600 | - * @return Certificate |
|
601 | - */ |
|
602 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
603 | - PrivateKeyInfo $privkey_info, Crypto $crypto = null): Certificate |
|
604 | - { |
|
605 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
606 | - $tbs_cert = clone $this; |
|
607 | - if (!isset($tbs_cert->_version)) { |
|
608 | - $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
609 | - } |
|
610 | - if (!isset($tbs_cert->_serialNumber)) { |
|
611 | - $tbs_cert->_serialNumber = strval(0); |
|
612 | - } |
|
613 | - $tbs_cert->_signature = $algo; |
|
614 | - $data = $tbs_cert->toASN1()->toDER(); |
|
615 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
616 | - return new Certificate($tbs_cert, $algo, $signature); |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * Determine minimum version for the certificate. |
|
621 | - * |
|
622 | - * @return int |
|
623 | - */ |
|
624 | - protected function _determineVersion(): int |
|
625 | - { |
|
626 | - // if extensions are present |
|
627 | - if (count($this->_extensions)) { |
|
628 | - return self::VERSION_3; |
|
629 | - } |
|
630 | - // if UniqueIdentifier is present |
|
631 | - if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
632 | - return self::VERSION_2; |
|
633 | - } |
|
634 | - return self::VERSION_1; |
|
635 | - } |
|
30 | + // Certificate version enumerations |
|
31 | + const VERSION_1 = 0; |
|
32 | + const VERSION_2 = 1; |
|
33 | + const VERSION_3 = 2; |
|
34 | + |
|
35 | + /** |
|
36 | + * Certificate version. |
|
37 | + * |
|
38 | + * @var int|null |
|
39 | + */ |
|
40 | + protected $_version; |
|
41 | + |
|
42 | + /** |
|
43 | + * Serial number. |
|
44 | + * |
|
45 | + * @var string|null |
|
46 | + */ |
|
47 | + protected $_serialNumber; |
|
48 | + |
|
49 | + /** |
|
50 | + * Signature algorithm. |
|
51 | + * |
|
52 | + * @var SignatureAlgorithmIdentifier|null |
|
53 | + */ |
|
54 | + protected $_signature; |
|
55 | + |
|
56 | + /** |
|
57 | + * Certificate issuer. |
|
58 | + * |
|
59 | + * @var Name $_issuer |
|
60 | + */ |
|
61 | + protected $_issuer; |
|
62 | + |
|
63 | + /** |
|
64 | + * Certificate validity period. |
|
65 | + * |
|
66 | + * @var Validity $_validity |
|
67 | + */ |
|
68 | + protected $_validity; |
|
69 | + |
|
70 | + /** |
|
71 | + * Certificate subject. |
|
72 | + * |
|
73 | + * @var Name $_subject |
|
74 | + */ |
|
75 | + protected $_subject; |
|
76 | + |
|
77 | + /** |
|
78 | + * Subject public key. |
|
79 | + * |
|
80 | + * @var PublicKeyInfo $_subjectPublicKeyInfo |
|
81 | + */ |
|
82 | + protected $_subjectPublicKeyInfo; |
|
83 | + |
|
84 | + /** |
|
85 | + * Issuer unique identifier. |
|
86 | + * |
|
87 | + * @var UniqueIdentifier|null $_issuerUniqueID |
|
88 | + */ |
|
89 | + protected $_issuerUniqueID; |
|
90 | + |
|
91 | + /** |
|
92 | + * Subject unique identifier. |
|
93 | + * |
|
94 | + * @var UniqueIdentifier|null $_subjectUniqueID |
|
95 | + */ |
|
96 | + protected $_subjectUniqueID; |
|
97 | + |
|
98 | + /** |
|
99 | + * Extensions. |
|
100 | + * |
|
101 | + * @var Extensions $_extensions |
|
102 | + */ |
|
103 | + protected $_extensions; |
|
104 | + |
|
105 | + /** |
|
106 | + * Constructor. |
|
107 | + * |
|
108 | + * @param Name $subject Certificate subject |
|
109 | + * @param PublicKeyInfo $pki Subject public key |
|
110 | + * @param Name $issuer Certificate issuer |
|
111 | + * @param Validity $validity Validity period |
|
112 | + */ |
|
113 | + public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
114 | + Validity $validity) |
|
115 | + { |
|
116 | + $this->_subject = $subject; |
|
117 | + $this->_subjectPublicKeyInfo = $pki; |
|
118 | + $this->_issuer = $issuer; |
|
119 | + $this->_validity = $validity; |
|
120 | + $this->_extensions = new Extensions(); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Initialize from ASN.1. |
|
125 | + * |
|
126 | + * @param Sequence $seq |
|
127 | + * @return self |
|
128 | + */ |
|
129 | + public static function fromASN1(Sequence $seq): self |
|
130 | + { |
|
131 | + $idx = 0; |
|
132 | + if ($seq->hasTagged(0)) { |
|
133 | + $idx++; |
|
134 | + $version = $seq->getTagged(0) |
|
135 | + ->asExplicit() |
|
136 | + ->asInteger() |
|
137 | + ->intNumber(); |
|
138 | + } else { |
|
139 | + $version = self::VERSION_1; |
|
140 | + } |
|
141 | + $serial = $seq->at($idx++) |
|
142 | + ->asInteger() |
|
143 | + ->number(); |
|
144 | + $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
145 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
146 | + throw new \UnexpectedValueException( |
|
147 | + "Unsupported signature algorithm " . $algo->name() . "."); |
|
148 | + } |
|
149 | + $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
150 | + $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
151 | + $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
152 | + $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
153 | + $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
154 | + $tbs_cert->_version = $version; |
|
155 | + $tbs_cert->_serialNumber = $serial; |
|
156 | + $tbs_cert->_signature = $algo; |
|
157 | + if ($seq->hasTagged(1)) { |
|
158 | + $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
159 | + $seq->getTagged(1) |
|
160 | + ->asImplicit(Element::TYPE_BIT_STRING) |
|
161 | + ->asBitString()); |
|
162 | + } |
|
163 | + if ($seq->hasTagged(2)) { |
|
164 | + $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
165 | + $seq->getTagged(2) |
|
166 | + ->asImplicit(Element::TYPE_BIT_STRING) |
|
167 | + ->asBitString()); |
|
168 | + } |
|
169 | + if ($seq->hasTagged(3)) { |
|
170 | + $tbs_cert->_extensions = Extensions::fromASN1( |
|
171 | + $seq->getTagged(3) |
|
172 | + ->asExplicit() |
|
173 | + ->asSequence()); |
|
174 | + } |
|
175 | + return $tbs_cert; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Initialize from certification request. |
|
180 | + * |
|
181 | + * Note that signature is not verified and must be done by the caller. |
|
182 | + * |
|
183 | + * @param CertificationRequest $cr |
|
184 | + * @return self |
|
185 | + */ |
|
186 | + public static function fromCSR(CertificationRequest $cr): self |
|
187 | + { |
|
188 | + $cri = $cr->certificationRequestInfo(); |
|
189 | + $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
190 | + Validity::fromStrings(null, null)); |
|
191 | + // if CSR has Extension Request attribute |
|
192 | + if ($cri->hasAttributes()) { |
|
193 | + $attribs = $cri->attributes(); |
|
194 | + if ($attribs->hasExtensionRequest()) { |
|
195 | + $tbs_cert = $tbs_cert->withExtensions( |
|
196 | + $attribs->extensionRequest() |
|
197 | + ->extensions()); |
|
198 | + } |
|
199 | + } |
|
200 | + // add Subject Key Identifier extension |
|
201 | + $tbs_cert = $tbs_cert->withAdditionalExtensions( |
|
202 | + new SubjectKeyIdentifierExtension(false, |
|
203 | + $cri->subjectPKInfo() |
|
204 | + ->keyIdentifier())); |
|
205 | + return $tbs_cert; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Get self with fields set from the issuer's certificate. |
|
210 | + * |
|
211 | + * Issuer shall be set to issuing certificate's subject. |
|
212 | + * Authority key identifier extensions shall be added with a key identifier |
|
213 | + * set to issuing certificate's public key identifier. |
|
214 | + * |
|
215 | + * @param Certificate $cert Issuing party's certificate |
|
216 | + * @return self |
|
217 | + */ |
|
218 | + public function withIssuerCertificate(Certificate $cert): self |
|
219 | + { |
|
220 | + $obj = clone $this; |
|
221 | + // set issuer DN from cert's subject |
|
222 | + $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
223 | + // add authority key identifier extension |
|
224 | + $key_id = $cert->tbsCertificate() |
|
225 | + ->subjectPublicKeyInfo() |
|
226 | + ->keyIdentifier(); |
|
227 | + $obj->_extensions = $obj->_extensions->withExtensions( |
|
228 | + new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
229 | + return $obj; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Get self with given version. |
|
234 | + * |
|
235 | + * If version is not set, appropriate version is automatically |
|
236 | + * determined during signing. |
|
237 | + * |
|
238 | + * @param int $version |
|
239 | + * @return self |
|
240 | + */ |
|
241 | + public function withVersion(int $version): self |
|
242 | + { |
|
243 | + $obj = clone $this; |
|
244 | + $obj->_version = $version; |
|
245 | + return $obj; |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Get self with given serial number. |
|
250 | + * |
|
251 | + * @param int|string $serial Base 10 number |
|
252 | + * @return self |
|
253 | + */ |
|
254 | + public function withSerialNumber($serial): self |
|
255 | + { |
|
256 | + $obj = clone $this; |
|
257 | + $obj->_serialNumber = strval($serial); |
|
258 | + return $obj; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Get self with random positive serial number. |
|
263 | + * |
|
264 | + * @param int $size Number of random bytes |
|
265 | + * @return self |
|
266 | + */ |
|
267 | + public function withRandomSerialNumber(int $size = 16): self |
|
268 | + { |
|
269 | + // ensure that first byte is always non-zero and having first bit unset |
|
270 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
271 | + for ($i = 1; $i < $size; ++$i) { |
|
272 | + $num <<= 8; |
|
273 | + $num += mt_rand(0, 0xff); |
|
274 | + } |
|
275 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Get self with given signature algorithm. |
|
280 | + * |
|
281 | + * @param SignatureAlgorithmIdentifier $algo |
|
282 | + * @return self |
|
283 | + */ |
|
284 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
285 | + { |
|
286 | + $obj = clone $this; |
|
287 | + $obj->_signature = $algo; |
|
288 | + return $obj; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Get self with given issuer. |
|
293 | + * |
|
294 | + * @param Name $issuer |
|
295 | + * @return self |
|
296 | + */ |
|
297 | + public function withIssuer(Name $issuer): self |
|
298 | + { |
|
299 | + $obj = clone $this; |
|
300 | + $obj->_issuer = $issuer; |
|
301 | + return $obj; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Get self with given validity. |
|
306 | + * |
|
307 | + * @param Validity $validity |
|
308 | + * @return self |
|
309 | + */ |
|
310 | + public function withValidity(Validity $validity): self |
|
311 | + { |
|
312 | + $obj = clone $this; |
|
313 | + $obj->_validity = $validity; |
|
314 | + return $obj; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * Get self with given subject. |
|
319 | + * |
|
320 | + * @param Name $subject |
|
321 | + * @return self |
|
322 | + */ |
|
323 | + public function withSubject(Name $subject): self |
|
324 | + { |
|
325 | + $obj = clone $this; |
|
326 | + $obj->_subject = $subject; |
|
327 | + return $obj; |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Get self with given subject public key info. |
|
332 | + * |
|
333 | + * @param PublicKeyInfo $pub_key_info |
|
334 | + * @return self |
|
335 | + */ |
|
336 | + public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
337 | + { |
|
338 | + $obj = clone $this; |
|
339 | + $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
340 | + return $obj; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Get self with issuer unique ID. |
|
345 | + * |
|
346 | + * @param UniqueIdentifier $id |
|
347 | + * @return self |
|
348 | + */ |
|
349 | + public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
350 | + { |
|
351 | + $obj = clone $this; |
|
352 | + $obj->_issuerUniqueID = $id; |
|
353 | + return $obj; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Get self with subject unique ID. |
|
358 | + * |
|
359 | + * @param UniqueIdentifier $id |
|
360 | + * @return self |
|
361 | + */ |
|
362 | + public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
363 | + { |
|
364 | + $obj = clone $this; |
|
365 | + $obj->_subjectUniqueID = $id; |
|
366 | + return $obj; |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Get self with given extensions. |
|
371 | + * |
|
372 | + * @param Extensions $extensions |
|
373 | + * @return self |
|
374 | + */ |
|
375 | + public function withExtensions(Extensions $extensions): self |
|
376 | + { |
|
377 | + $obj = clone $this; |
|
378 | + $obj->_extensions = $extensions; |
|
379 | + return $obj; |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Get self with extensions added. |
|
384 | + * |
|
385 | + * @param Extension ...$exts One or more Extension objects |
|
386 | + * @return self |
|
387 | + */ |
|
388 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
389 | + { |
|
390 | + $obj = clone $this; |
|
391 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
392 | + return $obj; |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Check whether version is set. |
|
397 | + * |
|
398 | + * @return bool |
|
399 | + */ |
|
400 | + public function hasVersion(): bool |
|
401 | + { |
|
402 | + return isset($this->_version); |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Get certificate version. |
|
407 | + * |
|
408 | + * @return int |
|
409 | + */ |
|
410 | + public function version(): int |
|
411 | + { |
|
412 | + if (!$this->hasVersion()) { |
|
413 | + throw new \LogicException("version not set."); |
|
414 | + } |
|
415 | + return $this->_version; |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Check whether serial number is set. |
|
420 | + * |
|
421 | + * @return bool |
|
422 | + */ |
|
423 | + public function hasSerialNumber(): bool |
|
424 | + { |
|
425 | + return isset($this->_serialNumber); |
|
426 | + } |
|
427 | + |
|
428 | + /** |
|
429 | + * Get serial number. |
|
430 | + * |
|
431 | + * @return string Base 10 integer |
|
432 | + */ |
|
433 | + public function serialNumber(): string |
|
434 | + { |
|
435 | + if (!$this->hasSerialNumber()) { |
|
436 | + throw new \LogicException("serialNumber not set."); |
|
437 | + } |
|
438 | + return $this->_serialNumber; |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Check whether signature algorithm is set. |
|
443 | + * |
|
444 | + * @return bool |
|
445 | + */ |
|
446 | + public function hasSignature(): bool |
|
447 | + { |
|
448 | + return isset($this->_signature); |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Get signature algorithm. |
|
453 | + * |
|
454 | + * @return SignatureAlgorithmIdentifier |
|
455 | + */ |
|
456 | + public function signature(): SignatureAlgorithmIdentifier |
|
457 | + { |
|
458 | + if (!$this->hasSignature()) { |
|
459 | + throw new \LogicException("signature not set."); |
|
460 | + } |
|
461 | + return $this->_signature; |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Get issuer. |
|
466 | + * |
|
467 | + * @return Name |
|
468 | + */ |
|
469 | + public function issuer(): Name |
|
470 | + { |
|
471 | + return $this->_issuer; |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * Get validity period. |
|
476 | + * |
|
477 | + * @return Validity |
|
478 | + */ |
|
479 | + public function validity(): Validity |
|
480 | + { |
|
481 | + return $this->_validity; |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Get subject. |
|
486 | + * |
|
487 | + * @return Name |
|
488 | + */ |
|
489 | + public function subject(): Name |
|
490 | + { |
|
491 | + return $this->_subject; |
|
492 | + } |
|
493 | + |
|
494 | + /** |
|
495 | + * Get subject public key. |
|
496 | + * |
|
497 | + * @return PublicKeyInfo |
|
498 | + */ |
|
499 | + public function subjectPublicKeyInfo(): PublicKeyInfo |
|
500 | + { |
|
501 | + return $this->_subjectPublicKeyInfo; |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * Whether issuer unique identifier is present. |
|
506 | + * |
|
507 | + * @return bool |
|
508 | + */ |
|
509 | + public function hasIssuerUniqueID(): bool |
|
510 | + { |
|
511 | + return isset($this->_issuerUniqueID); |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Get issuerUniqueID. |
|
516 | + * |
|
517 | + * @return UniqueIdentifier |
|
518 | + */ |
|
519 | + public function issuerUniqueID(): UniqueIdentifier |
|
520 | + { |
|
521 | + if (!$this->hasIssuerUniqueID()) { |
|
522 | + throw new \LogicException("issuerUniqueID not set."); |
|
523 | + } |
|
524 | + return $this->_issuerUniqueID; |
|
525 | + } |
|
526 | + |
|
527 | + /** |
|
528 | + * Whether subject unique identifier is present. |
|
529 | + * |
|
530 | + * @return bool |
|
531 | + */ |
|
532 | + public function hasSubjectUniqueID(): bool |
|
533 | + { |
|
534 | + return isset($this->_subjectUniqueID); |
|
535 | + } |
|
536 | + |
|
537 | + /** |
|
538 | + * Get subjectUniqueID. |
|
539 | + * |
|
540 | + * @return UniqueIdentifier |
|
541 | + */ |
|
542 | + public function subjectUniqueID(): UniqueIdentifier |
|
543 | + { |
|
544 | + if (!$this->hasSubjectUniqueID()) { |
|
545 | + throw new \LogicException("subjectUniqueID not set."); |
|
546 | + } |
|
547 | + return $this->_subjectUniqueID; |
|
548 | + } |
|
549 | + |
|
550 | + /** |
|
551 | + * Get extensions. |
|
552 | + * |
|
553 | + * @return Extensions |
|
554 | + */ |
|
555 | + public function extensions(): Extensions |
|
556 | + { |
|
557 | + return $this->_extensions; |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * Generate ASN.1 structure. |
|
562 | + * |
|
563 | + * @return Sequence |
|
564 | + */ |
|
565 | + public function toASN1(): Sequence |
|
566 | + { |
|
567 | + $elements = array(); |
|
568 | + $version = $this->version(); |
|
569 | + // if version is not default |
|
570 | + if ($version != self::VERSION_1) { |
|
571 | + $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
572 | + } |
|
573 | + $serial = $this->serialNumber(); |
|
574 | + $signature = $this->signature(); |
|
575 | + // add required elements |
|
576 | + array_push($elements, new Integer($serial), $signature->toASN1(), |
|
577 | + $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
578 | + $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
579 | + if (isset($this->_issuerUniqueID)) { |
|
580 | + $elements[] = new ImplicitlyTaggedType(1, |
|
581 | + $this->_issuerUniqueID->toASN1()); |
|
582 | + } |
|
583 | + if (isset($this->_subjectUniqueID)) { |
|
584 | + $elements[] = new ImplicitlyTaggedType(2, |
|
585 | + $this->_subjectUniqueID->toASN1()); |
|
586 | + } |
|
587 | + if (count($this->_extensions)) { |
|
588 | + $elements[] = new ExplicitlyTaggedType(3, |
|
589 | + $this->_extensions->toASN1()); |
|
590 | + } |
|
591 | + return new Sequence(...$elements); |
|
592 | + } |
|
593 | + |
|
594 | + /** |
|
595 | + * Create signed certificate. |
|
596 | + * |
|
597 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
598 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
599 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
600 | + * @return Certificate |
|
601 | + */ |
|
602 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
603 | + PrivateKeyInfo $privkey_info, Crypto $crypto = null): Certificate |
|
604 | + { |
|
605 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
606 | + $tbs_cert = clone $this; |
|
607 | + if (!isset($tbs_cert->_version)) { |
|
608 | + $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
609 | + } |
|
610 | + if (!isset($tbs_cert->_serialNumber)) { |
|
611 | + $tbs_cert->_serialNumber = strval(0); |
|
612 | + } |
|
613 | + $tbs_cert->_signature = $algo; |
|
614 | + $data = $tbs_cert->toASN1()->toDER(); |
|
615 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
616 | + return new Certificate($tbs_cert, $algo, $signature); |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * Determine minimum version for the certificate. |
|
621 | + * |
|
622 | + * @return int |
|
623 | + */ |
|
624 | + protected function _determineVersion(): int |
|
625 | + { |
|
626 | + // if extensions are present |
|
627 | + if (count($this->_extensions)) { |
|
628 | + return self::VERSION_3; |
|
629 | + } |
|
630 | + // if UniqueIdentifier is present |
|
631 | + if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
632 | + return self::VERSION_2; |
|
633 | + } |
|
634 | + return self::VERSION_1; |
|
635 | + } |
|
636 | 636 | } |
@@ -15,60 +15,60 @@ |
||
15 | 15 | */ |
16 | 16 | class TargetGroup extends Target |
17 | 17 | { |
18 | - /** |
|
19 | - * Group name. |
|
20 | - * |
|
21 | - * @var GeneralName $_name |
|
22 | - */ |
|
23 | - protected $_name; |
|
18 | + /** |
|
19 | + * Group name. |
|
20 | + * |
|
21 | + * @var GeneralName $_name |
|
22 | + */ |
|
23 | + protected $_name; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Constructor. |
|
27 | - * |
|
28 | - * @param GeneralName $name |
|
29 | - */ |
|
30 | - public function __construct(GeneralName $name) |
|
31 | - { |
|
32 | - $this->_name = $name; |
|
33 | - $this->_type = self::TYPE_GROUP; |
|
34 | - } |
|
25 | + /** |
|
26 | + * Constructor. |
|
27 | + * |
|
28 | + * @param GeneralName $name |
|
29 | + */ |
|
30 | + public function __construct(GeneralName $name) |
|
31 | + { |
|
32 | + $this->_name = $name; |
|
33 | + $this->_type = self::TYPE_GROUP; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * @param TaggedType $el |
|
39 | - * @return self |
|
40 | - */ |
|
41 | - public static function fromChosenASN1(TaggedType $el): self |
|
42 | - { |
|
43 | - return new self(GeneralName::fromASN1($el)); |
|
44 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * @param TaggedType $el |
|
39 | + * @return self |
|
40 | + */ |
|
41 | + public static function fromChosenASN1(TaggedType $el): self |
|
42 | + { |
|
43 | + return new self(GeneralName::fromASN1($el)); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * |
|
48 | - * {@inheritdoc} |
|
49 | - */ |
|
50 | - public function string(): string |
|
51 | - { |
|
52 | - return $this->_name->string(); |
|
53 | - } |
|
46 | + /** |
|
47 | + * |
|
48 | + * {@inheritdoc} |
|
49 | + */ |
|
50 | + public function string(): string |
|
51 | + { |
|
52 | + return $this->_name->string(); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get group name. |
|
57 | - * |
|
58 | - * @return GeneralName |
|
59 | - */ |
|
60 | - public function name(): GeneralName |
|
61 | - { |
|
62 | - return $this->_name; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Get group name. |
|
57 | + * |
|
58 | + * @return GeneralName |
|
59 | + */ |
|
60 | + public function name(): GeneralName |
|
61 | + { |
|
62 | + return $this->_name; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * {@inheritdoc} |
|
68 | - * @return ExplicitlyTaggedType |
|
69 | - */ |
|
70 | - public function toASN1(): TaggedType |
|
71 | - { |
|
72 | - return new ExplicitlyTaggedType($this->_type, $this->_name->toASN1()); |
|
73 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * {@inheritdoc} |
|
68 | + * @return ExplicitlyTaggedType |
|
69 | + */ |
|
70 | + public function toASN1(): TaggedType |
|
71 | + { |
|
72 | + return new ExplicitlyTaggedType($this->_type, $this->_name->toASN1()); |
|
73 | + } |
|
74 | 74 | } |
@@ -14,131 +14,131 @@ |
||
14 | 14 | */ |
15 | 15 | class Targets implements \Countable, \IteratorAggregate |
16 | 16 | { |
17 | - /** |
|
18 | - * Target elements. |
|
19 | - * |
|
20 | - * @var Target[] $_targets |
|
21 | - */ |
|
22 | - protected $_targets; |
|
17 | + /** |
|
18 | + * Target elements. |
|
19 | + * |
|
20 | + * @var Target[] $_targets |
|
21 | + */ |
|
22 | + protected $_targets; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Constructor. |
|
26 | - * |
|
27 | - * @param Target ...$targets |
|
28 | - */ |
|
29 | - public function __construct(Target ...$targets) |
|
30 | - { |
|
31 | - $this->_targets = $targets; |
|
32 | - } |
|
24 | + /** |
|
25 | + * Constructor. |
|
26 | + * |
|
27 | + * @param Target ...$targets |
|
28 | + */ |
|
29 | + public function __construct(Target ...$targets) |
|
30 | + { |
|
31 | + $this->_targets = $targets; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Initialize from ASN.1. |
|
36 | - * |
|
37 | - * @param Sequence $seq |
|
38 | - * @return self |
|
39 | - */ |
|
40 | - public static function fromASN1(Sequence $seq): self |
|
41 | - { |
|
42 | - $targets = array_map( |
|
43 | - function (UnspecifiedType $el) { |
|
44 | - return Target::fromASN1($el->asTagged()); |
|
45 | - }, $seq->elements()); |
|
46 | - return new self(...$targets); |
|
47 | - } |
|
34 | + /** |
|
35 | + * Initialize from ASN.1. |
|
36 | + * |
|
37 | + * @param Sequence $seq |
|
38 | + * @return self |
|
39 | + */ |
|
40 | + public static function fromASN1(Sequence $seq): self |
|
41 | + { |
|
42 | + $targets = array_map( |
|
43 | + function (UnspecifiedType $el) { |
|
44 | + return Target::fromASN1($el->asTagged()); |
|
45 | + }, $seq->elements()); |
|
46 | + return new self(...$targets); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Get all targets. |
|
51 | - * |
|
52 | - * @return Target[] |
|
53 | - */ |
|
54 | - public function all(): array |
|
55 | - { |
|
56 | - return $this->_targets; |
|
57 | - } |
|
49 | + /** |
|
50 | + * Get all targets. |
|
51 | + * |
|
52 | + * @return Target[] |
|
53 | + */ |
|
54 | + public function all(): array |
|
55 | + { |
|
56 | + return $this->_targets; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Get all targets of given type. |
|
61 | - * |
|
62 | - * @param int $type |
|
63 | - * @return Target[] |
|
64 | - */ |
|
65 | - protected function _allOfType(int $type): array |
|
66 | - { |
|
67 | - return array_values( |
|
68 | - array_filter($this->_targets, |
|
69 | - function (Target $target) use ($type) { |
|
70 | - return $target->type() == $type; |
|
71 | - })); |
|
72 | - } |
|
59 | + /** |
|
60 | + * Get all targets of given type. |
|
61 | + * |
|
62 | + * @param int $type |
|
63 | + * @return Target[] |
|
64 | + */ |
|
65 | + protected function _allOfType(int $type): array |
|
66 | + { |
|
67 | + return array_values( |
|
68 | + array_filter($this->_targets, |
|
69 | + function (Target $target) use ($type) { |
|
70 | + return $target->type() == $type; |
|
71 | + })); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Get all name targets. |
|
76 | - * |
|
77 | - * @return Target[] |
|
78 | - */ |
|
79 | - public function nameTargets(): array |
|
80 | - { |
|
81 | - return $this->_allOfType(Target::TYPE_NAME); |
|
82 | - } |
|
74 | + /** |
|
75 | + * Get all name targets. |
|
76 | + * |
|
77 | + * @return Target[] |
|
78 | + */ |
|
79 | + public function nameTargets(): array |
|
80 | + { |
|
81 | + return $this->_allOfType(Target::TYPE_NAME); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Get all group targets. |
|
86 | - * |
|
87 | - * @return Target[] |
|
88 | - */ |
|
89 | - public function groupTargets(): array |
|
90 | - { |
|
91 | - return $this->_allOfType(Target::TYPE_GROUP); |
|
92 | - } |
|
84 | + /** |
|
85 | + * Get all group targets. |
|
86 | + * |
|
87 | + * @return Target[] |
|
88 | + */ |
|
89 | + public function groupTargets(): array |
|
90 | + { |
|
91 | + return $this->_allOfType(Target::TYPE_GROUP); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Check whether given target is present. |
|
96 | - * |
|
97 | - * @param Target $target |
|
98 | - * @return boolean |
|
99 | - */ |
|
100 | - public function hasTarget(Target $target): bool |
|
101 | - { |
|
102 | - foreach ($this->_allOfType($target->type()) as $t) { |
|
103 | - if ($target->equals($t)) { |
|
104 | - return true; |
|
105 | - } |
|
106 | - } |
|
107 | - return false; |
|
108 | - } |
|
94 | + /** |
|
95 | + * Check whether given target is present. |
|
96 | + * |
|
97 | + * @param Target $target |
|
98 | + * @return boolean |
|
99 | + */ |
|
100 | + public function hasTarget(Target $target): bool |
|
101 | + { |
|
102 | + foreach ($this->_allOfType($target->type()) as $t) { |
|
103 | + if ($target->equals($t)) { |
|
104 | + return true; |
|
105 | + } |
|
106 | + } |
|
107 | + return false; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Generate ASN.1 structure. |
|
112 | - * |
|
113 | - * @return Sequence |
|
114 | - */ |
|
115 | - public function toASN1(): Sequence |
|
116 | - { |
|
117 | - $elements = array_map( |
|
118 | - function (Target $target) { |
|
119 | - return $target->toASN1(); |
|
120 | - }, $this->_targets); |
|
121 | - return new Sequence(...$elements); |
|
122 | - } |
|
110 | + /** |
|
111 | + * Generate ASN.1 structure. |
|
112 | + * |
|
113 | + * @return Sequence |
|
114 | + */ |
|
115 | + public function toASN1(): Sequence |
|
116 | + { |
|
117 | + $elements = array_map( |
|
118 | + function (Target $target) { |
|
119 | + return $target->toASN1(); |
|
120 | + }, $this->_targets); |
|
121 | + return new Sequence(...$elements); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * |
|
126 | - * @see \Countable::count() |
|
127 | - * @return int |
|
128 | - */ |
|
129 | - public function count(): int |
|
130 | - { |
|
131 | - return count($this->_targets); |
|
132 | - } |
|
124 | + /** |
|
125 | + * |
|
126 | + * @see \Countable::count() |
|
127 | + * @return int |
|
128 | + */ |
|
129 | + public function count(): int |
|
130 | + { |
|
131 | + return count($this->_targets); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * Get iterator for targets. |
|
136 | - * |
|
137 | - * @see \IteratorAggregate::getIterator() |
|
138 | - * @return \ArrayIterator |
|
139 | - */ |
|
140 | - public function getIterator(): \ArrayIterator |
|
141 | - { |
|
142 | - return new \ArrayIterator($this->_targets); |
|
143 | - } |
|
134 | + /** |
|
135 | + * Get iterator for targets. |
|
136 | + * |
|
137 | + * @see \IteratorAggregate::getIterator() |
|
138 | + * @return \ArrayIterator |
|
139 | + */ |
|
140 | + public function getIterator(): \ArrayIterator |
|
141 | + { |
|
142 | + return new \ArrayIterator($this->_targets); |
|
143 | + } |
|
144 | 144 | } |
@@ -13,89 +13,89 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class Target |
15 | 15 | { |
16 | - const TYPE_NAME = 0; |
|
17 | - const TYPE_GROUP = 1; |
|
18 | - const TYPE_CERT = 2; |
|
16 | + const TYPE_NAME = 0; |
|
17 | + const TYPE_GROUP = 1; |
|
18 | + const TYPE_CERT = 2; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Type tag. |
|
22 | - * |
|
23 | - * @var int $_type |
|
24 | - */ |
|
25 | - protected $_type; |
|
20 | + /** |
|
21 | + * Type tag. |
|
22 | + * |
|
23 | + * @var int $_type |
|
24 | + */ |
|
25 | + protected $_type; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Generate ASN.1 element. |
|
29 | - * |
|
30 | - * @return \ASN1\Element |
|
31 | - */ |
|
32 | - abstract public function toASN1(); |
|
27 | + /** |
|
28 | + * Generate ASN.1 element. |
|
29 | + * |
|
30 | + * @return \ASN1\Element |
|
31 | + */ |
|
32 | + abstract public function toASN1(); |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get string value of the target. |
|
36 | - * |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - abstract public function string(): string; |
|
34 | + /** |
|
35 | + * Get string value of the target. |
|
36 | + * |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + abstract public function string(): string; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Initialize concrete object from the chosen ASN.1 element. |
|
43 | - * |
|
44 | - * @param TaggedType $el |
|
45 | - * @return self |
|
46 | - */ |
|
47 | - public static function fromChosenASN1(TaggedType $el) |
|
48 | - { |
|
49 | - throw new \BadMethodCallException( |
|
50 | - __FUNCTION__ . " must be implemented in the derived class."); |
|
51 | - } |
|
41 | + /** |
|
42 | + * Initialize concrete object from the chosen ASN.1 element. |
|
43 | + * |
|
44 | + * @param TaggedType $el |
|
45 | + * @return self |
|
46 | + */ |
|
47 | + public static function fromChosenASN1(TaggedType $el) |
|
48 | + { |
|
49 | + throw new \BadMethodCallException( |
|
50 | + __FUNCTION__ . " must be implemented in the derived class."); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Parse from ASN.1. |
|
55 | - * |
|
56 | - * @param TaggedType $el |
|
57 | - * @throws \UnexpectedValueException |
|
58 | - * @return self |
|
59 | - */ |
|
60 | - public static function fromASN1(TaggedType $el): self |
|
61 | - { |
|
62 | - switch ($el->tag()) { |
|
63 | - case self::TYPE_NAME: |
|
64 | - return TargetName::fromChosenASN1($el->asExplicit()->asTagged()); |
|
65 | - case self::TYPE_GROUP: |
|
66 | - return TargetGroup::fromChosenASN1( |
|
67 | - $el->asExplicit()->asTagged()); |
|
68 | - case self::TYPE_CERT: |
|
69 | - throw new \RuntimeException("targetCert not supported."); |
|
70 | - } |
|
71 | - throw new \UnexpectedValueException( |
|
72 | - "Target type " . $el->tag() . " not supported."); |
|
73 | - } |
|
53 | + /** |
|
54 | + * Parse from ASN.1. |
|
55 | + * |
|
56 | + * @param TaggedType $el |
|
57 | + * @throws \UnexpectedValueException |
|
58 | + * @return self |
|
59 | + */ |
|
60 | + public static function fromASN1(TaggedType $el): self |
|
61 | + { |
|
62 | + switch ($el->tag()) { |
|
63 | + case self::TYPE_NAME: |
|
64 | + return TargetName::fromChosenASN1($el->asExplicit()->asTagged()); |
|
65 | + case self::TYPE_GROUP: |
|
66 | + return TargetGroup::fromChosenASN1( |
|
67 | + $el->asExplicit()->asTagged()); |
|
68 | + case self::TYPE_CERT: |
|
69 | + throw new \RuntimeException("targetCert not supported."); |
|
70 | + } |
|
71 | + throw new \UnexpectedValueException( |
|
72 | + "Target type " . $el->tag() . " not supported."); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Get type tag. |
|
77 | - * |
|
78 | - * @return int |
|
79 | - */ |
|
80 | - public function type(): int |
|
81 | - { |
|
82 | - return $this->_type; |
|
83 | - } |
|
75 | + /** |
|
76 | + * Get type tag. |
|
77 | + * |
|
78 | + * @return int |
|
79 | + */ |
|
80 | + public function type(): int |
|
81 | + { |
|
82 | + return $this->_type; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Check whether target is equal to another. |
|
87 | - * |
|
88 | - * @param Target $other |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - public function equals(Target $other): bool |
|
92 | - { |
|
93 | - if ($this->_type != $other->_type) { |
|
94 | - return false; |
|
95 | - } |
|
96 | - if ($this->toASN1()->toDER() != $other->toASN1()->toDER()) { |
|
97 | - return false; |
|
98 | - } |
|
99 | - return true; |
|
100 | - } |
|
85 | + /** |
|
86 | + * Check whether target is equal to another. |
|
87 | + * |
|
88 | + * @param Target $other |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + public function equals(Target $other): bool |
|
92 | + { |
|
93 | + if ($this->_type != $other->_type) { |
|
94 | + return false; |
|
95 | + } |
|
96 | + if ($this->toASN1()->toDER() != $other->toASN1()->toDER()) { |
|
97 | + return false; |
|
98 | + } |
|
99 | + return true; |
|
100 | + } |
|
101 | 101 | } |
@@ -15,60 +15,60 @@ |
||
15 | 15 | */ |
16 | 16 | class TargetName extends Target |
17 | 17 | { |
18 | - /** |
|
19 | - * Name. |
|
20 | - * |
|
21 | - * @var GeneralName $_name |
|
22 | - */ |
|
23 | - protected $_name; |
|
18 | + /** |
|
19 | + * Name. |
|
20 | + * |
|
21 | + * @var GeneralName $_name |
|
22 | + */ |
|
23 | + protected $_name; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Constructor. |
|
27 | - * |
|
28 | - * @param GeneralName $name |
|
29 | - */ |
|
30 | - public function __construct(GeneralName $name) |
|
31 | - { |
|
32 | - $this->_name = $name; |
|
33 | - $this->_type = self::TYPE_NAME; |
|
34 | - } |
|
25 | + /** |
|
26 | + * Constructor. |
|
27 | + * |
|
28 | + * @param GeneralName $name |
|
29 | + */ |
|
30 | + public function __construct(GeneralName $name) |
|
31 | + { |
|
32 | + $this->_name = $name; |
|
33 | + $this->_type = self::TYPE_NAME; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * @param TaggedType $el |
|
39 | - * @return self |
|
40 | - */ |
|
41 | - public static function fromChosenASN1(TaggedType $el): self |
|
42 | - { |
|
43 | - return new self(GeneralName::fromASN1($el)); |
|
44 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * @param TaggedType $el |
|
39 | + * @return self |
|
40 | + */ |
|
41 | + public static function fromChosenASN1(TaggedType $el): self |
|
42 | + { |
|
43 | + return new self(GeneralName::fromASN1($el)); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * |
|
48 | - * {@inheritdoc} |
|
49 | - */ |
|
50 | - public function string(): string |
|
51 | - { |
|
52 | - return $this->_name->string(); |
|
53 | - } |
|
46 | + /** |
|
47 | + * |
|
48 | + * {@inheritdoc} |
|
49 | + */ |
|
50 | + public function string(): string |
|
51 | + { |
|
52 | + return $this->_name->string(); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get name. |
|
57 | - * |
|
58 | - * @return GeneralName |
|
59 | - */ |
|
60 | - public function name(): GeneralName |
|
61 | - { |
|
62 | - return $this->_name; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Get name. |
|
57 | + * |
|
58 | + * @return GeneralName |
|
59 | + */ |
|
60 | + public function name(): GeneralName |
|
61 | + { |
|
62 | + return $this->_name; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * {@inheritdoc} |
|
68 | - * @return ExplicitlyTaggedType |
|
69 | - */ |
|
70 | - public function toASN1(): TaggedType |
|
71 | - { |
|
72 | - return new ExplicitlyTaggedType($this->_type, $this->_name->toASN1()); |
|
73 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * {@inheritdoc} |
|
68 | + * @return ExplicitlyTaggedType |
|
69 | + */ |
|
70 | + public function toASN1(): TaggedType |
|
71 | + { |
|
72 | + return new ExplicitlyTaggedType($this->_type, $this->_name->toASN1()); |
|
73 | + } |
|
74 | 74 | } |
@@ -19,93 +19,93 @@ |
||
19 | 19 | */ |
20 | 20 | class DisplayText |
21 | 21 | { |
22 | - /** |
|
23 | - * Text. |
|
24 | - * |
|
25 | - * @var string $_text |
|
26 | - */ |
|
27 | - protected $_text; |
|
22 | + /** |
|
23 | + * Text. |
|
24 | + * |
|
25 | + * @var string $_text |
|
26 | + */ |
|
27 | + protected $_text; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Element tag. |
|
31 | - * |
|
32 | - * @var int $_tag |
|
33 | - */ |
|
34 | - protected $_tag; |
|
29 | + /** |
|
30 | + * Element tag. |
|
31 | + * |
|
32 | + * @var int $_tag |
|
33 | + */ |
|
34 | + protected $_tag; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor. |
|
38 | - * |
|
39 | - * @param string $text |
|
40 | - * @param int $tag |
|
41 | - */ |
|
42 | - public function __construct(string $text, int $tag) |
|
43 | - { |
|
44 | - $this->_text = $text; |
|
45 | - $this->_tag = $tag; |
|
46 | - } |
|
36 | + /** |
|
37 | + * Constructor. |
|
38 | + * |
|
39 | + * @param string $text |
|
40 | + * @param int $tag |
|
41 | + */ |
|
42 | + public function __construct(string $text, int $tag) |
|
43 | + { |
|
44 | + $this->_text = $text; |
|
45 | + $this->_tag = $tag; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Initialize from ASN.1. |
|
50 | - * |
|
51 | - * @param StringType $el |
|
52 | - * @return self |
|
53 | - */ |
|
54 | - public static function fromASN1(StringType $el): self |
|
55 | - { |
|
56 | - return new self($el->string(), $el->tag()); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Initialize from ASN.1. |
|
50 | + * |
|
51 | + * @param StringType $el |
|
52 | + * @return self |
|
53 | + */ |
|
54 | + public static function fromASN1(StringType $el): self |
|
55 | + { |
|
56 | + return new self($el->string(), $el->tag()); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Initialize from a UTF-8 string. |
|
61 | - * |
|
62 | - * @param string $str |
|
63 | - * @return self |
|
64 | - */ |
|
65 | - public static function fromString(string $str): self |
|
66 | - { |
|
67 | - return new self($str, Element::TYPE_UTF8_STRING); |
|
68 | - } |
|
59 | + /** |
|
60 | + * Initialize from a UTF-8 string. |
|
61 | + * |
|
62 | + * @param string $str |
|
63 | + * @return self |
|
64 | + */ |
|
65 | + public static function fromString(string $str): self |
|
66 | + { |
|
67 | + return new self($str, Element::TYPE_UTF8_STRING); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get the text. |
|
72 | - * |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function string(): string |
|
76 | - { |
|
77 | - return $this->_text; |
|
78 | - } |
|
70 | + /** |
|
71 | + * Get the text. |
|
72 | + * |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function string(): string |
|
76 | + { |
|
77 | + return $this->_text; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Generate ASN.1 element. |
|
82 | - * |
|
83 | - * @throws \UnexpectedValueException |
|
84 | - * @return StringType |
|
85 | - */ |
|
86 | - public function toASN1(): StringType |
|
87 | - { |
|
88 | - switch ($this->_tag) { |
|
89 | - case Element::TYPE_IA5_STRING: |
|
90 | - return new IA5String($this->_text); |
|
91 | - case Element::TYPE_VISIBLE_STRING: |
|
92 | - return new VisibleString($this->_text); |
|
93 | - case Element::TYPE_BMP_STRING: |
|
94 | - return new BMPString($this->_text); |
|
95 | - case Element::TYPE_UTF8_STRING: |
|
96 | - return new UTF8String($this->_text); |
|
97 | - default: |
|
98 | - throw new \UnexpectedValueException( |
|
99 | - "Type " . Element::tagToName($this->_tag) . " not supported."); |
|
100 | - } |
|
101 | - } |
|
80 | + /** |
|
81 | + * Generate ASN.1 element. |
|
82 | + * |
|
83 | + * @throws \UnexpectedValueException |
|
84 | + * @return StringType |
|
85 | + */ |
|
86 | + public function toASN1(): StringType |
|
87 | + { |
|
88 | + switch ($this->_tag) { |
|
89 | + case Element::TYPE_IA5_STRING: |
|
90 | + return new IA5String($this->_text); |
|
91 | + case Element::TYPE_VISIBLE_STRING: |
|
92 | + return new VisibleString($this->_text); |
|
93 | + case Element::TYPE_BMP_STRING: |
|
94 | + return new BMPString($this->_text); |
|
95 | + case Element::TYPE_UTF8_STRING: |
|
96 | + return new UTF8String($this->_text); |
|
97 | + default: |
|
98 | + throw new \UnexpectedValueException( |
|
99 | + "Type " . Element::tagToName($this->_tag) . " not supported."); |
|
100 | + } |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function __toString() |
|
108 | - { |
|
109 | - return $this->string(); |
|
110 | - } |
|
103 | + /** |
|
104 | + * |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function __toString() |
|
108 | + { |
|
109 | + return $this->string(); |
|
110 | + } |
|
111 | 111 | } |
@@ -16,85 +16,85 @@ |
||
16 | 16 | */ |
17 | 17 | abstract class PolicyQualifierInfo |
18 | 18 | { |
19 | - /** |
|
20 | - * OID for the CPS Pointer qualifier. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - const OID_CPS = "1.3.6.1.5.5.7.2.1"; |
|
19 | + /** |
|
20 | + * OID for the CPS Pointer qualifier. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + const OID_CPS = "1.3.6.1.5.5.7.2.1"; |
|
25 | 25 | |
26 | - /** |
|
27 | - * OID for the user notice qualifier. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - const OID_UNOTICE = "1.3.6.1.5.5.7.2.2"; |
|
26 | + /** |
|
27 | + * OID for the user notice qualifier. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + const OID_UNOTICE = "1.3.6.1.5.5.7.2.2"; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Qualifier identifier. |
|
35 | - * |
|
36 | - * @var string $_oid |
|
37 | - */ |
|
38 | - protected $_oid; |
|
33 | + /** |
|
34 | + * Qualifier identifier. |
|
35 | + * |
|
36 | + * @var string $_oid |
|
37 | + */ |
|
38 | + protected $_oid; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Generate ASN.1 for the 'qualifier' field. |
|
42 | - * |
|
43 | - * @return \ASN1\Element |
|
44 | - */ |
|
45 | - abstract protected function _qualifierASN1(); |
|
40 | + /** |
|
41 | + * Generate ASN.1 for the 'qualifier' field. |
|
42 | + * |
|
43 | + * @return \ASN1\Element |
|
44 | + */ |
|
45 | + abstract protected function _qualifierASN1(); |
|
46 | 46 | |
47 | - /** |
|
48 | - * Initialize from qualifier ASN.1 element. |
|
49 | - * |
|
50 | - * @param UnspecifiedType $el |
|
51 | - * @return self |
|
52 | - */ |
|
53 | - public static function fromQualifierASN1(UnspecifiedType $el) |
|
54 | - { |
|
55 | - throw new \BadMethodCallException( |
|
56 | - __FUNCTION__ . " must be implemented in the derived class."); |
|
57 | - } |
|
47 | + /** |
|
48 | + * Initialize from qualifier ASN.1 element. |
|
49 | + * |
|
50 | + * @param UnspecifiedType $el |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public static function fromQualifierASN1(UnspecifiedType $el) |
|
54 | + { |
|
55 | + throw new \BadMethodCallException( |
|
56 | + __FUNCTION__ . " must be implemented in the derived class."); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Initialize from ASN.1. |
|
61 | - * |
|
62 | - * @param Sequence $seq |
|
63 | - * @throws \UnexpectedValueException |
|
64 | - * @return self |
|
65 | - */ |
|
66 | - public static function fromASN1(Sequence $seq): self |
|
67 | - { |
|
68 | - $oid = $seq->at(0) |
|
69 | - ->asObjectIdentifier() |
|
70 | - ->oid(); |
|
71 | - switch ($oid) { |
|
72 | - case self::OID_CPS: |
|
73 | - return CPSQualifier::fromQualifierASN1($seq->at(1)); |
|
74 | - case self::OID_UNOTICE: |
|
75 | - return UserNoticeQualifier::fromQualifierASN1($seq->at(1)); |
|
76 | - } |
|
77 | - throw new \UnexpectedValueException("Qualifier $oid not supported."); |
|
78 | - } |
|
59 | + /** |
|
60 | + * Initialize from ASN.1. |
|
61 | + * |
|
62 | + * @param Sequence $seq |
|
63 | + * @throws \UnexpectedValueException |
|
64 | + * @return self |
|
65 | + */ |
|
66 | + public static function fromASN1(Sequence $seq): self |
|
67 | + { |
|
68 | + $oid = $seq->at(0) |
|
69 | + ->asObjectIdentifier() |
|
70 | + ->oid(); |
|
71 | + switch ($oid) { |
|
72 | + case self::OID_CPS: |
|
73 | + return CPSQualifier::fromQualifierASN1($seq->at(1)); |
|
74 | + case self::OID_UNOTICE: |
|
75 | + return UserNoticeQualifier::fromQualifierASN1($seq->at(1)); |
|
76 | + } |
|
77 | + throw new \UnexpectedValueException("Qualifier $oid not supported."); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Get qualifier identifier. |
|
82 | - * |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function oid(): string |
|
86 | - { |
|
87 | - return $this->_oid; |
|
88 | - } |
|
80 | + /** |
|
81 | + * Get qualifier identifier. |
|
82 | + * |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function oid(): string |
|
86 | + { |
|
87 | + return $this->_oid; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Generate ASN.1 structure. |
|
92 | - * |
|
93 | - * @return Sequence |
|
94 | - */ |
|
95 | - public function toASN1(): Sequence |
|
96 | - { |
|
97 | - return new Sequence(new ObjectIdentifier($this->_oid), |
|
98 | - $this->_qualifierASN1()); |
|
99 | - } |
|
90 | + /** |
|
91 | + * Generate ASN.1 structure. |
|
92 | + * |
|
93 | + * @return Sequence |
|
94 | + */ |
|
95 | + public function toASN1(): Sequence |
|
96 | + { |
|
97 | + return new Sequence(new ObjectIdentifier($this->_oid), |
|
98 | + $this->_qualifierASN1()); |
|
99 | + } |
|
100 | 100 | } |
@@ -15,51 +15,51 @@ |
||
15 | 15 | */ |
16 | 16 | class CPSQualifier extends PolicyQualifierInfo |
17 | 17 | { |
18 | - /** |
|
19 | - * URI. |
|
20 | - * |
|
21 | - * @var string $_uri |
|
22 | - */ |
|
23 | - protected $_uri; |
|
18 | + /** |
|
19 | + * URI. |
|
20 | + * |
|
21 | + * @var string $_uri |
|
22 | + */ |
|
23 | + protected $_uri; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Constructor. |
|
27 | - * |
|
28 | - * @param string $uri |
|
29 | - */ |
|
30 | - public function __construct(string $uri) |
|
31 | - { |
|
32 | - $this->_oid = self::OID_CPS; |
|
33 | - $this->_uri = $uri; |
|
34 | - } |
|
25 | + /** |
|
26 | + * Constructor. |
|
27 | + * |
|
28 | + * @param string $uri |
|
29 | + */ |
|
30 | + public function __construct(string $uri) |
|
31 | + { |
|
32 | + $this->_oid = self::OID_CPS; |
|
33 | + $this->_uri = $uri; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * @param UnspecifiedType $el |
|
39 | - * @return self |
|
40 | - */ |
|
41 | - public static function fromQualifierASN1(UnspecifiedType $el): self |
|
42 | - { |
|
43 | - return new self($el->asString()->string()); |
|
44 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * @param UnspecifiedType $el |
|
39 | + * @return self |
|
40 | + */ |
|
41 | + public static function fromQualifierASN1(UnspecifiedType $el): self |
|
42 | + { |
|
43 | + return new self($el->asString()->string()); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get URI. |
|
48 | - * |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function uri(): string |
|
52 | - { |
|
53 | - return $this->_uri; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Get URI. |
|
48 | + * |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function uri(): string |
|
52 | + { |
|
53 | + return $this->_uri; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * |
|
58 | - * {@inheritdoc} |
|
59 | - * @return IA5String |
|
60 | - */ |
|
61 | - protected function _qualifierASN1(): IA5String |
|
62 | - { |
|
63 | - return new IA5String($this->_uri); |
|
64 | - } |
|
56 | + /** |
|
57 | + * |
|
58 | + * {@inheritdoc} |
|
59 | + * @return IA5String |
|
60 | + */ |
|
61 | + protected function _qualifierASN1(): IA5String |
|
62 | + { |
|
63 | + return new IA5String($this->_uri); |
|
64 | + } |
|
65 | 65 | } |
@@ -16,114 +16,114 @@ |
||
16 | 16 | */ |
17 | 17 | class UserNoticeQualifier extends PolicyQualifierInfo |
18 | 18 | { |
19 | - /** |
|
20 | - * Explicit notice text. |
|
21 | - * |
|
22 | - * @var DisplayText $_text |
|
23 | - */ |
|
24 | - protected $_text; |
|
19 | + /** |
|
20 | + * Explicit notice text. |
|
21 | + * |
|
22 | + * @var DisplayText $_text |
|
23 | + */ |
|
24 | + protected $_text; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Notice reference. |
|
28 | - * |
|
29 | - * @var NoticeReference $_ref |
|
30 | - */ |
|
31 | - protected $_ref; |
|
26 | + /** |
|
27 | + * Notice reference. |
|
28 | + * |
|
29 | + * @var NoticeReference $_ref |
|
30 | + */ |
|
31 | + protected $_ref; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Constructor. |
|
35 | - * |
|
36 | - * @param DisplayText|null $text |
|
37 | - * @param NoticeReference|null $ref |
|
38 | - */ |
|
39 | - public function __construct(DisplayText $text = null, NoticeReference $ref = null) |
|
40 | - { |
|
41 | - $this->_oid = self::OID_UNOTICE; |
|
42 | - $this->_text = $text; |
|
43 | - $this->_ref = $ref; |
|
44 | - } |
|
33 | + /** |
|
34 | + * Constructor. |
|
35 | + * |
|
36 | + * @param DisplayText|null $text |
|
37 | + * @param NoticeReference|null $ref |
|
38 | + */ |
|
39 | + public function __construct(DisplayText $text = null, NoticeReference $ref = null) |
|
40 | + { |
|
41 | + $this->_oid = self::OID_UNOTICE; |
|
42 | + $this->_text = $text; |
|
43 | + $this->_ref = $ref; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * |
|
48 | - * @param UnspecifiedType $el |
|
49 | - * @return self |
|
50 | - */ |
|
51 | - public static function fromQualifierASN1(UnspecifiedType $el): self |
|
52 | - { |
|
53 | - $seq = $el->asSequence(); |
|
54 | - $ref = null; |
|
55 | - $text = null; |
|
56 | - $idx = 0; |
|
57 | - if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
58 | - $ref = NoticeReference::fromASN1($seq->at($idx++)->asSequence()); |
|
59 | - } |
|
60 | - if ($seq->has($idx, Element::TYPE_STRING)) { |
|
61 | - $text = DisplayText::fromASN1($seq->at($idx)->asString()); |
|
62 | - } |
|
63 | - return new self($text, $ref); |
|
64 | - } |
|
46 | + /** |
|
47 | + * |
|
48 | + * @param UnspecifiedType $el |
|
49 | + * @return self |
|
50 | + */ |
|
51 | + public static function fromQualifierASN1(UnspecifiedType $el): self |
|
52 | + { |
|
53 | + $seq = $el->asSequence(); |
|
54 | + $ref = null; |
|
55 | + $text = null; |
|
56 | + $idx = 0; |
|
57 | + if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
58 | + $ref = NoticeReference::fromASN1($seq->at($idx++)->asSequence()); |
|
59 | + } |
|
60 | + if ($seq->has($idx, Element::TYPE_STRING)) { |
|
61 | + $text = DisplayText::fromASN1($seq->at($idx)->asString()); |
|
62 | + } |
|
63 | + return new self($text, $ref); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Whether explicit text is present. |
|
68 | - * |
|
69 | - * @return bool |
|
70 | - */ |
|
71 | - public function hasExplicitText(): bool |
|
72 | - { |
|
73 | - return isset($this->_text); |
|
74 | - } |
|
66 | + /** |
|
67 | + * Whether explicit text is present. |
|
68 | + * |
|
69 | + * @return bool |
|
70 | + */ |
|
71 | + public function hasExplicitText(): bool |
|
72 | + { |
|
73 | + return isset($this->_text); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Get explicit text. |
|
78 | - * |
|
79 | - * @return DisplayText |
|
80 | - */ |
|
81 | - public function explicitText(): DisplayText |
|
82 | - { |
|
83 | - if (!$this->hasExplicitText()) { |
|
84 | - throw new \LogicException("explicitText not set."); |
|
85 | - } |
|
86 | - return $this->_text; |
|
87 | - } |
|
76 | + /** |
|
77 | + * Get explicit text. |
|
78 | + * |
|
79 | + * @return DisplayText |
|
80 | + */ |
|
81 | + public function explicitText(): DisplayText |
|
82 | + { |
|
83 | + if (!$this->hasExplicitText()) { |
|
84 | + throw new \LogicException("explicitText not set."); |
|
85 | + } |
|
86 | + return $this->_text; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Whether notice reference is present. |
|
91 | - * |
|
92 | - * @return bool |
|
93 | - */ |
|
94 | - public function hasNoticeRef(): bool |
|
95 | - { |
|
96 | - return isset($this->_ref); |
|
97 | - } |
|
89 | + /** |
|
90 | + * Whether notice reference is present. |
|
91 | + * |
|
92 | + * @return bool |
|
93 | + */ |
|
94 | + public function hasNoticeRef(): bool |
|
95 | + { |
|
96 | + return isset($this->_ref); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get notice reference. |
|
101 | - * |
|
102 | - * @throws \RuntimeException |
|
103 | - * @return NoticeReference |
|
104 | - */ |
|
105 | - public function noticeRef(): NoticeReference |
|
106 | - { |
|
107 | - if (!$this->hasNoticeRef()) { |
|
108 | - throw new \LogicException("noticeRef not set."); |
|
109 | - } |
|
110 | - return $this->_ref; |
|
111 | - } |
|
99 | + /** |
|
100 | + * Get notice reference. |
|
101 | + * |
|
102 | + * @throws \RuntimeException |
|
103 | + * @return NoticeReference |
|
104 | + */ |
|
105 | + public function noticeRef(): NoticeReference |
|
106 | + { |
|
107 | + if (!$this->hasNoticeRef()) { |
|
108 | + throw new \LogicException("noticeRef not set."); |
|
109 | + } |
|
110 | + return $this->_ref; |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * |
|
115 | - * {@inheritdoc} |
|
116 | - * @return Sequence |
|
117 | - */ |
|
118 | - protected function _qualifierASN1(): Sequence |
|
119 | - { |
|
120 | - $elements = array(); |
|
121 | - if (isset($this->_ref)) { |
|
122 | - $elements[] = $this->_ref->toASN1(); |
|
123 | - } |
|
124 | - if (isset($this->_text)) { |
|
125 | - $elements[] = $this->_text->toASN1(); |
|
126 | - } |
|
127 | - return new Sequence(...$elements); |
|
128 | - } |
|
113 | + /** |
|
114 | + * |
|
115 | + * {@inheritdoc} |
|
116 | + * @return Sequence |
|
117 | + */ |
|
118 | + protected function _qualifierASN1(): Sequence |
|
119 | + { |
|
120 | + $elements = array(); |
|
121 | + if (isset($this->_ref)) { |
|
122 | + $elements[] = $this->_ref->toASN1(); |
|
123 | + } |
|
124 | + if (isset($this->_text)) { |
|
125 | + $elements[] = $this->_text->toASN1(); |
|
126 | + } |
|
127 | + return new Sequence(...$elements); |
|
128 | + } |
|
129 | 129 | } |