diff options
author | Jakub Zelenka <bukka@php.net> | 2016-03-06 11:44:01 +0000 |
---|---|---|
committer | Jakub Zelenka <bukka@php.net> | 2016-07-17 17:43:34 +0100 |
commit | d73735a750c7623d410f462159d30683a0714ef9 (patch) | |
tree | e0355c47bacc6963f52b2964c9bfa8ee5a7d44b9 | |
parent | 1a4e910e8d6f635eb7b3e6ee56c095558f47a8e0 (diff) | |
download | php-git-d73735a750c7623d410f462159d30683a0714ef9.tar.gz |
Move and use opaque pkey in openssl_dh_compute_key
-rw-r--r-- | ext/openssl/openssl.c | 89 |
1 files changed, 47 insertions, 42 deletions
diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index 0308ee7078..4e4065b345 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -4514,6 +4514,53 @@ PHP_FUNCTION(openssl_pkey_get_details) } /* }}} */ +/* {{{ proto string openssl_dh_compute_key(string pub_key, resource dh_key) + Computes shared secret for public value of remote DH key and local DH key */ +PHP_FUNCTION(openssl_dh_compute_key) +{ + zval *key; + char *pub_str; + size_t pub_len; + DH *dh; + EVP_PKEY *pkey; + BIGNUM *pub; + zend_string *data; + int len; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "sr", &pub_str, &pub_len, &key) == FAILURE) { + return; + } + if ((pkey = (EVP_PKEY *)zend_fetch_resource(Z_RES_P(key), "OpenSSL key", le_key)) == NULL) { + RETURN_FALSE; + } + if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DH) { + RETURN_FALSE; + } + dh = EVP_PKEY_get0_DH(pkey); + if (dh == NULL) { + RETURN_FALSE; + } + + PHP_OPENSSL_CHECK_SIZE_T_TO_INT(pub_len, pub_key); + pub = BN_bin2bn((unsigned char*)pub_str, (int)pub_len, NULL); + + data = zend_string_alloc(DH_size(dh), 0); + len = DH_compute_key((unsigned char*)ZSTR_VAL(data), pub, dh); + + if (len >= 0) { + ZSTR_LEN(data) = len; + ZSTR_VAL(data)[len] = 0; + RETVAL_STR(data); + } else { + php_openssl_store_errors(); + zend_string_release(data); + RETVAL_FALSE; + } + + BN_free(pub); +} +/* }}} */ + /* }}} */ /* {{{ proto string openssl_pbkdf2(string password, string salt, long key_length, long iterations [, string digest_method = "sha1"]) @@ -6186,48 +6233,6 @@ PHP_FUNCTION(openssl_cipher_iv_length) /* }}} */ -/* {{{ proto string openssl_dh_compute_key(string pub_key, resource dh_key) - Computes shared secret for public value of remote DH key and local DH key */ -PHP_FUNCTION(openssl_dh_compute_key) -{ - zval *key; - char *pub_str; - size_t pub_len; - EVP_PKEY *pkey; - BIGNUM *pub; - zend_string *data; - int len; - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "sr", &pub_str, &pub_len, &key) == FAILURE) { - return; - } - if ((pkey = (EVP_PKEY *)zend_fetch_resource(Z_RES_P(key), "OpenSSL key", le_key)) == NULL) { - RETURN_FALSE; - } - if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH || !pkey->pkey.dh) { - RETURN_FALSE; - } - - PHP_OPENSSL_CHECK_SIZE_T_TO_INT(pub_len, pub_key); - pub = BN_bin2bn((unsigned char*)pub_str, (int)pub_len, NULL); - - data = zend_string_alloc(DH_size(pkey->pkey.dh), 0); - len = DH_compute_key((unsigned char*)ZSTR_VAL(data), pub, pkey->pkey.dh); - - if (len >= 0) { - ZSTR_LEN(data) = len; - ZSTR_VAL(data)[len] = 0; - RETVAL_STR(data); - } else { - php_openssl_store_errors(); - zend_string_release(data); - RETVAL_FALSE; - } - - BN_free(pub); -} -/* }}} */ - /* {{{ proto string openssl_random_pseudo_bytes(integer length [, &bool returned_strong_result]) Returns a string of the length specified filled with random pseudo bytes */ PHP_FUNCTION(openssl_random_pseudo_bytes) |