Skip to content

Commit 5f9923f

Browse files
committed
Add dsig11:ECParameters-element
1 parent 4181fdf commit 5f9923f

File tree

4 files changed

+334
-0
lines changed

4 files changed

+334
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
9+
/**
10+
* Abstract class representing a dsig11:ECParametersType
11+
*
12+
* @package simplesaml/xml-security
13+
*/
14+
abstract class AbstractECParametersType extends AbstractDsig11Element
15+
{
16+
/**
17+
* Initialize a ECParametersType element.
18+
*
19+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\FieldID $fieldId
20+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\Curve $curve
21+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\Base $base
22+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\Order $order
23+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\CoFactor|null $coFactor
24+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\ValidationData|null $validationData
25+
*/
26+
public function __construct(
27+
protected FieldID $fieldId,
28+
protected Curve $curve,
29+
protected Base $base,
30+
protected Order $order,
31+
protected ?CoFactor $coFactor = null,
32+
protected ?ValidationData $validationData = null,
33+
) {
34+
}
35+
36+
37+
/**
38+
* Collect the value of the fieldId-property
39+
*
40+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\FieldID
41+
*/
42+
public function getFieldID(): FieldID
43+
{
44+
return $this->fieldId;
45+
}
46+
47+
48+
/**
49+
* Collect the value of the curve-property
50+
*
51+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\Curve
52+
*/
53+
public function getCurve(): Curve
54+
{
55+
return $this->curve;
56+
}
57+
58+
59+
/**
60+
* Collect the value of the base-property
61+
*
62+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\Base
63+
*/
64+
public function getBase(): Base
65+
{
66+
return $this->base;
67+
}
68+
69+
70+
/**
71+
* Collect the value of the order-property
72+
*
73+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\Order
74+
*/
75+
public function getOrder(): Order
76+
{
77+
return $this->order;
78+
}
79+
80+
81+
/**
82+
* Collect the value of the coFactor-property
83+
*
84+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\CoFactor|null
85+
*/
86+
public function getCoFactor(): ?CoFactor
87+
{
88+
return $this->coFactor;
89+
}
90+
91+
92+
/**
93+
* Collect the value of the validationData-property
94+
*
95+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\ValidationData|null
96+
*/
97+
public function getValidationData(): ?ValidationData
98+
{
99+
return $this->validationData;
100+
}
101+
102+
103+
/**
104+
* Convert this ECParametersType element to XML.
105+
*
106+
* @param \DOMElement|null $parent The element we should append this ECParametersType element to.
107+
* @return \DOMElement
108+
*/
109+
public function toXML(?DOMElement $parent = null): DOMElement
110+
{
111+
$e = $this->instantiateParentElement($parent);
112+
113+
$this->getFieldId()->toXML($e);
114+
$this->getCurve()->toXML($e);
115+
$this->getBase()->toXML($e);
116+
$this->getOrder()->toXML($e);
117+
$this->getCoFactor()?->toXML($e);
118+
$this->getValidationData()?->toXML($e);
119+
120+
return $e;
121+
}
122+
}

