Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extension/Target/Target.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension\Target;
6
7 use RuntimeException;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\ASN1\Type\TaggedType;
10 use UnexpectedValueException;
11
12 /**
13 * Base class for *Target* ASN.1 CHOICE type.
14 *
15 * @see https://tools.ietf.org/html/rfc5755#section-4.3.2
16 */
17 abstract class Target
18 {
19 public const TYPE_NAME = 0;
20
21 public const TYPE_GROUP = 1;
22
23 public const TYPE_CERT = 2;
24
25 protected function __construct(
26 protected int $type
27 ) {
28 }
29
30 /**
31 * Generate ASN.1 element.
32 */
33 abstract public function toASN1(): Element;
34
35 /**
36 * Get string value of the target.
37 */
38 abstract public function string(): string;
39
40 /**
41 * Initialize concrete object from the chosen ASN.1 element.
42 */
43 abstract public static function fromChosenASN1(TaggedType $el): self;
44
45 /**
46 * Parse from ASN.1.
47 */
48 public static function fromASN1(TaggedType $el): self
49 {
50 return match ($el->tag()) {
51 self::TYPE_NAME => TargetName::fromChosenASN1($el->asExplicit()->asTagged()),
52 self::TYPE_GROUP => TargetGroup::fromChosenASN1($el->asExplicit()->asTagged()),
53 self::TYPE_CERT => throw new RuntimeException('targetCert not supported.'),
54 default => throw new UnexpectedValueException('Target type ' . $el->tag() . ' not supported.'),
55 };
56 }
57
58 /**
59 * Get type tag.
60 */
61 public function type(): int
62 {
63 return $this->type;
64 }
65
66 /**
67 * Check whether target is equal to another.
68 */
69 public function equals(self $other): bool
70 {
71 if ($this->type !== $other->type) {
72 return false;
73 }
74 if ($this->toASN1()->toDER() !== $other->toASN1()->toDER()) {
75 return false;
76 }
77 return true;
78 }
79 }