1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate\Extension; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
9
|
|
|
use Sop\ASN1\Type\Primitive\Boolean; |
10
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
11
|
|
|
use Sop\ASN1\Type\Primitive\ObjectIdentifier; |
12
|
|
|
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType; |
13
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Implements 'AA Controls' certificate extension. |
17
|
|
|
* |
18
|
|
|
* @see https://tools.ietf.org/html/rfc5755#section-7.4 |
19
|
|
|
*/ |
20
|
|
|
class AAControlsExtension extends Extension |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Path length contraint. |
24
|
|
|
* |
25
|
|
|
* @var null|int |
26
|
|
|
*/ |
27
|
|
|
protected $_pathLenConstraint; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Permitted attributes. |
31
|
|
|
* |
32
|
|
|
* Array of OID's. |
33
|
|
|
* |
34
|
|
|
* @var null|string[] |
35
|
|
|
*/ |
36
|
|
|
protected $_permittedAttrs; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Excluded attributes. |
40
|
|
|
* |
41
|
|
|
* Array of OID's. |
42
|
|
|
* |
43
|
|
|
* @var null|string[] |
44
|
|
|
*/ |
45
|
|
|
protected $_excludedAttrs; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Whether to permit unspecified attributes. |
49
|
|
|
* |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
protected $_permitUnSpecified; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor. |
56
|
|
|
* |
57
|
|
|
* @param bool $critical |
58
|
|
|
* @param null|int $path_len |
59
|
|
|
* @param null|string[] $permitted |
60
|
|
|
* @param null|string[] $excluded |
61
|
|
|
* @param bool $permit_unspecified |
62
|
|
|
*/ |
63
|
4 |
|
public function __construct(bool $critical, ?int $path_len = null, |
64
|
|
|
?array $permitted = null, ?array $excluded = null, bool $permit_unspecified = true) |
65
|
|
|
{ |
66
|
4 |
|
parent::__construct(self::OID_AA_CONTROLS, $critical); |
67
|
4 |
|
$this->_pathLenConstraint = $path_len; |
68
|
4 |
|
$this->_permittedAttrs = $permitted; |
69
|
4 |
|
$this->_excludedAttrs = $excluded; |
70
|
4 |
|
$this->_permitUnSpecified = $permit_unspecified; |
71
|
4 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check whether path length constraint is present. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
2 |
|
public function hasPathLen(): bool |
79
|
|
|
{ |
80
|
2 |
|
return isset($this->_pathLenConstraint); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get path length constraint. |
85
|
|
|
* |
86
|
|
|
* @throws \LogicException If not set |
87
|
|
|
* |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
2 |
|
public function pathLen(): int |
91
|
|
|
{ |
92
|
2 |
|
if (!$this->hasPathLen()) { |
93
|
1 |
|
throw new \LogicException('pathLen not set.'); |
94
|
|
|
} |
95
|
1 |
|
return $this->_pathLenConstraint; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check whether permitted attributes are present. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
2 |
|
public function hasPermittedAttrs(): bool |
104
|
|
|
{ |
105
|
2 |
|
return isset($this->_permittedAttrs); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get OID's of permitted attributes. |
110
|
|
|
* |
111
|
|
|
* @throws \LogicException If not set |
112
|
|
|
* |
113
|
|
|
* @return string[] |
114
|
|
|
*/ |
115
|
2 |
|
public function permittedAttrs(): array |
116
|
|
|
{ |
117
|
2 |
|
if (!$this->hasPermittedAttrs()) { |
118
|
1 |
|
throw new \LogicException('permittedAttrs not set.'); |
119
|
|
|
} |
120
|
1 |
|
return $this->_permittedAttrs; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check whether excluded attributes are present. |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
2 |
|
public function hasExcludedAttrs(): bool |
129
|
|
|
{ |
130
|
2 |
|
return isset($this->_excludedAttrs); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get OID's of excluded attributes. |
135
|
|
|
* |
136
|
|
|
* @throws \LogicException If not set |
137
|
|
|
* |
138
|
|
|
* @return string[] |
139
|
|
|
*/ |
140
|
2 |
|
public function excludedAttrs(): array |
141
|
|
|
{ |
142
|
2 |
|
if (!$this->hasExcludedAttrs()) { |
143
|
1 |
|
throw new \LogicException('excludedAttrs not set.'); |
144
|
|
|
} |
145
|
1 |
|
return $this->_excludedAttrs; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Whether to permit attributes that are not explicitly specified in |
150
|
|
|
* neither permitted nor excluded list. |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
1 |
|
public function permitUnspecified(): bool |
155
|
|
|
{ |
156
|
1 |
|
return $this->_permitUnSpecified; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
2 |
|
protected static function _fromDER(string $data, bool $critical): Extension |
163
|
|
|
{ |
164
|
2 |
|
$seq = UnspecifiedType::fromDER($data)->asSequence(); |
165
|
2 |
|
$path_len = null; |
166
|
2 |
|
$permitted = null; |
167
|
2 |
|
$excluded = null; |
168
|
2 |
|
$permit_unspecified = true; |
169
|
2 |
|
$idx = 0; |
170
|
2 |
|
if ($seq->has($idx, Element::TYPE_INTEGER)) { |
171
|
1 |
|
$path_len = $seq->at($idx++)->asInteger()->intNumber(); |
172
|
|
|
} |
173
|
2 |
|
if ($seq->hasTagged(0)) { |
174
|
1 |
|
$attr_seq = $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
175
|
1 |
|
->asSequence(); |
176
|
1 |
|
$permitted = array_map( |
177
|
|
|
function (UnspecifiedType $el) { |
178
|
1 |
|
return $el->asObjectIdentifier()->oid(); |
179
|
1 |
|
}, $attr_seq->elements()); |
180
|
1 |
|
++$idx; |
181
|
|
|
} |
182
|
2 |
|
if ($seq->hasTagged(1)) { |
183
|
1 |
|
$attr_seq = $seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE) |
184
|
1 |
|
->asSequence(); |
185
|
1 |
|
$excluded = array_map( |
186
|
|
|
function (UnspecifiedType $el) { |
187
|
1 |
|
return $el->asObjectIdentifier()->oid(); |
188
|
1 |
|
}, $attr_seq->elements()); |
189
|
1 |
|
++$idx; |
190
|
|
|
} |
191
|
2 |
|
if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
192
|
1 |
|
$permit_unspecified = $seq->at($idx++)->asBoolean()->value(); |
193
|
|
|
} |
194
|
2 |
|
return new self($critical, $path_len, $permitted, $excluded, $permit_unspecified); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
2 |
|
protected function _valueASN1(): Element |
201
|
|
|
{ |
202
|
2 |
|
$elements = []; |
203
|
2 |
|
if (isset($this->_pathLenConstraint)) { |
204
|
1 |
|
$elements[] = new Integer($this->_pathLenConstraint); |
205
|
|
|
} |
206
|
2 |
|
if (isset($this->_permittedAttrs)) { |
207
|
1 |
|
$oids = array_map( |
208
|
|
|
function ($oid) { |
209
|
1 |
|
return new ObjectIdentifier($oid); |
210
|
1 |
|
}, $this->_permittedAttrs); |
211
|
1 |
|
$elements[] = new ImplicitlyTaggedType(0, new Sequence(...$oids)); |
212
|
|
|
} |
213
|
2 |
|
if (isset($this->_excludedAttrs)) { |
214
|
1 |
|
$oids = array_map( |
215
|
|
|
function ($oid) { |
216
|
1 |
|
return new ObjectIdentifier($oid); |
217
|
1 |
|
}, $this->_excludedAttrs); |
218
|
1 |
|
$elements[] = new ImplicitlyTaggedType(1, new Sequence(...$oids)); |
219
|
|
|
} |
220
|
2 |
|
if (true !== $this->_permitUnSpecified) { |
221
|
1 |
|
$elements[] = new Boolean(false); |
222
|
|
|
} |
223
|
2 |
|
return new Sequence(...$elements); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|