66
77use DOMElement ;
88use SimpleSAML \Assert \Assert ;
9- use SimpleSAML \XML \ElementInterface ;
9+ use SimpleSAML \XML \Chunk ;
1010use SimpleSAML \XML \Exception \InvalidDOMElementException ;
1111use SimpleSAML \XML \Exception \SchemaViolationException ;
1212use SimpleSAML \XML \Exception \TooManyElementsException ;
1313use SimpleSAML \XML \ExtendableElementTrait ;
1414use SimpleSAML \XML \SchemaValidatableElementInterface ;
1515use SimpleSAML \XML \SchemaValidatableElementTrait ;
16+ use SimpleSAML \XML \SerializableElementInterface ;
1617use 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 ;
1723
1824/**
1925 * Class representing a ds:KeyValue element.
2228 */
2329final class KeyValue extends AbstractDsElement implements SchemaValidatableElementInterface
2430{
25- use ExtendableElementTrait;
31+ use ExtendableElementTrait {
32+ // We use our own getter instead of the trait's one
33+ getElements as private ;
34+ setElements as private ;
35+ }
2636 use SchemaValidatableElementTrait;
2737
2838
@@ -33,33 +43,41 @@ final class KeyValue extends AbstractDsElement implements SchemaValidatableEleme
3343 /**
3444 * Initialize an KeyValue.
3545 *
36- * @param \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null $RSAKeyValue
37- * @param \SimpleSAML\XML\SerializableElementInterface|null $element
46+ * @param \SimpleSAML\XML\SerializableElementInterface $keyValue
3847 */
3948 final public function __construct (
40- protected ?RSAKeyValue $ RSAKeyValue ,
41- ?ElementInterface $ element = null ,
49+ protected RSAKeyValue |DSAKeyValue |ECKeyValue |SerializableElementInterface $ keyValue ,
4250 ) {
43- Assert::false (
44- is_null ($ RSAKeyValue ) && is_null ($ element ),
45- 'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other ' ,
46- SchemaViolationException::class,
47- );
48-
49- if ($ element !== null ) {
50- $ this ->setElements ([$ element ]);
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+ );
5166 }
5267 }
5368
5469
5570 /**
5671 * Collect the value of the RSAKeyValue-property
5772 *
58- * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null
73+ * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|
74+ * \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue|
75+ * \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue|
76+ * \SimpeSAML\XML\SerializableElementInterface
5977 */
60- public function getRSAKeyValue (): ? RSAKeyValue
78+ public function getKeyValue (): RSAKeyValue | DSAKeyValue | ECKeyValue | SerializableElementInterface
6179 {
62- return $ this ->RSAKeyValue ;
80+ return $ this ->keyValue ;
6381 }
6482
6583
@@ -77,23 +95,20 @@ public static function fromXML(DOMElement $xml): static
7795 Assert::same ($ xml ->localName , 'KeyValue ' , InvalidDOMElementException::class);
7896 Assert::same ($ xml ->namespaceURI , KeyValue::NS , InvalidDOMElementException::class);
7997
80- $ RSAKeyValue = RSAKeyValue::getChildrenOfClass ($ xml );
81- Assert::maxCount (
82- $ RSAKeyValue ,
83- 1 ,
84- 'A <ds:KeyValue> can contain exactly one <ds:RSAKeyValue> ' ,
85- TooManyElementsException::class,
98+ $ keyValue = array_merge (
99+ RSAKeyValue::getChildrenOfClass ($ xml ),
100+ DSAKeyValue::getChildrenOfClass ($ xml ),
101+ self ::getChildElementsFromXML ($ xml ),
86102 );
87103
88- $ elements = self ::getChildElementsFromXML ($ xml );
89- Assert::maxCount (
90- $ elements ,
104+ Assert::count (
105+ $ keyValue ,
91106 1 ,
92- 'A <ds:KeyValue> can contain exactly one element in namespace ##other ' ,
107+ 'A <ds:KeyValue> must contain exactly one child element ' ,
93108 TooManyElementsException::class,
94109 );
95110
96- return new static (array_pop ($ RSAKeyValue ), array_pop ( $ elements ));
111+ return new static (array_pop ($ keyValue ));
97112 }
98113
99114
@@ -107,13 +122,7 @@ public function toXML(?DOMElement $parent = null): DOMElement
107122 {
108123 $ e = $ this ->instantiateParentElement ($ parent );
109124
110- $ this ->getRSAKeyValue ()?->toXML($ e );
111-
112- foreach ($ this ->elements as $ elt ) {
113- if (!$ elt ->isEmptyElement ()) {
114- $ elt ->toXML ($ e );
115- }
116- }
125+ $ this ->getKeyValue ()->toXML ($ e );
117126
118127 return $ e ;
119128 }
0 commit comments