Skip to content

Commit 3fbdc66

Browse files
committed
Add element xenc:AgreementMethod
1 parent 26d0811 commit 3fbdc66

File tree

5 files changed

+418
-0
lines changed

5 files changed

+418
-0
lines changed

src/Constants.php

+3
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,7 @@ class Constants extends \SimpleSAML\XML\Constants
152152
public const XMLENC_ELEMENT = 'http://www.w3.org/2001/04/xmlenc#Element';
153153
public const XMLENC_ENCRYPTEDKEY = 'http://www.w3.org/2001/04/xmlenc#EncryptedKey';
154154
public const XMLENC_EXI = 'http://www.w3.org/2009/xmlenc11#EXI';
155+
156+
// The namespace for the Elliptic Curve Diffie-Hellman Ephemeral Static (ECDH-ES) algorithm
157+
public const XMLENC11_ECDH_ES = 'http://www.w3.org/2009/xmlenc11#ECDH-ES';
155158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\xenc;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
use SimpleSAML\XML\ExtendableElementTrait;
13+
use SimpleSAML\XML\XsNamespace as NS;
14+
15+
use function array_pop;
16+
17+
/**
18+
* A class implementing the xenc:AbstractAgreementMethodType element.
19+
*
20+
* @package simplesamlphp/xml-security
21+
*/
22+
abstract class AbstractAgreementMethodType extends AbstractXencElement
23+
{
24+
use ExtendableElementTrait;
25+
26+
/** The namespace-attribute for the xs:any element */
27+
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
28+
29+
30+
/**
31+
* AgreementMethodType constructor.
32+
*
33+
* @param string $algorithm
34+
* @param \SimpleSAML\XMLSecurity\XML\xenc\KANonce|null $kaNonce
35+
* @param \SimpleSAML\XMLSecurity\XML\xenc\OriginatorKeyInfo|null $originatorKeyInfo
36+
* @param \SimpleSAML\XMLSecurity\XML\xenc\RecipientKeyInfo|null $recipientKeyInfo
37+
* @param list<\SimpleSAML\XML\SerializableElementInterface> $children
38+
*/
39+
final public function __construct(
40+
protected string $algorithm,
41+
protected ?KANonce $kaNonce = null,
42+
protected ?OriginatorKeyInfo $originatorKeyInfo = null,
43+
protected ?RecipientKeyInfo $recipientKeyInfo = null,
44+
protected array $children = [],
45+
) {
46+
Assert::validURI($algorithm, SchemaViolationException::class); // Covers the empty string
47+
48+
$this->setElements($children);
49+
}
50+
51+
52+
/**
53+
* Get the URI identifying the algorithm used by this agreement method.
54+
*
55+
* @return string
56+
*/
57+
public function getAlgorithm(): string
58+
{
59+
return $this->algorithm;
60+
}
61+
62+
63+
/**
64+
* Get the KA-Nonce.
65+
*
66+
* @return \SimpleSAML\XMLSecurity\XML\xenc\KANonce|null
67+
*/
68+
public function getKANonce(): ?KANonce
69+
{
70+
return $this->kaNonce;
71+
}
72+
73+
74+
/**
75+
* Get the Originator KeyInfo.
76+
*
77+
* @return \SimpleSAML\XMLSecurity\XML\xenc\OriginatorKeyInfo|null
78+
*/
79+
public function getOriginatorKeyInfo(): ?OriginatorKeyInfo
80+
{
81+
return $this->originatorKeyInfo;
82+
}
83+
84+
85+
/**
86+
* Get the Recipient KeyInfo.
87+
*
88+
* @return \SimpleSAML\XMLSecurity\XML\xenc\RecipientKeyInfo|null
89+
*/
90+
public function getRecipientKeyInfo(): ?RecipientKeyInfo
91+
{
92+
return $this->recipientKeyInfo;
93+
}
94+
95+
96+
/**
97+
* Initialize an AgreementMethod object from an existing XML.
98+
*
99+
* @param \DOMElement $xml
100+
* @return static
101+
*
102+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
103+
* if the qualified name of the supplied element is wrong
104+
* @throws \SimpleSAML\XML\Exception\MissingAttributeException
105+
* if the supplied element is missing one of the mandatory attributes
106+
* @throws \SimpleSAML\XML\Exception\TooManyElementsException
107+
* if too many child-elements of a type are specified
108+
*/
109+
public static function fromXML(DOMElement $xml): static
110+
{
111+
Assert::same($xml->localName, 'AgreementMethod', InvalidDOMElementException::class);
112+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
113+
114+
$algorithm = self::getAttribute($xml, 'Algorithm');
115+
116+
$kaNonce = KANonce::getChildrenOfClass($xml);
117+
Assert::maxCount($kaNonce, 1, TooManyElementsException::class);
118+
119+
$originatorKeyInfo = OriginatorKeyInfo::getChildrenOfClass($xml);
120+
Assert::maxCount($originatorKeyInfo, 1, TooManyElementsException::class);
121+
122+
$recipientKeyInfo = RecipientKeyInfo::getChildrenOfClass($xml);
123+
Assert::maxCount($recipientKeyInfo, 1, TooManyElementsException::class);
124+
125+
$children = self::getChildElementsFromXML($xml);
126+
127+
return new static(
128+
$algorithm,
129+
array_pop($kaNonce),
130+
array_pop($originatorKeyInfo),
131+
array_pop($recipientKeyInfo),
132+
$children,
133+
);
134+
}
135+
136+
137+
/**
138+
* Convert this AgreementMethod object to XML.
139+
*
140+
* @param \DOMElement|null $parent The element we should append this AgreementMethod to.
141+
* @return \DOMElement
142+
*/
143+
public function toXML(DOMElement $parent = null): DOMElement
144+
{
145+
$e = $this->instantiateParentElement($parent);
146+
$e->setAttribute('Algorithm', $this->getAlgorithm());
147+
148+
$this->getKANonce()?->toXML($e);
149+
150+
foreach ($this->getElements() as $child) {
151+
$child->toXML($e);
152+
}
153+
154+
$this->getOriginatorKeyInfo()?->toXML($e);
155+
$this->getRecipientKeyInfo()?->toXML($e);
156+
157+
return $e;
158+
}
159+
}

src/XML/xenc/AgreementMethod.php

+14
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\xenc;
6+
7+
/**
8+
* A class implementing the xenc:AgreementMethod element.
9+
*
10+
* @package simplesamlphp/xml-security
11+
*/
12+
final class AgreementMethod extends AbstractAgreementMethodType
13+
{
14+
}

0 commit comments

Comments
 (0)