Skip to content

Commit 041f61e

Browse files
committed
Add xenc11:ConcatKDFParams element
1 parent 7c7c607 commit 041f61e

File tree

5 files changed

+270
-2
lines changed

5 files changed

+270
-2
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
"ext-pcre": "*",
4444
"ext-spl": "*",
4545

46-
"simplesamlphp/assert": "^1.5",
47-
"simplesamlphp/xml-common": "^1.20.0"
46+
"simplesamlphp/assert": "^1.6",
47+
"simplesamlphp/xml-common": "^1.21.0"
4848
},
4949
"require-dev": {
5050
"simplesamlphp/simplesamlphp-test-framework": "^1.7"
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\xenc11;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\MissingElementException;
11+
use SimpleSAML\XML\Exception\SchemaViolationException;
12+
use SimpleSAML\XML\Exception\TooManyElementsException;
13+
use SimpleSAML\XMLSecurity\XML\ds\DigestMethod;
14+
15+
use function array_pop;
16+
17+
/**
18+
* Class representing <xenc11:ConcatKDFParamsType>.
19+
*
20+
* @package simplesamlphp/xml-security
21+
*/
22+
abstract class AbstractConcatKDFParamsType extends AbstractXenc11Element
23+
{
24+
/**
25+
* ConcatKDFParams constructor.
26+
*
27+
* @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod
28+
* @param string|null $AlgorithmID
29+
* @param string|null $PartyUInfo
30+
* @param string|null $PartyVInfo
31+
* @param string|null $SuppPubInfo
32+
* @param string|null $SuppPrivInfo
33+
*/
34+
final public function __construct(
35+
protected DigestMethod $digestMethod,
36+
protected ?string $AlgorithmID = null,
37+
protected ?string $PartyUInfo = null,
38+
protected ?string $PartyVInfo = null,
39+
protected ?string $SuppPubInfo = null,
40+
protected ?string $SuppPrivInfo = null,
41+
) {
42+
Assert::validHexBinary($AlgorithmID, SchemaViolationException::class);
43+
Assert::validHexBinary($PartyUInfo, SchemaViolationException::class);
44+
Assert::validHexBinary($PartyVInfo, SchemaViolationException::class);
45+
Assert::validHexBinary($SuppPubInfo, SchemaViolationException::class);
46+
Assert::validHexBinary($SuppPrivInfo, SchemaViolationException::class);
47+
}
48+
49+
50+
/**
51+
* Get the value of the $digestMethod property.
52+
*
53+
* @return \SimpleSAML\XMLSecurity\XML\ds\DigestMethod
54+
*/
55+
public function getDigestMethod(): DigestMethod
56+
{
57+
return $this->digestMethod;
58+
}
59+
60+
61+
/**
62+
* Get the value of the $AlgorithmID property.
63+
*
64+
* @return string|null
65+
*/
66+
public function getAlgorithmID(): ?string
67+
{
68+
return $this->AlgorithmID;
69+
}
70+
71+
72+
/**
73+
* Get the value of the $PartyUInfo property.
74+
*
75+
* @return string|null
76+
*/
77+
public function getPartyUInfo(): ?string
78+
{
79+
return $this->PartyUInfo;
80+
}
81+
82+
83+
/**
84+
* Get the value of the $PartyVInfo property.
85+
*
86+
* @return string|null
87+
*/
88+
public function getPartyVInfo(): ?string
89+
{
90+
return $this->PartyVInfo;
91+
}
92+
93+
94+
/**
95+
* Get the value of the $SuppPubInfo property.
96+
*
97+
* @return string|null
98+
*/
99+
public function getSuppPubInfo(): ?string
100+
{
101+
return $this->SuppPubInfo;
102+
}
103+
104+
105+
/**
106+
* Get the value of the $SuppPrivInfo property.
107+
*
108+
* @return string|null
109+
*/
110+
public function getSuppPrivInfo(): ?string
111+
{
112+
return $this->SuppPrivInfo;
113+
}
114+
115+
116+
/**
117+
* @inheritDoc
118+
*
119+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
120+
* If the qualified name of the supplied element is wrong
121+
*/
122+
public static function fromXML(DOMElement $xml): static
123+
{
124+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
125+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
126+
127+
$digestMethod = DigestMethod::getChildrenOfClass($xml);
128+
Assert::minCount($digestMethod, 1, MissingElementException::class);
129+
Assert::maxCount($digestMethod, 1, TooManyElementsException::class);
130+
131+
return new static(
132+
array_pop($digestMethod),
133+
self::getOptionalAttribute($xml, 'AlgorithmID', null),
134+
self::getOptionalAttribute($xml, 'PartyUInfo', null),
135+
self::getOptionalAttribute($xml, 'PartyVInfo', null),
136+
self::getOptionalAttribute($xml, 'SuppPubInfo', null),
137+
self::getOptionalAttribute($xml, 'SuppPrivInfo', null),
138+
);
139+
}
140+
141+
142+
/**
143+
* @inheritDoc
144+
*/
145+
public function toXML(?DOMElement $parent = null): DOMElement
146+
{
147+
$e = $this->instantiateParentElement($parent);
148+
149+
if ($this->getAlgorithmID() !== null) {
150+
$e->setAttribute('AlgorithmID', $this->getAlgorithmID());
151+
}
152+
153+
if ($this->getPartyUInfo() !== null) {
154+
$e->setAttribute('PartyUInfo', $this->getPartyUInfo());
155+
}
156+
157+
if ($this->getPartyVInfo() !== null) {
158+
$e->setAttribute('PartyVInfo', $this->getPartyVInfo());
159+
}
160+
161+
if ($this->getSuppPubInfo() !== null) {
162+
$e->setAttribute('SuppPubInfo', $this->getSuppPubInfo());
163+
}
164+
165+
if ($this->getSuppPrivInfo() !== null) {
166+
$e->setAttribute('SuppPrivInfo', $this->getSuppPrivInfo());
167+
}
168+
169+
$this->getDigestMethod()->toXML($e);
170+
171+
return $e;
172+
}
173+
}

src/XML/xenc11/ConcatKDFParams.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\xenc11;
6+
7+
/**
8+
* A class implementing the xenc11:ConcatKDFParams element.
9+
*
10+
* @package simplesamlphp/xml-security
11+
*/
12+
final class ConcatKDFParams extends AbstractConcatKDFParamsType
13+
{
14+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML\xenc11;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\Chunk;
10+
use SimpleSAML\XML\DOMDocumentFactory;
11+
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
12+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
13+
use SimpleSAML\XMLSecurity\Constants as C;
14+
use SimpleSAML\XMLSecurity\XML\ds\DigestMethod;
15+
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractConcatKDFParamsType;
16+
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
17+
use SimpleSAML\XMLSecurity\XML\xenc11\ConcatKDFParams;
18+
19+
use function dirname;
20+
use function strval;
21+
22+
/**
23+
* Class \SimpleSAML\XMLSecurity\XML\Test\xenc11\ConcatKDFParamsTest
24+
*
25+
* @package simplesamlphp/xml-security
26+
*/
27+
#[CoversClass(AbstractXenc11Element::class)]
28+
#[CoversClass(AbstractConcatKDFParamsType::class)]
29+
#[CoversClass(ConcatKDFParams::class)]
30+
final class ConcatKDFParamsTest extends TestCase
31+
{
32+
use SchemaValidationTestTrait;
33+
use SerializableElementTestTrait;
34+
35+
/**
36+
*/
37+
public static function setUpBeforeClass(): void
38+
{
39+
self::$testedClass = ConcatKDFParams::class;
40+
41+
self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xenc-schema-11.xsd';
42+
43+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
44+
dirname(__FILE__, 3) . '/resources/xml/xenc11_ConcatKDFParams.xml',
45+
);
46+
}
47+
48+
49+
/**
50+
*/
51+
public function testMarshalling(): void
52+
{
53+
$digestMethod = new DigestMethod(
54+
C::DIGEST_SHA256,
55+
[
56+
new Chunk(DOMDocumentFactory::fromString(
57+
'<some:Chunk xmlns:some="urn:test:some">Random</some:Chunk>',
58+
)->documentElement),
59+
],
60+
);
61+
62+
$concatKdfParams = new ConcatKDFParams(
63+
$digestMethod,
64+
'a1b2',
65+
'b2c3',
66+
'c3d4',
67+
'd4e5',
68+
'e5f6',
69+
);
70+
71+
$this->assertEquals(
72+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
73+
strval($concatKdfParams),
74+
);
75+
}
76+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<xenc11:ConcatKDFParams xmlns:xenc11="http://www.w3.org/2009/xmlenc11#" AlgorithmID="a1b2" PartyUInfo="b2c3" PartyVInfo="c3d4" SuppPubInfo="d4e5" SuppPrivInfo="e5f6">
2+
<ds:DigestMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/04/xmlenc#sha256">
3+
<some:Chunk xmlns:some="urn:test:some">Random</some:Chunk>
4+
</ds:DigestMethod>
5+
</xenc11:ConcatKDFParams>

0 commit comments

Comments
 (0)