openssl_free_key.phpt 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. --TEST--
  2. void openssl_free_key ( resource $key_identifier );
  3. --CREDITS--
  4. marcosptf - <marcosptf@yahoo.com.br> - @phpsp - sao paulo - br
  5. --SKIPIF--
  6. <?php
  7. if (!extension_loaded("openssl"))
  8. die("skip");
  9. if (!@openssl_pkey_new())
  10. die("skip cannot create private key");
  11. ?>
  12. --FILE--
  13. <?php
  14. echo "Creating private key\n";
  15. /* stack up some entropy; performance is not critical,
  16. * and being slow will most likely even help the test.
  17. */
  18. for ($z = "", $i = 0; $i < 1024; $i++) {
  19. $z .= $i * $i;
  20. if (function_exists("usleep"))
  21. usleep($i);
  22. }
  23. $conf = array('config' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'openssl.cnf');
  24. $privkey = openssl_pkey_new($conf);
  25. if ($privkey === false)
  26. die("failed to create private key");
  27. $passphrase = "banana";
  28. $key_file_name = tempnam(sys_get_temp_dir(), "ssl");
  29. if ($key_file_name === false)
  30. die("failed to get a temporary filename!");
  31. echo "Export key to file\n";
  32. openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf) or die("failed to export to file $key_file_name");
  33. echo "Load key from file - array syntax\n";
  34. $loaded_key = openssl_pkey_get_private(array("file://$key_file_name", $passphrase));
  35. if ($loaded_key === false)
  36. die("failed to load key using array syntax");
  37. openssl_free_key($loaded_key);
  38. echo "Load key using direct syntax\n";
  39. $loaded_key = openssl_pkey_get_private("file://$key_file_name", $passphrase);
  40. if ($loaded_key === false)
  41. die("failed to load key using direct syntax");
  42. openssl_free_key($loaded_key);
  43. echo "Load key manually and use string syntax\n";
  44. $key_content = file_get_contents($key_file_name);
  45. $loaded_key = openssl_pkey_get_private($key_content, $passphrase);
  46. if ($loaded_key === false)
  47. die("failed to load key using string syntax");
  48. openssl_free_key($loaded_key);
  49. echo "OK!\n";
  50. @unlink($key_file_name);
  51. ?>
  52. --EXPECT--
  53. Creating private key
  54. Export key to file
  55. Load key from file - array syntax
  56. Load key using direct syntax
  57. Load key manually and use string syntax
  58. OK!