Openssl_pkcs7_sign():错误打开文件

时间:2017-10-05 09:22:18

标签: php openssl php-openssl

这是我第一次使用openssl签署证书。继续遇到上述错误并尝试使用realpath()并附加file://但仍无法通过openssl对配置文件进行签名。我不明白这是如何工作的。任何见解都将不胜感激。

Edit1:我不确定哪个文件有问题。错误消息不够具体。有办法告诉吗?

以下代码和屏幕截图:

function signProfile()
{
    $filename = "./template.mobileconfig";
    $filename = realpath($filename);
    $outFilename = $filename . ".tmp";
    $pkey = dirname(__FILE__) . "/PteKey.key";
    $pkey = realpath($pkey);
    $certFile = dirname(__FILE__) . "/CertToSign.crt";
    $certFile = realpath($certFile);

    // try signing the plain XML profile
    if (openssl_pkcs7_sign($filename, $outFilename, 'file://'.$certFile, array('file://'.$pkey, ""), array(), 0, "")) 
    {
        // get the data back from the filesystem
        $signedString = file_get_contents($outFilename);
        // trim the fat
        $trimmedString = preg_replace('/(.+\n)+\n/', '', $signedString, 1);
        // convert to binary (DER)
        $decodedString = base64_decode($trimmedString);
        // write the file back to the filesystem (using the filename originally given)
        $fh = fopen($filename, 'w');
        fwrite($fh, $decodedString);
        fclose($fh);
        // delete the temporary file
        unlink($outFilename);
        return TRUE;
    } 
    else
    {
        return FALSE;
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 如果未在

    中使用,请删除不需要的字段

    openssl_pkcs7_sign($ mobileConfig,$ tmpMobileConfig,$ certFile,array($ pkey,“”),array());

  2. 确保正确提供文件路径。

    require_once('variables.php'); //stores abs path to $tmpMobileConfig/$pteKeyPath/$CertToSignPath
    $prepend = "file://";
    $mobileConfig = realpath("./template.mobileconfig");
    $pkey = $prepend . $pteKeyPath;
    $pkey = str_replace('\\', '/', $pkey);
    $certFile = $prepend . $CertToSignPath;
    $certFile = str_replace('\\', '/', $certFile);
    $isSignedCert = openssl_pkcs7_sign($mobileConfig, $tmpMobileConfig, $certFile, array($pkey, ""), array());