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