Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: [BREAKING] remove old pure php elliptic curve implementation #390

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ There is no support and maintenance for older PHP versions, however you are free
- PHP 7.1: `v3.x-v5.x`
- PHP 7.2: `v6.x`
- PHP 7.3 7.4: `v7.x`
- PHP 8.0: `v8.x`
- PHP 8.0 / Openssl without elliptic curve support: `v8.x`

This README is only compatible with the latest version. Each version of the library has a git tag where the corresponding README can be read.

Expand Down
46 changes: 2 additions & 44 deletions src/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
namespace Minishlink\WebPush;

use Base64Url\Base64Url;
use Brick\Math\BigInteger;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\Ecc\NistCurve;
use Jose\Component\Core\Util\Ecc\PrivateKey;
use Jose\Component\Core\Util\ECKey;

Expand Down Expand Up @@ -236,58 +234,18 @@ private static function createInfo(string $type, ?string $context, string $conte
}

private static function createLocalKeyObject(): array
{
try {
return self::createLocalKeyObjectUsingOpenSSL();
} catch (\Exception $e) {
return self::createLocalKeyObjectUsingPurePhpMethod();
}
}

private static function createLocalKeyObjectUsingPurePhpMethod(): array
{
$curve = NistCurve::curve256();
$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(false))),
'y' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getY()->toBytes(false))),
'd' => Base64Url::encode(self::addNullPadding($privateKey->getSecret()->toBytes(false))),
]),
];
}

return [
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)))),
]),
];
}

private static function createLocalKeyObjectUsingOpenSSL(): array
{
$keyResource = openssl_pkey_new([
'curve_name' => 'prime256v1',
'private_key_type' => OPENSSL_KEYTYPE_EC,
]);

if (!$keyResource) {
throw new \RuntimeException('Unable to create the key');
throw new \RuntimeException('Unable to create the local key.');
}

$details = openssl_pkey_get_details($keyResource);

if (!$details) {
throw new \RuntimeException('Unable to get the key details');
throw new \RuntimeException('Unable to get the local key details.');
}

return [
Expand Down
10 changes: 2 additions & 8 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Minishlink\WebPush;

use Base64Url\Base64Url;
use Brick\Math\BigInteger;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\Ecc\PublicKey;

Expand All @@ -29,13 +28,8 @@ public static function serializePublicKey(PublicKey $publicKey): string
{
$hexString = '04';
$point = $publicKey->getPoint();
if ($point->getX() instanceof BigInteger) {
$hexString .= str_pad($point->getX()->toBase(16), 64, '0', STR_PAD_LEFT);
$hexString .= str_pad($point->getY()->toBase(16), 64, '0', STR_PAD_LEFT);
} else { // @phpstan-ignore-line
$hexString .= str_pad(gmp_strval($point->getX(), 16), 64, '0', STR_PAD_LEFT);
$hexString .= str_pad(gmp_strval($point->getY(), 16), 64, '0', STR_PAD_LEFT); // @phpstan-ignore-line
}
$hexString .= str_pad($point->getX()->toBase(16), 64, '0', STR_PAD_LEFT);
Rotzbua marked this conversation as resolved.
Show resolved Hide resolved
$hexString .= str_pad($point->getY()->toBase(16), 64, '0', STR_PAD_LEFT);

return $hexString;
}
Expand Down