| 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\UnspecifiedType; |
| 14 |
use SpomkyLabs\Pki\X501\ASN1\Attribute; |
| 15 |
use SpomkyLabs\Pki\X501\ASN1\Collection\SequenceOfAttributes; |
| 16 |
use UnexpectedValueException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class SubjectDirectoryAttributesExtension extends Extension implements Countable, IteratorAggregate |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private readonly SequenceOfAttributes $attributes; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private function __construct(bool $critical, Attribute ...$attribs) |
| 34 |
{ |
| 35 |
parent::__construct(self::OID_SUBJECT_DIRECTORY_ATTRIBUTES, $critical); |
| 36 |
$this->attributes = SequenceOfAttributes::create(...$attribs); |
| 37 |
} |
| 38 |
|
| 39 |
public static function create(bool $critical, Attribute ...$attribs): self |
| 40 |
{ |
| 41 |
return new self($critical, ...$attribs); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function has(string $name): bool |
| 50 |
{ |
| 51 |
return $this->attributes->has($name); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function firstOf(string $name): Attribute |
| 60 |
{ |
| 61 |
return $this->attributes->firstOf($name); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function allOf(string $name): array |
| 72 |
{ |
| 73 |
return $this->attributes->allOf($name); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function all(): array |
| 82 |
{ |
| 83 |
return $this->attributes->all(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function count(): int |
| 90 |
{ |
| 91 |
return count($this->attributes); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function getIterator(): ArrayIterator |
| 100 |
{ |
| 101 |
return $this->attributes->getIterator(); |
| 102 |
} |
| 103 |
|
| 104 |
protected static function fromDER(string $data, bool $critical): static |
| 105 |
{ |
| 106 |
$attribs = SequenceOfAttributes::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
| 107 |
if (count($attribs) === 0) { |
| 108 |
throw new UnexpectedValueException('SubjectDirectoryAttributes must have at least one Attribute.'); |
| 109 |
} |
| 110 |
return self::create($critical, ...$attribs->all()); |
| 111 |
} |
| 112 |
|
| 113 |
protected function valueASN1(): Element |
| 114 |
{ |
| 115 |
if (count($this->attributes) === 0) { |
| 116 |
throw new LogicException('No attributes'); |
| 117 |
} |
| 118 |
return $this->attributes->toASN1(); |
| 119 |
} |
| 120 |
} |