| 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\CertificatePolicy\PolicyInformation; |
| 16 |
use UnexpectedValueException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class CertificatePoliciesExtension extends Extension implements Countable, IteratorAggregate |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
protected array $_policies; |
| 31 |
|
| 32 |
private function __construct(bool $critical, PolicyInformation ...$policies) |
| 33 |
{ |
| 34 |
parent::__construct(Extension::OID_CERTIFICATE_POLICIES, $critical); |
| 35 |
$this->_policies = []; |
| 36 |
foreach ($policies as $policy) { |
| 37 |
$this->_policies[$policy->oid()] = $policy; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public static function create(bool $critical, PolicyInformation ...$policies): self |
| 42 |
{ |
| 43 |
return new self($critical, ...$policies); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function has(string $oid): bool |
| 50 |
{ |
| 51 |
return isset($this->_policies[$oid]); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function get(string $oid): PolicyInformation |
| 58 |
{ |
| 59 |
if (! $this->has($oid)) { |
| 60 |
throw new LogicException("Not certificate policy by OID {$oid}."); |
| 61 |
} |
| 62 |
return $this->_policies[$oid]; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function hasAnyPolicy(): bool |
| 69 |
{ |
| 70 |
return $this->has(PolicyInformation::OID_ANY_POLICY); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function anyPolicy(): PolicyInformation |
| 77 |
{ |
| 78 |
if (! $this->hasAnyPolicy()) { |
| 79 |
throw new LogicException('No anyPolicy.'); |
| 80 |
} |
| 81 |
return $this->get(PolicyInformation::OID_ANY_POLICY); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function count(): int |
| 90 |
{ |
| 91 |
return count($this->_policies); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function getIterator(): ArrayIterator |
| 100 |
{ |
| 101 |
return new ArrayIterator($this->_policies); |
| 102 |
} |
| 103 |
|
| 104 |
protected static function fromDER(string $data, bool $critical): static |
| 105 |
{ |
| 106 |
$policies = array_map( |
| 107 |
static fn (UnspecifiedType $el) => PolicyInformation::fromASN1($el->asSequence()), |
| 108 |
UnspecifiedType::fromDER($data)->asSequence()->elements() |
| 109 |
); |
| 110 |
if (count($policies) === 0) { |
| 111 |
throw new UnexpectedValueException('certificatePolicies must contain at least one PolicyInformation.'); |
| 112 |
} |
| 113 |
return self::create($critical, ...$policies); |
| 114 |
} |
| 115 |
|
| 116 |
protected function valueASN1(): Element |
| 117 |
{ |
| 118 |
if (count($this->_policies) === 0) { |
| 119 |
throw new LogicException('No policies.'); |
| 120 |
} |
| 121 |
$elements = array_map(static fn (PolicyInformation $pi) => $pi->toASN1(), array_values($this->_policies)); |
| 122 |
return Sequence::create(...$elements); |
| 123 |
} |
| 124 |
} |