Cannot sign anything with PHP openssl_sign?

TheLQ picture TheLQ · Dec 29, 2011 · Viewed 13.2k times · Source

I am attempting to use PHP-DKIM to send DKIM signed emails. Its a little old so I've had to change some things, but this stumps me:

Warning: openssl_sign() [function.openssl-sign]: supplied key param cannot be coerced into a private key in /.../pages/user/dkim.php on line 66
Cannot sign

Relevant section of code (note I've added the $pkeyid, originally the private key was just passed straight to the open_ssl function which also didn't work)

$pkeyid = openssl_get_privatekey($open_SSL_priv);
if (openssl_sign($s, $signature, $pkeyid))
    return base64_encode($signature) ;
else
    die("Cannot sign") ;

So obviously something really bad is going on here. However I know my private key and public key are valid. I even tried the example key provided in the comments for openssl_sign which didn't work

$open_SSL_pub=<<<EOD
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6
zxqlVzz0wy2j4kQVUC4ZRZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQ==
-----END PUBLIC KEY-----
EOD;

$open_SSL_priv=<<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6zxqlVzz0wy2j4kQVUC4Z
RZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQJAL151ZeMKHEU2c1qdRKS9
sTxCcc2pVwoAGVzRccNX16tfmCf8FjxuM3WmLdsPxYoHrwb1LFNxiNk1MXrxjH3R
6QIhAPB7edmcjH4bhMaJBztcbNE1VRCEi/bisAwiPPMq9/2nAiEA3lyc5+f6DEIJ
h1y6BWkdVULDSM+jpi1XiV/DevxuijMCIQCAEPGqHsF+4v7Jj+3HAgh9PU6otj2n
Y79nJtCYmvhoHwIgNDePaS4inApN7omp7WdXyhPZhBmulnGDYvEoGJN66d0CIHra
I2SvDkQ5CmrzkW5qPaE2oO7BSqAhRZxiYpZFb5CI
-----END RSA PRIVATE KEY-----
EOD;

I'm at a loss of what to do. OpenSSL "0.9.8e-fips-rhel5 01 Jul 2008" is installed and active in PHP. Both the key I generated and that key are known working. So why does openssl_sign keep failing?

Answer

user2778850 picture user2778850 · Jul 16, 2014

I had this issue today, and the problem was slightly different; I had my privateKey as string in PHP, rather than stored in a file and retrieved using file_get_contents. As soon as I switched to a file-based solution, everything started to work.