【问题标题】:Openssl_pkcs7_sign(): error opening fileopenssl_pkcs7_sign():打开文件出错
【发布时间】:2017-10-05 09:22:18
【问题描述】:

这是我第一次使用 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;
    }
}

【问题讨论】:

  • 如果您遇到同样的问题,请随时 PM 我。 :)

标签: php openssl php-openssl


【解决方案1】:
  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());
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多