Warning: openssl_pkcs7_sign(): error getting priva

2020-07-24 05:49发布

Maybe this is a duplicate question and asked in this or this but this problem seems specific.

I want to connect to a bank internet payment system that uses ssl certificates but I face this error :

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

and this is the part of my code that uses certificate file (on 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
            );

it works fine in wamp on local (window 8) because the address of ZarsamHonar.pem is absolute. However this code does not work on remote server(Linux) because i think that this way of addresses is particular for windows.

I tried many Addressing methods found online and the linked SO questions.
for example I try this code (enpayment.php page):

$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);

But it did not work out either.

this is a screenshot of related files structure and location of ZarsamHonar.pem , msg.txt and signed.txt files:

enter image description here

can any one help me?

标签: php ssl
2条回答
\"骚年 ilove
2楼-- · 2020-07-24 06:25

after hours of search and try different method and guidance @Sjon and of course according this user Comment on php.net I found the Solution.

the finally code is :

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
            );  

in the first parameter must use realpath function but do not use for second one.
because it does not exist yet. and for addressing third and forth parameter must use absolute path along with file:// prefix.

查看更多
ら.Afraid
3楼-- · 2020-07-24 06:39

You should definetly use relative paths here (should work both locally and online), in this case (in enpayment.php) you should use:

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

To confirm the path is resolved correctly, you can test by putting echo file_get_contents("../certs/ZarsamHonar.pem"); in front of the openssl call and see if outputs the expected file

查看更多
登录 后发表回答