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\ExtendableElementTrait; |
||
10 | use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; |
||
11 | use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException, TooManyElementsException}; |
||
12 | use SimpleSAML\XMLSchema\Type\AnyURIValue; |
||
13 | use SimpleSAML\XMLSchema\XML\Enumeration\NamespaceEnum; |
||
14 | use SimpleSAML\XMLSecurity\Constants as C; |
||
15 | use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
||
16 | |||
17 | use function array_keys; |
||
18 | use function array_merge; |
||
19 | use function array_pop; |
||
20 | use function strval; |
||
21 | |||
22 | /** |
||
23 | * Class representing a ds:SignatureMethod element. |
||
24 | * |
||
25 | * @package simplesamlphp/xml-security |
||
26 | */ |
||
27 | final class SignatureMethod extends AbstractDsElement implements SchemaValidatableElementInterface |
||
28 | { |
||
29 | use ExtendableElementTrait; |
||
30 | use SchemaValidatableElementTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
31 | |||
32 | /** The namespace-attribute for the xs:any element */ |
||
33 | public const XS_ANY_ELT_NAMESPACE = NamespaceEnum::Other; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Initialize a SignatureMethod element. |
||
38 | * |
||
39 | * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $Algorithm |
||
40 | * @param \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null $hmacOutputLength |
||
41 | * @param array<\SimpleSAML\XML\SerializableElementInterface> $children |
||
42 | */ |
||
43 | public function __construct( |
||
44 | protected AnyURIValue $Algorithm, |
||
45 | protected ?HMACOutputLength $hmacOutputLength = null, |
||
46 | array $children = [], |
||
47 | ) { |
||
48 | Assert::oneOf( |
||
49 | $Algorithm->getValue(), |
||
50 | array_merge( |
||
51 | array_keys(C::$RSA_DIGESTS), |
||
52 | array_keys(C::$HMAC_DIGESTS), |
||
53 | ), |
||
54 | 'Invalid signature method: %s', |
||
55 | InvalidArgumentException::class, |
||
56 | ); |
||
57 | |||
58 | $this->setElements($children); |
||
59 | } |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Collect the value of the Algorithm-property |
||
64 | * |
||
65 | * @return \SimpleSAML\XMLSchema\Type\AnyURIValue |
||
66 | */ |
||
67 | public function getAlgorithm(): AnyURIValue |
||
68 | { |
||
69 | return $this->Algorithm; |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Collect the value of the hmacOutputLength-property |
||
75 | * |
||
76 | * @return \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null |
||
77 | */ |
||
78 | public function getHMACOutputLength(): ?HMACOutputLength |
||
79 | { |
||
80 | return $this->hmacOutputLength; |
||
81 | } |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Convert XML into a SignatureMethod |
||
86 | * |
||
87 | * @param \DOMElement $xml The XML element we should load |
||
88 | * @return static |
||
89 | * |
||
90 | * @throws \SimpleSAML\XMLSchema\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, 'SignatureMethod', InvalidDOMElementException::class); |
||
96 | Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class); |
||
97 | |||
98 | $Algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class); |
||
99 | |||
100 | $hmacOutputLength = HMACOutputLength::getChildrenOfClass($xml); |
||
101 | Assert::maxCount($hmacOutputLength, 1, TooManyElementsException::class); |
||
102 | |||
103 | return new static($Algorithm, array_pop($hmacOutputLength), self::getChildElementsFromXML($xml)); |
||
104 | } |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Convert this SignatureMethod element to XML. |
||
109 | * |
||
110 | * @param \DOMElement|null $parent The element we should append this SignatureMethod element to. |
||
111 | * @return \DOMElement |
||
112 | */ |
||
113 | public function toXML(?DOMElement $parent = null): DOMElement |
||
114 | { |
||
115 | $e = $this->instantiateParentElement($parent); |
||
116 | $e->setAttribute('Algorithm', strval($this->getAlgorithm())); |
||
117 | |||
118 | $this->getHMACOutputLength()?->toXML($e); |
||
119 | |||
120 | foreach ($this->getElements() as $elt) { |
||
121 | $elt->toXML($e); |
||
122 | } |
||
123 | |||
124 | return $e; |
||
125 | } |
||
126 | } |
||
127 |