Skip to content

Commit 17e9866

Browse files
committed
Adds test cases for openssl EC improvements
1 parent e795675 commit 17e9866

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

ext/openssl/tests/027.phpt

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

ext/openssl/tests/028.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
openssl_pkey_get_details() with EC key
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("openssl")) die("skip");
6+
if (!defined('OPENSSL_KEYTYPE_EC')) die("skip no EC available");
7+
?>
8+
--FILE--
9+
<?php
10+
$key = openssl_pkey_get_private('file://' . dirname(__FILE__) . '/private_ec.key');
11+
12+
print_r(openssl_pkey_get_details($key));
13+
?>
14+
--EXPECTF--
15+
Array
16+
(
17+
[bits] => 256
18+
[key] => -----BEGIN PUBLIC KEY-----%a
19+
-----END PUBLIC KEY-----
20+
21+
[ec] => Array
22+
(
23+
[curve_name] => prime256v1
24+
[curve_oid] => 1.2.840.10045.3.1.7
25+
)
26+
27+
[type] => 3
28+
)

ext/openssl/tests/private_ec.key

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MHcCAQEEILPkqoeyM7XgwYkuSj3077lrsrfWJK5LqMolv+m2oOjZoAoGCCqGSM49
3+
AwEHoUQDQgAEPq4hbIWHvB51rdWr8ejrjWo4qVNWVugYFtPg/xLQw0mHkIPZ4DvK
4+
sqOTOnMoezkbSmVVMuwz9flvnqHGmQvmug==
5+
-----END EC PRIVATE KEY-----

ext/openssl/tests/public_ec.key

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEPq4hbIWHvB51rdWr8ejrjWo4qVNW
3+
VugYFtPg/xLQw0mHkIPZ4DvKsqOTOnMoezkbSmVVMuwz9flvnqHGmQvmug==
4+
-----END PUBLIC KEY-----

0 commit comments

Comments
 (0)