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