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\XMLSecurity\Constants as C; |
12
|
|
|
use 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; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class representing a ds:KeyInfo element. |
19
|
|
|
* |
20
|
|
|
* @package simplesamlphp/xml-security |
21
|
|
|
*/ |
22
|
|
|
final class KeyInfo extends AbstractDsElement |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Initialize a KeyInfo element. |
26
|
|
|
* |
27
|
|
|
* @param list<\SimpleSAML\XML\Chunk| |
28
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\KeyName| |
29
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue| |
30
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod| |
31
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509Data| |
32
|
|
|
* \SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference| |
33
|
|
|
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData| |
34
|
|
|
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey> $info |
35
|
|
|
* @param string|null $Id |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
protected array $info, |
39
|
|
|
protected ?string $Id = null, |
40
|
|
|
) { |
41
|
|
|
Assert::notEmpty($info, 'ds:KeyInfo cannot be empty', InvalidArgumentException::class); |
42
|
|
|
Assert::allIsInstanceOfAny( |
43
|
|
|
$info, |
44
|
|
|
[ |
45
|
|
|
Chunk::class, |
46
|
|
|
KeyName::class, |
47
|
|
|
KeyValue::class, |
48
|
|
|
RetrievalMethod::class, |
49
|
|
|
X509Data::class, |
50
|
|
|
EncryptedData::class, |
51
|
|
|
EncryptedKey::class, |
52
|
|
|
], |
53
|
|
|
'KeyInfo can only contain instances of KeyName, X509Data, EncryptedKey or Chunk.', |
54
|
|
|
InvalidArgumentException::class, |
55
|
|
|
); |
56
|
|
|
Assert::nullOrValidNCName($Id); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Collect the value of the Id-property |
62
|
|
|
* |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
|
|
public function getId(): ?string |
66
|
|
|
{ |
67
|
|
|
return $this->Id; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Collect the value of the info-property |
73
|
|
|
* |
74
|
|
|
* @return (\SimpleSAML\XML\Chunk| |
|
|
|
|
75
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\KeyName| |
76
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue| |
77
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod| |
78
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509Data| |
79
|
|
|
* \SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference| |
80
|
|
|
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData| |
81
|
|
|
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey)[] |
82
|
|
|
*/ |
83
|
|
|
public function getInfo(): array |
84
|
|
|
{ |
85
|
|
|
return $this->info; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Convert XML into a KeyInfo |
91
|
|
|
* |
92
|
|
|
* @param \DOMElement $xml The XML element we should load |
93
|
|
|
* @return static |
94
|
|
|
* |
95
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
96
|
|
|
* If the qualified name of the supplied element is wrong |
97
|
|
|
*/ |
98
|
|
|
public static function fromXML(DOMElement $xml): static |
99
|
|
|
{ |
100
|
|
|
Assert::same($xml->localName, 'KeyInfo', InvalidDOMElementException::class); |
101
|
|
|
Assert::same($xml->namespaceURI, KeyInfo::NS, InvalidDOMElementException::class); |
102
|
|
|
|
103
|
|
|
$Id = self::getAttribute($xml, 'Id', null); |
104
|
|
|
$info = []; |
105
|
|
|
|
106
|
|
|
foreach ($xml->childNodes as $n) { |
107
|
|
|
if (!($n instanceof DOMElement)) { |
108
|
|
|
continue; |
109
|
|
|
} elseif ($n->namespaceURI === C::NS_XDSIG) { |
110
|
|
|
$info[] = match ($n->localName) { |
111
|
|
|
'KeyName' => KeyName::fromXML($n), |
112
|
|
|
'KeyValue' => KeyValue::fromXML($n), |
113
|
|
|
'RetrievalMethod' => RetrievalMethod::fromXML($n), |
114
|
|
|
'X509Data' => X509Data::fromXML($n), |
115
|
|
|
default => new Chunk($n), |
116
|
|
|
}; |
117
|
|
|
} elseif ($n->namespaceURI === C::NS_XDSIG11) { |
118
|
|
|
$info[] = match ($n->localName) { |
119
|
|
|
'KeyInfoReference' => KeyInfoReference::fromXML($n), |
120
|
|
|
default => new Chunk($n), |
121
|
|
|
}; |
122
|
|
|
} elseif ($n->namespaceURI === C::NS_XENC) { |
123
|
|
|
$info[] = match ($n->localName) { |
124
|
|
|
'EncryptedData' => EncryptedData::fromXML($n), |
125
|
|
|
'EncryptedKey' => EncryptedKey::fromXML($n), |
126
|
|
|
default => new Chunk($n), |
127
|
|
|
}; |
128
|
|
|
} else { |
129
|
|
|
$info[] = new Chunk($n); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return new static($info, $Id); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Convert this KeyInfo to XML. |
139
|
|
|
* |
140
|
|
|
* @param \DOMElement|null $parent The element we should append this KeyInfo to. |
141
|
|
|
* @return \DOMElement |
142
|
|
|
*/ |
143
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
144
|
|
|
{ |
145
|
|
|
$e = $this->instantiateParentElement($parent); |
146
|
|
|
|
147
|
|
|
if ($this->Id !== null) { |
148
|
|
|
$e->setAttribute('Id', $this->getId()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
foreach ($this->info as $n) { |
152
|
|
|
$n->toXML($e); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $e; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|