|
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\ECDHES; |
|
14
|
|
|
use Jose\Algorithm\KeyEncryption\ECDHESA128KW; |
|
15
|
|
|
use Jose\Algorithm\KeyEncryption\ECDHESA192KW; |
|
16
|
|
|
use Jose\Algorithm\KeyEncryption\ECDHESA256KW; |
|
17
|
|
|
use Jose\Object\JWK; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ECDHESKeyAgreementTest. |
|
21
|
|
|
* |
|
22
|
|
|
* @group ECDHES |
|
23
|
|
|
* @group Unit |
|
24
|
|
|
*/ |
|
25
|
|
|
class ECDHESKeyAgreementTest extends \PHPUnit_Framework_TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @see https://tools.ietf.org/html/rfc7518#appendix-C |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testGetAgreementKey() |
|
31
|
|
|
{ |
|
32
|
|
|
$receiver = new JWK([ |
|
33
|
|
|
'kty' => 'EC', |
|
34
|
|
|
'crv' => 'P-256', |
|
35
|
|
|
'x' => 'weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ', |
|
36
|
|
|
'y' => 'e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck', |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
$header = [ |
|
40
|
|
|
'enc' => 'A128GCM', |
|
41
|
|
|
'apu' => 'QWxpY2U', |
|
42
|
|
|
'apv' => 'Qm9i', |
|
43
|
|
|
]; |
|
44
|
|
|
$ecdh_es = new ECDHES(); |
|
45
|
|
|
$additional_header_values = []; |
|
46
|
|
|
|
|
47
|
|
|
$ecdh_es->getAgreementKey(128, 'A128GCM', $receiver, $header, $additional_header_values); |
|
48
|
|
|
$this->assertTrue(array_key_exists('epk', $additional_header_values)); |
|
49
|
|
|
$this->assertTrue(array_key_exists('kty', $additional_header_values['epk'])); |
|
50
|
|
|
$this->assertTrue(array_key_exists('crv', $additional_header_values['epk'])); |
|
51
|
|
|
$this->assertTrue(array_key_exists('x', $additional_header_values['epk'])); |
|
52
|
|
|
$this->assertTrue(array_key_exists('y', $additional_header_values['epk'])); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testGetAgreementKeyWithA128KeyWrap() |
|
56
|
|
|
{ |
|
57
|
|
|
$header = ['enc' => 'A128GCM']; |
|
58
|
|
|
|
|
59
|
|
|
$public = new JWK([ |
|
60
|
|
|
'kty' => 'EC', |
|
61
|
|
|
'crv' => 'P-256', |
|
62
|
|
|
'x' => 'weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ', |
|
63
|
|
|
'y' => 'e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck', |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
$private = new JWK([ |
|
67
|
|
|
'kty' => 'EC', |
|
68
|
|
|
'crv' => 'P-256', |
|
69
|
|
|
'x' => 'weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ', |
|
70
|
|
|
'y' => 'e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck', |
|
71
|
|
|
'd' => 'VEmDZpDXXK8p8N0Cndsxs924q6nS1RXFASRl6BfUqdw', |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
|
|
$cek = [4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106, 206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156, 44, 207]; |
|
75
|
|
|
foreach ($cek as $key => $value) { |
|
76
|
|
|
$cek[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT); |
|
77
|
|
|
} |
|
78
|
|
|
$cek = hex2bin(implode('', $cek)); |
|
79
|
|
|
|
|
80
|
|
|
$ecdh_es = new ECDHESA128KW(); |
|
81
|
|
|
$encrypted_cek = $ecdh_es->wrapAgreementKey($public, $cek, 128, $header, $header); |
|
82
|
|
|
$this->assertTrue(array_key_exists('epk', $header)); |
|
83
|
|
|
$this->assertTrue(array_key_exists('crv', $header['epk'])); |
|
84
|
|
|
$this->assertTrue(array_key_exists('kty', $header['epk'])); |
|
85
|
|
|
$this->assertTrue(array_key_exists('x', $header['epk'])); |
|
86
|
|
|
$this->assertTrue(array_key_exists('y', $header['epk'])); |
|
87
|
|
|
$this->assertEquals('P-256', $header['epk']['crv']); |
|
88
|
|
|
$this->assertEquals('EC', $header['epk']['kty']); |
|
89
|
|
|
$this->assertEquals($cek, $ecdh_es->unwrapAgreementKey($private, $encrypted_cek, 128, $header)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testGetAgreementKeyWithA192KeyWrap() |
|
93
|
|
|
{ |
|
94
|
|
|
$header = ['enc' => 'A192GCM']; |
|
95
|
|
|
|
|
96
|
|
|
$public = new JWK([ |
|
97
|
|
|
'kty' => 'EC', |
|
98
|
|
|
'crv' => 'P-256', |
|
99
|
|
|
'x' => 'weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ', |
|
100
|
|
|
'y' => 'e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck', |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
$private = new JWK([ |
|
104
|
|
|
'kty' => 'EC', |
|
105
|
|
|
'crv' => 'P-256', |
|
106
|
|
|
'x' => 'weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ', |
|
107
|
|
|
'y' => 'e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck', |
|
108
|
|
|
'd' => 'VEmDZpDXXK8p8N0Cndsxs924q6nS1RXFASRl6BfUqdw', |
|
109
|
|
|
]); |
|
110
|
|
|
|
|
111
|
|
|
$cek = [4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106, 206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156, 44, 207]; |
|
112
|
|
|
foreach ($cek as $key => $value) { |
|
113
|
|
|
$cek[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT); |
|
114
|
|
|
} |
|
115
|
|
|
$cek = hex2bin(implode('', $cek)); |
|
116
|
|
|
|
|
117
|
|
|
$ecdh_es = new ECDHESA192KW(); |
|
118
|
|
|
$encrypted_cek = $ecdh_es->wrapAgreementKey($public, $cek, 192, $header, $header); |
|
119
|
|
|
$this->assertTrue(array_key_exists('epk', $header)); |
|
120
|
|
|
$this->assertTrue(array_key_exists('crv', $header['epk'])); |
|
121
|
|
|
$this->assertTrue(array_key_exists('kty', $header['epk'])); |
|
122
|
|
|
$this->assertTrue(array_key_exists('x', $header['epk'])); |
|
123
|
|
|
$this->assertTrue(array_key_exists('y', $header['epk'])); |
|
124
|
|
|
$this->assertEquals('P-256', $header['epk']['crv']); |
|
125
|
|
|
$this->assertEquals('EC', $header['epk']['kty']); |
|
126
|
|
|
$this->assertEquals($cek, $ecdh_es->unwrapAgreementKey($private, $encrypted_cek, 192, $header)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testGetAgreementKeyWithA256KeyWrap() |
|
130
|
|
|
{ |
|
131
|
|
|
$header = ['enc' => 'A256GCM']; |
|
132
|
|
|
|
|
133
|
|
|
$public = new JWK([ |
|
134
|
|
|
'kty' => 'EC', |
|
135
|
|
|
'crv' => 'P-256', |
|
136
|
|
|
'x' => 'weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ', |
|
137
|
|
|
'y' => 'e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck', |
|
138
|
|
|
]); |
|
139
|
|
|
|
|
140
|
|
|
$private = new JWK([ |
|
141
|
|
|
'kty' => 'EC', |
|
142
|
|
|
'crv' => 'P-256', |
|
143
|
|
|
'x' => 'weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ', |
|
144
|
|
|
'y' => 'e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck', |
|
145
|
|
|
'd' => 'VEmDZpDXXK8p8N0Cndsxs924q6nS1RXFASRl6BfUqdw', |
|
146
|
|
|
]); |
|
147
|
|
|
|
|
148
|
|
|
$cek = [4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106, 206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156, 44, 207]; |
|
149
|
|
|
foreach ($cek as $key => $value) { |
|
150
|
|
|
$cek[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT); |
|
151
|
|
|
} |
|
152
|
|
|
$cek = hex2bin(implode('', $cek)); |
|
153
|
|
|
|
|
154
|
|
|
$ecdh_es = new ECDHESA256KW(); |
|
155
|
|
|
$encrypted_cek = $ecdh_es->wrapAgreementKey($public, $cek, 256, $header, $header); |
|
156
|
|
|
$this->assertTrue(array_key_exists('epk', $header)); |
|
157
|
|
|
$this->assertTrue(array_key_exists('crv', $header['epk'])); |
|
158
|
|
|
$this->assertTrue(array_key_exists('kty', $header['epk'])); |
|
159
|
|
|
$this->assertTrue(array_key_exists('x', $header['epk'])); |
|
160
|
|
|
$this->assertTrue(array_key_exists('y', $header['epk'])); |
|
161
|
|
|
$this->assertEquals('P-256', $header['epk']['crv']); |
|
162
|
|
|
$this->assertEquals('EC', $header['epk']['kty']); |
|
163
|
|
|
$this->assertEquals($cek, $ecdh_es->unwrapAgreementKey($private, $encrypted_cek, 256, $header)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @expectedException \InvalidArgumentException |
|
168
|
|
|
* @expectedExceptionMessage The header parameter "epk" is missing |
|
169
|
|
|
*/ |
|
170
|
|
|
public function testEPKParameterAreMissing() |
|
171
|
|
|
{ |
|
172
|
|
|
$sender = new JWK([ |
|
173
|
|
|
'kty' => 'EC', |
|
174
|
|
|
'crv' => 'P-256', |
|
175
|
|
|
'x' => 'gI0GAILBdu7T53akrFmMyGcsF3n5dO7MmwNBHKW5SV0', |
|
176
|
|
|
'y' => 'SLW_xSffzlPWrHEVI30DHM_4egVwt3NQqeUD7nMFpps', |
|
177
|
|
|
'd' => '0_NxaRPUMQoAJt50Gz8YiTr8gRTwyEaCumd-MToTmIo', |
|
178
|
|
|
]); |
|
179
|
|
|
|
|
180
|
|
|
$ecdh_es = new ECDHES(); |
|
181
|
|
|
$ecdh_es->getAgreementKey(256, 'A128GCM', $sender); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @expectedException \InvalidArgumentException |
|
186
|
|
|
* @expectedExceptionMessage The header parameter "epk" is not an array of parameter |
|
187
|
|
|
*/ |
|
188
|
|
|
public function testBadEPKParameter() |
|
189
|
|
|
{ |
|
190
|
|
|
$header = ['epk' => 'foo']; |
|
191
|
|
|
$sender = new JWK([ |
|
192
|
|
|
'kty' => 'EC', |
|
193
|
|
|
'crv' => 'P-256', |
|
194
|
|
|
'x' => 'gI0GAILBdu7T53akrFmMyGcsF3n5dO7MmwNBHKW5SV0', |
|
195
|
|
|
'y' => 'SLW_xSffzlPWrHEVI30DHM_4egVwt3NQqeUD7nMFpps', |
|
196
|
|
|
'd' => '0_NxaRPUMQoAJt50Gz8YiTr8gRTwyEaCumd-MToTmIo', |
|
197
|
|
|
]); |
|
198
|
|
|
|
|
199
|
|
|
$ecdh_es = new ECDHES(); |
|
200
|
|
|
$ecdh_es->getAgreementKey(256, 'A128GCM', $sender, $header); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @expectedException \InvalidArgumentException |
|
205
|
|
|
* @expectedExceptionMessage Wrong key type. |
|
206
|
|
|
*/ |
|
207
|
|
|
public function testNotAnECKey() |
|
208
|
|
|
{ |
|
209
|
|
|
$receiver = new JWK([ |
|
210
|
|
|
'kty' => 'dir', |
|
211
|
|
|
'dir' => Base64Url::encode('ABCD'), |
|
212
|
|
|
]); |
|
213
|
|
|
|
|
214
|
|
|
$ecdh_es = new ECDHES(); |
|
215
|
|
|
$ecdh_es->getAgreementKey(256, 'A128GCM', $receiver); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @expectedException \InvalidArgumentException |
|
220
|
|
|
* @expectedExceptionMessage The key parameter "x" is missing. |
|
221
|
|
|
*/ |
|
222
|
|
|
public function testECKeyHasMissingParameters() |
|
223
|
|
|
{ |
|
224
|
|
|
$receiver = new JWK([ |
|
225
|
|
|
'kty' => 'EC', |
|
226
|
|
|
'dir' => Base64Url::encode('ABCD'), |
|
227
|
|
|
]); |
|
228
|
|
|
|
|
229
|
|
|
$ecdh_es = new ECDHES(); |
|
230
|
|
|
$ecdh_es->getAgreementKey(256, 'A128GCM', $receiver); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @expectedException \InvalidArgumentException |
|
235
|
|
|
* @expectedExceptionMessage The curve "P-192" is not supported |
|
236
|
|
|
*/ |
|
237
|
|
|
public function testUnsupportedCurve() |
|
238
|
|
|
{ |
|
239
|
|
|
$header = [ |
|
240
|
|
|
'enc' => 'A128GCM', |
|
241
|
|
|
'apu' => 'QWxpY2U', |
|
242
|
|
|
'apv' => 'Qm9i', |
|
243
|
|
|
]; |
|
244
|
|
|
|
|
245
|
|
|
$receiver = new JWK([ |
|
246
|
|
|
'kty' => 'EC', |
|
247
|
|
|
'crv' => 'P-192', |
|
248
|
|
|
'x' => 'm2Jmp98NRH83ramvp0VVIQJXK56ZEwuM', |
|
249
|
|
|
'y' => '84lz6hQtPJe9WFPPgEyOUwh3tuW2kOS_', |
|
250
|
|
|
]); |
|
251
|
|
|
|
|
252
|
|
|
$ecdh_es = new ECDHES(); |
|
253
|
|
|
$ecdh_es->getAgreementKey(256, 'A128GCM', $receiver, $header); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|