PHP8 openssl_csr_sign

2024-03-11 16:52 更新

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

openssl_csr_sign — 用另一個證書簽署 CSR(或者本身)并且生成一個證書

說明

openssl_csr_sign(
    OpenSSLCertificateSigningRequest|string $csr,
    OpenSSLCertificate|string|null $ca_certificate,
    OpenSSLAsymmetricKey|OpenSSLCertificate|array|string $private_key,
    int $days,
    ?array $options = null,
    int $serial = 0
): OpenSSLCertificate|false

openssl_csr_sign() 從給定的 CSR 生成 x509 證書。

注意: 必須安裝有效的 openssl.cnf 以保證此函數(shù)正確運(yùn)行。參考有關(guān)安裝的說明以獲得更多信息。

參數(shù) 

csr

由 openssl_csr_new() 函數(shù)生成的 CSR。 也可以是由類似file://path/to/csr格式指定的指向 PEM 編碼的 CSR 路徑,或者是一個由openssl_csr_export()函數(shù)生成的字符串。

ca_certificate

生成的證書將由ca_certificate簽名。 如果ca_certificate 為 null, 生成的證書將是自簽名證書。

private_key

private_key 是 ca_certificate 證書對應(yīng)的私鑰。

days

days 指定生成的證書在幾天內(nèi)有效的時間長度。

options

可以通過 options 確定 CSR 簽名。 查看 openssl_csr_new() 方法獲取 options 的更多相關(guān)信息。

serial

可選的發(fā)行證書編號。如果沒有指定默認(rèn)值為0.

返回值 

成功時返回 OpenSSLCertificate,失敗則返回 false。

更新日志 

版本說明
8.0.0成功時,此函數(shù)現(xiàn)在返回 OpenSSLCertificate 實例;之前返回類型 OpenSSL X.509 的 resource。
8.0.0csr 現(xiàn)在接受 OpenSSLCertificateSigningRequest 實例;之前接受類型 OpenSSL X.509 CSR 的 resource。
8.0.0ca_certificate 現(xiàn)在接受 OpenSSLCertificate 實例;之前接受類型 OpenSSL X.509 的 resource。
8.0.0private_key 現(xiàn)在接受 OpenSSLAsymmetricKey 或 OpenSSLCertificate 實例;之前接受類型 OpenSSL key 或 OpenSSL X.509 的 resource。

示例 

示例 #1 openssl_csr_sign() 示例——簽署 CSR(如何實現(xiàn)自己的 CA)

<?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";
}
?>


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號