|
5 | 5 | namespace SimpleSAML\XMLSecurity\XML\ds; |
6 | 6 |
|
7 | 7 | use DOMElement; |
8 | | -use Exception; |
9 | 8 | use SimpleSAML\Assert\Assert; |
| 9 | +use SimpleSAML\XML\Chunk; |
| 10 | +use SimpleSAML\XML\Constants; |
10 | 11 | use SimpleSAML\XML\Exception\InvalidDOMElementException; |
11 | | -use SimpleSAML\XML\Utils as XMLUtils; |
12 | | -use SimpleSAML\XMLSecurity\Utils\Certificate; |
13 | | -use SimpleSAML\XMLSecurity\Utils\Security; |
14 | | -use SimpleSAML\XMLSecurity\XMLSecurityDSig; |
15 | | -use SimpleSAML\XMLSecurity\XMLSecurityKey; |
16 | 12 |
|
17 | 13 | /** |
18 | | - * Wrapper class for XML signatures |
| 14 | + * Class representing a ds:Signature element. |
19 | 15 | * |
20 | 16 | * @package simplesamlphp/xml-security |
21 | 17 | */ |
22 | 18 | final class Signature extends AbstractDsElement |
23 | 19 | { |
24 | | - /** @var string */ |
25 | | - protected string $algorithm; |
26 | | - |
27 | | - /** @var string[] */ |
28 | | - protected array $certificates = []; |
| 20 | + /** @var \SimpleSAML\XMLSecurity\XML\ds\SignedInfo */ |
| 21 | + protected SignedInfo $signedInfo; |
29 | 22 |
|
30 | 23 | /** @var \SimpleSAML\XMLSecurity\XML\ds\SignatureValue */ |
31 | | - protected SignatureValue $value; |
| 24 | + protected SignatureValue $signatureValue; |
| 25 | + |
| 26 | + /** @var \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo */ |
| 27 | + protected ?KeyInfo $keyInfo; |
32 | 28 |
|
33 | | - /** @var \SimpleSAML\XMLSecurity\XMLSecurityKey|null */ |
34 | | - protected ?XMLSecurityKey $key; |
| 29 | + /** @var \SimpleSAML\XML\Chunk[] */ |
| 30 | + protected array $objects; |
35 | 31 |
|
36 | | - /** @var \SimpleSAML\XMLSecurity\XMLSecurityDSig */ |
37 | | - protected XMLSecurityDSig $signer; |
| 32 | + /** @var string|null */ |
| 33 | + protected ?string $Id; |
38 | 34 |
|
39 | 35 |
|
40 | 36 | /** |
41 | 37 | * Signature constructor. |
42 | 38 | * |
43 | | - * @param string $algorithm |
44 | | - * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $value |
45 | | - * @param string[] $certificates |
46 | | - * @param \SimpleSAML\XMLSecurity\XMLSecurityKey|null $key |
47 | | - * |
48 | | - * @throws \Exception |
| 39 | + * @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo $signedInfo |
| 40 | + * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue |
| 41 | + * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo |
| 42 | + * @param \SimpleSAML\XML\Chunk[] $objects |
| 43 | + * @param string|null $Id |
49 | 44 | */ |
50 | 45 | public function __construct( |
51 | | - string $algorithm, |
52 | | - SignatureValue $value, |
53 | | - array $certificates = [], |
54 | | - ?XMLSecurityKey $key = null |
| 46 | + SignedInfo $signedInfo, |
| 47 | + SignatureValue $signatureValue, |
| 48 | + ?KeyInfo $keyInfo, |
| 49 | + array $objects = [], |
| 50 | + ?string $Id = null |
55 | 51 | ) { |
56 | | - $this->setAlgorithm($algorithm); |
57 | | - $this->setCertificates($certificates); |
58 | | - $this->setKey($key); |
59 | | - |
60 | | - $this->signer = new XMLSecurityDSig(); |
61 | | - $this->signer->idKeys[] = 'ID'; |
| 52 | + $this->setSignedInfo($signedInfo); |
| 53 | + $this->setSignatureValue($signatureValue); |
| 54 | + $this->setKeyInfo($keyInfo); |
| 55 | + $this->setObjects($objects); |
| 56 | + $this->setId($Id); |
62 | 57 | } |
63 | 58 |
|
64 | 59 |
|
65 | 60 | /** |
66 | | - * Get the algorithm used by this signature. |
| 61 | + * Get the Id used for this signature. |
67 | 62 | * |
68 | | - * @return string |
| 63 | + * @return string|null |
69 | 64 | */ |
70 | | - public function getAlgorithm(): string |
| 65 | + public function getId(): ?string |
71 | 66 | { |
72 | | - return $this->algorithm; |
| 67 | + return $this->Id; |
73 | 68 | } |
74 | 69 |
|
75 | 70 |
|
76 | 71 | /** |
77 | | - * Set the algorithm used by this signature. |
| 72 | + * Set the Id used for this signature. |
78 | 73 | * |
79 | | - * @param string $algorithm |
| 74 | + * @param string|null $Id |
80 | 75 | */ |
81 | | - protected function setAlgorithm(string $algorithm): void |
| 76 | + protected function setId(?string $Id): void |
82 | 77 | { |
83 | | - Assert::notWhitespaceOnly($algorithm, 'Signature algorithm cannot be empty'); |
84 | | - $this->algorithm = $algorithm; |
| 78 | + $this->Id = $Id; |
85 | 79 | } |
86 | 80 |
|
87 | 81 |
|
88 | 82 | /** |
89 | | - * Get the array of certificates attached to this signature. |
90 | | - * |
91 | | - * @return string[] |
| 83 | + * @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo |
92 | 84 | */ |
93 | | - public function getCertificates(): array |
| 85 | + protected function setSignedInfo(SignedInfo $signedInfo): void |
94 | 86 | { |
95 | | - return $this->certificates; |
| 87 | + $this->signedInfo = $signedInfo; |
96 | 88 | } |
97 | 89 |
|
98 | 90 |
|
99 | 91 | /** |
100 | | - * Set the array of certificates (in PEM format) attached to this signature. |
101 | | - * |
102 | | - * @param string[] $certificates |
| 92 | + * @return \SimpleSAML\XMLSecurity\XML\ds\SignedInfo |
103 | 93 | */ |
104 | | - protected function setCertificates(array $certificates): void |
| 94 | + public function getSignedInfo(): SignedInfo |
105 | 95 | { |
106 | | - Assert::allStringNotEmpty($certificates, 'Cannot add empty certificates.'); |
107 | | - Assert::allTrue( |
108 | | - array_map([Certificate::class, 'hasValidStructure'], $certificates), |
109 | | - 'One or more certificates have an invalid format.' |
110 | | - ); |
111 | | - $this->certificates = $certificates; |
| 96 | + return $this->signedInfo; |
112 | 97 | } |
113 | 98 |
|
114 | 99 |
|
115 | 100 | /** |
116 | | - * @param \SimpleSAML\XMLSecurity\XMLSecurityKey|null $key |
| 101 | + * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue |
117 | 102 | */ |
118 | | - protected function setKey(?XMLSecurityKey $key): void |
| 103 | + protected function setSignatureValue(SignatureValue $signatureValue): void |
119 | 104 | { |
120 | | - if ($key !== null) { |
121 | | - Assert::eq($this->algorithm, $key->getAlgorithm(), 'Key type does not match signature algorithm.'); |
122 | | - } |
123 | | - $this->key = $key; |
| 105 | + $this->signatureValue = $signatureValue; |
124 | 106 | } |
125 | 107 |
|
126 | 108 |
|
127 | 109 | /** |
128 | | - * Get the SignatureValue corresponding to this signature. |
129 | | - * |
130 | 110 | * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureValue |
131 | 111 | */ |
132 | 112 | public function getSignatureValue(): SignatureValue |
133 | 113 | { |
134 | | - return $this->value; |
| 114 | + return $this->signatureValue; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + /** |
| 119 | + * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null |
| 120 | + */ |
| 121 | + protected function setKeyInfo(?KeyInfo $keyInfo): void |
| 122 | + { |
| 123 | + $this->keyInfo = $keyInfo; |
135 | 124 | } |
136 | 125 |
|
137 | 126 |
|
138 | 127 | /** |
139 | | - * Set the SignatureValue. |
| 128 | + * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null |
| 129 | + */ |
| 130 | + public function getKeyInfo(): ?KeyInfo |
| 131 | + { |
| 132 | + return $this->keyInfo; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + /** |
| 137 | + * Get the array of ds:Object elements attached to this signature. |
140 | 138 | * |
141 | | - * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $value |
| 139 | + * @return \SimpleSAML\XML\Chunk[] |
142 | 140 | */ |
143 | | - protected function setSignatureValue(SignatureValue $value): void |
| 141 | + public function getObjects(): array |
144 | 142 | { |
145 | | - $this->value = $value; |
| 143 | + return $this->objects; |
146 | 144 | } |
147 | 145 |
|
148 | 146 |
|
149 | 147 | /** |
150 | | - * @return XMLSecurityDSig |
| 148 | + * Set the array of ds:Object elements attached to this signature. |
| 149 | + * |
| 150 | + * @param \SimpleSAML\XML\Chunk[] $objects |
151 | 151 | */ |
152 | | - public function getSigner(): XMLSecurityDSig |
| 152 | + protected function setObjects(array $objects): void |
153 | 153 | { |
154 | | - return $this->signer; |
| 154 | + Assert::allIsInstanceOf($objects, Chunk::class); |
| 155 | + |
| 156 | + foreach ($objects as $o) { |
| 157 | + Assert::true( |
| 158 | + $o->getNamespaceURI() === Constants::NS_XDSIG |
| 159 | + && $o->getLocalName() === 'Object', |
| 160 | + 'Only elements of type ds:Object are allowed.' |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + $this->objects = $objects; |
155 | 165 | } |
156 | 166 |
|
157 | 167 |
|
158 | 168 | /** |
159 | | - * @param \DOMElement $xml |
| 169 | + * Convert XML into a Signature element |
160 | 170 | * |
| 171 | + * @param \DOMElement $xml |
161 | 172 | * @return \SimpleSAML\XML\AbstractXMLElement |
162 | | - * @throws \Exception |
163 | 173 | * |
164 | 174 | * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
165 | 175 | * If the qualified name of the supplied element is wrong |
166 | | - * @throws \SimpleSAML\XML\Exception\MissingAttributeException |
167 | | - * If the supplied signature is missing an Algorithm attribute |
168 | 176 | */ |
169 | 177 | public static function fromXML(DOMElement $xml): object |
170 | 178 | { |
171 | 179 | Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class); |
172 | 180 | Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class); |
173 | 181 |
|
174 | | - $parent = $xml->parentNode; |
175 | | - |
176 | | - $sigMethod = XMLUtils::xpQuery($xml, './ds:SignedInfo/ds:SignatureMethod'); |
177 | | - Assert::notEmpty($sigMethod, 'Missing ds:SignatureMethod element.'); |
178 | | - /** @var \DOMElement $sigMethod */ |
179 | | - $sigMethod = $sigMethod[0]; |
180 | | - Assert::true( |
181 | | - $sigMethod->hasAttribute('Algorithm'), |
182 | | - 'Missing "Algorithm" attribute on ds:SignatureMethod element.' |
183 | | - ); |
184 | | - |
185 | | - // now we extract all available X509 certificates in the signature element |
186 | | - $certificates = []; |
187 | | - foreach (XMLUtils::xpQuery($xml, './ds:KeyInfo/ds:X509Data/ds:X509Certificate') as $certNode) { |
188 | | - $certificates[] = Certificate::convertToCertificate( |
189 | | - str_replace(["\r", "\n", "\t", ' '], '', trim($certNode->textContent)) |
190 | | - ); |
191 | | - } |
192 | | - |
193 | | - $value = SignatureValue::getChildrenOfClass($xml); |
194 | | - Assert::count($value, 1, 'ds:Signature needs exactly one ds:SignatureValue'); |
195 | | - |
196 | | - $signature = new self(self::getAttribute($sigMethod, 'Algorithm'), $value[0], $certificates); |
| 182 | + $Id = self::getAttribute($xml, 'Id', null); |
197 | 183 |
|
198 | | - $signature->signer->sigNode = $xml; |
| 184 | + $signedInfo = SignedInfo::getChildrenOfClass($xml); |
| 185 | + Assert::count($signedInfo, 1, 'ds:Signature needs exactly one ds:SignedInfo element.'); |
199 | 186 |
|
200 | | - // canonicalize the XMLDSig SignedInfo element in the message |
201 | | - $signature->signer->canonicalizeSignedInfo(); |
| 187 | + $signatureValue = SignatureValue::getChildrenOfClass($xml); |
| 188 | + Assert::count($signatureValue, 1, 'ds:Signature needs exactly one ds:SignatureValue element.'); |
202 | 189 |
|
203 | | - // validate referenced xml nodes |
204 | | - if (!$signature->signer->validateReference()) { |
205 | | - throw new Exception('Digest validation failed.'); |
206 | | - } |
| 190 | + $keyInfo = KeyInfo::getChildrenOfClass($xml); |
| 191 | + Assert::maxCount($keyInfo, 1, 'ds:Signature can hold a maximum of one ds:KeyInfo element.'); |
207 | 192 |
|
208 | | - // check that $root is one of the signed nodes |
209 | | - $rootSigned = false; |
210 | | - /** @var \DOMNode $signedNode */ |
211 | | - foreach ($signature->signer->getValidatedNodes() as $signedNode) { |
212 | | - if ($signedNode->isSameNode($parent)) { |
213 | | - $rootSigned = true; |
214 | | - break; |
215 | | - } elseif ($parent->parentNode instanceof \DOMDocument && $signedNode->isSameNode($parent->ownerDocument)) { |
216 | | - // $parent is the root element of a signed document |
217 | | - $rootSigned = true; |
218 | | - break; |
| 193 | + $objects = []; |
| 194 | + foreach ($xml->childNodes as $o) { |
| 195 | + if ( |
| 196 | + $o instanceof DOMElement |
| 197 | + && $o->namespaceURI === Constants::NS_XDSIG |
| 198 | + && $o->localName === 'Object' |
| 199 | + ) { |
| 200 | + $objects[] = Chunk::fromXML($o); |
219 | 201 | } |
220 | 202 | } |
221 | | - if (!$rootSigned) { |
222 | | - throw new Exception('The parent element is not signed.'); |
223 | | - } |
224 | 203 |
|
225 | | - return $signature; |
| 204 | + return new self( |
| 205 | + array_pop($signedInfo), |
| 206 | + array_pop($signatureValue), |
| 207 | + empty($keyInfo) ? null : array_pop($keyInfo), |
| 208 | + $objects, |
| 209 | + $Id |
| 210 | + ); |
226 | 211 | } |
227 | 212 |
|
228 | 213 |
|
229 | 214 | /** |
230 | | - * @param \DOMElement|null $parent |
| 215 | + * Convert this Signature element to XML. |
231 | 216 | * |
| 217 | + * @param \DOMElement|null $parent The element we should append this Signature element to. |
232 | 218 | * @return \DOMElement |
233 | | - * |
234 | | - * @psalm-suppress MoreSpecificReturnType |
235 | 219 | */ |
236 | 220 | public function toXML(DOMElement $parent = null): DOMElement |
237 | 221 | { |
238 | | - Assert::notNull($parent, 'Cannot create a Signature without anything to sign.'); |
239 | | - Assert::notNull($this->key, 'Cannot sign without a signing key.'); |
240 | | - |
241 | | - // find first child element |
242 | | - $childElements = XMLUtils::xpQuery($parent, './*'); |
243 | | - $firstChildElement = null; |
244 | | - if (count($childElements) > 0) { |
245 | | - $firstChildElement = $childElements[0]; |
| 222 | + $e = $this->instantiateParentElement($parent); |
| 223 | + |
| 224 | + if ($this->Id !== null) { |
| 225 | + $e->setAttribute('Id', $this->Id); |
246 | 226 | } |
247 | 227 |
|
248 | | - Security::insertSignature($this->key, $this->certificates, $parent, $firstChildElement); |
| 228 | + $this->signedInfo->toXML($e); |
| 229 | + $this->signatureValue->toXML($e); |
| 230 | + |
| 231 | + if ($this->keyInfo !== null) { |
| 232 | + $this->keyInfo->toXML($e); |
| 233 | + } |
| 234 | + |
| 235 | + foreach ($this->objects as $o) { |
| 236 | + $o->toXML($e); |
| 237 | + } |
249 | 238 |
|
250 | | - /** @psalm-suppress LessSpecificReturnStatement */ |
251 | | - return XMLUtils::xpQuery($parent, './ds:Signature')[0]; |
| 239 | + return $e; |
252 | 240 | } |
253 | 241 | } |
0 commit comments