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, SerializableElementInterface}; |
||
11 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
12 | use SimpleSAML\XMLSchema\XML\Enumeration\NamespaceEnum; |
||
13 | use SimpleSAML\XMLSecurity\Constants as C; |
||
14 | use SimpleSAML\XMLSecurity\Exception\{InvalidArgumentException, ProtocolViolationException}; |
||
15 | use SimpleSAML\XMLSecurity\XML\dsig11\X509Digest; |
||
16 | |||
17 | /** |
||
18 | * Class representing a ds:X509Data element. |
||
19 | * |
||
20 | * @package simplesamlphp/xml-security |
||
21 | */ |
||
22 | final class X509Data extends AbstractDsElement implements SchemaValidatableElementInterface |
||
23 | { |
||
24 | use ExtendableElementTrait; |
||
25 | use SchemaValidatableElementTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
26 | |||
27 | /** The namespace-attribute for the xs:any element */ |
||
28 | public const XS_ANY_ELT_NAMESPACE = NamespaceEnum::Other; |
||
29 | |||
30 | /** The exclusions for the xs:any element */ |
||
31 | public const XS_ANY_ELT_EXCLUSIONS = [ |
||
32 | [X509Digest::NS, 'X509Digest'], |
||
33 | ]; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Initialize a X509Data. |
||
38 | * |
||
39 | * @param ( |
||
40 | * \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| |
||
41 | * \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| |
||
42 | * \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName| |
||
43 | * \SimpleSAML\XMLSecurity\XML\ds\X509SKI| |
||
44 | * \SimpleSAML\XMLSecurity\XML\ds\X509CRL| |
||
45 | * \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest |
||
46 | * )[] $data |
||
47 | * @param \SimpleSAML\XML\SerializableElementInterface[] $children |
||
48 | */ |
||
49 | public function __construct( |
||
50 | protected array $data, |
||
51 | protected array $children = [], |
||
52 | ) { |
||
53 | /** |
||
54 | * At least one element from the dsig namespaces should be present and |
||
55 | * additional elements from an external namespace to accompany/complement them. |
||
56 | */ |
||
57 | Assert::minCount($data, 1, ProtocolViolationException::class); |
||
58 | Assert::maxCount($data, C::UNBOUNDED_LIMIT); |
||
59 | Assert::allIsInstanceOfAny( |
||
60 | $data, |
||
61 | [ |
||
62 | X509Certificate::class, |
||
63 | X509IssuerSerial::class, |
||
64 | X509SubjectName::class, |
||
65 | X509Digest::class, |
||
66 | X509SKI::class, |
||
67 | X509CRL::class, |
||
68 | ], |
||
69 | InvalidArgumentException::class, |
||
70 | ); |
||
71 | |||
72 | $this->setElements($children); |
||
73 | } |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Collect the value of the data-property |
||
78 | * |
||
79 | * @return ( |
||
80 | * \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| |
||
81 | * \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| |
||
82 | * \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName| |
||
83 | * \SimpleSAML\XMLSecurity\XML\ds\X509SKI| |
||
84 | * \SimpleSAML\XMLSecurity\XML\ds\X509CRL| |
||
85 | * \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest |
||
86 | * )[] |
||
87 | */ |
||
88 | public function getData(): array |
||
89 | { |
||
90 | return $this->data; |
||
91 | } |
||
92 | |||
93 | |||
94 | /** |
||
95 | * Convert XML into a X509Data |
||
96 | * |
||
97 | * @param \DOMElement $xml The XML element we should load |
||
98 | * @return static |
||
99 | * |
||
100 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
101 | * If the qualified name of the supplied element is wrong |
||
102 | */ |
||
103 | public static function fromXML(DOMElement $xml): static |
||
104 | { |
||
105 | Assert::same($xml->localName, 'X509Data', InvalidDOMElementException::class); |
||
106 | Assert::same($xml->namespaceURI, X509Data::NS, InvalidDOMElementException::class); |
||
107 | |||
108 | $x509Certificate = X509Certificate::getChildrenOfClass($xml); |
||
109 | $x509IssuerSerial = X509IssuerSerial::getChildrenOfClass($xml); |
||
110 | $x509SubjectName = X509SubjectName::getChildrenOfClass($xml); |
||
111 | $x509SKI = X509SKI::getChildrenOfClass($xml); |
||
112 | $x509CRL = X509CRL::getChildrenOfClass($xml); |
||
113 | $x509Digest = X509Digest::getChildrenOfClass($xml); |
||
114 | |||
115 | $data = array_merge($x509Certificate, $x509IssuerSerial, $x509SubjectName, $x509SKI, $x509CRL, $x509Digest); |
||
116 | $children = self::getChildElementsFromXML($xml); |
||
117 | |||
118 | return new static($data, $children); |
||
119 | } |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Convert this X509Data element to XML. |
||
124 | * |
||
125 | * @param \DOMElement|null $parent The element we should append this X509Data element to. |
||
126 | * @return \DOMElement |
||
127 | */ |
||
128 | public function toXML(?DOMElement $parent = null): DOMElement |
||
129 | { |
||
130 | $e = $this->instantiateParentElement($parent); |
||
131 | |||
132 | foreach ($this->getData() as $d) { |
||
133 | $d->toXML($e); |
||
134 | } |
||
135 | |||
136 | foreach ($this->getElements() as $c) { |
||
137 | $c->toXML($e); |
||
138 | } |
||
139 | |||
140 | return $e; |
||
141 | } |
||
142 | } |
||
143 |