|
16 | 16 | use Base64Url\Base64Url;
|
17 | 17 | use Brick\Math\BigInteger;
|
18 | 18 | use Jose\Component\Core\JWK;
|
19 |
| -use Jose\Component\Core\Util\Ecc\Curve; |
20 | 19 | use Jose\Component\Core\Util\Ecc\NistCurve;
|
21 | 20 | use Jose\Component\Core\Util\Ecc\PrivateKey;
|
22 | 21 | use Jose\Component\Core\Util\ECKey;
|
@@ -96,7 +95,7 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
|
96 | 95 | $localJwk = new JWK([
|
97 | 96 | 'kty' => 'EC',
|
98 | 97 | 'crv' => 'P-256',
|
99 |
| - 'd' => $localPrivateKeyObject->getSecret()->getX(), // @phpstan-ignore-line |
| 98 | + 'd' => Base64Url::encode($localPrivateKeyObject->getSecret()->toBytes()), |
100 | 99 | 'x' => Base64Url::encode($localPublicKeyObject[0]),
|
101 | 100 | 'y' => Base64Url::encode($localPublicKeyObject[1]),
|
102 | 101 | ]);
|
@@ -276,9 +275,26 @@ private static function createLocalKeyObjectUsingPurePhpMethod(): array
|
276 | 275 | $privateKey = $curve->createPrivateKey();
|
277 | 276 | $publicKey = $curve->createPublicKey($privateKey);
|
278 | 277 |
|
| 278 | + if ($publicKey->getPoint()->getX() instanceof BigInteger) { |
| 279 | + return [ |
| 280 | + new JWK([ |
| 281 | + 'kty' => 'EC', |
| 282 | + 'crv' => 'P-256', |
| 283 | + 'x' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getX()->toBytes())), |
| 284 | + 'y' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getY()->toBytes())), |
| 285 | + 'd' => Base64Url::encode(self::addNullPadding($privateKey->getSecret()->toBytes())), |
| 286 | + ]) |
| 287 | + ]; |
| 288 | + } |
| 289 | + |
279 | 290 | return [
|
280 |
| - $publicKey, |
281 |
| - $privateKey, |
| 291 | + new JWK([ |
| 292 | + 'kty' => 'EC', |
| 293 | + 'crv' => 'P-256', |
| 294 | + 'x' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($publicKey->getPoint()->getX(), 16)))), |
| 295 | + 'y' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($publicKey->getPoint()->getY(), 16)))), |
| 296 | + 'd' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($privateKey->getSecret(), 16)))), |
| 297 | + ]) |
282 | 298 | ];
|
283 | 299 | }
|
284 | 300 |
|
@@ -307,9 +323,9 @@ private static function createLocalKeyObjectUsingOpenSSL(): array
|
307 | 323 | new JWK([
|
308 | 324 | 'kty' => 'EC',
|
309 | 325 | 'crv' => 'P-256',
|
310 |
| - 'x' => Base64Url::encode($details['ec']['x']), |
311 |
| - 'y' => Base64Url::encode($details['ec']['y']), |
312 |
| - 'd' => Base64Url::encode($details['ec']['d']), |
| 326 | + 'x' => Base64Url::encode(self::addNullPadding($details['ec']['x'])), |
| 327 | + 'y' => Base64Url::encode(self::addNullPadding($details['ec']['y'])), |
| 328 | + 'd' => Base64Url::encode(self::addNullPadding($details['ec']['d'])), |
313 | 329 | ])
|
314 | 330 | ];
|
315 | 331 | }
|
@@ -366,7 +382,7 @@ private static function calculateAgreementKey(JWK $private_key, JWK $public_key)
|
366 | 382 | $priv_key = PrivateKey::create($sen_d);
|
367 | 383 | $pub_key = $curve->getPublicKeyFrom($rec_x, $rec_y);
|
368 | 384 |
|
369 |
| - return hex2bin($curve->mul($pub_key->getPoint(), $priv_key->getSecret())->getX()->toBase(16)); // @phpstan-ignore-line |
| 385 | + return hex2bin(str_pad($curve->mul($pub_key->getPoint(), $priv_key->getSecret())->getX()->toBase(16), 64, '0', STR_PAD_LEFT)); // @phpstan-ignore-line |
370 | 386 | } catch (\Throwable $e) {
|
371 | 387 | $rec_x = self::convertBase64ToGMP($public_key->get('x'));
|
372 | 388 | $rec_y = self::convertBase64ToGMP($public_key->get('y'));
|
@@ -399,4 +415,9 @@ private static function convertBase64ToGMP(string $value): \GMP
|
399 | 415 |
|
400 | 416 | return gmp_init($value[1], 16);
|
401 | 417 | }
|
| 418 | + |
| 419 | + private static function addNullPadding(string $data): string |
| 420 | + { |
| 421 | + return str_pad($data, 32, chr(0), STR_PAD_LEFT); |
| 422 | + } |
402 | 423 | }
|
0 commit comments