1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
10
|
|
|
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory; |
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
|
|
|
|
18
|
|
|
use function dirname; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class \SimpleSAML\XMLSecurity\Test\XML\EncryptedCustomTest |
22
|
|
|
* |
23
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\EncryptableElementTrait |
24
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\EncryptedElementTrait |
25
|
|
|
* @covers \SimpleSAML\XMLSecurity\Test\XML\EncryptedCustom |
26
|
|
|
* |
27
|
|
|
* @package simplesamlphp/xml-security |
28
|
|
|
*/ |
29
|
|
|
class EncryptedCustomTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** @var \DOMElement */ |
32
|
|
|
private DOMElement $signedDocument; |
33
|
|
|
|
34
|
|
|
/** @var PrivateKey */ |
35
|
|
|
protected PrivateKey $privKey; |
36
|
|
|
|
37
|
|
|
/** @var PublicKey */ |
38
|
|
|
protected PublicKey $pubKey; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
*/ |
43
|
|
|
public function setUp(): void |
44
|
|
|
{ |
45
|
|
|
$this->signedDocument = DOMDocumentFactory::fromFile( |
46
|
|
|
dirname(__FILE__, 2) . '/resources/xml/custom_CustomSignableSigned.xml', |
47
|
|
|
)->documentElement; |
48
|
|
|
|
49
|
|
|
$this->privKey = PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::PRIVATE_KEY); |
50
|
|
|
$this->pubKey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test encrypting an object and then decrypting it. |
56
|
|
|
*/ |
57
|
|
|
public function testEncryptAndDecryptSharedSecret(): void |
58
|
|
|
{ |
59
|
|
|
// instantiate |
60
|
|
|
$customSigned = CustomSignable::fromXML($this->signedDocument); |
61
|
|
|
$sharedKey = SymmetricKey::generate(16); |
62
|
|
|
|
63
|
|
|
// encrypt |
64
|
|
|
$factory = new EncryptionAlgorithmFactory(); |
65
|
|
|
$encryptor = $factory->getAlgorithm(C::BLOCK_ENC_AES128, $sharedKey); |
66
|
|
|
$encryptedCustom = new EncryptedCustom($customSigned->encrypt($encryptor)); |
67
|
|
|
|
68
|
|
|
// decrypt |
69
|
|
|
$decryptedCustom = $encryptedCustom->decrypt($encryptor); |
70
|
|
|
|
71
|
|
|
$this->assertEquals($customSigned, $decryptedCustom); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Test encrypting an object with a session key and asymmetric encryption, then decrypting it. |
77
|
|
|
*/ |
78
|
|
|
public function testEncryptAndDecryptSessionKey(): void |
79
|
|
|
{ |
80
|
|
|
// instantiate |
81
|
|
|
$customSigned = CustomSignable::fromXML($this->signedDocument); |
82
|
|
|
|
83
|
|
|
// encrypt |
84
|
|
|
$factory = new KeyTransportAlgorithmFactory(); |
85
|
|
|
$encryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, $this->pubKey); |
86
|
|
|
$encryptedCustom = new EncryptedCustom($customSigned->encrypt($encryptor)); |
87
|
|
|
|
88
|
|
|
// decrypt |
89
|
|
|
$decryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, $this->privKey); |
90
|
|
|
$decryptedCustom = $encryptedCustom->decrypt($decryptor); |
91
|
|
|
|
92
|
|
|
$this->assertEquals($customSigned, $decryptedCustom); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|