Skip to content

Commit dacf799

Browse files
committed
Prefix filenames with file:// if necessary, or openssl will fail
1 parent 546a3f0 commit dacf799

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Key/PrivateKey.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use function openssl_pkey_export;
1212
use function openssl_pkey_get_private;
13+
use function preg_filter;
14+
use function preg_match;
1315

1416
/**
1517
* A class modeling private keys for their use in asymmetric algorithms.
@@ -52,6 +54,10 @@ public static function fromFile(
5254
#[\SensitiveParameter]
5355
string $passphrase = '',
5456
): static {
57+
if (preg_match('/^(file:\/\/)/i', $file) !== 1) {
58+
$file = preg_filter('/^/', 'file://', $file);
59+
}
60+
5561
if (($key = openssl_pkey_get_private($file, $passphrase)) === false) {
5662
throw new OpenSSLException('Failed to read key');
5763
}

tests/Key/PrivateKeyTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,15 @@ public function testFromFile(): void
5656
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_private($k->getMaterial()));
5757
$this->assertEquals(self::$privKey['key'], $keyDetails['key']);
5858
}
59+
60+
61+
/**
62+
* Test creation from a file without file:// prefix succeeds.
63+
*/
64+
public function testFromFileNoPrefix(): void
65+
{
66+
$k = PrivateKey::fromFile('./resources/keys/privkey.pem');
67+
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_private($k->getMaterial()));
68+
$this->assertEquals(self::$privKey['key'], $keyDetails['key']);
69+
}
5970
}

0 commit comments

Comments
 (0)