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 | // We use our own getter instead of the trait's one, so we prevent their use by marking them private |
||
32 | use ExtendableElementTrait { |
||
33 | getElements as private; |
||
34 | setElements as private; |
||
35 | } |
||
36 | use SchemaValidatableElementTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
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 | /** @var \SimpleSAML\XML\AbstractElement|\SimpleSAML\XML\Chunk $keyValue */ |
||
52 | if ( |
||
53 | !($keyValue instanceof RSAKeyValue |
||
54 | || $keyValue instanceof DSAKeyValue |
||
55 | || $keyValue instanceof ECKeyValue) |
||
56 | ) { |
||
57 | Assert::true( |
||
58 | (($keyValue instanceof Chunk) ? $keyValue->getNamespaceURI() : $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| |
||
0 ignored issues
–
show
|
|||
72 | * \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue| |
||
73 | * \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue| |
||
74 | * \SimpleSAML\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 |