Skip to content

Commit 52d0f88

Browse files
xginSpomky
authored andcommitted
Do not init curves with GMP when EC key is created using OpenSSL (#179)
1 parent 19795d7 commit 52d0f88

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/Component/Core/Util/ECKey.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ private static function getNistCurve(string $curve): Curve
119119
}
120120
}
121121

122+
private static function getNistCurveSize(string $curve): int
123+
{
124+
switch ($curve) {
125+
case 'P-256':
126+
return 256;
127+
case 'P-384':
128+
return 384;
129+
case 'P-521':
130+
return 521;
131+
default:
132+
throw new InvalidArgumentException(sprintf('The curve "%s" is not supported.', $curve));
133+
}
134+
}
135+
122136
private static function createECKeyUsingPurePhp(string $curve): array
123137
{
124138
$nistCurve = self::getNistCurve($curve);
@@ -152,14 +166,14 @@ private static function createECKeyUsingOpenSSL(string $curve): array
152166
throw new RuntimeException('Unable to create the key');
153167
}
154168
$details = openssl_pkey_get_details($res);
155-
$nistCurve = self::getNistCurve($curve);
169+
$nistCurveSize = self::getNistCurveSize($curve);
156170

157171
return [
158172
'kty' => 'EC',
159173
'crv' => $curve,
160-
'd' => Base64Url::encode(str_pad($details['ec']['d'], (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
161-
'x' => Base64Url::encode(str_pad($details['ec']['x'], (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
162-
'y' => Base64Url::encode(str_pad($details['ec']['y'], (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
174+
'd' => Base64Url::encode(str_pad($details['ec']['d'], (int) ceil($nistCurveSize / 8), "\0", STR_PAD_LEFT)),
175+
'x' => Base64Url::encode(str_pad($details['ec']['x'], (int) ceil($nistCurveSize / 8), "\0", STR_PAD_LEFT)),
176+
'y' => Base64Url::encode(str_pad($details['ec']['y'], (int) ceil($nistCurveSize / 8), "\0", STR_PAD_LEFT)),
163177
];
164178
}
165179

@@ -281,8 +295,8 @@ private static function p521PrivateKey(JWK $jwk): string
281295

282296
private static function getKey(JWK $jwk): string
283297
{
284-
$nistCurve = self::getNistCurve($jwk->get('crv'));
285-
$length = (int) ceil($nistCurve->getSize() / 8);
298+
$nistCurveSize = self::getNistCurveSize($jwk->get('crv'));
299+
$length = (int) ceil($nistCurveSize / 8);
286300

287301
return
288302
"\04"

0 commit comments

Comments
 (0)