1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace X509\AttributeCertificate\Attribute; |
6
|
|
|
|
7
|
|
|
use ASN1\Element; |
8
|
|
|
use ASN1\Type\UnspecifiedType; |
9
|
|
|
use ASN1\Type\Primitive\ObjectIdentifier; |
10
|
|
|
use ASN1\Type\Primitive\OctetString; |
11
|
|
|
use ASN1\Type\Primitive\UTF8String; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Implements <i>IetfAttrSyntax.values</i> ASN.1 CHOICE type. |
15
|
|
|
* |
16
|
|
|
* @link https://tools.ietf.org/html/rfc5755#section-4.4 |
17
|
|
|
*/ |
18
|
|
|
class IetfAttrValue |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Element type tag. |
22
|
|
|
* |
23
|
|
|
* @var int $_type |
24
|
|
|
*/ |
25
|
|
|
protected $_type; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Value. |
29
|
|
|
* |
30
|
|
|
* @var string $_value |
31
|
|
|
*/ |
32
|
|
|
protected $_value; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $value |
38
|
|
|
* @param int $type |
39
|
|
|
*/ |
40
|
|
|
public function __construct(string $value, int $type) |
41
|
|
|
{ |
42
|
|
|
$this->_type = $type; |
43
|
|
|
$this->_value = $value; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initialize from ASN.1. |
48
|
|
|
* |
49
|
|
|
* @param UnspecifiedType $el |
50
|
|
|
* @throws \UnexpectedValueException |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
|
|
public static function fromASN1(UnspecifiedType $el) |
54
|
|
|
{ |
55
|
|
|
switch ($el->tag()) { |
56
|
|
|
case Element::TYPE_OCTET_STRING: |
57
|
|
|
case Element::TYPE_UTF8_STRING: |
58
|
|
|
return new self($el->asString()->string(), $el->tag()); |
59
|
|
|
case Element::TYPE_OBJECT_IDENTIFIER: |
60
|
|
|
return new self($el->asObjectIdentifier()->oid(), $el->tag()); |
61
|
|
|
} |
62
|
|
|
throw new \UnexpectedValueException( |
63
|
|
|
"Type " . Element::tagToName($el->tag()) . " not supported."); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initialize from octet string. |
68
|
|
|
* |
69
|
|
|
* @param string $octets |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
|
|
public static function fromOctets(string $octets) |
73
|
|
|
{ |
74
|
|
|
return new self($octets, Element::TYPE_OCTET_STRING); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Initialize from UTF-8 string. |
79
|
|
|
* |
80
|
|
|
* @param string $str |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
|
|
public static function fromString(string $str) |
84
|
|
|
{ |
85
|
|
|
return new self($str, Element::TYPE_UTF8_STRING); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Initialize from OID. |
90
|
|
|
* |
91
|
|
|
* @param string $oid |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
|
|
public static function fromOID(string $oid) |
95
|
|
|
{ |
96
|
|
|
return new self($oid, Element::TYPE_OBJECT_IDENTIFIER); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get type tag. |
101
|
|
|
* |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
|
public function type(): int |
105
|
|
|
{ |
106
|
|
|
return $this->_type; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Whether value type is octets. |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function isOctets(): bool |
115
|
|
|
{ |
116
|
|
|
return $this->_type === Element::TYPE_OCTET_STRING; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Whether value type is OID. |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isOID(): bool |
125
|
|
|
{ |
126
|
|
|
return $this->_type === Element::TYPE_OBJECT_IDENTIFIER; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Whether value type is string. |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function isString(): bool |
135
|
|
|
{ |
136
|
|
|
return $this->_type === Element::TYPE_UTF8_STRING; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get value. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function value(): string |
145
|
|
|
{ |
146
|
|
|
return $this->_value; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Generate ASN.1 structure. |
151
|
|
|
* |
152
|
|
|
* @throws \LogicException |
153
|
|
|
* @return Element |
154
|
|
|
*/ |
155
|
|
|
public function toASN1(): Element |
156
|
|
|
{ |
157
|
|
|
switch ($this->_type) { |
158
|
|
|
case Element::TYPE_OCTET_STRING: |
159
|
|
|
return new OctetString($this->_value); |
160
|
|
|
case Element::TYPE_UTF8_STRING: |
161
|
|
|
return new UTF8String($this->_value); |
162
|
|
|
case Element::TYPE_OBJECT_IDENTIFIER: |
163
|
|
|
return new ObjectIdentifier($this->_value); |
164
|
|
|
} |
165
|
|
|
throw new \LogicException( |
166
|
|
|
"Type " . Element::tagToName($this->_type) . " not supported."); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function __toString() |
174
|
|
|
{ |
175
|
|
|
return $this->_value; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|