警告:openssl_pkcs7_sign():在Linux服务器上获取私钥时出错

时间:2015-09-27 07:18:04

标签: php ssl

也许这是一个重复的问题,并在thisthis中询问,但这个问题似乎是特定的。

我想连接到使用ssl证书的银行互联网支付系统,但我遇到了这个错误:

Warning: openssl_pkcs7_sign(): error getting private key in /home/zarsamco/public_html/eghtesad/ipg/enpayment.php on line 52

这是我的代码中使用证书文件的一部分(在wamp local上):

openssl_pkcs7_sign(realpath("msg.txt"), realpath("signed.txt"), "file://D:/wamp/www/zarsam/eghtesad/certs/ZarsamHonar.pem",
                array ("file://D:/wamp/www/zarsam/eghtesad/certs/ZarsamHonar.pem", "secretPass"),
                array (), PKCS7_NOSIGS
            );

它在本地(窗口8)的wamp中工作正常,因为 ZarsamHonar.pem 的地址是绝对的。但是这段代码在远程服务器(Linux)上不起作用,因为我认为这种地址方式特别适用于Windows。

我尝试了很多在线发现的寻址方法和链接的SO问题 例如,我尝试此代码( enpayment.php 页面):

$prepend = "file://";
openssl_pkcs7_sign(realpath(dirname(__FILE__)) . "../msg.txt",
realpath(dirname(__FILE__)) . "../signed.txt",
$prepend . realpath(dirname(__FILE__)) ."/certs/ZarsamHonar.pem",
array($prepend . realpath(dirname(__FILE__)) ."/certs/ZarsamHonar.pem", "secretPass"),array(), PKCS7_NOSIGS);

但它也没有成功。

这是ZarsamHonar.pem,msg.txt和signed.txt文件的相关文件结构和位置的屏幕截图:

enter image description here

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:5)

经过数小时的搜索并尝试不同的方法和指导@Sjon,当然在php.net上根据this user Comment我找到了解决方案。

最后的代码是:

openssl_pkcs7_sign(realpath("msg.txt"), "signed.txt",
                'file://'.realpath('/home/zarsamco/public_html/eghtesad/certs/zarsamhonar.pem'),
                array ('file://'.realpath('/home/zarsamco/public_html/eghtesad/certs/zarsamhonar.pem'), "secretPass"),
                array (), PKCS7_NOSIGS
            );  
第一个参数中的

必须使用 realpath 功能,但不要用于第二个参数  因为它还不存在对于寻址第三个和第四个参数,必须使用绝对路径以及 file:// 前缀。

答案 1 :(得分:1)

你应该在这里明确地使用相对路径(应该在本地和在线工作),在这种情况下(在enpayment.php中)你应该使用:

openssl_pkcs7_sign(realpath("msg.txt"), realpath("signed.txt"),
    "../certs/ZarsamHonar.pem",
    array ("../certs/ZarsamHonar.pem", "secretPass"),
    array (), PKCS7_NOSIGS
);

要确认路径已正确解析,您可以通过将echo file_get_contents("../certs/ZarsamHonar.pem");置于openssl调用之前进行测试,并查看是否输出了预期文件