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?
I had the same error occur using PHP-DKIM
Without seeing the rest of your code, It's difficult to see if it is for the same reason.
However, my issue was that I was including the PHP-DKIM script inside a function.
ie
function sendEmail(){
require 'dkim.php';
//DO STUFF HERE
}
including the file outside the function stopped the error.
ie
require 'dkim.php';
function sendEmail(){
//DO STUFF HERE
}
I hope this helps.
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.
Personally, I would recommend phpseclib, a pure PHP RSA implementation, be used. eg.
<?php
$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
file_put_contents('signature.txt', $rsa->sign(file_get_contents('plaintext.txt')));
?>
That is fully interoperable with OpenSSL as the following demonstrates:
openssl dgst -sha1 -prverify privatekey.txt -signature signature plaintext.txt
来源:https://stackoverflow.com/questions/8672177/cannot-sign-anything-with-php-openssl-sign