Skip to content

Commit 0181f5a

Browse files
committed
Add element xenc:RecipientKeyInfo
1 parent 94216ed commit 0181f5a

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

src/XML/xenc/RecipientKeyInfo.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\xenc;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XMLSecurity\Constants as C;
11+
use SimpleSAML\XMLSecurity\XML\ds\AbstractKeyInfoType;
12+
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
13+
use SimpleSAML\XMLSecurity\XML\ds\KeyValue;
14+
use SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod;
15+
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
16+
17+
use function array_merge;
18+
19+
/**
20+
* Class representing a xenc:RecipientKeyInfo element.
21+
*
22+
* @package simplesamlphp/xml-security
23+
*/
24+
final class RecipientKeyInfo extends AbstractKeyInfoType
25+
{
26+
/** @var string */
27+
public const NS = C::NS_XENC;
28+
29+
/** @var string */
30+
public const NS_PREFIX = 'xenc';
31+
32+
33+
/**
34+
* Convert XML into a RecipientKeyInfo
35+
*
36+
* @param \DOMElement $xml The XML element we should load
37+
* @return static
38+
*
39+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
40+
* If the qualified name of the supplied element is wrong
41+
*/
42+
public static function fromXML(DOMElement $xml): static
43+
{
44+
Assert::same($xml->localName, 'RecipientKeyInfo', InvalidDOMElementException::class);
45+
Assert::same($xml->namespaceURI, RecipientKeyInfo::NS, InvalidDOMElementException::class);
46+
47+
$Id = self::getOptionalAttribute($xml, 'Id', null);
48+
49+
$keyName = KeyName::getChildrenOfClass($xml);
50+
$keyValue = KeyValue::getChildrenOfClass($xml);
51+
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml);
52+
$x509Data = X509Data::getChildrenOfClass($xml);
53+
//$pgpData = PGPData::getChildrenOfClass($xml);
54+
//$spkiData = SPKIData::getChildrenOfClass($xml);
55+
//$mgmtData = MgmtData::getChildrenOfClass($xml);
56+
$other = self::getChildElementsFromXML($xml);
57+
58+
$info = array_merge(
59+
$keyName,
60+
$keyValue,
61+
$retrievalMethod,
62+
$x509Data,
63+
//$pgpdata,
64+
//$spkidata,
65+
//$mgmtdata,
66+
$other,
67+
);
68+
69+
return new static($info, $Id);
70+
}
71+
}
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML\xenc;
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\Exception\InvalidArgumentException;
13+
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
14+
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
15+
use SimpleSAML\XMLSecurity\XML\ds\AbstractKeyInfoType;
16+
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
17+
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
18+
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
19+
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
20+
use SimpleSAML\XMLSecurity\XML\xenc\RecipientKeyInfo;
21+
22+
use function dirname;
23+
use function openssl_x509_parse;
24+
use function str_replace;
25+
use function strval;
26+
27+
/**
28+
* Class \SimpleSAML\XMLSecurity\Test\XML\xenc\RecipientKeyInfoTest
29+
*
30+
* @package simplesamlphp/xml-security
31+
*/
32+
#[CoversClass(AbstractDsElement::class)]
33+
#[CoversClass(AbstractKeyInfoType::class)]
34+
#[CoversClass(RecipientKeyInfo::class)]
35+
final class RecipientKeyInfoTest extends TestCase
36+
{
37+
use SerializableElementTestTrait;
38+
39+
/** @var string */
40+
private static string $certificate;
41+
42+
/** @var string[] */
43+
private static array $certData;
44+
45+
46+
/**
47+
*/
48+
public function setUp(): void
49+
{
50+
self::$testedClass = RecipientKeyInfo::class;
51+
52+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
53+
dirname(__FILE__, 3) . '/resources/xml/xenc_RecipientKeyInfo.xml',
54+
);
55+
56+
self::$certificate = str_replace(
57+
[
58+
'-----BEGIN CERTIFICATE-----',
59+
'-----END CERTIFICATE-----',
60+
'-----BEGIN RSA PUBLIC KEY-----',
61+
'-----END RSA PUBLIC KEY-----',
62+
"\r\n",
63+
"\n",
64+
],
65+
[
66+
'',
67+
'',
68+
'',
69+
'',
70+
"\n",
71+
'',
72+
],
73+
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
74+
);
75+
76+
self::$certData = openssl_x509_parse(
77+
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
78+
);
79+
}
80+
81+
82+
/**
83+
*/
84+
public function testMarshalling(): void
85+
{
86+
$recipientKeyInfo = new RecipientKeyInfo(
87+
[
88+
new KeyName('testkey'),
89+
new X509Data(
90+
[
91+
new X509Certificate(self::$certificate),
92+
new X509SubjectName(self::$certData['name']),
93+
],
94+
),
95+
new Chunk(DOMDocumentFactory::fromString(
96+
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
97+
)->documentElement),
98+
],
99+
'fed654',
100+
);
101+
102+
$this->assertEquals(
103+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
104+
strval($recipientKeyInfo),
105+
);
106+
}
107+
108+
109+
/**
110+
*/
111+
public function testMarshallingEmpty(): void
112+
{
113+
$this->expectException(InvalidArgumentException::class);
114+
$this->expectExceptionMessage('xenc:RecipientKeyInfo cannot be empty');
115+
116+
new RecipientKeyInfo([]);
117+
}
118+
119+
120+
/**
121+
*/
122+
public function testUnmarshallingEmpty(): void
123+
{
124+
$document = DOMDocumentFactory::fromString('<xenc:RecipientKeyInfo xmlns:xenc="' . RecipientKeyInfo::NS . '"/>');
125+
126+
$this->expectException(InvalidArgumentException::class);
127+
$this->expectExceptionMessage('xenc:RecipientKeyInfo cannot be empty');
128+
129+
RecipientKeyInfo::fromXML($document->documentElement);
130+
}
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<xenc:RecipientKeyInfo xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="fed654">
2+
<ds:KeyName xmlns:ds="http://www.w3.org/2000/09/xmldsig#">testkey</ds:KeyName>
3+
<ds:X509Data xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
4+
<ds:X509Certificate>MIICxDCCAi2gAwIBAgIUZ9QDx+SBFHednUWDFGm9tyVKrgQwDQYJKoZIhvcNAQELBQAwczElMCMGA1UEAwwcc2VsZnNpZ25lZC5zaW1wbGVzYW1scGhwLm9yZzEZMBcGA1UECgwQU2ltcGxlU0FNTHBocCBIUTERMA8GA1UEBwwISG9ub2x1bHUxDzANBgNVBAgMBkhhd2FpaTELMAkGA1UEBhMCVVMwIBcNMjIxMjAzMTAzNTQwWhgPMjEyMjExMDkxMDM1NDBaMHMxJTAjBgNVBAMMHHNlbGZzaWduZWQuc2ltcGxlc2FtbHBocC5vcmcxGTAXBgNVBAoMEFNpbXBsZVNBTUxwaHAgSFExETAPBgNVBAcMCEhvbm9sdWx1MQ8wDQYDVQQIDAZIYXdhaWkxCzAJBgNVBAYTAlVTMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDessdFRVDTMQQW3Na81B1CjJV1tmY3nopoIhZrkbDxLa+pv7jGDRcYreyu1DoQxEs06V2nHLoyOPhqJXSFivqtUwVYhR6NYgbNI6RRSsIJCweH0YOdlHna7gULPcLX0Bfbi4odStaFwG9yzDySwSEPtsKxm5pENPjNVGh+jJ+H/QIDAQABo1MwUTAdBgNVHQ4EFgQUvV75t8EoQo2fVa0E9otdtIGK5X0wHwYDVR0jBBgwFoAUvV75t8EoQo2fVa0E9otdtIGK5X0wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQANQUeiwPJXkWMXuaDHToEBKcezYGqGEYnGUi9LMjeb+Kln7X8nn5iknlz4k77rWCbSwLPC/WDr0ySYQA+HagaeUaFpoiYFJKS6uFlK1HYWnM3W4PUiGHg1/xeZlMO44wTwybXVo0y9KMhchfB5XNbDdoJcqWYvi6xtmZZNRbxUyw==</ds:X509Certificate>
5+
<ds:X509SubjectName>/CN=selfsigned.simplesamlphp.org/O=SimpleSAMLphp HQ/L=Honolulu/ST=Hawaii/C=US</ds:X509SubjectName>
6+
</ds:X509Data>
7+
<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>
8+
</xenc:RecipientKeyInfo>

0 commit comments

Comments
 (0)