Skip to content

Commit c61e284

Browse files
committed
Fixes to reflect upstream changes in xml-security
1 parent 0faddd5 commit c61e284

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\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\XML\CanonicalizableElementTrait as BaseCanonicalizableElementTrait;
12+
use SimpleSAML\XMLSecurity\XML\ds\Transforms;
13+
14+
/**
15+
* A trait implementing the CanonicalizableElementInterface.
16+
*
17+
* @package simplesamlphp/xml-security
18+
*/
19+
trait CanonicalizableElementTrait
20+
{
21+
use BaseCanonicalizableElementTrait;
22+
23+
24+
/**
25+
* Process all transforms specified by a given Reference element.
26+
*
27+
* @param \SimpleSAML\XMLSecurity\XML\ds\Transforms $transforms The transforms to apply.
28+
* @param \DOMElement $data The data referenced.
29+
*
30+
* @return string The canonicalized data after applying all transforms specified by $ref.
31+
*
32+
* @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel
33+
*/
34+
#[Override]
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::C14N_EXCLUSIVE_WITHOUT_COMMENTS:
54+
case C::C14N_EXCLUSIVE_WITH_COMMENTS:
55+
$inclusiveNamespaces = $transform->getInclusiveNamespaces();
56+
if ($inclusiveNamespaces !== null) {
57+
$prefixes = $inclusiveNamespaces->getPrefixes();
58+
if ($prefixes !== null) {
59+
$prefixList = array_map('strval', $prefixes->toArray());
60+
}
61+
}
62+
break;
63+
default:
64+
throw new CanonicalizationFailedException(
65+
'Message rejected due to unsupported canonicalization transform.',
66+
);
67+
}
68+
}
69+
70+
return $this->canonicalizeData($data, $canonicalMethod, $arXPath, $prefixList);
71+
}
72+
}

src/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;
@@ -109,7 +108,7 @@ protected function doSign(DOMElement $xml): DOMElement
109108
new Transform(AnyURIValue::fromString($this->c14nAlg)),
110109
]);
111110

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

114113
$signedInfo = new SignedInfo(
115114
new CanonicalizationMethod(AnyURIValue::fromString($this->c14nAlg)),

tests/Vulnerabilities/XmlSignatureWrappingTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use SimpleSAML\SAML2\Signature\Validator;
1212
use SimpleSAML\SAML2\XML\saml\Assertion;
1313
use SimpleSAML\XML\DOMDocumentFactory;
14-
use SimpleSAML\XMLSecurity\Exception\ReferenceValidationFailedException;
14+
use SimpleSAML\XMLSecurity\Exception\SignatureVerificationFailedException;
1515
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
1616

1717
/**
@@ -43,8 +43,8 @@ public static function setUpBeforeClass(): void
4343
*/
4444
public function testThatASignatureReferencingAnEmbeddedAssertionIsNotValid(): void
4545
{
46-
$this->expectException(ReferenceValidationFailedException::class);
47-
$this->expectExceptionMessage('Reference does not point to given element.');
46+
$this->expectException(SignatureVerificationFailedException::class);
47+
$this->expectExceptionMessage('Failed to verify signature.');
4848

4949
$assertion = $this->getSignedAssertionWithEmbeddedAssertionReferencedInSignature();
5050
self::$signatureValidator->hasValidSignature($assertion, self::$identityProviderConfiguration);
@@ -55,8 +55,8 @@ public function testThatASignatureReferencingAnEmbeddedAssertionIsNotValid(): vo
5555
*/
5656
public function testThatASignatureReferencingAnotherAssertionIsNotValid(): void
5757
{
58-
$this->expectException(ReferenceValidationFailedException::class);
59-
$this->expectExceptionMessage('Reference does not point to given element.');
58+
$this->expectException(SignatureVerificationFailedException::class);
59+
$this->expectExceptionMessage('Failed to verify signature.');
6060

6161
$assertion = $this->getSignedAssertionWithSignatureThatReferencesAnotherAssertion();
6262
self::$signatureValidator->hasValidSignature($assertion, self::$identityProviderConfiguration);

0 commit comments

Comments
 (0)