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