2002-03-26 08:03:11 +08:00
|
|
|
--TEST--
|
|
|
|
OpenSSL private key functions
|
|
|
|
--SKIPIF--
|
2018-09-17 01:16:42 +08:00
|
|
|
<?php
|
|
|
|
if (!extension_loaded("openssl")) die("skip");
|
|
|
|
if (!@openssl_pkey_new()) die("skip cannot create private key");
|
2005-12-28 01:17:11 +08:00
|
|
|
?>
|
2002-03-26 08:03:11 +08:00
|
|
|
--FILE--
|
|
|
|
<?php
|
|
|
|
echo "Creating private key\n";
|
|
|
|
|
2018-06-21 23:38:19 +08:00
|
|
|
$conf = array('config' => __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf');
|
2017-03-13 15:17:42 +08:00
|
|
|
$privkey = openssl_pkey_new($conf);
|
2002-03-26 08:03:11 +08:00
|
|
|
|
2018-06-21 23:38:19 +08:00
|
|
|
if ($privkey === false) {
|
|
|
|
die("failed to create private key");
|
|
|
|
}
|
2002-03-26 08:03:11 +08:00
|
|
|
|
|
|
|
$passphrase = "banana";
|
2018-06-21 23:38:19 +08:00
|
|
|
$key_file_name = __DIR__ . '/001-tmp.key';
|
|
|
|
if ($key_file_name === false) {
|
|
|
|
die("failed to get a temporary filename!");
|
|
|
|
}
|
2002-03-26 08:03:11 +08:00
|
|
|
|
|
|
|
echo "Export key to file\n";
|
|
|
|
|
2018-06-21 23:38:19 +08:00
|
|
|
if (!openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf)) {
|
|
|
|
die("failed to export to file $key_file_name");
|
|
|
|
}
|
2017-10-31 00:05:00 +08:00
|
|
|
var_dump(is_resource($privkey));
|
2002-03-26 08:03:11 +08:00
|
|
|
|
|
|
|
echo "Load key from file - array syntax\n";
|
|
|
|
|
|
|
|
$loaded_key = openssl_pkey_get_private(array("file://$key_file_name", $passphrase));
|
|
|
|
|
2018-06-21 23:38:19 +08:00
|
|
|
if ($loaded_key === false) {
|
|
|
|
die("failed to load key using array syntax");
|
|
|
|
}
|
2002-03-26 08:03:11 +08:00
|
|
|
|
|
|
|
openssl_pkey_free($loaded_key);
|
|
|
|
|
|
|
|
echo "Load key using direct syntax\n";
|
|
|
|
|
|
|
|
$loaded_key = openssl_pkey_get_private("file://$key_file_name", $passphrase);
|
|
|
|
|
2018-06-21 23:38:19 +08:00
|
|
|
if ($loaded_key === false) {
|
|
|
|
die("failed to load key using direct syntax");
|
|
|
|
}
|
2002-03-26 08:03:11 +08:00
|
|
|
|
|
|
|
openssl_pkey_free($loaded_key);
|
|
|
|
|
|
|
|
echo "Load key manually and use string syntax\n";
|
|
|
|
|
2002-03-28 08:56:19 +08:00
|
|
|
$key_content = file_get_contents($key_file_name);
|
2002-03-26 08:03:11 +08:00
|
|
|
$loaded_key = openssl_pkey_get_private($key_content, $passphrase);
|
|
|
|
|
2018-06-21 23:38:19 +08:00
|
|
|
if ($loaded_key === false) {
|
|
|
|
die("failed to load key using string syntax");
|
|
|
|
}
|
2002-03-26 08:03:11 +08:00
|
|
|
openssl_pkey_free($loaded_key);
|
|
|
|
|
|
|
|
echo "OK!\n";
|
|
|
|
|
|
|
|
?>
|
|
|
|
--EXPECT--
|
|
|
|
Creating private key
|
|
|
|
Export key to file
|
2017-10-31 00:05:00 +08:00
|
|
|
bool(true)
|
2002-03-26 08:03:11 +08:00
|
|
|
Load key from file - array syntax
|
|
|
|
Load key using direct syntax
|
|
|
|
Load key manually and use string syntax
|
|
|
|
OK!
|
2018-06-21 23:38:19 +08:00
|
|
|
--CLEAN--
|
|
|
|
<?php
|
|
|
|
$key_file_name = __DIR__ . DIRECTORY_SEPARATOR . '001-tmp.key';
|
|
|
|
@unlink($key_file_name);
|
2018-10-15 10:33:09 +08:00
|
|
|
?>
|