1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\XML\xenc; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use SimpleSAML\XML\Chunk; |
9
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
10
|
|
|
use SimpleSAML\XML\Exception\MissingAttributeException; |
11
|
|
|
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; |
12
|
|
|
use SimpleSAML\XMLSecurity\Utils\XPath; |
13
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod; |
14
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\KeySize; |
15
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\OAEPparams; |
16
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
17
|
|
|
|
18
|
|
|
use function dirname; |
19
|
|
|
use function strval; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Tests for the xenc:EncryptionMethod element. |
23
|
|
|
* |
24
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractXencElement |
25
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractEncryptionMethod |
26
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod |
27
|
|
|
* @package simplesamlphp/xml-security |
28
|
|
|
*/ |
29
|
|
|
final class EncryptionMethodTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
use SerializableElementTestTrait; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
*/ |
35
|
|
|
public static function setUpBeforeClass(): void |
36
|
|
|
{ |
37
|
|
|
self::$testedClass = EncryptionMethod::class; |
38
|
|
|
|
39
|
|
|
self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
40
|
|
|
dirname(__FILE__, 3) . '/resources/xml/xenc_EncryptionMethod.xml', |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
// test marshalling |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test creating an EncryptionMethod object from scratch. |
50
|
|
|
*/ |
51
|
|
|
public function testMarshalling(): void |
52
|
|
|
{ |
53
|
|
|
$alg = 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'; |
54
|
|
|
$chunkXml = DOMDocumentFactory::fromString('<other:Element xmlns:other="urn:other:enc">Value</other:Element>'); |
55
|
|
|
$chunk = Chunk::fromXML($chunkXml->documentElement); |
56
|
|
|
|
57
|
|
|
$em = new EncryptionMethod($alg, new KeySize(10), new OAEPparams('9lWu3Q=='), [$chunk]); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( |
60
|
|
|
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
61
|
|
|
strval($em), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Test that creating an EncryptionMethod object from scratch works when no optional elements have been specified. |
68
|
|
|
*/ |
69
|
|
|
public function testMarshallingWithoutOptionalParameters(): void |
70
|
|
|
{ |
71
|
|
|
$em = new EncryptionMethod('http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'); |
72
|
|
|
$document = DOMDocumentFactory::fromString( |
73
|
|
|
'<xenc:EncryptionMethod xmlns:xenc="' . C::NS_XENC . |
74
|
|
|
'" Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>', |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->assertNull($em->getKeySize()); |
78
|
|
|
$this->assertNull($em->getOAEPParams()); |
79
|
|
|
$this->assertEmpty($em->getElements()); |
80
|
|
|
$this->assertEquals( |
81
|
|
|
$document->saveXML($document->documentElement), |
82
|
|
|
strval($em), |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function testMarshallingElementOrdering(): void |
88
|
|
|
{ |
89
|
|
|
$alg = 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'; |
90
|
|
|
$chunkXml = DOMDocumentFactory::fromString('<other:Element xmlns:other="urn:other:enc">Value</other:Element>'); |
91
|
|
|
$chunk = Chunk::fromXML($chunkXml->documentElement); |
92
|
|
|
|
93
|
|
|
$em = new EncryptionMethod($alg, new KeySize(10), new OAEPparams('9lWu3Q=='), [$chunk]); |
94
|
|
|
|
95
|
|
|
// Marshall it to a \DOMElement |
96
|
|
|
$emElement = $em->toXML(); |
97
|
|
|
|
98
|
|
|
$xpCache = XPath::getXPath($emElement); |
99
|
|
|
|
100
|
|
|
// Test for a KeySize |
101
|
|
|
$keySizeElements = XPath::xpQuery($emElement, './xenc:KeySize', $xpCache); |
102
|
|
|
$this->assertCount(1, $keySizeElements); |
103
|
|
|
$this->assertEquals('10', $keySizeElements[0]->textContent); |
104
|
|
|
|
105
|
|
|
// Test ordering of EncryptionMethod contents |
106
|
|
|
/** @var \DOMElement[] $emElements */ |
107
|
|
|
$emElements = XPath::xpQuery($emElement, './xenc:KeySize/following-sibling::*', $xpCache); |
108
|
|
|
|
109
|
|
|
$this->assertCount(2, $emElements); |
110
|
|
|
$this->assertEquals('xenc:OAEPparams', $emElements[0]->tagName); |
111
|
|
|
$this->assertEquals('other:Element', $emElements[1]->tagName); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
// test unmarshalling |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Test that creating an EncryptionMethod object from XML without an Algorithm attribute fails. |
120
|
|
|
*/ |
121
|
|
|
public function testUnmarshallingWithoutAlgorithm(): void |
122
|
|
|
{ |
123
|
|
|
$xmlRepresentation = clone self::$xmlRepresentation->documentElement; |
124
|
|
|
$xmlRepresentation->removeAttribute('Algorithm'); |
125
|
|
|
|
126
|
|
|
$this->expectException(MissingAttributeException::class); |
127
|
|
|
$this->expectExceptionMessage('Missing \'Algorithm\' attribute on xenc:EncryptionMethod.'); |
128
|
|
|
EncryptionMethod::fromXML($xmlRepresentation); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test that creating an EncryptionMethod object from XML works if no optional elements are present. |
134
|
|
|
*/ |
135
|
|
|
public function testUnmarshallingWithoutOptionalParameters(): void |
136
|
|
|
{ |
137
|
|
|
$xencns = C::NS_XENC; |
138
|
|
|
$document = DOMDocumentFactory::fromString(<<<XML |
139
|
|
|
<xenc:EncryptionMethod xmlns:xenc="{$xencns}" Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/> |
140
|
|
|
XML |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$em = EncryptionMethod::fromXML($document->documentElement); |
144
|
|
|
$this->assertNull($em->getKeySize()); |
145
|
|
|
$this->assertNull($em->getOAEPParams()); |
146
|
|
|
$this->assertEmpty($em->getElements()); |
147
|
|
|
$this->assertEquals( |
148
|
|
|
$document->saveXML($document->documentElement), |
149
|
|
|
strval($em), |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|