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\ExtendableElementTrait; |
11
|
|
|
use SimpleSAML\XML\SerializableElementInterface; |
12
|
|
|
use SimpleSAML\XML\XsNamespace as NS; |
13
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
14
|
|
|
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class representing a ds:KeyInfo element. |
18
|
|
|
* |
19
|
|
|
* @package simplesamlphp/xml-security |
20
|
|
|
*/ |
21
|
|
|
final class KeyInfo extends AbstractDsElement |
22
|
|
|
{ |
23
|
|
|
use ExtendableElementTrait; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** @var \SimpleSAML\XML\XsNamespace */ |
26
|
|
|
public const XS_ANY_ELT_NAMESPACE = NS::OTHER; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initialize a KeyInfo element. |
31
|
|
|
* |
32
|
|
|
* @param ( |
33
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\KeyName| |
34
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue| |
35
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod| |
36
|
|
|
* \SimpleSAML\XMLSecurity\XML\ds\X509Data| |
37
|
|
|
* \SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference| |
38
|
|
|
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData| |
39
|
|
|
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey |
40
|
|
|
* )[] $info |
41
|
|
|
* @param \SimpleSAML\XML\SerializableElementInterface[] $children |
42
|
|
|
* @param string|null $Id |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
protected array $info, |
46
|
|
|
array $children = [], |
47
|
|
|
protected ?string $Id = null, |
48
|
|
|
) { |
49
|
|
|
$combi = array_merge($info, $children); |
50
|
|
|
|
51
|
|
|
Assert::notEmpty($combi, 'ds:KeyInfo cannot be empty', InvalidArgumentException::class); |
52
|
|
|
Assert::maxCount($combi, C::UNBOUNDED_LIMIT); |
53
|
|
|
Assert::allIsInstanceOf( |
54
|
|
|
$combi, |
55
|
|
|
SerializableElementInterface::class, |
56
|
|
|
InvalidArgumentException::class, |
57
|
|
|
); |
58
|
|
|
Assert::nullOrValidNCName($Id); |
59
|
|
|
|
60
|
|
|
$this->setElements($children); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Collect the value of the Id-property |
66
|
|
|
* |
67
|
|
|
* @return string|null |
68
|
|
|
*/ |
69
|
|
|
public function getId(): ?string |
70
|
|
|
{ |
71
|
|
|
return $this->Id; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Collect the value of the info-property |
77
|
|
|
* |
78
|
|
|
* @return list<\SimpleSAML\XML\SerializableElementInterface> |
|
|
|
|
79
|
|
|
*/ |
80
|
|
|
public function getInfo(): array |
81
|
|
|
{ |
82
|
|
|
return array_merge($this->info, $this->getElements()); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Convert XML into a KeyInfo |
88
|
|
|
* |
89
|
|
|
* @param \DOMElement $xml The XML element we should load |
90
|
|
|
* @return static |
91
|
|
|
* |
92
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
93
|
|
|
* If the qualified name of the supplied element is wrong |
94
|
|
|
*/ |
95
|
|
|
public static function fromXML(DOMElement $xml): static |
96
|
|
|
{ |
97
|
|
|
Assert::same($xml->localName, 'KeyInfo', InvalidDOMElementException::class); |
98
|
|
|
Assert::same($xml->namespaceURI, KeyInfo::NS, InvalidDOMElementException::class); |
99
|
|
|
|
100
|
|
|
$Id = self::getOptionalAttribute($xml, 'Id', null); |
101
|
|
|
|
102
|
|
|
$keyName = KeyName::getChildrenOfClass($xml); |
103
|
|
|
$keyValue = KeyValue::getChildrenOfClass($xml); |
104
|
|
|
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml); |
105
|
|
|
$x509Data = X509Data::getChildrenOfClass($xml); |
106
|
|
|
//$pgpData = PGPData::getChildrenOfClass($xml); |
107
|
|
|
//$spkiData = SPKIData::getChildrenOfClass($xml); |
108
|
|
|
//$mgmtData = MgmtData::getChildrenOfClass($xml); |
109
|
|
|
|
110
|
|
|
$info = array_merge( |
111
|
|
|
$keyName, |
112
|
|
|
$keyValue, |
113
|
|
|
$retrievalMethod, |
114
|
|
|
$x509Data, |
115
|
|
|
//$pgpdata, |
116
|
|
|
//$spkidata, |
117
|
|
|
//$mgmtdata, |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$children = self::getChildElementsFromXML($xml); |
121
|
|
|
return new static($info, $children, $Id); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Convert this KeyInfo to XML. |
127
|
|
|
* |
128
|
|
|
* @param \DOMElement|null $parent The element we should append this KeyInfo to. |
129
|
|
|
* @return \DOMElement |
130
|
|
|
*/ |
131
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
132
|
|
|
{ |
133
|
|
|
$e = $this->instantiateParentElement($parent); |
134
|
|
|
|
135
|
|
|
if ($this->getId() !== null) { |
136
|
|
|
$e->setAttribute('Id', $this->getId()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
foreach ($this->getInfo() as $elt) { |
140
|
|
|
$elt->toXML($e); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $e; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|