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