xref: /PHP-8.2/ext/openssl/tests/001.phpt (revision 74859783)
1--TEST--
2OpenSSL private key functions
3--EXTENSIONS--
4openssl
5--SKIPIF--
6<?php
7if (!@openssl_pkey_new()) die("skip cannot create private key");
8?>
9--FILE--
10<?php
11echo "Creating private key\n";
12
13$conf = array('config' => __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf');
14$privkey = openssl_pkey_new($conf);
15
16if ($privkey === false) {
17    die("failed to create private key");
18}
19
20$passphrase = "banana";
21$key_file_name = __DIR__ . '/001-tmp.key';
22if ($key_file_name === false) {
23    die("failed to get a temporary filename!");
24}
25
26echo "Export key to file\n";
27
28if (!openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf)) {
29    die("failed to export to file $key_file_name");
30}
31var_dump($privkey instanceof OpenSSLAsymmetricKey);
32
33echo "Load key from file - array syntax\n";
34
35$loaded_key = openssl_pkey_get_private(array("file://$key_file_name", $passphrase));
36
37if ($loaded_key === false) {
38    die("failed to load key using array syntax");
39}
40
41openssl_pkey_free($loaded_key);
42
43echo "Load key using direct syntax\n";
44
45$loaded_key = openssl_pkey_get_private("file://$key_file_name", $passphrase);
46
47if ($loaded_key === false) {
48    die("failed to load key using direct syntax");
49}
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?>
66--CLEAN--
67<?php
68$key_file_name = __DIR__ . DIRECTORY_SEPARATOR . '001-tmp.key';
69@unlink($key_file_name);
70?>
71--EXPECTF--
72Creating private key
73Export key to file
74bool(true)
75Load key from file - array syntax
76
77Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
78Load key using direct syntax
79
80Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
81Load key manually and use string syntax
82
83Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
84OK!
85