|
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\A128KW; |
|
14
|
|
|
use Jose\Algorithm\KeyEncryption\A192KW; |
|
15
|
|
|
use Jose\Algorithm\KeyEncryption\A256KW; |
|
16
|
|
|
use Jose\Object\JWK; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @group AESKW |
|
20
|
|
|
*/ |
|
21
|
|
|
class AESKWKeyEncryptionTest extends \PHPUnit_Framework_TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testA128KW() |
|
27
|
|
|
{ |
|
28
|
|
|
$header = []; |
|
29
|
|
|
$key = new JWK([ |
|
30
|
|
|
'kty' => 'oct', |
|
31
|
|
|
'k' => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F')), |
|
32
|
|
|
]); |
|
33
|
|
|
|
|
34
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
35
|
|
|
|
|
36
|
|
|
$aeskw = new A128KW(); |
|
37
|
|
|
|
|
38
|
|
|
$wrapped_cek = $aeskw->wrapKey($key, $cek, $header); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertEquals($wrapped_cek, hex2bin('11826840774D993FF9C2FA02CCA3CEA0E93B1E1CF96361F93EA6DC2F345194E7B30F964C79F9E61D')); |
|
41
|
|
|
$this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @expectedException \InvalidArgumentException |
|
46
|
|
|
* @expectedExceptionMessage The key is not valid |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testBadKey() |
|
49
|
|
|
{ |
|
50
|
|
|
$header = []; |
|
51
|
|
|
$key = new JWK([ |
|
52
|
|
|
'kty' => 'EC', |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
56
|
|
|
|
|
57
|
|
|
$aeskw = new A128KW(); |
|
58
|
|
|
|
|
59
|
|
|
$aeskw->wrapKey($key, $cek, $header); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testA192KW() |
|
66
|
|
|
{ |
|
67
|
|
|
$header = []; |
|
68
|
|
|
$key = new JWK([ |
|
69
|
|
|
'kty' => 'oct', |
|
70
|
|
|
'k' => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F1011121314151617')), |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
74
|
|
|
|
|
75
|
|
|
$aeskw = new A192KW(); |
|
76
|
|
|
|
|
77
|
|
|
$wrapped_cek = $aeskw->wrapKey($key, $cek, $header); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertEquals($wrapped_cek, hex2bin('08861E000AABFA4479C7191F9DC51CCA37C50F16CC14441C6EA4980CFCE0F41D9285758C6F74AC6D')); |
|
80
|
|
|
$this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* |
|
85
|
|
|
*/ |
|
86
|
|
|
public function testA256KW() |
|
87
|
|
|
{ |
|
88
|
|
|
$header = []; |
|
89
|
|
|
$key = new JWK([ |
|
90
|
|
|
'kty' => 'oct', |
|
91
|
|
|
'k' => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F')), |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
95
|
|
|
|
|
96
|
|
|
$aeskw = new A256KW(); |
|
97
|
|
|
|
|
98
|
|
|
$wrapped_cek = $aeskw->wrapKey($key, $cek, $header); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals($wrapped_cek, hex2bin('28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21')); |
|
101
|
|
|
$this->assertEquals($cek, $aeskw->unwrapKey($key, $wrapped_cek, $header)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @expectedException \InvalidArgumentException |
|
106
|
|
|
* @expectedExceptionMessage The key size is not valid |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testBadKeySize() |
|
109
|
|
|
{ |
|
110
|
|
|
$header = []; |
|
111
|
|
|
$key = new JWK([ |
|
112
|
|
|
'kty' => 'oct', |
|
113
|
|
|
'k' => Base64Url::encode(hex2bin('000102030405060708090A0B0C0D0E0F')), |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
$cek = hex2bin('00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F'); |
|
117
|
|
|
|
|
118
|
|
|
$aeskw = new A256KW(); |
|
119
|
|
|
|
|
120
|
|
|
$wrapped_cek = $aeskw->wrapKey($key, $cek, $header); |
|
121
|
|
|
|
|
122
|
|
|
$aeskw->unwrapKey($key, $wrapped_cek, $header); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|