|
14 | 14 | namespace Minishlink\WebPush; |
15 | 15 |
|
16 | 16 | use Base64Url\Base64Url; |
17 | | -use Brick\Math\BigInteger; |
18 | 17 | use Jose\Component\Core\JWK; |
19 | | -use Jose\Component\Core\Util\Ecc\NistCurve; |
20 | 18 | use Jose\Component\Core\Util\Ecc\PrivateKey; |
21 | 19 | use Jose\Component\Core\Util\ECKey; |
22 | 20 |
|
@@ -236,58 +234,18 @@ private static function createInfo(string $type, ?string $context, string $conte |
236 | 234 | } |
237 | 235 |
|
238 | 236 | 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 |
277 | 237 | { |
278 | 238 | $keyResource = openssl_pkey_new([ |
279 | 239 | 'curve_name' => 'prime256v1', |
280 | 240 | 'private_key_type' => OPENSSL_KEYTYPE_EC, |
281 | 241 | ]); |
282 | | - |
283 | 242 | if (!$keyResource) { |
284 | | - throw new \RuntimeException('Unable to create the key'); |
| 243 | + throw new \RuntimeException('Unable to create the local key.'); |
285 | 244 | } |
286 | 245 |
|
287 | 246 | $details = openssl_pkey_get_details($keyResource); |
288 | | - |
289 | 247 | if (!$details) { |
290 | | - throw new \RuntimeException('Unable to get the key details'); |
| 248 | + throw new \RuntimeException('Unable to get the local key details.'); |
291 | 249 | } |
292 | 250 |
|
293 | 251 | return [ |
|
0 commit comments