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\DOMDocumentFactory; |
9
|
|
|
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait; |
10
|
|
|
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; |
11
|
|
|
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory; |
12
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
13
|
|
|
use SimpleSAML\XMLSecurity\Key\PrivateKey; |
14
|
|
|
use SimpleSAML\XMLSecurity\Key\PublicKey; |
15
|
|
|
use SimpleSAML\XMLSecurity\Key\SymmetricKey; |
16
|
|
|
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
17
|
|
|
use SimpleSAML\XMLSecurity\Utils\XPath; |
18
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo; |
19
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\CarriedKeyName; |
20
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\CipherData; |
21
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\CipherValue; |
22
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\DataReference; |
23
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey; |
24
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod; |
25
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\ReferenceList; |
26
|
|
|
|
27
|
|
|
use function bin2hex; |
28
|
|
|
use function dirname; |
29
|
|
|
use function strval; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class \SimpleSAML\XMLSecurity\Test\XML\xenc\EncryptedKeyTest |
33
|
|
|
* |
34
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractXencElement |
35
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\xenc\AbstractEncryptedType |
36
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey |
37
|
|
|
* |
38
|
|
|
* @package simplesamlphp/xml-security |
39
|
|
|
*/ |
40
|
|
|
final class EncryptedKeyTest extends TestCase |
41
|
|
|
{ |
42
|
|
|
use SchemaValidationTestTrait; |
|
|
|
|
43
|
|
|
use SerializableElementTestTrait; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** @var \SimpleSAML\XMLSecurity\Key\PrivateKey */ |
46
|
|
|
protected static PrivateKey $privKey; |
47
|
|
|
|
48
|
|
|
/** @var \SimpleSAML\XMLSecurity\Key\PublicKey */ |
49
|
|
|
protected static PublicKey $pubKey; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
*/ |
53
|
|
|
public static function setUpBeforeClass(): void |
54
|
|
|
{ |
55
|
|
|
self::$testedClass = EncryptedKey::class; |
56
|
|
|
|
57
|
|
|
self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xenc-schema.xsd'; |
58
|
|
|
|
59
|
|
|
self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
60
|
|
|
dirname(__FILE__, 3) . '/resources/xml/xenc_EncryptedKey.xml', |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
self::$privKey = PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::PRIVATE_KEY); |
64
|
|
|
self::$pubKey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
// marshalling |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
*/ |
73
|
|
|
public function testMarshalling(): void |
74
|
|
|
{ |
75
|
|
|
$encryptedKey = new EncryptedKey( |
76
|
|
|
new CipherData(new CipherValue('3W3C4UoWshi02yrqsLC2z8Qr1FjdTz7LV9CvpunilOX4teGKsjKqNbS92DKcXLwS8s' |
77
|
|
|
. '4eHBdHejiL1bySDQT5diN/TVo8zz0AmPwX3/eHPQE91NWzceB+yaoEDauMPvi7twUdoipbLZa7cyT4QR+RO9w5P5wf4wDoTPUoQ' |
78
|
|
|
. 'V6dF9YSJqehuRFCqVJprIDZNfrKnm7WfwMiaMLvaLVdLWgXjuVdiH0lT/F4KJrhJwAnjp57KGn9mhAcwkFe+qDIMSi8Ond6I0FO' |
79
|
|
|
. 'V3SOx8NxpSTHYfZ4qE1Xn/dvUUXqgRnEFPHAw4JFmJPjgTSCPU6BdwBLzqVjh1pCLoCn66P/Zt7I9Q==')), |
80
|
|
|
'Encrypted_KEY_ID', |
81
|
|
|
'http://www.w3.org/2001/04/xmlenc#Element', |
82
|
|
|
'text/plain', |
83
|
|
|
'urn:x-simplesamlphp:encoding', |
84
|
|
|
'some_ENTITY_ID', |
85
|
|
|
new CarriedKeyName('Name of the key'), |
86
|
|
|
new EncryptionMethod('http://www.w3.org/2001/04/xmlenc#rsa-1_5'), |
87
|
|
|
new KeyInfo( |
88
|
|
|
[ |
89
|
|
|
new EncryptedKey( |
90
|
|
|
new CipherData(new CipherValue('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=')), |
91
|
|
|
null, |
92
|
|
|
null, |
93
|
|
|
null, |
94
|
|
|
null, |
95
|
|
|
null, |
96
|
|
|
null, |
97
|
|
|
new EncryptionMethod('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'), |
98
|
|
|
), |
99
|
|
|
], |
100
|
|
|
), |
101
|
|
|
new ReferenceList([new DataReference('#Encrypted_DATA_ID')]), |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$this->assertEquals( |
105
|
|
|
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
106
|
|
|
strval($encryptedKey), |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
*/ |
113
|
|
|
public function testMarshallingElementOrdering(): void |
114
|
|
|
{ |
115
|
|
|
$encryptedKey = new EncryptedKey( |
116
|
|
|
new CipherData(new CipherValue('3W3C4UoWshi02yrqsLC2z8Qr1FjdTz7LV9CvpunilOX4teGKsjKqNbS92DKcXLwS8s' |
117
|
|
|
. '4eHBdHejiL1bySDQT5diN/TVo8zz0AmPwX3/eHPQE91NWzceB+yaoEDauMPvi7twUdoipbLZa7cyT4QR+RO9w5P5wf4wDoTPUoQ' |
118
|
|
|
. 'V6dF9YSJqehuRFCqVJprIDZNfrKnm7WfwMiaMLvaLVdLWgXjuVdiH0lT/F4KJrhJwAnjp57KGn9mhAcwkFe+qDIMSi8Ond6I0FO' |
119
|
|
|
. 'V3SOx8NxpSTHYfZ4qE1Xn/dvUUXqgRnEFPHAw4JFmJPjgTSCPU6BdwBLzqVjh1pCLoCn66P/Zt7I9Q==')), |
120
|
|
|
'Encrypted_KEY_ID', |
121
|
|
|
'http://www.w3.org/2001/04/xmlenc#Element', |
122
|
|
|
'text/plain', |
123
|
|
|
'urn:x-simplesamlphp:encoding', |
124
|
|
|
'some_ENTITY_ID', |
125
|
|
|
new CarriedKeyName('Name of the key'), |
126
|
|
|
new EncryptionMethod('http://www.w3.org/2001/04/xmlenc#rsa-1_5'), |
127
|
|
|
new KeyInfo( |
128
|
|
|
[ |
129
|
|
|
new EncryptedKey( |
130
|
|
|
new CipherData(new CipherValue('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=')), |
131
|
|
|
null, |
132
|
|
|
null, |
133
|
|
|
null, |
134
|
|
|
null, |
135
|
|
|
null, |
136
|
|
|
null, |
137
|
|
|
new EncryptionMethod('http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'), |
138
|
|
|
), |
139
|
|
|
], |
140
|
|
|
), |
141
|
|
|
new ReferenceList([new DataReference('#Encrypted_DATA_ID')]), |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
// Marshall it to a \DOMElement |
145
|
|
|
$encryptedKeyElement = $encryptedKey->toXML(); |
146
|
|
|
|
147
|
|
|
$xpCache = XPath::getXPath($encryptedKeyElement); |
148
|
|
|
// Test for a ReferenceList |
149
|
|
|
$encryptedKeyElements = XPath::xpQuery( |
150
|
|
|
$encryptedKeyElement, |
151
|
|
|
'./xenc:ReferenceList', |
152
|
|
|
$xpCache, |
153
|
|
|
); |
154
|
|
|
$this->assertCount(1, $encryptedKeyElements); |
155
|
|
|
|
156
|
|
|
// Test ordering of EncryptedKey contents |
157
|
|
|
/** @var \DOMElement[] $encryptedKeyElements */ |
158
|
|
|
$encryptedKeyElements = XPath::xpQuery( |
159
|
|
|
$encryptedKeyElement, |
160
|
|
|
'./xenc:ReferenceList/following-sibling::*', |
161
|
|
|
$xpCache, |
162
|
|
|
); |
163
|
|
|
$this->assertCount(1, $encryptedKeyElements); |
164
|
|
|
$this->assertEquals('xenc:CarriedKeyName', $encryptedKeyElements[0]->tagName); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Test encryption and decryption with PKCS1 RSA 1.5. |
170
|
|
|
*/ |
171
|
|
|
public function testPKCS1Encryption(): void |
172
|
|
|
{ |
173
|
|
|
$factory = new KeyTransportAlgorithmFactory([]); |
174
|
|
|
$encryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, self::$pubKey); |
175
|
|
|
$symmetricKey = SymmetricKey::generate(8); |
176
|
|
|
$encryptedKey = EncryptedKey::fromKey( |
177
|
|
|
$symmetricKey, |
178
|
|
|
$encryptor, |
179
|
|
|
new EncryptionMethod(C::KEY_TRANSPORT_RSA_1_5), |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
$decryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_RSA_1_5, self::$privKey); |
183
|
|
|
$decryptedKey = $encryptedKey->decrypt($decryptor); |
184
|
|
|
|
185
|
|
|
$this->assertEquals(bin2hex($symmetricKey->getMaterial()), bin2hex($decryptedKey)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Test encryption and decryption with RSA OAEP |
191
|
|
|
*/ |
192
|
|
|
public function testOAEPEncryption(): void |
193
|
|
|
{ |
194
|
|
|
$factory = new KeyTransportAlgorithmFactory([]); |
195
|
|
|
$encryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, self::$pubKey); |
196
|
|
|
$symmetricKey = SymmetricKey::generate(16); |
197
|
|
|
$encryptedKey = EncryptedKey::fromKey( |
198
|
|
|
$symmetricKey, |
199
|
|
|
$encryptor, |
200
|
|
|
new EncryptionMethod(C::KEY_TRANSPORT_OAEP), |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
$decryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP, self::$privKey); |
204
|
|
|
$decryptedKey = $encryptedKey->decrypt($decryptor); |
205
|
|
|
|
206
|
|
|
$this->assertEquals(bin2hex($symmetricKey->getMaterial()), bin2hex($decryptedKey)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Test encryption and decryption with RSA OAEP-MGF1P |
212
|
|
|
*/ |
213
|
|
|
public function testOAEMGF1PPEncryption(): void |
214
|
|
|
{ |
215
|
|
|
$factory = new KeyTransportAlgorithmFactory([]); |
216
|
|
|
$encryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, self::$pubKey); |
217
|
|
|
$symmetricKey = SymmetricKey::generate(16); |
218
|
|
|
$encryptedKey = EncryptedKey::fromKey( |
219
|
|
|
$symmetricKey, |
220
|
|
|
$encryptor, |
221
|
|
|
new EncryptionMethod(C::KEY_TRANSPORT_OAEP_MGF1P), |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
$decryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, self::$privKey); |
225
|
|
|
$decryptedKey = $encryptedKey->decrypt($decryptor); |
226
|
|
|
|
227
|
|
|
$this->assertEquals(bin2hex($symmetricKey->getMaterial()), bin2hex($decryptedKey)); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|