终于成功了。
$dn = array(
"countryName" => "XX",
"stateOrProvinceName" => "Location",
"localityName" => "Local",
"organizationName" => "Sample Organization",
"organizationalUnitName" => "Organizational Unit",
"commonName" => "Sample",
"emailAddress" => "[email protected]"
);
// 生成新的私钥(和公钥)对
$privkey = openssl_pkey_new(array(
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
));
// 生成证书签署请求
$csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha512'));
// 生成自签名证书,有效期为 365 天
$x509 = openssl_csr_sign($csr, null, $privkey, $days=365, array('digest_alg' => 'sha512'));
// 保存您的私钥、CSR 和自签名证书以备后用
openssl_csr_export($csr, $csrout) and var_dump($csrout); // .csr
openssl_x509_export($x509, $certout) and var_dump($certout); // .crt.pem
openssl_pkey_export($privkey, $pkeyout, "user_defined_password") and var_dump($pkeyout); // .key.pem
if(openssl_cms_sign ( "file_to_sign", "Sample.p7m" , $x509 , $privkey, null , 0 , 0 , null )){
echo "SIGNED SUCCESSFULLY! Sample.p7m created... \r\n";
}
else
{
echo "SIGNED FAILED!\r\n";
}