I'm receiving binary data in php as certificate content and I want to download it to browser . I'm getting this message always when opening it with certificate viewer :
hex(19).pfx Could not display 'hex(19).pfx' Reason: Unrecognized or unsupported data.
this binary data is correct , I put it in a file on the server immediately and it made a valid certificate .
I think the problem is in two where :
output of command
exec('ssh root@192.168.0.137 "echo '.$bindata.' | xxd -p -r | tr -d \' \' "',$output);
while $bindata
is coming from converting certificate pfx file after making it in bash with xxd -p
or it's in the header , there is a lost or addition :
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=hex.pfx");
header('Content-Length: '. strlen($output[0]));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $output[0];
exit();
what's the wrong ?
solution is to remove spaces of the output of xxd
in bash :
res=`xxd -p $exportedkey`
echo "${res//[[:space:]]/}"
in php :
$hex = hex2bin($result);
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=hex.pfx");
header('Content-Length: '. strlen($hex));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $hex;
exit();
hope it helps at least someone