Skip to content

Commit

Permalink
chore: [BREAKING] remove old pure php elliptic curve implementation
Browse files Browse the repository at this point in the history
Now requires openssl with elliptic curve support. This is the usual case.
  • Loading branch information
Rotzbua committed Jan 30, 2024
1 parent fd2d54a commit 2e55b6c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 53 deletions.
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 @@ -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 [
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);
$hexString .= str_pad($point->getY()->toBase(16), 64, '0', STR_PAD_LEFT);

return $hexString;
}
Expand Down

0 comments on commit 2e55b6c

Please sign in to comment.