1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\XMLSecurity; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* xmlseclibs.php |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2007-2017, Robert Richards <[email protected]>. |
9
|
|
|
* All rights reserved. |
10
|
|
|
* |
11
|
|
|
* Redistribution and use in source and binary forms, with or without |
12
|
|
|
* modification, are permitted provided that the following conditions |
13
|
|
|
* are met: |
14
|
|
|
* |
15
|
|
|
* * Redistributions of source code must retain the above copyright |
16
|
|
|
* notice, this list of conditions and the following disclaimer. |
17
|
|
|
* |
18
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
19
|
|
|
* notice, this list of conditions and the following disclaimer in |
20
|
|
|
* the documentation and/or other materials provided with the |
21
|
|
|
* distribution. |
22
|
|
|
* |
23
|
|
|
* * Neither the name of Robert Richards nor the names of his |
24
|
|
|
* contributors may be used to endorse or promote products derived |
25
|
|
|
* from this software without specific prior written permission. |
26
|
|
|
* |
27
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
30
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
31
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
32
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
33
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
34
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
35
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
36
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
37
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
39
|
|
|
* |
40
|
|
|
* @copyright 2007-2017 Robert Richards <[email protected]> |
41
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
class XMLSecurityDSig extends \RobRichards\XMLSecLibs\XMLSecurityDSig |
45
|
|
|
{ |
46
|
|
|
/** @var string */ |
47
|
|
|
private $prefix = ''; |
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return bool |
52
|
|
|
* @throws Exception |
53
|
|
|
*/ |
54
|
|
|
public function validateReference() |
55
|
|
|
{ |
56
|
|
|
$sigNode = $this->sigNode; |
57
|
|
|
$docElem = $sigNode->ownerDocument->documentElement; |
58
|
|
|
|
59
|
|
|
// enveloped signature, remove it |
60
|
|
|
if (!$docElem->isSameNode($sigNode)) { |
|
|
|
|
61
|
|
|
if ($sigNode->parentNode !== null) { |
62
|
|
|
$sigNode->parentNode->removeChild($sigNode); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$xpath = $this->getXPathObj(); |
66
|
|
|
$query = "./secdsig:SignedInfo[1]/secdsig:Reference"; |
67
|
|
|
$nodeset = $xpath->query($query, $sigNode); |
68
|
|
|
if ($nodeset->length < 1) { |
69
|
|
|
throw new Exception("Reference nodes not found"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* Initialize/reset the list of validated nodes. */ |
73
|
|
|
$this->validatedNodes = []; |
|
|
|
|
74
|
|
|
|
75
|
|
|
foreach ($nodeset as $refNode) { |
76
|
|
|
if (!$this->processRefNode($refNode)) { |
77
|
|
|
/* Clear the list of validated nodes. */ |
78
|
|
|
$this->validatedNodes = null; |
79
|
|
|
throw new Exception("Reference validation failed"); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the XPathObj or null if xPathCtx is set and sigNode is empty. |
88
|
|
|
* |
89
|
|
|
* @return DOMXPath|null |
90
|
|
|
*/ |
91
|
|
|
private function getXPathObj() |
92
|
|
|
{ |
93
|
|
|
if (empty($this->xPathCtx) && ! empty($this->sigNode)) { |
|
|
|
|
94
|
|
|
$xpath = new DOMXPath($this->sigNode->ownerDocument); |
|
|
|
|
95
|
|
|
$xpath->registerNamespace('secdsig', self::XMLDSIGNS); |
96
|
|
|
$this->xPathCtx = $xpath; |
97
|
|
|
} |
98
|
|
|
return $this->xPathCtx; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|