From 9a79a2f37cc4d2dc2e48c9a3d9f6bd19924ef48e Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Tue, 15 Aug 2006 00:38:05 +0000 Subject: [PATCH] - add OPENSSL_KEYTYPE_EC constant - openssl_pkey_get_details(), returns the key details --- NEWS | 9 ++++-- ext/openssl/openssl.c | 65 ++++++++++++++++++++++++++++++++++++++- ext/openssl/php_openssl.h | 1 + 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 07a52b0a5b..386d891e9b 100644 --- a/NEWS +++ b/NEWS @@ -26,9 +26,9 @@ PHP NEWS - Improved OpenSSL extension: (Pierre) . Added support for all supported algorithms in openssl_verify - . Implement #36732 (req/x509 extensions support for openssl_csr_new and - openssl_csr_sign) (ben at psc dot edu, Pierre) - . Implement #28382 (openssl_x509_parse() extensions support) + . Added openssl_pkey_get_details, returns the details of a key + . Added x509 v3 extensions support + . Added a new constant OPENSSL_KEYTYPE_EC - Fixed overflow on 64bit systems in str_repeat() and wordwrap(). (Stefan E.) - Disabled CURLOPT_FOLLOWLOCATION in curl when open_basedir or safe_mode are @@ -106,8 +106,11 @@ PHP NEWS destruction). (Ilia) - Fixed bug #37265 (Added missing safe_mode & open_basedir checks to imap_body()). (Ilia) +- Implement #36732 (req/x509 extensions support for openssl_csr_new and + openssl_csr_sign) (ben at psc dot edu, Pierre) - Fixed bug #35973 (Error ORA-24806 occurs when trying to fetch a NCLOB field). (Tony) +- Implement #28382 (openssl_x509_parse() extensions support) (Pierre) 24 Jul 2006, PHP 5.2.0RC1 - Updated bundled MySQL client library to version 5.0.22 in the Windows diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index 9291843cd7..ad2fca4b4a 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -60,11 +60,17 @@ static ZEND_ARG_PASS_INFO(1) ZEND_END_ARG_INFO(); +/* FIXME: Use the openssl constants instead of + * enum. It is now impossible to match real values + * against php constants. Also sorry to break the + * enum principles here, BC... + */ enum php_openssl_key_type { OPENSSL_KEYTYPE_RSA, OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, - OPENSSL_KEYTYPE_DEFAULT = OPENSSL_KEYTYPE_RSA + OPENSSL_KEYTYPE_DEFAULT = OPENSSL_KEYTYPE_RSA, + OPENSSL_KEYTYPE_EC = OPENSSL_KEYTYPE_DH +1 }; enum php_openssl_cipher_type { @@ -87,6 +93,7 @@ zend_function_entry openssl_functions[] = { PHP_FE(openssl_pkey_export_to_file, NULL) PHP_FE(openssl_pkey_get_private, NULL) PHP_FE(openssl_pkey_get_public, NULL) + PHP_FE(openssl_pkey_get_details, NULL) PHP_FALIAS(openssl_free_key, openssl_pkey_free, NULL) PHP_FALIAS(openssl_get_privatekey, openssl_pkey_get_private, NULL) @@ -680,6 +687,7 @@ PHP_MINIT_FUNCTION(openssl) REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_DSA", OPENSSL_KEYTYPE_DSA, CONST_CS|CONST_PERSISTENT); #endif REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_DH", OPENSSL_KEYTYPE_DH, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_EC", OPENSSL_KEYTYPE_EC, CONST_CS|CONST_PERSISTENT); /* Determine default SSL configuration file */ config_filename = getenv("OPENSSL_CONF"); @@ -2216,6 +2224,61 @@ PHP_FUNCTION(openssl_pkey_get_private) /* }}} */ +/* {{{ proto resource openssl_pkey_get_details(resource key) + returns an array with the key details (bits, pkey, type)*/ +PHP_FUNCTION(openssl_pkey_get_details) +{ + zval *key; + EVP_PKEY *pkey; + BIO *out; + unsigned int pbio_len; + char *pbio; + long ktype; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &key) == FAILURE) { + return; + } + ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key); + if (!pkey) { + RETURN_FALSE; + } + out = BIO_new(BIO_s_mem()); + PEM_write_bio_PUBKEY(out, pkey); + pbio_len = BIO_get_mem_data(out, &pbio); + + array_init(return_value); + add_assoc_long(return_value, "bits", EVP_PKEY_bits(pkey)); + add_assoc_stringl(return_value, "key", pbio, pbio_len, 1); + /*TODO: Use the real values once the openssl constants are used + * See the enum at the top of this file + */ + switch (EVP_PKEY_type(pkey->type)) { + case EVP_PKEY_RSA: + case EVP_PKEY_RSA2: + ktype = OPENSSL_KEYTYPE_RSA; + break; + case EVP_PKEY_DSA: + case EVP_PKEY_DSA2: + case EVP_PKEY_DSA3: + case EVP_PKEY_DSA4: + ktype = OPENSSL_KEYTYPE_DSA; + break; + case EVP_PKEY_DH: + ktype = OPENSSL_KEYTYPE_DH; + break; + case EVP_PKEY_EC: + ktype = OPENSSL_KEYTYPE_EC; + break; + default: + ktype = -1; + break; + } + add_assoc_long(return_value, "type", ktype); + + BIO_free(out); +} +/* }}} */ + /* }}} */ /* {{{ PKCS7 S/MIME functions */ diff --git a/ext/openssl/php_openssl.h b/ext/openssl/php_openssl.h index bf8c548837..789f576f66 100644 --- a/ext/openssl/php_openssl.h +++ b/ext/openssl/php_openssl.h @@ -38,6 +38,7 @@ PHP_FUNCTION(openssl_pkey_free); PHP_FUNCTION(openssl_pkey_new); PHP_FUNCTION(openssl_pkey_export); PHP_FUNCTION(openssl_pkey_export_to_file); +PHP_FUNCTION(openssl_pkey_get_details); PHP_FUNCTION(openssl_sign); PHP_FUNCTION(openssl_verify); -- 2.50.1