Skip to content

Commit bf21fb6

Browse files
committed
Add saml11-custom version of the CanonicalizableElementTrait to deny any transforms we don't know
1 parent 670482d commit bf21fb6

4 files changed

Lines changed: 87 additions & 3 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML11\XML;
6+
7+
use DOMElement;
8+
use SimpleSAML\SAML11\Assert\Assert;
9+
use SimpleSAML\XMLSecurity\Constants as C;
10+
use SimpleSAML\XMLSecurity\Exception\CanonicalizationFailedException;
11+
use SimpleSAML\XMLSecurity\Exception\ReferenceValidationFailedException;
12+
use SimpleSAML\XMLSecurity\XML\CanonicalizableElementTrait as BaseCanonicalizableElementTrait;
13+
use SimpleSAML\XMLSecurity\XML\ds\Transforms;
14+
15+
/**
16+
* A trait implementing the CanonicalizableElementInterface.
17+
*
18+
* @package simplesamlphp/saml11
19+
*/
20+
trait CanonicalizableElementTrait
21+
{
22+
use BaseCanonicalizableElementTrait;
23+
24+
25+
/**
26+
* Process all transforms specified by a given Reference element.
27+
*
28+
* @param \SimpleSAML\XMLSecurity\XML\ds\Transforms $transforms The transforms to apply.
29+
* @param \DOMElement $data The data referenced.
30+
*
31+
* @return string The canonicalized data after applying all transforms specified by $ref.
32+
*
33+
* @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel
34+
*/
35+
public function processTransforms(
36+
Transforms $transforms,
37+
DOMElement $data,
38+
): string {
39+
Assert::maxCount(
40+
$transforms->getTransform(),
41+
C::MAX_TRANSFORMS,
42+
ReferenceValidationFailedException::class,
43+
'Too many transforms.',
44+
);
45+
46+
$canonicalMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS;
47+
$arXPath = null;
48+
$prefixList = null;
49+
50+
foreach ($transforms->getTransform() as $transform) {
51+
$canonicalMethod = $transform->getAlgorithm()->getValue();
52+
switch ($canonicalMethod) {
53+
case C::XMLDSIG_ENVELOPED:
54+
break;
55+
case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS:
56+
case C::C14N_EXCLUSIVE_WITH_COMMENTS:
57+
$inclusiveNamespaces = $transform->getInclusiveNamespaces();
58+
if ($inclusiveNamespaces !== null) {
59+
$prefixes = $inclusiveNamespaces->getPrefixes();
60+
if ($prefixes !== null) {
61+
$prefixList = array_map('strval', $prefixes->toArray());
62+
}
63+
}
64+
break;
65+
default:
66+
throw new CanonicalizationFailedException(sprintf(
67+
'Message rejected due to unsupported canonicalization transform; %s',
68+
$canonicalMethod,
69+
));
70+
}
71+
}
72+
73+
return $this->canonicalizeData($data, $canonicalMethod, $arXPath, $prefixList);
74+
}
75+
}

src/SAML11/XML/SignableElementTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use SimpleSAML\XMLSecurity\Constants as C;
1515
use SimpleSAML\XMLSecurity\Exception\RuntimeException;
1616
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException;
17-
use SimpleSAML\XMLSecurity\Utils\XML;
1817
use SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod;
1918
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
2019
use SimpleSAML\XMLSecurity\XML\ds\Signature;
@@ -110,7 +109,7 @@ protected function doSign(DOMElement $xml): DOMElement
110109
),
111110
]);
112111

113-
$canonicalDocument = XML::processTransforms($transforms, $xml);
112+
$canonicalDocument = $this->processTransforms($transforms, $xml);
114113

115114
$signedInfo = new SignedInfo(
116115
new CanonicalizationMethod(

src/SAML11/XML/saml/AbstractAssertionType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
use SimpleSAML\SAML11\Type\SAMLDateTimeValue;
1313
use SimpleSAML\SAML11\Type\SAMLStringValue;
1414
use SimpleSAML\SAML11\Utils\XPath;
15+
use SimpleSAML\SAML11\XML\CanonicalizableElementTrait;
1516
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
1617
use SimpleSAML\XMLSchema\Exception\MissingElementException;
1718
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
1819
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
1920
use SimpleSAML\XMLSchema\Type\IDValue;
2021
use SimpleSAML\XMLSchema\Type\NonNegativeIntegerValue;
22+
use SimpleSAML\XMLSecurity\XML\CanonicalizableElementInterface;
2123
use SimpleSAML\XMLSecurity\XML\ds\Signature;
2224
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
2325
use SimpleSAML\XMLSecurity\XML\SignableElementTrait;
@@ -36,9 +38,11 @@
3638
* @package simplesamlphp/saml11
3739
*/
3840
abstract class AbstractAssertionType extends AbstractSamlElement implements
41+
CanonicalizableElementInterface,
3942
SignableElementInterface,
4043
SignedElementInterface
4144
{
45+
use CanonicalizableElementTrait;
4246
use SignableElementTrait;
4347
use SignedElementTrait;
4448

src/SAML11/XML/samlp/AbstractMessage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use DOMElement;
88
use SimpleSAML\SAML11\Type\SAMLDateTimeValue;
9+
use SimpleSAML\SAML11\XML\CanonicalizableElementTrait;
910
use SimpleSAML\SAML11\XML\SignableElementTrait;
1011
use SimpleSAML\SAML11\XML\SignedElementTrait;
1112
use SimpleSAML\XMLSchema\Type\NonNegativeIntegerValue;
13+
use SimpleSAML\XMLSecurity\XML\CanonicalizableElementInterface;
1214
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
1315
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
1416

@@ -22,8 +24,12 @@
2224
*
2325
* @package simplesamlphp/saml11
2426
*/
25-
abstract class AbstractMessage extends AbstractSamlpElement implements SignableElementInterface, SignedElementInterface
27+
abstract class AbstractMessage extends AbstractSamlpElement implements
28+
CanonicalizableElementInterface,
29+
SignableElementInterface,
30+
SignedElementInterface
2631
{
32+
use CanonicalizableElementTrait;
2733
use SignableElementTrait;
2834
use SignedElementTrait {
2935
SignedElementTrait::getBlacklistedAlgorithms insteadof SignableElementTrait;

0 commit comments

Comments
 (0)