Skip to content

Commit

Permalink
Merge pull request #409 from martinssipenko/fix-openssl_pkey_get_private
Browse files Browse the repository at this point in the history
FIX: Fix openssl_pkey_get_* functions
  • Loading branch information
Kharhamel committed Apr 5, 2023
2 parents 1336004 + 3cd2bdf commit 3115ecd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
43 changes: 36 additions & 7 deletions generated/openssl.php
Expand Up @@ -968,11 +968,40 @@ function openssl_pkey_export($key, ?string &$output, ?string $passphrase = null,
}


/**
* This function returns the key details (bits, key, type).
*
* @param \OpenSSLAsymmetricKey $key Resource holding the key.
* @return array Returns an array with the key details on success.
* Returned array has indexes bits (number of bits),
* key (string representation of the public key) and
* type (type of the key which is one of
* OPENSSL_KEYTYPE_RSA,
* OPENSSL_KEYTYPE_DSA,
* OPENSSL_KEYTYPE_DH,
* OPENSSL_KEYTYPE_EC or -1 meaning unknown).
*
* Depending on the key type used, additional details may be returned. Note that
* some elements may not always be available.
* @throws OpensslException
*
*/
function openssl_pkey_get_details(\OpenSSLAsymmetricKey $key): array
{
error_clear_last();
$safeResult = \openssl_pkey_get_details($key);
if ($safeResult === false) {
throw OpensslException::createFromPhpError();
}
return $safeResult;
}


/**
* openssl_pkey_get_private parses
* private_key and prepares it for use by other functions.
*
* @param string $private_key private_key can be one of the following:
* @param \OpenSSLAsymmetricKey|\OpenSSLCertificate|array|string $private_key private_key can be one of the following:
*
* a string having the format
* file://path/to/file.pem. The named file must
Expand All @@ -981,13 +1010,13 @@ function openssl_pkey_export($key, ?string &$output, ?string $passphrase = null,
*
* A PEM formatted private key.
*
* @param string $passphrase The optional parameter passphrase must be used
* @param string|null $passphrase The optional parameter passphrase must be used
* if the specified key is encrypted (protected by a passphrase).
* @return resource Returns an OpenSSLAsymmetricKey instance on success.
* @return \OpenSSLAsymmetricKey Returns an OpenSSLAsymmetricKey instance on success.
* @throws OpensslException
*
*/
function openssl_pkey_get_private(string $private_key, string $passphrase = null)
function openssl_pkey_get_private($private_key, ?string $passphrase = null): \OpenSSLAsymmetricKey
{
error_clear_last();
if ($passphrase !== null) {
Expand All @@ -1007,7 +1036,7 @@ function openssl_pkey_get_private(string $private_key, string $passphrase = null
* public_key and prepares it for use by other
* functions.
*
* @param resource|string $public_key public_key can be one of the following:
* @param \OpenSSLAsymmetricKey|\OpenSSLCertificate|array|string $public_key public_key can be one of the following:
*
* an OpenSSLAsymmetricKey instance
* a string having the format
Expand All @@ -1017,11 +1046,11 @@ function openssl_pkey_get_private(string $private_key, string $passphrase = null
*
* A PEM formatted public key.
*
* @return resource Returns an OpenSSLAsymmetricKey instance on success.
* @return \OpenSSLAsymmetricKey Returns an OpenSSLAsymmetricKey instance on success.
* @throws OpensslException
*
*/
function openssl_pkey_get_public($public_key)
function openssl_pkey_get_public($public_key): \OpenSSLAsymmetricKey
{
error_clear_last();
$safeResult = \openssl_pkey_get_public($public_key);
Expand Down
4 changes: 4 additions & 0 deletions generator/config/CustomPhpStanFunctionMap.php
Expand Up @@ -26,6 +26,10 @@
'curl_share_errno' => ['int', 'share_handle' => 'CurlShareHandle'],
'curl_share_setopt' => ['void', 'share_handle' => 'CurlShareHandle', 'option' => 'int', 'value' => 'mixed'],
'curl_unescape' => ['string', 'handle' => 'CurlHandle', 'string' => 'string'],
// theses replace ressource by OpenSSLAsymmetricKey
'openssl_pkey_get_details' => ['array|false', 'key'=>'OpenSSLAsymmetricKey'],
'openssl_pkey_get_private' => ['OpenSSLAsymmetricKey|false', 'private_key'=>'OpenSSLAsymmetricKey|OpenSSLCertificate|array|string', 'passphrase='=>'null|string'],
'openssl_pkey_get_public' => ['OpenSSLAsymmetricKey|false', 'public_key'=>'OpenSSLAsymmetricKey|OpenSSLCertificate|array|string'],
// theses replace ressource by OpenSSLCertificate
'openssl_verify' => ['-1|0|1|false', 'data'=>'string', 'signature'=>'string', 'pub_key_id'=>' OpenSSLAsymmetricKey|OpenSSLCertificate|string', 'signature_alg='=>'int|string'],
'openssl_x509_read' => ['OpenSSLCertificate|false', 'x509certdata'=>'OpenSSLCertificate|string'], // this replaces ressource by OpenSSLCertificate
Expand Down
1 change: 1 addition & 0 deletions rector-migrate.php
Expand Up @@ -675,6 +675,7 @@
'openssl_pkey_derive' => 'Safe\openssl_pkey_derive',
'openssl_pkey_export' => 'Safe\openssl_pkey_export',
'openssl_pkey_export_to_file' => 'Safe\openssl_pkey_export_to_file',
'openssl_pkey_get_details' => 'Safe\openssl_pkey_get_details',
'openssl_pkey_get_private' => 'Safe\openssl_pkey_get_private',
'openssl_pkey_get_public' => 'Safe\openssl_pkey_get_public',
'openssl_pkey_new' => 'Safe\openssl_pkey_new',
Expand Down

0 comments on commit 3115ecd

Please sign in to comment.