Scipius2012 Scipius2012 -5 years ago 294
PHP Question

Error "BIO_new_file:no such file" by using "openssl_pkcs7_decrypt" in PHP

I have encrypted a file thanks to OpenSSL with the following command:

openssl smime -encrypt -in myfile.xml -out myfile.p7m -outform DER -binary publicKey.pem

Now, I would to decrypt the file "myfile.p7m" with PHP. Currently, I'm using this code but without success:

$output = "myfile.xml";
$crt = file_get_contents("mycert.crt");
$private = openssl_pkey_get_private (file_get_contents("privateKey.pem"), "password");
openssl_pkcs7_decrypt ("myfile.p7m", $output, $crt, $private);
while($error = openssl_error_string()){
echo $error.'<br />'.PHP_EOL;

At this moment, I get this error:

error:2006D080:BIO routines:BIO_new_file:no such file

I have no idea what this mean.

Can you help me?

I have already decrypted this file with success by using an OpenSSL command:

openssl smime -decrypt -in myfile.p7m -out myfile.xml -inkey
privateKey.pem -inform DER -passin pass:password


Follow the advice of Vladimir Kunschikov, I've used full path for the files. I've replaced "myfile.xml" by "file://c:/wamp/www/test/myfile.xml" and "myfile.p7m" by "file://c:/wamp/www/test/myfile.p7m". Now, I have two another errors:

error:0200107B:system library:fopen:Unknown error
error:2006D002:BIO routines:BIO_new_file:system lib

Edit 2

Thank you Giovani for your response. I replace all paths as you have directed. Now, a new error is appeared!

error:0D0D20CC:asn1 encoding routines:SMIME_read_ASN1:no content type

Answer Source

Finally, I've found a solution of this problem. Firstly, the file paths were wrong. I've resolved it by following the proposal of Giovani. For my second error, I've just encoded in base 64 my ".P7M" obtained thanks to my OPENSSL command and I've added an header:

MIME-Version: 1.0
Content-Disposition: attachment; filename="smime.p7m"
Content-Type: application/x-pkcs7-mime; smime-type=enveloped-data; name="smime.p7m"
Content-Transfer-Encoding: base64


In this way, I can decrypt correctly the P7M file.

So, I've noticed by deleting, in my OPENSSL command, the option "-outform DER", I've get a file enable to be decrypted with "openssl_pksc7_decrypt":

openssl smime -encrypt -in myfile.xml -out myfile.p7m -binary publicKey.pem
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download