66
77use DOMElement ;
88use SimpleSAML \Assert \Assert ;
9- use SimpleSAML \XML \Chunk ;
109use SimpleSAML \XML \Exception \InvalidDOMElementException ;
10+ use SimpleSAML \XML \ExtendableElementTrait ;
11+ use SimpleSAML \XML \SerializableElementInterface ;
12+ use SimpleSAML \XML \XsNamespace as NS ;
1113use SimpleSAML \XMLSecurity \Constants as C ;
1214use SimpleSAML \XMLSecurity \Exception \InvalidArgumentException ;
13- use SimpleSAML \XMLSecurity \XML \dsig11 \KeyInfoReference ;
14- use SimpleSAML \XMLSecurity \XML \xenc \EncryptedData ;
15- use SimpleSAML \XMLSecurity \XML \xenc \EncryptedKey ;
1615
1716/**
1817 * Class representing a ds:KeyInfo element.
2120 */
2221final class KeyInfo extends AbstractDsElement
2322{
23+ use ExtendableElementTrait;
24+
25+ /** @var \SimpleSAML\XML\XsNamespace */
26+ public const XS_ANY_ELT_NAMESPACE = NS ::OTHER ;
27+
28+
2429 /**
2530 * Initialize a KeyInfo element.
2631 *
2732 * @param (
28- * \SimpleSAML\XML\SerializableElementInterface|
2933 * \SimpleSAML\XMLSecurity\XML\ds\KeyName|
3034 * \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
3135 * \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
@@ -34,29 +38,26 @@ final class KeyInfo extends AbstractDsElement
3438 * \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
3539 * \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey
3640 * )[] $info
41+ * @param \SimpleSAML\XML\SerializableElementInterface[] $children
3742 * @param string|null $Id
3843 */
3944 public function __construct (
4045 protected array $ info ,
46+ array $ children = [],
4147 protected ?string $ Id = null ,
4248 ) {
43- Assert::notEmpty ($ info , 'ds:KeyInfo cannot be empty ' , InvalidArgumentException::class);
44- Assert::maxCount ($ info , C::UNBOUNDED_LIMIT );
45- Assert::allIsInstanceOfAny (
46- $ info ,
47- [
48- Chunk::class,
49- KeyName::class,
50- KeyValue::class,
51- RetrievalMethod::class,
52- X509Data::class,
53- EncryptedData::class,
54- EncryptedKey::class,
55- ],
56- 'KeyInfo can only contain instances of KeyName, X509Data, EncryptedKey or Chunk. ' ,
49+ $ combi = array_merge ($ info , $ children );
50+
51+ Assert::notEmpty ($ combi , 'ds:KeyInfo cannot be empty ' , InvalidArgumentException::class);
52+ Assert::maxCount ($ combi , C::UNBOUNDED_LIMIT );
53+ Assert::allIsInstanceOf (
54+ $ combi ,
55+ SerializableElementInterface::class,
5756 InvalidArgumentException::class,
5857 );
5958 Assert::nullOrValidNCName ($ Id );
59+
60+ $ this ->setElements ($ children );
6061 }
6162
6263
@@ -74,20 +75,11 @@ public function getId(): ?string
7475 /**
7576 * Collect the value of the info-property
7677 *
77- * @return (
78- * \SimpleSAML\XML\SerializableElementInterface|
79- * \SimpleSAML\XMLSecurity\XML\ds\KeyName|
80- * \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
81- * \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
82- * \SimpleSAML\XMLSecurity\XML\ds\X509Data|
83- * \SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference|
84- * \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
85- * \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey
86- * )[]
78+ * @return list<\SimpleSAML\XML\SerializableElementInterface>
8779 */
8880 public function getInfo (): array
8981 {
90- return $ this ->info ;
82+ return array_merge ( $ this ->info , $ this -> getElements ()) ;
9183 }
9284
9385
@@ -106,36 +98,27 @@ public static function fromXML(DOMElement $xml): static
10698 Assert::same ($ xml ->namespaceURI , KeyInfo::NS , InvalidDOMElementException::class);
10799
108100 $ Id = self ::getOptionalAttribute ($ xml , 'Id ' , null );
109- $ info = [];
110-
111- foreach ($ xml ->childNodes as $ n ) {
112- if (!($ n instanceof DOMElement)) {
113- continue ;
114- } elseif ($ n ->namespaceURI === C::NS_XDSIG ) {
115- $ info [] = match ($ n ->localName ) {
116- 'KeyName ' => KeyName::fromXML ($ n ),
117- 'KeyValue ' => KeyValue::fromXML ($ n ),
118- 'RetrievalMethod ' => RetrievalMethod::fromXML ($ n ),
119- 'X509Data ' => X509Data::fromXML ($ n ),
120- default => new Chunk ($ n ),
121- };
122- } elseif ($ n ->namespaceURI === C::NS_XDSIG11 ) {
123- $ info [] = match ($ n ->localName ) {
124- 'KeyInfoReference ' => KeyInfoReference::fromXML ($ n ),
125- default => new Chunk ($ n ),
126- };
127- } elseif ($ n ->namespaceURI === C::NS_XENC ) {
128- $ info [] = match ($ n ->localName ) {
129- 'EncryptedData ' => EncryptedData::fromXML ($ n ),
130- 'EncryptedKey ' => EncryptedKey::fromXML ($ n ),
131- default => new Chunk ($ n ),
132- };
133- } else {
134- $ info [] = new Chunk ($ n );
135- }
136- }
137101
138- return new static ($ info , $ Id );
102+ $ keyName = KeyName::getChildrenOfClass ($ xml );
103+ $ keyValue = KeyValue::getChildrenOfClass ($ xml );
104+ $ retrievalMethod = RetrievalMethod::getChildrenOfClass ($ xml );
105+ $ x509Data = X509Data::getChildrenOfClass ($ xml );
106+ //$pgpData = PGPData::getChildrenOfClass($xml);
107+ //$spkiData = SPKIData::getChildrenOfClass($xml);
108+ //$mgmtData = MgmtData::getChildrenOfClass($xml);
109+
110+ $ info = array_merge (
111+ $ keyName ,
112+ $ keyValue ,
113+ $ retrievalMethod ,
114+ $ x509Data ,
115+ //$pgpdata,
116+ //$spkidata,
117+ //$mgmtdata,
118+ );
119+
120+ $ children = self ::getChildElementsFromXML ($ xml );
121+ return new static ($ info , $ children , $ Id );
139122 }
140123
141124
@@ -153,8 +136,8 @@ public function toXML(DOMElement $parent = null): DOMElement
153136 $ e ->setAttribute ('Id ' , $ this ->getId ());
154137 }
155138
156- foreach ($ this ->getInfo () as $ n ) {
157- $ n ->toXML ($ e );
139+ foreach ($ this ->getInfo () as $ elt ) {
140+ $ elt ->toXML ($ e );
158141 }
159142
160143 return $ e ;
0 commit comments