Skip to content

Commit b5fed62

Browse files
committed
Add ds:SPKIData element
1 parent 47ff576 commit b5fed62

15 files changed

+289
-7
lines changed

src/XML/ds/AbstractKeyInfoType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ abstract class AbstractKeyInfoType extends AbstractDsElement
3636
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
3737
* \SimpleSAML\XMLSecurity\XML\ds\X509Data|
3838
* \SimpleSAML\XMLSecurity\XML\ds\PGPData|
39+
* \SimpleSAML\XMLSecurity\XML\ds\SPKIData|
3940
* \SimpleSAML\XMLSecurity\XML\ds\MgmtData|
4041
* \SimpleSAML\XML\SerializableElementInterface
4142
* )[] $info
@@ -72,7 +73,7 @@ final public function __construct(
7273
RetrievalMethod::class,
7374
X509Data::class,
7475
PGPData::class,
75-
// SPKIData::class,
76+
SPKIData::class,
7677
MgmtData::class,
7778
],
7879
SchemaViolationException::class,
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\ds;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Chunk;
10+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11+
use SimpleSAML\XML\Exception\SchemaViolationException;
12+
use SimpleSAML\XML\Exception\TooManyElementsException;
13+
use SimpleSAML\XML\Registry\ElementRegistry;
14+
use SimpleSAML\XML\SerializableElementInterface;
15+
use SimpleSAML\XML\XsNamespace as NS;
16+
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
17+
18+
/**
19+
* Abstract class representing the SPKIDataType.
20+
*
21+
* @package simplesamlphp/xml-security
22+
*/
23+
abstract class AbstractSPKIDataType extends AbstractDsElement
24+
{
25+
/**
26+
* Initialize a SPKIData element.
27+
*
28+
* @param array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null> $tuples
29+
*/
30+
final public function __construct(
31+
protected array $tuples,
32+
) {
33+
Assert::allIsArray($tuples, SchemaViolationException::class);
34+
Assert::allCount($tuples, 2);
35+
36+
foreach ($tuples as $tuple) {
37+
list($spkisExp, $other) = $tuple;
38+
Assert::isInstanceOf($spkisExp, SPKISexp::class, SchemaViolationException::class);
39+
Assert::nullOrIsInstanceOf($other, SerializableElementInterface::class, SchemaViolationException::class);
40+
}
41+
}
42+
43+
44+
/**
45+
* Collect the value of the SPKISexp-property
46+
*
47+
* @return array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null>
48+
*/
49+
public function getTuples(): array
50+
{
51+
return $this->tuples;
52+
}
53+
54+
55+
/**
56+
* Convert XML into a SPKIData
57+
*
58+
* @param \DOMElement $xml The XML element we should load
59+
* @return static
60+
*
61+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
62+
* If the qualified name of the supplied element is wrong
63+
*/
64+
public static function fromXML(DOMElement $xml): static
65+
{
66+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
67+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
68+
69+
$registry = ElementRegistry::getInstance();
70+
$tuples = [];
71+
$tuple = [null, null];
72+
foreach ($xml->childNodes as $node) {
73+
if ($node instanceof DOMElement) {
74+
if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') {
75+
if ($tuple[0] !== null) {
76+
$tuples[] = $tuple;
77+
}
78+
$tuple = [SPKISexp::fromXML($node), null];
79+
} elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) {
80+
$handler = $registry->getElementHandler($node->namespaceURI, $node->localName);
81+
$tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node);
82+
$tuples[] = $tuple;
83+
$tuple = [null, null];
84+
}
85+
}
86+
}
87+
88+
if ($tuple[0] !== null) {
89+
$tuples[] = $tuple;
90+
}
91+
92+
return new static($tuples);
93+
}
94+
95+
96+
/**
97+
* Convert this SPKIData to XML.
98+
*
99+
* @param \DOMElement|null $parent The element we should append this SPKIData to.
100+
* @return \DOMElement
101+
*/
102+
public function toXML(?DOMElement $parent = null): DOMElement
103+
{
104+
$e = $this->instantiateParentElement($parent);
105+
106+
foreach ($this->getTuples() as $tuple) {
107+
list($spkisExp, $other) = $tuple;
108+
109+
$spkisExp->toXML($e);
110+
$other?->toXML($e);
111+
}
112+
113+
return $e;
114+
}
115+
}

