| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class CRLDistributionPointsExtension extends Extension implements Countable, IteratorAggregate |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function distributionPoints(): array |
| 49 |
{ |
| 50 |
return $this->distributionPoints; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function count(): int |
| 59 |
{ |
| 60 |
return count($this->distributionPoints); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
} |