com php-src: Adds better handling of EC keys: OPENSSL_KEYTYPE _EC: ext/openssl/openssl.c
| From: | Stanislav Malyshev | Date: | Mon, 01 Dec 2014 02:02:55 +0000 |
| Subject: | com php-src: Adds better handling of EC keys: OPENSSL_KEYTYPE _EC: ext/openssl/openssl.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-83406@lists.php.net to get a copy of this message | ||
Commit: 5d1cc7d13536d1753adce40b903d71c5c841c30a
Author: Dominic Luechinger <doldev@snowgarden.ch> Mon, 24 Nov 2014 02:04:37 +0100
Committer: Stanislav Malyshev <stas@php.net> Sun, 30 Nov 2014 18:02:55 -0800
Parents: ffb62112f86b047c7e2881dbadd2647c4729c54e
Branches: PHP-5.6 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=5d1cc7d13536d1753adce40b903d71c5c841c30a
Log:
Adds better handling of EC keys: OPENSSL_KEYTYPE_EC
When working with elliptic curve and openssl some functions are not
aware of the different handling.
Improvements:
- Fixes export of EC private keys
- Adds curve information to openssl_pkey_get_details
Conflicts:
ext/openssl/openssl.c
Changed paths:
M ext/openssl/openssl.c
Diff:
diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
index 4a4ec6f..a130443 100755
--- a/ext/openssl/openssl.c
+++ b/ext/openssl/openssl.c
@@ -3192,9 +3192,12 @@ PHP_FUNCTION(openssl_pkey_export_to_file)
{
struct php_x509_request req;
zval ** zpkey, * args = NULL;
- char * passphrase = NULL; int passphrase_len = 0;
- char * filename = NULL; int filename_len = 0;
+ char * passphrase = NULL;
+ int passphrase_len = 0;
+ char * filename = NULL;
+ int filename_len = 0;
long key_resource = -1;
+ int pem_write = 0;
EVP_PKEY * key;
BIO * bio_out = NULL;
const EVP_CIPHER * cipher;
@@ -3229,7 +3232,19 @@ PHP_FUNCTION(openssl_pkey_export_to_file)
} else {
cipher = NULL;
}
- if (PEM_write_bio_PrivateKey(bio_out, key, cipher, (unsigned char *)passphrase, passphrase_len,
NULL, NULL)) {
+
+ switch (EVP_PKEY_type(key->type)) {
+#ifdef HAVE_EVP_PKEY_EC
+ case EVP_PKEY_EC:
+ pem_write = PEM_write_bio_ECPrivateKey(bio_out, EVP_PKEY_get1_EC_KEY(key), cipher, (unsigned
char *)passphrase, passphrase_len, NULL, NULL);
+ break;
+#endif
+ default:
+ pem_write = PEM_write_bio_PrivateKey(bio_out, key, cipher, (unsigned char *)passphrase,
passphrase_len, NULL, NULL);
+ break;
+ }
+
+ if (pem_write) {
/* Success!
* If returning the output as a string, do so now */
RETVAL_TRUE;
@@ -3252,8 +3267,10 @@ PHP_FUNCTION(openssl_pkey_export)
{
struct php_x509_request req;
zval ** zpkey, * args = NULL, *out;
- char * passphrase = NULL; int passphrase_len = 0;
+ char * passphrase = NULL;
+ int passphrase_len = 0;
long key_resource = -1;
+ int pem_write = 0;
EVP_PKEY * key;
BIO * bio_out = NULL;
const EVP_CIPHER * cipher;
@@ -3284,7 +3301,19 @@ PHP_FUNCTION(openssl_pkey_export)
} else {
cipher = NULL;
}
- if (PEM_write_bio_PrivateKey(bio_out, key, cipher, (unsigned char *)passphrase, passphrase_len,
NULL, NULL)) {
+
+ switch (EVP_PKEY_type(key->type)) {
+#ifdef HAVE_EVP_PKEY_EC
+ case EVP_PKEY_EC:
+ pem_write = PEM_write_bio_ECPrivateKey(bio_out, EVP_PKEY_get1_EC_KEY(key), cipher, (unsigned
char *)passphrase, passphrase_len, NULL, NULL);
+ break;
+#endif
+ default:
+ pem_write = PEM_write_bio_PrivateKey(bio_out, key, cipher, (unsigned char *)passphrase,
passphrase_len, NULL, NULL);
+ break;
+ }
+
+ if (pem_write) {
/* Success!
* If returning the output as a string, do so now */
@@ -3455,6 +3484,42 @@ PHP_FUNCTION(openssl_pkey_get_details)
#ifdef HAVE_EVP_PKEY_EC
case EVP_PKEY_EC:
ktype = OPENSSL_KEYTYPE_EC;
+ if (pkey->pkey.ec == NULL) {
+ break;
+ }
+
+ zval ec;
+ const EC_GROUP *ec_group;
+ int nid;
+ char *crv_sn;
+ ASN1_OBJECT *obj;
+ // openssl recommends a buffer length of 80
+ char oir_buf[80];
+
+ ec_group = EC_KEY_get0_group(EVP_PKEY_get1_EC_KEY(pkey));
+
+ // Curve nid (numerical identifier) used for ASN1 mapping
+ nid = EC_GROUP_get_curve_name(ec_group);
+ if (nid == NID_undef) {
+ break;
+ }
+
+ array_init(&ec);
+
+ // Short object name
+ crv_sn = (char*) OBJ_nid2sn(nid);
+ if (crv_sn != NULL) {
+ add_assoc_string(&ec, "curve_name", crv_sn);
+ }
+
+ obj = OBJ_nid2obj(nid);
+ if (obj != NULL) {
+ int oir_len = OBJ_obj2txt(oir_buf, sizeof(oir_buf), obj, 1);
+ add_assoc_stringl(&ec, "curve_oid", (char*)oir_buf, oir_len);
+ ASN1_OBJECT_free(obj);
+ }
+
+ add_assoc_zval(return_value, "ec", &ec);
break;
#endif
default: