Skip to content

Commit

Permalink
Feature/ds missing elements (#58)
Browse files Browse the repository at this point in the history
* Refactor X509SerialNumber

* Implement IntegerElementTrait

* Add HMACOutputLength element

* Refactor ds:SignatureMethod

* Add ds:MgmtData element

* Add ds:PGPKeyID element

* Add ds:PGPKeyPacket element

* Add ds:PGPData element

* Add ds:MgmtData element

* Add ds:SPKISexp element

* Add ds:SPKIData element
  • Loading branch information
tvdijen authored Dec 3, 2024
1 parent 2f478b2 commit 56a532b
Show file tree
Hide file tree
Showing 42 changed files with 1,132 additions and 87 deletions.
13 changes: 12 additions & 1 deletion src/XML/ds/AbstractKeyInfoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ abstract class AbstractKeyInfoType extends AbstractDsElement
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
* \SimpleSAML\XMLSecurity\XML\ds\X509Data|
* \SimpleSAML\XMLSecurity\XML\ds\PGPData|
* \SimpleSAML\XMLSecurity\XML\ds\SPKIData|
* \SimpleSAML\XMLSecurity\XML\ds\MgmtData|
* \SimpleSAML\XML\SerializableElementInterface
* )[] $info
* @param string|null $Id
Expand Down Expand Up @@ -64,7 +67,15 @@ final public function __construct(
if ($item instanceof AbstractDsElement) {
Assert::isInstanceOfAny(
$item,
[KeyName::class, KeyValue::class, RetrievalMethod::class, X509Data::class],
[
KeyName::class,
KeyValue::class,
RetrievalMethod::class,
X509Data::class,
PGPData::class,
SPKIData::class,
MgmtData::class,
],
SchemaViolationException::class,
);
}
Expand Down
120 changes: 120 additions & 0 deletions src/XML/ds/AbstractPGPDataType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\ds;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;

use function array_pop;

/**
* Abstract class representing the PGPDataType.
*
* @package simplesamlphp/xml-security
*/
abstract class AbstractPGPDataType extends AbstractDsElement
{
use ExtendableElementTrait;

/** @var \SimpleSAML\XML\XsNamespace */
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;


/**
* Initialize a PGPData element.
*
* @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null $pgpKeyId
* @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null $pgpKeyPacket
* @param array<\SimpleSAML\XML\SerializableElementInterface> $children
*/
final public function __construct(
protected ?PGPKeyID $pgpKeyId = null,
protected ?PGPKeyPacket $pgpKeyPacket = null,
array $children = [],
) {
if ($pgpKeyId === null && $pgpKeyPacket === null) {
throw new SchemaViolationException("ds:PGPKeyID and ds:PGPKeyPacket can't both be null.");
}

$this->setElements($children);
}


/**
* Collect the value of the PGPKeyID-property
*
* @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null
*/
public function getPGPKeyID(): ?PGPKeyID
{
return $this->pgpKeyId;
}


/**
* Collect the value of the PGPKeyPacket-property
*
* @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null
*/
public function getPGPKeyPacket(): ?PGPKeyPacket
{
return $this->pgpKeyPacket;
}


/**
* Convert XML into a PGPData
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @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::NS, InvalidDOMElementException::class);

$pgpKeyId = PGPKeyID::getChildrenOfClass($xml);
Assert::maxCount($pgpKeyId, 1, TooManyElementsException::class);

$pgpKeyPacket = PGPKeyPacket::getChildrenOfClass($xml);
Assert::maxCount($pgpKeyPacket, 1, TooManyElementsException::class);

return new static(
array_pop($pgpKeyId),
array_pop($pgpKeyPacket),
self::getChildElementsFromXML($xml),
);
}


/**
* Convert this PGPData to XML.
*
* @param \DOMElement|null $parent The element we should append this PGPData to.
* @return \DOMElement
*/
public function toXML(?DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);

$this->getPGPKeyId()?->toXML($e);
$this->getPGPKeyPacket()?->toXML($e);

foreach ($this->getElements() as $elt) {
$elt->toXML($e);
}

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

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\ds;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\Registry\ElementRegistry;
use SimpleSAML\XML\SerializableElementInterface;
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;

/**
* Abstract class representing the SPKIDataType.
*
* @package simplesamlphp/xml-security
*/
abstract class AbstractSPKIDataType extends AbstractDsElement
{
/**
* Initialize a SPKIData element.
*
* @param array{array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}} $tuples
*/
final public function __construct(
protected array $tuples,
) {
Assert::allIsArray($tuples, SchemaViolationException::class);
Assert::allCount($tuples, 2);

foreach ($tuples as $tuple) {
Assert::isInstanceOf($tuple[0], SPKISexp::class, SchemaViolationException::class);
Assert::nullOrIsInstanceOf($tuple[1], SerializableElementInterface::class, SchemaViolationException::class);
}
}


/**
* Collect the value of the SPKISexp-property
*
* @return array{array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}}
*/
public function getTuples(): array
{
return $this->tuples;
}


/**
* Convert XML into a SPKIData
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @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::NS, InvalidDOMElementException::class);

$registry = ElementRegistry::getInstance();
$tuples = [];
$tuple = [null, null];
foreach ($xml->childNodes as $node) {
if ($node instanceof DOMElement) {
if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') {
if ($tuple[0] !== null) {
$tuples[] = $tuple;
}
$tuple = [SPKISexp::fromXML($node), null];
} elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) {
$handler = $registry->getElementHandler($node->namespaceURI, $node->localName);
$tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node);
$tuples[] = $tuple;
$tuple = [null, null];
}
}
}

if ($tuple[0] !== null) {
$tuples[] = $tuple;
}

return new static($tuples);
}


/**
* Convert this SPKIData to XML.
*
* @param \DOMElement|null $parent The element we should append this SPKIData to.
* @return \DOMElement
*/
public function toXML(?DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);

foreach ($this->getTuples() as $tuple) {
$tuple[0]->toXML($e);
$tuple[1]?->toXML($e);
}

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

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\ds;

use SimpleSAML\XML\IntegerElementTrait;

/**
* Class representing a ds:HMACOutputLength element.
*
* @package simplesamlphp/xml-security
*/
final class HMACOutputLength extends AbstractDsElement
{
use IntegerElementTrait;


/**
* @param string $length
*/
public function __construct(string $length)
{
$this->setContent($length);
}
}
12 changes: 6 additions & 6 deletions src/XML/ds/KeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public static function fromXML(DOMElement $xml): static
$keyValue = KeyValue::getChildrenOfClass($xml);
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml);
$x509Data = X509Data::getChildrenOfClass($xml);
//$pgpData = PGPData::getChildrenOfClass($xml);
//$spkiData = SPKIData::getChildrenOfClass($xml);
//$mgmtData = MgmtData::getChildrenOfClass($xml);
$pgpData = PGPData::getChildrenOfClass($xml);
$spkiData = SPKIData::getChildrenOfClass($xml);
$mgmtData = MgmtData::getChildrenOfClass($xml);
$other = self::getChildElementsFromXML($xml);

$info = array_merge(
$keyName,
$keyValue,
$retrievalMethod,
$x509Data,
//$pgpdata,
//$spkidata,
//$mgmtdata,
$pgpData,
$spkiData,
$mgmtData,
$other,
);

Expand Down
26 changes: 26 additions & 0 deletions src/XML/ds/MgmtData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\ds;

use SimpleSAML\XML\StringElementTrait;

/**
* Class representing a ds:MgmtData element.
*
* @package simplesamlphp/xml-security
*/
final class MgmtData extends AbstractDsElement
{
use StringElementTrait;


/**
* @param string $content
*/
public function __construct(string $content)
{
$this->setContent($content);
}
}
14 changes: 14 additions & 0 deletions src/XML/ds/PGPData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\ds;

/**
* Class representing a ds:PGPData element.
*
* @package simplesaml/xml-security
*/
final class PGPData extends AbstractPGPDataType
{
}
26 changes: 26 additions & 0 deletions src/XML/ds/PGPKeyID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\ds;

use SimpleSAML\XML\Base64ElementTrait;

/**
* Class representing a ds:PGPKeyID element.
*
* @package simplesaml/xml-security
*/
final class PGPKeyID extends AbstractDsElement
{
use Base64ElementTrait;


/**
* @param string $content
*/
public function __construct(string $content)
{
$this->setContent($content);
}
}
Loading

0 comments on commit 56a532b

Please sign in to comment.