openssl_sign(): supplied key param cannot be coerced into a private key

Hello, just wanted to write something coming back after a long time. Please excuse my absence, life has been very busy with so much other priorities.

This post is for those who are using a PHP-DKIM solution and experiencing this below error –

openssl_sign(): supplied key param cannot be coerced into a private key

This happens when you have inputted a wrong private key input in the openssl_sign(). The third parameter is actually the “private key id” received from a call on openssl_get_privatekey(). But often its mistakenly gets passed with a string value of the private key lines.

So, here is the solution –
1) Prepare the “private key id”:
$fp = fopen("/path/to/file/.htkeyprivate", "r");
$privKey = fread($fp, 8192);
fclose($fp);
$pKeyId = openssl_get_privatekey($privKey, 'optional_passphrase');

2) Use in the openssl_sign:
openssl_sign($dataToSign, $signatureVar, $pKeyId);

If you’re using PHP-DKIM class based solution (object oriented), you may put the below code in the _construct() of the main class:

public function __construct()
{
$fp = fopen("/path/to/file/.htkeyprivate", "r");
$privKey = fread($fp, 8192);
fclose($fp);
$pKeyId = openssl_get_privatekey($privKey, 'optional_passphase');

$this->open_SSL_priv = $pKeyId;
}

Hope this helps you. I will definitely try to write again whenever I get time. Theres a lot to share from regular development experiences but time has bound me.

Happy Blogging!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.