001.phpt 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --TEST--
  2. OpenSSL private key functions
  3. --EXTENSIONS--
  4. openssl
  5. --SKIPIF--
  6. <?php
  7. if (!@openssl_pkey_new()) die("skip cannot create private key");
  8. ?>
  9. --FILE--
  10. <?php
  11. echo "Creating private key\n";
  12. $conf = array('config' => __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf');
  13. $privkey = openssl_pkey_new($conf);
  14. if ($privkey === false) {
  15. die("failed to create private key");
  16. }
  17. $passphrase = "banana";
  18. $key_file_name = __DIR__ . '/001-tmp.key';
  19. if ($key_file_name === false) {
  20. die("failed to get a temporary filename!");
  21. }
  22. echo "Export key to file\n";
  23. if (!openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf)) {
  24. die("failed to export to file $key_file_name");
  25. }
  26. var_dump($privkey instanceof OpenSSLAsymmetricKey);
  27. echo "Load key from file - array syntax\n";
  28. $loaded_key = openssl_pkey_get_private(array("file://$key_file_name", $passphrase));
  29. if ($loaded_key === false) {
  30. die("failed to load key using array syntax");
  31. }
  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. }
  38. openssl_pkey_free($loaded_key);
  39. echo "Load key manually and use string syntax\n";
  40. $key_content = file_get_contents($key_file_name);
  41. $loaded_key = openssl_pkey_get_private($key_content, $passphrase);
  42. if ($loaded_key === false) {
  43. die("failed to load key using string syntax");
  44. }
  45. openssl_pkey_free($loaded_key);
  46. echo "OK!\n";
  47. ?>
  48. --CLEAN--
  49. <?php
  50. $key_file_name = __DIR__ . DIRECTORY_SEPARATOR . '001-tmp.key';
  51. @unlink($key_file_name);
  52. ?>
  53. --EXPECTF--
  54. Creating private key
  55. Export key to file
  56. bool(true)
  57. Load key from file - array syntax
  58. Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
  59. Load key using direct syntax
  60. Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
  61. Load key manually and use string syntax
  62. Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
  63. OK!