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($privkey instanceof OpenSSLAsymmetricKey); 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--CLEAN-- 66<?php 67$key_file_name = __DIR__ . DIRECTORY_SEPARATOR . '001-tmp.key'; 68@unlink($key_file_name); 69?> 70--EXPECTF-- 71Creating private key 72Export key to file 73bool(true) 74Load key from file - array syntax 75 76Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d 77Load key using direct syntax 78 79Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d 80Load key manually and use string syntax 81 82Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d 83OK! 84