Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/AttributeCertificate/Attribute/IetfAttrValue.php

raw symbols SCIP
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\Primitive\ObjectIdentifier;
10 use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString;
11 use SpomkyLabs\Pki\ASN1\Type\Primitive\UTF8String;
12 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
13 use Stringable;
14 use UnexpectedValueException;
15
16 /**
17 * Implements *IetfAttrSyntax.values* ASN.1 CHOICE type.
18 *
19 * @see https://tools.ietf.org/html/rfc5755#section-4.4
20 */
21 final class IetfAttrValue implements Stringable
22 {
23 private function __construct(
24 private readonly string $value,
25 private readonly int $type
26 ) {
27 }
28
29 public function __toString(): string
30 {
31 return $this->value;
32 }
33
34 public static function create(string $value, int $type): self
35 {
36 return new self($value, $type);
37 }
38
39 /**
40 * Initialize from ASN.1.
41 */
42 public static function fromASN1(UnspecifiedType $el): self
43 {
44 return match ($el->tag()) {
45 Element::TYPE_OCTET_STRING, Element::TYPE_UTF8_STRING => self::create(
46 $el->asString()
47 ->string(),
48 $el->tag()
49 ),
50 Element::TYPE_OBJECT_IDENTIFIER => self::create($el->asObjectIdentifier()->oid(), $el->tag()),
51 default => throw new UnexpectedValueException('Type ' . Element::tagToName($el->tag()) . ' not supported.'),
52 };
53 }
54
55 /**
56 * Initialize from octet string.
57 */
58 public static function fromOctets(string $octets): self
59 {
60 return self::create($octets, Element::TYPE_OCTET_STRING);
61 }
62
63 /**
64 * Initialize from UTF-8 string.
65 */
66 public static function fromString(string $str): self
67 {
68 return self::create($str, Element::TYPE_UTF8_STRING);
69 }
70
71 /**
72 * Initialize from OID.
73 */
74 public static function fromOID(string $oid): self
75 {
76 return self::create($oid, Element::TYPE_OBJECT_IDENTIFIER);
77 }
78
79 /**
80 * Get type tag.
81 */
82 public function type(): int
83 {
84 return $this->type;
85 }
86
87 /**
88 * Whether value type is octets.
89 */
90 public function isOctets(): bool
91 {
92 return $this->type === Element::TYPE_OCTET_STRING;
93 }
94
95 /**
96 * Whether value type is OID.
97 */
98 public function isOID(): bool
99 {
100 return $this->type === Element::TYPE_OBJECT_IDENTIFIER;
101 }
102
103 /**
104 * Whether value type is string.
105 */
106 public function isString(): bool
107 {
108 return $this->type === Element::TYPE_UTF8_STRING;
109 }
110
111 public function value(): string
112 {
113 return $this->value;
114 }
115
116 /**
117 * Generate ASN.1 structure.
118 */
119 public function toASN1(): Element
120 {
121 return match ($this->type) {
122 Element::TYPE_OCTET_STRING => OctetString::create($this->value),
123 Element::TYPE_UTF8_STRING => UTF8String::create($this->value),
124 Element::TYPE_OBJECT_IDENTIFIER => ObjectIdentifier::create($this->value),
125 default => throw new LogicException('Type ' . Element::tagToName($this->type) . ' not supported.'),
126 };
127 }
128 }