openssl_csr_new() generates a new CSR
based on the information provided by distinguished_names.
Note:
You need to have a valid openssl.cnf installed for
this function to operate correctly.
See the notes under the installation
section for more information.
Parameters
distinguished_names
The Distinguished Name or subject fields to be included in the
certificate. The distinguished_names is an
associative array where the keys represent the attribute names of
Distinguished Names and the values can either be strings (for single
value) or arrays (if multiple values need to be set).
private_key
private_key should be set to a private key that
was previously generated by openssl_pkey_new() (or
otherwise obtained from the other openssl_pkey family of functions), or
null variable. If its value is null variable, a new private key is
generated based on the supplied options and
assigned to supplied variable. The corresponding public portion of the
key will be used to sign the CSR.
options
By default, the information in your system openssl.conf
is used to initialize the request; you can specify a configuration file
section by setting the config_section_section key in
options. You can also specify an alternative
OpenSSL configuration file by setting the value of the
config key to the path of the file you want to use.
The following keys, if present in options
behave as their equivalents in the openssl.conf, as
listed in the table below.
Digest method or signature hash, usually one of
openssl_get_md_methods(). The special value
"null" is used for signature algorithms that do
not accept a digest algorithm as a parameter.
extra_attributes is used to specify additional
attributes for the CSR. It is an associative arrays
where the keys are converted to OIDs and applied as
CSR attributes.
Return Values
Returns the CSR on success, true if
CSR creation is successful but signing
fails or false on failure.
Changelog
Version
Description
8.4.0
The distinguished_names associative array now supports arrays as values,
allowing multiple values to be specified for a single attribute.
8.4.0
The extra_attributes parameter now correctly sets the CSR attributes,
rather than modifying the subject's Distinguished Name as it previously did incorrectly.
private_key accepts an OpenSSLAsymmetricKey instance now;
previously, a resource of type OpenSSL key was accepted.
7.1.0
options now also supports curve_name.
Examples
Example #1 Creating a self-signed certificate
<?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 Creating a self-signed ECC certificate (as of PHP 7.1.0)