|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleSAML\XMLSecurity; |
|
4
|
|
|
|
|
5
|
|
|
use DOMDocument; |
|
6
|
|
|
use DOMElement; |
|
7
|
|
|
use DOMNode; |
|
8
|
|
|
use SimpleSAML\Assert\Assert; |
|
9
|
|
|
use SimpleSAML\XML\Chunk; |
|
10
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
|
11
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
|
12
|
|
|
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory; |
|
13
|
|
|
use SimpleSAML\XMLSecurity\Backend\SignatureBackend; |
|
14
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
|
15
|
|
|
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
|
16
|
|
|
use SimpleSAML\XMLSecurity\Exception\NoSignatureFound; |
|
17
|
|
|
use SimpleSAML\XMLSecurity\Exception\RuntimeException; |
|
18
|
|
|
use SimpleSAML\XMLSecurity\Key; |
|
19
|
|
|
use SimpleSAML\XMLSecurity\Utils\Certificate as CertificateUtils; |
|
20
|
|
|
use SimpleSAML\XMLSecurity\Utils\Security as Sec; |
|
21
|
|
|
use SimpleSAML\XMLSecurity\Utils\XPath as XP; |
|
22
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\DigestMethod; |
|
23
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\DigestValue; |
|
24
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo; |
|
25
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Signature as Sig; |
|
26
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Transform; |
|
27
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate; |
|
28
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509Data; |
|
29
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509Digest; |
|
30
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerName; |
|
31
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial; |
|
32
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber; |
|
33
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class implementing XML digital signatures. |
|
37
|
|
|
* |
|
38
|
|
|
* @package SimpleSAML\XMLSecurity |
|
39
|
|
|
*/ |
|
40
|
|
|
class Signature |
|
41
|
|
|
{ |
|
42
|
|
|
/** @var array */ |
|
43
|
|
|
public array $idNS = []; |
|
44
|
|
|
|
|
45
|
|
|
/** @var array */ |
|
46
|
|
|
public array $idKeys = []; |
|
47
|
|
|
|
|
48
|
|
|
/** @var \SimpleSAML\XMLSecurity\Backend\SignatureBackend|null */ |
|
49
|
|
|
protected ?SignatureBackend $backend = null; |
|
50
|
|
|
|
|
51
|
|
|
/** @var \DOMElement */ |
|
52
|
|
|
protected DOMElement $root; |
|
53
|
|
|
|
|
54
|
|
|
/** @var \DOMElement|null */ |
|
55
|
|
|
protected ?DOMElement $sigNode = null; |
|
56
|
|
|
|
|
57
|
|
|
/** @var \DOMElement */ |
|
58
|
|
|
protected DOMElement $sigMethodNode; |
|
59
|
|
|
|
|
60
|
|
|
/** @var \DOMElement */ |
|
61
|
|
|
protected DOMElement $c14nMethodNode; |
|
62
|
|
|
|
|
63
|
|
|
/** @var \DOMElement */ |
|
64
|
|
|
protected DOMElement $sigInfoNode; |
|
65
|
|
|
|
|
66
|
|
|
/** @var \DOMElement|null */ |
|
67
|
|
|
protected ?DOMElement $objectNode = null; |
|
68
|
|
|
|
|
69
|
|
|
/** @var string */ |
|
70
|
|
|
protected string $signfo; |
|
71
|
|
|
|
|
72
|
|
|
/** @var string */ |
|
73
|
|
|
protected string $sigAlg; |
|
74
|
|
|
|
|
75
|
|
|
/** @var \DOMElement[] */ |
|
76
|
|
|
protected array $verifiedElements = []; |
|
77
|
|
|
|
|
78
|
|
|
/** @var string */ |
|
79
|
|
|
protected string $c14nMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS; |
|
80
|
|
|
|
|
81
|
|
|
/** @var string */ |
|
82
|
|
|
protected string $nsPrefix = 'ds:'; |
|
83
|
|
|
|
|
84
|
|
|
/** @var array */ |
|
85
|
|
|
protected array $algBlacklist = [ |
|
86
|
|
|
C::SIG_RSA_SHA1, |
|
87
|
|
|
C::SIG_HMAC_SHA1, |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
/** @var array */ |
|
91
|
|
|
protected array $references = []; |
|
92
|
|
|
|
|
93
|
|
|
/** @var bool */ |
|
94
|
|
|
protected bool $enveloping = false; |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Signature constructor. |
|
99
|
|
|
* |
|
100
|
|
|
* @param \DOMElement|string $root The DOM element or a string of data we want to sign. |
|
101
|
|
|
* @param \SimpleSAML\XMLSecurity\Backend\SignatureBackend|null $backend The backend to use to |
|
102
|
|
|
* generate or verify signatures. See individual algorithms for defaults. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function __construct($root, SignatureBackend $backend = null) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->backend = $backend; |
|
107
|
|
|
$this->initSignature(); |
|
108
|
|
|
|
|
109
|
|
|
if (is_string($root)) { |
|
110
|
|
|
$this->root = $this->addObject($root); |
|
111
|
|
|
$this->enveloping = true; |
|
112
|
|
|
} else { |
|
113
|
|
|
$this->root = $root; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Add an object element to the signature containing the given data. |
|
120
|
|
|
* |
|
121
|
|
|
* @param \DOMElement|string $data The data we want to envelope inside the signature. |
|
122
|
|
|
* @param string|null $mimetype An optional mime type to specify. |
|
123
|
|
|
* @param string|null $encoding An optional encoding to specify. |
|
124
|
|
|
* |
|
125
|
|
|
* @return \DOMElement The resulting object element added to the signature. |
|
126
|
|
|
*/ |
|
127
|
|
|
public function addObject($data, ?string $mimetype = null, ?string $encoding = null): DOMElement |
|
128
|
|
|
{ |
|
129
|
|
|
if ($this->objectNode === null) { |
|
130
|
|
|
$this->objectNode = $this->createElement('Object'); |
|
131
|
|
|
$this->sigNode->appendChild($this->objectNode); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (is_string($mimetype) && !empty($mimetype)) { |
|
135
|
|
|
$this->objectNode->setAttribute('MimeType', $mimetype); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if (is_string($encoding) && !empty($encoding)) { |
|
139
|
|
|
$this->objectNode->setAttribute('Encoding', $encoding); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if ($data instanceof DOMElement) { |
|
143
|
|
|
$this->objectNode->appendChild($this->sigNode->ownerDocument->importNode($data, true)); |
|
|
|
|
|
|
144
|
|
|
} else { |
|
145
|
|
|
$this->objectNode->appendChild($this->sigNode->ownerDocument->createTextNode($data)); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this->objectNode; |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Add a reference to a given node (an element or a document). |
|
154
|
|
|
* |
|
155
|
|
|
* @param \DOMNode $node A DOMElement that we want to sign, or a DOMDocument if we want to sign the entire document. |
|
156
|
|
|
* @param string $alg The identifier of a supported digest algorithm to use when processing this reference. |
|
157
|
|
|
* @param array $transforms An array containing a list of transforms that must be applied to the reference. |
|
158
|
|
|
* Optional. |
|
159
|
|
|
* @param array $options An array containing a set of options for this reference. Optional. Supported options are: |
|
160
|
|
|
* - prefix (string): the XML prefix used in the element being referenced. Defaults to none (no prefix used). |
|
161
|
|
|
* |
|
162
|
|
|
* - prefix_ns (string): the namespace associated with the given prefix. Defaults to none (no prefix used). |
|
163
|
|
|
* |
|
164
|
|
|
* - id_name (string): the name of the "id" attribute in the referenced element. Defaults to "Id". |
|
165
|
|
|
* |
|
166
|
|
|
* - force_uri (boolean): Whether to explicitly add a URI attribute to the reference when referencing a |
|
167
|
|
|
* DOMDocument or not. Defaults to true. If force_uri is false and $node is a DOMDocument, the URI attribute |
|
168
|
|
|
* will be completely omitted. |
|
169
|
|
|
* |
|
170
|
|
|
* - overwrite (boolean): Whether to overwrite the identifier existing in the element referenced with a new, |
|
171
|
|
|
* random one, or not. Defaults to true. |
|
172
|
|
|
* |
|
173
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $node is not |
|
174
|
|
|
* an instance of DOMDocument or DOMElement. |
|
175
|
|
|
*/ |
|
176
|
|
|
public function addReference(DOMNode $node, string $alg, array $transforms = [], array $options = []): void |
|
177
|
|
|
{ |
|
178
|
|
|
Assert::isInstanceOfAny( |
|
179
|
|
|
$node, |
|
180
|
|
|
[DOMDocument::class, DOMElement::class], |
|
181
|
|
|
'Only references to the DOM document or elements are allowed.' |
|
182
|
|
|
); |
|
183
|
|
|
|
|
184
|
|
|
$prefix = @$options['prefix'] ?: null; |
|
185
|
|
|
$prefixNS = @$options['prefix_ns'] ?: null; |
|
186
|
|
|
$idName = @$options['id_name'] ?: 'Id'; |
|
187
|
|
|
$attrName = $prefix ? $prefix . ':' . $idName : $idName; |
|
188
|
|
|
$forceURI = true; |
|
189
|
|
|
if (isset($options['force_uri'])) { |
|
190
|
|
|
$forceURI = $options['force_uri']; |
|
191
|
|
|
} |
|
192
|
|
|
$overwrite = true; |
|
193
|
|
|
if (isset($options['overwrite'])) { |
|
194
|
|
|
$overwrite = $options['overwrite']; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$reference = $this->createElement('Reference'); |
|
198
|
|
|
$this->sigInfoNode->appendChild($reference); |
|
199
|
|
|
|
|
200
|
|
|
// register reference |
|
201
|
|
|
$includeCommentNodes = false; |
|
202
|
|
|
if ($node instanceof DOMElement) { |
|
203
|
|
|
$uri = null; |
|
204
|
|
|
if (!$overwrite) { |
|
205
|
|
|
$uri = $prefixNS ? $node->getAttributeNS($prefixNS, $idName) : $node->getAttribute($idName); |
|
206
|
|
|
} |
|
207
|
|
|
if (empty($uri)) { |
|
208
|
|
|
$uri = Utils\Random::generateGUID(); |
|
209
|
|
|
$node->setAttributeNS($prefixNS, $attrName, $uri); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
if ( |
|
213
|
|
|
in_array(C::C14N_EXCLUSIVE_WITH_COMMENTS, $transforms) |
|
214
|
|
|
|| in_array(C::C14N_INCLUSIVE_WITH_COMMENTS, $transforms) |
|
215
|
|
|
) { |
|
216
|
|
|
$includeCommentNodes = true; |
|
217
|
|
|
$reference->setAttribute('URI', "#xpointer($attrName('$uri'))"); |
|
218
|
|
|
} else { |
|
219
|
|
|
$reference->setAttribute('URI', '#' . $uri); |
|
220
|
|
|
} |
|
221
|
|
|
} elseif ($forceURI) { |
|
222
|
|
|
// $node is a \DOMDocument, should add a reference to the root element (enveloped signature) |
|
223
|
|
|
if (in_array($this->c14nMethod, [C::C14N_INCLUSIVE_WITH_COMMENTS, C::C14N_EXCLUSIVE_WITH_COMMENTS])) { |
|
224
|
|
|
// if we want to use a C14N method that includes comments, the URI must be an xpointer |
|
225
|
|
|
$reference->setAttribute('URI', '#xpointer(/)'); |
|
226
|
|
|
} else { |
|
227
|
|
|
// C14N without comments, we can set an empty URI |
|
228
|
|
|
$reference->setAttribute('URI', ''); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
// apply and register transforms |
|
233
|
|
|
$transformList = $this->createElement('Transforms'); |
|
234
|
|
|
$reference->appendChild($transformList); |
|
235
|
|
|
|
|
236
|
|
|
if (!empty($transforms)) { |
|
237
|
|
|
foreach ($transforms as $transform) { |
|
238
|
|
|
if (is_array($transform) && !empty($transform[C::XPATH_URI]['query'])) { |
|
239
|
|
|
$t = new Transform(C::XPATH_URI, [new Chunk($transform[C::XPATH_URI]['query'])]); |
|
|
|
|
|
|
240
|
|
|
} else { |
|
241
|
|
|
$t = new Transform($transform); |
|
242
|
|
|
} |
|
243
|
|
|
$t->toXML($transformList); |
|
244
|
|
|
} |
|
245
|
|
|
} elseif (!empty($this->c14nMethod)) { |
|
246
|
|
|
$t = new Transform($this->c14nMethod); |
|
247
|
|
|
$t->toXML($transformList); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
$canonicalData = $this->processTransforms($reference, $node, $includeCommentNodes); |
|
251
|
|
|
$digest = $this->hash($alg, $canonicalData); |
|
252
|
|
|
|
|
253
|
|
|
$digestMethod = new DigestMethod($alg); |
|
254
|
|
|
$digestMethod->toXML($reference); |
|
255
|
|
|
|
|
256
|
|
|
$digestValue = new DigestValue($digest); |
|
257
|
|
|
$digestValue->toXML($reference); |
|
258
|
|
|
|
|
259
|
|
|
if (!in_array($node, $this->references)) { |
|
260
|
|
|
$this->references[] = $node; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Add a set of references to the signature. |
|
267
|
|
|
* |
|
268
|
|
|
* @param \DOMNode[] $nodes An array of DOMNode objects to be referred in the signature. |
|
269
|
|
|
* @param string $alg The identifier of the digest algorithm to use. |
|
270
|
|
|
* @param array $transforms An array of transforms to apply to each reference. |
|
271
|
|
|
* @param array $options An array of options. |
|
272
|
|
|
* |
|
273
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If any of the nodes in the $nodes |
|
274
|
|
|
* array is not an instance of DOMDocument or DOMElement. |
|
275
|
|
|
* |
|
276
|
|
|
* @see addReference() |
|
277
|
|
|
*/ |
|
278
|
|
|
public function addReferences(array $nodes, string $alg, array $transforms = [], $options = []): void |
|
279
|
|
|
{ |
|
280
|
|
|
foreach ($nodes as $node) { |
|
281
|
|
|
$this->addReference($node, $alg, $transforms, $options); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Attach one or more X509 certificates to the signature. |
|
288
|
|
|
* |
|
289
|
|
|
* @param \SimpleSAML\XMLSecurity\Key\X509Certificate[] $certs |
|
290
|
|
|
* An X509Certificate object or an array of them. |
|
291
|
|
|
* @param boolean $addSubject Whether to add the subject of the certificate or not. |
|
292
|
|
|
* @param string|false $digest A digest algorithm identifier if the digest of the certificate should be added. False |
|
293
|
|
|
* otherwise. |
|
294
|
|
|
* @param boolean $addIssuerSerial Whether to add the serial number of the issuer or not. |
|
295
|
|
|
* |
|
296
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $certs is not a |
|
297
|
|
|
* X509Certificate object or an array of them. |
|
298
|
|
|
*/ |
|
299
|
|
|
public function addX509Certificates( |
|
300
|
|
|
array $certs, |
|
301
|
|
|
bool $addSubject = false, |
|
302
|
|
|
$digest = false, |
|
303
|
|
|
bool $addIssuerSerial = false |
|
304
|
|
|
): void { |
|
305
|
|
|
Assert::allIsInstanceOf($certs, Key\X509Certificate::class); |
|
306
|
|
|
|
|
307
|
|
|
$certData = []; |
|
308
|
|
|
|
|
309
|
|
|
foreach ($certs as $cert) { |
|
310
|
|
|
$details = $cert->getCertificateDetails(); |
|
311
|
|
|
|
|
312
|
|
|
if ($addSubject && isset($details['subject'])) { |
|
313
|
|
|
// add subject |
|
314
|
|
|
$subjectNameValue = $details['subject']; |
|
315
|
|
|
if (is_array($subjectNameValue)) { |
|
316
|
|
|
$parts = []; |
|
317
|
|
|
foreach ($details['subject'] as $key => $value) { |
|
318
|
|
|
if (is_array($value)) { |
|
319
|
|
|
foreach ($value as $valueElement) { |
|
320
|
|
|
array_unshift($parts, $key . '=' . $valueElement); |
|
321
|
|
|
} |
|
322
|
|
|
} else { |
|
323
|
|
|
array_unshift($parts, $key . '=' . $value); |
|
324
|
|
|
} |
|
325
|
|
|
} |
|
326
|
|
|
$subjectNameValue = implode(',', $parts); |
|
327
|
|
|
} |
|
328
|
|
|
$certData[] = new X509SubjectName($subjectNameValue); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
if ($digest !== false) { |
|
332
|
|
|
// add certificate digest |
|
333
|
|
|
$fingerprint = base64_encode(hex2bin($cert->getRawThumbprint($digest))); |
|
334
|
|
|
$certData[] = new X509Digest($fingerprint, $digest); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
if ($addIssuerSerial && isset($details['issuer']) && isset($details['serialNumber'])) { |
|
338
|
|
|
$issuerName = CertificateUtils::parseIssuer($details['issuer']); |
|
339
|
|
|
|
|
340
|
|
|
$certData[] = new X509IssuerSerial( |
|
341
|
|
|
new X509IssuerName($issuerName), |
|
342
|
|
|
new X509SerialNumber($details['serialNumber']) |
|
343
|
|
|
); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
$certData[] = new X509Certificate(CertificateUtils::stripHeaders($cert->getCertificate())); |
|
347
|
|
|
} |
|
348
|
|
|
$keyInfo = new KeyInfo([new X509Data($certData)]); |
|
349
|
|
|
$keyInfoNode = $keyInfo->toXML(); |
|
350
|
|
|
|
|
351
|
|
|
if ($this->objectNode === null) { |
|
352
|
|
|
$this->sigNode->appendChild($this->sigNode->ownerDocument->importNode($keyInfoNode, true)); |
|
353
|
|
|
} else { |
|
354
|
|
|
$this->sigNode->insertBefore($keyInfoNode, $this->objectNode); |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Append a signature as the last child of the signed element. |
|
361
|
|
|
* |
|
362
|
|
|
* @return \DOMNode The appended signature. |
|
363
|
|
|
*/ |
|
364
|
|
|
public function append(): DOMNode |
|
365
|
|
|
{ |
|
366
|
|
|
return $this->insert(); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Use this signature as an enveloping signature, effectively adding the signed data to a ds:Object element. |
|
372
|
|
|
* |
|
373
|
|
|
* @param string|null $mimetype The mime type corresponding to the signed data. |
|
374
|
|
|
* @param string|null $encoding The encoding corresponding to the signed data. |
|
375
|
|
|
*/ |
|
376
|
|
|
public function envelop(string $mimetype = null, string $encoding = null): void |
|
377
|
|
|
{ |
|
378
|
|
|
$this->root = $this->addObject($this->root, $mimetype, $encoding); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Build a new XML digital signature from a given document or node. |
|
384
|
|
|
* |
|
385
|
|
|
* @param \DOMNode $node The DOMDocument or DOMElement that contains the signature. |
|
386
|
|
|
* |
|
387
|
|
|
* @return Signature A Signature object corresponding to the signature present in the given DOM document or element. |
|
388
|
|
|
* |
|
389
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $node is not |
|
390
|
|
|
* an instance of DOMDocument or DOMElement. |
|
391
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\NoSignatureFound If there is no signature in the $node. |
|
392
|
|
|
*/ |
|
393
|
|
|
public static function fromXML(DOMNode $node): Signature |
|
394
|
|
|
{ |
|
395
|
|
|
Assert::isInstanceOfAny( |
|
396
|
|
|
$node, |
|
397
|
|
|
[DOMDocument::class, DOMElement::class], |
|
398
|
|
|
'Signatures can only be created from DOM documents or elements' |
|
399
|
|
|
); |
|
400
|
|
|
|
|
401
|
|
|
$signature = self::findSignature($node); |
|
402
|
|
|
if ($node instanceof DOMDocument) { |
|
403
|
|
|
$node = $node->documentElement; |
|
404
|
|
|
} |
|
405
|
|
|
$dsig = new self($node); |
|
406
|
|
|
$dsig->setSignatureElement($signature); |
|
407
|
|
|
return $dsig; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* Obtain the list of currently blacklisted algorithms. |
|
413
|
|
|
* |
|
414
|
|
|
* Signatures using blacklisted algorithms cannot be created or verified. |
|
415
|
|
|
* |
|
416
|
|
|
* @return array An array containing the identifiers of the algorithms blacklisted currently. |
|
417
|
|
|
*/ |
|
418
|
|
|
public function getBlacklistedAlgorithms(): array |
|
419
|
|
|
{ |
|
420
|
|
|
return $this->algBlacklist; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* Get the list of namespaces to designate ID attributes. |
|
426
|
|
|
* |
|
427
|
|
|
* @return array An array of strings with the namespaces used in ID attributes. |
|
428
|
|
|
*/ |
|
429
|
|
|
public function getIdNamespaces(): array |
|
430
|
|
|
{ |
|
431
|
|
|
return $this->idNS; |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* Get a list of attributes used as an ID. |
|
437
|
|
|
* |
|
438
|
|
|
* @return array An array of strings with the attributes used as an ID. |
|
439
|
|
|
*/ |
|
440
|
|
|
public function getIdAttributes(): array |
|
441
|
|
|
{ |
|
442
|
|
|
return $this->idKeys; |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Get the root configured for this signature. |
|
448
|
|
|
* |
|
449
|
|
|
* This will be the signed element, whether that's a user-provided XML element or a ds:Object element containing |
|
450
|
|
|
* the actual data (which can in turn be either XML or not). |
|
451
|
|
|
* |
|
452
|
|
|
* @return \DOMElement The root element for this signature. |
|
453
|
|
|
*/ |
|
454
|
|
|
public function getRoot(): DOMElement |
|
455
|
|
|
{ |
|
456
|
|
|
return $this->root; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* Get the identifier of the algorithm used in this signature. |
|
462
|
|
|
* |
|
463
|
|
|
* @return string The identifier of the algorithm used in this signature. |
|
464
|
|
|
*/ |
|
465
|
|
|
public function getSignatureMethod(): string |
|
466
|
|
|
{ |
|
467
|
|
|
return $this->sigAlg; |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Get a list of elements verified by this signature. |
|
473
|
|
|
* |
|
474
|
|
|
* The elements in this list are referenced by the signature and the references verified to be correct. However, |
|
475
|
|
|
* this doesn't mean the signature is valid, only that the references were so. |
|
476
|
|
|
* |
|
477
|
|
|
* Note that the list returned will be empty unless verify() has been called before. |
|
478
|
|
|
* |
|
479
|
|
|
* @return \DOMElement[] A list of elements correctly referenced by this signature. An empty list of verify() has |
|
480
|
|
|
* not been called yet, or if the references couldn't be verified. |
|
481
|
|
|
*/ |
|
482
|
|
|
public function getVerifiedElements(): array |
|
483
|
|
|
{ |
|
484
|
|
|
return $this->verifiedElements; |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* Insert a signature as a child of the signed element, optionally before a given element. |
|
490
|
|
|
* |
|
491
|
|
|
* @param \DOMElement|false $before An optional DOM element the signature should be prepended to. |
|
492
|
|
|
* |
|
493
|
|
|
* @return \DOMNode The inserted signature. |
|
494
|
|
|
* |
|
495
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this signature is in enveloping mode. |
|
496
|
|
|
*/ |
|
497
|
|
|
public function insert($before = false): DOMNode |
|
498
|
|
|
{ |
|
499
|
|
|
Assert::false( |
|
500
|
|
|
$this->enveloping, |
|
501
|
|
|
'Cannot insert the signature in the object it is enveloping.', |
|
502
|
|
|
RuntimeException::class |
|
503
|
|
|
); |
|
504
|
|
|
|
|
505
|
|
|
$signature = $this->root->ownerDocument->importNode($this->sigNode, true); |
|
|
|
|
|
|
506
|
|
|
|
|
507
|
|
|
if ($before instanceof DOMElement) { |
|
508
|
|
|
return $this->root->insertBefore($signature, $before); |
|
509
|
|
|
} |
|
510
|
|
|
return $this->root->insertBefore($signature); |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Prepend a signature as the first child of the signed element. |
|
516
|
|
|
* |
|
517
|
|
|
* @return \DOMNode The prepended signature. |
|
518
|
|
|
*/ |
|
519
|
|
|
public function prepend(): DOMNode |
|
520
|
|
|
{ |
|
521
|
|
|
foreach ($this->root->childNodes as $child) { |
|
522
|
|
|
// look for the first child element, if any |
|
523
|
|
|
if ($child instanceof \DOMElement) { |
|
524
|
|
|
return $this->insert($child); |
|
525
|
|
|
} |
|
526
|
|
|
} |
|
527
|
|
|
return $this->append(); |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
|
|
531
|
|
|
/** |
|
532
|
|
|
* Set the backend to create or verify signatures. |
|
533
|
|
|
* |
|
534
|
|
|
* @param SignatureBackend $backend The SignatureBackend implementation to use. See individual algorithms for |
|
535
|
|
|
* details about the default backends used. |
|
536
|
|
|
*/ |
|
537
|
|
|
public function setBackend(SignatureBackend $backend): void |
|
538
|
|
|
{ |
|
539
|
|
|
$this->backend = $backend; |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
|
|
543
|
|
|
/** |
|
544
|
|
|
* Set the list of currently blacklisted algorithms. |
|
545
|
|
|
* |
|
546
|
|
|
* Signatures using blacklisted algorithms cannot be created or verified. |
|
547
|
|
|
* |
|
548
|
|
|
* @param array $algs An array containing the identifiers of the algorithms to blacklist. |
|
549
|
|
|
*/ |
|
550
|
|
|
public function setBlacklistedAlgorithms(array $algs): void |
|
551
|
|
|
{ |
|
552
|
|
|
$this->algBlacklist = $algs; |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
|
|
556
|
|
|
/** |
|
557
|
|
|
* Set the canonicalization method used in this signature. |
|
558
|
|
|
* |
|
559
|
|
|
* Note that exclusive canonicalization without comments is used by default, so it's not necessary to call |
|
560
|
|
|
* setCanonicalizationMethod() if that canonicalization method is desired. |
|
561
|
|
|
* |
|
562
|
|
|
* @param string $method The identifier of the canonicalization method to use. |
|
563
|
|
|
* |
|
564
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $method is not a valid |
|
565
|
|
|
* identifier of a supported canonicalization method. |
|
566
|
|
|
*/ |
|
567
|
|
|
public function setCanonicalizationMethod(string $method): void |
|
568
|
|
|
{ |
|
569
|
|
|
Assert::oneOf( |
|
570
|
|
|
$method, |
|
571
|
|
|
[ |
|
572
|
|
|
C::C14N_EXCLUSIVE_WITH_COMMENTS, |
|
573
|
|
|
C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, |
|
574
|
|
|
C::C14N_INCLUSIVE_WITH_COMMENTS, |
|
575
|
|
|
C::C14N_INCLUSIVE_WITHOUT_COMMENTS |
|
576
|
|
|
], |
|
577
|
|
|
'Invalid canonicalization method', |
|
578
|
|
|
InvalidArgumentException::class |
|
579
|
|
|
); |
|
580
|
|
|
|
|
581
|
|
|
$this->c14nMethod = $method; |
|
582
|
|
|
$this->c14nMethodNode->setAttribute('Algorithm', $method); |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
|
|
586
|
|
|
/** |
|
587
|
|
|
* Set the encoding for the signed contents in an enveloping signature. |
|
588
|
|
|
* |
|
589
|
|
|
* @param string $encoding The encoding used in the signed contents. |
|
590
|
|
|
* |
|
591
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this is not an enveloping signature. |
|
592
|
|
|
*/ |
|
593
|
|
|
public function setEncoding(string $encoding): void |
|
594
|
|
|
{ |
|
595
|
|
|
Assert::true( |
|
596
|
|
|
$this->enveloping, |
|
597
|
|
|
'Cannot set the encoding for non-enveloping signatures.', |
|
598
|
|
|
RuntimeException::class |
|
599
|
|
|
); |
|
600
|
|
|
|
|
601
|
|
|
$this->root->setAttribute('Encoding', $encoding); |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
|
|
605
|
|
|
/** |
|
606
|
|
|
* Set a list of attributes used as an ID. |
|
607
|
|
|
* |
|
608
|
|
|
* @param array $keys An array of strings with the attributes used as an ID. |
|
609
|
|
|
*/ |
|
610
|
|
|
public function setIdAttributes(array $keys): void |
|
611
|
|
|
{ |
|
612
|
|
|
$this->idKeys = $keys; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
|
|
616
|
|
|
/** |
|
617
|
|
|
* Set the list of namespaces to designate ID attributes. |
|
618
|
|
|
* |
|
619
|
|
|
* @param array $namespaces An array of strings with the namespaces used in ID attributes. |
|
620
|
|
|
*/ |
|
621
|
|
|
public function setIdNamespaces(array $namespaces): void |
|
622
|
|
|
{ |
|
623
|
|
|
$this->idNS = $namespaces; |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
|
|
627
|
|
|
/** |
|
628
|
|
|
* Set the mime type for the signed contents in an enveloping signature. |
|
629
|
|
|
* |
|
630
|
|
|
* @param string $mimetype The mime type of the signed contents. |
|
631
|
|
|
* |
|
632
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this is not an enveloping signature. |
|
633
|
|
|
*/ |
|
634
|
|
|
public function setMimeType(string $mimetype): void |
|
635
|
|
|
{ |
|
636
|
|
|
Assert::true( |
|
637
|
|
|
$this->enveloping, |
|
638
|
|
|
'Cannot set the mime type for non-enveloping signatures.', |
|
639
|
|
|
RuntimeException::class |
|
640
|
|
|
); |
|
641
|
|
|
|
|
642
|
|
|
$this->root->setAttribute('MimeType', $mimetype); |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
|
|
646
|
|
|
/** |
|
647
|
|
|
* Set the signature element to a given one, and initialize the signature from there. |
|
648
|
|
|
* |
|
649
|
|
|
* @param \DOMElement $element A DOM element containing an XML signature. |
|
650
|
|
|
* |
|
651
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException If the element does not correspond to an XML |
|
652
|
|
|
* signature or it is malformed (e.g. there are missing mandatory elements or attributes). |
|
653
|
|
|
*/ |
|
654
|
|
|
public function setSignatureElement(DOMElement $element): void |
|
655
|
|
|
{ |
|
656
|
|
|
Assert::same($element->localName, 'Signature', InvalidDOMElementException::class); |
|
657
|
|
|
Assert::same($element->namespaceURI, Sig::NS, InvalidDOMElementException::class); |
|
658
|
|
|
|
|
659
|
|
|
$this->sigNode = $element; |
|
660
|
|
|
|
|
661
|
|
|
$xp = XP::getXPath($this->sigNode->ownerDocument); |
|
|
|
|
|
|
662
|
|
|
|
|
663
|
|
|
$signedInfoNodes = $xp->query('./ds:SignedInfo', $this->sigNode); |
|
664
|
|
|
|
|
665
|
|
|
Assert::minCount( |
|
666
|
|
|
$signedInfoNodes, |
|
667
|
|
|
1, |
|
668
|
|
|
'There is no SignedInfo element in the signature', |
|
669
|
|
|
RuntimeException::class |
|
670
|
|
|
); |
|
671
|
|
|
$this->sigInfoNode = $signedInfoNodes->item(0); |
|
672
|
|
|
|
|
673
|
|
|
|
|
674
|
|
|
$this->sigAlg = $xp->evaluate('string(./ds:SignedInfo/ds:SignatureMethod/@Algorithm)', $this->sigNode); |
|
675
|
|
|
Assert::stringNotEmpty($this->sigAlg, 'Unable to determine SignatureMethod', RuntimeException::class); |
|
676
|
|
|
|
|
677
|
|
|
$c14nMethodNodes = $xp->query('./ds:CanonicalizationMethod', $this->sigInfoNode); |
|
678
|
|
|
Assert::minCount( |
|
679
|
|
|
$c14nMethodNodes, |
|
680
|
|
|
1, |
|
681
|
|
|
'There is no CanonicalizationMethod in the signature', |
|
682
|
|
|
RuntimeException::class |
|
683
|
|
|
); |
|
684
|
|
|
|
|
685
|
|
|
$this->c14nMethodNode = $c14nMethodNodes->item(0); |
|
686
|
|
|
if (!$this->c14nMethodNode->hasAttribute('Algorithm')) { |
|
687
|
|
|
throw new RuntimeException('CanonicalizationMethod missing required Algorithm attribute'); |
|
688
|
|
|
} |
|
689
|
|
|
$this->c14nMethod = $this->c14nMethodNode->getAttribute('Algorithm'); |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
|
|
693
|
|
|
/** |
|
694
|
|
|
* Sign the document or element. |
|
695
|
|
|
* |
|
696
|
|
|
* This method will finish the signature process. It will create an XML signature valid for document or elements |
|
697
|
|
|
* specified previously with addReference() or addReferences(). If none of those methods have been called previous |
|
698
|
|
|
* to calling sign() (there are no references in the signature), the $root passed during construction of the |
|
699
|
|
|
* Signature object will be referenced automatically. |
|
700
|
|
|
* |
|
701
|
|
|
* @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use for signing. Bear in mind that the type of |
|
702
|
|
|
* this key must be compatible with the types of key accepted by the algorithm specified in $alg. |
|
703
|
|
|
* @param string $alg The identifier of the signature algorithm to use. See \SimpleSAML\XMLSecurity\Constants. |
|
704
|
|
|
* @param bool $appendToNode Whether to append the signature as the last child of the root element or not. |
|
705
|
|
|
* |
|
706
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $appendToNode is true and |
|
707
|
|
|
* this is an enveloping signature. |
|
708
|
|
|
*/ |
|
709
|
|
|
public function sign(Key\AbstractKey $key, string $alg, bool $appendToNode = false): void |
|
710
|
|
|
{ |
|
711
|
|
|
Assert::false( |
|
712
|
|
|
($this->enveloping && $appendToNode), |
|
713
|
|
|
'Cannot append the signature, we are in enveloping mode.', |
|
714
|
|
|
InvalidArgumentException::class |
|
715
|
|
|
); |
|
716
|
|
|
|
|
717
|
|
|
$this->sigMethodNode->setAttribute('Algorithm', $alg); |
|
718
|
|
|
$factory = new SignatureAlgorithmFactory($this->algBlacklist); |
|
719
|
|
|
$signer = $factory->getAlgorithm($alg, $key); |
|
720
|
|
|
if ($this->backend !== null) { |
|
721
|
|
|
$signer->setBackend($this->backend); |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
if (empty($this->references)) { |
|
725
|
|
|
// no references have been added, ref root |
|
726
|
|
|
$transforms = []; |
|
727
|
|
|
if (!$this->enveloping) { |
|
728
|
|
|
$transforms[] = C::XMLDSIG_ENVELOPED; |
|
729
|
|
|
} |
|
730
|
|
|
$this->addReference($this->root->ownerDocument, $signer->getDigest(), $transforms, []); |
|
|
|
|
|
|
731
|
|
|
} |
|
732
|
|
|
|
|
733
|
|
|
if ($appendToNode) { |
|
734
|
|
|
$this->sigNode = $this->append(); |
|
735
|
|
|
} elseif (in_array($this->c14nMethod, [C::C14N_INCLUSIVE_WITHOUT_COMMENTS, C::C14N_INCLUSIVE_WITH_COMMENTS])) { |
|
736
|
|
|
// append Signature to root node for inclusive canonicalization |
|
737
|
|
|
$restoreSigNode = $this->sigNode; |
|
738
|
|
|
$this->sigNode = $this->prepend(); |
|
739
|
|
|
} |
|
740
|
|
|
|
|
741
|
|
|
$sigValue = base64_encode($signer->sign($this->canonicalizeData($this->sigInfoNode, $this->c14nMethod))); |
|
742
|
|
|
|
|
743
|
|
|
// remove Signature from node if we added it for c14n |
|
744
|
|
|
if ( |
|
745
|
|
|
!$appendToNode && |
|
746
|
|
|
in_array($this->c14nMethod, [C::C14N_INCLUSIVE_WITHOUT_COMMENTS, C::C14N_INCLUSIVE_WITH_COMMENTS]) |
|
747
|
|
|
) { // remove from root in case we added it for inclusive canonicalization |
|
748
|
|
|
$this->root->removeChild($this->root->lastChild); |
|
|
|
|
|
|
749
|
|
|
/** @var \DOMElement $restoreSigNode */ |
|
750
|
|
|
$this->sigNode = $restoreSigNode; |
|
|
|
|
|
|
751
|
|
|
} |
|
752
|
|
|
|
|
753
|
|
|
$sigValueNode = $this->createElement('SignatureValue', $sigValue); |
|
754
|
|
|
if ($this->sigInfoNode->nextSibling) { |
|
755
|
|
|
$this->sigInfoNode->nextSibling->parentNode->insertBefore($sigValueNode, $this->sigInfoNode->nextSibling); |
|
|
|
|
|
|
756
|
|
|
} else { |
|
757
|
|
|
$this->sigNode->appendChild($sigValueNode); |
|
758
|
|
|
} |
|
759
|
|
|
} |
|
760
|
|
|
|
|
761
|
|
|
|
|
762
|
|
|
/** |
|
763
|
|
|
* Verify this signature with a given key. |
|
764
|
|
|
* |
|
765
|
|
|
* @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use to verify this signature. |
|
766
|
|
|
* |
|
767
|
|
|
* @return bool True if the signature can be verified with $key, false otherwise. |
|
768
|
|
|
* |
|
769
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there is no SignatureValue in |
|
770
|
|
|
* the signature, or we couldn't verify all the references. |
|
771
|
|
|
*/ |
|
772
|
|
|
public function verify(Key\AbstractKey $key): bool |
|
773
|
|
|
{ |
|
774
|
|
|
$xp = XP::getXPath($this->sigNode->ownerDocument); |
|
|
|
|
|
|
775
|
|
|
$sigval = $xp->evaluate('string(./ds:SignatureValue)', $this->sigNode); |
|
776
|
|
|
if (empty($sigval)) { |
|
777
|
|
|
throw new RuntimeException('Unable to locate SignatureValue'); |
|
778
|
|
|
} |
|
779
|
|
|
|
|
780
|
|
|
$siginfo = $this->canonicalizeData($this->sigInfoNode, $this->c14nMethod); |
|
781
|
|
|
if (!$this->validateReferences()) { |
|
782
|
|
|
throw new RuntimeException('Unable to verify all references'); |
|
783
|
|
|
} |
|
784
|
|
|
|
|
785
|
|
|
$factory = new SignatureAlgorithmFactory($this->algBlacklist); |
|
786
|
|
|
$alg = $factory->getAlgorithm($this->sigAlg, $key); |
|
787
|
|
|
if ($this->backend !== null) { |
|
788
|
|
|
$alg->setBackend($this->backend); |
|
789
|
|
|
} |
|
790
|
|
|
return $alg->verify($siginfo, base64_decode($sigval)); |
|
791
|
|
|
} |
|
792
|
|
|
|
|
793
|
|
|
|
|
794
|
|
|
/** |
|
795
|
|
|
* Canonicalize any given node. |
|
796
|
|
|
* |
|
797
|
|
|
* @param \DOMNode $node The DOM node that needs canonicalization. |
|
798
|
|
|
* @param string $c14nMethod The identifier of the canonicalization algorithm to use. |
|
799
|
|
|
* See \SimpleSAML\XMLSecurity\Constants. |
|
800
|
|
|
* @param array|null $xpaths An array of xpaths to filter the nodes by. Defaults to null (no filters). |
|
801
|
|
|
* @param array|null $prefixes An array of namespace prefixes to filter the nodes by. Defaults to null (no filters). |
|
802
|
|
|
* |
|
803
|
|
|
* @return string The canonical representation of the given DOM node, according to the algorithm requested. |
|
804
|
|
|
*/ |
|
805
|
|
|
protected function canonicalizeData( |
|
806
|
|
|
DOMNode $node, |
|
807
|
|
|
string $c14nMethod, |
|
808
|
|
|
array $xpaths = null, |
|
809
|
|
|
array $prefixes = null |
|
810
|
|
|
): string { |
|
811
|
|
|
$exclusive = false; |
|
812
|
|
|
$withComments = false; |
|
813
|
|
|
switch ($c14nMethod) { |
|
814
|
|
|
case C::C14N_EXCLUSIVE_WITH_COMMENTS: |
|
815
|
|
|
case C::C14N_INCLUSIVE_WITH_COMMENTS: |
|
816
|
|
|
$withComments = true; |
|
817
|
|
|
} |
|
818
|
|
|
switch ($c14nMethod) { |
|
819
|
|
|
case C::C14N_EXCLUSIVE_WITH_COMMENTS: |
|
820
|
|
|
case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS: |
|
821
|
|
|
$exclusive = true; |
|
822
|
|
|
} |
|
823
|
|
|
|
|
824
|
|
|
if ( |
|
825
|
|
|
is_null($xpaths) |
|
826
|
|
|
&& ($node->ownerDocument !== null) |
|
827
|
|
|
&& $node->isSameNode($node->ownerDocument->documentElement) |
|
828
|
|
|
) { |
|
829
|
|
|
// check for any PI or comments as they would have been excluded |
|
830
|
|
|
$element = $node; |
|
831
|
|
|
while ($refNode = $element->previousSibling) { |
|
832
|
|
|
if ( |
|
833
|
|
|
(($refNode->nodeType === XML_COMMENT_NODE) && $withComments) |
|
834
|
|
|
|| $refNode->nodeType === XML_PI_NODE |
|
835
|
|
|
) { |
|
836
|
|
|
break; |
|
837
|
|
|
} |
|
838
|
|
|
$element = $refNode; |
|
839
|
|
|
} |
|
840
|
|
|
if ($refNode == null) { |
|
841
|
|
|
$node = $node->ownerDocument; |
|
842
|
|
|
} |
|
843
|
|
|
} |
|
844
|
|
|
|
|
845
|
|
|
return $node->C14N($exclusive, $withComments, $xpaths, $prefixes); |
|
846
|
|
|
} |
|
847
|
|
|
|
|
848
|
|
|
|
|
849
|
|
|
/** |
|
850
|
|
|
* Create a new element in this signature. |
|
851
|
|
|
* |
|
852
|
|
|
* @param string $name The name of this element. |
|
853
|
|
|
* @param string|null $content The text contents of the element, or null if it is not supposed to have any text |
|
854
|
|
|
* contents. Defaults to null. |
|
855
|
|
|
* @param string $ns The namespace the new element must be created under. Defaults to the standard XMLDSIG |
|
856
|
|
|
* namespace. |
|
857
|
|
|
* |
|
858
|
|
|
* @return \DOMElement A new DOM element with the given name. |
|
859
|
|
|
*/ |
|
860
|
|
|
protected function createElement( |
|
861
|
|
|
string $name, |
|
862
|
|
|
string $content = null, |
|
863
|
|
|
string $ns = C::XMLDSIGNS |
|
864
|
|
|
): DOMElement { |
|
865
|
|
|
if ($this->sigNode === null) { |
|
866
|
|
|
// initialize signature |
|
867
|
|
|
$doc = DOMDocumentFactory::create(); |
|
868
|
|
|
} else { |
|
869
|
|
|
$doc = $this->sigNode->ownerDocument; |
|
870
|
|
|
} |
|
871
|
|
|
|
|
872
|
|
|
$nsPrefix = $this->nsPrefix; |
|
873
|
|
|
|
|
874
|
|
|
if ($content !== null) { |
|
875
|
|
|
return $doc->createElementNS($ns, $nsPrefix . $name, $content); |
|
876
|
|
|
} |
|
877
|
|
|
|
|
878
|
|
|
return $doc->createElementNS($ns, $nsPrefix . $name); |
|
879
|
|
|
} |
|
880
|
|
|
|
|
881
|
|
|
|
|
882
|
|
|
/** |
|
883
|
|
|
* Find a signature from a given node. |
|
884
|
|
|
* |
|
885
|
|
|
* @param \DOMNode $node A DOMElement node where a signature is expected as a child (enveloped) or a DOMDocument |
|
886
|
|
|
* node to search for document signatures (one single reference with an empty URI). |
|
887
|
|
|
* |
|
888
|
|
|
* @return \DOMElement The signature element. |
|
889
|
|
|
* |
|
890
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there is no DOMDocument element available. |
|
891
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\NoSignatureFound If no signature is found. |
|
892
|
|
|
*/ |
|
893
|
|
|
protected static function findSignature(DOMNode $node): DOMElement |
|
894
|
|
|
{ |
|
895
|
|
|
$doc = $node instanceof DOMDocument ? $node : $node->ownerDocument; |
|
896
|
|
|
|
|
897
|
|
|
Assert::notNull($doc, 'Cannot search for signatures, no DOM document available', RuntimeException::class); |
|
898
|
|
|
|
|
899
|
|
|
$xp = XP::getXPath($doc); |
|
|
|
|
|
|
900
|
|
|
$nodeset = $xp->query('./ds:Signature', $node); |
|
901
|
|
|
|
|
902
|
|
|
if ($nodeset->length === 0) { |
|
903
|
|
|
throw new NoSignatureFound(); |
|
904
|
|
|
} |
|
905
|
|
|
return $nodeset->item(0); |
|
|
|
|
|
|
906
|
|
|
} |
|
907
|
|
|
|
|
908
|
|
|
|
|
909
|
|
|
/** |
|
910
|
|
|
* Compute the hash for some data with a given algorithm. |
|
911
|
|
|
* |
|
912
|
|
|
* @param string $alg The identifier of the algorithm to use. |
|
913
|
|
|
* @param string $data The data to digest. |
|
914
|
|
|
* @param bool $encode Whether to bas64-encode the result or not. Defaults to true. |
|
915
|
|
|
* |
|
916
|
|
|
* @return string The (binary or base64-encoded) digest corresponding to the given data. |
|
917
|
|
|
* |
|
918
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $alg is not a valid |
|
919
|
|
|
* identifier of a supported digest algorithm. |
|
920
|
|
|
*/ |
|
921
|
|
|
protected function hash(string $alg, string $data, bool $encode = true): string |
|
922
|
|
|
{ |
|
923
|
|
|
Assert::keyExists( |
|
924
|
|
|
C::$DIGEST_ALGORITHMS, |
|
925
|
|
|
$alg, |
|
926
|
|
|
'Unsupported digest method "%s"', |
|
927
|
|
|
InvalidArgumentException::class |
|
928
|
|
|
); |
|
929
|
|
|
|
|
930
|
|
|
$digest = hash(C::$DIGEST_ALGORITHMS[$alg], $data, true); |
|
931
|
|
|
return $encode ? base64_encode($digest) : $digest; |
|
932
|
|
|
} |
|
933
|
|
|
|
|
934
|
|
|
|
|
935
|
|
|
/** |
|
936
|
|
|
* Initialize the basic structure of a signature from scratch. |
|
937
|
|
|
* |
|
938
|
|
|
*/ |
|
939
|
|
|
protected function initSignature(): void |
|
940
|
|
|
{ |
|
941
|
|
|
$this->sigNode = $this->createElement('Signature'); |
|
942
|
|
|
$this->sigInfoNode = $this->createElement('SignedInfo'); |
|
943
|
|
|
$this->c14nMethodNode = $this->createElement('CanonicalizationMethod'); |
|
944
|
|
|
$this->c14nMethodNode->setAttribute('Algorithm', $this->c14nMethod); |
|
945
|
|
|
$this->sigMethodNode = $this->createElement('SignatureMethod'); |
|
946
|
|
|
|
|
947
|
|
|
$this->sigInfoNode->appendChild($this->c14nMethodNode); |
|
948
|
|
|
$this->sigInfoNode->appendChild($this->sigMethodNode); |
|
949
|
|
|
$this->sigNode->appendChild($this->sigInfoNode); |
|
950
|
|
|
$this->sigNode->ownerDocument->appendChild($this->sigNode); |
|
951
|
|
|
} |
|
952
|
|
|
|
|
953
|
|
|
|
|
954
|
|
|
/** |
|
955
|
|
|
* Process a given reference, by looking for it, processing the specified transforms, canonicalizing the result |
|
956
|
|
|
* and comparing its corresponding digest. |
|
957
|
|
|
* |
|
958
|
|
|
* Verified references will be stored in the "verifiedElements" property. |
|
959
|
|
|
* |
|
960
|
|
|
* @param \DOMElement $ref The ds:Reference element to process. |
|
961
|
|
|
* |
|
962
|
|
|
* @return bool True if the digest of the processed reference matches the one given, false otherwise. |
|
963
|
|
|
* |
|
964
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If the referenced element is missing or |
|
965
|
|
|
* the reference points to an external document. |
|
966
|
|
|
* |
|
967
|
|
|
* @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel |
|
968
|
|
|
*/ |
|
969
|
|
|
protected function processReference(DOMElement $ref): bool |
|
970
|
|
|
{ |
|
971
|
|
|
/* |
|
972
|
|
|
* Depending on the URI, we may need to remove comments during canonicalization. |
|
973
|
|
|
* See: http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel |
|
974
|
|
|
*/ |
|
975
|
|
|
$includeCommentNodes = true; |
|
976
|
|
|
$dataObject = $ref->ownerDocument; |
|
977
|
|
|
if ($ref->hasAttribute("URI")) { |
|
978
|
|
|
$uri = $ref->getAttribute('URI'); |
|
979
|
|
|
if (empty($uri)) { |
|
980
|
|
|
// this reference identifies the enclosing XML, it should not include comments |
|
981
|
|
|
$includeCommentNodes = false; |
|
982
|
|
|
} |
|
983
|
|
|
$arUrl = parse_url($uri); |
|
984
|
|
|
if (empty($arUrl['path'])) { |
|
985
|
|
|
if ($identifier = @$arUrl['fragment']) { |
|
986
|
|
|
/* |
|
987
|
|
|
* This reference identifies a node with the given ID by using a URI on the form '#identifier'. |
|
988
|
|
|
* This should not include comments. |
|
989
|
|
|
*/ |
|
990
|
|
|
$includeCommentNodes = false; |
|
991
|
|
|
|
|
992
|
|
|
$xp = XP::getXPath($ref->ownerDocument); |
|
|
|
|
|
|
993
|
|
|
if ($this->idNS && is_array($this->idNS)) { |
|
|
|
|
|
|
994
|
|
|
foreach ($this->idNS as $nspf => $ns) { |
|
995
|
|
|
$xp->registerNamespace($nspf, $ns); |
|
996
|
|
|
} |
|
997
|
|
|
} |
|
998
|
|
|
$iDlist = '@Id="' . $identifier . '"'; |
|
999
|
|
|
if (is_array($this->idKeys)) { |
|
|
|
|
|
|
1000
|
|
|
foreach ($this->idKeys as $idKey) { |
|
1001
|
|
|
$iDlist .= " or @$idKey='$identifier'"; |
|
1002
|
|
|
} |
|
1003
|
|
|
} |
|
1004
|
|
|
$query = '//*[' . $iDlist . ']'; |
|
1005
|
|
|
$dataObject = $xp->query($query)->item(0); |
|
1006
|
|
|
if ($dataObject === null) { |
|
1007
|
|
|
throw new RuntimeException('Reference not found'); |
|
1008
|
|
|
} |
|
1009
|
|
|
} |
|
1010
|
|
|
} else { |
|
1011
|
|
|
throw new RuntimeException('Processing of external documents is not supported'); |
|
1012
|
|
|
} |
|
1013
|
|
|
} else { |
|
1014
|
|
|
// this reference identifies the root node with an empty URI, it should not include comments |
|
1015
|
|
|
$includeCommentNodes = false; |
|
1016
|
|
|
} |
|
1017
|
|
|
|
|
1018
|
|
|
$data = $this->processTransforms($ref, $dataObject, $includeCommentNodes); |
|
1019
|
|
|
if (!$this->validateDigest($ref, $data)) { |
|
1020
|
|
|
return false; |
|
1021
|
|
|
} |
|
1022
|
|
|
|
|
1023
|
|
|
// parse the canonicalized reference... |
|
1024
|
|
|
$doc = DOMDocumentFactory::create(); |
|
1025
|
|
|
$doc->loadXML($data); |
|
1026
|
|
|
$dataObject = $doc->documentElement; |
|
1027
|
|
|
|
|
1028
|
|
|
// ... and add it to the list of verified elements |
|
1029
|
|
|
if (!empty($identifier)) { |
|
1030
|
|
|
$this->verifiedElements[$identifier] = $dataObject; |
|
1031
|
|
|
} else { |
|
1032
|
|
|
$this->verifiedElements[] = $dataObject; |
|
1033
|
|
|
} |
|
1034
|
|
|
|
|
1035
|
|
|
return true; |
|
1036
|
|
|
} |
|
1037
|
|
|
|
|
1038
|
|
|
|
|
1039
|
|
|
/** |
|
1040
|
|
|
* Process all transforms specified by a given Reference element. |
|
1041
|
|
|
* |
|
1042
|
|
|
* @param \DOMElement $ref The Reference element. |
|
1043
|
|
|
* @param mixed $data The data referenced. |
|
1044
|
|
|
* @param bool $includeCommentNodes Whether to allow canonicalization with comments or not. |
|
1045
|
|
|
* |
|
1046
|
|
|
* @return string The canonicalized data after applying all transforms specified by $ref. |
|
1047
|
|
|
* |
|
1048
|
|
|
* @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel |
|
1049
|
|
|
*/ |
|
1050
|
|
|
protected function processTransforms(DOMElement $ref, $data, bool $includeCommentNodes = false): string |
|
1051
|
|
|
{ |
|
1052
|
|
|
if (!($data instanceof DOMNode)) { |
|
1053
|
|
|
return $data; |
|
1054
|
|
|
} |
|
1055
|
|
|
|
|
1056
|
|
|
$xp = XP::getXPath($ref->ownerDocument); |
|
|
|
|
|
|
1057
|
|
|
$transforms = $xp->query('./ds:Transforms/ds:Transform', $ref); |
|
1058
|
|
|
$canonicalMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS; |
|
1059
|
|
|
$arXPath = null; |
|
1060
|
|
|
$prefixList = null; |
|
1061
|
|
|
foreach ($transforms as $transform) { |
|
1062
|
|
|
/** @var \DOMElement $transform */ |
|
1063
|
|
|
$algorithm = $transform->getAttribute("Algorithm"); |
|
1064
|
|
|
switch ($algorithm) { |
|
1065
|
|
|
case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS: |
|
1066
|
|
|
case C::C14N_EXCLUSIVE_WITH_COMMENTS: |
|
1067
|
|
|
if (!$includeCommentNodes) { |
|
1068
|
|
|
// remove comment nodes by forcing it to use a canonicalization without comments |
|
1069
|
|
|
$canonicalMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS; |
|
1070
|
|
|
} else { |
|
1071
|
|
|
$canonicalMethod = $algorithm; |
|
1072
|
|
|
} |
|
1073
|
|
|
|
|
1074
|
|
|
$node = $transform->firstChild; |
|
1075
|
|
|
while ($node) { |
|
1076
|
|
|
if ($node->localName === 'InclusiveNamespaces') { |
|
1077
|
|
|
if ($pfx = $node->getAttribute('PrefixList')) { |
|
1078
|
|
|
$arpfx = []; |
|
1079
|
|
|
$pfxlist = explode(" ", $pfx); |
|
1080
|
|
|
foreach ($pfxlist as $pfx) { |
|
1081
|
|
|
$val = trim($pfx); |
|
1082
|
|
|
if (! empty($val)) { |
|
1083
|
|
|
$arpfx[] = $val; |
|
1084
|
|
|
} |
|
1085
|
|
|
} |
|
1086
|
|
|
if (count($arpfx) > 0) { |
|
1087
|
|
|
$prefixList = $arpfx; |
|
1088
|
|
|
} |
|
1089
|
|
|
} |
|
1090
|
|
|
break; |
|
1091
|
|
|
} |
|
1092
|
|
|
$node = $node->nextSibling; |
|
1093
|
|
|
} |
|
1094
|
|
|
break; |
|
1095
|
|
|
case C::C14N_INCLUSIVE_WITHOUT_COMMENTS: |
|
1096
|
|
|
case C::C14N_INCLUSIVE_WITH_COMMENTS: |
|
1097
|
|
|
if (!$includeCommentNodes) { |
|
1098
|
|
|
// remove comment nodes by forcing it to use a canonicalization without comments |
|
1099
|
|
|
$canonicalMethod = C::C14N_INCLUSIVE_WITHOUT_COMMENTS; |
|
1100
|
|
|
} else { |
|
1101
|
|
|
$canonicalMethod = $algorithm; |
|
1102
|
|
|
} |
|
1103
|
|
|
|
|
1104
|
|
|
break; |
|
1105
|
|
|
case C::XPATH_URI: |
|
1106
|
|
|
$node = $transform->firstChild; |
|
1107
|
|
|
while ($node) { |
|
1108
|
|
|
if ($node->localName == 'XPath') { |
|
1109
|
|
|
$arXPath = []; |
|
1110
|
|
|
$arXPath['query'] = '(.//. | .//@* | .//namespace::*)[' . $node->nodeValue . ']'; |
|
1111
|
|
|
$arXpath['namespaces'] = []; |
|
1112
|
|
|
$nslist = $xp->query('./namespace::*', $node); |
|
1113
|
|
|
foreach ($nslist as $nsnode) { |
|
1114
|
|
|
if ($nsnode->localName != "xml") { |
|
1115
|
|
|
$arXPath['namespaces'][$nsnode->localName] = $nsnode->nodeValue; |
|
1116
|
|
|
} |
|
1117
|
|
|
} |
|
1118
|
|
|
break; |
|
1119
|
|
|
} |
|
1120
|
|
|
$node = $node->nextSibling; |
|
1121
|
|
|
} |
|
1122
|
|
|
break; |
|
1123
|
|
|
} |
|
1124
|
|
|
} |
|
1125
|
|
|
|
|
1126
|
|
|
return $this->canonicalizeData($data, $canonicalMethod, $arXPath, $prefixList); |
|
1127
|
|
|
} |
|
1128
|
|
|
|
|
1129
|
|
|
|
|
1130
|
|
|
/** |
|
1131
|
|
|
* Compute and compare the digest corresponding to some data given to the one specified by a reference. |
|
1132
|
|
|
* |
|
1133
|
|
|
* @param \DOMElement $ref The ds:Reference element containing the digest. |
|
1134
|
|
|
* @param string $data The referenced element, canonicalized, to digest and compare. |
|
1135
|
|
|
* |
|
1136
|
|
|
* @return bool True if the resulting digest matches the one in the reference, false otherwise. |
|
1137
|
|
|
*/ |
|
1138
|
|
|
protected function validateDigest(DOMElement $ref, string $data): bool |
|
1139
|
|
|
{ |
|
1140
|
|
|
$xp = XP::getXPath($ref->ownerDocument); |
|
|
|
|
|
|
1141
|
|
|
$alg = $xp->evaluate('string(./ds:DigestMethod/@Algorithm)', $ref); |
|
1142
|
|
|
$computed = $this->hash($alg, $data, false); |
|
1143
|
|
|
$evaluated = base64_decode($xp->evaluate('string(./ds:DigestValue)', $ref)); |
|
1144
|
|
|
return Sec::compareStrings($computed, $evaluated); |
|
1145
|
|
|
} |
|
1146
|
|
|
|
|
1147
|
|
|
|
|
1148
|
|
|
/** |
|
1149
|
|
|
* Iterate over the references specified by the signature, apply their transforms, and validate their digests |
|
1150
|
|
|
* against the referenced elements. |
|
1151
|
|
|
* |
|
1152
|
|
|
* @return boolean True if all references could be verified, false otherwise. |
|
1153
|
|
|
* |
|
1154
|
|
|
* @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there are no references. |
|
1155
|
|
|
*/ |
|
1156
|
|
|
protected function validateReferences(): bool |
|
1157
|
|
|
{ |
|
1158
|
|
|
$doc = $this->sigNode->ownerDocument; |
|
1159
|
|
|
|
|
1160
|
|
|
if (!$doc->documentElement->isSameNode($this->sigNode) && $this->sigNode->parentNode !== null) { |
|
|
|
|
|
|
1161
|
|
|
// enveloped signature, remove it |
|
1162
|
|
|
$this->sigNode->parentNode->removeChild($this->sigNode); |
|
|
|
|
|
|
1163
|
|
|
} |
|
1164
|
|
|
|
|
1165
|
|
|
$xp = XP::getXPath($doc); |
|
|
|
|
|
|
1166
|
|
|
$refNodes = $xp->query('./ds:SignedInfo/ds:Reference', $this->sigNode); |
|
1167
|
|
|
Assert::minCount($refNodes, 1, 'There are no Reference nodes', RuntimeException::class); |
|
1168
|
|
|
|
|
1169
|
|
|
$verified = true; |
|
1170
|
|
|
foreach ($refNodes as $refNode) { |
|
1171
|
|
|
$verified = $this->processReference($refNode) && $verified; |
|
1172
|
|
|
} |
|
1173
|
|
|
|
|
1174
|
|
|
return $verified; |
|
1175
|
|
|
} |
|
1176
|
|
|
} |
|
1177
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.