Skip to content

Commit ae8dca7

Browse files
committed
Add dsig11:DEREncodedKeyValue
1 parent e131fee commit ae8dca7

File tree

6 files changed

+167
-4
lines changed

6 files changed

+167
-4
lines changed

src/XML/ds/AbstractKeyInfoType.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use SimpleSAML\XMLSecurity\Assert\Assert;
1313
use SimpleSAML\XMLSecurity\Constants as C;
1414
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
15-
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
15+
use SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue;
1616

1717
/**
1818
* Abstract class representing the KeyInfoType.
@@ -38,6 +38,7 @@ abstract class AbstractKeyInfoType extends AbstractDsElement
3838
* \SimpleSAML\XMLSecurity\XML\ds\PGPData|
3939
* \SimpleSAML\XMLSecurity\XML\ds\SPKIData|
4040
* \SimpleSAML\XMLSecurity\XML\ds\MgmtData|
41+
* \SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue|
4142
* \SimpleSAML\XML\SerializableElementInterface
4243
* )[] $info
4344
* @param string|null $Id
@@ -78,6 +79,14 @@ final public function __construct(
7879
],
7980
SchemaViolationException::class,
8081
);
82+
} elseif ($item instanceof AbstractDsElement) {
83+
Assert::isInstanceOfAny(
84+
$item,
85+
[
86+
DEREncodedKeyValue::class,
87+
],
88+
SchemaViolationException::class,
89+
);
8190
}
8291
}
8392
}

src/XML/ds/KeyInfo.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10-
use SimpleSAML\XML\SchemaValidatableElementInterface;
11-
use SimpleSAML\XML\SchemaValidatableElementTrait;
10+
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
11+
use SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue;
1212

1313
use function array_merge;
1414

@@ -44,6 +44,7 @@ public static function fromXML(DOMElement $xml): static
4444
$pgpData = PGPData::getChildrenOfClass($xml);
4545
$spkiData = SPKIData::getChildrenOfClass($xml);
4646
$mgmtData = MgmtData::getChildrenOfClass($xml);
47+
$derEncodedKeyValue = DEREncodedKeyValue::getChildrenOfClass($xml);
4748
$other = self::getChildElementsFromXML($xml);
4849

4950
$info = array_merge(
@@ -54,6 +55,7 @@ public static function fromXML(DOMElement $xml): static
5455
$pgpData,
5556
$spkiData,
5657
$mgmtData,
58+
$derEncodedKeyValue,
5759
$other,
5860
);
5961

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
use SimpleSAML\XML\Assert\Assert;
9+
use SimpleSAML\XML\Base64ElementTrait;
10+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11+
use SimpleSAML\XML\Exception\SchemaViolationException;
12+
use SimpleSAML\XML\SchemaValidatableElementInterface;
13+
use SimpleSAML\XML\SchemaValidatableElementTrait;
14+
15+
/**
16+
* Class representing a dsig11:DEREncodedKeyValue element.
17+
*
18+
* @package simplesaml/xml-security
19+
*/
20+
final class DEREncodedKeyValue extends AbstractDsig11Element implements SchemaValidatableElementInterface
21+
{
22+
use Base64ElementTrait;
23+
use SchemaValidatableElementTrait;
24+
25+
26+
/**
27+
* Initialize a DEREncodedKeyValue element.
28+
*
29+
* @param string $value
30+
* @param string|null $Id
31+
*/
32+
public function __construct(
33+
string $value,
34+
protected ?string $Id = null,
35+
) {
36+
Assert::validNCName($Id, SchemaViolationException::class);
37+
38+
$this->setContent($value);
39+
}
40+
41+
42+
/**
43+
* Collect the value of the Id-property
44+
*
45+
* @return string|null
46+
*/
47+
public function getId(): ?string
48+
{
49+
return $this->Id;
50+
}
51+
52+
53+
/**
54+
* Convert XML into a DEREncodedKeyValue
55+
*
56+
* @param \DOMElement $xml The XML element we should load
57+
* @return static
58+
*
59+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
60+
* If the qualified name of the supplied element is wrong
61+
*/
62+
public static function fromXML(DOMElement $xml): static
63+
{
64+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
65+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
66+
67+
return new static(
68+
$xml->textContent,
69+
self::getOptionalAttribute($xml, 'Id', null),
70+
);
71+
}
72+
73+
74+
/**
75+
* Convert this DEREncodedKeyValue element to XML.
76+
*
77+
* @param \DOMElement|null $parent The element we should append this DEREncodedKeyValue element to.
78+
* @return \DOMElement
79+
*/
80+
public function toXML(?DOMElement $parent = null): DOMElement
81+
{
82+
$e = $this->instantiateParentElement($parent);
83+
$e->textContent = $this->getContent();
84+
85+
if ($this->getId() !== null) {
86+
$e->setAttribute('Id', $this->getId());
87+
}
88+
89+
return $e;
90+
}
91+
}

