File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010
1111use function openssl_pkey_export ;
1212use 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 }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments