Skip to content

Commit 1d6d6fa

Browse files
committed
Add dsig11:ECKeyValue-element
1 parent f8410a0 commit 1d6d6fa

File tree

4 files changed

+322
-0
lines changed

4 files changed

+322
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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\SchemaViolationException;
10+
11+
/**
12+
* Abstract class representing a dsig11:ECKeyValueType
13+
*
14+
* @package simplesaml/xml-security
15+
*/
16+
abstract class AbstractECKeyValueType extends AbstractDsig11Element
17+
{
18+
/**
19+
* Initialize a FieldIDType element.
20+
*
21+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\PublicKey $publicKey
22+
* @param string|null $id
23+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\ECParameters|null $ecParameters
24+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\NamedCurve|null $namedCurve
25+
*/
26+
public function __construct(
27+
protected PublicKey $publicKey,
28+
protected ?string $id = null,
29+
protected ?ECParameters $ecParameters = null,
30+
protected ?NamedCurve $ecParamOrNamedCurve = null,
31+
) {
32+
Assert::validNCName($id, SchemaViolationException::class);
33+
Assert::oneOf(
34+
null,
35+
[$ecParameters, $namedCurve],
36+
'The ECParameters and NamedCurve are mutually exclusive; please specify one or the other.',
37+
SchemaViolationException::class,
38+
);
39+
}
40+
41+
42+
/**
43+
* Collect the value of the ecParameters-property
44+
*
45+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\ECParameters|null
46+
*/
47+
public function getECParameters(): ?ECParameters
48+
{
49+
return $this->ecParameters;
50+
}
51+
52+
53+
/**
54+
* Collect the value of the namedCurve-property
55+
*
56+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\NamedCurve|null
57+
*/
58+
public function getNamedCurve(): ?NamedCurve
59+
{
60+
return $this->namedCurve;
61+
}
62+
63+
64+
/**
65+
* Collect the value of the publicKey-property
66+
*
67+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\PublicKey
68+
*/
69+
public function getPublicKey(): PublicKey
70+
{
71+
return $this->publicKey;
72+
}
73+
74+
75+
/**
76+
* Collect the value of the id-property
77+
*
78+
* @return string|null
79+
*/
80+
public function getId(): string
81+
{
82+
return $this->id;
83+
}
84+
85+
86+
/**
87+
* Convert this ECKeyValueType element to XML.
88+
*
89+
* @param \DOMElement|null $parent The element we should append this ECKeyValueType element to.
90+
* @return \DOMElement
91+
*/
92+
public function toXML(?DOMElement $parent = null): DOMElement
93+
{
94+
$e = $this->instantiateParentElement($parent);
95+
96+
if ($this->getId() !== null) {
97+
$e->setAttribute('Id', $this->getId());
98+
}
99+
100+
$this->getECParameters()?->toXML($e);
101+
$this->getNamedCurve()?->toXML($e);
102+
$this->getPublicKey()->toXML($e);
103+
104+
return $e;
105+
}
106+
}

src/XML/dsig11/ECKeyValue.php

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

0 commit comments

Comments
 (0)