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\Exception\InvalidDOMElementException; |
10
|
|
|
use SimpleSAML\XML\Chunk; |
11
|
|
|
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class representing a ds:X509Data element. |
15
|
|
|
* |
16
|
|
|
* @package simplesamlphp/xml-security |
17
|
|
|
*/ |
18
|
|
|
final class X509Data extends AbstractDsElement |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The various X509 data elements. |
22
|
|
|
* |
23
|
|
|
* Array with various elements describing this certificate. |
24
|
|
|
* Unknown elements will be represented by \SimpleSAML\XML\Chunk. |
25
|
|
|
* |
26
|
|
|
* @var (\SimpleSAML\XML\Chunk| |
|
|
|
|
27
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| |
28
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| |
29
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName)[] |
30
|
|
|
*/ |
31
|
|
|
protected array $data = []; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize a X509Data. |
36
|
|
|
* |
37
|
|
|
* @param (\SimpleSAML\XML\Chunk| |
38
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| |
39
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| |
40
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName)[] $data |
41
|
|
|
*/ |
42
|
|
|
public function __construct(array $data) |
43
|
|
|
{ |
44
|
|
|
$this->setData($data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Collect the value of the data-property |
50
|
|
|
* |
51
|
|
|
* @return (\SimpleSAML\XML\Chunk| |
|
|
|
|
52
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| |
53
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| |
54
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName)[] |
55
|
|
|
*/ |
56
|
|
|
public function getData(): array |
57
|
|
|
{ |
58
|
|
|
return $this->data; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the value of the data-property |
64
|
|
|
* |
65
|
|
|
* @param (\SimpleSAML\XML\Chunk| |
66
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| |
67
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| |
68
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName)[] $data |
69
|
|
|
* @throws \SimpleSAML\Assert\AssertionFailedException |
70
|
|
|
* if $data contains anything other than X509Certificate or Chunk |
71
|
|
|
*/ |
72
|
|
|
private function setData(array $data): void |
73
|
|
|
{ |
74
|
|
|
Assert::allIsInstanceOfAny( |
75
|
|
|
$data, |
76
|
|
|
[Chunk::class, X509Certificate::class, X509IssuerSerial::class, X509SubjectName::class], |
77
|
|
|
InvalidArgumentException::class, |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->data = $data; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Convert XML into a X509Data |
86
|
|
|
* |
87
|
|
|
* @param \DOMElement $xml The XML element we should load |
88
|
|
|
* @return static |
89
|
|
|
* |
90
|
|
|
* @throws \SimpleSAML\XML\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, 'X509Data', InvalidDOMElementException::class); |
96
|
|
|
Assert::same($xml->namespaceURI, X509Data::NS, InvalidDOMElementException::class); |
97
|
|
|
|
98
|
|
|
$data = []; |
99
|
|
|
|
100
|
|
|
for ($n = $xml->firstChild; $n !== null; $n = $n->nextSibling) { |
101
|
|
|
if (!($n instanceof DOMElement)) { |
102
|
|
|
continue; |
103
|
|
|
} elseif ($n->namespaceURI !== self::NS) { |
104
|
|
|
$data[] = new Chunk($n); |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$data[] = match ($n->localName) { |
109
|
|
|
'X509Certificate' => X509Certificate::fromXML($n), |
110
|
|
|
'X509IssuerSerial' => X509IssuerSerial::fromXML($n), |
111
|
|
|
'X509SubjectName' => X509SubjectName::fromXML($n), |
112
|
|
|
default => new Chunk($n), |
113
|
|
|
}; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return new static($data); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Convert this X509Data element to XML. |
122
|
|
|
* |
123
|
|
|
* @param \DOMElement|null $parent The element we should append this X509Data element to. |
124
|
|
|
* @return \DOMElement |
125
|
|
|
*/ |
126
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
127
|
|
|
{ |
128
|
|
|
$e = $this->instantiateParentElement($parent); |
129
|
|
|
|
130
|
|
|
foreach ($this->getData() as $n) { |
131
|
|
|
$n->toXML($e); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $e; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|