| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X501\ASN1; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use IteratorAggregate; |
| 11 |
use RangeException; |
| 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\X501\ASN1\AttributeValue\AttributeValue; |
| 16 |
use SpomkyLabs\Pki\X501\DN\DNParser; |
| 17 |
use Stringable; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
final class Name implements Countable, IteratorAggregate, Stringable |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private readonly array $rdns; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private function __construct(RDN ...$rdns) |
| 39 |
{ |
| 40 |
$this->rdns = $rdns; |
| 41 |
} |
| 42 |
|
| 43 |
public function __toString(): string |
| 44 |
{ |
| 45 |
return $this->toString(); |
| 46 |
} |
| 47 |
|
| 48 |
public static function create(RDN ...$rdns): self |
| 49 |
{ |
| 50 |
return new self(...$rdns); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static function fromASN1(Sequence $seq): self |
| 57 |
{ |
| 58 |
$rdns = array_map(static fn (UnspecifiedType $el) => RDN::fromASN1($el->asSet()), $seq->elements()); |
| 59 |
return self::create(...$rdns); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static function fromString(string $str): self |
| 68 |
{ |
| 69 |
$rdns = []; |
| 70 |
foreach (DNParser::parseString($str) as $nameComponent) { |
| 71 |
$attribs = []; |
| 72 |
foreach ($nameComponent as [$name, $val]) { |
| 73 |
$type = AttributeType::fromName($name); |
| 74 |
|
| 75 |
if ($val instanceof Element) { |
| 76 |
$el = $val; |
| 77 |
} else { |
| 78 |
$el = AttributeType::asn1StringForType($type->oid(), $val); |
| 79 |
} |
| 80 |
$value = AttributeValue::fromASN1ByOID($type->oid(), $el->asUnspecified()); |
| 81 |
$attribs[] = AttributeTypeAndValue::create($type, $value); |
| 82 |
} |
| 83 |
$rdns[] = RDN::create(...$attribs); |
| 84 |
} |
| 85 |
return self::create(...$rdns); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function toASN1(): Sequence |
| 92 |
{ |
| 93 |
$elements = array_map(static fn (RDN $rdn) => $rdn->toASN1(), $this->rdns); |
| 94 |
return Sequence::create(...$elements); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function toString(): string |
| 103 |
{ |
| 104 |
$parts = array_map(static fn (RDN $rdn) => $rdn->toString(), array_reverse($this->rdns)); |
| 105 |
return implode(',', $parts); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function equals(self $other): bool |
| 118 |
{ |
| 119 |
|
| 120 |
if (count($this) !== count($other)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
for ($i = count($this) - 1; $i >= 0; --$i) { |
| 124 |
$rdn1 = $this->rdns[$i]; |
| 125 |
$rdn2 = $other->rdns[$i]; |
| 126 |
if (! $rdn1->equals($rdn2)) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function all(): array |
| 139 |
{ |
| 140 |
return $this->rdns; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public function firstValueOf(string $name): AttributeValue |
| 153 |
{ |
| 154 |
$oid = AttributeType::attrNameToOID($name); |
| 155 |
foreach ($this->rdns as $rdn) { |
| 156 |
$tvs = $rdn->allOf($oid); |
| 157 |
if (count($tvs) > 1) { |
| 158 |
throw new RangeException("RDN with multiple {$name} attributes."); |
| 159 |
} |
| 160 |
if (count($tvs) === 1) { |
| 161 |
return $tvs[0]->value(); |
| 162 |
} |
| 163 |
} |
| 164 |
throw new RangeException("Attribute {$name} not found."); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function count(): int |
| 171 |
{ |
| 172 |
return count($this->rdns); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function countOfType(string $name): int |
| 181 |
{ |
| 182 |
$oid = AttributeType::attrNameToOID($name); |
| 183 |
return array_sum(array_map(static fn (RDN $rdn): int => count($rdn->allOf($oid)), $this->rdns)); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function getIterator(): ArrayIterator |
| 190 |
{ |
| 191 |
return new ArrayIterator($this->rdns); |
| 192 |
} |
| 193 |
} |