Skip to content

Commit d5016d4

Browse files
authored
Merge pull request #7714 from JacobBarthelmeh/coverity
Coverity issues reported
2 parents 7ef424b + f8eb0c3 commit d5016d4

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

src/ssl_load.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5264,7 +5264,7 @@ int wolfSSL_SetTmpDH(WOLFSSL* ssl, const unsigned char* p, int pSz,
52645264
ret = wolfssl_set_tmp_dh(ssl, pAlloc, pSz, gAlloc, gSz);
52655265
}
52665266

5267-
if (ret != 1) {
5267+
if (ret != 1 && ssl != NULL) {
52685268
/* Free the allocated buffers if not assigned into SSL. */
52695269
XFREE(pAlloc, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
52705270
XFREE(gAlloc, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
@@ -5496,7 +5496,7 @@ long wolfSSL_set_tmp_dh(WOLFSSL *ssl, WOLFSSL_DH *dh)
54965496
ret = wolfssl_set_tmp_dh(ssl, p, pSz, g, gSz);
54975497
}
54985498

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

5566-
if (ret != 1) {
5566+
if (ret != 1 && ctx != NULL) {
55675567
/* Free the allocated buffers if not assigned into SSL. */
55685568
XFREE(p, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
55695569
XFREE(g, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);

wolfcrypt/src/dh.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,6 +2940,14 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh)
29402940
if (ret == 0) {
29412941
/* modulus size in bytes */
29422942
modSz /= WOLFSSL_BIT_SIZE;
2943+
2944+
if ((word32)modSz < groupSz) {
2945+
WOLFSSL_MSG("DH modSz was too small");
2946+
ret = BAD_FUNC_ARG;
2947+
}
2948+
}
2949+
2950+
if (ret == 0) {
29432951
bufSz = (word32)modSz - groupSz;
29442952

29452953
/* allocate ram */

wolfcrypt/src/pkcs7.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8634,6 +8634,8 @@ int wc_PKCS7_PadData(byte* in, word32 inSz, byte* out, word32 outSz,
86348634
return BAD_FUNC_ARG;
86358635

86368636
padSz = wc_PKCS7_GetPadSize(inSz, blockSz);
8637+
if (padSz < 0)
8638+
return padSz;
86378639

86388640
if (outSz < (inSz + padSz))
86398641
return BAD_FUNC_ARG;

wolfcrypt/src/rsa.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,17 @@ static int _ifc_pairwise_consistency_test(RsaKey* key, WC_RNG* rng)
673673

674674
int wc_CheckRsaKey(RsaKey* key)
675675
{
676-
DECL_MP_INT_SIZE_DYN(tmp, mp_bitsused(&key->n), RSA_MAX_SIZE);
677676
#ifdef WOLFSSL_SMALL_STACK
678677
WC_RNG *rng = NULL;
679678
#else
680679
WC_RNG rng[1];
681680
#endif
682681
int ret = 0;
682+
DECL_MP_INT_SIZE_DYN(tmp, (key)? mp_bitsused(&key->n) : 0, RSA_MAX_SIZE);
683+
684+
if (key == NULL) {
685+
return BAD_FUNC_ARG;
686+
}
683687

684688
#ifdef WOLFSSL_CAAM
685689
/* can not perform these checks on an encrypted key */
@@ -711,11 +715,6 @@ int wc_CheckRsaKey(RsaKey* key)
711715
ret = MP_INIT_E;
712716
}
713717

714-
if (ret == 0) {
715-
if (key == NULL)
716-
ret = BAD_FUNC_ARG;
717-
}
718-
719718
if (ret == 0)
720719
ret = _ifc_pairwise_consistency_test(key, rng);
721720

wolfcrypt/test/test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22272,6 +22272,11 @@ static wc_test_ret_t dh_generate_test(WC_RNG *rng)
2227222272
if (ret != 0)
2227322273
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
2227422274

22275+
/* should fail since modSz is 16 and group size is 20 */
22276+
ret = wc_DhGenerateParams(rng, 128, smallKey);
22277+
if (ret == 0)
22278+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
22279+
2227522280
ret = wc_DhGenerateParams(rng, 2056, smallKey);
2227622281
if (ret != 0)
2227722282
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
@@ -49466,6 +49471,11 @@ static wc_test_ret_t pkcs7signed_run_vectors(
4946649471

4946749472
XMEMSET(out, 0, outSz);
4946849473

49474+
/* test inner pad size error with block size being 0 */
49475+
ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 0);
49476+
if (ret > 0)
49477+
ERROR_OUT(-1, out);
49478+
4946949479
ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
4947049480
if (ret < 0)
4947149481
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);

wolfssl/wolfcrypt/sp_int.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,11 @@ typedef struct sp_ecc_ctx {
695695
#define sp_clamp(a) \
696696
do { \
697697
int ii; \
698-
for (ii = (int)(a)->used - 1; ii >= 0 && (a)->dp[ii] == 0; ii--) { \
698+
if ((a)->used > 0) { \
699+
for (ii = (int)(a)->used - 1; ii >= 0 && (a)->dp[ii] == 0; ii--) { \
700+
} \
701+
(a)->used = (unsigned int)ii + 1; \
699702
} \
700-
(a)->used = (unsigned int)ii + 1; \
701703
} while (0)
702704

703705
/* Check the compiled and linked math implementation are the same.

0 commit comments

Comments
 (0)