diff --git a/README.md b/README.md index 655cd90..ebabcb4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Encryption.php b/src/Encryption.php index b1e0ed7..5c347e1 100644 --- a/src/Encryption.php +++ b/src/Encryption.php @@ -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; @@ -233,58 +231,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 [ diff --git a/src/Utils.php b/src/Utils.php index a23200a..dcb5fc4 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -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; @@ -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); + $hexString .= str_pad($point->getY()->toBase(16), 64, '0', STR_PAD_LEFT); return $hexString; }