From 7c302f8afc1d36ec12effd0c08047baced095b46 Mon Sep 17 00:00:00 2001 From: Benjamin Kaduk Date: Thu, 21 May 2020 14:10:50 -0700 Subject: [PATCH] params: do not ignore zero-length strings Prior to this commit, if a string (or octet string) parameter was present but indicated it was zero-length, we would return success but with a NULL output value. This can be problematic in cases where there is a protocol-level distinction between parameter-absent and parameter-present-but-zero-length, which is uncommon but can happen. Since OPENSSL_malloc() returns NULL for zero-length allocation requests, make a dummy allocation for this case, to give a signal that the string parameter does exist but has zero length. Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/11920) --- crypto/params.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crypto/params.c b/crypto/params.c index 06ae1bc44f..9bccc51760 100644 --- a/crypto/params.c +++ b/crypto/params.c @@ -788,8 +788,6 @@ static int get_string_internal(const OSSL_PARAM *p, void **val, size_t max_len, if (used_len != NULL) *used_len = sz; - if (sz == 0) - return 1; if (p->data == NULL) return 0; @@ -797,12 +795,13 @@ static int get_string_internal(const OSSL_PARAM *p, void **val, size_t max_len, return 1; if (*val == NULL) { - char *const q = OPENSSL_malloc(sz); + char *const q = OPENSSL_malloc(sz > 0 ? sz : 1); if (q == NULL) return 0; *val = q; - memcpy(q, p->data, sz); + if (sz != 0) + memcpy(q, p->data, sz); return 1; } if (max_len < sz) -- 2.34.1