1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\GeneralName; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
8
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
9
|
|
|
use Sop\X501\ASN1\Name; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Implements *GeneralNames* ASN.1 type. |
13
|
|
|
* |
14
|
|
|
* Provides convenience methods to retrieve the first value of commonly used |
15
|
|
|
* CHOICE types. |
16
|
|
|
* |
17
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.6 |
18
|
|
|
*/ |
19
|
|
|
class GeneralNames implements \Countable, \IteratorAggregate |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* GeneralName objects. |
23
|
|
|
* |
24
|
|
|
* @var GeneralName[] |
25
|
|
|
*/ |
26
|
|
|
protected $_names; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param GeneralName ...$names One or more GeneralName objects |
32
|
|
|
*/ |
33
|
66 |
|
public function __construct(GeneralName ...$names) |
34
|
|
|
{ |
35
|
66 |
|
$this->_names = $names; |
36
|
66 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initialize from ASN.1. |
40
|
|
|
* |
41
|
|
|
* @param Sequence $seq |
42
|
|
|
* |
43
|
|
|
* @throws \UnexpectedValueException |
44
|
|
|
* |
45
|
|
|
* @return self |
46
|
|
|
*/ |
47
|
36 |
|
public static function fromASN1(Sequence $seq): GeneralNames |
48
|
|
|
{ |
49
|
36 |
|
if (!count($seq)) { |
50
|
1 |
|
throw new \UnexpectedValueException( |
51
|
1 |
|
'GeneralNames must have at least one GeneralName.'); |
52
|
|
|
} |
53
|
35 |
|
$names = array_map( |
54
|
|
|
function (UnspecifiedType $el) { |
55
|
35 |
|
return GeneralName::fromASN1($el->asTagged()); |
56
|
35 |
|
}, $seq->elements()); |
57
|
35 |
|
return new self(...$names); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check whether GeneralNames contains a GeneralName of given type. |
62
|
|
|
* |
63
|
|
|
* @param int $tag One of `GeneralName::TAG_*` enumerations |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
2 |
|
public function has(int $tag): bool |
68
|
|
|
{ |
69
|
2 |
|
return null !== $this->_findFirst($tag); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get first GeneralName of given type. |
74
|
|
|
* |
75
|
|
|
* @param int $tag One of `GeneralName::TAG_*` enumerations |
76
|
|
|
* |
77
|
|
|
* @throws \OutOfBoundsException |
78
|
|
|
* |
79
|
|
|
* @return GeneralName |
80
|
|
|
*/ |
81
|
54 |
|
public function firstOf(int $tag): GeneralName |
82
|
|
|
{ |
83
|
54 |
|
$name = $this->_findFirst($tag); |
84
|
54 |
|
if (!$name) { |
85
|
1 |
|
throw new \UnexpectedValueException("No GeneralName by tag {$tag}."); |
86
|
|
|
} |
87
|
53 |
|
return $name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get all GeneralName objects of given type. |
92
|
|
|
* |
93
|
|
|
* @param int $tag One of `GeneralName::TAG_*` enumerations |
94
|
|
|
* |
95
|
|
|
* @return GeneralName[] |
96
|
|
|
*/ |
97
|
4 |
|
public function allOf(int $tag): array |
98
|
|
|
{ |
99
|
4 |
|
$names = array_filter($this->_names, |
100
|
|
|
function (GeneralName $name) use ($tag) { |
101
|
4 |
|
return $name->tag() === $tag; |
102
|
4 |
|
}); |
103
|
4 |
|
return array_values($names); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get value of the first 'dNSName' type. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
2 |
|
public function firstDNS(): string |
112
|
|
|
{ |
113
|
2 |
|
$gn = $this->firstOf(GeneralName::TAG_DNS_NAME); |
114
|
2 |
|
if (!$gn instanceof DNSName) { |
115
|
1 |
|
throw new \RuntimeException( |
116
|
1 |
|
DNSName::class . ' expected, got ' . get_class($gn)); |
117
|
|
|
} |
118
|
1 |
|
return $gn->name(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get value of the first 'directoryName' type. |
123
|
|
|
* |
124
|
|
|
* @return Name |
125
|
|
|
*/ |
126
|
37 |
|
public function firstDN(): Name |
127
|
|
|
{ |
128
|
37 |
|
$gn = $this->firstOf(GeneralName::TAG_DIRECTORY_NAME); |
129
|
37 |
|
if (!$gn instanceof DirectoryName) { |
130
|
1 |
|
throw new \RuntimeException( |
131
|
1 |
|
DirectoryName::class . ' expected, got ' . get_class($gn)); |
132
|
|
|
} |
133
|
36 |
|
return $gn->dn(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get value of the first 'uniformResourceIdentifier' type. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
3 |
|
public function firstURI(): string |
142
|
|
|
{ |
143
|
3 |
|
$gn = $this->firstOf(GeneralName::TAG_URI); |
144
|
3 |
|
if (!$gn instanceof UniformResourceIdentifier) { |
145
|
1 |
|
throw new \RuntimeException( |
146
|
1 |
|
UniformResourceIdentifier::class . ' expected, got ' . get_class($gn)); |
147
|
|
|
} |
148
|
2 |
|
return $gn->uri(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Generate ASN.1 structure. |
153
|
|
|
* |
154
|
|
|
* @return Sequence |
155
|
|
|
*/ |
156
|
53 |
|
public function toASN1(): Sequence |
157
|
|
|
{ |
158
|
53 |
|
if (!count($this->_names)) { |
159
|
1 |
|
throw new \LogicException( |
160
|
1 |
|
'GeneralNames must have at least one GeneralName.'); |
161
|
|
|
} |
162
|
52 |
|
$elements = array_map( |
163
|
|
|
function (GeneralName $name) { |
164
|
52 |
|
return $name->toASN1(); |
165
|
52 |
|
}, $this->_names); |
166
|
52 |
|
return new Sequence(...$elements); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @see \Countable::count() |
171
|
|
|
* |
172
|
|
|
* @return int |
173
|
|
|
*/ |
174
|
1 |
|
public function count(): int |
175
|
|
|
{ |
176
|
1 |
|
return count($this->_names); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get iterator for GeneralName objects. |
181
|
|
|
* |
182
|
|
|
* @see \IteratorAggregate::getIterator() |
183
|
|
|
* |
184
|
|
|
* @return \ArrayIterator |
185
|
|
|
*/ |
186
|
1 |
|
public function getIterator(): \ArrayIterator |
187
|
|
|
{ |
188
|
1 |
|
return new \ArrayIterator($this->_names); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Find first GeneralName by given tag. |
193
|
|
|
* |
194
|
|
|
* @param int $tag |
195
|
|
|
* |
196
|
|
|
* @return null|GeneralName |
197
|
|
|
*/ |
198
|
56 |
|
protected function _findFirst(int $tag): ?GeneralName |
199
|
|
|
{ |
200
|
56 |
|
foreach ($this->_names as $name) { |
201
|
56 |
|
if ($name->tag() === $tag) { |
202
|
56 |
|
return $name; |
203
|
|
|
} |
204
|
|
|
} |
205
|
2 |
|
return null; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|