Skip to content

Commit

Permalink
Add xenc11:ConcatKDFParams element
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 5, 2024
1 parent 7c7c607 commit 041f61e
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 2 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"ext-pcre": "*",
"ext-spl": "*",

"simplesamlphp/assert": "^1.5",
"simplesamlphp/xml-common": "^1.20.0"
"simplesamlphp/assert": "^1.6",
"simplesamlphp/xml-common": "^1.21.0"
},
"require-dev": {
"simplesamlphp/simplesamlphp-test-framework": "^1.7"
Expand Down
173 changes: 173 additions & 0 deletions src/XML/xenc11/AbstractConcatKDFParamsType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XMLSecurity\XML\ds\DigestMethod;

use function array_pop;

/**
* Class representing <xenc11:ConcatKDFParamsType>.
*
* @package simplesamlphp/xml-security
*/
abstract class AbstractConcatKDFParamsType extends AbstractXenc11Element
{
/**
* ConcatKDFParams constructor.
*
* @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod
* @param string|null $AlgorithmID
* @param string|null $PartyUInfo
* @param string|null $PartyVInfo
* @param string|null $SuppPubInfo
* @param string|null $SuppPrivInfo
*/
final public function __construct(
protected DigestMethod $digestMethod,
protected ?string $AlgorithmID = null,
protected ?string $PartyUInfo = null,
protected ?string $PartyVInfo = null,
protected ?string $SuppPubInfo = null,
protected ?string $SuppPrivInfo = null,
) {
Assert::validHexBinary($AlgorithmID, SchemaViolationException::class);
Assert::validHexBinary($PartyUInfo, SchemaViolationException::class);
Assert::validHexBinary($PartyVInfo, SchemaViolationException::class);
Assert::validHexBinary($SuppPubInfo, SchemaViolationException::class);
Assert::validHexBinary($SuppPrivInfo, SchemaViolationException::class);
}


/**
* Get the value of the $digestMethod property.
*
* @return \SimpleSAML\XMLSecurity\XML\ds\DigestMethod
*/
public function getDigestMethod(): DigestMethod
{
return $this->digestMethod;
}


/**
* Get the value of the $AlgorithmID property.
*
* @return string|null
*/
public function getAlgorithmID(): ?string
{
return $this->AlgorithmID;
}


/**
* Get the value of the $PartyUInfo property.
*
* @return string|null
*/
public function getPartyUInfo(): ?string
{
return $this->PartyUInfo;
}


/**
* Get the value of the $PartyVInfo property.
*
* @return string|null
*/
public function getPartyVInfo(): ?string
{
return $this->PartyVInfo;
}


/**
* Get the value of the $SuppPubInfo property.
*
* @return string|null
*/
public function getSuppPubInfo(): ?string
{
return $this->SuppPubInfo;
}


/**
* Get the value of the $SuppPrivInfo property.
*
* @return string|null
*/
public function getSuppPrivInfo(): ?string
{
return $this->SuppPrivInfo;
}


/**
* @inheritDoc
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

$digestMethod = DigestMethod::getChildrenOfClass($xml);
Assert::minCount($digestMethod, 1, MissingElementException::class);
Assert::maxCount($digestMethod, 1, TooManyElementsException::class);

return new static(
array_pop($digestMethod),
self::getOptionalAttribute($xml, 'AlgorithmID', null),
self::getOptionalAttribute($xml, 'PartyUInfo', null),
self::getOptionalAttribute($xml, 'PartyVInfo', null),
self::getOptionalAttribute($xml, 'SuppPubInfo', null),
self::getOptionalAttribute($xml, 'SuppPrivInfo', null),
);
}


/**
* @inheritDoc
*/
public function toXML(?DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);

if ($this->getAlgorithmID() !== null) {
$e->setAttribute('AlgorithmID', $this->getAlgorithmID());
}

if ($this->getPartyUInfo() !== null) {
$e->setAttribute('PartyUInfo', $this->getPartyUInfo());
}

if ($this->getPartyVInfo() !== null) {
$e->setAttribute('PartyVInfo', $this->getPartyVInfo());
}

if ($this->getSuppPubInfo() !== null) {
$e->setAttribute('SuppPubInfo', $this->getSuppPubInfo());
}

if ($this->getSuppPrivInfo() !== null) {
$e->setAttribute('SuppPrivInfo', $this->getSuppPrivInfo());
}

$this->getDigestMethod()->toXML($e);

return $e;
}
}
14 changes: 14 additions & 0 deletions src/XML/xenc11/ConcatKDFParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

/**
* A class implementing the xenc11:ConcatKDFParams element.
*
* @package simplesamlphp/xml-security
*/
final class ConcatKDFParams extends AbstractConcatKDFParamsType
{
}
76 changes: 76 additions & 0 deletions tests/XML/xenc11/ConcatKDFParamsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\Test\XML\xenc11;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XMLSecurity\Constants as C;
use SimpleSAML\XMLSecurity\XML\ds\DigestMethod;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractConcatKDFParamsType;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
use SimpleSAML\XMLSecurity\XML\xenc11\ConcatKDFParams;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\XMLSecurity\XML\Test\xenc11\ConcatKDFParamsTest
*
* @package simplesamlphp/xml-security
*/
#[CoversClass(AbstractXenc11Element::class)]
#[CoversClass(AbstractConcatKDFParamsType::class)]
#[CoversClass(ConcatKDFParams::class)]
final class ConcatKDFParamsTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;

/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = ConcatKDFParams::class;

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xenc-schema-11.xsd';

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 3) . '/resources/xml/xenc11_ConcatKDFParams.xml',
);
}


/**
*/
public function testMarshalling(): void
{
$digestMethod = new DigestMethod(
C::DIGEST_SHA256,
[
new Chunk(DOMDocumentFactory::fromString(
'<some:Chunk xmlns:some="urn:test:some">Random</some:Chunk>',
)->documentElement),
],
);

$concatKdfParams = new ConcatKDFParams(
$digestMethod,
'a1b2',
'b2c3',
'c3d4',
'd4e5',
'e5f6',
);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($concatKdfParams),
);
}
}
5 changes: 5 additions & 0 deletions tests/resources/xml/xenc11_ConcatKDFParams.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<xenc11:ConcatKDFParams xmlns:xenc11="http://www.w3.org/2009/xmlenc11#" AlgorithmID="a1b2" PartyUInfo="b2c3" PartyVInfo="c3d4" SuppPubInfo="d4e5" SuppPrivInfo="e5f6">
<ds:DigestMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/04/xmlenc#sha256">
<some:Chunk xmlns:some="urn:test:some">Random</some:Chunk>
</ds:DigestMethod>
</xenc11:ConcatKDFParams>

0 comments on commit 041f61e

Please sign in to comment.