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