Passed
Push — master ( 61dee4...131a2a )
by Tim
02:09
created

EncryptedCustomTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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\CustomSigned;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\Test\XML\CustomSigned was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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