9

I need to use openssl within my php project, so I created a test php page using openssl. However, I keep getting these errors and I am not sure why. openssl is enabled.

Warning: openssl_pkey_export() [function.openssl-pkey-export]: cannot get key from parameter 1 in C:\wamp\www\opensslsample\index.php on line 18

Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in C:\wamp\www\opensslsample\index.php on line 21

<?php
 //echo phpinfo();

   $privateKey = openssl_pkey_new(array(
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
));

openssl_pkey_export($privateKey, $privkey,"123");

$pubkey=openssl_pkey_get_details($privateKey);
$pubkey=$pubkey["key"];
?>
Community
  • 1
  • 1
thejus_r
  • 291
  • 2
  • 5
  • 17
  • Take a look at the following explanation. It might help: http://stackoverflow.com/questions/16866994/openssl-wont-create-private-keys/22847076#22847076 – tabone Apr 09 '14 at 14:42

5 Answers5

35

This may help if you are on windows:

  1. Click on the START button
  2. Click on CONTROL PANEL
  3. Click on SYSTEM AND SECURITY
  4. Click on SYSTEM
  5. Click on ADVANCED SYSTEM SETTINGS
  6. Click on ENVIRONMENT VARIABLES
  7. Under "System Variables" click on "NEW"
  8. Enter the "Variable name" OPENSSL_CONF
  9. Enter the "Variable value". My is - C:\wamp\bin\apache\Apache2.2.17\conf\openssl.cnf
  10. Click "OK" and close all the windows and RESTART your computer.

The OPENSSL should be correctly working.

Ansari
  • 361
  • 3
  • 3
  • 4
    The 10th step is crucial. I tried a lot of solutions that I found on stackoverflow and other blogs, and none of them mentioned that you need to RESTART your computer. I spend 3 hours trying make this work, but only worked after I restarted my PC. – Fabrizio Valencia Jul 25 '19 at 22:34
  • @Ansari It worked! I am using PHP built in web server on Windows10, so I added the following to system vriables: C:\php\extras\ssl\openssl.cnf, then I restarted windows, that is all, thanks. – Isabella Dec 01 '19 at 12:02
  • 10. just restart Apache? – executor Jan 05 '23 at 04:31
  • great it worked! for those who're working on xampp your env variable should look like ```C:\xampp\apache\conf\openssl.cnf``` instead – Adnane Kadri Jan 08 '23 at 12:56
1

Check openssl_error_string. My guess is that your openssl.cnf file is missing or something.

Alternatively, you could use phpseclib, a pure PHP RSA implementation, to generate keys. eg.

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();

extract($rsa->createKey());

echo "$privatekey<br />$publickey";
?>
neubert
  • 15,947
  • 24
  • 120
  • 212
1

I extract from phpseclib to fix it in jose-jwt lib and it worked, you need this several changes:

<?php

$config = array();
$config['config'] = dirname(__FILE__) . '/openssl.cnf';

$privateKey = openssl_pkey_new(array(
  'private_key_bits' => 1024,
  'private_key_type' => OPENSSL_KEYTYPE_RSA,
) + $config);

openssl_pkey_export($privateKey, $privkey, "123", $config);

And this minimalist config file:

# minimalist openssl.cnf file for use with phpseclib

HOME            = .
RANDFILE        = $ENV::HOME/.rnd

[ v3_ca ]

Called openssl.cnf. With all this it should work well.

z3nth10n
  • 2,341
  • 2
  • 25
  • 49
0

The PHP needs to find your openssl.cnf. The best way to achieve this is to add the directory location of it in the PATH environment variable.

jairhumberto
  • 535
  • 1
  • 11
  • 31
0

You can workaround by using ParagonIE\EasyRSA\KeyPair. Take a look at https://github.com/paragonie/EasyRSA.