(PHP 4 >= 4.2.0, PHP 5, PHP 7)
openssl_csr_new — 生成一个 CSR
$dn
, resource &$privkey
[, array $configargs
[, array $extraattribs
]] ) : mixed
openssl_csr_new() 根据dn
提供的信息生成新的CSR(证书签名请求)。
Note: 必须安装有效的 openssl.cnf 以保证此函数正确运行。参考有关安装的说明以获得更多信息。
dn
在证书中使用的专有名称或主题字段。
privkey
privkey
应该被设置为由openssl_pkey_new()函数预先生成(或者以其他方式从openssl_pkey函数集中获得)的私钥。该密钥的相应公共部分将用于签署CSR.
configargs
默认的, 是通过你系统里的openssl.conf配置来初始化请求; 您可以通过设置configargs
的config_section_section项来指定配置文件部分。
您还可以通过将config键的值设置为您想要使用的文件路径来指定另一个openssl配置文件。如果在configargs
中存在下列键,它们的行为就像在openssl.conf中一样。如下表所示:
configargs 键 |
type | 等同于 openssl.conf | 描述 |
---|---|---|---|
digest_alg | string | default_md | 摘要算法或签名哈希算法,通常是 openssl_get_md_methods() 之一。 |
x509_extensions | string | x509_extensions | 选择在创建x509证书时应该使用哪些扩展 |
req_extensions | string | req_extensions | 创建CSR时,选择使用哪个扩展 |
private_key_bits | integer | default_bits | 指定应该使用多少位来生成私钥 |
private_key_type | integer | none | 选择在创建CSR时应该使用哪些扩展。可选值有
OPENSSL_KEYTYPE_DSA ,
OPENSSL_KEYTYPE_DH ,
OPENSSL_KEYTYPE_RSA 或
OPENSSL_KEYTYPE_EC .
默认值是 OPENSSL_KEYTYPE_RSA .
|
encrypt_key | boolean | encrypt_key | 是否应该对导出的密钥(带有密码短语)进行加密? |
encrypt_key_cipher | integer | none | cipher constants常量之一。 |
curve_name | string | none | 要求PHP7.1+, openssl_get_curve_names()之一。 |
config | string | N/A | 自定义 openssl.conf 文件的路径。 |
extraattribs
extraattribs
用于为CSR指定额外的配置选项。dn
和
extraattribs
都是关联数组它们的键被转换成OIDs,并应用到请求的相关部分。
成功,返回CSR 或者在失败时返回 FALSE
.
Example #1 创建一个自签名的证书
<?php
// for SSL server certificates the commonName is the domain name to be secured
// for S/MIME email certificates the commonName is the owner of the email address
// location and identification fields refer to the owner of domain or email subject to be secured
$dn = array(
"countryName" => "GB",
"stateOrProvinceName" => "Somerset",
"localityName" => "Glastonbury",
"organizationName" => "The Brain Room Limited",
"organizationalUnitName" => "PHP Documentation Team",
"commonName" => "Wez Furlong",
"emailAddress" => "wez@example.com"
);
// Generate a new private (and public) key pair
$privkey = openssl_pkey_new(array(
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
));
// Generate a certificate signing request
$csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256'));
// Generate a self-signed cert, valid for 365 days
$x509 = openssl_csr_sign($csr, null, $privkey, $days=365, array('digest_alg' => 'sha256'));
// Save your private key, CSR and self-signed cert for later use
openssl_csr_export($csr, $csrout) and var_dump($csrout);
openssl_x509_export($x509, $certout) and var_dump($certout);
openssl_pkey_export($privkey, $pkeyout, "mypassword") and var_dump($pkeyout);
// Show any errors that occurred here
while (($e = openssl_error_string()) !== false) {
echo $e . "\n";
}
?>
Example #2 在PHP 7.1+版本中创建一个自签名的ECC证书
<?php
$subject = array(
"commonName" => "docs.php.net",
);
// Generate a new private (and public) key pair
$private_key = openssl_pkey_new(array(
"private_key_type" => OPENSSL_KEYTYPE_EC,
"curve_name" => 'prime256v1',
));
// Generate a certificate signing request
$csr = openssl_csr_new($subject, $private_key, array('digest_alg' => 'sha384'));
// Generate self-signed EC cert
$x509 = openssl_csr_sign($csr, null, $private_key, $days=365, array('digest_alg' => 'sha384'));
openssl_x509_export_to_file($x509, 'ecc-cert.pem');
openssl_pkey_export_to_file($private_key, 'ecc-private.key');
?>