1--TEST-- 2openssl_pkey_export() with EC key 3--EXTENSIONS-- 4openssl 5--SKIPIF-- 6<?php 7if (!defined('OPENSSL_KEYTYPE_EC')) 8 die("skip no EC available"); 9?> 10--FILE-- 11<?php 12$key = openssl_pkey_get_private('file://' . __DIR__ . '/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://' . __DIR__ . '/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($key instanceof OpenSSLAsymmetricKey); 43// Clean the temporary file 44@unlink($tempname); 45?> 46--EXPECTF-- 47object(OpenSSLAsymmetricKey)#%d (0) { 48} 49bool(true) 50-----BEGIN PRIVATE KEY----- 51MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgs+Sqh7IzteDBiS5K 52PfTvuWuyt9YkrkuoyiW/6bag6NmhRANCAAQ+riFshYe8HnWt1avx6OuNajipU1ZW 536BgW0+D/EtDDSYeQg9ngO8qyo5M6cyh7ORtKZVUy7DP1+W+eocaZC+a6 54-----END PRIVATE KEY----- 55bool(true) 56bool(true) 57object(OpenSSLAsymmetricKey)#%d (0) { 58} 59array(1) { 60 ["d"]=> 61 string(32) "%a" 62} 63bool(true) 64bool(true) 65bool(true) 66