Passed
Pull Request — master (#61)
by
unknown
12:50
created

EncryptedKeyTest::testMarshallingElementOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 36
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 52
rs 9.344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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