| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\GeneralName; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use IteratorAggregate; |
| 11 |
use LogicException; |
| 12 |
use RuntimeException; |
| 13 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 14 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 15 |
use SpomkyLabs\Pki\X501\ASN1\Name; |
| 16 |
use UnexpectedValueException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
final class GeneralNames implements Countable, IteratorAggregate |
| 26 |
{ |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private readonly array $_names; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private function __construct(GeneralName ...$names) |
| 38 |
{ |
| 39 |
$this->_names = $names; |
| 40 |
} |
| 41 |
|
| 42 |
public static function create(GeneralName ...$names): self |
| 43 |
{ |
| 44 |
return new self(...$names); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static function fromASN1(Sequence $seq): self |
| 51 |
{ |
| 52 |
if (count($seq) === 0) { |
| 53 |
throw new UnexpectedValueException('GeneralNames must have at least one GeneralName.'); |
| 54 |
} |
| 55 |
$names = array_map(static fn (UnspecifiedType $el) => GeneralName::fromASN1($el->asTagged()), $seq->elements()); |
| 56 |
return self::create(...$names); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function has(int $tag): bool |
| 65 |
{ |
| 66 |
return $this->findFirst($tag) !== null; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function firstOf(int $tag): GeneralName |
| 75 |
{ |
| 76 |
$name = $this->findFirst($tag); |
| 77 |
if ($name === null) { |
| 78 |
throw new UnexpectedValueException("No GeneralName by tag {$tag}."); |
| 79 |
} |
| 80 |
return $name; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function allOf(int $tag): array |
| 91 |
{ |
| 92 |
$names = array_filter($this->_names, fn (GeneralName $name) => $name->tag() === $tag); |
| 93 |
return array_values($names); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function firstDNS(): string |
| 100 |
{ |
| 101 |
$gn = $this->firstOf(GeneralName::TAG_DNS_NAME); |
| 102 |
if (! $gn instanceof DNSName) { |
| 103 |
throw new RuntimeException(DNSName::class . ' expected, got ' . $gn::class); |
| 104 |
} |
| 105 |
return $gn->name(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function firstDN(): Name |
| 112 |
{ |
| 113 |
$gn = $this->firstOf(GeneralName::TAG_DIRECTORY_NAME); |
| 114 |
if (! $gn instanceof DirectoryName) { |
| 115 |
throw new RuntimeException(DirectoryName::class . ' expected, got ' . $gn::class); |
| 116 |
} |
| 117 |
return $gn->dn(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function firstURI(): string |
| 124 |
{ |
| 125 |
$gn = $this->firstOf(GeneralName::TAG_URI); |
| 126 |
if (! $gn instanceof UniformResourceIdentifier) { |
| 127 |
throw new RuntimeException(UniformResourceIdentifier::class . ' expected, got ' . $gn::class); |
| 128 |
} |
| 129 |
return $gn->uri(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function toASN1(): Sequence |
| 136 |
{ |
| 137 |
if (count($this->_names) === 0) { |
| 138 |
throw new LogicException('GeneralNames must have at least one GeneralName.'); |
| 139 |
} |
| 140 |
$elements = array_map(static fn (GeneralName $name) => $name->toASN1(), $this->_names); |
| 141 |
return Sequence::create(...$elements); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function count(): int |
| 148 |
{ |
| 149 |
return count($this->_names); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function getIterator(): ArrayIterator |
| 158 |
{ |
| 159 |
return new ArrayIterator($this->_names); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
private function findFirst(int $tag): ?GeneralName |
| 166 |
{ |
| 167 |
foreach ($this->_names as $name) { |
| 168 |
if ($name->tag() === $tag) { |
| 169 |
return $name; |
| 170 |
} |
| 171 |
} |
| 172 |
return null; |
| 173 |
} |
| 174 |
} |