src/XML/ds/KeyInfo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function fromXML(DOMElement $xml): static
3838
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml);
3939
$x509Data = X509Data::getChildrenOfClass($xml);
4040
$pgpData = PGPData::getChildrenOfClass($xml);
41-
//$spkiData = SPKIData::getChildrenOfClass($xml);
41+
$spkiData = SPKIData::getChildrenOfClass($xml);
4242
$mgmtData = MgmtData::getChildrenOfClass($xml);
4343
$other = self::getChildElementsFromXML($xml);
4444

@@ -48,7 +48,7 @@ public static function fromXML(DOMElement $xml): static
4848
$retrievalMethod,
4949
$x509Data,
5050
$pgpData,
51-
//$spkiData,
51+
$spkiData,
5252
$mgmtData,
5353
$other,
5454
);

src/XML/ds/SPKIData.php

Lines changed: 14 additions & 0 deletions
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\ds;
6+
7+
/**
8+
* Class representing a ds:SPKIData element.
9+
*
10+
* @package simplesaml/xml-security
11+
*/
12+
final class SPKIData extends AbstractSPKIDataType
13+
{
14+
}

src/XML/xenc/OriginatorKeyInfo.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SimpleSAML\XMLSecurity\XML\ds\MgmtData;
1515
use SimpleSAML\XMLSecurity\XML\ds\PGPData;
1616
use SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod;
17+
use SimpleSAML\XMLSecurity\XML\ds\SPKIData;
1718
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
1819

1920
use function array_merge;
@@ -53,7 +54,7 @@ public static function fromXML(DOMElement $xml): static
5354
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml);
5455
$x509Data = X509Data::getChildrenOfClass($xml);
5556
$pgpData = PGPData::getChildrenOfClass($xml);
56-
//$spkiData = SPKIData::getChildrenOfClass($xml);
57+
$spkiData = SPKIData::getChildrenOfClass($xml);
5758
$mgmtData = MgmtData::getChildrenOfClass($xml);
5859
$other = self::getChildElementsFromXML($xml);
5960

@@ -63,7 +64,7 @@ public static function fromXML(DOMElement $xml): static
6364
$retrievalMethod,
6465
$x509Data,
6566
$pgpData,
66-
//$spkiData,
67+
$spkiData,
6768
$mgmtData,
6869
$other,
6970
);

src/XML/xenc/RecipientKeyInfo.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SimpleSAML\XMLSecurity\XML\ds\MgmtData;
1515
use SimpleSAML\XMLSecurity\XML\ds\PGPData;
1616
use SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod;
17+
use SimpleSAML\XMLSecurity\XML\ds\SPKIData;
1718
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
1819

1920
use function array_merge;
@@ -53,7 +54,7 @@ public static function fromXML(DOMElement $xml): static
5354
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml);
5455
$x509Data = X509Data::getChildrenOfClass($xml);
5556
$pgpData = PGPData::getChildrenOfClass($xml);
56-
//$spkiData = SPKIData::getChildrenOfClass($xml);
57+
$spkiData = SPKIData::getChildrenOfClass($xml);
5758
$mgmtData = MgmtData::getChildrenOfClass($xml);
5859
$other = self::getChildElementsFromXML($xml);
5960

@@ -63,7 +64,7 @@ public static function fromXML(DOMElement $xml): static
6364
$retrievalMethod,
6465
$x509Data,
6566
$pgpData,
66-
//$spkiData,
67+
$spkiData,
6768
$mgmtData,
6869
$other,
6970
);

tests/XML/ds/KeyInfoTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
use SimpleSAML\XMLSecurity\XML\ds\PGPData;
2121
use SimpleSAML\XMLSecurity\XML\ds\PGPKeyID;
2222
use SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket;
23+
use SimpleSAML\XMLSecurity\XML\ds\SPKIData;
24+
use SimpleSAML\XMLSecurity\XML\ds\SPKISexp;
2325
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
2426
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
2527
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
28+
use SimpleSAML\XMLSecurity\XML\xenc\CarriedKeyName;
2629
use SimpleSAML\XMLSecurity\XML\xenc\P;
30+
use SimpleSAML\XMLSecurity\XML\xenc\Seed;
2731

