1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
10
|
|
|
use SimpleSAML\XMLSecurity\Alg\SignatureAlgorithm; |
11
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
12
|
|
|
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
13
|
|
|
use SimpleSAML\XMLSecurity\Exception\RuntimeException; |
14
|
|
|
use SimpleSAML\XMLSecurity\Utils\Security; |
15
|
|
|
use SimpleSAML\XMLSecurity\Utils\XML; |
16
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod; |
17
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\DigestMethod; |
18
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\DigestValue; |
19
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo; |
20
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Reference; |
21
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Signature; |
22
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\SignatureMethod; |
23
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\SignatureValue; |
24
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\SignedInfo; |
25
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Transform; |
26
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Transforms; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Trait SignableElementTrait |
30
|
|
|
* |
31
|
|
|
* @package simplesamlphp/xml-security |
32
|
|
|
*/ |
33
|
|
|
trait SignableElementTrait |
34
|
|
|
{ |
35
|
|
|
use CanonicalizableElementTrait; |
36
|
|
|
|
37
|
|
|
/** @var \SimpleSAML\XMLSecurity\XML\ds\Signature|null */ |
38
|
|
|
protected ?Signature $signature = null; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
private string $c14nAlg = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS; |
42
|
|
|
|
43
|
|
|
/** @var \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null */ |
44
|
|
|
private ?KeyInfo $keyInfo = null; |
45
|
|
|
|
46
|
|
|
/** @var \SimpleSAML\XMLSecurity\Alg\SignatureAlgorithm|null */ |
47
|
|
|
private ?SignatureAlgorithm $signer = null; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the ID of this element. |
52
|
|
|
* |
53
|
|
|
* When this method returns null, the signature created for this object will reference the entire document. |
54
|
|
|
* |
55
|
|
|
* @return string|null The ID of this element, or null if we don't have one. |
56
|
|
|
*/ |
57
|
|
|
abstract public function getId(): ?string; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sign the current element. |
62
|
|
|
* |
63
|
|
|
* The signature will not be applied until toXML() is called. |
64
|
|
|
* |
65
|
|
|
* @param \SimpleSAML\XMLSecurity\Alg\SignatureAlgorithm $signer The actual signer implementation to use. |
66
|
|
|
* @param string $canonicalizationAlg The identifier of the canonicalization algorithm to use. |
67
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo A KeyInfo object to add to the signature. |
68
|
|
|
*/ |
69
|
|
|
public function sign( |
70
|
|
|
SignatureAlgorithm $signer, |
71
|
|
|
string $canonicalizationAlg = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, |
72
|
|
|
?KeyInfo $keyInfo = null |
73
|
|
|
): void { |
74
|
|
|
$this->signer = $signer; |
75
|
|
|
$this->keyInfo = $keyInfo; |
76
|
|
|
Assert::oneOf( |
77
|
|
|
$canonicalizationAlg, |
78
|
|
|
[ |
79
|
|
|
C::C14N_INCLUSIVE_WITH_COMMENTS, |
80
|
|
|
C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, |
81
|
|
|
C::C14N_EXCLUSIVE_WITH_COMMENTS, |
82
|
|
|
C::C14N_EXCLUSIVE_WITHOUT_COMMENTS |
83
|
|
|
], |
84
|
|
|
'Unsupported canonicalization algorithm', |
85
|
|
|
InvalidArgumentException::class |
86
|
|
|
); |
87
|
|
|
$this->c14nAlg = $canonicalizationAlg; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get a ds:Reference pointing to this object. |
93
|
|
|
* |
94
|
|
|
* @param string $digestAlg The digest algorithm to use. |
95
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\Transforms $transforms The transforms to apply to the object. |
96
|
|
|
*/ |
97
|
|
|
private function getReference( |
98
|
|
|
string $digestAlg, |
99
|
|
|
Transforms $transforms, |
100
|
|
|
DOMElement $xml, |
101
|
|
|
string $canonicalDocument |
102
|
|
|
): Reference { |
103
|
|
|
$id = $this->getId(); |
104
|
|
|
$uri = null; |
105
|
|
|
if (empty($id)) { // document reference |
106
|
|
|
Assert::notNull( |
107
|
|
|
$xml->ownerDocument->documentElement, |
108
|
|
|
'Cannot create a document reference without a root element in the document.', |
109
|
|
|
RuntimeException::class |
110
|
|
|
); |
111
|
|
|
Assert::true( |
112
|
|
|
$xml->isSameNode($xml->ownerDocument->documentElement), |
113
|
|
|
'Cannot create a document reference when signing an object that is not the root of the document. ' . |
114
|
|
|
'Please give your object an identifier.', |
115
|
|
|
RuntimeException::class |
116
|
|
|
); |
117
|
|
|
if (in_array($this->c14nAlg, [C::C14N_INCLUSIVE_WITH_COMMENTS, C::C14N_EXCLUSIVE_WITH_COMMENTS])) { |
118
|
|
|
$uri = '#xpointer(/)'; |
119
|
|
|
} |
120
|
|
|
} elseif (in_array($this->c14nAlg, [C::C14N_INCLUSIVE_WITH_COMMENTS, C::C14N_EXCLUSIVE_WITH_COMMENTS])) { |
121
|
|
|
// regular reference, but must retain comments |
122
|
|
|
$uri = '#xpointer(id(' . $id . '))'; |
123
|
|
|
} else { // regular reference, can ignore comments |
124
|
|
|
$uri = '#' . $id; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return new Reference( |
128
|
|
|
new DigestMethod($digestAlg), |
129
|
|
|
new DigestValue(Security::hash($digestAlg, $canonicalDocument)), |
130
|
|
|
$transforms, |
131
|
|
|
null, |
132
|
|
|
null, |
133
|
|
|
$uri |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Do the actual signing of the document. |
140
|
|
|
* |
141
|
|
|
* Note that this method does not insert the signature in the returned \DOMElement. The signature will be available |
142
|
|
|
* in $this->signature as a \SimpleSAML\XMLSecurity\XML\ds\Signature object, which can then be converted to XML |
143
|
|
|
* calling toXML() on it, passing the \DOMElement value returned here as a parameter. The resulting \DOMElement |
144
|
|
|
* can then be inserted in the position desired. |
145
|
|
|
* |
146
|
|
|
* E.g.: |
147
|
|
|
* $xml = // our XML to sign |
148
|
|
|
* $signedXML = $this->doSign($xml); |
149
|
|
|
* $signedXML->appendChild($this->signature->toXML($signedXML)); |
150
|
|
|
* |
151
|
|
|
* @param \DOMElement $xml The element to sign. |
152
|
|
|
* @return \DOMElement The signed element, without the signature attached to it just yet. |
153
|
|
|
*/ |
154
|
|
|
protected function doSign(DOMElement $xml): DOMElement |
155
|
|
|
{ |
156
|
|
|
Assert::notNull( |
157
|
|
|
$this->signer, |
158
|
|
|
'Cannot call toSignedXML() without calling sign() first.', |
159
|
|
|
RuntimeException::class |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
$algorithm = $this->signer->getAlgorithmId(); |
|
|
|
|
163
|
|
|
$digest = $this->signer->getDigest(); |
164
|
|
|
|
165
|
|
|
$transforms = new Transforms([ |
166
|
|
|
new Transform(C::XMLDSIG_ENVELOPED), |
167
|
|
|
new Transform($this->c14nAlg) |
168
|
|
|
]); |
169
|
|
|
|
170
|
|
|
$canonicalDocument = XML::processTransforms($transforms, $xml); |
171
|
|
|
|
172
|
|
|
$signedInfo = new SignedInfo( |
173
|
|
|
new CanonicalizationMethod($this->c14nAlg), |
174
|
|
|
new SignatureMethod($algorithm), |
175
|
|
|
[$this->getReference($digest, $transforms, $xml, $canonicalDocument)] |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
$signingData = $signedInfo->canonicalize($this->c14nAlg); |
179
|
|
|
$signedData = base64_encode($this->signer->sign($signingData)); |
|
|
|
|
180
|
|
|
|
181
|
|
|
$this->signature = new Signature($signedInfo, new SignatureValue($signedData), $this->keyInfo); |
182
|
|
|
return DOMDocumentFactory::fromString($canonicalDocument)->documentElement; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.