Erik Edgren Erik Edgren - 5 months ago 17
PHP Question

Can't create a new OpenSSL key

I want to learn OpenSSL but I'm just getting

boolean false
when I am trying to use this code:

$config = array('private_key_bits' => 512);
$privKey = openssl_pkey_new($config);

var_dump($privKey);


The OpenSSL extension is activated on my server. Have I missed something?

Answer

Use openssl_error_string() after your openssl_* function is called:

while($e = openssl_error_string() ) {
    print_r($e."\n");
}

On my Windows it outputs:

error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
error:0E064002:configuration file routines:CONF_load:system lib
error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
error:0E064002:configuration file routines:CONF_load:system lib

See the first note at this function openssl_pkey_new():

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.