From 86b21185506fd789184b035cab221f083e92ffd6 Mon Sep 17 00:00:00 2001 From: Elms Date: Mon, 19 Oct 2020 09:52:16 -0700 Subject: [PATCH 1/2] Address some cppcheck issues --- src/bio.c | 10 +++++----- src/internal.c | 14 +++++++++----- wolfcrypt/src/integer.c | 8 +++++--- wolfcrypt/src/sp_dsp32.c | 19 ++++++++++--------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/bio.c b/src/bio.c index 657c4040b..02ac5b353 100644 --- a/src/bio.c +++ b/src/bio.c @@ -571,22 +571,22 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len) } /* write bios */ - if (bio && bio->type == WOLFSSL_BIO_BIO) { + if (bio->type == WOLFSSL_BIO_BIO) { ret = wolfSSL_BIO_BIO_write(bio, data, len); } - if (bio && bio->type == WOLFSSL_BIO_MEMORY) { + if (bio->type == WOLFSSL_BIO_MEMORY) { ret = wolfSSL_BIO_MEMORY_write(bio, data, len); } #ifndef NO_FILESYSTEM - if (bio && bio->type == WOLFSSL_BIO_FILE) { + if (bio->type == WOLFSSL_BIO_FILE) { ret = (int)XFWRITE(data, 1, len, (XFILE)bio->ptr); } #endif #ifndef WOLFCRYPT_ONLY - if (bio && bio->type == WOLFSSL_BIO_SSL) { + if (bio->type == WOLFSSL_BIO_SSL) { /* already got eof, again is error */ if (front->eof) { ret = SSL_FATAL_ERROR; @@ -596,7 +596,7 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len) } } - if (bio && bio->type == WOLFSSL_BIO_MD) { + if (bio->type == WOLFSSL_BIO_MD) { if (bio->next != NULL) { /* data passing through MD BIO */ ret = wolfSSL_BIO_MD_write(bio, data, len); } diff --git a/src/internal.c b/src/internal.c index 8a5cc24e0..2981dbd3d 100644 --- a/src/internal.c +++ b/src/internal.c @@ -9984,7 +9984,13 @@ int DoVerifyCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, int ret, ProcPeerCertArgs* args) { int verify_ok = 0, use_cb = 0; - void *heap = (ssl != NULL) ? ssl->heap : cm->heap; + void *heap; + + if (cm == NULL) { + return BAD_FUNC_ARG; + } + + heap = (ssl != NULL) ? ssl->heap : cm->heap; /* Determine if verify was okay */ if (ret == 0) { @@ -10189,7 +10195,7 @@ int DoVerifyCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, int ret, } #ifndef NO_WOLFSSL_CM_VERIFY /* non-zero return code indicates failure override */ - if ((cm != NULL) && (cm->verifyCallback != NULL)) { + if (cm->verifyCallback != NULL) { store->userCtx = cm; if (cm->verifyCallback(verify_ok, store)) { if (ret != 0) { @@ -11037,9 +11043,7 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, /* check if fatal error */ if (args->verifyErr) { args->fatal = 1; - if (ret == 0) { - ret = args->lastErr; - } + ret = args->lastErr; } else { args->fatal = 0; diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 9793624f1..33d741c61 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -2269,13 +2269,15 @@ int mp_exptmod_base_2(mp_int * X, mp_int * P, mp_int * Y) redux = fast_mp_montgomery_reduce; } else #endif - { #ifdef BN_MP_MONTGOMERY_REDUCE_C + { /* use slower baseline Montgomery method */ redux = mp_montgomery_reduce; -#else - return MP_VAL; + } #endif + + if (redux == NULL) { + return MP_VAL; } #ifdef WOLFSSL_SMALL_STACK diff --git a/wolfcrypt/src/sp_dsp32.c b/wolfcrypt/src/sp_dsp32.c index beb25c5cf..0b31f25af 100644 --- a/wolfcrypt/src/sp_dsp32.c +++ b/wolfcrypt/src/sp_dsp32.c @@ -118,15 +118,18 @@ static int sp_ecc_point_new_ex(void* heap, sp_point* sp, sp_point** p) { int ret = MP_OKAY; (void)heap; -#if defined(WOLFSSL_SP_SMALL) || defined(WOLFSSL_SMALL_STACK) - (void)sp; - *p = (sp_point*)XMALLOC(sizeof(sp_point), heap, DYNAMIC_TYPE_ECC); -#else - *p = sp; -#endif + if (p == NULL) { ret = MEMORY_E; + } else { +#if defined(WOLFSSL_SP_SMALL) || defined(WOLFSSL_SMALL_STACK) + (void)sp; + *p = (sp_point*)XMALLOC(sizeof(sp_point), heap, DYNAMIC_TYPE_ECC); +#else + *p = sp; +#endif } + return ret; } @@ -4533,14 +4536,12 @@ void wc_ecc_fp_free(void) AEEResult wolfSSL_open(const char *uri, remote_handle64 *handle) { - void *tptr; /* can be any value or ignored, rpc layer doesn't care * also ok * *handle = 0; * *handle = 0xdeadc0de; */ - tptr = (void *)malloc(1); - *handle = (remote_handle64)tptr; + *handle = (remote_handle64)malloc(1); return 0; } From c3dba3f9af2d78213e6ab38f38e99934a345bb50 Mon Sep 17 00:00:00 2001 From: Elms Date: Wed, 21 Oct 2020 09:59:39 -0700 Subject: [PATCH 2/2] Add additional checks to sp_ecc_point_new --- wolfcrypt/src/sp_dsp32.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/src/sp_dsp32.c b/wolfcrypt/src/sp_dsp32.c index 0b31f25af..38d6dc721 100644 --- a/wolfcrypt/src/sp_dsp32.c +++ b/wolfcrypt/src/sp_dsp32.c @@ -117,7 +117,6 @@ static const sp_point p256_base __attribute__((aligned(128))) = { static int sp_ecc_point_new_ex(void* heap, sp_point* sp, sp_point** p) { int ret = MP_OKAY; - (void)heap; if (p == NULL) { ret = MEMORY_E; @@ -125,8 +124,16 @@ static int sp_ecc_point_new_ex(void* heap, sp_point* sp, sp_point** p) #if defined(WOLFSSL_SP_SMALL) || defined(WOLFSSL_SMALL_STACK) (void)sp; *p = (sp_point*)XMALLOC(sizeof(sp_point), heap, DYNAMIC_TYPE_ECC); + if (*p == NULL) { + ret = MEMORY_E; + } #else - *p = sp; + (void)heap; + if (sp == NULL) { + ret = MEMORY_E; + } else { + *p = sp; + } #endif }