diff options
author | Andris Zeila <andris.zeila@zabbix.com> | 2022-04-21 12:56:41 +0300 |
---|---|---|
committer | Andris Zeila <andris.zeila@zabbix.com> | 2022-04-21 12:56:41 +0300 |
commit | adccb91156388f346696fe46a60ff8a6475f998c (patch) | |
tree | 0807de8e3a333cb897510bf25c279bf3473d1090 /src | |
parent | 05a8627a05b2f43c4e211905f0f990907c65c1b0 (diff) | |
parent | 3302cfda444c36a780394ce6488fcb7362cfc6e0 (diff) |
.......PS. [ZBXNEXT-7616] fixed javascript md5 and sha256 functions to support binary data (including terminating zero character)
Merge in ZBX/zabbix from feature/ZBXNEXT-7616-6.0 to release/6.0
* commit '3302cfda444c36a780394ce6488fcb7362cfc6e0':
.......PS. [ZBXNEXT-7616] fixed http request functions to support binary data
........S. [ZBXNEXT-7616] fixed coding style
.D........ [ZBXNEXT-7616] added changelog entry
.......PS. [ZBXNEXT-7616] fixed javascript md5 and sha256 functions to support binary data (including terminating zero character)
Diffstat (limited to 'src')
-rw-r--r-- | src/libs/zbxcrypto/sha256crypt.c | 9 | ||||
-rw-r--r-- | src/libs/zbxembed/global.c | 68 | ||||
-rw-r--r-- | src/libs/zbxembed/httprequest.c | 4 |
3 files changed, 47 insertions, 34 deletions
diff --git a/src/libs/zbxcrypto/sha256crypt.c b/src/libs/zbxcrypto/sha256crypt.c index 0317226a747..aaf0e059bbf 100644 --- a/src/libs/zbxcrypto/sha256crypt.c +++ b/src/libs/zbxcrypto/sha256crypt.c @@ -305,6 +305,15 @@ void zbx_sha256_hash(const char *in, char *out) sha256_finish_ctx (&ctx, out); } +void zbx_sha256_hash_len(const char *in, size_t len, char *out) +{ + sha256_ctx ctx; + + sha256_init_ctx(&ctx); + sha256_process_bytes (in, len, &ctx); + sha256_finish_ctx(&ctx, out); +} + /* helper functions for sha256 hmac*/ typedef struct diff --git a/src/libs/zbxembed/global.c b/src/libs/zbxembed/global.c index d8af3902419..9dee353d98e 100644 --- a/src/libs/zbxembed/global.c +++ b/src/libs/zbxembed/global.c @@ -80,6 +80,29 @@ static duk_ret_t es_atob(duk_context *ctx) /****************************************************************************** * * + * Purpose: convert binary data to hex string * + * * + * Parameters: bin - [IN] the data to convert * + * len - [IN] the number of bytes to convert * + * out - [OUT] the output buffer (must be 2x + 1 of input len) * + * * + ******************************************************************************/ +static void es_bin_to_hex(const unsigned char *bin, size_t len, char *out) +{ + const char *hex = "0123456789abcdef"; + size_t i; + + for (i = 0; i < len; i++) + { + *out++ = hex[bin[i] >> 4]; + *out++ = hex[bin[i] & 15]; + } + + *out = '\0'; +} + +/****************************************************************************** + * * * Purpose: compute a md5 checksum * * * * Parameters: ctx - [IN] pointer to duk_context * @@ -91,31 +114,23 @@ static duk_ret_t es_atob(duk_context *ctx) ******************************************************************************/ static duk_ret_t es_md5(duk_context *ctx) { - const char *hex = "0123456789abcdef"; + const char *str; md5_state_t state; md5_byte_t hash[MD5_DIGEST_SIZE]; - int i; - char *str = NULL, *md5sum, *ptr; + char *md5sum; + duk_size_t len; - if (SUCCEED != zbx_cesu8_to_utf8(duk_require_string(ctx, 0), &str)) - return duk_error(ctx, DUK_RET_TYPE_ERROR, "cannot convert value to utf8"); + str = duk_require_lstring(ctx, 0, &len); - ptr = md5sum = (char *)zbx_malloc(NULL, MD5_DIGEST_SIZE * 2 + 1); + md5sum = (char *)zbx_malloc(NULL, MD5_DIGEST_SIZE * 2 + 1); zbx_md5_init(&state); - zbx_md5_append(&state, (const md5_byte_t *)str, strlen(str)); + zbx_md5_append(&state, (const md5_byte_t *)str, (int)len); zbx_md5_finish(&state, hash); - for (i = 0; i < MD5_DIGEST_SIZE; i++) - { - *ptr++ = hex[hash[i] >> 4]; - *ptr++ = hex[hash[i] & 15]; - } - - *ptr = '\0'; + es_bin_to_hex(hash, MD5_DIGEST_SIZE, md5sum); duk_push_string(ctx, md5sum); - zbx_free(str); zbx_free(md5sum); return 1; } @@ -133,27 +148,16 @@ static duk_ret_t es_md5(duk_context *ctx) ******************************************************************************/ static duk_ret_t es_sha256(duk_context *ctx) { - char *str = NULL, hash_res[ZBX_SHA256_DIGEST_SIZE], hash_res_stringhexes[ZBX_SHA256_DIGEST_SIZE * 2 + 1]; - int i; - - if (SUCCEED != zbx_cesu8_to_utf8(duk_require_string(ctx, 0), &str)) - return duk_error(ctx, DUK_RET_TYPE_ERROR, "cannot convert value to utf8"); + const char *str; + char hash_res[ZBX_SHA256_DIGEST_SIZE], hash_res_stringhexes[ZBX_SHA256_DIGEST_SIZE * 2 + 1]; + duk_size_t len; - zbx_sha256_hash(str, hash_res); + str = duk_require_lstring(ctx, 0, &len); - for (i = 0 ; i < ZBX_SHA256_DIGEST_SIZE; i++) - { - char z[3]; - - zbx_snprintf(z, 3, "%02x", (unsigned char)hash_res[i]); - hash_res_stringhexes[i * 2] = z[0]; - hash_res_stringhexes[i * 2 + 1] = z[1]; - } - - hash_res_stringhexes[ZBX_SHA256_DIGEST_SIZE * 2] = '\0'; + zbx_sha256_hash_len(str, len, hash_res); + es_bin_to_hex((const unsigned char *)hash_res, ZBX_SHA256_DIGEST_SIZE, hash_res_stringhexes); duk_push_string(ctx, hash_res_stringhexes); - zbx_free(str); return 1; } diff --git a/src/libs/zbxembed/httprequest.c b/src/libs/zbxembed/httprequest.c index 18892e800c7..9a56a0952ef 100644 --- a/src/libs/zbxembed/httprequest.c +++ b/src/libs/zbxembed/httprequest.c @@ -71,7 +71,7 @@ static size_t curl_write_cb(void *ptr, size_t size, size_t nmemb, void *userdata size_t r_size = size * nmemb; zbx_es_httprequest_t *request = (zbx_es_httprequest_t *)userdata; - zbx_strncpy_alloc(&request->data, &request->data_alloc, &request->data_offset, (const char *)ptr, r_size); + zbx_str_memcpy_alloc(&request->data, &request->data_alloc, &request->data_offset, (const char *)ptr, r_size); return r_size; } @@ -331,7 +331,7 @@ out: if (-1 != err_index) return duk_throw(ctx); - duk_push_string(ctx, request->data); + duk_push_lstring(ctx, request->data, request->data_offset); return 1; } |