2017-10-05 144 views
0

这是我第一次使用openssl签署证书。继续打上面的错误并尝试realpath()并追加file://但仍然无法获得openssl签名配置文件。我不明白这是如何工作的。任何见解,将不胜感激。Openssl_pkcs7_sign():错误打开文件

编辑1:我不确定哪个文件是有问题的文件。错误消息不够具体。有没有办法告诉?

代码和下面截图:

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; 
    } 
} 
+0

随意PM我,如果你碰到同样的问题。 :) – f0rfun

回答

0
  1. 如果不是在

    openssl_pkcs7_sign($ mobileConfig,$ tmpMobileConfig,$ certFile中,阵列($ p键, “”)用于删除不需要的领域,阵列());

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