2
votes

I would like to know how could I sign a file in php. I need to sign an XML file. When I'm executing this piece of code, it gives me the following error:

Warning: openssl_sign(): supplied key param cannot be coerced into a private key in C:\xampp\htdocs\test\index.php on line 35

Warning: openssl_free_key() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\index.php on line 38

The code is this one:

$data->save("test.xml");

$signature;
$pkeyid = openssl_pkey_get_public("./public.cer");
$path = 'test.xml';
openssl_sign($path, $signature, $pkeyid);
openssl_free_key($pkeyid);

Fixed: Ok, so I had a misconception about the fact that I should sign data with a public certificate, and also I didn't load the data properly. I changed it and It worked :) The function that I've used is this one:

$pkeyid = openssl_pkey_get_private(file_get_contents('./private.key'));

1
You've got your concepts wrong. You sign data with a private key, you verify data with a public key - President James K. Polk

1 Answers

1
votes

The path "./public.cer" is most likely the error. Try to use an absolute path instead. You're second warning is telling you that $pkeyid is boolean which is exactly what openssl_pkey_get_public() returns on error.