Skip to content

Commit 9a79a2f

Browse files
committed
- add OPENSSL_KEYTYPE_EC constant
- openssl_pkey_get_details(), returns the key details
1 parent 2dbce54 commit 9a79a2f

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

NEWS

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ PHP NEWS
2626

2727
- Improved OpenSSL extension: (Pierre)
2828
. Added support for all supported algorithms in openssl_verify
29-
. Implement #36732 (req/x509 extensions support for openssl_csr_new and
30-
openssl_csr_sign) (ben at psc dot edu, Pierre)
31-
. Implement #28382 (openssl_x509_parse() extensions support)
29+
. Added openssl_pkey_get_details, returns the details of a key
30+
. Added x509 v3 extensions support
31+
. Added a new constant OPENSSL_KEYTYPE_EC
3232

3333
- Fixed overflow on 64bit systems in str_repeat() and wordwrap(). (Stefan E.)
3434
- Disabled CURLOPT_FOLLOWLOCATION in curl when open_basedir or safe_mode are
@@ -106,8 +106,11 @@ PHP NEWS
106106
destruction). (Ilia)
107107
- Fixed bug #37265 (Added missing safe_mode & open_basedir checks to
108108
imap_body()). (Ilia)
109+
- Implement #36732 (req/x509 extensions support for openssl_csr_new and
110+
openssl_csr_sign) (ben at psc dot edu, Pierre)
109111
- Fixed bug #35973 (Error ORA-24806 occurs when trying to fetch a NCLOB
110112
field). (Tony)
113+
- Implement #28382 (openssl_x509_parse() extensions support) (Pierre)
111114

112115
24 Jul 2006, PHP 5.2.0RC1
113116
- Updated bundled MySQL client library to version 5.0.22 in the Windows

ext/openssl/openssl.c

+64-1
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ static
6060
ZEND_ARG_PASS_INFO(1)
6161
ZEND_END_ARG_INFO();
6262

63+
/* FIXME: Use the openssl constants instead of
64+
* enum. It is now impossible to match real values
65+
* against php constants. Also sorry to break the
66+
* enum principles here, BC...
67+
*/
6368
enum php_openssl_key_type {
6469
OPENSSL_KEYTYPE_RSA,
6570
OPENSSL_KEYTYPE_DSA,
6671
OPENSSL_KEYTYPE_DH,
67-
OPENSSL_KEYTYPE_DEFAULT = OPENSSL_KEYTYPE_RSA
72+
OPENSSL_KEYTYPE_DEFAULT = OPENSSL_KEYTYPE_RSA,
73+
OPENSSL_KEYTYPE_EC = OPENSSL_KEYTYPE_DH +1
6874
};
6975

7076
enum php_openssl_cipher_type {
@@ -87,6 +93,7 @@ zend_function_entry openssl_functions[] = {
8793
PHP_FE(openssl_pkey_export_to_file, NULL)
8894
PHP_FE(openssl_pkey_get_private, NULL)
8995
PHP_FE(openssl_pkey_get_public, NULL)
96+
PHP_FE(openssl_pkey_get_details, NULL)
9097

9198
PHP_FALIAS(openssl_free_key, openssl_pkey_free, NULL)
9299
PHP_FALIAS(openssl_get_privatekey, openssl_pkey_get_private, NULL)
@@ -680,6 +687,7 @@ PHP_MINIT_FUNCTION(openssl)
680687
REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_DSA", OPENSSL_KEYTYPE_DSA, CONST_CS|CONST_PERSISTENT);
681688
#endif
682689
REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_DH", OPENSSL_KEYTYPE_DH, CONST_CS|CONST_PERSISTENT);
690+
REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_EC", OPENSSL_KEYTYPE_EC, CONST_CS|CONST_PERSISTENT);
683691

684692
/* Determine default SSL configuration file */
685693
config_filename = getenv("OPENSSL_CONF");
@@ -2216,6 +2224,61 @@ PHP_FUNCTION(openssl_pkey_get_private)
22162224

22172225
/* }}} */
22182226

2227+
/* {{{ proto resource openssl_pkey_get_details(resource key)
2228+
returns an array with the key details (bits, pkey, type)*/
2229+
PHP_FUNCTION(openssl_pkey_get_details)
2230+
{
2231+
zval *key;
2232+
EVP_PKEY *pkey;
2233+
BIO *out;
2234+
unsigned int pbio_len;
2235+
char *pbio;
2236+
long ktype;
2237+
2238+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &key) == FAILURE) {
2239+
return;
2240+
}
2241+
ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key);
2242+
if (!pkey) {
2243+
RETURN_FALSE;
2244+
}
2245+
out = BIO_new(BIO_s_mem());
2246+
PEM_write_bio_PUBKEY(out, pkey);
2247+
pbio_len = BIO_get_mem_data(out, &pbio);
2248+
2249+
array_init(return_value);
2250+
add_assoc_long(return_value, "bits", EVP_PKEY_bits(pkey));
2251+
add_assoc_stringl(return_value, "key", pbio, pbio_len, 1);
2252+
/*TODO: Use the real values once the openssl constants are used
2253+
* See the enum at the top of this file
2254+
*/
2255+
switch (EVP_PKEY_type(pkey->type)) {
2256+
case EVP_PKEY_RSA:
2257+
case EVP_PKEY_RSA2:
2258+
ktype = OPENSSL_KEYTYPE_RSA;
2259+
break;
2260+
case EVP_PKEY_DSA:
2261+
case EVP_PKEY_DSA2:
2262+
case EVP_PKEY_DSA3:
2263+
case EVP_PKEY_DSA4:
2264+
ktype = OPENSSL_KEYTYPE_DSA;
2265+
break;
2266+
case EVP_PKEY_DH:
2267+
ktype = OPENSSL_KEYTYPE_DH;
2268+
break;
2269+
case EVP_PKEY_EC:
2270+
ktype = OPENSSL_KEYTYPE_EC;
2271+
break;
2272+
default:
2273+
ktype = -1;
2274+
break;
2275+
}
2276+
add_assoc_long(return_value, "type", ktype);
2277+
2278+
BIO_free(out);
2279+
}
2280+
/* }}} */
2281+
22192282
/* }}} */
22202283

22212284
/* {{{ PKCS7 S/MIME functions */

ext/openssl/php_openssl.h

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ PHP_FUNCTION(openssl_pkey_free);
3838
PHP_FUNCTION(openssl_pkey_new);
3939
PHP_FUNCTION(openssl_pkey_export);
4040
PHP_FUNCTION(openssl_pkey_export_to_file);
41+
PHP_FUNCTION(openssl_pkey_get_details);
4142

4243
PHP_FUNCTION(openssl_sign);
4344
PHP_FUNCTION(openssl_verify);

0 commit comments

Comments
 (0)