001.phpt 1.7 KB

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