1--TEST-- 2openssl_pkey_export() with EC key 3--SKIPIF-- 4<?php 5if (!extension_loaded("openssl")) 6 die("skip"); 7if (!defined('OPENSSL_KEYTYPE_EC')) 8 die("skip no EC available"); 9?> 10--FILE-- 11<?php 12$key = openssl_pkey_get_private('file://' . dirname(__FILE__) . '/private_ec.key'); 13var_dump($key); 14 15$config_arg = array("config" => __DIR__ . DIRECTORY_SEPARATOR . "openssl.cnf"); 16 17var_dump(openssl_pkey_export($key, $output, NULL, $config_arg)); 18echo $output; 19 20// Load the private key from the exported pem string 21$details = openssl_pkey_get_details(openssl_pkey_get_private($output)); 22var_dump(OPENSSL_KEYTYPE_EC === $details['type']); 23 24// Export key with passphrase 25openssl_pkey_export($key, $output, 'passphrase', $config_arg); 26 27$details = openssl_pkey_get_details(openssl_pkey_get_private($output, 'passphrase')); 28var_dump(OPENSSL_KEYTYPE_EC === $details['type']); 29 30// Read public key 31$pKey = openssl_pkey_get_public('file://' . dirname(__FILE__) . '/public_ec.key'); 32var_dump($pKey); 33// The details are the same for a public or private key, expect the private key parameter 'd 34$detailsPKey = openssl_pkey_get_details($pKey); 35var_dump(array_diff_assoc($details['ec'], $detailsPKey['ec'])); 36 37// Export to file 38$tempname = tempnam(sys_get_temp_dir(), 'openssl_ec'); 39var_dump(openssl_pkey_export_to_file($key, $tempname, NULL, $config_arg)); 40$details = openssl_pkey_get_details(openssl_pkey_get_private('file://' . $tempname)); 41var_dump(OPENSSL_KEYTYPE_EC === $details['type']); 42var_dump(is_resource($key)); 43// Clean the temporary file 44@unlink($tempname); 45?> 46--EXPECTF-- 47resource(%d) of type (OpenSSL key) 48bool(true) 49-----BEGIN EC PRIVATE KEY-----%a-----END EC PRIVATE KEY----- 50bool(true) 51bool(true) 52resource(%d) of type (OpenSSL key) 53array(1) { 54 ["d"]=> 55 string(32) "%a" 56} 57bool(true) 58bool(true) 59bool(true) 60