Skip to content

Commit 813f252

Browse files
authored
chore: [BREAKING] remove old pure php elliptic curve implementation (#390)
Now requires openssl with elliptic curve support. This is the usual case.
1 parent 8c52880 commit 813f252

File tree

3 files changed

+5
-53
lines changed

3 files changed

+5
-53
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ There is no support and maintenance for older PHP versions, however you are free
2121
- PHP 7.1: `v3.x-v5.x`
2222
- PHP 7.2: `v6.x`
2323
- PHP 7.3 7.4: `v7.x`
24-
- PHP 8.0: `v8.x`
24+
- PHP 8.0 / Openssl without elliptic curve support: `v8.x`
2525

2626
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.
2727

src/Encryption.php

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
namespace Minishlink\WebPush;
1515

1616
use Base64Url\Base64Url;
17-
use Brick\Math\BigInteger;
1817
use Jose\Component\Core\JWK;
19-
use Jose\Component\Core\Util\Ecc\NistCurve;
2018
use Jose\Component\Core\Util\Ecc\PrivateKey;
2119
use Jose\Component\Core\Util\ECKey;
2220

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

238236
private static function createLocalKeyObject(): array
239-
{
240-
try {
241-
return self::createLocalKeyObjectUsingOpenSSL();
242-
} catch (\Exception $e) {
243-
return self::createLocalKeyObjectUsingPurePhpMethod();
244-
}
245-
}
246-
247-
private static function createLocalKeyObjectUsingPurePhpMethod(): array
248-
{
249-
$curve = NistCurve::curve256();
250-
$privateKey = $curve->createPrivateKey();
251-
$publicKey = $curve->createPublicKey($privateKey);
252-
253-
if ($publicKey->getPoint()->getX() instanceof BigInteger) {
254-
return [
255-
new JWK([
256-
'kty' => 'EC',
257-
'crv' => 'P-256',
258-
'x' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getX()->toBytes(false))),
259-
'y' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getY()->toBytes(false))),
260-
'd' => Base64Url::encode(self::addNullPadding($privateKey->getSecret()->toBytes(false))),
261-
]),
262-
];
263-
}
264-
265-
return [
266-
new JWK([
267-
'kty' => 'EC',
268-
'crv' => 'P-256',
269-
'x' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($publicKey->getPoint()->getX(), 16)))),
270-
'y' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($publicKey->getPoint()->getY(), 16)))),
271-
'd' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($privateKey->getSecret(), 16)))),
272-
]),
273-
];
274-
}
275-
276-
private static function createLocalKeyObjectUsingOpenSSL(): array
277237
{
278238
$keyResource = openssl_pkey_new([
279239
'curve_name' => 'prime256v1',
280240
'private_key_type' => OPENSSL_KEYTYPE_EC,
281241
]);
282-
283242
if (!$keyResource) {
284-
throw new \RuntimeException('Unable to create the key');
243+
throw new \RuntimeException('Unable to create the local key.');
285244
}
286245

287246
$details = openssl_pkey_get_details($keyResource);
288-
289247
if (!$details) {
290-
throw new \RuntimeException('Unable to get the key details');
248+
throw new \RuntimeException('Unable to get the local key details.');
291249
}
292250

293251
return [

src/Utils.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Minishlink\WebPush;
1515

1616
use Base64Url\Base64Url;
17-
use Brick\Math\BigInteger;
1817
use Jose\Component\Core\JWK;
1918
use Jose\Component\Core\Util\Ecc\PublicKey;
2019

@@ -29,13 +28,8 @@ public static function serializePublicKey(PublicKey $publicKey): string
2928
{
3029
$hexString = '04';
3130
$point = $publicKey->getPoint();
32-
if ($point->getX() instanceof BigInteger) {
33-
$hexString .= str_pad($point->getX()->toBase(16), 64, '0', STR_PAD_LEFT);
34-
$hexString .= str_pad($point->getY()->toBase(16), 64, '0', STR_PAD_LEFT);
35-
} else { // @phpstan-ignore-line
36-
$hexString .= str_pad(gmp_strval($point->getX(), 16), 64, '0', STR_PAD_LEFT);
37-
$hexString .= str_pad(gmp_strval($point->getY(), 16), 64, '0', STR_PAD_LEFT); // @phpstan-ignore-line
38-
}
31+
$hexString .= str_pad($point->getX()->toBase(16), 64, '0', STR_PAD_LEFT);
32+
$hexString .= str_pad($point->getY()->toBase(16), 64, '0', STR_PAD_LEFT);
3933

4034
return $hexString;
4135
}

0 commit comments

Comments
 (0)