Skip to content

Commit 6c764ae

Browse files
committed
RSA key creation with length lower than 512 is now rejected
1 parent c5e828a commit 6c764ae

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

src/Component/KeyManagement/JWKFactory.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ public static function createRSAKey(int $size, array $values = []): JWK
3434
throw new \InvalidArgumentException('Invalid key size.');
3535
}
3636

37-
if (384 > $size) {
38-
throw new \InvalidArgumentException('Key length is too short. It needs to be at least 384 bits.');
37+
if (512 > $size) {
38+
throw new \InvalidArgumentException('Key length is too short. It needs to be at least 512 bits.');
3939
}
4040

4141
$key = \openssl_pkey_new([
4242
'private_key_bits' => $size,
4343
'private_key_type' => OPENSSL_KEYTYPE_RSA,
4444
]);
45-
\openssl_pkey_export($key, $out);
46-
$rsa = RSAKey::createFromPEM($out);
45+
$details = \openssl_pkey_get_details($key);
46+
\openssl_free_key($key);
47+
$rsa = RSAKey::createFromKeyDetails($details['rsa']);
4748
$values = \array_merge(
4849
$values,
4950
$rsa->toArray()

src/Component/KeyManagement/KeyConverter/RSAKey.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,55 +38,57 @@ private function __construct(array $data)
3838
/**
3939
* @return RSAKey
4040
*/
41-
public static function createFromPEM(string $pem): self
41+
public static function createFromKeyDetails(array $details): self
4242
{
43-
$data = self::loadPEM($pem);
43+
$values = ['kty' => 'RSA'];
44+
$keys = [
45+
'n' => 'n',
46+
'e' => 'e',
47+
'd' => 'd',
48+
'p' => 'p',
49+
'q' => 'q',
50+
'dp' => 'dmp1',
51+
'dq' => 'dmq1',
52+
'qi' => 'iqmp',
53+
];
54+
foreach ($details as $key => $value) {
55+
if (\in_array($key, $keys, true)) {
56+
$value = Base64Url::encode($value);
57+
$values[\array_search($key, $keys, true)] = $value;
58+
}
59+
}
4460

45-
return new self($data);
61+
return new self($values);
4662
}
4763

4864
/**
4965
* @return RSAKey
5066
*/
51-
public static function createFromJWK(JWK $jwk): self
52-
{
53-
return new self($jwk->all());
54-
}
55-
56-
private static function loadPEM(string $data): array
67+
public static function createFromPEM(string $pem): self
5768
{
58-
$res = \openssl_pkey_get_private($data);
69+
$res = \openssl_pkey_get_private($pem);
5970
if (false === $res) {
60-
$res = \openssl_pkey_get_public($data);
71+
$res = \openssl_pkey_get_public($pem);
6172
}
6273
if (false === $res) {
6374
throw new \InvalidArgumentException('Unable to load the key.');
6475
}
6576

6677
$details = \openssl_pkey_get_details($res);
78+
\openssl_free_key($res);
6779
if (!\array_key_exists('rsa', $details)) {
6880
throw new \InvalidArgumentException('Unable to load the key.');
6981
}
7082

71-
$values = ['kty' => 'RSA'];
72-
$keys = [
73-
'n' => 'n',
74-
'e' => 'e',
75-
'd' => 'd',
76-
'p' => 'p',
77-
'q' => 'q',
78-
'dp' => 'dmp1',
79-
'dq' => 'dmq1',
80-
'qi' => 'iqmp',
81-
];
82-
foreach ($details['rsa'] as $key => $value) {
83-
if (\in_array($key, $keys, true)) {
84-
$value = Base64Url::encode($value);
85-
$values[\array_search($key, $keys, true)] = $value;
86-
}
87-
}
83+
return self::createFromKeyDetails($details['rsa']);
84+
}
8885

89-
return $values;
86+
/**
87+
* @return RSAKey
88+
*/
89+
public static function createFromJWK(JWK $jwk): self
90+
{
91+
return new self($jwk->all());
9092
}
9193

9294
public function isPublic(): bool

src/Component/KeyManagement/Tests/Keys/RSAKeysTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ public function convertPrivateKeyToPublic()
280280
/**
281281
* @test
282282
*/
283-
public function createRSAKey384Bits()
283+
public function createRSAKey512Bits()
284284
{
285-
$jwk = JWKFactory::createRSAKey(384);
285+
$jwk = JWKFactory::createRSAKey(512);
286286

287287
static::assertEquals('RSA', $jwk->get('kty'));
288288
static::assertTrue($jwk->has('p'));

0 commit comments

Comments
 (0)