Skip to content

Commit 2a2c3bc

Browse files
committed
Un-nest CanonicalizableElementTrait from Signed/SignableElementTrait
1 parent 536f014 commit 2a2c3bc

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed
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\SAML2\XML;
6+
7+
use DOMElement;
8+
use SimpleSAML\XMLSecurity\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/xml-security
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/XML/md/AbstractSignedMdElement.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace SimpleSAML\SAML2\XML\md;
66

77
use DOMElement;
8+
use SimpleSAML\SAML2\XML\CanonicalizableElementTrait;
89
use SimpleSAML\SAML2\XML\SignableElementTrait;
910
use SimpleSAML\SAML2\XML\SignedElementTrait;
11+
use SimpleSAML\XMLSecurity\XML\CanonicalizableElementInterface;
1012
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
1113
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
1214

@@ -18,9 +20,11 @@
1820
* @package simplesamlphp/saml2
1921
*/
2022
abstract class AbstractSignedMdElement extends AbstractMdElement implements
23+
CanonicalizableElementInterface,
2124
SignableElementInterface,
2225
SignedElementInterface
2326
{
27+
use CanonicalizableElementTrait;
2428
use SignableElementTrait;
2529
use SignedElementTrait {
2630
SignedElementTrait::getBlacklistedAlgorithms insteadof SignableElementTrait;

src/XML/saml/Assertion.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
1414
use SimpleSAML\SAML2\Type\SAMLStringValue;
1515
use SimpleSAML\SAML2\Utils\XPath;
16+
use SimpleSAML\SAML2\XML\CanonicalizableElementTrait;
1617
use SimpleSAML\SAML2\XML\EncryptableElementTrait;
1718
use SimpleSAML\SAML2\XML\SignableElementTrait;
1819
use SimpleSAML\SAML2\XML\SignedElementTrait;
@@ -23,6 +24,7 @@
2324
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
2425
use SimpleSAML\XMLSchema\Type\IDValue;
2526
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
27+
use SimpleSAML\XMLSecurity\XML\CanonicalizableElementInterface;
2628
use SimpleSAML\XMLSecurity\XML\ds\Signature;
2729
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
2830
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
@@ -41,17 +43,17 @@
4143
* @package simplesamlphp/saml2
4244
*/
4345
final class Assertion extends AbstractSamlElement implements
46+
CanonicalizableElementInterface,
4447
EncryptableElementInterface,
4548
SchemaValidatableElementInterface,
4649
SignableElementInterface,
4750
SignedElementInterface
4851
{
52+
use CanonicalizableElementTrait;
4953
use EncryptableElementTrait {
5054
EncryptableElementTrait::getBlacklistedAlgorithms insteadof SignedElementTrait;
5155
EncryptableElementTrait::getBlacklistedAlgorithms insteadof SignableElementTrait;
5256
}
53-
54-
5557
use SchemaValidatableElementTrait;
5658
use SignableElementTrait;
5759
use SignedElementTrait;

src/XML/samlp/AbstractMessage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
99
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
1010
use SimpleSAML\SAML2\Utils\XPath;
11+
use SimpleSAML\SAML2\XML\CanonicalizableElementTrait;
1112
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
1213
use SimpleSAML\SAML2\XML\saml\Issuer;
1314
use SimpleSAML\SAML2\XML\SignableElementTrait;
1415
use SimpleSAML\SAML2\XML\SignedElementTrait;
1516
use SimpleSAML\XMLSchema\Type\IDValue;
17+
use SimpleSAML\XMLSecurity\XML\CanonicalizableElementInterface;
1618
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
1719
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
1820

@@ -26,8 +28,12 @@
2628
*
2729
* @package simplesamlphp/saml2
2830
*/
29-
abstract class AbstractMessage extends AbstractSamlpElement implements SignableElementInterface, SignedElementInterface
31+
abstract class AbstractMessage extends AbstractSamlpElement implements
32+
CanonicalizableElementInterface,
33+
SignableElementInterface,
34+
SignedElementInterface
3035
{
36+
use CanonicalizableElementTrait;
3137
use ExtendableElementTrait;
3238
use SignableElementTrait;
3339
use SignedElementTrait {

0 commit comments

Comments
 (0)