Failed Conditions
Push — v7 ( 334279...19cd68 )
by Florent
01:45
created

PBES2_HS_AESKWKeyEncryptionTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 218
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 6
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 Base64Url\Base64Url;
17
use Jose\Component\Core\JWK;
18
use Jose\Component\Encryption\Algorithm\KeyEncryption\PBES2HS256A128KW;
19
use Jose\Component\Encryption\Algorithm\KeyEncryption\PBES2HS384A192KW;
20
use Jose\Component\Encryption\Algorithm\KeyEncryption\PBES2HS512A256KW;
21
use PHPUnit\Framework\TestCase;
22
23
/**
24
 * final class PBES2_HS_AESKWKeyEncryptionTest.
25
 *
26
 * @group PBES2HSAESKW
27
 * @group Unit
28
 */
29
final class PBES2_HS_AESKWKeyEncryptionTest extends TestCase
30
{
31
    /**
32
     * @see https://tools.ietf.org/html/rfc7517#appendix-C
33
     */
34
    public function testPBES2HS256A128KW()
35
    {
36
        $header = [
37
          'alg' => 'PBES2-HS256+A128KW',
38
          'p2s' => '2WCTcJZ1Rvd_CJuJripQ1w',
39
          'p2c' => 4096,
40
          'enc' => 'A128CBC-HS256',
41
          'cty' => 'jwk+json',
42
        ];
43
        $key = JWK::create([
44
            'kty' => 'oct',
45
            'k' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
46
        ]);
47
48
        $expected_cek = $this->convertArrayToBinString([111, 27, 25, 52, 66, 29, 20, 78, 92, 176, 56, 240, 65, 208, 82, 112, 161, 131, 36, 55, 202, 236, 185, 172, 129, 23, 153, 194, 195, 48, 253, 182]);
49
50
        $pbes2 = new PBES2HS256A128KW();
51
52
        $wrapped_cek = Base64Url::decode('TrqXOwuNUfDV9VPTNbyGvEJ9JMjefAVn-TR1uIxR9p6hsRQh9Tk7BA');
53
54
        $this->assertEquals($expected_cek, $pbes2->unwrapKey($key, $wrapped_cek, $header));
55
    }
56
57
    public function testPBES2HS256A128KW_Bis()
58
    {
59
        $header = [
60
          'alg' => 'PBES2-HS256+A128KW',
61
          'enc' => 'A128CBC-HS256',
62
          'cty' => 'jwk+json',
63
        ];
64
        $key = JWK::create([
65
            'kty' => 'oct',
66
            'k' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
67
        ]);
68
69
        $cek = $this->convertArrayToBinString([111, 27, 25, 52, 66, 29, 20, 78, 92, 176, 56, 240, 65, 208, 82, 112, 161, 131, 36, 55, 202, 236, 185, 172, 129, 23, 153, 194, 195, 48, 253, 182]);
70
71
        $pbes2 = new PBES2HS256A128KW();
72
        $encrypted_cek = $pbes2->wrapKey($key, $cek, $header, $header);
73
        $this->assertTrue(isset($header['p2s']));
74
        $this->assertEquals(4096, $header['p2c']);
75
        $this->assertEquals($cek, $pbes2->unwrapKey($key, $encrypted_cek, $header));
76
    }
77
78
    public function testPBES2HS384A192KW()
79
    {
80
        $header = [
81
          'alg' => 'PBES2-HS384+A192KW',
82
          'enc' => 'A192CBC-HS384',
83
          'cty' => 'jwk+json',
84
        ];
85
        $key = JWK::create([
86
            'kty' => 'oct',
87
            'k' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
88
        ]);
89
90
        $cek = $this->convertArrayToBinString([111, 27, 25, 52, 66, 29, 20, 78, 92, 176, 56, 240, 65, 208, 82, 112, 161, 131, 36, 55, 202, 236, 185, 172, 129, 23, 153, 194, 195, 48, 253, 182]);
91
92
        $pbes2 = new PBES2HS384A192KW();
93
        $encrypted_cek = $pbes2->wrapKey($key, $cek, $header, $header);
94
        $this->assertTrue(isset($header['p2s']));
95
        $this->assertEquals(4096, $header['p2c']);
96
        $this->assertEquals($cek, $pbes2->unwrapKey($key, $encrypted_cek, $header));
97
    }
98
99
    public function testPBES2HS512A256KW()
100
    {
101
        $header = [
102
          'alg' => 'PBES2-HS512+A256KW',
103
          'enc' => 'A256CBC-HS512',
104
          'cty' => 'jwk+json',
105
        ];
106
        $key = JWK::create([
107
            'kty' => 'oct',
108
            'k' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
109
        ]);
110
111
        $cek = $this->convertArrayToBinString([111, 27, 25, 52, 66, 29, 20, 78, 92, 176, 56, 240, 65, 208, 82, 112, 161, 131, 36, 55, 202, 236, 185, 172, 129, 23, 153, 194, 195, 48, 253, 182]);
112
113
        $pbes2 = new PBES2HS512A256KW();
114
        $encrypted_cek = $pbes2->wrapKey($key, $cek, $header, $header);
115
        $this->assertTrue(isset($header['p2s']));
116
        $this->assertEquals(4096, $header['p2c']);
117
        $this->assertEquals($cek, $pbes2->unwrapKey($key, $encrypted_cek, $header));
118
    }
119
120
    /**
121
     * @expectedException \InvalidArgumentException
122
     * @expectedExceptionMessage Wrong key type.
123
     */
124
    public function testBadKeyType()
125
    {
126
        $header = [
127
          'alg' => 'PBES2-HS512+A256KW',
128
          'enc' => 'A256CBC-HS512',
129
          'cty' => 'jwk+json',
130
        ];
131
        $key = JWK::create([
132
            'kty' => 'dir',
133
            'dir' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
134
        ]);
135
136
        $cek = $this->convertArrayToBinString([111, 27, 25, 52, 66, 29, 20, 78, 92, 176, 56, 240, 65, 208, 82, 112, 161, 131, 36, 55, 202, 236, 185, 172, 129, 23, 153, 194, 195, 48, 253, 182]);
137
138
        $pbes2 = new PBES2HS512A256KW();
139
        $pbes2->wrapKey($key, $cek, $header, $header);
140
    }
141
142
    /**
143
     * @expectedException \InvalidArgumentException
144
     * @expectedExceptionMessage The key parameter "k" is missing.
145
     */
146
    public function testInvalidKeyType()
147
    {
148
        $header = [
149
          'alg' => 'PBES2-HS512+A256KW',
150
          'enc' => 'A256CBC-HS512',
151
          'cty' => 'jwk+json',
152
        ];
153
        $key = JWK::create([
154
            'kty' => 'oct',
155
            'dir' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
156
        ]);
157
158
        $cek = $this->convertArrayToBinString([111, 27, 25, 52, 66, 29, 20, 78, 92, 176, 56, 240, 65, 208, 82, 112, 161, 131, 36, 55, 202, 236, 185, 172, 129, 23, 153, 194, 195, 48, 253, 182]);
159
160
        $pbes2 = new PBES2HS512A256KW();
161
        $pbes2->wrapKey($key, $cek, $header, $header);
162
    }
163
164
    /**
165
     * @expectedException \InvalidArgumentException
166
     * @expectedExceptionMessage The header parameter "alg" is missing.
167
     */
168
    public function testAlgorithmParameterIsMissing()
169
    {
170
        $header = [
171
          'enc' => 'A256CBC-HS512',
172
          'cty' => 'jwk+json',
173
        ];
174
        $key = JWK::create([
175
            'kty' => 'oct',
176
            'k' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
177
        ]);
178
179
        $cek = $this->convertArrayToBinString([111, 27, 25, 52, 66, 29, 20, 78, 92, 176, 56, 240, 65, 208, 82, 112, 161, 131, 36, 55, 202, 236, 185, 172, 129, 23, 153, 194, 195, 48, 253, 182]);
180
181
        $pbes2 = new PBES2HS512A256KW();
182
        $pbes2->wrapKey($key, $cek, $header, $header);
183
    }
184
185
    /**
186
     * @expectedException \InvalidArgumentException
187
     * @expectedExceptionMessage The header parameter "p2s" is missing.
188
     */
189
    public function testP2CParameterIsMissing()
190
    {
191
        $header = [
192
            'alg' => 'PBES2-HS256+A128KW',
193
            'p2c' => 4096,
194
            'enc' => 'A128CBC-HS256',
195
            'cty' => 'jwk+json',
196
        ];
197
        $key = JWK::create([
198
            'kty' => 'oct',
199
            'k' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
200
        ]);
201
202
        $pbes2 = new PBES2HS256A128KW();
203
204
        $wrapped_cek = Base64Url::decode('TrqXOwuNUfDV9VPTNbyGvEJ9JMjefAVn-TR1uIxR9p6hsRQh9Tk7BA');
205
206
        $pbes2->unwrapKey($key, $wrapped_cek, $header);
207
    }
208
209
    /**
210
     * @expectedException \InvalidArgumentException
211
     * @expectedExceptionMessage The header parameter "p2c" is missing.
212
     */
213
    public function testP2SParameterIsMissing()
214
    {
215
        $header = [
216
            'alg' => 'PBES2-HS256+A128KW',
217
            'p2s' => '2WCTcJZ1Rvd_CJuJripQ1w',
218
            'enc' => 'A128CBC-HS256',
219
            'cty' => 'jwk+json',
220
        ];
221
        $key = JWK::create([
222
            'kty' => 'oct',
223
            'k' => Base64Url::encode($this->convertArrayToBinString([84, 104, 117, 115, 32, 102, 114, 111, 109, 32, 109, 121, 32, 108, 105, 112, 115, 44, 32, 98, 121, 32, 121, 111, 117, 114, 115, 44, 32, 109, 121, 32, 115, 105, 110, 32, 105, 115, 32, 112, 117, 114, 103, 101, 100, 46])),
224
        ]);
225
226
        $pbes2 = new PBES2HS256A128KW();
227
228
        $wrapped_cek = Base64Url::decode('TrqXOwuNUfDV9VPTNbyGvEJ9JMjefAVn-TR1uIxR9p6hsRQh9Tk7BA');
229
230
        $pbes2->unwrapKey($key, $wrapped_cek, $header);
231
    }
232
233
    /**
234
     * @param array $data
235
     *
236
     * @return string
237
     */
238
    private function convertArrayToBinString(array $data)
239
    {
240
        foreach ($data as $key => $value) {
241
            $data[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
242
        }
243
244
        return hex2bin(implode('', $data));
245
    }
246
}
247