Skip to content

Commit eddd95c

Browse files
authored
Fix undefined method Brick\Math\BigInteger::getX() (#297)
* Fix #292 * Fix inconsistent local key generation * Fix GMP support of key creation * Fix duplicated import and hex size * Fix duplicated import and hex size * Typo… * Fix invalid hex value length * Fix invalid hex value length * self:: call removed * Ignore irrelevant PHPStan error
1 parent a9b6896 commit eddd95c

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

phpstan.neon

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ parameters:
33
paths:
44
- src
55
checkMissingIterableValueType: false
6+
ignoreErrors:
7+
- '#Unreachable statement \- code above always terminates\.#'

src/Encryption.php

+29-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Base64Url\Base64Url;
1717
use Brick\Math\BigInteger;
1818
use Jose\Component\Core\JWK;
19-
use Jose\Component\Core\Util\Ecc\Curve;
2019
use Jose\Component\Core\Util\Ecc\NistCurve;
2120
use Jose\Component\Core\Util\Ecc\PrivateKey;
2221
use Jose\Component\Core\Util\ECKey;
@@ -96,7 +95,7 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
9695
$localJwk = new JWK([
9796
'kty' => 'EC',
9897
'crv' => 'P-256',
99-
'd' => $localPrivateKeyObject->getSecret()->getX(), // @phpstan-ignore-line
98+
'd' => Base64Url::encode($localPrivateKeyObject->getSecret()->toBytes()),
10099
'x' => Base64Url::encode($localPublicKeyObject[0]),
101100
'y' => Base64Url::encode($localPublicKeyObject[1]),
102101
]);
@@ -276,9 +275,26 @@ private static function createLocalKeyObjectUsingPurePhpMethod(): array
276275
$privateKey = $curve->createPrivateKey();
277276
$publicKey = $curve->createPublicKey($privateKey);
278277

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+
279290
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+
])
282298
];
283299
}
284300

@@ -307,9 +323,9 @@ private static function createLocalKeyObjectUsingOpenSSL(): array
307323
new JWK([
308324
'kty' => 'EC',
309325
'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'])),
313329
])
314330
];
315331
}
@@ -366,7 +382,7 @@ private static function calculateAgreementKey(JWK $private_key, JWK $public_key)
366382
$priv_key = PrivateKey::create($sen_d);
367383
$pub_key = $curve->getPublicKeyFrom($rec_x, $rec_y);
368384

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
370386
} catch (\Throwable $e) {
371387
$rec_x = self::convertBase64ToGMP($public_key->get('x'));
372388
$rec_y = self::convertBase64ToGMP($public_key->get('y'));
@@ -399,4 +415,9 @@ private static function convertBase64ToGMP(string $value): \GMP
399415

400416
return gmp_init($value[1], 16);
401417
}
418+
419+
private static function addNullPadding(string $data): string
420+
{
421+
return str_pad($data, 32, chr(0), STR_PAD_LEFT);
422+
}
402423
}

0 commit comments

Comments
 (0)