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
|
|
|
namespace Jose\Algorithm\KeyEncryption; |
13
|
|
|
|
14
|
|
|
use Assert\Assertion; |
15
|
|
|
use Base64Url\Base64Url; |
16
|
|
|
use Jose\Factory\JWKFactory; |
17
|
|
|
use Jose\Object\JWK; |
18
|
|
|
use Jose\Object\JWKInterface; |
19
|
|
|
use Jose\Util\ConcatKDF; |
20
|
|
|
use Mdanter\Ecc\Crypto\EcDH\EcDH; |
21
|
|
|
use Mdanter\Ecc\EccFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class ECDHES. |
25
|
|
|
*/ |
26
|
|
|
final class ECDHES implements KeyAgreementInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getAgreementKey($encryption_key_length, $algorithm, JWKInterface $recipient_key, array $complete_header = [], array &$additional_header_values = []) |
32
|
|
|
{ |
33
|
|
|
if ($recipient_key->has('d')) { |
34
|
|
|
$this->checkKey($recipient_key, true); |
35
|
|
|
$private_key = $recipient_key; |
36
|
|
|
$public_key = $this->getPublicKey($complete_header); |
37
|
|
|
} else { |
38
|
|
|
$this->checkKey($recipient_key, false); |
39
|
|
|
$public_key = $recipient_key; |
40
|
|
|
switch ($public_key->get('crv')) { |
41
|
|
|
case 'P-256': |
42
|
|
|
case 'P-384': |
43
|
|
|
case 'P-521': |
44
|
|
|
$private_key = JWKFactory::createRandomECPrivateKey($public_key->get('crv')); |
45
|
|
|
$epk = [ |
46
|
|
|
'kty' => $private_key->get('kty'), |
47
|
|
|
'crv' => $private_key->get('crv'), |
48
|
|
|
'x' => $private_key->get('x'), |
49
|
|
|
'y' => $private_key->get('y'), |
50
|
|
|
]; |
51
|
|
|
break; |
52
|
|
|
case 'X25519': |
53
|
|
|
$private_key = JWKFactory::createRandomX25519PrivateKey(); |
54
|
|
|
$epk = [ |
55
|
|
|
'kty' => $private_key->get('kty'), |
56
|
|
|
'crv' => $private_key->get('crv'), |
57
|
|
|
'x' => $private_key->get('x'), |
58
|
|
|
]; |
59
|
|
|
break; |
60
|
|
|
default: |
61
|
|
|
throw new \InvalidArgumentException(sprintf('The curve "%s" is not supported', $public_key->get('crv'))); |
62
|
|
|
} |
63
|
|
|
$additional_header_values = array_merge($additional_header_values, [ |
64
|
|
|
'epk' => $epk, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
Assertion::eq($private_key->get('crv'), $public_key->get('crv'), 'Curves are different'); |
68
|
|
|
|
69
|
|
|
$agreed_key = $this->calculateAgreementKey($private_key, $public_key); |
70
|
|
|
|
71
|
|
|
$apu = array_key_exists('apu', $complete_header) ? $complete_header['apu'] : ''; |
72
|
|
|
$apv = array_key_exists('apv', $complete_header) ? $complete_header['apv'] : ''; |
73
|
|
|
|
74
|
|
|
return ConcatKDF::generate($agreed_key, $algorithm, $encryption_key_length, $apu, $apv); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \Jose\Object\JWKInterface $private_key |
79
|
|
|
* @param \Jose\Object\JWKInterface $public_key |
80
|
|
|
* |
81
|
|
|
* @throws \InvalidArgumentException |
82
|
|
|
* |
83
|
|
|
* @return int|string|void |
84
|
|
|
*/ |
85
|
|
|
public function calculateAgreementKey(JWKInterface $private_key, JWKInterface $public_key) |
86
|
|
|
{ |
87
|
|
|
switch ($public_key->get('crv')) { |
88
|
|
|
case 'P-256': |
89
|
|
|
case 'P-384': |
90
|
|
|
case 'P-521': |
91
|
|
|
$p = $this->getGenerator($private_key); |
92
|
|
|
|
93
|
|
|
$rec_x = $this->convertBase64ToGmp($public_key->get('x')); |
94
|
|
|
$rec_y = $this->convertBase64ToGmp($public_key->get('y')); |
95
|
|
|
$sen_d = $this->convertBase64ToGmp($private_key->get('d')); |
96
|
|
|
|
97
|
|
|
$priv_key = $p->getPrivateKeyFrom($sen_d); |
|
|
|
|
98
|
|
|
$pub_key = $p->getPublicKeyFrom($rec_x, $rec_y); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$ecdh = new EcDH(EccFactory::getAdapter()); |
101
|
|
|
$ecdh->setSenderKey($priv_key); |
102
|
|
|
$ecdh->setRecipientKey($pub_key); |
103
|
|
|
|
104
|
|
|
return $this->convertDecToBin($ecdh->calculateSharedKey()); |
105
|
|
|
case 'X25519': |
106
|
|
|
return curve25519_shared( |
107
|
|
|
Base64Url::decode($private_key->get('d')), |
108
|
|
|
Base64Url::decode($public_key->get('x')) |
109
|
|
|
); |
110
|
|
|
default: |
111
|
|
|
throw new \InvalidArgumentException(sprintf('The curve "%s" is not supported', $public_key->get('crv'))); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function getAlgorithmName() |
119
|
|
|
{ |
120
|
|
|
return 'ECDH-ES'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function getKeyManagementMode() |
127
|
|
|
{ |
128
|
|
|
return self::MODE_AGREEMENT; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array $complete_header |
133
|
|
|
* |
134
|
|
|
* @return \Jose\Object\JWKInterface |
135
|
|
|
*/ |
136
|
|
|
private function getPublicKey(array $complete_header) |
137
|
|
|
{ |
138
|
|
|
Assertion::keyExists($complete_header, 'epk', 'The header parameter "epk" is missing'); |
139
|
|
|
Assertion::isArray($complete_header['epk'], 'The header parameter "epk" is not an array of parameter'); |
140
|
|
|
|
141
|
|
|
$public_key = new JWK($complete_header['epk']); |
142
|
|
|
$this->checkKey($public_key, false); |
143
|
|
|
|
144
|
|
|
return $public_key; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param \Jose\Object\JWKInterface $key |
149
|
|
|
* @param bool $is_private |
150
|
|
|
*/ |
151
|
|
|
private function checkKey(JWKInterface $key, $is_private) |
152
|
|
|
{ |
153
|
|
|
Assertion::true($key->has('x'), 'The key parameter "x" is missing.'); |
154
|
|
|
Assertion::true($key->has('crv'), 'The key parameter "crv" is missing.'); |
155
|
|
|
|
156
|
|
|
switch ($key->get('crv')) { |
157
|
|
|
case 'P-256': |
158
|
|
|
case 'P-384': |
159
|
|
|
case 'P-521': |
160
|
|
|
Assertion::eq($key->get('kty'), 'EC', 'Wrong key type.'); |
161
|
|
|
Assertion::true($key->has('y'), 'The key parameter "y" is missing.'); |
162
|
|
|
break; |
163
|
|
|
case 'X25519': |
164
|
|
|
Assertion::eq($key->get('kty'), 'OKP', 'Wrong key type.'); |
165
|
|
|
break; |
166
|
|
|
default: |
167
|
|
|
throw new \InvalidArgumentException(sprintf('The curve "%s" is not supported', $key->get('crv'))); |
168
|
|
|
} |
169
|
|
|
if (true === $is_private) { |
170
|
|
|
Assertion::true($key->has('d'), 'The key parameter "d" is missing.'); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param JWKInterface $key |
176
|
|
|
* |
177
|
|
|
* @throws \InvalidArgumentException |
178
|
|
|
* |
179
|
|
|
* @return \Mdanter\Ecc\Primitives\GeneratorPoint |
180
|
|
|
*/ |
181
|
|
|
private function getGenerator(JWKInterface $key) |
182
|
|
|
{ |
183
|
|
|
$crv = $key->get('crv'); |
184
|
|
|
|
185
|
|
|
switch ($crv) { |
186
|
|
|
case 'P-256': |
187
|
|
|
return EccFactory::getNistCurves()->generator256(); |
188
|
|
|
case 'P-384': |
189
|
|
|
return EccFactory::getNistCurves()->generator384(); |
190
|
|
|
case 'P-521': |
191
|
|
|
return EccFactory::getNistCurves()->generator521(); |
192
|
|
|
default: |
193
|
|
|
throw new \InvalidArgumentException(sprintf('The curve "%s" is not supported', $crv)); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $value |
199
|
|
|
* |
200
|
|
|
* @return \GMP |
201
|
|
|
*/ |
202
|
|
|
private function convertBase64ToGmp($value) |
203
|
|
|
{ |
204
|
|
|
$value = unpack('H*', Base64Url::decode($value)); |
205
|
|
|
$value = '0x'.$value[1]; |
206
|
|
|
|
207
|
|
|
return gmp_init($value); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param $value |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
private function convertDecToBin($value) |
216
|
|
|
{ |
217
|
|
|
$value = gmp_strval($value); |
218
|
|
|
$adapter = EccFactory::getAdapter(); |
219
|
|
|
|
220
|
|
|
return hex2bin($adapter->decHex($value)); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: