Passed
Branch master (c86cc6)
by Tim
11:28
created

EncryptedKeyTest::testMarshallingElementOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

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

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