Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extension/CRLDistributionPointsExtension.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use LogicException;
12 use SpomkyLabs\Pki\ASN1\Element;
13 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
14 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
15 use SpomkyLabs\Pki\X509\Certificate\Extension\DistributionPoint\DistributionPoint;
16 use UnexpectedValueException;
17
18 /**
19 * Implements 'CRL Distribution Points' certificate extension.
20 *
21 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.13
22 */
23 class CRLDistributionPointsExtension extends Extension implements Countable, IteratorAggregate
24 {
25 /**
26 * Distribution points.
27 *
28 * @var DistributionPoint[]
29 */
30 protected array $distributionPoints;
31
32 protected function __construct(string $oid, bool $critical, DistributionPoint ...$distributionPoints)
33 {
34 parent::__construct($oid, $critical);
35 $this->distributionPoints = $distributionPoints;
36 }
37
38 public static function create(bool $critical, DistributionPoint ...$distribution_points): self
39 {
40 return new self(self::OID_CRL_DISTRIBUTION_POINTS, $critical, ...$distribution_points);
41 }
42
43 /**
44 * Get distribution points.
45 *
46 * @return DistributionPoint[]
47 */
48 public function distributionPoints(): array
49 {
50 return $this->distributionPoints;
51 }
52
53 /**
54 * Get the number of distribution points.
55 *
56 * @see \Countable::count()
57 */
58 public function count(): int
59 {
60 return count($this->distributionPoints);
61 }
62
63 /**
64 * Get iterator for distribution points.
65 *
66 * @see \IteratorAggregate::getIterator()
67 */
68 public function getIterator(): ArrayIterator
69 {
70 return new ArrayIterator($this->distributionPoints);
71 }
72
73 protected static function fromDER(string $data, bool $critical): static
74 {
75 $dps = array_map(
76 static fn (UnspecifiedType $el) => DistributionPoint::fromASN1($el->asSequence()),
77 UnspecifiedType::fromDER($data)->asSequence()->elements()
78 );
79 if (count($dps) === 0) {
80 throw new UnexpectedValueException('CRLDistributionPoints must have at least one DistributionPoint.');
81 }
82 // late static bound, extended by Freshest CRL extension
83 return static::create($critical, ...$dps);
84 }
85
86 protected function valueASN1(): Element
87 {
88 if (count($this->distributionPoints) === 0) {
89 throw new LogicException('No distribution points.');
90 }
91 $elements = array_map(static fn (DistributionPoint $dp) => $dp->toASN1(), $this->distributionPoints);
92 return Sequence::create(...$elements);
93 }
94 }