2832
use function dirname;
2933
use function openssl_x509_parse;
@@ -92,6 +96,12 @@ public function setUp(): void
9296
*/
9397
public function testMarshalling(): void
9498
{
99+
$SPKISexp1 = new SPKISexp('GpM6');
100+
$seed = new Seed('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
101+
$SPKISexp2 = new SPKISexp('GpM7');
102+
$SPKISexp3 = new SPKISexp('GpM8');
103+
$carriedKeyName = new CarriedKeyName('Some label');
104+
95105
$keyInfo = new KeyInfo(
96106
[
97107
new KeyName('testkey'),
@@ -106,6 +116,11 @@ public function testMarshalling(): void
106116
new PGPKeyPacket('GpM8'),
107117
[new P('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=')],
108118
),
119+
new SPKIData([
120+
[$SPKISexp1, $seed],
121+
[$SPKISexp2, null],
122+
[$SPKISexp3, $carriedKeyName],
123+
]),
109124
new MgmtData('ManagementData'),
110125
new Chunk(DOMDocumentFactory::fromString(
111126
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',

tests/XML/ds/SPKIDataTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\DOMDocumentFactory;
10+
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
11+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12+
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
13+
use SimpleSAML\XMLSecurity\XML\ds\AbstractSPKIData;
14+
use SimpleSAML\XMLSecurity\XML\ds\SPKIData;
15+
use SimpleSAML\XMLSecurity\XML\ds\SPKISexp;
16+
use SimpleSAML\XMLSecurity\XML\xenc\CarriedKeyName;
17+
use SimpleSAML\XMLSecurity\XML\xenc\Seed;
18+
19+
use function dirname;
20+
use function strval;
21+
22+
/**
23+
* Class \SimpleSAML\XMLSecurity\Test\XML\ds\SPKIDataTest
24+
*
25+
* @package simplesamlphp/xml-security
26+
*/
27+
#[CoversClass(AbstractDsElement::class)]
28+
#[CoversClass(AbstractSPKIData::class)]
29+
#[CoversClass(SPKIData::class)]
30+
final class SPKIDataTest extends TestCase
31+
{
32+
use SchemaValidationTestTrait;
33+
use SerializableElementTestTrait;
34+
35+
/**
36+
*/
37+
public static function setUpBeforeClass(): void
38+
{
39+
self::$testedClass = SPKIData::class;
40+
41+
self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd';
42+
43+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
44+
dirname(__FILE__, 3) . '/resources/xml/ds_SPKIData.xml',
45+
);
46+
}
47+
48+
49+
/**
50+
*/
51+
public function testMarshalling(): void
52+
{
53+
$SPKISexp1 = new SPKISexp('GpM6');
54+
$seed = new Seed('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
55+
$SPKISexp2 = new SPKISexp('GpM7');
56+
$SPKISexp3 = new SPKISexp('GpM8');
57+
$carriedKeyName = new CarriedKeyName('Some label');
58+
59+
$SPKIData = new SPKIData([
60+
[$SPKISexp1, $seed],
61+
[$SPKISexp2, null],
62+
[$SPKISexp3, $carriedKeyName],
63+
]);
64+
65+
$this->assertEquals(
66+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
67+
strval($SPKIData),
68+
);
69+
}
70+
}

tests/XML/xenc/OriginatorKeyInfoTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
use SimpleSAML\XMLSecurity\XML\ds\PGPData;
1919
use SimpleSAML\XMLSecurity\XML\ds\PGPKeyID;
2020
use SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket;
21+
use SimpleSAML\XMLSecurity\XML\ds\SPKIData;
22+
use SimpleSAML\XMLSecurity\XML\ds\SPKISexp;
2123
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
2224
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
2325
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
26+
use SimpleSAML\XMLSecurity\XML\xenc\CarriedKeyName;
2427
use SimpleSAML\XMLSecurity\XML\xenc\OriginatorKeyInfo;
2528
use SimpleSAML\XMLSecurity\XML\xenc\P;
29+
use SimpleSAML\XMLSecurity\XML\xenc\Seed;
2630

2731
use function dirname;
2832
use function openssl_x509_parse;
@@ -88,6 +92,12 @@ public function setUp(): void
8892
*/
8993
public function testMarshalling(): void
9094
{
95+
$SPKISexp1 = new SPKISexp('GpM6');
96+
$seed = new Seed('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
97+
$SPKISexp2 = new SPKISexp('GpM7');
98+
$SPKISexp3 = new SPKISexp('GpM8');
99+
$carriedKeyName = new CarriedKeyName('Some label');
100+
91101
$originatorKeyInfo = new OriginatorKeyInfo(
92102
[
93103
new KeyName('testkey'),
@@ -102,6 +112,11 @@ public function testMarshalling(): void
102112
new PGPKeyPacket('GpM8'),
103113
[new P('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=')],
104114
),
115+
new SPKIData([
116+
[$SPKISexp1, $seed],
117+
[$SPKISexp2, null],
118+
[$SPKISexp3, $carriedKeyName],
119+
]),
105120
new MgmtData('ManagementData'),
106121
new Chunk(DOMDocumentFactory::fromString(
107122
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',

0 commit comments

Comments
 (0)