| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\AttributeCertificate\Attribute; |
| 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\ExplicitlyTaggedType; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 13 |
use SpomkyLabs\Pki\X501\ASN1\AttributeType; |
| 14 |
use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue; |
| 15 |
use SpomkyLabs\Pki\X501\MatchingRule\BinaryMatch; |
| 16 |
use SpomkyLabs\Pki\X501\MatchingRule\MatchingRule; |
| 17 |
use SpomkyLabs\Pki\X509\GeneralName\GeneralName; |
| 18 |
use SpomkyLabs\Pki\X509\GeneralName\GeneralNames; |
| 19 |
use SpomkyLabs\Pki\X509\GeneralName\UniformResourceIdentifier; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
final class RoleAttributeValue extends AttributeValue |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private function __construct( |
| 33 |
private readonly GeneralName $roleName, |
| 34 |
private readonly ?GeneralNames $roleAuthority |
| 35 |
) { |
| 36 |
parent::__construct(AttributeType::OID_ROLE); |
| 37 |
} |
| 38 |
|
| 39 |
public static function create(GeneralName $roleName, ?GeneralNames $roleAuthority = null): self |
| 40 |
{ |
| 41 |
return new self($roleName, $roleAuthority); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static function fromString(string $role_name, ?GeneralNames $authority = null): self |
| 51 |
{ |
| 52 |
return self::create(UniformResourceIdentifier::create($role_name), $authority); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static function fromASN1(UnspecifiedType $el): AttributeValue |
| 59 |
{ |
| 60 |
$seq = $el->asSequence(); |
| 61 |
$authority = null; |
| 62 |
if ($seq->hasTagged(0)) { |
| 63 |
$authority = GeneralNames::fromASN1( |
| 64 |
$seq->getTagged(0) |
| 65 |
->asImplicit(Element::TYPE_SEQUENCE) |
| 66 |
->asSequence() |
| 67 |
); |
| 68 |
} |
| 69 |
$name = GeneralName::fromASN1($seq->getTagged(1)->asExplicit()->asTagged()); |
| 70 |
return self::create($name, $authority); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function hasRoleAuthority(): bool |
| 77 |
{ |
| 78 |
return isset($this->roleAuthority); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function roleAuthority(): GeneralNames |
| 85 |
{ |
| 86 |
if (! $this->hasRoleAuthority()) { |
| 87 |
throw new LogicException('roleAuthority not set.'); |
| 88 |
} |
| 89 |
return $this->roleAuthority; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function roleName(): GeneralName |
| 96 |
{ |
| 97 |
return $this->roleName; |
| 98 |
} |
| 99 |
|
| 100 |
public function toASN1(): Element |
| 101 |
{ |
| 102 |
$elements = []; |
| 103 |
if (isset($this->roleAuthority)) { |
| 104 |
$elements[] = ImplicitlyTaggedType::create(0, $this->roleAuthority->toASN1()); |
| 105 |
} |
| 106 |
$elements[] = ExplicitlyTaggedType::create(1, $this->roleName->toASN1()); |
| 107 |
return Sequence::create(...$elements); |
| 108 |
} |
| 109 |
|
| 110 |
public function stringValue(): string |
| 111 |
{ |
| 112 |
return '#' . bin2hex($this->toASN1()->toDER()); |
| 113 |
} |
| 114 |
|
| 115 |
public function equalityMatchingRule(): MatchingRule |
| 116 |
{ |
| 117 |
return new BinaryMatch(); |
| 118 |
} |
| 119 |
|
| 120 |
public function rfc2253String(): string |
| 121 |
{ |
| 122 |
return $this->stringValue(); |
| 123 |
} |
| 124 |
|
| 125 |
protected function _transcodedString(): string |
| 126 |
{ |
| 127 |
return $this->stringValue(); |
| 128 |
} |
| 129 |
} |