Skip to content

Commit 4254c4c

Browse files
committed
Coverity warning fixes
1 parent 591728b commit 4254c4c

File tree

4 files changed

+160
-78
lines changed

4 files changed

+160
-78
lines changed

src/tls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9384,9 +9384,9 @@ static int TLSX_KeyShare_ProcessEcc_ex(WOLFSSL* ssl,
93849384
break;
93859385
#endif
93869386
default:
9387-
/* unsupported curve */
93889387
curveId = ECC_CURVE_INVALID;
93899388
WOLFSSL_ERROR_VERBOSE(ECC_PEERKEY_ERROR);
9389+
(void)curveId;
93909390
return ECC_PEERKEY_ERROR;
93919391
}
93929392

wolfcrypt/src/asn.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25028,8 +25028,10 @@ Signer* findSignerByName(Signer *list, byte *hash)
2502825028
}
2502925029
return NULL;
2503025030
}
25031+
/*declared with WOLFSSL_LOCAL due to coverity warning "routine not emitted",
25032+
likely due to inconsistencies between its declaration and definition. */
2503125033

25032-
int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm,
25034+
WOLFSSL_LOCAL int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm,
2503325035
Signer *extraCAList)
2503425036
{
2503525037
int ret = 0;

wolfcrypt/src/hmac.c

Lines changed: 142 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -152,71 +152,122 @@ static int HmacKeyInitHash(wc_HmacHash* hash, int type, void* heap, int devId)
152152
{
153153
int ret = 0;
154154

155-
switch (type) {
156-
#ifndef NO_MD5
157-
case WC_MD5:
155+
switch ((enum wc_HashType)type) {
156+
case WC_HASH_TYPE_MD5:
157+
#ifndef NO_MD5
158158
ret = wc_InitMd5_ex(&hash->md5, heap, devId);
159+
#else
160+
ret = BAD_FUNC_ARG;
161+
#endif
159162
break;
160-
#endif /* !NO_MD5 */
161163

162-
#ifndef NO_SHA
163-
case WC_SHA:
164+
case WC_HASH_TYPE_SHA:
165+
#ifndef NO_SHA
164166
ret = wc_InitSha_ex(&hash->sha, heap, devId);
167+
#else
168+
ret = BAD_FUNC_ARG;
169+
#endif
165170
break;
166-
#endif /* !NO_SHA */
167171

168-
#ifdef WOLFSSL_SHA224
169-
case WC_SHA224:
172+
case WC_HASH_TYPE_SHA224:
173+
#ifdef WOLFSSL_SHA224
170174
ret = wc_InitSha224_ex(&hash->sha224, heap, devId);
175+
#else
176+
ret = BAD_FUNC_ARG;
177+
#endif
171178
break;
172-
#endif /* WOLFSSL_SHA224 */
173179

174-
#ifndef NO_SHA256
175-
case WC_SHA256:
180+
case WC_HASH_TYPE_SHA256:
181+
#ifndef NO_SHA256
176182
ret = wc_InitSha256_ex(&hash->sha256, heap, devId);
183+
#else
184+
ret = BAD_FUNC_ARG;
185+
#endif
177186
break;
178-
#endif /* !NO_SHA256 */
179187

180-
#ifdef WOLFSSL_SHA384
181-
case WC_SHA384:
188+
case WC_HASH_TYPE_SHA384:
189+
#ifdef WOLFSSL_SHA384
182190
ret = wc_InitSha384_ex(&hash->sha384, heap, devId);
191+
#else
192+
ret = BAD_FUNC_ARG;
193+
#endif
183194
break;
184-
#endif /* WOLFSSL_SHA384 */
185-
#ifdef WOLFSSL_SHA512
186-
case WC_SHA512:
195+
196+
case WC_HASH_TYPE_SHA512:
197+
#ifdef WOLFSSL_SHA512
187198
ret = wc_InitSha512_ex(&hash->sha512, heap, devId);
199+
#else
200+
ret = BAD_FUNC_ARG;
201+
#endif
188202
break;
189-
#endif /* WOLFSSL_SHA512 */
190203

191-
#ifdef WOLFSSL_SHA3
192-
#ifndef WOLFSSL_NOSHA3_224
193-
case WC_SHA3_224:
204+
case WC_HASH_TYPE_SHA3_224:
205+
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
194206
ret = wc_InitSha3_224(&hash->sha3, heap, devId);
207+
#else
208+
ret = BAD_FUNC_ARG;
209+
#endif
195210
break;
196-
#endif
197-
#ifndef WOLFSSL_NOSHA3_256
198-
case WC_SHA3_256:
211+
212+
case WC_HASH_TYPE_SHA3_256:
213+
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
199214
ret = wc_InitSha3_256(&hash->sha3, heap, devId);
215+
#else
216+
ret = BAD_FUNC_ARG;
217+
#endif
200218
break;
201-
#endif
202-
#ifndef WOLFSSL_NOSHA3_384
203-
case WC_SHA3_384:
219+
220+
case WC_HASH_TYPE_SHA3_384:
221+
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
204222
ret = wc_InitSha3_384(&hash->sha3, heap, devId);
223+
#else
224+
ret = BAD_FUNC_ARG;
225+
#endif
205226
break;
206-
#endif
207-
#ifndef WOLFSSL_NOSHA3_512
208-
case WC_SHA3_512:
227+
228+
case WC_HASH_TYPE_SHA3_512:
229+
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
209230
ret = wc_InitSha3_512(&hash->sha3, heap, devId);
231+
#else
232+
ret = BAD_FUNC_ARG;
233+
#endif
210234
break;
211-
#endif
212-
#endif
213235

214236
#ifdef WOLFSSL_SM3
215-
case WC_SM3:
237+
case WC_HASH_TYPE_SM3:
216238
ret = wc_InitSm3(&hash->sm3, heap, devId);
217239
break;
218240
#endif
219241

242+
case WC_HASH_TYPE_NONE:
243+
case WC_HASH_TYPE_MD2:
244+
case WC_HASH_TYPE_MD4:
245+
case WC_HASH_TYPE_MD5_SHA:
246+
case WC_HASH_TYPE_BLAKE2B:
247+
case WC_HASH_TYPE_BLAKE2S:
248+
ret = BAD_FUNC_ARG;
249+
break;
250+
251+
#ifndef WOLFSSL_NOSHA512_224
252+
case WC_HASH_TYPE_SHA512_224:
253+
ret = BAD_FUNC_ARG;
254+
break;
255+
#endif
256+
#ifndef WOLFSSL_NOSHA512_256
257+
case WC_HASH_TYPE_SHA512_256:
258+
ret = BAD_FUNC_ARG;
259+
break;
260+
#endif
261+
#ifdef WOLFSSL_SHAKE128
262+
case WC_HASH_TYPE_SHAKE128:
263+
ret = BAD_FUNC_ARG;
264+
break;
265+
#endif
266+
#ifdef WOLFSSL_SHAKE256
267+
case WC_HASH_TYPE_SHAKE256:
268+
ret = BAD_FUNC_ARG;
269+
break;
270+
#endif
220271
default:
221272
ret = BAD_FUNC_ARG;
222273
break;
@@ -329,70 +380,93 @@ static int HmacKeyHashUpdate(byte macType, wc_HmacHash* hash, byte* pad)
329380
{
330381
int ret = 0;
331382

332-
switch (macType) {
333-
#ifndef NO_MD5
334-
case WC_MD5:
383+
switch ((enum wc_HashType)macType) {
384+
case WC_HASH_TYPE_MD5:
385+
#ifndef NO_MD5
335386
ret = wc_Md5Update(&hash->md5, pad, WC_MD5_BLOCK_SIZE);
387+
#endif
336388
break;
337-
#endif /* !NO_MD5 */
338389

339-
#ifndef NO_SHA
340-
case WC_SHA:
390+
case WC_HASH_TYPE_SHA:
391+
#ifndef NO_SHA
341392
ret = wc_ShaUpdate(&hash->sha, pad, WC_SHA_BLOCK_SIZE);
393+
#endif
342394
break;
343-
#endif /* !NO_SHA */
344395

345-
#ifdef WOLFSSL_SHA224
346-
case WC_SHA224:
396+
case WC_HASH_TYPE_SHA224:
397+
#ifdef WOLFSSL_SHA224
347398
ret = wc_Sha224Update(&hash->sha224, pad, WC_SHA224_BLOCK_SIZE);
399+
#endif
348400
break;
349-
#endif /* WOLFSSL_SHA224 */
350-
#ifndef NO_SHA256
351-
case WC_SHA256:
401+
402+
case WC_HASH_TYPE_SHA256:
403+
#ifndef NO_SHA256
352404
ret = wc_Sha256Update(&hash->sha256, pad, WC_SHA256_BLOCK_SIZE);
405+
#endif
353406
break;
354-
#endif /* !NO_SHA256 */
355407

356-
#ifdef WOLFSSL_SHA384
357-
case WC_SHA384:
408+
case WC_HASH_TYPE_SHA384:
409+
#ifdef WOLFSSL_SHA384
358410
ret = wc_Sha384Update(&hash->sha384, pad, WC_SHA384_BLOCK_SIZE);
411+
#endif
359412
break;
360-
#endif /* WOLFSSL_SHA384 */
361-
#ifdef WOLFSSL_SHA512
362-
case WC_SHA512:
413+
414+
case WC_HASH_TYPE_SHA512:
415+
#ifdef WOLFSSL_SHA512
363416
ret = wc_Sha512Update(&hash->sha512, pad, WC_SHA512_BLOCK_SIZE);
417+
#endif
364418
break;
365-
#endif /* WOLFSSL_SHA512 */
366419

367-
#ifdef WOLFSSL_SHA3
368-
#ifndef WOLFSSL_NOSHA3_224
369-
case WC_SHA3_224:
420+
case WC_HASH_TYPE_SHA3_224:
421+
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
370422
ret = wc_Sha3_224_Update(&hash->sha3, pad, WC_SHA3_224_BLOCK_SIZE);
423+
#endif
371424
break;
372-
#endif
373-
#ifndef WOLFSSL_NOSHA3_256
374-
case WC_SHA3_256:
425+
426+
case WC_HASH_TYPE_SHA3_256:
427+
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
375428
ret = wc_Sha3_256_Update(&hash->sha3, pad, WC_SHA3_256_BLOCK_SIZE);
429+
#endif
376430
break;
377-
#endif
378-
#ifndef WOLFSSL_NOSHA3_384
379-
case WC_SHA3_384:
431+
432+
case WC_HASH_TYPE_SHA3_384:
433+
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
380434
ret = wc_Sha3_384_Update(&hash->sha3, pad, WC_SHA3_384_BLOCK_SIZE);
435+
#endif
381436
break;
382-
#endif
383-
#ifndef WOLFSSL_NOSHA3_512
384-
case WC_SHA3_512:
437+
438+
case WC_HASH_TYPE_SHA3_512:
439+
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
385440
ret = wc_Sha3_512_Update(&hash->sha3, pad, WC_SHA3_512_BLOCK_SIZE);
441+
#endif
386442
break;
387-
#endif
388-
#endif /* WOLFSSL_SHA3 */
389443

390444
#ifdef WOLFSSL_SM3
391-
case WC_SM3:
445+
case WC_HASH_TYPE_SM3:
392446
ret = wc_Sm3Update(&hash->sm3, pad, WC_SM3_BLOCK_SIZE);
393447
break;
394448
#endif
395449

450+
/* HmacKeyHashUpdate is only ever called with a valid hash type.
451+
* Default to no-op. */
452+
case WC_HASH_TYPE_NONE:
453+
case WC_HASH_TYPE_MD2:
454+
case WC_HASH_TYPE_MD4:
455+
case WC_HASH_TYPE_MD5_SHA:
456+
case WC_HASH_TYPE_BLAKE2B:
457+
case WC_HASH_TYPE_BLAKE2S:
458+
#ifndef WOLFSSL_NOSHA512_224
459+
case WC_HASH_TYPE_SHA512_224:
460+
#endif
461+
#ifndef WOLFSSL_NOSHA512_256
462+
case WC_HASH_TYPE_SHA512_256:
463+
#endif
464+
#ifdef WOLFSSL_SHAKE128
465+
case WC_HASH_TYPE_SHAKE128:
466+
#endif
467+
#ifdef WOLFSSL_SHAKE256
468+
case WC_HASH_TYPE_SHAKE256:
469+
#endif
396470
default:
397471
break;
398472
}

wolfcrypt/src/pkcs12.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,10 @@ int wc_d2i_PKCS12_fp(const char* file, WC_PKCS12** pkcs12)
830830
wc_PKCS12_free(*pkcs12);
831831
*pkcs12 = NULL;
832832
}
833-
XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
833+
if (buf != NULL) {
834+
XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
835+
buf = NULL;
836+
}
834837

835838
WOLFSSL_LEAVE("wc_d2i_PKCS12_fp", ret);
836839

@@ -1715,7 +1718,11 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
17151718
}
17161719

17171720
/* free temporary buffer */
1718-
XFREE(buf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
1721+
/* At the other location where buf is freed */
1722+
if (buf != NULL) {
1723+
XFREE(buf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
1724+
buf = NULL; /* Set to NULL to prevent double-free at exit */
1725+
}
17191726

17201727
ci = ci->next;
17211728
WOLFSSL_MSG("Done Parsing PKCS12 Content Info Container");
@@ -1742,23 +1749,22 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
17421749
ret = 0; /* success */
17431750

17441751
exit_pk12par:
1745-
17461752
if (ret != 0) {
17471753
/* failure cleanup */
17481754
if (*pkey) {
17491755
XFREE(*pkey, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
17501756
*pkey = NULL;
17511757
}
1752-
XFREE(buf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
1753-
buf = NULL;
1754-
17551758
wc_FreeCertList(certList, pkcs12->heap);
17561759
}
1760+
/* Always cleanup buf, but check if it's already been freed */
1761+
if (buf != NULL) {
1762+
XFREE(buf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
1763+
/* No buf = NULL needed since we return immediately */
1764+
}
17571765

17581766
return ret;
17591767
}
1760-
1761-
17621768
/* Helper function to get parameters for key and cert encryptions */
17631769
static int wc_PKCS12_get_enc_params(int inAlgo, int* vPKCS, int* outAlgo,
17641770
int* blkOid, int* hmacOid)

0 commit comments

Comments
 (0)