src/XML/element.registry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
'X509Data' => '\SimpleSAML\XMLSecurity\XML\ds\X509Data',
3131
],
3232
'http://www.w3.org/2009/xmldsig11#' => [
33-
// 'DEREncodedKeyValue' => '\SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue',
33+
'DEREncodedKeyValue' => '\SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue',
3434
// 'ECKeyValue' => '\SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue',
3535
// 'GnB' => '\SimpleSAML\XMLSecurity\XML\dsig11\GnB',
3636
'KeyInfoReference' => '\SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference',
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\DOMDocumentFactory;
10+
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
11+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12+
use SimpleSAML\XMLSecurity\Test\XML\XMLDumper;
13+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
14+
use SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue;
15+
16+
use function dirname;
17+
use function strval;
18+
19+
/**
20+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\DEREncodedKeyValueTest
21+
*
22+
* @package simplesamlphp/xml-security
23+
*/
24+
#[CoversClass(AbstractDsig11Element::class)]
25+
#[CoversClass(DEREncodedKeyValue::class)]
26+
final class DEREncodedKeyValueTest extends TestCase
27+
{
28+
use SchemaValidationTestTrait;
29+
use SerializableElementTestTrait;
30+
31+
32+
/**
33+
*/
34+
public static function setUpBeforeClass(): void
35+
{
36+
self::$testedClass = DEREncodedKeyValue::class;
37+
38+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
39+
dirname(__FILE__, 3) . '/resources/xml/dsig11_DEREncodedKeyValue.xml',
40+
);
41+
}
42+
43+
44+
/**
45+
*/
46+
public function testMarshalling(): void
47+
{
48+
$derEncodedKeyValue = new DEREncodedKeyValue(
49+
'MGYwHwYIKoUDBwEBAQEwEwYHKoUDAgIkAAYIKoUDBwEBAgIDQwAEQLrf0MNTFKvS'
50+
. 'j6pHRwtsQBdyu07oB36PZ+duQ9rOZhWXQ+acH/dP4uLxdJhZq/Z30cDGD+KND4NZ'
51+
. 'jp+UZWlzWK0=',
52+
'phpunit',
53+
);
54+
55+
$this->assertEquals(
56+
XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation),
57+
strval($derEncodedKeyValue),
58+
);
59+
}
60+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<dsig11:DEREncodedKeyValue xmlns:dsig11="http://www.w3.org/2009/xmldsig11#" Id="phpunit">MGYwHwYIKoUDBwEBAQEwEwYHKoUDAgIkAAYIKoUDBwEBAgIDQwAEQLrf0MNTFKvSj6pHRwtsQBdyu07oB36PZ+duQ9rOZhWXQ+acH/dP4uLxdJhZq/Z30cDGD+KND4NZjp+UZWlzWK0=</dsig11:DEREncodedKeyValue>

0 commit comments

Comments
 (0)