Skip to content

Commit 9fe7dcd

Browse files
committed
Refactor ds:SignatureMethod
1 parent d5c568e commit 9fe7dcd

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

src/XML/ds/SignatureMethod.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,41 @@
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\InvalidDOMElementException;
1010
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
use SimpleSAML\XML\ExtendableElementTrait;
13+
use SimpleSAML\XML\SerializableElementInterface;
14+
use SimpleSAML\XML\XsNamespace as NS;
1115
use SimpleSAML\XMLSecurity\Constants as C;
1216
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
1317

18+
use function array_keys;
19+
use function array_merge;
20+
use function array_pop;
21+
1422
/**
1523
* Class representing a ds:SignatureMethod element.
1624
*
1725
* @package simplesamlphp/xml-security
1826
*/
1927
final class SignatureMethod extends AbstractDsElement
2028
{
29+
use ExtendableElementTrait;
30+
31+
/** The namespace-attribute for the xs:any element */
32+
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
33+
34+
2135
/**
2236
* Initialize a SignatureMethod element.
2337
*
2438
* @param string $Algorithm
39+
* @param \SimpleSAML\XMLSecurity\ds\HMACOutputLength|null $hmacOutputLength
40+
* @param array<\SimpleSAML\XML\SerializableElementInterface> $children
2541
*/
2642
public function __construct(
2743
protected string $Algorithm,
44+
protected ?HMACOutputLength $hmacOutputLength = null,
45+
array $children = [],
2846
) {
2947
Assert::validURI($Algorithm, SchemaViolationException::class);
3048
Assert::oneOf(
@@ -36,6 +54,8 @@ public function __construct(
3654
'Invalid signature method: %s',
3755
InvalidArgumentException::class,
3856
);
57+
58+
$this->setElements($children);
3959
}
4060

4161

@@ -50,6 +70,17 @@ public function getAlgorithm(): string
5070
}
5171

5272

73+
/**
74+
* Collect the value of the hmacOutputLength-property
75+
*
76+
* @return \SimpleSAML\XMLSecurity\ds\HMACOutputLength|null
77+
*/
78+
public function getHMACOutputLength(): ?HMACOutputLength
79+
{
80+
return $this->hmacOutputLength;
81+
}
82+
83+
5384
/**
5485
* Convert XML into a SignatureMethod
5586
*
@@ -66,7 +97,10 @@ public static function fromXML(DOMElement $xml): static
6697

6798
$Algorithm = SignatureMethod::getAttribute($xml, 'Algorithm');
6899

69-
return new static($Algorithm);
100+
$hmacOutputLength = HMACOutputLength::getChildrenOfClass($xml);
101+
Assert::maxCount($hmacOutputLength, 1, TooManyElementsException::class);
102+
103+
return new static($Algorithm, array_pop($hmacOutputLength), self::getChildElementsFromXML($xml));
70104
}
71105

72106

@@ -81,6 +115,12 @@ public function toXML(DOMElement $parent = null): DOMElement
81115
$e = $this->instantiateParentElement($parent);
82116
$e->setAttribute('Algorithm', $this->getAlgorithm());
83117

118+
$this->getHMACOutputLength()?->toXML($e);
119+
120+
foreach ($this->getElements() as $elt) {
121+
$elt->toXML($e);
122+
}
123+
84124
return $e;
85125
}
86126
}

tests/XML/ds/SignatureMethodTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\Chunk;
910
use SimpleSAML\XML\DOMDocumentFactory;
1011
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
1112
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
1213
use SimpleSAML\XMLSecurity\Constants as C;
1314
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
15+
use SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength;
1416
use SimpleSAML\XMLSecurity\XML\ds\SignatureMethod;
1517

1618
use function dirname;
@@ -34,7 +36,7 @@ public static function setUpBeforeClass(): void
3436
{
3537
self::$testedClass = SignatureMethod::class;
3638

37-
self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd';
39+
self::$schemaFile = dirname(__FILE__, 3) . '/resources/schemas/simplesamlphp.xsd';
3840

3941
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
4042
dirname(__FILE__, 3) . '/resources/xml/ds_SignatureMethod.xml',
@@ -46,7 +48,13 @@ public static function setUpBeforeClass(): void
4648
*/
4749
public function testMarshalling(): void
4850
{
49-
$signatureMethod = new SignatureMethod(C::SIG_RSA_SHA256);
51+
$hmacOutputLength = new HMACOutputLength('1234');
52+
53+
$chunk = new Chunk(DOMDocumentFactory::fromString(
54+
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
55+
)->documentElement);
56+
57+
$signatureMethod = new SignatureMethod(C::SIG_RSA_SHA256, $hmacOutputLength, [$chunk]);
5058

5159
$this->assertEquals(
5260
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE schema
3+
PUBLIC "-//W3C//DTD XMLSchema 200102//EN" "http://www.w3.org/2001/XMLSchema.dtd"
4+
[
5+
<!ATTLIST schema
6+
xmlns:ds CDATA #FIXED "urn:x-simplesamlphp:namespace">
7+
<!ENTITY ssp 'urn:x-simplesamlphp:namespace'>
8+
<!ENTITY % p ''>
9+
<!ENTITY % s ''>
10+
]>
11+
12+
<!-- Schema for SimpleSAMLphp dummy classes -->
13+
14+
15+
<schema xmlns="http://www.w3.org/2001/XMLSchema"
16+
xmlns:ssp="urn:x-simplesamlphp:namespace"
17+
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
18+
targetNamespace="urn:x-simplesamlphp:namespace"
19+
version="0.1" elementFormDefault="qualified">
20+
21+
<import namespace='http://www.w3.org/2000/09/xmldsig#'
22+
schemaLocation='../../../resources/schemas/xmldsig1-schema.xsd'/>
23+
24+
<!-- Start Chunk -->
25+
26+
<element name="Chunk" type="string"/>
27+
28+
<!-- End Chunk -->
29+
30+
</schema>
31+
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
<ds:SignatureMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
1+
<ds:SignatureMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256">
2+
<ds:HMACOutputLength>1234</ds:HMACOutputLength>
3+
<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>
4+
</ds:SignatureMethod>

0 commit comments

Comments
 (0)