| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate\Extension; |
| 6 |
|
| 7 |
use LogicException; |
| 8 |
use SpomkyLabs\Pki\ASN1\Element; |
| 9 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 10 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 12 |
use SpomkyLabs\Pki\X509\Certificate\Extension\NameConstraints\GeneralSubtrees; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
final class NameConstraintsExtension extends Extension |
| 20 |
{ |
| 21 |
private function __construct( |
| 22 |
bool $critical, |
| 23 |
private readonly ?GeneralSubtrees $permitted, |
| 24 |
private readonly ?GeneralSubtrees $excluded |
| 25 |
) { |
| 26 |
parent::__construct(self::OID_NAME_CONSTRAINTS, $critical); |
| 27 |
} |
| 28 |
|
| 29 |
public static function create( |
| 30 |
bool $critical, |
| 31 |
?GeneralSubtrees $permitted = null, |
| 32 |
?GeneralSubtrees $excluded = null |
| 33 |
): self { |
| 34 |
return new self($critical, $permitted, $excluded); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function hasPermittedSubtrees(): bool |
| 41 |
{ |
| 42 |
return isset($this->permitted); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function permittedSubtrees(): GeneralSubtrees |
| 49 |
{ |
| 50 |
if (! $this->hasPermittedSubtrees()) { |
| 51 |
throw new LogicException('No permitted subtrees.'); |
| 52 |
} |
| 53 |
return $this->permitted; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function hasExcludedSubtrees(): bool |
| 60 |
{ |
| 61 |
return isset($this->excluded); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function excludedSubtrees(): GeneralSubtrees |
| 68 |
{ |
| 69 |
if (! $this->hasExcludedSubtrees()) { |
| 70 |
throw new LogicException('No excluded subtrees.'); |
| 71 |
} |
| 72 |
return $this->excluded; |
| 73 |
} |
| 74 |
|
| 75 |
protected static function fromDER(string $data, bool $critical): static |
| 76 |
{ |
| 77 |
$seq = UnspecifiedType::fromDER($data)->asSequence(); |
| 78 |
$permitted = null; |
| 79 |
$excluded = null; |
| 80 |
if ($seq->hasTagged(0)) { |
| 81 |
$permitted = GeneralSubtrees::fromASN1( |
| 82 |
$seq->getTagged(0) |
| 83 |
->asImplicit(Element::TYPE_SEQUENCE)->asSequence() |
| 84 |
); |
| 85 |
} |
| 86 |
if ($seq->hasTagged(1)) { |
| 87 |
$excluded = GeneralSubtrees::fromASN1( |
| 88 |
$seq->getTagged(1) |
| 89 |
->asImplicit(Element::TYPE_SEQUENCE)->asSequence() |
| 90 |
); |
| 91 |
} |
| 92 |
return self::create($critical, $permitted, $excluded); |
| 93 |
} |
| 94 |
|
| 95 |
protected function valueASN1(): Element |
| 96 |
{ |
| 97 |
$elements = []; |
| 98 |
if (isset($this->permitted)) { |
| 99 |
$elements[] = ImplicitlyTaggedType::create(0, $this->permitted->toASN1()); |
| 100 |
} |
| 101 |
if (isset($this->excluded)) { |
| 102 |
$elements[] = ImplicitlyTaggedType::create(1, $this->excluded->toASN1()); |
| 103 |
} |
| 104 |
return Sequence::create(...$elements); |
| 105 |
} |
| 106 |
} |