<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.openssl.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.openssl-csr-new.php',
    1 => 'openssl_csr_new',
    2 => 'Generates a CSR',
  ),
  'up' => 
  array (
    0 => 'ref.openssl.php',
    1 => 'OpenSSL Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.openssl-csr-get-subject.php',
    1 => 'openssl_csr_get_subject',
  ),
  'next' => 
  array (
    0 => 'function.openssl-csr-sign.php',
    1 => 'openssl_csr_sign',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/openssl/functions/openssl-csr-new.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.openssl-csr-new" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_csr_new</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.2.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">openssl_csr_new</span> &mdash; <span class="dc-title">Generates a <abbr title="Certificate Signing Request">CSR</abbr></span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-csr-new-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_csr_new</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$distinguished_names</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="attribute"><a href="class.sensitiveparameter.php">#[\SensitiveParameter]</a> </span><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="class.opensslasymmetrickey.php" class="type OpenSSLAsymmetricKey">OpenSSLAsymmetricKey</a></span></span> <code class="parameter reference">&$private_key</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.array.php" class="type array">array</a></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.array.php" class="type array">array</a></span></span> <code class="parameter">$extra_attributes</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="class.opensslcertificatesigningrequest.php" class="type OpenSSLCertificateSigningRequest">OpenSSLCertificateSigningRequest</a></span>|<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_csr_new()</strong></span> generates a new <abbr title="Certificate Signing Request">CSR</abbr>
   based on the information provided by <code class="parameter">distinguished_names</code>.
  </p>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
 <span class="simpara">
  Occorre un <var class="filename">openssl.cnf</var> valido installato perché
  questa funzione venga eseguita correttamente.
  Vedere le note nella <a href="openssl.installation.php" class="link">sezione sull&#039;installazione
  </a> per ulteriori informazioni.
 </span>
</p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-csr-new-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">distinguished_names</code></dt>
     <dd>
      <p class="para">
       The Distinguished Name or subject fields to be included in the
       certificate. The <code class="parameter">distinguished_names</code> 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).
      </p>
     </dd>
    
    
     <dt><code class="parameter">private_key</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">private_key</code> should be set to a private key that
       was previously generated by <span class="function"><a href="function.openssl-pkey-new.php" class="function">openssl_pkey_new()</a></span> (or
       otherwise obtained from the other openssl_pkey family of functions), or
       <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> variable. If its value is <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> variable, a new private key is
       generated based on the supplied <code class="parameter">options</code> and
       assigned to supplied variable. The corresponding public portion of the
       key will be used to sign the <abbr title="Certificate Signing Request">CSR</abbr>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">options</code></dt>
     <dd>
      <p class="para">
       By default, the information in your system <code class="literal">openssl.conf</code>
       is used to initialize the request; you can specify a configuration file
       section by setting the <code class="literal">config_section_section</code> key in
       <code class="parameter">options</code>.  You can also specify an alternative
       OpenSSL configuration file by setting the value of the
       <code class="literal">config</code> key to the path of the file you want to use.
       The following keys, if present in <code class="parameter">options</code>
       behave as their equivalents in the <code class="literal">openssl.conf</code>, as
       listed in the table below.
       <table class="doctable table">
        <caption><strong>Configuration overrides</strong></caption>
        
         <thead>
          <tr>
           <th><code class="parameter">options</code> key</th>
           <th>type</th>
           <th><code class="literal">openssl.conf</code> equivalent</th>
           <th>description</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>digest_alg</td>
           <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td>
           <td>default_md</td>
           <td>Digest method or signature hash, usually one of <span class="function"><a href="function.openssl-get-md-methods.php" class="function">openssl_get_md_methods()</a></span></td>
          </tr>

          <tr>
           <td>x509_extensions</td>
           <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td>
           <td>x509_extensions</td>
           <td>Selects which extensions should be used when creating an x509
           certificate</td>
          </tr>

          <tr>
           <td>req_extensions</td>
           <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td>
           <td>req_extensions</td>
           <td>Selects which extensions should be used when creating a <abbr title="Certificate Signing Request">CSR</abbr></td>
          </tr>

          <tr>
           <td>private_key_bits</td>
           <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
           <td>default_bits</td>
           <td>Specifies how many bits should be used to generate a private key</td>
          </tr>

          <tr>
           <td>private_key_type</td>
           <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
           <td>none</td>
           <td>Specifies the type of private key to create.  This can be one
            of <strong><code><a href="openssl.key-types.php#constant.openssl-keytype-dsa">OPENSSL_KEYTYPE_DSA</a></code></strong>,
            <strong><code><a href="openssl.key-types.php#constant.openssl-keytype-dh">OPENSSL_KEYTYPE_DH</a></code></strong>,
            <strong><code><a href="openssl.key-types.php#constant.openssl-keytype-rsa">OPENSSL_KEYTYPE_RSA</a></code></strong> or
            <strong><code><a href="openssl.key-types.php#constant.openssl-keytype-ec">OPENSSL_KEYTYPE_EC</a></code></strong>.
            The default value is <strong><code><a href="openssl.key-types.php#constant.openssl-keytype-rsa">OPENSSL_KEYTYPE_RSA</a></code></strong>.
           </td>
          </tr>

          <tr>
           <td>encrypt_key</td>
           <td><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></td>
           <td>encrypt_key</td>
           <td>Should an exported key (with passphrase) be encrypted?</td>
          </tr>

          <tr>
           <td>encrypt_key_cipher</td>
           <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
           <td>none</td>
           <td>
            One of <a href="openssl.ciphers.php" class="link">cipher constants</a>.
           </td>
          </tr>

          <tr>
           <td>curve_name</td>
           <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td>
           <td>none</td>
           <td>
            One of <span class="function"><a href="function.openssl-get-curve-names.php" class="function">openssl_get_curve_names()</a></span>.
           </td>
          </tr>

          <tr>
           <td>config</td>
           <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td>
           <td>N/A</td>
           <td>
            Path to your own alternative openssl.conf file.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>
    
    
     <dt><code class="parameter">extra_attributes</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">extra_attributes</code> is used to specify additional
       attributes for the <abbr title="Certificate Signing Request">CSR</abbr>. It is an associative arrays
       where the keys are converted to OIDs and applied as
       <abbr title="Certificate Signing Request">CSR</abbr> attributes.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-csr-new-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   Returns the <abbr title="Certificate Signing Request">CSR</abbr> on success, <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if
   <abbr title="Certificate Signing Request">CSR</abbr> creation is successful but signing
   fails o <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> in caso di fallimento.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.openssl-csr-new-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versione</th>
      <th>Descrizione</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.4.0</td>
      <td>
       The <code class="parameter">distinguished_names</code> associative array now supports arrays as values,
       allowing multiple values to be specified for a single attribute.
      </td>
     </tr>

     <tr>
      <td>8.4.0</td>
      <td>
       The <code class="parameter">extra_attributes</code> parameter now correctly sets the CSR attributes,
       rather than modifying the subject&#039;s Distinguished Name as it previously did incorrectly.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       On success, this function returns an <span class="classname"><a href="class.opensslcertificatesigningrequest.php" class="classname">OpenSSLCertificateSigningRequest</a></span> instance now;
       previously, a <a href="language.types.resource.php" class="link">resource</a> of type <code class="literal">OpenSSL X.509 CSR</code> was returned.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">private_key</code> accepts an <span class="classname"><a href="class.opensslasymmetrickey.php" class="classname">OpenSSLAsymmetricKey</a></span> instance now;
       previously, a <a href="language.types.resource.php" class="link">resource</a> of type <code class="literal">OpenSSL key</code> was accepted.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       <code class="parameter">options</code> now also supports <code class="literal">curve_name</code>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-csr-new-examples">
  <h3 class="title">Esempi</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Creating a self-signed certificate</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// for SSL server certificates the commonName is the domain name to be secured<br />// for S/MIME email certificates the commonName is the owner of the email address<br />// location and identification fields refer to the owner of domain or email subject to be secured<br /></span><span style="color: #0000BB">$dn </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">"countryName" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"GB"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"stateOrProvinceName" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Somerset"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"localityName" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Glastonbury"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"organizationName" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"The Brain Room Limited"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"organizationalUnitName" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"PHP Documentation Team"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"commonName" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Wez Furlong"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"emailAddress" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"wez@example.com"<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Generate a new private (and public) key pair<br /></span><span style="color: #0000BB">$privkey </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_pkey_new</span><span style="color: #007700">(array(<br />    </span><span style="color: #DD0000">"private_key_bits" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2048</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"private_key_type" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">OPENSSL_KEYTYPE_RSA</span><span style="color: #007700">,<br />));<br /><br /></span><span style="color: #FF8000">// Generate a certificate signing request<br /></span><span style="color: #0000BB">$csr </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_csr_new</span><span style="color: #007700">(</span><span style="color: #0000BB">$dn</span><span style="color: #007700">, </span><span style="color: #0000BB">$privkey</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'digest_alg' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Generate a self-signed cert, valid for 365 days<br /></span><span style="color: #0000BB">$x509 </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_csr_sign</span><span style="color: #007700">(</span><span style="color: #0000BB">$csr</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$privkey</span><span style="color: #007700">, </span><span style="color: #0000BB">$days</span><span style="color: #007700">=</span><span style="color: #0000BB">365</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'digest_alg' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Save your private key, CSR and self-signed cert for later use<br /></span><span style="color: #0000BB">openssl_csr_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$csr</span><span style="color: #007700">, </span><span style="color: #0000BB">$csrout</span><span style="color: #007700">) and </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$csrout</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">openssl_x509_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$x509</span><span style="color: #007700">, </span><span style="color: #0000BB">$certout</span><span style="color: #007700">) and </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$certout</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">openssl_pkey_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$privkey</span><span style="color: #007700">, </span><span style="color: #0000BB">$pkeyout</span><span style="color: #007700">, </span><span style="color: #DD0000">"mypassword"</span><span style="color: #007700">) and </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$pkeyout</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Show any errors that occurred here<br /></span><span style="color: #007700">while ((</span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_error_string</span><span style="color: #007700">()) !== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$e </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   
   <div class="example" id="example-2">
    <p><strong>Example #2 Creating a self-signed ECC certificate (as of PHP 7.1.0)</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$subject </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">"commonName" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"docs.php.net"</span><span style="color: #007700">,<br />);<br /><br /></span><span style="color: #FF8000">// Generate a new private (and public) key pair<br /></span><span style="color: #0000BB">$private_key </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_pkey_new</span><span style="color: #007700">(array(<br />    </span><span style="color: #DD0000">"private_key_type" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">OPENSSL_KEYTYPE_EC</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"curve_name" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'prime256v1'</span><span style="color: #007700">,<br />));<br /><br /></span><span style="color: #FF8000">// Generate a certificate signing request<br /></span><span style="color: #0000BB">$csr </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_csr_new</span><span style="color: #007700">(</span><span style="color: #0000BB">$subject</span><span style="color: #007700">, </span><span style="color: #0000BB">$private_key</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'digest_alg' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'sha384'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Generate self-signed EC cert<br /></span><span style="color: #0000BB">$x509 </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_csr_sign</span><span style="color: #007700">(</span><span style="color: #0000BB">$csr</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$private_key</span><span style="color: #007700">, </span><span style="color: #0000BB">$days</span><span style="color: #007700">=</span><span style="color: #0000BB">365</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'digest_alg' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'sha384'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">openssl_x509_export_to_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$x509</span><span style="color: #007700">, </span><span style="color: #DD0000">'ecc-cert.pem'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">openssl_pkey_export_to_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$private_key</span><span style="color: #007700">, </span><span style="color: #DD0000">'ecc-private.key'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   
   
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-csr-new-seealso">
  <h3 class="title">Vedere anche:</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.openssl-csr-sign.php" class="function" rel="rdfs-seeAlso">openssl_csr_sign()</a> - Sign a CSR with another certificate (or itself) and generate a certificate</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>