1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\md; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\SAML2\XML\SignableElementTrait; |
9
|
|
|
use SimpleSAML\SAML2\XML\SignedElementTrait; |
10
|
|
|
use SimpleSAML\XMLSecurity\XML\SignableElementInterface; |
11
|
|
|
use SimpleSAML\XMLSecurity\XML\SignedElementInterface; |
12
|
|
|
|
13
|
|
|
use function method_exists; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Abstract class that represents a signed metadata element. |
17
|
|
|
* |
18
|
|
|
* @package simplesamlphp/saml2 |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractSignedMdElement extends AbstractMdElement implements |
21
|
|
|
SignableElementInterface, |
22
|
|
|
SignedElementInterface |
23
|
|
|
{ |
24
|
|
|
use SignableElementTrait; |
|
|
|
|
25
|
|
|
use SignedElementTrait { |
|
|
|
|
26
|
|
|
SignedElementTrait::getBlacklistedAlgorithms insteadof SignableElementTrait; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The original signed XML |
31
|
|
|
* |
32
|
|
|
* @var \DOMElement |
33
|
|
|
*/ |
34
|
|
|
protected DOMElement $xml; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the XML element. |
39
|
|
|
* |
40
|
|
|
* @return \DOMElement |
41
|
|
|
*/ |
42
|
|
|
public function getXML(): DOMElement |
43
|
|
|
{ |
44
|
|
|
return $this->xml; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set the XML element. |
50
|
|
|
* |
51
|
|
|
* @param \DOMElement $xml |
52
|
|
|
*/ |
53
|
|
|
protected function setXML(DOMElement $xml): void |
54
|
|
|
{ |
55
|
|
|
$this->xml = $xml; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \DOMElement|null $parent The EntityDescriptor we should append this SPSSODescriptor to. |
61
|
|
|
* @return \DOMElement |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
public function toXML(?DOMElement $parent = null): DOMElement |
65
|
|
|
{ |
66
|
|
|
if ($this->isSigned() === true && $this->signer === null) { |
67
|
|
|
// We already have a signed document and no signer was set to re-sign it |
68
|
|
|
if ($parent === null) { |
69
|
|
|
return $this->getXML(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$node = $parent->ownerDocument?->importNode($this->getXML(), true); |
73
|
|
|
$parent->appendChild($node); |
74
|
|
|
return $parent; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$e = $this->toUnsignedXML($parent); |
78
|
|
|
// This is a dirty hack, but if we add the xsi-type on AbstractRoleDescriptor we cannot |
79
|
|
|
// get the tests to pass because the attribute-order is messed up. This has something |
80
|
|
|
// to do with the fact that toUnsignedXML's recursive nature. |
81
|
|
|
if (method_exists(static::class, 'getXsiTypePrefix')) { |
82
|
|
|
$e->setAttributeNS( |
83
|
|
|
'http://www.w3.org/2000/xmlns/', |
84
|
|
|
'xmlns:' . static::getXsiTypePrefix(), |
|
|
|
|
85
|
|
|
static::getXsiTypeNamespaceURI(), |
|
|
|
|
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->signer !== null) { |
90
|
|
|
$signedXML = $this->doSign($e); |
91
|
|
|
$signedXML->insertBefore($this->signature?->toXML($signedXML), $signedXML->firstChild); |
92
|
|
|
return $signedXML; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $e; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param \DOMElement|null $parent |
101
|
|
|
* @return \DOMElement |
102
|
|
|
*/ |
103
|
|
|
abstract public function toUnsignedXML(?DOMElement $parent = null): DOMElement; |
104
|
|
|
} |
105
|
|
|
|