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