| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate\Extension\NameConstraints; |
| 6 |
|
| 7 |
use function count; |
| 8 |
use SpomkyLabs\Pki\ASN1\Element; |
| 9 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 10 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType; |
| 12 |
use SpomkyLabs\Pki\X509\GeneralName\GeneralName; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
final class GeneralSubtree |
| 20 |
{ |
| 21 |
private function __construct( |
| 22 |
private readonly GeneralName $base, |
| 23 |
private readonly int $min, |
| 24 |
private readonly ?int $max |
| 25 |
) { |
| 26 |
} |
| 27 |
|
| 28 |
public static function create(GeneralName $base, int $min = 0, ?int $max = null): self |
| 29 |
{ |
| 30 |
return new self($base, $min, $max); |
| 31 |
} |
| 32 |
|
| 33 |
public function getBase(): GeneralName |
| 34 |
{ |
| 35 |
return $this->base; |
| 36 |
} |
| 37 |
|
| 38 |
public function getMin(): int |
| 39 |
{ |
| 40 |
return $this->min; |
| 41 |
} |
| 42 |
|
| 43 |
public function getMax(): ?int |
| 44 |
{ |
| 45 |
return $this->max; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static function fromASN1(Sequence $seq): self |
| 52 |
{ |
| 53 |
$base = GeneralName::fromASN1($seq->at(0)->asTagged()); |
| 54 |
$min = 0; |
| 55 |
$max = null; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
$count = count($seq); |
| 61 |
for ($i = 1; $i < $count; ++$i) { |
| 62 |
$el = $seq->at($i) |
| 63 |
->expectTagged(); |
| 64 |
switch ($el->tag()) { |
| 65 |
case 0: |
| 66 |
$min = $el->asImplicit(Element::TYPE_INTEGER) |
| 67 |
->asInteger() |
| 68 |
->intNumber(); |
| 69 |
break; |
| 70 |
case 1: |
| 71 |
$max = $el->asImplicit(Element::TYPE_INTEGER) |
| 72 |
->asInteger() |
| 73 |
->intNumber(); |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
return self::create($base, $min, $max); |
| 78 |
} |
| 79 |
|
| 80 |
public function base(): GeneralName |
| 81 |
{ |
| 82 |
return $this->base; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function toASN1(): Sequence |
| 89 |
{ |
| 90 |
$elements = [$this->base->toASN1()]; |
| 91 |
if ($this->min !== 0) { |
| 92 |
$elements[] = ImplicitlyTaggedType::create(0, Integer::create($this->min)); |
| 93 |
} |
| 94 |
if (isset($this->max)) { |
| 95 |
$elements[] = ImplicitlyTaggedType::create(1, Integer::create($this->max)); |
| 96 |
} |
| 97 |
return Sequence::create(...$elements); |
| 98 |
} |
| 99 |
} |