Failed Conditions
Push — v7 ( e44e34...2109ab )
by Florent
04:36
created

testEncryptionAlgorithms()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Encryption\Tests;
15
16
use Jose\Component\Core\JWAManager;
17
use Jose\Component\Core\JWK;
18
use Jose\Component\Encryption\Algorithm\ContentEncryption\A256GCM;
19
use Jose\Component\Encryption\Algorithm\KeyEncryption\RSA15;
20
use Jose\Component\Encryption\Algorithm\KeyEncryption\RSAOAEP;
21
use Jose\Component\Encryption\Algorithm\KeyEncryption\RSAOAEP256;
22
use Jose\Component\Encryption\Algorithm\KeyEncryptionAlgorithmInterface;
23
use Jose\Component\Encryption\Compression\CompressionMethodsManager;
24
use Jose\Component\Encryption\Compression\Deflate;
25
use Jose\Component\Encryption\Decrypter;
26
use Jose\Component\Encryption\JWE;
27
use Jose\Component\Encryption\JWELoader;
28
use Jose\Component\KeyManagement\JWKFactory;
29
use Jose\Component\Signature\Algorithm\PS256;
30
use Jose\Component\Signature\Algorithm\PS384;
31
use Jose\Component\Signature\Algorithm\PS512;
32
use Jose\Component\Signature\Algorithm\RS256;
33
use Jose\Component\Signature\Algorithm\RS384;
34
use Jose\Component\Signature\Algorithm\RS512;
35
use Jose\Component\Signature\JWS;
36
use Jose\Component\Signature\JWSBuilder;
37
use Jose\Component\Signature\JWSLoader;
38
use Jose\Component\Signature\SignatureAlgorithmInterface;
39
use Jose\Component\Signature\Verifier;
40
41
/**
42
 * final class RSAKeyWithoutAllPrimesTest.
43
 *
44
 * @group RSA2
45
 * @group Unit
46
 */
