Skip to content

Commit 3115ecd

Browse files
authored
Merge pull request #409 from martinssipenko/fix-openssl_pkey_get_private
FIX: Fix openssl_pkey_get_* functions
2 parents 1336004 + 3cd2bdf commit 3115ecd

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

generated/openssl.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -968,11 +968,40 @@ function openssl_pkey_export($key, ?string &$output, ?string $passphrase = null,
968968
}
969969

970970

971+
/**
972+
* This function returns the key details (bits, key, type).
973+
*
974+
* @param \OpenSSLAsymmetricKey $key Resource holding the key.
975+
* @return array Returns an array with the key details on success.
976+
* Returned array has indexes bits (number of bits),
977+
* key (string representation of the public key) and
978+
* type (type of the key which is one of
979+
* OPENSSL_KEYTYPE_RSA,
980+
* OPENSSL_KEYTYPE_DSA,
981+
* OPENSSL_KEYTYPE_DH,
982+
* OPENSSL_KEYTYPE_EC or -1 meaning unknown).
983+
*
984+
* Depending on the key type used, additional details may be returned. Note that
985+
* some elements may not always be available.
986+
* @throws OpensslException
987+
*
988+
*/
989+
function openssl_pkey_get_details(\OpenSSLAsymmetricKey $key): array
990+
{
991+
error_clear_last();
992+
$safeResult = \openssl_pkey_get_details($key);
993+
if ($safeResult === false) {
994+
throw OpensslException::createFromPhpError();
995+
}
996+
return $safeResult;
997+
}
998+
999+
9711000
/**
9721001
* openssl_pkey_get_private parses
9731002
* private_key and prepares it for use by other functions.
9741003
*
975-
* @param string $private_key private_key can be one of the following:
1004+
* @param \OpenSSLAsymmetricKey|\OpenSSLCertificate|array|string $private_key private_key can be one of the following:
9761005
*
9771006
* a string having the format
9781007
* file://path/to/file.pem. The named file must
@@ -981,13 +1010,13 @@ function openssl_pkey_export($key, ?string &$output, ?string $passphrase = null,
9811010
*
9821011
* A PEM formatted private key.
9831012
*
984-
* @param string $passphrase The optional parameter passphrase must be used
1013+
* @param string|null $passphrase The optional parameter passphrase must be used
9851014
* if the specified key is encrypted (protected by a passphrase).
986-
* @return resource Returns an OpenSSLAsymmetricKey instance on success.
1015+
* @return \OpenSSLAsymmetricKey Returns an OpenSSLAsymmetricKey instance on success.
9871016
* @throws OpensslException
9881017
*
9891018
*/
990-
function openssl_pkey_get_private(string $private_key, string $passphrase = null)
1019+
function openssl_pkey_get_private($private_key, ?string $passphrase = null): \OpenSSLAsymmetricKey
9911020
{
9921021
error_clear_last();
9931022
if ($passphrase !== null) {
@@ -1007,7 +1036,7 @@ function openssl_pkey_get_private(string $private_key, string $passphrase = null
10071036
* public_key and prepares it for use by other
10081037
* functions.
10091038
*
1010-
* @param resource|string $public_key public_key can be one of the following:
1039+
* @param \OpenSSLAsymmetricKey|\OpenSSLCertificate|array|string $public_key public_key can be one of the following:
10111040
*
10121041
* an OpenSSLAsymmetricKey instance
10131042
* a string having the format
@@ -1017,11 +1046,11 @@ function openssl_pkey_get_private(string $private_key, string $passphrase = null
10171046
*
10181047
* A PEM formatted public key.
10191048
*
1020-
* @return resource Returns an OpenSSLAsymmetricKey instance on success.
1049+
* @return \OpenSSLAsymmetricKey Returns an OpenSSLAsymmetricKey instance on success.
10211050
* @throws OpensslException
10221051
*
10231052
*/
1024-
function openssl_pkey_get_public($public_key)
1053+
function openssl_pkey_get_public($public_key): \OpenSSLAsymmetricKey
10251054
{
10261055
error_clear_last();
10271056
$safeResult = \openssl_pkey_get_public($public_key);

generator/config/CustomPhpStanFunctionMap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
'curl_share_errno' => ['int', 'share_handle' => 'CurlShareHandle'],
2727
'curl_share_setopt' => ['void', 'share_handle' => 'CurlShareHandle', 'option' => 'int', 'value' => 'mixed'],
2828
'curl_unescape' => ['string', 'handle' => 'CurlHandle', 'string' => 'string'],
29+
// theses replace ressource by OpenSSLAsymmetricKey
30+
'openssl_pkey_get_details' => ['array|false', 'key'=>'OpenSSLAsymmetricKey'],
31+
'openssl_pkey_get_private' => ['OpenSSLAsymmetricKey|false', 'private_key'=>'OpenSSLAsymmetricKey|OpenSSLCertificate|array|string', 'passphrase='=>'null|string'],
32+
'openssl_pkey_get_public' => ['OpenSSLAsymmetricKey|false', 'public_key'=>'OpenSSLAsymmetricKey|OpenSSLCertificate|array|string'],
2933
// theses replace ressource by OpenSSLCertificate
3034
'openssl_verify' => ['-1|0|1|false', 'data'=>'string', 'signature'=>'string', 'pub_key_id'=>' OpenSSLAsymmetricKey|OpenSSLCertificate|string', 'signature_alg='=>'int|string'],
3135
'openssl_x509_read' => ['OpenSSLCertificate|false', 'x509certdata'=>'OpenSSLCertificate|string'], // this replaces ressource by OpenSSLCertificate

rector-migrate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@
675675
'openssl_pkey_derive' => 'Safe\openssl_pkey_derive',
676676
'openssl_pkey_export' => 'Safe\openssl_pkey_export',
677677
'openssl_pkey_export_to_file' => 'Safe\openssl_pkey_export_to_file',
678+
'openssl_pkey_get_details' => 'Safe\openssl_pkey_get_details',
678679
'openssl_pkey_get_private' => 'Safe\openssl_pkey_get_private',
679680
'openssl_pkey_get_public' => 'Safe\openssl_pkey_get_public',
680681
'openssl_pkey_new' => 'Safe\openssl_pkey_new',

0 commit comments

Comments
 (0)