Skip to content

Commit 869d42f

Browse files
committed
Add xenc11:KeyDerivationMethod element
1 parent 9c6dde5 commit 869d42f

File tree

5 files changed

+176
-1
lines changed

5 files changed

+176
-1
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\ExtendableElementTrait;
13+
use SimpleSAML\XML\XsNamespace as NS;
14+
15+
/**
16+
* Class representing <xenc11:KeyDerivationMethodType>.
17+
*
18+
* @package simplesamlphp/xml-security
19+
*/
20+
abstract class AbstractKeyDerivationMethodType extends AbstractXenc11Element
21+
{
22+
use ExtendableElementTrait;
23+
24+
/** The namespace-attribute for the xs:any element */
25+
public const XS_ANY_ELT_NAMESPACE = NS::ANY;
26+
27+
28+
/**
29+
* KeyDerivationMethod constructor.
30+
*
31+
* @param string $Algorithm
32+
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
33+
*/
34+
final public function __construct(
35+
protected string $Algorithm,
36+
array $children,
37+
) {
38+
Assert::validURI($Algorithm, SchemaViolationException::class);
39+
40+
$this->setElements($children);
41+
}
42+
43+
44+
/**
45+
* Get the value of the $Algorithm property.
46+
*
47+
* @return string
48+
*/
49+
public function getAlgorithm(): string
50+
{
51+
return $this->Algorithm;
52+
}
53+
54+
55+
/**
56+
* @inheritDoc
57+
*
58+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
59+
* If the qualified name of the supplied element is wrong
60+
*/
61+
public static function fromXML(DOMElement $xml): static
62+
{
63+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
64+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
65+
66+
return new static(
67+
self::getOptionalAttribute($xml, 'Algorithm'),
68+
self::getChildElementsFromXML($xml),
69+
);
70+
}
71+
72+
73+
/**
74+
* @inheritDoc
75+
*/
76+
public function toXML(?DOMElement $parent = null): DOMElement
77+
{
78+
$e = $this->instantiateParentElement($parent);
79+
$e->setAttribute('Algorithm', $this->getAlgorithm());
80+
81+
foreach ($this->getElements() as $child) {
82+
if (!$child->isEmptyElement()) {
83+
$child->toXML($e);
84+
}
85+
}
86+
87+
return $e;
88+
}
89+
}

src/XML/xenc11/AbstractXenc11Element.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @package simplesamlphp/xml-security
1414
*/
15-
abstract class AbstractXencElement extends AbstractElement
15+
abstract class AbstractXenc11Element extends AbstractElement
1616
{
1717
/** @var string */
1818
public const NS = C::NS_XENC11;
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:KeyDerivationMethod element.
9+
*
10+
* @package simplesamlphp/xml-security
11+
*/
12+
final class KeyDerivationMethod extends AbstractKeyDerivationMethodType
13+
{
14+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\DOMDocumentFactory;
10+
use SimpleSAML\XML\Exception\MissingAttributeException;
11+
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
12+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
13+
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
14+
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractKeyDerivationMethodType;
15+
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
16+
use SimpleSAML\XMLSecurity\XML\xenc11\KeyDerivationMethod;
17+
18+
use function dirname;
19+
use function strval;
20+
21+
/**
22+
* Tests for the xenc:KeyDerivationMethod element.
23+
*
24+
* @covers \SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element
25+
* @covers \SimpleSAML\XMLSecurity\XML\xenc11\AbstractKeyDerivationMethodType
26+
* @covers \SimpleSAML\XMLSecurity\XML\xenc11\KeyDerivationMethod
27+
* @package simplesamlphp/xml-security
28+
*/
29+
#[CoversClass(AbstractXenc11Element::class)]
30+
#[CoversClass(AbstractKeyDerivationMethodType::class)]
31+
#[CoversClass(KeyDerivationMethod::class)]
32+
final class KeyDerivationMethodTest extends TestCase
33+
{
34+
use SchemaValidationTestTrait;
35+
use SerializableElementTestTrait;
36+
37+
/**
38+
*/
39+
public static function setUpBeforeClass(): void
40+
{
41+
self::$testedClass = KeyDerivationMethod::class;
42+
43+
self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xenc-schema-11.xsd';
44+
45+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
46+
dirname(__FILE__, 3) . '/resources/xml/xenc11_KeyDerivationMethod.xml',
47+
);
48+
}
49+
50+
51+
// test marshalling
52+
53+
54+
/**
55+
* Test creating an KeyDerivationMethod object from scratch.
56+
*/
57+
public function testMarshalling(): void
58+
{
59+
$alg = 'http://www.w3.org/2009/xmlenc11#ConcatKDF';
60+
$keyName = new KeyName('testkey');
61+
62+
$kdm = new KeyDerivationMethod($alg, [$keyName]);
63+
64+
$this->assertEquals(
65+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
66+
strval($kdm),
67+
);
68+
}
69+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<xenc11:KeyDerivationMethod xmlns:xenc11="http://www.w3.org/2009/xmlenc11#" Algorithm="http://www.w3.org/2009/xmlenc11#ConcatKDF">
2+
<ds:KeyName xmlns:ds="http://www.w3.org/2000/09/xmldsig#">testkey</ds:KeyName>
3+
</xenc11:KeyDerivationMethod>

0 commit comments

Comments
 (0)