1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SAML2\Compat\ContainerSingleton; |
10
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
11
|
|
|
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmInterface; |
12
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
13
|
|
|
use SimpleSAML\XMLSecurity\Exception\RuntimeException; |
14
|
|
|
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException; |
15
|
|
|
use SimpleSAML\XMLSecurity\Utils\XML; |
16
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod; |
17
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo; |
18
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Signature; |
19
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\SignatureMethod; |
20
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\SignatureValue; |
21
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\SignedInfo; |
22
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Transform; |
23
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Transforms; |
24
|
|
|
use SimpleSAML\XMLSecurity\XML\SignableElementTrait as BaseSignableElementTrait; |
25
|
|
|
|
26
|
|
|
use function base64_encode; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Helper trait for processing signable elements. |
30
|
|
|
* |
31
|
|
|
* @package simplesamlphp/saml2 |
32
|
|
|
*/ |
33
|
|
|
trait SignableElementTrait |
34
|
|
|
{ |
35
|
|
|
use BaseSignableElementTrait; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sign the current element. |
40
|
|
|
* |
41
|
|
|
* The signature will not be applied until toXML() is called. |
42
|
|
|
* |
43
|
|
|
* @param \SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmInterface $signer The actual signer implementation |
44
|
|
|
* to use. |
45
|
|
|
* @param string $canonicalizationAlg The identifier of the canonicalization algorithm to use. |
46
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo A KeyInfo object to add to the signature. |
47
|
|
|
*/ |
48
|
|
|
public function sign( |
49
|
|
|
SignatureAlgorithmInterface $signer, |
50
|
|
|
string $canonicalizationAlg = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, |
51
|
|
|
?KeyInfo $keyInfo = null, |
52
|
|
|
): void { |
53
|
|
|
/** |
54
|
|
|
* 5.4.2: SAML assertions and protocol messages MUST supply a value for the ID attribute |
55
|
|
|
* on the root element of the assertion or protocol message being signed. |
56
|
|
|
*/ |
57
|
|
|
Assert::notNull($this->getID(), "Signable element must have an ID set before it can be signed."); |
58
|
|
|
|
59
|
|
|
$this->signer = $signer; |
60
|
|
|
$this->keyInfo = $keyInfo; |
61
|
|
|
Assert::oneOf( |
62
|
|
|
$canonicalizationAlg, |
63
|
|
|
[ |
64
|
|
|
C::C14N_INCLUSIVE_WITH_COMMENTS, |
65
|
|
|
C::C14N_INCLUSIVE_WITHOUT_COMMENTS, |
66
|
|
|
C::C14N_EXCLUSIVE_WITH_COMMENTS, |
67
|
|
|
C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, |
68
|
|
|
], |
69
|
|
|
'Unsupported canonicalization algorithm: %s', |
70
|
|
|
UnsupportedAlgorithmException::class, |
71
|
|
|
); |
72
|
|
|
$this->c14nAlg = $canonicalizationAlg; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Do the actual signing of the document. |
78
|
|
|
* |
79
|
|
|
* Note that this method does not insert the signature in the returned \DOMElement. The signature will be available |
80
|
|
|
* in $this->signature as a \SimpleSAML\XMLSecurity\XML\ds\Signature object, which can then be converted to XML |
81
|
|
|
* calling toXML() on it, passing the \DOMElement value returned here as a parameter. The resulting \DOMElement |
82
|
|
|
* can then be inserted in the position desired. |
83
|
|
|
* |
84
|
|
|
* E.g.: |
85
|
|
|
* $xml = // our XML to sign |
86
|
|
|
* $signedXML = $this->doSign($xml); |
87
|
|
|
* $signedXML->appendChild($this->signature->toXML($signedXML)); |
88
|
|
|
* |
89
|
|
|
* @param \DOMElement $xml The element to sign. |
90
|
|
|
* @return \DOMElement The signed element, without the signature attached to it just yet. |
91
|
|
|
*/ |
92
|
|
|
protected function doSign(DOMElement $xml): DOMElement |
93
|
|
|
{ |
94
|
|
|
Assert::notNull( |
95
|
|
|
$this->signer, |
96
|
|
|
'Cannot call toSignedXML() without calling sign() first.', |
97
|
|
|
RuntimeException::class, |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$algorithm = $this->signer->getAlgorithmId(); |
|
|
|
|
101
|
|
|
$digest = $this->signer->getDigest(); |
102
|
|
|
|
103
|
|
|
$transforms = new Transforms([ |
104
|
|
|
/** |
105
|
|
|
* 5.4.1: SAML assertions and protocols MUST use enveloped signatures when |
106
|
|
|
* signing assertions and protocol messages |
107
|
|
|
*/ |
108
|
|
|
new Transform(C::XMLDSIG_ENVELOPED), |
109
|
|
|
new Transform($this->c14nAlg), |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$canonicalDocument = XML::processTransforms($transforms, $xml); |
113
|
|
|
|
114
|
|
|
$signedInfo = new SignedInfo( |
115
|
|
|
new CanonicalizationMethod($this->c14nAlg), |
116
|
|
|
new SignatureMethod($algorithm), |
117
|
|
|
[$this->getReference($digest, $transforms, $xml, $canonicalDocument)], |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$signingData = $signedInfo->canonicalize($this->c14nAlg); |
121
|
|
|
$signedData = base64_encode($this->signer->sign($signingData)); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->signature = new Signature($signedInfo, new SignatureValue($signedData), $this->keyInfo); |
124
|
|
|
return DOMDocumentFactory::fromString($canonicalDocument)->documentElement; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
public function getBlacklistedAlgorithms(): ?array |
129
|
|
|
{ |
130
|
|
|
$container = ContainerSingleton::getInstance(); |
131
|
|
|
return $container->getBlacklistedEncryptionAlgorithms(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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.