Skip to content

Commit

Permalink
Implement missing xenc elements (#57)
Browse files Browse the repository at this point in the history
* Add element xenc:KA-nonce

* Refactor ds:KeyInfo

* Add element xenc:OriginatorKeyInfo

* Add element xenc:RecipientKeyInfo

* Add element xenc:AgreementMethod

* Add element xenc:P

* Add element xenc:Q

* Add element xenc:Generator

* Add element xenc:Public

* Add element xenc:seed

* Add element xenc:pgenGenerator

* Add element xenc:DHKeyValue
  • Loading branch information
tvdijen authored Nov 28, 2024
1 parent 6e13445 commit 0156f2b
Show file tree
Hide file tree
Showing 40 changed files with 1,947 additions and 99 deletions.
3 changes: 3 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,7 @@ class Constants extends \SimpleSAML\XML\Constants
public const XMLENC_ELEMENT = 'http://www.w3.org/2001/04/xmlenc#Element';
public const XMLENC_ENCRYPTEDKEY = 'http://www.w3.org/2001/04/xmlenc#EncryptedKey';
public const XMLENC_EXI = 'http://www.w3.org/2009/xmlenc11#EXI';

// The namespace for the Elliptic Curve Diffie-Hellman Ephemeral Static (ECDH-ES) algorithm
public const XMLENC11_ECDH_ES = 'http://www.w3.org/2009/xmlenc11#ECDH-ES';
}
117 changes: 117 additions & 0 deletions src/XML/ds/AbstractKeyInfoType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\ds;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SerializableElementInterface;
use SimpleSAML\XML\XsNamespace as NS;
use SimpleSAML\XMLSecurity\Constants as C;
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;

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

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


/**
* Initialize a KeyInfo element.
*
* @param (
* \SimpleSAML\XMLSecurity\XML\ds\KeyName|
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
* \SimpleSAML\XMLSecurity\XML\ds\X509Data|
* \SimpleSAML\XML\SerializableElementInterface
* )[] $info
* @param string|null $Id
*/
final public function __construct(
protected array $info,
protected ?string $Id = null,
) {
Assert::notEmpty(
$info,
sprintf(
'%s:%s cannot be empty',
static::getNamespacePrefix(),
static::getLocalName(),
),
InvalidArgumentException::class,
);
Assert::maxCount($info, C::UNBOUNDED_LIMIT);
Assert::allIsInstanceOf(
$info,
SerializableElementInterface::class,
InvalidArgumentException::class,
);
Assert::nullOrValidNCName($Id);

foreach ($info as $item) {
if ($item instanceof AbstractDsElement) {
Assert::isInstanceOfAny(
$item,
[KeyName::class, KeyValue::class, RetrievalMethod::class, X509Data::class],
SchemaViolationException::class,
);
}
}
}


/**
* Collect the value of the Id-property
*
* @return string|null
*/
public function getId(): ?string
{
return $this->Id;
}


/**
* Collect the value of the info-property
*
* @return list<\SimpleSAML\XML\SerializableElementInterface>
*/
public function getInfo(): array
{
return $this->info;
}


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

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

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

return $e;
}
}
98 changes: 3 additions & 95 deletions src/XML/ds/KeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,16 @@
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SerializableElementInterface;
use SimpleSAML\XML\XsNamespace as NS;
use SimpleSAML\XMLSecurity\Constants as C;
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;

use function array_merge;

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

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


/**
* Initialize a KeyInfo element.
*
* @param (
* \SimpleSAML\XMLSecurity\XML\ds\KeyName|
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
* \SimpleSAML\XMLSecurity\XML\ds\X509Data|
* \SimpleSAML\XML\SerializableElementInterface
* )[] $info
* @param string|null $Id
*/
public function __construct(
protected array $info,
protected ?string $Id = null,
) {
Assert::notEmpty($info, 'ds:KeyInfo cannot be empty', InvalidArgumentException::class);
Assert::maxCount($info, C::UNBOUNDED_LIMIT);
Assert::allIsInstanceOf(
$info,
SerializableElementInterface::class,
InvalidArgumentException::class,
);
Assert::nullOrValidNCName($Id);

foreach ($info as $item) {
if ($item instanceof AbstractDsElement) {
Assert::isInstanceOfAny(
$item,
[KeyName::class, KeyValue::class, RetrievalMethod::class, X509Data::class],
SchemaViolationException::class,
);
}
}
}


/**
* Collect the value of the Id-property
*
* @return string|null
*/
public function getId(): ?string
{
return $this->Id;
}


/**
* Collect the value of the info-property
*
* @return list<\SimpleSAML\XML\SerializableElementInterface>
*/
public function getInfo(): array
{
return $this->info;
}


/**
* Convert XML into a KeyInfo
*
Expand Down Expand Up @@ -125,26 +55,4 @@ public static function fromXML(DOMElement $xml): static

return new static($info, $Id);
}


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

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

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

return $e;
}
}
4 changes: 2 additions & 2 deletions src/XML/element.registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
'InclusiveNamespaces' => '\SimpleSAML\XMLSecurity\XML\ec\InclusiveNamespaces',
],
'http://www.w3.org/2001/04/xmlenc#' => [
// 'AgreementMethod' => '\SimpleSAML\XMLSecurity\XML\xenc\AgreementMethod',
'AgreementMethod' => '\SimpleSAML\XMLSecurity\XML\xenc\AgreementMethod',
'CipherData' => '\SimpleSAML\XMLSecurity\XML\xenc\CipherData',
'CipherReference' => '\SimpleSAML\XMLSecurity\XML\xenc\CipherReference',
// 'DHKeyValue' => '\SimpleSAML\XMLSecurity\XML\xenc\DHKeyValue',
'DHKeyValue' => '\SimpleSAML\XMLSecurity\XML\xenc\DHKeyValue',
'EncryptedData' => '\SimpleSAML\XMLSecurity\XML\xenc\EncryptedData',
'EncryptedKey' => '\SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey',
'EncryptionProperties' => '\SimpleSAML\XMLSecurity\XML\xenc\EncryptionProperties',
Expand Down
Loading

0 comments on commit 0156f2b

Please sign in to comment.