fix for coverity issue 394670 possible overflow

pull/7714/head
JacobBarthelmeh 2024-07-05 11:53:19 -06:00
parent fbdb064a4b
commit 50a7243486
3 changed files with 14 additions and 1 deletions

View File

@ -5563,7 +5563,7 @@ long wolfSSL_CTX_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL_DH* dh)
ret = wolfssl_ctx_set_tmp_dh(ctx, p, pSz, g, gSz);
}
if (ret != 1) {
if (ret != 1 && ctx != NULL) {
/* Free the allocated buffers if not assigned into SSL. */
XFREE(p, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
XFREE(g, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);

View File

@ -2940,6 +2940,14 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh)
if (ret == 0) {
/* modulus size in bytes */
modSz /= WOLFSSL_BIT_SIZE;
if ((word32)modSz < groupSz) {
WOLFSSL_MSG("DH modSz was too small");
ret = BAD_FUNC_ARG;
}
}
if (ret == 0) {
bufSz = (word32)modSz - groupSz;
/* allocate ram */

View File

@ -22272,6 +22272,11 @@ static wc_test_ret_t dh_generate_test(WC_RNG *rng)
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
/* should fail since modSz is 16 and group size is 20 */
ret = wc_DhGenerateParams(rng, 128, smallKey);
if (ret == 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
ret = wc_DhGenerateParams(rng, 2056, smallKey);
if (ret != 0)
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);