-
Notifications
You must be signed in to change notification settings - Fork 2
/
Signature.php
182 lines (153 loc) · 4.74 KB
/
Signature.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
namespace SimpleSAML\XMLSecurity\XML\ds;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XMLSecurity\Constants as C;
use function array_pop;
/**
* Class representing a ds:Signature element.
*
* @package simplesamlphp/xml-security
*/
final class Signature extends AbstractDsElement
{
/**
* Signature constructor.
*
* @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo $signedInfo
* @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue
* @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo
* @param \SimpleSAML\XMLSecurity\XML\ds\DsObject[] $objects
* @param string|null $Id
*/
public function __construct(
protected SignedInfo $signedInfo,
protected SignatureValue $signatureValue,
protected ?KeyInfo $keyInfo,
protected array $objects = [],
protected ?string $Id = null,
) {
Assert::maxCount($objects, C::UNBOUNDED_LIMIT);
Assert::allIsInstanceOf($objects, DsObject::class);
Assert::nullOrValidNCName($Id);
}
/**
* Get the Id used for this signature.
*
* @return string|null
*/
public function getId(): ?string
{
return $this->Id;
}
/**
* @return \SimpleSAML\XMLSecurity\XML\ds\SignedInfo
*/
public function getSignedInfo(): SignedInfo
{
return $this->signedInfo;
}
/**
* @return \SimpleSAML\XMLSecurity\XML\ds\SignatureValue
*/
public function getSignatureValue(): SignatureValue
{
return $this->signatureValue;
}
/**
* @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
*/
public function getKeyInfo(): ?KeyInfo
{
return $this->keyInfo;
}
/**
* Get the array of ds:Object elements attached to this signature.
*
* @return \SimpleSAML\XMLSecurity\XML\ds\DsObject[]
*/
public function getObjects(): array
{
return $this->objects;
}
/**
* Convert XML into a Signature element
*
* @param \DOMElement $xml
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class);
$Id = self::getOptionalAttribute($xml, 'Id', null);
$signedInfo = SignedInfo::getChildrenOfClass($xml);
Assert::minCount(
$signedInfo,
1,
'ds:Signature needs exactly one ds:SignedInfo element.',
MissingElementException::class,
);
Assert::maxCount(
$signedInfo,
1,
'ds:Signature needs exactly one ds:SignedInfo element.',
TooManyElementsException::class,
);
$signatureValue = SignatureValue::getChildrenOfClass($xml);
Assert::minCount(
$signatureValue,
1,
'ds:Signature needs exactly one ds:SignatureValue element.',
MissingElementException::class,
);
Assert::maxCount(
$signatureValue,
1,
'ds:Signature needs exactly one ds:SignatureValue element.',
TooManyElementsException::class,
);
$keyInfo = KeyInfo::getChildrenOfClass($xml);
Assert::maxCount(
$keyInfo,
1,
'ds:Signature can hold a maximum of one ds:KeyInfo element.',
TooManyElementsException::class,
);
$objects = DsObject::getChildrenOfClass($xml);
return new static(
array_pop($signedInfo),
array_pop($signatureValue),
empty($keyInfo) ? null : array_pop($keyInfo),
$objects,
$Id,
);
}
/**
* Convert this Signature element to XML.
*
* @param \DOMElement|null $parent The element we should append this Signature element to.
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
if ($this->getId() !== null) {
$e->setAttribute('Id', $this->getId());
}
$this->getSignedInfo()->toXML($e);
$this->getSignatureValue()->toXML($e);
$this->getKeyInfo()?->toXML($e);
foreach ($this->getObjects() as $o) {
$o->toXML($e);
}
return $e;
}
}