下载证书二进制数据时,结果为无效证书


when downloading certficate binary data , result is invalid certificate

我在php中接收二进制数据作为证书内容,我想将其下载到浏览器。当使用证书查看器打开它时,我总是收到此消息:

hex(19).pfx
Could not display 'hex(19).pfx'
Reason:   Unrecognized or unsupported data.

这个二进制数据是正确的,我把它放在服务器上的一个文件中,它产生了一个有效的证书。

我认为问题出在两个方面:

  • 命令输出

    exec('ssh root@192.168.0.137 "echo '.$bindata.' | xxd -p -r | tr -d '''n'' "',$output);

$bindata是来自转换证书pfx文件后,使它在bash与xxd -p

  • 或者它在标题中,有一个丢失或添加:

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

解决方法是删除xxd

输出中的空格

:

res=`xxd -p $exportedkey`
echo "${res//[[:space:]]/}"

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

希望它至少能帮助到一些人