This is an old revision of php/openssl from 09.11.2017 20:53 edited by EvaggelosBalaskas.

<?php
    // Example CommonName
    $commonname="example.org";

    $keysize = 2048;
    $keysize = 4096;

    $days    = 365;
    $days    = 1825;

    // openssl conf
    $ssl = array(
        'private_key_bits'  => (int)$keysize,
        'private_key_type'  => 'OPENSSL_KEYTYPE_RSA',
        'digest_alg'        => 'sha256',
    );

    $alg = array(
        'digest_alg'        => 'sha256',
    );

    $sslcnf = array(
        'countryName'            => 'GR',
        'stateOrProvinceName'    => 'Attica',
        'localityName'           => 'Athens',
        'organizationName'       => 'Example Ltd',
        'organizationalUnitName' => 'The Web,
        'emailAddress'           => 'postmaster@example.org',
        'commonName'             => $commonname,
    );
    // Generates a new private key - return is a resource

    $private_key = openssl_pkey_new($ssl);

    // Gets an exportable representation of a key into a string
    // privkey: is the text output of resource: private_key
    openssl_pkey_export($private_key, $privkey);

    // Generate a certificate signing request
    $csr = openssl_csr_new($sslcnf, $private_key);

    // Self-Signed
    $req = openssl_csr_sign($csr, NULL, $private_key, $days, $alg);

    // Exports a certificate as a string
    openssl_x509_export($req, $x509);

    // Generate a certificate signing request
    $ret = array(
        'privkey' => $privkey,
        'cert'    => $x509,
    );
    echo json_encode($ret);

?>