| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\GeneralName; |
| 6 |
|
| 7 |
use SpomkyLabs\Pki\ASN1\Element; |
| 8 |
use SpomkyLabs\Pki\ASN1\Type\TaggedType; |
| 9 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 10 |
use Stringable; |
| 11 |
use UnexpectedValueException; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
abstract class GeneralName implements Stringable |
| 19 |
{ |
| 20 |
|
| 21 |
public const TAG_OTHER_NAME = 0; |
| 22 |
|
| 23 |
public const TAG_RFC822_NAME = 1; |
| 24 |
|
| 25 |
public const TAG_DNS_NAME = 2; |
| 26 |
|
| 27 |
public const TAG_X400_ADDRESS = 3; |
| 28 |
|
| 29 |
public const TAG_DIRECTORY_NAME = 4; |
| 30 |
|
| 31 |
public const TAG_EDI_PARTY_NAME = 5; |
| 32 |
|
| 33 |
public const TAG_URI = 6; |
| 34 |
|
| 35 |
public const TAG_IP_ADDRESS = 7; |
| 36 |
|
| 37 |
public const TAG_REGISTERED_ID = 8; |
| 38 |
|
| 39 |
protected function __construct( |
| 40 |
protected int $tag |
| 41 |
) { |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __toString(): string |
| 48 |
{ |
| 49 |
return $this->string(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
abstract public function string(): string; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
abstract public static function fromChosenASN1(UnspecifiedType $el): self; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static function fromASN1(TaggedType $el): self |
| 66 |
{ |
| 67 |
return match ($el->tag()) { |
| 68 |
self::TAG_OTHER_NAME => OtherName::fromChosenASN1($el->asImplicit(Element::TYPE_SEQUENCE)), |
| 69 |
self::TAG_RFC822_NAME => RFC822Name::fromChosenASN1($el->asImplicit(Element::TYPE_IA5_STRING)), |
| 70 |
self::TAG_DNS_NAME => DNSName::fromChosenASN1($el->asImplicit(Element::TYPE_IA5_STRING)), |
| 71 |
self::TAG_X400_ADDRESS => X400Address::fromChosenASN1($el->asImplicit(Element::TYPE_SEQUENCE)), |
| 72 |
self::TAG_DIRECTORY_NAME => DirectoryName::fromChosenASN1($el->asExplicit()), |
| 73 |
self::TAG_EDI_PARTY_NAME => EDIPartyName::fromChosenASN1($el->asImplicit(Element::TYPE_SEQUENCE)), |
| 74 |
self::TAG_URI => UniformResourceIdentifier::fromChosenASN1($el->asImplicit(Element::TYPE_IA5_STRING)), |
| 75 |
self::TAG_IP_ADDRESS => IPAddress::fromChosenASN1($el->asImplicit(Element::TYPE_OCTET_STRING)), |
| 76 |
self::TAG_REGISTERED_ID => RegisteredID::fromChosenASN1($el->asImplicit(Element::TYPE_OBJECT_IDENTIFIER)), |
| 77 |
default => throw new UnexpectedValueException('GeneralName type ' . $el->tag() . ' not supported.'), |
| 78 |
}; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function tag(): int |
| 85 |
{ |
| 86 |
return $this->tag; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function toASN1(): Element |
| 93 |
{ |
| 94 |
return $this->choiceASN1(); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function equals(self $other): bool |
| 105 |
{ |
| 106 |
if ($this->tag !== $other->tag) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
if ($this->choiceASN1()->toDER() !== $other->choiceASN1()->toDER()) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
abstract protected function choiceASN1(): TaggedType; |
| 119 |
} |