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