File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change 10
10
11
11
use function openssl_pkey_export ;
12
12
use function openssl_pkey_get_private ;
13
+ use function preg_filter ;
14
+ use function preg_match ;
13
15
14
16
/**
15
17
* A class modeling private keys for their use in asymmetric algorithms.
@@ -52,6 +54,10 @@ public static function fromFile(
52
54
#[\SensitiveParameter]
53
55
string $ passphrase = '' ,
54
56
): static {
57
+ if (preg_match ('/^(file:\/\/)/i ' , $ file ) !== 1 ) {
58
+ $ file = preg_filter ('/^/ ' , 'file:// ' , $ file );
59
+ }
60
+
55
61
if (($ key = openssl_pkey_get_private ($ file , $ passphrase )) === false ) {
56
62
throw new OpenSSLException ('Failed to read key ' );
57
63
}
Original file line number Diff line number Diff line change @@ -56,4 +56,15 @@ public function testFromFile(): void
56
56
$ keyDetails = openssl_pkey_get_details (openssl_pkey_get_private ($ k ->getMaterial ()));
57
57
$ this ->assertEquals (self ::$ privKey ['key ' ], $ keyDetails ['key ' ]);
58
58
}
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
+ }
59
70
}
You can’t perform that action at this time.
0 commit comments