Completed
Push — v2.0.x ( 11273c...f488cd )
by Florent
04:53 queued 01:28
created

PBES2_HS_AESKWKeyEncryptionTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 227
rs 10
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
use Base64Url\Base64Url;
13
use Jose\Algorithm\KeyEncryption\PBES2HS256A128KW;
14
use Jose\Algorithm\KeyEncryption\PBES2HS384A192KW;
15
use Jose\Algorithm\KeyEncryption\PBES2HS512A256KW;
16
use Jose\Object\JWK;
17
18
/**
19
 * Class PBES2_HS_AESKWKeyEncryptionTest.
20
 *
21
 * @group PBES2HSAESKW
22
 */
23
class PBES2_HS_AESKWKeyEncryptionTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @see https://tools.ietf.org/html/rfc7517#appendix-C
27
     */
28
    public function testPBES2HS256A128KW()
29
    {
30
        $header = [
31
          'alg' => 'PBES2-HS256+A128KW',
32
          'p2s' => '2WCTcJZ1Rvd_CJuJripQ1w',
33
          'p2c' => 4096,
34
          'enc' => 'A128CBC-HS256',
35
          'cty' => 'jwk+json',
36
        ];
37
        $key = new JWK([
38
            'kty' => 'oct',
39
            '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])),
40
        ]);
41
42
        $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]);
43
44
        $pbes2 = new PBES2HS256A128KW();
45
46
        $wrapped_cek = Base64Url::decode('TrqXOwuNUfDV9VPTNbyGvEJ9JMjefAVn-TR1uIxR9p6hsRQh9Tk7BA');
47
48
        $this->assertEquals($expected_cek, $pbes2->decryptKey($key, $wrapped_cek, $header));
49
    }
50
51
    /**
52
     *
53
     */
54
    public function testPBES2HS256A128KW_Bis()
55
    {
56
        $header = [
57
          'alg' => 'PBES2-HS256+A128KW',
58
          'enc' => 'A128CBC-HS256',
59
          'cty' => 'jwk+json',
60
        ];
61
        $key = new JWK([
62
            'kty' => 'oct',
63
            '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])),
64
        ]);
65
66
        $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]);
67
68
        $pbes2 = new PBES2HS256A128KW();
69
        $encrypted_cek = $pbes2->encryptKey($key, $cek, $header);
70
        $this->assertTrue(isset($header['p2s']));
71
        $this->assertEquals(4096, $header['p2c']);
72
        $this->assertEquals($cek, $pbes2->decryptKey($key, $encrypted_cek, $header));
73
    }
74
75
    /**
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 = new JWK([
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->encryptKey($key, $cek, $header);
94
        $this->assertTrue(isset($header['p2s']));
95
        $this->assertEquals(4096, $header['p2c']);
96
        $this->assertEquals($cek, $pbes2->decryptKey($key, $encrypted_cek, $header));
97
    }
98
99
    /**
100
     *
101
     */
102
    public function testPBES2HS512A256KW()
103
    {
104
        $header = [
105
          'alg' => 'PBES2-HS512+A256KW',
106
          'enc' => 'A256CBC-HS512',
107
          'cty' => 'jwk+json',
108
        ];
109
        $key = new JWK([
110
            'kty' => 'oct',
111
            '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])),
112
        ]);
113
114
        $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]);
115
116
        $pbes2 = new PBES2HS512A256KW();
117
        $encrypted_cek = $pbes2->encryptKey($key, $cek, $header);
118
        $this->assertTrue(isset($header['p2s']));
119
        $this->assertEquals(4096, $header['p2c']);
120
        $this->assertEquals($cek, $pbes2->decryptKey($key, $encrypted_cek, $header));
121
    }
122
123
    /**
124
     * @expectedException \InvalidArgumentException
125
     * @expectedExceptionMessage The key is not valid
126
     */
127
    public function testBadKeyType()
128
    {
129
        $header = [
130
          'alg' => 'PBES2-HS512+A256KW',
131
          'enc' => 'A256CBC-HS512',
132
          'cty' => 'jwk+json',
133
        ];
134
        $key = new JWK([
135
            'kty'   => 'dir',
136
            '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])),
137
        ]);
138
139
        $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]);
140
141
        $pbes2 = new PBES2HS512A256KW();
142
        $pbes2->encryptKey($key, $cek, $header);
143
    }
144
145
    /**
146
     * @expectedException \InvalidArgumentException
147
     * @expectedExceptionMessage The key is not valid
148
     */
149
    public function testInvalidKeyType()
150
    {
151
        $header = [
152
          'alg' => 'PBES2-HS512+A256KW',
153
          'enc' => 'A256CBC-HS512',
154
          'cty' => 'jwk+json',
155
        ];
156
        $key = new JWK([
157
            'kty'   => 'oct',
158
            '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])),
159
        ]);
160
161
        $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]);
162
163
        $pbes2 = new PBES2HS512A256KW();
164
        $pbes2->encryptKey($key, $cek, $header);
165
    }
166
167
    /**
168
     * @expectedException \InvalidArgumentException
169
     * @expectedExceptionMessage The header parameter 'alg' is missing or invalid.
170
     */
171
    public function testAlgorithmParameterIsMissing()
172
    {
173
        $header = [
174
          'enc' => 'A256CBC-HS512',
175
          'cty' => 'jwk+json',
176
        ];
177
        $key = new JWK([
178
            'kty' => 'oct',
179
            '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])),
180
        ]);
181
182
        $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]);
183
184
        $pbes2 = new PBES2HS512A256KW();
185
        $pbes2->encryptKey($key, $cek, $header);
186
    }
187
188
    /**
189
     * @expectedException \InvalidArgumentException
190
     * @expectedExceptionMessage The header is not valid. 'p2s' or 'p2c' parameter is missing or invalid.
191
     */
192
    public function testP2CParameterIsMissing()
193
    {
194
        $header = [
195
            'alg' => 'PBES2-HS256+A128KW',
196
            'p2c' => 4096,
197
            'enc' => 'A128CBC-HS256',
198
            'cty' => 'jwk+json',
199
        ];
200
        $key = new JWK([
201
            'kty' => 'oct',
202
            '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])),
203
        ]);
204
205
        $pbes2 = new PBES2HS256A128KW();
206
207
        $wrapped_cek = Base64Url::decode('TrqXOwuNUfDV9VPTNbyGvEJ9JMjefAVn-TR1uIxR9p6hsRQh9Tk7BA');
208
209
        $pbes2->decryptKey($key, $wrapped_cek, $header);
210
    }
211
212
    /**
213
     * @expectedException \InvalidArgumentException
214
     * @expectedExceptionMessage The header is not valid. 'p2s' or 'p2c' parameter is missing or invalid.
215
     */
216
    public function testP2SParameterIsMissing()
217
    {
218
        $header = [
219
            'alg' => 'PBES2-HS256+A128KW',
220
            'p2s' => '2WCTcJZ1Rvd_CJuJripQ1w',
221
            'enc' => 'A128CBC-HS256',
222
            'cty' => 'jwk+json',
223
        ];
224
        $key = new JWK([
225
            'kty' => 'oct',
226
            '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])),
227
        ]);
228
229
        $pbes2 = new PBES2HS256A128KW();
230
231
        $wrapped_cek = Base64Url::decode('TrqXOwuNUfDV9VPTNbyGvEJ9JMjefAVn-TR1uIxR9p6hsRQh9Tk7BA');
232
233
        $pbes2->decryptKey($key, $wrapped_cek, $header);
234
    }
235
236
    /**
237
     * @param array $data
238
     *
239
     * @return string
240
     */
241
    private function convertArrayToBinString(array $data)
242
    {
243
        foreach ($data as $key => $value) {
244
            $data[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
245
        }
246
247
        return hex2bin(implode('', $data));
248
    }
249
}
250