Skip to content

Commit

Permalink
Add xenc11:PBKDF2-params element
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 7, 2024
1 parent f098143 commit d32f66c
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/XML/xenc11/AbstractPBKDF2ParameterType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?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\TooManyElementsException;

use function array_pop;

/**
* Class representing <xenc11:PBKDF2ParameterType>.
*
* @package simplesamlphp/xml-security
*/
abstract class AbstractPBKDF2ParameterType extends AbstractXenc11Element
{
/**
* PBKDF2ParameterType constructor.
*
* @param \SimpleSAML\XMLSecurity\XML\xenc11\Salt $salt
* @param \SimpleSAML\XMLSecurity\XML\xenc11\IterationCount $iterationCount
* @param \SimpleSAML\XMLSecurity\XML\xenc11\KeyLength $keyLength
* @param \SimpleSAML\XMLSecurity\XML\xenc11\PRF $prf
*/
final public function __construct(
protected Salt $salt,
protected IterationCount $iterationCount,
protected KeyLength $keyLength,
protected PRF $prf,
) {
}


/**
* Get the value of the $salt property.
*
* @return \SimpleSAML\XMLSecurity\XML\xenc11\Salt
*/
public function getSalt(): Salt
{
return $this->salt;
}


/**
* Get the value of the $iterationCount property.
*
* @return \SimpleSAML\XMLSecurity\XML\xenc11\IterationCount
*/
public function getIterationCount(): IterationCount
{
return $this->iterationCount;
}


/**
* Get the value of the $keyLength property.
*
* @return \SimpleSAML\XMLSecurity\XML\xenc11\KeyLength
*/
public function getKeyLength(): KeyLength
{
return $this->keyLength;
}


/**
* Get the value of the $prf property.
*
* @return \SimpleSAML\XMLSecurity\XML\xenc11\PRF
*/
public function getPRF(): PRF
{
return $this->prf;
}


/**
* @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);

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

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

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

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

return new static(
array_pop($salt),
array_pop($iterationCount),
array_pop($keyLength),
array_pop($prf),
);
}


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

$this->getSalt()->toXML($e);
$this->getIterationCount()->toXML($e);
$this->getKeyLength()->toXML($e);
$this->getPRF()->toXML($e);

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

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

/**
* A class implementing the xenc11:PBKDF2-params element.
*
* @package simplesamlphp/xml-security
*/
final class PBKDF2params extends AbstractPBKDF2ParameterType
{
/** @var string */
public const LOCALNAME = 'PBKDF2-params';
}
84 changes: 84 additions & 0 deletions tests/XML/xenc11/PBKDF2paramsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\SAML2\XML\xenc11;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractPBKDF2ParameterType;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
use SimpleSAML\XMLSecurity\XML\xenc11\IterationCount;
use SimpleSAML\XMLSecurity\XML\xenc11\KeyLength;
use SimpleSAML\XMLSecurity\XML\xenc11\OtherSource;
use SimpleSAML\XMLSecurity\XML\xenc11\Parameters;
use SimpleSAML\XMLSecurity\XML\xenc11\PBKDF2params;
use SimpleSAML\XMLSecurity\XML\xenc11\PRF;
use SimpleSAML\XMLSecurity\XML\xenc11\Salt;

use function dirname;
use function strval;

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

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

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

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


// marshalling


/**
*/
public function testMarshalling(): void
{
$someDoc = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
);

$parameters = new Parameters(
[new Chunk($someDoc->documentElement)],
[new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1')],
);

$otherSource = new OtherSource('urn:x-simplesamlphp:algorithm', $parameters);

$salt = new Salt($otherSource);
$iterationCount = new IterationCount(3);
$keyLength = new KeyLength(4096);
$prf = new PRF('urn:x-simplesamlphp:algorithm');

$PBKDF2params = new PBKDF2params($salt, $iterationCount, $keyLength, $prf);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($PBKDF2params),
);
}
}
12 changes: 12 additions & 0 deletions tests/resources/xml/xenc11_PBKDF2-params.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<xenc11:PBKDF2-params xmlns:xenc11="http://www.w3.org/2009/xmlenc11#">
<xenc11:Salt>
<xenc11:OtherSource Algorithm="urn:x-simplesamlphp:algorithm">
<xenc11:Parameters xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<ssp:Chunk>Some</ssp:Chunk>
</xenc11:Parameters>
</xenc11:OtherSource>
</xenc11:Salt>
<xenc11:IterationCount>3</xenc11:IterationCount>
<xenc11:KeyLength>4096</xenc11:KeyLength>
<xenc11:PRF Algorithm="urn:x-simplesamlphp:algorithm" />
</xenc11:PBKDF2-params>

0 comments on commit d32f66c

Please sign in to comment.