openssl_csr_new
(PHP 4 >= 4.2.0, PHP 5, PHP 7)
openssl_csr_new — Generates a CSR
Description
$dn
, resource &$privkey
[, array $configargs
[, array $extraattribs
]] ) : mixed
openssl_csr_new() generates a new CSR (Certificate Signing Request)
based on the information provided by dn
.
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
-
dn
-
The Distinguished Name or subject fields to be used in the certificate.
-
privkey
-
privkey
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). The corresponding public portion of the key will be used to sign the CSR. -
configargs
-
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 of
configargs
. 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 inconfigargs
behave as their equivalents in the openssl.conf, as listed in the table below.Configuration overrides configargs
keytype openssl.conf equivalent description digest_alg string default_md Digest method or signature hash, usually one of openssl_get_md_methods() x509_extensions string x509_extensions Selects which extensions should be used when creating an x509 certificate req_extensions string req_extensions Selects which extensions should be used when creating a CSR private_key_bits integer default_bits Specifies how many bits should be used to generate a private key private_key_type integer none Specifies the type of private key to create. This can be one of OPENSSL_KEYTYPE_DSA
,OPENSSL_KEYTYPE_DH
,OPENSSL_KEYTYPE_RSA
orOPENSSL_KEYTYPE_EC
. The default value isOPENSSL_KEYTYPE_RSA
.encrypt_key boolean encrypt_key Should an exported key (with passphrase) be encrypted? encrypt_key_cipher integer none One of cipher constants. curve_name string none One of openssl_get_curve_names(). config string N/A Path to your own alternative openssl.conf file. -
extraattribs
-
extraattribs
is used to specify additional configuration options for the CSR. Bothdn
andextraattribs
are associative arrays whose keys are converted to OIDs and applied to the relevant part of the request.
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)
<?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');
?>
See Also
- openssl_csr_sign() - Sign a CSR with another certificate (or itself) and generate a certificate
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.openssl-csr-new.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.