1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate\Extension\CertificatePolicy; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
8
|
|
|
use Sop\ASN1\Type\Primitive\ObjectIdentifier; |
9
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Implements *PolicyInformation* ASN.1 type used by 'Certificate Policies' |
13
|
|
|
* certificate extension. |
14
|
|
|
* |
15
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.4 |
16
|
|
|
*/ |
17
|
|
|
class PolicyInformation implements \Countable, \IteratorAggregate |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Wildcard policy. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const OID_ANY_POLICY = '2.5.29.32.0'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Policy identifier. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $_oid; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Policy qualifiers. |
35
|
|
|
* |
36
|
|
|
* @var PolicyQualifierInfo[] |
37
|
|
|
*/ |
38
|
|
|
protected $_qualifiers; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $oid |
44
|
|
|
* @param PolicyQualifierInfo ...$qualifiers |
45
|
|
|
*/ |
46
|
24 |
|
public function __construct(string $oid, PolicyQualifierInfo ...$qualifiers) |
47
|
|
|
{ |
48
|
24 |
|
$this->_oid = $oid; |
49
|
24 |
|
$this->_qualifiers = []; |
50
|
24 |
|
foreach ($qualifiers as $qual) { |
51
|
16 |
|
$this->_qualifiers[$qual->oid()] = $qual; |
52
|
|
|
} |
53
|
24 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initialize from ASN.1. |
57
|
|
|
* |
58
|
|
|
* @param Sequence $seq |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
11 |
|
public static function fromASN1(Sequence $seq): self |
63
|
|
|
{ |
64
|
11 |
|
$oid = $seq->at(0)->asObjectIdentifier()->oid(); |
65
|
11 |
|
$qualifiers = []; |
66
|
11 |
|
if (count($seq) > 1) { |
67
|
11 |
|
$qualifiers = array_map( |
68
|
|
|
function (UnspecifiedType $el) { |
69
|
11 |
|
return PolicyQualifierInfo::fromASN1($el->asSequence()); |
70
|
11 |
|
}, $seq->at(1)->asSequence()->elements()); |
71
|
|
|
} |
72
|
11 |
|
return new self($oid, ...$qualifiers); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get policy identifier. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
26 |
|
public function oid(): string |
81
|
|
|
{ |
82
|
26 |
|
return $this->_oid; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Check whether this policy is anyPolicy. |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
15 |
|
public function isAnyPolicy(): bool |
91
|
|
|
{ |
92
|
15 |
|
return self::OID_ANY_POLICY === $this->_oid; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get policy qualifiers. |
97
|
|
|
* |
98
|
|
|
* @return PolicyQualifierInfo[] |
99
|
|
|
*/ |
100
|
14 |
|
public function qualifiers(): array |
101
|
|
|
{ |
102
|
14 |
|
return array_values($this->_qualifiers); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check whether qualifier is present. |
107
|
|
|
* |
108
|
|
|
* @param string $oid |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
13 |
|
public function has(string $oid): bool |
113
|
|
|
{ |
114
|
13 |
|
return isset($this->_qualifiers[$oid]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get qualifier by OID. |
119
|
|
|
* |
120
|
|
|
* @param string $oid |
121
|
|
|
* |
122
|
|
|
* @throws \LogicException IF not set |
123
|
|
|
* |
124
|
|
|
* @return PolicyQualifierInfo |
125
|
|
|
*/ |
126
|
9 |
|
public function get(string $oid): PolicyQualifierInfo |
127
|
|
|
{ |
128
|
9 |
|
if (!$this->has($oid)) { |
129
|
1 |
|
throw new \LogicException("No {$oid} qualifier."); |
130
|
|
|
} |
131
|
8 |
|
return $this->_qualifiers[$oid]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Check whether CPS qualifier is present. |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
3 |
|
public function hasCPSQualifier(): bool |
140
|
|
|
{ |
141
|
3 |
|
return $this->has(PolicyQualifierInfo::OID_CPS); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get CPS qualifier. |
146
|
|
|
* |
147
|
|
|
* @throws \LogicException If not set |
148
|
|
|
* |
149
|
|
|
* @return CPSQualifier |
150
|
|
|
*/ |
151
|
3 |
|
public function CPSQualifier(): CPSQualifier |
152
|
|
|
{ |
153
|
3 |
|
if (!$this->hasCPSQualifier()) { |
154
|
1 |
|
throw new \LogicException('CPS qualifier not set.'); |
155
|
|
|
} |
156
|
2 |
|
return $this->get(PolicyQualifierInfo::OID_CPS); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Check whether user notice qualifier is present. |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
4 |
|
public function hasUserNoticeQualifier(): bool |
165
|
|
|
{ |
166
|
4 |
|
return $this->has(PolicyQualifierInfo::OID_UNOTICE); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get user notice qualifier. |
171
|
|
|
* |
172
|
|
|
* @throws \LogicException If not set |
173
|
|
|
* |
174
|
|
|
* @return UserNoticeQualifier |
175
|
|
|
*/ |
176
|
4 |
|
public function userNoticeQualifier(): UserNoticeQualifier |
177
|
|
|
{ |
178
|
4 |
|
if (!$this->hasUserNoticeQualifier()) { |
179
|
1 |
|
throw new \LogicException('User notice qualifier not set.'); |
180
|
|
|
} |
181
|
3 |
|
return $this->get(PolicyQualifierInfo::OID_UNOTICE); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get ASN.1 structure. |
186
|
|
|
* |
187
|
|
|
* @return Sequence |
188
|
|
|
*/ |
189
|
31 |
|
public function toASN1(): Sequence |
190
|
|
|
{ |
191
|
31 |
|
$elements = [new ObjectIdentifier($this->_oid)]; |
192
|
31 |
|
if (count($this->_qualifiers)) { |
193
|
18 |
|
$qualifiers = array_map( |
194
|
|
|
function (PolicyQualifierInfo $pqi) { |
195
|
18 |
|
return $pqi->toASN1(); |
196
|
18 |
|
}, array_values($this->_qualifiers)); |
197
|
18 |
|
$elements[] = new Sequence(...$qualifiers); |
198
|
|
|
} |
199
|
31 |
|
return new Sequence(...$elements); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get number of qualifiers. |
204
|
|
|
* |
205
|
|
|
* @see \Countable::count() |
206
|
|
|
* |
207
|
|
|
* @return int |
208
|
|
|
*/ |
209
|
2 |
|
public function count(): int |
210
|
|
|
{ |
211
|
2 |
|
return count($this->_qualifiers); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get iterator for qualifiers. |
216
|
|
|
* |
217
|
|
|
* @see \IteratorAggregate::getIterator() |
218
|
|
|
* |
219
|
|
|
* @return \ArrayIterator |
220
|
|
|
*/ |
221
|
2 |
|
public function getIterator(): \ArrayIterator |
222
|
|
|
{ |
223
|
2 |
|
return new \ArrayIterator($this->_qualifiers); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|