xref: /PHP-7.3/ext/openssl/tests/001.phpt (revision b746e698)
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$conf = array('config' => __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf');
13$privkey = openssl_pkey_new($conf);
14
15if ($privkey === false) {
16    die("failed to create private key");
17}
18
19$passphrase = "banana";
20$key_file_name = __DIR__ . '/001-tmp.key';
21if ($key_file_name === false) {
22    die("failed to get a temporary filename!");
23}
24
25echo "Export key to file\n";
26
27if (!openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf)) {
28    die("failed to export to file $key_file_name");
29}
30var_dump(is_resource($privkey));
31
32echo "Load key from file - array syntax\n";
33
34$loaded_key = openssl_pkey_get_private(array("file://$key_file_name", $passphrase));
35
36if ($loaded_key === false) {
37    die("failed to load key using array syntax");
38}
39
40openssl_pkey_free($loaded_key);
41
42echo "Load key using direct syntax\n";
43
44$loaded_key = openssl_pkey_get_private("file://$key_file_name", $passphrase);
45
46if ($loaded_key === false) {
47    die("failed to load key using direct syntax");
48}
49
50openssl_pkey_free($loaded_key);
51
52echo "Load key manually and use string syntax\n";
53
54$key_content = file_get_contents($key_file_name);
55$loaded_key = openssl_pkey_get_private($key_content, $passphrase);
56
57if ($loaded_key === false) {
58    die("failed to load key using string syntax");
59}
60openssl_pkey_free($loaded_key);
61
62echo "OK!\n";
63
64?>
65--EXPECT--
66Creating private key
67Export key to file
68bool(true)
69Load key from file - array syntax
70Load key using direct syntax
71Load key manually and use string syntax
72OK!
73--CLEAN--
74<?php
75$key_file_name = __DIR__ . DIRECTORY_SEPARATOR . '001-tmp.key';
76@unlink($key_file_name);
77?>
78