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    usleep($i);
22}
23
24$conf = array('config' => __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf');
25$privkey = openssl_pkey_new($conf);
26
27if ($privkey === false)
28    die("failed to create private key");
29
30$passphrase = "banana";
31$key_file_name = tempnam(sys_get_temp_dir(), "ssl");
32if ($key_file_name === false)
33    die("failed to get a temporary filename!");
34
35echo "Export key to file\n";
36
37openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf) or die("failed to export to file $key_file_name");
38
39echo "Load key from file - array syntax\n";
40
41$loaded_key = openssl_pkey_get_private(array("file://$key_file_name", $passphrase));
42
43if ($loaded_key === false)
44    die("failed to load key using array syntax");
45
46openssl_free_key($loaded_key);
47
48echo "Load key using direct syntax\n";
49
50$loaded_key = openssl_pkey_get_private("file://$key_file_name", $passphrase);
51
52if ($loaded_key === false)
53    die("failed to load key using direct syntax");
54
55openssl_free_key($loaded_key);
56
57echo "Load key manually and use string syntax\n";
58
59$key_content = file_get_contents($key_file_name);
60$loaded_key = openssl_pkey_get_private($key_content, $passphrase);
61
62if ($loaded_key === false)
63    die("failed to load key using string syntax");
64
65openssl_free_key($loaded_key);
66
67echo "OK!\n";
68
69@unlink($key_file_name);
70?>
71--EXPECTF--
72Creating private key
73Export key to file
74Load key from file - array syntax
75
76Deprecated: Function openssl_free_key() is deprecated in %s on line %d
77Load key using direct syntax
78
79Deprecated: Function openssl_free_key() is deprecated in %s on line %d
80Load key manually and use string syntax
81
82Deprecated: Function openssl_free_key() is deprecated in %s on line %d
83OK!
84