1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace X509\Certificate\Extension; |
6
|
|
|
|
7
|
|
|
use ASN1\Type\UnspecifiedType; |
8
|
|
|
use ASN1\Type\Constructed\Sequence; |
9
|
|
|
use X509\Certificate\Extension\Target\Target; |
10
|
|
|
use X509\Certificate\Extension\Target\Targets; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Implements 'AC Targeting' certificate extension. |
14
|
|
|
* |
15
|
|
|
* <b>NOTE</b>: Syntax is <i>SEQUENCE OF Targets</i>, but only one |
16
|
|
|
* <i>Targets</i> element must be used. |
17
|
|
|
* Multiple <i>Targets</i> elements shall be merged into single <i>Targets</i>. |
18
|
|
|
* |
19
|
|
|
* @link https://tools.ietf.org/html/rfc5755#section-4.3.2 |
20
|
|
|
*/ |
21
|
|
|
class TargetInformationExtension extends Extension implements |
22
|
|
|
\Countable, |
23
|
|
|
\IteratorAggregate |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Targets elements. |
27
|
|
|
* |
28
|
|
|
* @var Targets[] $_targets |
29
|
|
|
*/ |
30
|
|
|
protected $_targets; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Targets[] merged to single Targets. |
34
|
|
|
* |
35
|
|
|
* @var Targets|null |
36
|
|
|
*/ |
37
|
|
|
private $_merged; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param bool $critical |
43
|
|
|
* @param Targets ...$targets |
44
|
|
|
*/ |
45
|
5 |
|
public function __construct(bool $critical, Targets ...$targets) |
46
|
|
|
{ |
47
|
5 |
|
parent::__construct(self::OID_TARGET_INFORMATION, $critical); |
48
|
5 |
|
$this->_targets = $targets; |
49
|
5 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initialize from one or more Target objects. |
53
|
|
|
* |
54
|
|
|
* Extension criticality shall be set to true as specified by RFC 5755. |
55
|
|
|
* |
56
|
|
|
* @param Target ...$target |
57
|
|
|
* @return TargetInformationExtension |
58
|
|
|
*/ |
59
|
1 |
|
public static function fromTargets(Target ...$target): self |
60
|
|
|
{ |
61
|
1 |
|
return new self(true, new Targets(...$target)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Reset internal state on clone. |
66
|
|
|
*/ |
67
|
1 |
|
public function __clone() |
68
|
|
|
{ |
69
|
1 |
|
$this->_merged = null; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
* @return self |
76
|
|
|
*/ |
77
|
3 |
|
protected static function _fromDER(string $data, bool $critical): self |
78
|
|
|
{ |
79
|
3 |
|
$targets = array_map( |
80
|
3 |
|
function (UnspecifiedType $el) { |
81
|
3 |
|
return Targets::fromASN1($el->asSequence()); |
82
|
3 |
|
}, UnspecifiedType::fromDER($data)->asSequence()->elements()); |
83
|
3 |
|
return new self($critical, ...$targets); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get all targets. |
88
|
|
|
* |
89
|
|
|
* @return Targets |
90
|
|
|
*/ |
91
|
8 |
|
public function targets(): Targets |
92
|
|
|
{ |
93
|
8 |
|
if (!isset($this->_merged)) { |
94
|
4 |
|
$a = array(); |
95
|
4 |
|
foreach ($this->_targets as $targets) { |
96
|
4 |
|
$a = array_merge($a, $targets->all()); |
97
|
|
|
} |
98
|
4 |
|
$this->_merged = new Targets(...$a); |
99
|
|
|
} |
100
|
8 |
|
return $this->_merged; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get all name targets. |
105
|
|
|
* |
106
|
|
|
* @return Target[] |
107
|
|
|
*/ |
108
|
1 |
|
public function names(): array |
109
|
|
|
{ |
110
|
1 |
|
return $this->targets()->nameTargets(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get all group targets. |
115
|
|
|
* |
116
|
|
|
* @return Target[] |
117
|
|
|
*/ |
118
|
1 |
|
public function groups(): array |
119
|
|
|
{ |
120
|
1 |
|
return $this->targets()->groupTargets(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* |
125
|
|
|
* @see \X509\Certificate\Extension\Extension::_valueASN1() |
126
|
|
|
* @return Sequence |
127
|
|
|
*/ |
128
|
6 |
|
protected function _valueASN1(): Sequence |
129
|
|
|
{ |
130
|
6 |
|
$elements = array_map( |
131
|
6 |
|
function (Targets $targets) { |
132
|
6 |
|
return $targets->toASN1(); |
133
|
6 |
|
}, $this->_targets); |
134
|
6 |
|
return new Sequence(...$elements); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* @see \Countable::count() |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
1 |
|
public function count(): int |
143
|
|
|
{ |
144
|
1 |
|
return count($this->targets()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get iterator for targets. |
149
|
|
|
* |
150
|
|
|
* @see \IteratorAggregate::getIterator() |
151
|
|
|
* @return \ArrayIterator |
152
|
|
|
*/ |
153
|
1 |
|
public function getIterator(): \ArrayIterator |
154
|
|
|
{ |
155
|
1 |
|
return new \ArrayIterator($this->targets()->all()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|