47
final class RSAKeyWithoutAllPrimesTest extends AbstractEncryptionTest
48
{
49
    /**
50
     * @param SignatureAlgorithmInterface $signature_algorithm
51
     *
52
     * @dataProvider dataSignatureAlgorithms
53
     */
54
    public function testSignatureAlgorithms(SignatureAlgorithmInterface $signature_algorithm)
55
    {
56
        $key = $this->getPrivateKey();
57
58
        $claims = json_encode(['foo' => 'bar']);
59
60
        $algorithmManager = JWAManager::create([$signature_algorithm]);
61
        $builder = new JWSBuilder($algorithmManager);
62
        $jws = $builder
63
            ->withPayload($claims)
64
            ->addSignature($key, ['alg' => $signature_algorithm->name()])
65
            ->build()
66
            ->toCompactJSON(0);
67
68
        $loaded = JWSLoader::load($jws);
69
        $this->assertInstanceOf(JWS::class, $loaded);
70
71
        $verifier = new Verifier($algorithmManager);
72
        $verifier->verifyWithKey($loaded, $key);
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function dataSignatureAlgorithms()
79
    {
80
        return [
81
            [new RS256()],
82
            [new RS384()],
83
            [new RS512()],
84
            [new PS256()],
85
            [new PS384()],
86
            [new PS512()],
87
        ];
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function dataSignatureAlgorithmsWithSimpleKey()
94
    {
95
        return [
96
            [new PS256()],
97
            [new PS384()],
98
            [new PS512()],
99
        ];
100
    }
101
102
    /**
103
     * @param KeyEncryptionAlgorithmInterface $encryption_algorithm
104
     *
105
     * @dataProvider dataEncryptionAlgorithms
106
     */
107
    public function testEncryptionAlgorithms(KeyEncryptionAlgorithmInterface $encryption_algorithm)
108
    {
109
        $key = $this->getPrivateKey();
110
111
        $claims = json_encode(['foo' => 'bar']);
112
113
        $keyEncryptionAlgorithmManager = JWAManager::create([$encryption_algorithm]);
114
        $contentEncryptionAlgorithmManager = JWAManager::create([new A256GCM()]);
115
        $compressionManager = CompressionMethodsManager::create([new Deflate()]);
116
        $jweBuilder = $this->getJWEBuilderFactory()->create([$encryption_algorithm->name()], ['A256GCM'], ['DEF']);
117
        $decrypter = new Decrypter($keyEncryptionAlgorithmManager, $contentEncryptionAlgorithmManager, $compressionManager);
118
119
        $jwt = $jweBuilder
120
            ->withPayload($claims)
121
            ->withSharedProtectedHeaders(['alg' => $encryption_algorithm->name(), 'enc' => 'A256GCM'])
122
            ->addRecipient($key)
123
            ->build()
124
            ->toCompactJSON(0);
125
126
        $loaded = JWELoader::load($jwt);
127
        $this->assertInstanceOf(JWE::class, $loaded);
128
129
        $decrypter->decryptUsingKey($loaded, $key);
130
    }
131
132
    /**
133
     * @param KeyEncryptionAlgorithmInterface $encryption_algorithm
134
     *
135
     * @dataProvider dataEncryptionAlgorithms
136
     */
137
    public function testEncryptionAlgorithmsWithMinimalRsaKey(KeyEncryptionAlgorithmInterface $encryption_algorithm)
138
    {
139
        $key = $this->getMinimalPrivateKey();
140
141
        $claims = json_encode(['foo' => 'bar']);
142
143
        $keyEncryptionAlgorithmManager = JWAManager::create([$encryption_algorithm]);
144
        $contentEncryptionAlgorithmManager = JWAManager::create([new A256GCM()]);
145
        $compressionManager = CompressionMethodsManager::create([new Deflate()]);
146
        $jweBuilder = $this->getJWEBuilderFactory()->create([$encryption_algorithm->name()], ['A256GCM'], ['DEF']);
147
        $decrypter = new Decrypter($keyEncryptionAlgorithmManager, $contentEncryptionAlgorithmManager, $compressionManager);
148
149
        $jwt = $jweBuilder
150
            ->withPayload($claims)
151
            ->withSharedProtectedHeaders(['alg' => $encryption_algorithm->name(), 'enc' => 'A256GCM'])
152
            ->addRecipient($key)
153
            ->build()
154
            ->toCompactJSON(0);
155
156
        $loaded = JWELoader::load($jwt);
157
        $this->assertInstanceOf(JWE::class, $loaded);
158
159
        $decrypter->decryptUsingKey($loaded, $key);
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function dataEncryptionAlgorithms(): array
166
    {
167
        return [
168
            [new RSA15()],
169
            [new RSAOAEP()],
170
            [new RSAOAEP256()],
171
        ];
172
    }
173
174
    /**
175
     * @return array
176
     */
177
    public function dataEncryptionAlgorithmsWithSimpleKey(): array
178
    {
179
        return [
180
            [new RSAOAEP()],
181
            [new RSAOAEP256()],
182
        ];
183
    }
184
185
    /**
186
     * @return JWK
187
     */
188
    private function getPrivateKey(): JWK
189
    {
190
        return JWKFactory::createFromValues(
191
            [
192
                'kty' => 'RSA',
193
                'kid' => 'private',
194
                'n' => '2NRPORHXd7wPU6atHqmSfWgEPvsP8HVUkY2AwQQAc8x1J509X5HFxeSXnQym9eAnZHl0JCPbvHoPH4QHlvITYoh0MSgFm2aOPyqOD-XcNdKWtnNX2JIurUCyVlwSwtlmy2ZbCz8YuUmFO0iacahfK1wbWT5QoY-pU3UxnMzDhlBslZN5uL7nRE8Sh_8BthsrMdYeGIMY55kh-P7xTs3MHzpOKhFSrOhdN6aO3HWYUuMAdoMNB-hJvckb2PbCy0_K1Wm3SBHtXn-cuMIUF00W9AR3amp3u3hLa2rcz29jEFXTr2FxKyLH4SdlnFFMJl2vaXuxM4PXgLN33Kj34PfKgc8ljDJ7oaSI9bKt7gunXOLv_o4XWYDq91cvUkOIDAsvqxzzHPZBt0Hru7roW3btkUOiqR6RWy-Cw272yiSEC5QA93m_vklD1KajoFeWN0BW2lWGlfGieZldvKX0sumk1TZuLhlHPHSKYcpeCfahT-jLr1yAeHql6qRN_a0BiHu-SSSjts6InmF1pAELznZ3Jn9-QXX78LsY3xaqOlYqHbCohxXorlYRi4so6eMGILtXjqHOoISb13Ez4YNOQmV4ygmyABRkE0AQG5KLy5cZB7LZn7zqw869UjXxWrmiOaBeDqOkxww6qiWIEDwPIouRLwOfPFtC4LGlb9LmG9Hlhp8',
195
                'e' => 'AQAB',
196
                'd' => 'PsMls2VAsz3SSepjDg8Tgg1LvVc6w-WSdxc4f6ZC40H5X2AaVcGCN8f1QtZYta8Od_zX62Ydwq6qFftHnx-vEMRirZ_iD5td7VbKDDwCw-mTCnjUorGdpTSm6mx4WcJICPQ1wkmfRHLNh916JxAPjCN7Hxf0iu9kme3AUJzMs-IvrBQmFZ3cn18sBAWCX0358NEDo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SxSycr58rTyu3khHQapGHkS5-2Y_w40GUSfVJ3XP48delYpK-PZP71hn89MJTnnfPOtvJAk1wbEev5wQFTJd-PGOudkGkuEIXryF4TGxRPltl5UeF0CwQ',
199
            ]
200
        );
201
    }
202
203
    /**
204
     * @return JWK
205
     */
206
    private function getMinimalPrivateKey(): JWK
207
    {
208
        return JWKFactory::createFromValues(
209
            [
210
                'd' => 'JSqz6ijkk3dfdSEA_0iMT_1HeIJ1ft4msZ6qw7_1JSCGQAALeZ1yM0QHO3uX-Jr7HC7v1rGVcwsonAhei2qu3rk-w_iCnRL6QkkMNBnDQycwaWpwGsMBFF-UqstOJNggE4AHX-aDnbd4wbKVvdX7ieehPngbPkHcJFdg_iSZCQNoajz6XfEruyIi7_IFXYEGmH_UyEbQkgNtriZysutgYdolUjo9flUlh20HbuV3NwsPjGyDG4dUMpNpdBpSuRHYKLX6h3FjeLhItBmhBfuL7d-G3EXwKlwfNXXYivqY5NQAkFNrRbvFlc_ARIws3zAfykPDIWGWFiPiN3H-hXMgAQ',
211
                'e' => 'AQAB',
212
                'n' => 'gVf-iyhwLn2J2Up4EKjwdLYmk5n24gjGk4oQkCHVcE7j8wkS1iSzcu0ApVcMPLklEp_PWycZE12vL90gPeVjF2IPL_MKFL0b6Wy7A1f4kCDkKv7TDDjt1IIwbS-Jdp-2pG7bPb3tWjJUu6QZBLoXfRtW3cMDkQjXaVGixENORLAZs6qdu2MMKV94jetCiFd0JYCjxGVC0HW2OKnM21B_2R1NubOvMlWA7gypdpvmBYDGpkw4mjV3walWlCZObG7IH84Ovl7wOP8XLzqi2un4e6fNzy3rdp4OUSPYItF4ZX5qThWYY2R47Z5sbrZxHjNeDECKUeio0KPQNrgr6FSKSw',
213
                'kty' => 'RSA',
214
                'kid' => 'test-key',
215
            ]
216
        );
217
    }
218
}
219