1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\XML\ds; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
10
|
|
|
use SimpleSAML\XML\Exception\MissingElementException; |
11
|
|
|
use SimpleSAML\XML\Exception\TooManyElementsException; |
12
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
13
|
|
|
|
14
|
|
|
use function array_pop; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class representing a ds:Signature element. |
18
|
|
|
* |
19
|
|
|
* @package simplesamlphp/xml-security |
20
|
|
|
*/ |
21
|
|
|
final class Signature extends AbstractDsElement |
22
|
|
|
{ |
23
|
|
|
/** @var \SimpleSAML\XMLSecurity\XML\ds\SignedInfo */ |
24
|
|
|
protected SignedInfo $signedInfo; |
25
|
|
|
|
26
|
|
|
/** @var \SimpleSAML\XMLSecurity\XML\ds\SignatureValue */ |
27
|
|
|
protected SignatureValue $signatureValue; |
28
|
|
|
|
29
|
|
|
/** @var \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo */ |
30
|
|
|
protected ?KeyInfo $keyInfo; |
31
|
|
|
|
32
|
|
|
/** @var \SimpleSAML\XML\ds\DsObject[] */ |
33
|
|
|
protected array $objects; |
34
|
|
|
|
35
|
|
|
/** @var string|null */ |
36
|
|
|
protected ?string $Id; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Signature constructor. |
41
|
|
|
* |
42
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo $signedInfo |
43
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue |
44
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo |
45
|
|
|
* @param \SimpleSAML\XML\ds\DsObject[] $objects |
46
|
|
|
* @param string|null $Id |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
SignedInfo $signedInfo, |
50
|
|
|
SignatureValue $signatureValue, |
51
|
|
|
?KeyInfo $keyInfo, |
52
|
|
|
array $objects = [], |
53
|
|
|
?string $Id = null, |
54
|
|
|
) { |
55
|
|
|
$this->setSignedInfo($signedInfo); |
56
|
|
|
$this->setSignatureValue($signatureValue); |
57
|
|
|
$this->setKeyInfo($keyInfo); |
58
|
|
|
$this->setObjects($objects); |
59
|
|
|
$this->setId($Id); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the Id used for this signature. |
65
|
|
|
* |
66
|
|
|
* @return string|null |
67
|
|
|
*/ |
68
|
|
|
public function getId(): ?string |
69
|
|
|
{ |
70
|
|
|
return $this->Id; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the Id used for this signature. |
76
|
|
|
* |
77
|
|
|
* @param string|null $Id |
78
|
|
|
*/ |
79
|
|
|
protected function setId(?string $Id): void |
80
|
|
|
{ |
81
|
|
|
Assert::nullOrValidNCName($Id); |
82
|
|
|
$this->Id = $Id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo $signedInfo |
88
|
|
|
*/ |
89
|
|
|
protected function setSignedInfo(SignedInfo $signedInfo): void |
90
|
|
|
{ |
91
|
|
|
$this->signedInfo = $signedInfo; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return \SimpleSAML\XMLSecurity\XML\ds\SignedInfo |
97
|
|
|
*/ |
98
|
|
|
public function getSignedInfo(): SignedInfo |
99
|
|
|
{ |
100
|
|
|
return $this->signedInfo; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue |
106
|
|
|
*/ |
107
|
|
|
protected function setSignatureValue(SignatureValue $signatureValue): void |
108
|
|
|
{ |
109
|
|
|
$this->signatureValue = $signatureValue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return \SimpleSAML\XMLSecurity\XML\ds\SignatureValue |
115
|
|
|
*/ |
116
|
|
|
public function getSignatureValue(): SignatureValue |
117
|
|
|
{ |
118
|
|
|
return $this->signatureValue; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo |
124
|
|
|
*/ |
125
|
|
|
protected function setKeyInfo(?KeyInfo $keyInfo): void |
126
|
|
|
{ |
127
|
|
|
$this->keyInfo = $keyInfo; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null |
133
|
|
|
*/ |
134
|
|
|
public function getKeyInfo(): ?KeyInfo |
135
|
|
|
{ |
136
|
|
|
return $this->keyInfo; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the array of ds:Object elements attached to this signature. |
142
|
|
|
* |
143
|
|
|
* @return \SimpleSAML\XML\ds\DsObject[] |
144
|
|
|
*/ |
145
|
|
|
public function getObjects(): array |
146
|
|
|
{ |
147
|
|
|
return $this->objects; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the array of ds:Object elements attached to this signature. |
153
|
|
|
* |
154
|
|
|
* @param \SimpleSAML\XML\ds\DsObject[] $objects |
155
|
|
|
*/ |
156
|
|
|
protected function setObjects(array $objects): void |
157
|
|
|
{ |
158
|
|
|
Assert::allIsInstanceOf($objects, DsObject::class); |
159
|
|
|
$this->objects = $objects; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Convert XML into a Signature element |
165
|
|
|
* |
166
|
|
|
* @param \DOMElement $xml |
167
|
|
|
* @return static |
168
|
|
|
* |
169
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
170
|
|
|
* If the qualified name of the supplied element is wrong |
171
|
|
|
*/ |
172
|
|
|
public static function fromXML(DOMElement $xml): static |
173
|
|
|
{ |
174
|
|
|
Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class); |
175
|
|
|
Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class); |
176
|
|
|
|
177
|
|
|
$Id = self::getAttribute($xml, 'Id', null); |
178
|
|
|
|
179
|
|
|
$signedInfo = SignedInfo::getChildrenOfClass($xml); |
180
|
|
|
Assert::minCount( |
181
|
|
|
$signedInfo, |
182
|
|
|
1, |
183
|
|
|
'ds:Signature needs exactly one ds:SignedInfo element.', |
184
|
|
|
MissingElementException::class, |
185
|
|
|
); |
186
|
|
|
Assert::maxCount( |
187
|
|
|
$signedInfo, |
188
|
|
|
1, |
189
|
|
|
'ds:Signature needs exactly one ds:SignedInfo element.', |
190
|
|
|
TooManyElementsException::class, |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$signatureValue = SignatureValue::getChildrenOfClass($xml); |
194
|
|
|
Assert::minCount( |
195
|
|
|
$signatureValue, |
196
|
|
|
1, |
197
|
|
|
'ds:Signature needs exactly one ds:SignatureValue element.', |
198
|
|
|
MissingElementException::class, |
199
|
|
|
); |
200
|
|
|
Assert::maxCount( |
201
|
|
|
$signatureValue, |
202
|
|
|
1, |
203
|
|
|
'ds:Signature needs exactly one ds:SignatureValue element.', |
204
|
|
|
TooManyElementsException::class, |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
$keyInfo = KeyInfo::getChildrenOfClass($xml); |
208
|
|
|
Assert::maxCount( |
209
|
|
|
$keyInfo, |
210
|
|
|
1, |
211
|
|
|
'ds:Signature can hold a maximum of one ds:KeyInfo element.', |
212
|
|
|
TooManyElementsException::class, |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
$objects = DsObject::getChildrenOfClass($xml); |
216
|
|
|
|
217
|
|
|
return new static( |
218
|
|
|
array_pop($signedInfo), |
219
|
|
|
array_pop($signatureValue), |
220
|
|
|
empty($keyInfo) ? null : array_pop($keyInfo), |
221
|
|
|
$objects, |
222
|
|
|
$Id, |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Convert this Signature element to XML. |
229
|
|
|
* |
230
|
|
|
* @param \DOMElement|null $parent The element we should append this Signature element to. |
231
|
|
|
* @return \DOMElement |
232
|
|
|
*/ |
233
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
234
|
|
|
{ |
235
|
|
|
$e = $this->instantiateParentElement($parent); |
236
|
|
|
|
237
|
|
|
if ($this->Id !== null) { |
238
|
|
|
$e->setAttribute('Id', $this->Id); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$this->signedInfo->toXML($e); |
242
|
|
|
$this->signatureValue->toXML($e); |
243
|
|
|
|
244
|
|
|
if ($this->keyInfo !== null) { |
245
|
|
|
$this->keyInfo->toXML($e); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
foreach ($this->objects as $o) { |
249
|
|
|
$o->toXML($e); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $e; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|