Skip to content

Commit

Permalink
Fix undefined method Brick\Math\BigInteger::getX() (#297)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Spomky committed Sep 29, 2020
1 parent a9b6896 commit eddd95c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ parameters:
paths:
- src
checkMissingIterableValueType: false
ignoreErrors:
- '#Unreachable statement \- code above always terminates\.#'
37 changes: 29 additions & 8 deletions src/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Base64Url\Base64Url;
use Brick\Math\BigInteger;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\Ecc\Curve;
use Jose\Component\Core\Util\Ecc\NistCurve;
use Jose\Component\Core\Util\Ecc\PrivateKey;
use Jose\Component\Core\Util\ECKey;
Expand Down Expand Up @@ -96,7 +95,7 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
$localJwk = new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'd' => $localPrivateKeyObject->getSecret()->getX(), // @phpstan-ignore-line
'd' => Base64Url::encode($localPrivateKeyObject->getSecret()->toBytes()),
'x' => Base64Url::encode($localPublicKeyObject[0]),
'y' => Base64Url::encode($localPublicKeyObject[1]),
]);
Expand Down Expand Up @@ -276,9 +275,26 @@ private static function createLocalKeyObjectUsingPurePhpMethod(): array
$privateKey = $curve->createPrivateKey();
$publicKey = $curve->createPublicKey($privateKey);

if ($publicKey->getPoint()->getX() instanceof BigInteger) {
return [
new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getX()->toBytes())),
'y' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getY()->toBytes())),
'd' => Base64Url::encode(self::addNullPadding($privateKey->getSecret()->toBytes())),
])
];
}

return [
$publicKey,
$privateKey,
new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($publicKey->getPoint()->getX(), 16)))),
'y' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($publicKey->getPoint()->getY(), 16)))),
'd' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($privateKey->getSecret(), 16)))),
])
];
}

Expand Down Expand Up @@ -307,9 +323,9 @@ private static function createLocalKeyObjectUsingOpenSSL(): array
new JWK([
'kty' => 'EC',
'crv' => 'P-256',
'x' => Base64Url::encode($details['ec']['x']),
'y' => Base64Url::encode($details['ec']['y']),
'd' => Base64Url::encode($details['ec']['d']),
'x' => Base64Url::encode(self::addNullPadding($details['ec']['x'])),
'y' => Base64Url::encode(self::addNullPadding($details['ec']['y'])),
'd' => Base64Url::encode(self::addNullPadding($details['ec']['d'])),
])
];
}
Expand Down Expand Up @@ -366,7 +382,7 @@ private static function calculateAgreementKey(JWK $private_key, JWK $public_key)
$priv_key = PrivateKey::create($sen_d);
$pub_key = $curve->getPublicKeyFrom($rec_x, $rec_y);

return hex2bin($curve->mul($pub_key->getPoint(), $priv_key->getSecret())->getX()->toBase(16)); // @phpstan-ignore-line
return hex2bin(str_pad($curve->mul($pub_key->getPoint(), $priv_key->getSecret())->getX()->toBase(16), 64, '0', STR_PAD_LEFT)); // @phpstan-ignore-line
} catch (\Throwable $e) {
$rec_x = self::convertBase64ToGMP($public_key->get('x'));
$rec_y = self::convertBase64ToGMP($public_key->get('y'));
Expand Down Expand Up @@ -399,4 +415,9 @@ private static function convertBase64ToGMP(string $value): \GMP

return gmp_init($value[1], 16);
}

private static function addNullPadding(string $data): string
{
return str_pad($data, 32, chr(0), STR_PAD_LEFT);
}
}

0 comments on commit eddd95c

Please sign in to comment.