Passed
Push — master ( feb95e...9f38d8 )
by Tim
01:59
created

EncryptedCustomTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 105
rs 10
wmc 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML;
6
7
use DOMElement;
8
use PHPUnit\Framework\Attributes\CoversClass;
9
use PHPUnit\Framework\TestCase;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
12
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory;
13
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Key\PrivateKey;
16
use SimpleSAML\XMLSecurity\Key\PublicKey;
17
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
18
use SimpleSAML\XMLSecurity\Test\XML\EncryptedCustom;
19
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
20
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
21
use SimpleSAML\XMLSecurity\XML\EncryptedElementTrait;
22
23
use function dirname;
24
25
/**
26
 * Class \SimpleSAML\XMLSecurity\Test\XML\EncryptedCustomTest
27
 *
28
 * @package simplesamlphp/xml-security
29
 */
30
#[CoversClass(EncryptableElementTrait::class)]
31
#[CoversClass(EncryptedElementTrait::class)]
32
#[CoversClass(EncryptedCustom::class)]
33
class EncryptedCustomTest extends TestCase
34
{
35
    /** @var \DOMElement */
36
    private DOMElement $signableDocument;
37
38
    /** @var PrivateKey */
39
    protected PrivateKey $privKey;
40
41
    /** @var PublicKey */
42
    protected PublicKey $pubKey;
43
44
45
    /**
46
     */
47
    public function setUp(): void
48
    {
49
        $this->signableDocument = DOMDocumentFactory::fromFile(
50
            dirname(__FILE__, 2) . '/resources/xml/custom_CustomSignable.xml',
51
        )->documentElement;
52
53
        $this->privKey = PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::PRIVATE_KEY);
54
        $this->pubKey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY);
55
    }
56
57
58
    /**
59
     * Test encrypting an object and then decrypting it.
60
     */
61
    public function testEncryptAndDecryptSharedSecret(): void
62
    {
63
        // instantiate
64
        $customSigned = CustomSignable::fromXML($this->signableDocument);
65
        $sharedKey = SymmetricKey::generate(16);
66
67
        // encrypt
68
        $factory = new EncryptionAlgorithmFactory();
69
        $encryptor = $factory->getAlgorithm(C::BLOCK_ENC_AES128, $sharedKey);
70
        $encryptedCustom = new EncryptedCustom($customSigned->encrypt($encryptor));
71
72
        // decrypt
73
        $decryptedCustom = $encryptedCustom->decrypt($encryptor);
74
75
        $this->assertEquals($customSigned, $decryptedCustom);
76
    }
77
78
79
    /**
80
     * Test encrypting an object with a session key and asymmetric encryption, then decrypting it.
81
     */
82
    public function testEncryptAndDecryptSessionKey(): void
83
    {
84
        // instantiate
85
        $customSigned = CustomSignable::fromXML($this->signableDocument);
86
87
        // encrypt
88
        $factory = new KeyTransportAlgorithmFactory();
89
        $encryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, $this->pubKey);
90
        $encryptedCustom = new EncryptedCustom($customSigned->encrypt($encryptor));
91
92
        // decrypt
93
        $decryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, $this->privKey);
94
        $decryptedCustom = $encryptedCustom->decrypt($decryptor);
95
96
        $this->assertEquals($customSigned, $decryptedCustom);
97
    }
98
99
100
    /**
101
     * Test that a signature isn't mangled after encrypting/decrypting a signed object.
102
     */
103
    public function testSignatureVerifiesAfterEncryptionAndDecryption(): void
104
    {
105
        // instantiate
106
        $customSigned = CustomSignable::fromXML($this->signableDocument);
107
108
        // sign
109
        $privateKey = PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::SELFSIGNED_PRIVATE_KEY);
110
        $signer = (new SignatureAlgorithmFactory())->getAlgorithm(
111
            C::SIG_RSA_SHA256,
112
            $privateKey,
113
        );
114
        $customSigned->sign($signer);
115
        $customSigned = CustomSignable::fromXML($customSigned->toXML());
116
117
        // encrypt
118
        $factory = new KeyTransportAlgorithmFactory();
119
        $encryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, $this->pubKey);
120
        $encryptedCustom = new EncryptedCustom($customSigned->encrypt($encryptor));
121
122
        // decrypt
123
        $decryptor = $factory->getAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P, $this->privKey);
124
        $decryptedCustom = $encryptedCustom->decrypt($decryptor);
125
126
        // verify signature
127
        $publicKey = PEMCertificatesMock::getPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY);
128
        $verifier = (new SignatureAlgorithmFactory())->getAlgorithm(
129
            $decryptedCustom->getSignature()->getSignedInfo()->getSignatureMethod()->getAlgorithm(),
130
            $publicKey,
131
        );
132
133
        $verified = $decryptedCustom->verify($verifier);
134
        $this->assertInstanceOf(CustomSignable::class, $verified);
135
    }
136
}
137