1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\XML\ds; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
10
|
|
|
use SimpleSAML\XML\Exception\SchemaViolationException; |
11
|
|
|
use SimpleSAML\XML\Exception\TooManyElementsException; |
12
|
|
|
use SimpleSAML\XML\ExtendableElementTrait; |
13
|
|
|
use SimpleSAML\XML\SchemaValidatableElementInterface; |
14
|
|
|
use SimpleSAML\XML\SchemaValidatableElementTrait; |
15
|
|
|
use SimpleSAML\XML\SerializableElementInterface; |
16
|
|
|
use SimpleSAML\XML\XsNamespace as NS; |
17
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
18
|
|
|
|
19
|
|
|
use function array_merge; |
20
|
|
|
use function array_pop; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class representing a ds:KeyValue element. |
24
|
|
|
* |
25
|
|
|
* @package simplesamlphp/xml-security |
26
|
|
|
*/ |
27
|
|
|
final class KeyValue extends AbstractDsElement implements SchemaValidatableElementInterface |
28
|
|
|
{ |
29
|
|
|
use ExtendableElementTrait { |
30
|
|
|
// We use our own getter instead of the trait's one |
31
|
|
|
getElements as private; |
32
|
|
|
setElements as private; |
33
|
|
|
} |
34
|
|
|
use SchemaValidatableElementTrait; |
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** The namespace-attribute for the xs:any element */ |
38
|
|
|
public const XS_ANY_ELT_NAMESPACE = NS::OTHER; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initialize an KeyValue. |
43
|
|
|
* |
44
|
|
|
* @param \SimpleSAML\XML\SerializableElementInterface $keyValue |
45
|
|
|
*/ |
46
|
|
|
final public function __construct( |
47
|
|
|
protected RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface $keyValue, |
|
|
|
|
48
|
|
|
) { |
49
|
|
|
if (!( |
50
|
|
|
$keyValue instanceof RSAKeyValue |
51
|
|
|
|| $keyValue instanceof DSAKeyValue |
52
|
|
|
|| $keyValue instanceof ECKeyValue |
53
|
|
|
)) { |
54
|
|
|
Assert::true( |
55
|
|
|
( |
56
|
|
|
($keyValue instanceof Chunk) |
|
|
|
|
57
|
|
|
? $keyValue->getNamespaceURI() |
58
|
|
|
: $keyValue::getNameSpaceURI() |
59
|
|
|
) !== C::NS_XDSIG, |
60
|
|
|
'A <ds:KeyValue> requires either a RSAKeyValue, DSAKeyValue, ECKeyValue ' |
61
|
|
|
. 'or an element in namespace ##other', |
62
|
|
|
SchemaViolationException::class, |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Collect the value of the RSAKeyValue-property |
70
|
|
|
* |
71
|
|
|
* @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue| |
72
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue| |
73
|
|
|
* \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue| |
74
|
|
|
* \SimpeSAML\XML\SerializableElementInterface |
75
|
|
|
*/ |
76
|
|
|
public function getKeyValue(): RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface |
77
|
|
|
{ |
78
|
|
|
return $this->keyValue; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Convert XML into a KeyValue |
84
|
|
|
* |
85
|
|
|
* @param \DOMElement $xml The XML element we should load |
86
|
|
|
* @return static |
87
|
|
|
* |
88
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
89
|
|
|
* If the qualified name of the supplied element is wrong |
90
|
|
|
*/ |
91
|
|
|
public static function fromXML(DOMElement $xml): static |
92
|
|
|
{ |
93
|
|
|
Assert::same($xml->localName, 'KeyValue', InvalidDOMElementException::class); |
94
|
|
|
Assert::same($xml->namespaceURI, KeyValue::NS, InvalidDOMElementException::class); |
95
|
|
|
|
96
|
|
|
$keyValue = array_merge( |
97
|
|
|
RSAKeyValue::getChildrenOfClass($xml), |
98
|
|
|
DSAKeyValue::getChildrenOfClass($xml), |
99
|
|
|
self::getChildElementsFromXML($xml), |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
Assert::count( |
103
|
|
|
$keyValue, |
104
|
|
|
1, |
105
|
|
|
'A <ds:KeyValue> must contain exactly one child element', |
106
|
|
|
TooManyElementsException::class, |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
return new static(array_pop($keyValue)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Convert this KeyValue element to XML. |
115
|
|
|
* |
116
|
|
|
* @param \DOMElement|null $parent The element we should append this KeyValue element to. |
117
|
|
|
* @return \DOMElement |
118
|
|
|
*/ |
119
|
|
|
public function toXML(?DOMElement $parent = null): DOMElement |
120
|
|
|
{ |
121
|
|
|
$e = $this->instantiateParentElement($parent); |
122
|
|
|
|
123
|
|
|
$this->getKeyValue()->toXML($e); |
124
|
|
|
|
125
|
|
|
return $e; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|