src/XML/dsig11/ECParameters.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\MissingElementException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
13+
use function array_pop;
14+
15+
/**
16+
* Class representing a dsig11:ECParameters element.
17+
*
18+
* @package simplesaml/xml-security
19+
*/
20+
final class ECParameters extends AbstractParametersType
21+
{
22+
/**
23+
* Convert XML into a ECParameters element
24+
*
25+
* @param \DOMElement $xml The XML element we should load
26+
* @return static
27+
*
28+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
29+
* If the qualified name of the supplied element is wrong
30+
*/
31+
public static function fromXML(DOMElement $xml): static
32+
{
33+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
34+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
35+
36+
$fieldId = FieldID::getChildrenOfClass($xml);
37+
Assert::minCount($fieldId, 1, MissingElementException::class);
38+
Assert::maxCount($fieldId, 1, TooManyElementsException::class);
39+
40+
$curve = Curve::getChildrenOfClass($xml);
41+
Assert::minCount($curve, 1, MissingElementException::class);
42+
Assert::maxCount($curve, 1, TooManyElementsException::class);
43+
44+
$base = Base::getChildrenOfClass($xml);
45+
Assert::minCount($base, 1, MissingElementException::class);
46+
Assert::maxCount($base, 1, TooManyElementsException::class);
47+
48+
$order = Order::getChildrenOfClass($xml);
49+
Assert::minCount($order, 1, MissingElementException::class);
50+
Assert::maxCount($order, 1, TooManyElementsException::class);
51+
52+
$coFactor = CoFactor::getChildrenOfClass($xml);
53+
Assert::maxCount($coFactor, 1, TooManyElementsException::class);
54+
55+
$validationData = ValidationData::getChildrenOfClass($xml);
56+
Assert::maxCount($validationData, 1, TooManyElementsException::class);
57+
58+
return new static(
59+
array_pop($fieldId),
60+
array_pop($curve),
61+
array_pop($base),
62+
array_pop($order),
63+
array_pop($coFactor),
64+
array_pop($validationData),
65+
);
66+
}
67+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML\dsig11;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\Chunk;
10+
use SimpleSAML\XML\DOMDocumentFactory;
11+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12+
use SimpleSAML\XMLSecurity\XML\dsig11\A;
13+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
14+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractECParametersType;
15+
use SimpleSAML\XMLSecurity\XML\dsig11\B;
16+
use SimpleSAML\XMLSecurity\XML\dsig11\Base;
17+
use SimpleSAML\XMLSecurity\XML\dsig11\CoFactor;
18+
use SimpleSAML\XMLSecurity\XML\dsig11\Curve;
19+
use SimpleSAML\XMLSecurity\XML\dsig11\ECParameters;
20+
use SimpleSAML\XMLSecurity\XML\dsig11\FieldID;
21+
use SimpleSAML\XMLSecurity\XML\dsig11\GnB;
22+
use SimpleSAML\XMLSecurity\XML\dsig11\K;
23+
use SimpleSAML\XMLSecurity\XML\dsig11\K1;
24+
use SimpleSAML\XMLSecurity\XML\dsig11\K2;
25+
use SimpleSAML\XMLSecurity\XML\dsig11\K3;
26+
use SimpleSAML\XMLSecurity\XML\dsig11\M;
27+
use SimpleSAML\XMLSecurity\XML\dsig11\Order;
28+
use SimpleSAML\XMLSecurity\XML\dsig11\P;
29+
use SimpleSAML\XMLSecurity\XML\dsig11\PnB;
30+
use SimpleSAML\XMLSecurity\XML\dsig11\Prime;
31+
use SimpleSAML\XMLSecurity\XML\dsig11\Seed;
32+
use SimpleSAML\XMLSecurity\XML\dsig11\TnB;
33+
use SimpleSAML\XMLSecurity\XML\dsig11\ValidationData;
34+
35+
use function dirname;
36+
use function strval;
37+
38+
/**
39+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\ECParametersTest
40+
*
41+
* @package simplesamlphp/xml-security
42+
*/
43+
#[CoversClass(AbstractDsig11Element::class)]
44+
#[CoversClass(AbstractECParametersType::class)]
45+
#[CoversClass(ECParameters::class)]
46+
final class ECParametersTest extends TestCase
47+
{
48+
use SerializableElementTestTrait;
49+
50+
51+
/**
52+
*/
53+
public static function setUpBeforeClass(): void
54+
{
55+
self::$testedClass = ECParameters::class;
56+
57+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
58+
dirname(__FILE__, 3) . '/resources/xml/dsig11_ECParameters.xml',
59+
);
60+
}
61+
62+
63+
/**
64+
*/
65+
public function testMarshalling(): void
66+
{
67+
// Build FieldID
68+
$p = new P('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
69+
$prime = new Prime($p);
70+
71+
$m = new M(1024);
72+
$k = new K(64);
73+
$tnb = new TnB($m, $k);
74+
75+
$k1 = new K1(128);
76+
$k2 = new K2(256);
77+
$k3 = new K3(512);
78+
$pnb = new PnB($m, $k1, $k2, $k3);
79+
80+
$gnb = new GnB($m);
81+
82+
$chunk = new Chunk(DOMDocumentFactory::fromString(
83+
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
84+
)->documentElement);
85+
86+
$fieldId = new FieldID($prime, $tnb, $pnb, $gnb, [$chunk]);
87+
88+
// Build Curve
89+
$a = new A('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
90+
$b = new B('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
91+
$curve = new Curve($a, $b);
92+
93+
// Build Base
94+
$base = new Base('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
95+
96+
// Build Order
97+
$order = new Order('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
98+
99+
// Build CoFactor
100+
$coFactor = new CoFactor('128');
101+
102+
// Build ValidationData
103+
$seed = new Seed('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
104+
$validationData = new ValidationData($seed, C::DIGEST_SHA1);
105+
106+
// Build ECParameters
107+
$ecParameters = new ECParameters($fieldId, $curve, $base, $order, $coFactor, $validationData);
108+
109+
$this->assertEquals(
110+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
111+
strval($ecParameters),
112+
);
113+
}
114+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<dsig11:ECParameters xmlns:dsig11="http://www.w3.org/2009/xmldsig11#">
2+
<dsig11:FieldID>
3+
<dsig11:Prime>
4+
<dsig11:P>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:P>
5+
</dsig11:Prime>
6+
<dsig11:TnB>
7+
<dsig11:M>1024</dsig11:M>
8+
<dsig11:K>64</dsig11:K>
9+
</dsig11:TnB>
10+
<dsig11:PnB>
11+
<dsig11:M>1024</dsig11:M>
12+
<dsig11:K1>128</dsig11:K1>
13+
<dsig11:K2>256</dsig11:K2>
14+
<dsig11:K3>512</dsig11:K3>
15+
</dsig11:PnB>
16+
<dsig11:GnB>
17+
<dsig11:M>1024</dsig11:M>
18+
</dsig11:GnB>
19+
<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>
20+
</dsig11:FieldID>
21+
<dsig11:Curve>
22+
<dsig11:A>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:A>
23+
<dsig11:B>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:B>
24+
</dsig11:Curve>
25+
<dsig11:Base>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:Base>
26+
<dsig11:Orde>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:Order>
27+
<dsig11:CoFactor>128</dsig11:CoFactor>
28+
<dsig11:ValidationData hashAlgorithm="http://www.w3.org/2000/09/xmldsig#sha1">
29+
<dsig11:Seed>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:Seed>
30+
</dsig11:ValidationData>
31+
</dsig11:ECParameters>

0 commit comments

Comments
 (0)