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