Passed
Pull Request — master (#3)
by Tim
02:04
created

Signature::setPrefix()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
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;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\DigestMethod was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use SimpleSAML\XMLSecurity\XML\ds\DigestValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\DigestValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use SimpleSAML\XMLSecurity\XML\ds\Signature as Sig;
25
use SimpleSAML\XMLSecurity\XML\ds\Transform;
26
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
27
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
28
use SimpleSAML\XMLSecurity\XML\ds\X509Digest;
29
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
31
32
/**
33
 * Class implementing XML digital signatures.
34
 *
35
 * @package SimpleSAML\XMLSecurity
36
 */
37
class Signature
38
{
39
    /** @var array */
40
    public array $idNS = [];
41
42
    /** @var array */
43
    public array $idKeys = [];
44
45
    /** @var \SimpleSAML\XMLSecurity\Backend\SignatureBackend|null */
46
    protected ?SignatureBackend $backend = null;
47
48
    /** @var \DOMElement */
49
    protected DOMElement $root;
50
51
    /** @var \DOMElement|null */
52
    protected ?DOMElement $sigNode = null;
53
54
    /** @var \DOMElement */
55
    protected DOMElement $sigMethodNode;
56
57
    /** @var \DOMElement */
58
    protected DOMElement $c14nMethodNode;
59
60
    /** @var \DOMElement */
61
    protected DOMElement $sigInfoNode;
62
63
    /** @var \DOMElement|null */
64
    protected ?DOMElement $objectNode = null;
65
66
    /** @var string */
67
    protected string $signfo;
68
69
    /** @var string */
70
    protected string $sigAlg;
71
72
    /** @var \DOMElement[] */
73
    protected array $verifiedElements = [];
74
75
    /** @var string */
76
    protected string $c14nMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS;
77
78
    /** @var string */
79
    protected string $nsPrefix = 'ds:';
80
81
    /** @var array */
82
    protected array $algBlacklist = [
83
        C::SIG_RSA_SHA1,
84
        C::SIG_HMAC_SHA1,
85
    ];
86
87
    /** @var array */
88
    protected array $references = [];
89
90
    /** @var bool */
91
    protected bool $enveloping = false;
92
93
94
    /**
95
     * Signature constructor.
96
     *
97
     * @param \DOMElement|string $root The DOM element or a string of data we want to sign.
98
     * @param \SimpleSAML\XMLSecurity\Backend\SignatureBackend|null $backend The backend to use to
99
     *   generate or verify signatures. See individual algorithms for defaults.
100
     */
101
    public function __construct($root, SignatureBackend $backend = null)
102
    {
103
        $this->backend = $backend;
104
        $this->initSignature();
105
106
        if (is_string($root)) {
107
            $this->root = $this->addObject($root);
108
            $this->enveloping = true;
109
        } else {
110
            $this->root = $root;
111
        }
112
    }
113
114
115
    /**
116
     * Add an object element to the signature containing the given data.
117
     *
118
     * @param \DOMElement|string $data The data we want to envelope inside the signature.
119
     * @param string|null $mimetype An optional mime type to specify.
120
     * @param string|null $encoding An optional encoding to specify.
121
     *
122
     * @return \DOMElement The resulting object element added to the signature.
123
     */
124
    public function addObject($data, ?string $mimetype = null, ?string $encoding = null): DOMElement
125
    {
126
        if ($this->objectNode === null) {
127
            $this->objectNode = $this->createElement('Object');
128
            $this->sigNode->appendChild($this->objectNode);
0 ignored issues
show
Bug introduced by
The method appendChild() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
            $this->sigNode->/** @scrutinizer ignore-call */ 
129
                            appendChild($this->objectNode);

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.

Loading history...
Bug introduced by
$this->objectNode of type null is incompatible with the type DOMNode expected by parameter $node of DOMNode::appendChild(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
            $this->sigNode->appendChild(/** @scrutinizer ignore-type */ $this->objectNode);
Loading history...
129
        }
130
131
        if (is_string($mimetype) && !empty($mimetype)) {
132
            $this->objectNode->setAttribute('MimeType', $mimetype);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            $this->objectNode->/** @scrutinizer ignore-call */ 
133
                               setAttribute('MimeType', $mimetype);

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.

Loading history...
133
        }
134
135
        if (is_string($encoding) && !empty($encoding)) {
136
            $this->objectNode->setAttribute('Encoding', $encoding);
137
        }
138
139
        if ($data instanceof DOMElement) {
140
            $this->objectNode->appendChild($this->sigNode->ownerDocument->importNode($data, true));
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
            $this->objectNode->appendChild($this->sigNode->ownerDocument->/** @scrutinizer ignore-call */ importNode($data, true));

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.

Loading history...
141
        } else {
142
            $this->objectNode->appendChild($this->sigNode->ownerDocument->createTextNode($data));
143
        }
144
145
        return $this->objectNode;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->objectNode could return the type null which is incompatible with the type-hinted return DOMElement. Consider adding an additional type-check to rule them out.
Loading history...
146
    }
147
148
149
    /**
150
     * Add a reference to a given node (an element or a document).
151
     *
152
     * @param \DOMNode $node A DOMElement that we want to sign, or a DOMDocument if we want to sign the entire document.
153
     * @param string $alg The identifier of a supported digest algorithm to use when processing this reference.
154
     * @param array $transforms An array containing a list of transforms that must be applied to the reference.
155
     * Optional.
156
     * @param array $options An array containing a set of options for this reference. Optional. Supported options are:
157
     *   - prefix (string): the XML prefix used in the element being referenced. Defaults to none (no prefix used).
158
     *
159
     *   - prefix_ns (string): the namespace associated with the given prefix. Defaults to none (no prefix used).
160
     *
161
     *   - id_name (string): the name of the "id" attribute in the referenced element. Defaults to "Id".
162
     *
163
     *   - force_uri (boolean): Whether to explicitly add a URI attribute to the reference when referencing a
164
     *     DOMDocument or not. Defaults to true. If force_uri is false and $node is a DOMDocument, the URI attribute
165
     *     will be completely omitted.
166
     *
167
     *   - overwrite (boolean): Whether to overwrite the identifier existing in the element referenced with a new,
168
     *     random one, or not. Defaults to true.
169
     *
170
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $node is not
171
     *   an instance of DOMDocument or DOMElement.
172
     */
173
    public function addReference(DOMNode $node, string $alg, array $transforms = [], array $options = []): void
174
    {
175
        Assert::isInstanceOfAny(
176
            $node,
177
            [DOMDocument::class, DOMElement::class],
178
            'Only references to the DOM document or elements are allowed.'
179
        );
180
181
        $prefix = @$options['prefix'] ?: null;
182
        $prefixNS = @$options['prefix_ns'] ?: null;
183
        $idName = @$options['id_name'] ?: 'Id';
184
        $attrName = $prefix ? $prefix . ':' . $idName : $idName;
185
        $forceURI = true;
186
        if (isset($options['force_uri'])) {
187
            $forceURI = $options['force_uri'];
188
        }
189
        $overwrite = true;
190
        if (isset($options['overwrite'])) {
191
            $overwrite = $options['overwrite'];
192
        }
193
194
        $reference = $this->createElement('Reference');
195
        $this->sigInfoNode->appendChild($reference);
196
197
        // register reference
198
        $includeCommentNodes = false;
199
        if ($node instanceof DOMElement) {
200
            $uri = null;
201
            if (!$overwrite) {
202
                $uri = $prefixNS ? $node->getAttributeNS($prefixNS, $idName) : $node->getAttribute($idName);
203
            }
204
            if (empty($uri)) {
205
                $uri = Utils\Random::generateGUID();
206
                $node->setAttributeNS($prefixNS, $attrName, $uri);
207
            }
208
209
            if (
210
                in_array(C::C14N_EXCLUSIVE_WITH_COMMENTS, $transforms)
211
                || in_array(C::C14N_INCLUSIVE_WITH_COMMENTS, $transforms)
212
            ) {
213
                $includeCommentNodes = true;
214
                $reference->setAttribute('URI', "#xpointer($attrName('$uri'))");
215
            } else {
216
                $reference->setAttribute('URI', '#' . $uri);
217
            }
218
        } elseif ($forceURI) {
219
            // $node is a \DOMDocument, should add a reference to the root element (enveloped signature)
220
            if (in_array($this->c14nMethod, [C::C14N_INCLUSIVE_WITH_COMMENTS, C::C14N_EXCLUSIVE_WITH_COMMENTS])) {
221
                // if we want to use a C14N method that includes comments, the URI must be an xpointer
222
                $reference->setAttribute('URI', '#xpointer(/)');
223
            } else {
224
                // C14N without comments, we can set an empty URI
225
                $reference->setAttribute('URI', '');
226
            }
227
        }
228
229
        // apply and register transforms
230
        $transformList = $this->createElement('Transforms');
231
        $reference->appendChild($transformList);
232
233
        if (!empty($transforms)) {
234
            foreach ($transforms as $transform) {
235
                if (is_array($transform) && !empty($transform[C::XPATH_URI]['query'])) {
236
                    $t = new Transform(C::XPATH_URI, [new Chunk($transform[C::XPATH_URI]['query'])]);
237
                } else {
238
                    $t = new Transform($transform);
239
                }
240
                $t->toXML($transformList);
241
            }
242
        } elseif (!empty($this->c14nMethod)) {
243
            $t = new Transform($this->c14nMethod);
244
            $t->toXML($transformList);
245
        }
246
247
        $canonicalData = $this->processTransforms($reference, $node, $includeCommentNodes);
248
        $digest = $this->hash($alg, $canonicalData);
249
250
        $digestMethod = new DigestMethod($alg);
251
        $digestMethod->toXML($reference);
252
253
        $digestValue = new DigestValue($digest);
254
        $digestValue->toXML($reference);
255
256
        if (!in_array($node, $this->references)) {
257
            $this->references[] = $node;
258
        }
259
    }
260
261
262
    /**
263
     * Add a set of references to the signature.
264
     *
265
     * @param \DOMNode[] $nodes An array of DOMNode objects to be referred in the signature.
266
     * @param string $alg The identifier of the digest algorithm to use.
267
     * @param array $transforms An array of transforms to apply to each reference.
268
     * @param array $options An array of options.
269
     *
270
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If any of the nodes in the $nodes
271
     *   array is not an instance of DOMDocument or DOMElement.
272
     *
273
     * @see addReference()
274
     */
275
    public function addReferences(array $nodes, string $alg, array $transforms = [], $options = []): void
276
    {
277
        foreach ($nodes as $node) {
278
            $this->addReference($node, $alg, $transforms, $options);
279
        }
280
    }
281
282
283
    /**
284
     * Attach one or more X509 certificates to the signature.
285
     *
286
     * @param \SimpleSAML\XMLSecurity\Key\X509Certificate[] $certs
287
     *   An X509Certificate object or an array of them.
288
     * @param boolean $addSubject Whether to add the subject of the certificate or not.
289
     * @param string|false $digest A digest algorithm identifier if the digest of the certificate should be added. False
290
     * otherwise.
291
     * @param boolean $addIssuerSerial Whether to add the serial number of the issuer or not.
292
     *
293
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $certs is not a
294
     *   X509Certificate object or an array of them.
295
     */
296
    public function addX509Certificates(
297
        array $certs,
298
        bool $addSubject = false,
299
        $digest = false,
300
        bool $addIssuerSerial = false
301
    ): void {
302
        Assert::allIsInstanceOf($certs, Key\X509Certificate::class);
303
304
        $certData = [];
305
306
        foreach ($certs as $cert) {
307
            $details = $cert->getCertificateDetails();
308
309
            if ($addSubject && isset($details['subject'])) {
310
                // add subject
311
                $subjectNameValue = $details['issuer'];
312
                if (is_array($details['subject'])) {
313
                    $parts = [];
314
                    foreach ($details['subject'] as $key => $value) {
315
                        if (is_array($value)) {
316
                            foreach ($value as $valueElement) {
317
                                array_unshift($parts, $key . '=' . $valueElement);
318
                            }
319
                        } else {
320
                            array_unshift($parts, $key . '=' . $value);
321
                        }
322
                    }
323
                    $subjectNameValue = implode(',', $parts);
324
                }
325
                $certData[] = new X509SubjectName($subjectNameValue);
326
            }
327
328
            if ($digest !== false) {
329
                // add certificate digest
330
                $fingerprint = base64_encode(hex2bin($cert->getRawThumbprint($digest)));
331
                $certData[] = new X509Digest($fingerprint, $digest);
332
            }
333
334
            if ($addIssuerSerial && isset($details['issuer']) && isset($details['serialNumber'])) {
335
                $issuerName = CertificateUtils::parseIssuer($details['issuer']);
0 ignored issues
show
Bug introduced by
The method parseIssuer() does not exist on SimpleSAML\XMLSecurity\Utils\Certificate. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

335
                /** @scrutinizer ignore-call */ 
336
                $issuerName = CertificateUtils::parseIssuer($details['issuer']);

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.

Loading history...
336
337
                $certData[] = new X509IssuerSerial(
338
                    new X509IssuerName($issuerName),
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\X509IssuerName was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
339
                    new X509SerialNumber($details['serialNumber'])
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\X509SerialNumber was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
340
                );
341
            }
342
343
            $certData[] = new X509Certificate(CertificateUtils::stripHeaders($cert->getCertificate()));
344
        }
345
346
        $keyInfo = new KeyInfo($certData);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\KeyInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
347
        $keyInfoNode = $keyInfo->toXML();
348
349
        if ($this->objectNode === null) {
350
            $this->sigNode->appendChild($keyInfoNode);
351
        } else {
352
            $this->sigNode->insertBefore($keyInfoNode, $this->objectNode);
353
        }
354
    }
355
356
357
    /**
358
     * Append a signature as the last child of the signed element.
359
     *
360
     * @return \DOMNode The appended signature.
361
     */
362
    public function append(): DOMNode
363
    {
364
        return $this->insert();
365
    }
366
367
368
    /**
369
     * Use this signature as an enveloping signature, effectively adding the signed data to a ds:Object element.
370
     *
371
     * @param string|null $mimetype The mime type corresponding to the signed data.
372
     * @param string|null $encoding The encoding corresponding to the signed data.
373
     */
374
    public function envelop(string $mimetype = null, string $encoding = null): void
375
    {
376
        $this->root = $this->addObject($this->root, $mimetype, $encoding);
377
    }
378
379
380
    /**
381
     * Build a new XML digital signature from a given document or node.
382
     *
383
     * @param \DOMNode $node The DOMDocument or DOMElement that contains the signature.
384
     *
385
     * @return Signature A Signature object corresponding to the signature present in the given DOM document or element.
386
     *
387
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $node is not
388
     *   an instance of DOMDocument or DOMElement.
389
     * @throws \SimpleSAML\XMLSecurity\Exception\NoSignatureFound If there is no signature in the $node.
390
     */
391
    public static function fromXML(DOMNode $node): Signature
392
    {
393
        Assert::isInstanceOfAny(
394
            $node,
395
            [DOMDocument::class, DOMElement::class],
396
            'Signatures can only be created from DOM documents or elements'
397
        );
398
399
        $signature = self::findSignature($node);
400
        if ($node instanceof DOMDocument) {
401
            $node = $node->documentElement;
402
        }
403
        $dsig = new self($node);
404
        $dsig->setSignatureElement($signature);
405
        return $dsig;
406
    }
407
408
409
    /**
410
     * Obtain the list of currently blacklisted algorithms.
411
     *
412
     * Signatures using blacklisted algorithms cannot be created or verified.
413
     *
414
     * @return array An array containing the identifiers of the algorithms blacklisted currently.
415
     */
416
    public function getBlacklistedAlgorithms(): array
417
    {
418
        return $this->algBlacklist;
419
    }
420
421
422
    /**
423
     * Get the list of namespaces to designate ID attributes.
424
     *
425
     * @return array An array of strings with the namespaces used in ID attributes.
426
     */
427
    public function getIdNamespaces(): array
428
    {
429
        return $this->idNS;
430
    }
431
432
433
    /**
434
     * Get a list of attributes used as an ID.
435
     *
436
     * @return array An array of strings with the attributes used as an ID.
437
     */
438
    public function getIdAttributes(): array
439
    {
440
        return $this->idKeys;
441
    }
442
443
444
    /**
445
     * Get the root configured for this signature.
446
     *
447
     * This will be the signed element, whether that's a user-provided XML element or a ds:Object element containing
448
     * the actual data (which can in turn be either XML or not).
449
     *
450
     * @return \DOMElement The root element for this signature.
451
     */
452
    public function getRoot(): DOMElement
453
    {
454
        return $this->root;
455
    }
456
457
458
    /**
459
     * Get the identifier of the algorithm used in this signature.
460
     *
461
     * @return string The identifier of the algorithm used in this signature.
462
     */
463
    public function getSignatureMethod(): string
464
    {
465
        return $this->sigAlg;
466
    }
467
468
469
    /**
470
     * Get a list of elements verified by this signature.
471
     *
472
     * The elements in this list are referenced by the signature and the references verified to be correct. However,
473
     * this doesn't mean the signature is valid, only that the references were so.
474
     *
475
     * Note that the list returned will be empty unless verify() has been called before.
476
     *
477
     * @return \DOMElement[] A list of elements correctly referenced by this signature. An empty list of verify() has
478
     * not been called yet, or if the references couldn't be verified.
479
     */
480
    public function getVerifiedElements(): array
481
    {
482
        return $this->verifiedElements;
483
    }
484
485
486
    /**
487
     * Insert a signature as a child of the signed element, optionally before a given element.
488
     *
489
     * @param \DOMElement|false $before An optional DOM element the signature should be prepended to.
490
     *
491
     * @return \DOMNode The inserted signature.
492
     *
493
     * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this signature is in enveloping mode.
494
     */
495
    public function insert($before = false): DOMNode
496
    {
497
        Assert::false(
498
            $this->enveloping,
499
            'Cannot insert the signature in the object it is enveloping.',
500
            RuntimeException::class
501
        );
502
503
        $signature = $this->root->ownerDocument->importNode($this->sigNode, true);
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

503
        /** @scrutinizer ignore-call */ 
504
        $signature = $this->root->ownerDocument->importNode($this->sigNode, true);

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.

Loading history...
Bug introduced by
It seems like $this->sigNode can also be of type null; however, parameter $node of DOMDocument::importNode() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

503
        $signature = $this->root->ownerDocument->importNode(/** @scrutinizer ignore-type */ $this->sigNode, true);
Loading history...
504
505
        if ($before instanceof DOMElement) {
506
            return $this->root->insertBefore($signature, $before);
507
        }
508
        return $this->root->insertBefore($signature);
509
    }
510
511
512
    /**
513
     * Prepend a signature as the first child of the signed element.
514
     *
515
     * @return \DOMNode The prepended signature.
516
     */
517
    public function prepend(): DOMNode
518
    {
519
        foreach ($this->root->childNodes as $child) {
520
            // look for the first child element, if any
521
            if ($child instanceof \DOMElement) {
522
                return $this->insert($child);
523
            }
524
        }
525
        return $this->append();
526
    }
527
528
529
    /**
530
     * Set the backend to create or verify signatures.
531
     *
532
     * @param SignatureBackend $backend The SignatureBackend implementation to use. See individual algorithms for
533
     * details about the default backends used.
534
     */
535
    public function setBackend(SignatureBackend $backend): void
536
    {
537
        $this->backend = $backend;
538
    }
539
540
541
    /**
542
     * Set the list of currently blacklisted algorithms.
543
     *
544
     * Signatures using blacklisted algorithms cannot be created or verified.
545
     *
546
     * @param array $algs An array containing the identifiers of the algorithms to blacklist.
547
     */
548
    public function setBlacklistedAlgorithms(array $algs): void
549
    {
550
        $this->algBlacklist = $algs;
551
    }
552
553
554
    /**
555
     * Set the canonicalization method used in this signature.
556
     *
557
     * Note that exclusive canonicalization without comments is used by default, so it's not necessary to call
558
     * setCanonicalizationMethod() if that canonicalization method is desired.
559
     *
560
     * @param string $method The identifier of the canonicalization method to use.
561
     *
562
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $method is not a valid
563
     *   identifier of a supported canonicalization method.
564
     */
565
    public function setCanonicalizationMethod(string $method): void
566
    {
567
        Assert::oneOf(
568
            $method,
569
            [
570
                C::C14N_EXCLUSIVE_WITH_COMMENTS,
571
                C::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
572
                C::C14N_INCLUSIVE_WITH_COMMENTS,
573
                C::C14N_INCLUSIVE_WITHOUT_COMMENTS
574
            ],
575
            'Invalid canonicalization method',
576
            InvalidArgumentException::class
577
        );
578
579
        $this->c14nMethod = $method;
580
        $this->c14nMethodNode->setAttribute('Algorithm', $method);
581
    }
582
583
584
    /**
585
     * Set the encoding for the signed contents in an enveloping signature.
586
     *
587
     * @param string $encoding The encoding used in the signed contents.
588
     *
589
     * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this is not an enveloping signature.
590
     */
591
    public function setEncoding(string $encoding): void
592
    {
593
        Assert::true(
594
            $this->enveloping,
595
            'Cannot set the encoding for non-enveloping signatures.',
596
            RuntimeException::class
597
        );
598
599
        $this->root->setAttribute('Encoding', $encoding);
600
    }
601
602
603
    /**
604
     * Set a list of attributes used as an ID.
605
     *
606
     * @param array $keys An array of strings with the attributes used as an ID.
607
     */
608
    public function setIdAttributes(array $keys): void
609
    {
610
        $this->idKeys = $keys;
611
    }
612
613
614
    /**
615
     * Set the list of namespaces to designate ID attributes.
616
     *
617
     * @param array $namespaces An array of strings with the namespaces used in ID attributes.
618
     */
619
    public function setIdNamespaces(array $namespaces): void
620
    {
621
        $this->idNS = $namespaces;
622
    }
623
624
625
    /**
626
     * Set the mime type for the signed contents in an enveloping signature.
627
     *
628
     * @param string $mimetype The mime type of the signed contents.
629
     *
630
     * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this is not an enveloping signature.
631
     */
632
    public function setMimeType(string $mimetype): void
633
    {
634
        Assert::true(
635
            $this->enveloping,
636
            'Cannot set the mime type for non-enveloping signatures.',
637
            RuntimeException::class
638
        );
639
640
        $this->root->setAttribute('MimeType', $mimetype);
641
    }
642
643
644
    /**
645
     * Set the signature element to a given one, and initialize the signature from there.
646
     *
647
     * @param \DOMElement $element A DOM element containing an XML signature.
648
     *
649
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException If the element does not correspond to an XML
650
     *   signature or it is malformed (e.g. there are missing mandatory elements or attributes).
651
     */
652
    public function setSignatureElement(DOMElement $element): void
653
    {
654
        Assert::same($element->localName, 'Signature', InvalidDOMElementException::class);
655
        Assert::same($element->namespaceURI, Sig::NS, InvalidDOMElementException::class);
656
657
        $this->sigNode = $element;
658
659
        $xp = XP::getXPath($this->sigNode->ownerDocument);
0 ignored issues
show
Bug introduced by
It seems like $this->sigNode->ownerDocument can also be of type null; however, parameter $doc of SimpleSAML\XMLSecurity\Utils\XPath::getXPath() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

659
        $xp = XP::getXPath(/** @scrutinizer ignore-type */ $this->sigNode->ownerDocument);
Loading history...
660
661
        $signedInfoNodes = $xp->query('./ds:SignedInfo', $this->sigNode);
662
663
        Assert::minCount(
664
            $signedInfoNodes,
665
            1,
666
            'There is no SignedInfo element in the signature',
667
            RuntimeException::class
668
        );
669
        $this->sigInfoNode = $signedInfoNodes->item(0);
670
671
672
        $this->sigAlg = $xp->evaluate('string(./ds:SignedInfo/ds:SignatureMethod/@Algorithm)', $this->sigNode);
673
        Assert::stringNotEmpty($this->sigAlg, 'Unable to determine SignatureMethod', RuntimeException::class);
674
675
        $c14nMethodNodes = $xp->query('./ds:CanonicalizationMethod', $this->sigInfoNode);
676
        Assert::minCount(
677
            $c14nMethodNodes,
678
            1,
679
            'There is no CanonicalizationMethod in the signature',
680
            RuntimeException::class
681
        );
682
683
        $this->c14nMethodNode = $c14nMethodNodes->item(0);
684
        if (!$this->c14nMethodNode->hasAttribute('Algorithm')) {
685
            throw new RuntimeException('CanonicalizationMethod missing required Algorithm attribute');
686
        }
687
        $this->c14nMethod = $this->c14nMethodNode->getAttribute('Algorithm');
688
    }
689
690
691
    /**
692
     * Sign the document or element.
693
     *
694
     * This method will finish the signature process. It will create an XML signature valid for document or elements
695
     * specified previously with addReference() or addReferences(). If none of those methods have been called previous
696
     * to calling sign() (there are no references in the signature), the $root passed during construction of the
697
     * Signature object will be referenced automatically.
698
     *
699
     * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use for signing. Bear in mind that the type of
700
     *   this key must be compatible with the types of key accepted by the algorithm specified in $alg.
701
     * @param string $alg The identifier of the signature algorithm to use. See \SimpleSAML\XMLSecurity\Constants.
702
     * @param bool $appendToNode Whether to append the signature as the last child of the root element or not.
703
     *
704
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $appendToNode is true and
705
     *   this is an enveloping signature.
706
     */
707
    public function sign(Key\AbstractKey $key, string $alg, bool $appendToNode = false): void
708
    {
709
        Assert::false(
710
            ($this->enveloping && $appendToNode),
711
            'Cannot append the signature, we are in enveloping mode.',
712
            InvalidArgumentException::class
713
        );
714
715
        $this->sigMethodNode->setAttribute('Algorithm', $alg);
716
        $factory = new SignatureAlgorithmFactory($this->algBlacklist);
717
        $signer = $factory->getAlgorithm($alg, $key);
718
        if ($this->backend !== null) {
719
            $signer->setBackend($this->backend);
720
        }
721
722
        if (empty($this->references)) {
723
            // no references have been added, ref root
724
            $transforms = [];
725
            if (!$this->enveloping) {
726
                $transforms[] = C::XMLDSIG_ENVELOPED;
727
            }
728
            $this->addReference($this->root->ownerDocument, $signer->getDigest(), $transforms, []);
0 ignored issues
show
Bug introduced by
It seems like $this->root->ownerDocument can also be of type null; however, parameter $node of SimpleSAML\XMLSecurity\Signature::addReference() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

728
            $this->addReference(/** @scrutinizer ignore-type */ $this->root->ownerDocument, $signer->getDigest(), $transforms, []);
Loading history...
729
        }
730
731
        if ($appendToNode) {
732
            $this->sigNode = $this->append();
733
        } elseif (in_array($this->c14nMethod, [C::C14N_INCLUSIVE_WITHOUT_COMMENTS, C::C14N_INCLUSIVE_WITH_COMMENTS])) {
734
            // append Signature to root node for inclusive canonicalization
735
            $restoreSigNode = $this->sigNode;
736
            $this->sigNode = $this->prepend();
737
        }
738
739
        $sigValue = base64_encode($signer->sign($this->canonicalizeData($this->sigInfoNode, $this->c14nMethod)));
740
741
        // remove Signature from node if we added it for c14n
742
        if (
743
            !$appendToNode &&
744
            in_array($this->c14nMethod, [C::C14N_INCLUSIVE_WITHOUT_COMMENTS, C::C14N_INCLUSIVE_WITH_COMMENTS])
745
        ) { // remove from root in case we added it for inclusive canonicalization
746
            $this->root->removeChild($this->root->lastChild);
0 ignored issues
show
Bug introduced by
It seems like $this->root->lastChild can also be of type null; however, parameter $child of DOMNode::removeChild() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

746
            $this->root->removeChild(/** @scrutinizer ignore-type */ $this->root->lastChild);
Loading history...
747
            /** @var \DOMElement $restoreSigNode */
748
            $this->sigNode = $restoreSigNode;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $restoreSigNode does not seem to be defined for all execution paths leading up to this point.
Loading history...
749
        }
750
751
        $sigValueNode = $this->createElement('SignatureValue', $sigValue);
752
        if ($this->sigInfoNode->nextSibling) {
753
            $this->sigInfoNode->nextSibling->parentNode->insertBefore($sigValueNode, $this->sigInfoNode->nextSibling);
0 ignored issues
show
Bug introduced by
The method insertBefore() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

753
            $this->sigInfoNode->nextSibling->parentNode->/** @scrutinizer ignore-call */ 
754
                                                         insertBefore($sigValueNode, $this->sigInfoNode->nextSibling);

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.

Loading history...
754
        } else {
755
            $this->sigNode->appendChild($sigValueNode);
756
        }
757
    }
758
759
760
    /**
761
     * Verify this signature with a given key.
762
     *
763
     * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use to verify this signature.
764
     *
765
     * @return bool True if the signature can be verified with $key, false otherwise.
766
     *
767
     * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there is no SignatureValue in
768
     *   the signature, or we couldn't verify all the references.
769
     */
770
    public function verify(Key\AbstractKey $key): bool
771
    {
772
        $xp = XP::getXPath($this->sigNode->ownerDocument);
0 ignored issues
show
Bug introduced by
It seems like $this->sigNode->ownerDocument can also be of type null; however, parameter $doc of SimpleSAML\XMLSecurity\Utils\XPath::getXPath() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

772
        $xp = XP::getXPath(/** @scrutinizer ignore-type */ $this->sigNode->ownerDocument);
Loading history...
773
        $sigval = $xp->evaluate('string(./ds:SignatureValue)', $this->sigNode);
774
        if (empty($sigval)) {
775
            throw new RuntimeException('Unable to locate SignatureValue');
776
        }
777
778
        $siginfo = $this->canonicalizeData($this->sigInfoNode, $this->c14nMethod);
779
        if (!$this->validateReferences()) {
780
            throw new RuntimeException('Unable to verify all references');
781
        }
782
783
        $factory = new SignatureAlgorithmFactory($this->algBlacklist);
784
        $alg = $factory->getAlgorithm($this->sigAlg, $key);
785
        if ($this->backend !== null) {
786
            $alg->setBackend($this->backend);
787
        }
788
        return $alg->verify($siginfo, base64_decode($sigval));
789
    }
790
791
792
    /**
793
     * Canonicalize any given node.
794
     *
795
     * @param \DOMNode $node The DOM node that needs canonicalization.
796
     * @param string $c14nMethod The identifier of the canonicalization algorithm to use.
797
     * See \SimpleSAML\XMLSecurity\Constants.
798
     * @param array|null $xpaths An array of xpaths to filter the nodes by. Defaults to null (no filters).
799
     * @param array|null $prefixes An array of namespace prefixes to filter the nodes by. Defaults to null (no filters).
800
     *
801
     * @return string The canonical representation of the given DOM node, according to the algorithm requested.
802
     */
803
    protected function canonicalizeData(
804
        DOMNode $node,
805
        string $c14nMethod,
806
        array $xpaths = null,
807
        array $prefixes = null
808
    ): string {
809
        $exclusive = false;
810
        $withComments = false;
811
        switch ($c14nMethod) {
812
            case C::C14N_EXCLUSIVE_WITH_COMMENTS:
813
            case C::C14N_INCLUSIVE_WITH_COMMENTS:
814
                $withComments = true;
815
        }
816
        switch ($c14nMethod) {
817
            case C::C14N_EXCLUSIVE_WITH_COMMENTS:
818
            case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS:
819
                $exclusive = true;
820
        }
821
822
        if (
823
            is_null($xpaths)
824
            && ($node->ownerDocument !== null)
825
            && $node->isSameNode($node->ownerDocument->documentElement)
826
        ) {
827
            // check for any PI or comments as they would have been excluded
828
            $element = $node;
829
            while ($refNode = $element->previousSibling) {
830
                if (
831
                    (($refNode->nodeType === XML_COMMENT_NODE) && $withComments)
832
                    || $refNode->nodeType === XML_PI_NODE
833
                ) {
834
                    break;
835
                }
836
                $element = $refNode;
837
            }
838
            if ($refNode == null) {
839
                $node = $node->ownerDocument;
840
            }
841
        }
842
843
        return $node->C14N($exclusive, $withComments, $xpaths, $prefixes);
844
    }
845
846
847
    /**
848
     * Create a new element in this signature.
849
     *
850
     * @param string $name The name of this element.
851
     * @param string|null $content The text contents of the element, or null if it is not supposed to have any text
852
     * contents. Defaults to null.
853
     * @param string $ns The namespace the new element must be created under. Defaults to the standard XMLDSIG
854
     * namespace.
855
     *
856
     * @return \DOMElement A new DOM element with the given name.
857
     */
858
    protected function createElement(
859
        string $name,
860
        string $content = null,
861
        string $ns = C::XMLDSIGNS
862
    ): DOMElement {
863
        if ($this->sigNode === null) {
864
            // initialize signature
865
            $doc = DOMDocumentFactory::create();
866
        } else {
867
            $doc = $this->sigNode->ownerDocument;
868
        }
869
870
        $nsPrefix = $this->nsPrefix;
871
872
        if ($content !== null) {
873
            return $doc->createElementNS($ns, $nsPrefix . $name, $content);
874
        }
875
876
        return $doc->createElementNS($ns, $nsPrefix . $name);
877
    }
878
879
880
    /**
881
     * Find a signature from a given node.
882
     *
883
     * @param \DOMNode $node A DOMElement node where a signature is expected as a child (enveloped) or a DOMDocument
884
     * node to search for document signatures (one single reference with an empty URI).
885
     *
886
     * @return \DOMElement The signature element.
887
     *
888
     * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there is no DOMDocument element available.
889
     * @throws \SimpleSAML\XMLSecurity\Exception\NoSignatureFound If no signature is found.
890
     */
891
    protected static function findSignature(DOMNode $node): DOMElement
892
    {
893
        $doc = $node instanceof DOMDocument ? $node : $node->ownerDocument;
894
895
        Assert::notNull($doc, 'Cannot search for signatures, no DOM document available', RuntimeException::class);
896
897
        $xp = XP::getXPath($doc);
0 ignored issues
show
Bug introduced by
It seems like $doc can also be of type null; however, parameter $doc of SimpleSAML\XMLSecurity\Utils\XPath::getXPath() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

897
        $xp = XP::getXPath(/** @scrutinizer ignore-type */ $doc);
Loading history...
898
        $nodeset = $xp->query('./ds:Signature', $node);
899
900
        if ($nodeset->length === 0) {
901
            throw new NoSignatureFound();
902
        }
903
        return $nodeset->item(0);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $nodeset->item(0) returns the type null which is incompatible with the type-hinted return DOMElement.
Loading history...
904
    }
905
906
907
    /**
908
     * Compute the hash for some data with a given algorithm.
909
     *
910
     * @param string $alg The identifier of the algorithm to use.
911
     * @param string $data The data to digest.
912
     * @param bool $encode Whether to bas64-encode the result or not. Defaults to true.
913
     *
914
     * @return string The (binary or base64-encoded) digest corresponding to the given data.
915
     *
916
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $alg is not a valid
917
     *   identifier of a supported digest algorithm.
918
     */
919
    protected function hash(string $alg, string $data, bool $encode = true): string
920
    {
921
        Assert::keyExists(
922
            C::$DIGEST_ALGORITHMS,
923
            $alg,
924
            'Unsupported digest method "%s"',
925
            InvalidArgumentException::class
926
        );
927
928
        $digest = hash(C::$DIGEST_ALGORITHMS[$alg], $data, true);
929
        return $encode ? base64_encode($digest) : $digest;
930
    }
931
932
933
    /**
934
     * Initialize the basic structure of a signature from scratch.
935
     *
936
     */
937
    protected function initSignature(): void
938
    {
939
        $this->sigNode = $this->createElement('Signature');
940
        $this->sigInfoNode = $this->createElement('SignedInfo');
941
        $this->c14nMethodNode = $this->createElement('CanonicalizationMethod');
942
        $this->c14nMethodNode->setAttribute('Algorithm', $this->c14nMethod);
943
        $this->sigMethodNode = $this->createElement('SignatureMethod');
944
945
        $this->sigInfoNode->appendChild($this->c14nMethodNode);
946
        $this->sigInfoNode->appendChild($this->sigMethodNode);
947
        $this->sigNode->appendChild($this->sigInfoNode);
948
        $this->sigNode->ownerDocument->appendChild($this->sigNode);
949
    }
950
951
952
    /**
953
     * Process a given reference, by looking for it, processing the specified transforms, canonicalizing the result
954
     * and comparing its corresponding digest.
955
     *
956
     * Verified references will be stored in the "verifiedElements" property.
957
     *
958
     * @param \DOMElement $ref The ds:Reference element to process.
959
     *
960
     * @return bool True if the digest of the processed reference matches the one given, false otherwise.
961
     *
962
     * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If the referenced element is missing or
963
     *   the reference points to an external document.
964
     *
965
     * @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel
966
     */
967
    protected function processReference(DOMElement $ref): bool
968
    {
969
        /*
970
         * Depending on the URI, we may need to remove comments during canonicalization.
971
         * See: http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel
972
         */
973
        $includeCommentNodes = true;
974
        $dataObject = $ref->ownerDocument;
975
        if ($ref->hasAttribute("URI")) {
976
            $uri = $ref->getAttribute('URI');
977
            if (empty($uri)) {
978
                // this reference identifies the enclosing XML, it should not include comments
979
                $includeCommentNodes = false;
980
            }
981
            $arUrl = parse_url($uri);
982
            if (empty($arUrl['path'])) {
983
                if ($identifier = @$arUrl['fragment']) {
984
                    /*
985
                     * This reference identifies a node with the given ID by using a URI on the form '#identifier'.
986
                     * This should not include comments.
987
                     */
988
                    $includeCommentNodes = false;
989
990
                    $xp = XP::getXPath($ref->ownerDocument);
0 ignored issues
show
Bug introduced by
It seems like $ref->ownerDocument can also be of type null; however, parameter $doc of SimpleSAML\XMLSecurity\Utils\XPath::getXPath() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

990
                    $xp = XP::getXPath(/** @scrutinizer ignore-type */ $ref->ownerDocument);
Loading history...
991
                    if ($this->idNS && is_array($this->idNS)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->idNS of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
992
                        foreach ($this->idNS as $nspf => $ns) {
993
                            $xp->registerNamespace($nspf, $ns);
994
                        }
995
                    }
996
                    $iDlist = '@Id="' . $identifier . '"';
997
                    if (is_array($this->idKeys)) {
0 ignored issues
show
introduced by
The condition is_array($this->idKeys) is always true.
Loading history...
998
                        foreach ($this->idKeys as $idKey) {
999
                            $iDlist .= " or @$idKey='$identifier'";
1000
                        }
1001
                    }
1002
                    $query = '//*[' . $iDlist . ']';
1003
                    $dataObject = $xp->query($query)->item(0);
1004
                    if ($dataObject === null) {
1005
                        throw new RuntimeException('Reference not found');
1006
                    }
1007
                }
1008
            } else {
1009
                throw new RuntimeException('Processing of external documents is not supported');
1010
            }
1011
        } else {
1012
            // this reference identifies the root node with an empty URI, it should not include comments
1013
            $includeCommentNodes = false;
1014
        }
1015
1016
        $data = $this->processTransforms($ref, $dataObject, $includeCommentNodes);
1017
        if (!$this->validateDigest($ref, $data)) {
1018
            return false;
1019
        }
1020
1021
        // parse the canonicalized reference...
1022
        $doc = DOMDocumentFactory::create();
1023
        $doc->loadXML($data);
1024
        $dataObject = $doc->documentElement;
1025
1026
        // ... and add it to the list of verified elements
1027
        if (!empty($identifier)) {
1028
            $this->verifiedElements[$identifier] = $dataObject;
1029
        } else {
1030
            $this->verifiedElements[] = $dataObject;
1031
        }
1032
1033
        return true;
1034
    }
1035
1036
1037
    /**
1038
     * Process all transforms specified by a given Reference element.
1039
     *
1040
     * @param \DOMElement $ref The Reference element.
1041
     * @param mixed $data The data referenced.
1042
     * @param bool $includeCommentNodes Whether to allow canonicalization with comments or not.
1043
     *
1044
     * @return string The canonicalized data after applying all transforms specified by $ref.
1045
     *
1046
     * @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel
1047
     */
1048
    protected function processTransforms(DOMElement $ref, $data, bool $includeCommentNodes = false): string
1049
    {
1050
        if (!($data instanceof DOMNode)) {
1051
            return $data;
1052
        }
1053
1054
        $xp = XP::getXPath($ref->ownerDocument);
0 ignored issues
show
Bug introduced by
It seems like $ref->ownerDocument can also be of type null; however, parameter $doc of SimpleSAML\XMLSecurity\Utils\XPath::getXPath() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1054
        $xp = XP::getXPath(/** @scrutinizer ignore-type */ $ref->ownerDocument);
Loading history...
1055
        $transforms = $xp->query('./ds:Transforms/ds:Transform', $ref);
1056
        $canonicalMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS;
1057
        $arXPath = null;
1058
        $prefixList = null;
1059
        foreach ($transforms as $transform) {
1060
            /** @var \DOMElement $transform */
1061
            $algorithm = $transform->getAttribute("Algorithm");
1062
            switch ($algorithm) {
1063
                case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS:
1064
                case C::C14N_EXCLUSIVE_WITH_COMMENTS:
1065
                    if (!$includeCommentNodes) {
1066
                        // remove comment nodes by forcing it to use a canonicalization without comments
1067
                        $canonicalMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS;
1068
                    } else {
1069
                        $canonicalMethod = $algorithm;
1070
                    }
1071
1072
                    $node = $transform->firstChild;
1073
                    while ($node) {
1074
                        if ($node->localName === 'InclusiveNamespaces') {
1075
                            if ($pfx = $node->getAttribute('PrefixList')) {
1076
                                $arpfx = [];
1077
                                $pfxlist = explode(" ", $pfx);
1078
                                foreach ($pfxlist as $pfx) {
1079
                                    $val = trim($pfx);
1080
                                    if (! empty($val)) {
1081
                                        $arpfx[] = $val;
1082
                                    }
1083
                                }
1084
                                if (count($arpfx) > 0) {
1085
                                    $prefixList = $arpfx;
1086
                                }
1087
                            }
1088
                            break;
1089
                        }
1090
                        $node = $node->nextSibling;
1091
                    }
1092
                    break;
1093
                case C::C14N_INCLUSIVE_WITHOUT_COMMENTS:
1094
                case C::C14N_INCLUSIVE_WITH_COMMENTS:
1095
                    if (!$includeCommentNodes) {
1096
                        // remove comment nodes by forcing it to use a canonicalization without comments
1097
                        $canonicalMethod = C::C14N_INCLUSIVE_WITHOUT_COMMENTS;
1098
                    } else {
1099
                        $canonicalMethod = $algorithm;
1100
                    }
1101
1102
                    break;
1103
                case C::XPATH_URI:
1104
                    $node = $transform->firstChild;
1105
                    while ($node) {
1106
                        if ($node->localName == 'XPath') {
1107
                            $arXPath = [];
1108
                            $arXPath['query'] = '(.//. | .//@* | .//namespace::*)[' . $node->nodeValue . ']';
1109
                            $arXpath['namespaces'] = [];
1110
                            $nslist = $xp->query('./namespace::*', $node);
1111
                            foreach ($nslist as $nsnode) {
1112
                                if ($nsnode->localName != "xml") {
1113
                                    $arXPath['namespaces'][$nsnode->localName] = $nsnode->nodeValue;
1114
                                }
1115
                            }
1116
                            break;
1117
                        }
1118
                        $node = $node->nextSibling;
1119
                    }
1120
                    break;
1121
            }
1122
        }
1123
1124
        return $this->canonicalizeData($data, $canonicalMethod, $arXPath, $prefixList);
1125
    }
1126
1127
1128
    /**
1129
     * Compute and compare the digest corresponding to some data given to the one specified by a reference.
1130
     *
1131
     * @param \DOMElement $ref The ds:Reference element containing the digest.
1132
     * @param string $data The referenced element, canonicalized, to digest and compare.
1133
     *
1134
     * @return bool True if the resulting digest matches the one in the reference, false otherwise.
1135
     */
1136
    protected function validateDigest(DOMElement $ref, string $data): bool
1137
    {
1138
        $xp = XP::getXPath($ref->ownerDocument);
0 ignored issues
show
Bug introduced by
It seems like $ref->ownerDocument can also be of type null; however, parameter $doc of SimpleSAML\XMLSecurity\Utils\XPath::getXPath() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1138
        $xp = XP::getXPath(/** @scrutinizer ignore-type */ $ref->ownerDocument);
Loading history...
1139
        $alg = $xp->evaluate('string(./ds:DigestMethod/@Algorithm)', $ref);
1140
        $computed = $this->hash($alg, $data, false);
1141
        $evaluated = base64_decode($xp->evaluate('string(./ds:DigestValue)', $ref));
1142
        return Sec::compareStrings($computed, $evaluated);
1143
    }
1144
1145
1146
    /**
1147
     * Iterate over the references specified by the signature, apply their transforms, and validate their digests
1148
     * against the referenced elements.
1149
     *
1150
     * @return boolean True if all references could be verified, false otherwise.
1151
     *
1152
     * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there are no references.
1153
     */
1154
    protected function validateReferences(): bool
1155
    {
1156
        $doc = $this->sigNode->ownerDocument;
1157
1158
        if (!$doc->documentElement->isSameNode($this->sigNode) && $this->sigNode->parentNode !== null) {
0 ignored issues
show
Bug introduced by
It seems like $this->sigNode can also be of type null; however, parameter $otherNode of DOMNode::isSameNode() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1158
        if (!$doc->documentElement->isSameNode(/** @scrutinizer ignore-type */ $this->sigNode) && $this->sigNode->parentNode !== null) {
Loading history...
1159
            // enveloped signature, remove it
1160
            $this->sigNode->parentNode->removeChild($this->sigNode);
0 ignored issues
show
Bug introduced by
It seems like $this->sigNode can also be of type null; however, parameter $child of DOMNode::removeChild() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1160
            $this->sigNode->parentNode->removeChild(/** @scrutinizer ignore-type */ $this->sigNode);
Loading history...
1161
        }
1162
1163
        $xp = XP::getXPath($doc);
0 ignored issues
show
Bug introduced by
It seems like $doc can also be of type null; however, parameter $doc of SimpleSAML\XMLSecurity\Utils\XPath::getXPath() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1163
        $xp = XP::getXPath(/** @scrutinizer ignore-type */ $doc);
Loading history...
1164
        $refNodes = $xp->query('./ds:SignedInfo/ds:Reference', $this->sigNode);
1165
        Assert::minCount($refNodes, 1, 'There are no Reference nodes', RuntimeException::class);
1166
1167
        $verified = true;
1168
        foreach ($refNodes as $refNode) {
1169
            $verified = $this->processReference($refNode) && $verified;
1170
        }
1171
1172
        return $verified;
1173
    }
1174
}
1175