Skip to content

Commit 516483b

Browse files
committed
Add dsig11:DEREncodedKeyValue
1 parent e131fee commit 516483b

File tree

6 files changed

+175
-4
lines changed

6 files changed

+175
-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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\Base64ElementTrait;
10+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11+
use SimpleSAML\XML\Exception\SchemaViolationException;
12+
use SimpleSAML\XML\SchemaValidatableElementInterface;
13+
use SimpleSAML\XML\SchemaValidatableElementTrait;
14+
use SimpleSAML\XMLSecurity\Constants as C;
15+
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16+
17+
/**
18+
* Class representing a dsig11:DEREncodedKeyValue element.
19+
*
20+
* @package simplesaml/xml-security
21+
*/
22+
final class DEREncodedKeyValue extends AbstractDsig11Element implements SchemaValidatableElementInterface
23+
{
24+
use Base64ElementTrait;
25+
use SchemaValidatableElementTrait;
26+
27+
28+
/**
29+
* Initialize a DEREncodedKeyValue element.
30+
*
31+
* @param string $value
32+
* @param string|null $Id
33+
*/
34+
public function __construct(
35+
string $value,
36+
protected ?string $Id = null,
37+
) {
38+
Assert::validNCName($Id, SchemaViolationException::class);
39+
40+
$this->setContent($value);
41+
}
42+
43+
44+
/**
45+
* Collect the value of the Id-property
46+
*
47+
* @return string|null
48+
*/
49+
public function getId(): ?string
50+
{
51+
return $this->Id;
52+
}
53+
54+
55+
/**
56+
* Convert XML into a DEREncodedKeyValue
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::LOCALNAME, InvalidDOMElementException::class);
67+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
68+
69+
return new static(
70+
$xml->textContent,
71+
self::getOptionalAttribute($xml, 'Id', null),
72+
);
73+
}
74+
75+
76+
/**
77+
* Convert this DEREncodedKeyValue element to XML.
78+
*
79+
* @param \DOMElement|null $parent The element we should append this DEREncodedKeyValue element to.
80+
* @return \DOMElement
81+
*/
82+
public function toXML(?DOMElement $parent = null): DOMElement
83+
{
84+
$e = $this->instantiateParentElement($parent);
85+
$e->textContent = $this->getContent();
86+
87+
if ($this->getId() !== null) {
88+
$e->setAttribute('Id', $this->getId());
89+
}
90+
91+
return $e;
92+
}
93+
}

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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Constants as C;
13+
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
14+
use SimpleSAML\XMLSecurity\Key;
15+
use SimpleSAML\XMLSecurity\Test\XML\XMLDumper;
16+
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
17+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
18+
use SimpleSAML\XMLSecurity\XML\dsig11\DEREncodedKeyValue;
19+
20+
use function base64_encode;
21+
use function dirname;
22+
use function hex2bin;
23+
use function strval;
24+
25+
/**
26+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\DEREncodedKeyValueTest
27+
*
28+
* @package simplesamlphp/xml-security
29+
*/
30+
#[CoversClass(AbstractDsig11Element::class)]
31+
#[CoversClass(DEREncodedKeyValue::class)]
32+
final class DEREncodedKeyValueTest extends TestCase
33+
{
34+
use SchemaValidationTestTrait;
35+
use SerializableElementTestTrait;
36+
37+
38+
/**
39+
*/
40+
public static function setUpBeforeClass(): void
41+
{
42+
self::$testedClass = DEREncodedKeyValue::class;
43+
44+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
45+
dirname(__FILE__, 3) . '/resources/xml/dsig11_DEREncodedKeyValue.xml',
46+
);
47+
}
48+
49+
50+
/**
51+
*/
52+
public function testMarshalling(): void
53+
{
54+
$derEncodedKeyValue = new DEREncodedKeyValue(
55+
'MGYwHwYIKoUDBwEBAQEwEwYHKoUDAgIkAAYIKoUDBwEBAgIDQwAEQLrf0MNTFKvS'
56+
. 'j6pHRwtsQBdyu07oB36PZ+duQ9rOZhWXQ+acH/dP4uLxdJhZq/Z30cDGD+KND4NZ'
57+
. 'jp+UZWlzWK0=',
58+
'phpunit',
59+
);
60+
61+
$this->assertEquals(
62+
XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation),
63+
strval($derEncodedKeyValue),
64+
);
65+
}
66+
}
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)