首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

openssl_csr_sign

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

openssl_csr_sign - 用另一个证书(或其本身)签署 CSR 并生成证书

描述

代码语言:javascript
复制
resource openssl_csr_sign ( mixed $csr , mixed $cacert , mixed $priv_key , int $days [, array $configargs [, int $serial = 0 ]] )

openssl_csr_sign()从给定的 CSR 生成一个 x509 证书资源。

注意:您需要安装有效的 openssl.cnf 才能使此功能正常运行。有关更多信息,请参阅安装部分说明。

参数

csr

先前由 openssl_csr_new()生成的 CSR。当指定为 file:// path / to / csr 或由 openssl_csr_export()生成的导出字符串时,它也可以是 PEM 编码 CSR 的路径。

cacert

生成的证书将由签名cacert。如果cacertNULL,则生成的证书将是自签名证书。

priv_key

priv_keycacert对应的私钥。

days

days 指定生成的证书有效的时间长度,以天为单位。

configargs

你可以通过微调 CSR 签名configargs。有关更多信息,请参阅 openssl_csr_new()configargs

serial

可选的已颁发证书的序列号。如果未指定,它将默认为0。

返回值

返回FALSE失败时成功的 x509 证书资源。

例子

示例#1 openssl_csr_sign()示例 - 签名 CSR(如何实现您自己的 CA)

代码语言:javascript
复制
<?php
// Let's assume that this script is set to receive a CSR that has
// been pasted into a textarea from another page
$csrdata = $_POST["CSR"];

// We will sign the request using our own "certificate authority"
// certificate.  You can use any certificate to sign another, but
// the process is worthless unless the signing certificate is trusted
// by the software/users that will deal with the newly signed certificate

// We need our CA cert and its private key
$cacert = "file://path/to/ca.crt";
$privkey = array("file://path/to/ca.key", "your_ca_key_passphrase");

$usercert = openssl_csr_sign($csrdata, $cacert, $privkey, 365, array('digest_alg'=>'sha256') );

// Now display the generated certificate so that the user can
// copy and paste it into their local configuration (such as a file
// to hold the certificate for their SSL server)
openssl_x509_export($usercert, $certout);
echo $certout;

// Show any errors that occurred here
while (($e = openssl_error_string()) !== false) {
    echo $e . "\n";
}
?>

← openssl_csr_new

openssl_decrypt →

扫码关注腾讯云开发者

领取腾讯云代金券