Skip to content

Commit 6f7e5d0

Browse files
committed
Use size_t in wolfSSL_strnstr and reject negative indices in mp_get_digit
1 parent 3e0679e commit 6f7e5d0

9 files changed

Lines changed: 29 additions & 24 deletions

File tree

src/conf.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ WOLFSSL_TXT_DB *wolfSSL_TXT_DB_read(WOLFSSL_BIO *in, int num)
9090
char** fieldPtr = NULL;
9191
int fieldPtrIdx = 0;
9292
char* fieldCheckIdx = NULL;
93-
lineEnd = XSTRNSTR(idx, "\n", (unsigned int)(bufEnd - idx));
93+
lineEnd = XSTRNSTR(idx, "\n", (size_t)(bufEnd - idx));
9494
if (!lineEnd)
9595
lineEnd = bufEnd;
9696
if (idx == lineEnd) /* empty line */
@@ -852,11 +852,11 @@ int wolfSSL_NCONF_load(WOLFSSL_CONF *conf, const char *file, long *eline)
852852
idx = buf;
853853
bufEnd = buf + bufLen;
854854
while (idx < bufEnd) {
855-
char* lineEnd = XSTRNSTR(idx, "\n", (unsigned int)(bufEnd - idx));
855+
char* lineEnd = XSTRNSTR(idx, "\n", (size_t)(bufEnd - idx));
856856
char* maxIdx;
857857
if (!lineEnd)
858858
lineEnd = bufEnd; /* Last line in file */
859-
maxIdx = XSTRNSTR(idx, "#", (unsigned int)(lineEnd - idx));
859+
maxIdx = XSTRNSTR(idx, "#", (size_t)(lineEnd - idx));
860860
if (!maxIdx)
861861
maxIdx = lineEnd;
862862
line++;

src/ssl_load.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,21 +2677,21 @@ int ProcessFile(WOLFSSL_CTX* ctx, const char* fname, int format, int type,
26772677

26782678
/* Look for CA header and footer - same as CERT_TYPE. */
26792679
if (wc_PemGetHeaderFooter(CA_TYPE, &header, &footer) == 0 &&
2680-
(XSTRNSTR((char*)content.buffer, header, (word32)sz) != NULL)) {
2680+
(XSTRNSTR((char*)content.buffer, header, sz) != NULL)) {
26812681
type = CA_TYPE;
26822682
WOLFSSL_MSG_CERT_LOG_EX("Detected cert type CA_TYPE = %d:", type);
26832683
}
26842684
#ifdef HAVE_CRL
26852685
/* Look for CRL header and footer. */
26862686
else if (wc_PemGetHeaderFooter(CRL_TYPE, &header, &footer) == 0 &&
2687-
(XSTRNSTR((char*)content.buffer, header, (word32)sz) != NULL)) {
2687+
(XSTRNSTR((char*)content.buffer, header, sz) != NULL)) {
26882688
type = CRL_TYPE;
26892689
WOLFSSL_MSG_CERT_LOG_EX("Detected cert type CRL_TYPE = %d:", type);
26902690
}
26912691
#endif
26922692
/* Look for cert header and footer - same as CA_TYPE. */
26932693
else if (wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer) == 0 &&
2694-
(XSTRNSTR((char*)content.buffer, header, (word32)sz) !=
2694+
(XSTRNSTR((char*)content.buffer, header, sz) !=
26952695
NULL)) {
26962696
type = CERT_TYPE;
26972697
WOLFSSL_MSG_CERT_LOG_EX("Detected cert type CERT_TYPE = %d:", type);

src/x509.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8348,7 +8348,7 @@ int wolfSSL_X509_LOOKUP_load_file(WOLFSSL_X509_LOOKUP* lookup,
83488348
do {
83498349
/* get PEM header and footer based on type */
83508350
if (wc_PemGetHeaderFooter(CRL_TYPE, &header, &footer) == 0 &&
8351-
XSTRNSTR((char*)curr, header, (unsigned int)sz) != NULL) {
8351+
XSTRNSTR((char*)curr, header, sz) != NULL) {
83528352
#ifdef HAVE_CRL
83538353
WOLFSSL_CERT_MANAGER* cm = lookup->store->cm;
83548354

@@ -8365,15 +8365,15 @@ int wolfSSL_X509_LOOKUP_load_file(WOLFSSL_X509_LOOKUP* lookup,
83658365
if (ret != WOLFSSL_SUCCESS)
83668366
goto end;
83678367
#endif
8368-
curr = (byte*)XSTRNSTR((char*)curr, footer, (unsigned int)sz);
8368+
curr = (byte*)XSTRNSTR((char*)curr, footer, sz);
83698369
}
83708370
else if (wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer) == 0 &&
8371-
XSTRNSTR((char*)curr, header, (unsigned int)sz) != NULL) {
8371+
XSTRNSTR((char*)curr, header, sz) != NULL) {
83728372
ret = X509StoreLoadCertBuffer(lookup->store, curr,
83738373
(word32)sz, WOLFSSL_FILETYPE_PEM);
83748374
if (ret != WOLFSSL_SUCCESS)
83758375
goto end;
8376-
curr = (byte*)XSTRNSTR((char*)curr, footer, (unsigned int)sz);
8376+
curr = (byte*)XSTRNSTR((char*)curr, footer, sz);
83778377
}
83788378
else
83798379
goto end;
@@ -13765,13 +13765,12 @@ int wolfSSL_write_X509_CRL(WOLFSSL_X509_CRL* crl, const char* path, int type)
1376513765
while (i < l && wolfSSL_BIO_read(bio, &pem[i], 1) == 1) {
1376613766
i++;
1376713767
if (!header) {
13768-
header = XSTRNSTR(pem, "-----BEGIN ", (unsigned int)i);
13768+
header = XSTRNSTR(pem, "-----BEGIN ", i);
1376913769
}
1377013770
else if (!headerEnd) {
1377113771
headerEnd = XSTRNSTR(header + XSTR_SIZEOF("-----BEGIN "),
1377213772
"-----",
13773-
(unsigned int)
13774-
(i - (header + XSTR_SIZEOF("-----BEGIN ") - pem)));
13773+
i - (header + XSTR_SIZEOF("-----BEGIN ") - pem));
1377513774
if (headerEnd) {
1377613775
headerEnd += XSTR_SIZEOF("-----");
1377713776
/* Read in the newline */
@@ -13788,12 +13787,12 @@ int wolfSSL_write_X509_CRL(WOLFSSL_X509_CRL* crl, const char* path, int type)
1378813787
}
1378913788
else if (!footer) {
1379013789
footer = XSTRNSTR(headerEnd, "-----END ",
13791-
(unsigned int)(i - (headerEnd - pem)));
13790+
i - (headerEnd - pem));
1379213791
}
1379313792
else if (!footerEnd) {
1379413793
footerEnd = XSTRNSTR(footer + XSTR_SIZEOF("-----"),
13795-
"-----", (unsigned int)(i -
13796-
(footer + XSTR_SIZEOF("-----") - pem)));
13794+
"-----",
13795+
i - (footer + XSTR_SIZEOF("-----") - pem));
1379713796
if (footerEnd) {
1379813797
footerEnd += XSTR_SIZEOF("-----");
1379913798
/* Now check that footer matches header */

src/x509_str.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,7 @@ static int X509StoreReadFile(const char *fname,
17371737
#ifdef HAVE_CRL
17381738
/* Look for CRL header and footer. */
17391739
if (wc_PemGetHeaderFooter(CRL_TYPE, &header, &footer) == 0 &&
1740-
(XSTRNSTR((char*)content->buffer, header, (word32)sz) !=
1740+
(XSTRNSTR((char*)content->buffer, header, sz) !=
17411741
NULL)) {
17421742
*type = CRL_TYPE;
17431743
}

tests/api/test_wolfmath.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ int test_mp_get_digit(void)
7070
ExpectIntEQ(mp_get_digit(NULL, n), 0);
7171
ExpectIntEQ(mp_get_digit(&a, n), 0);
7272

73+
/* negative index must return 0, not index out of bounds */
74+
ExpectIntEQ(mp_get_digit(&a, -1), 0);
75+
ExpectIntEQ(mp_get_digit(&a, -100), 0);
76+
ExpectIntEQ(mp_get_digit(NULL, -1), 0);
77+
7378
mp_clear(&a);
7479
#endif
7580
return EXPECT_RESULT();

wolfcrypt/src/asn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24072,8 +24072,8 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
2407224072
#endif /* WOLFSSL_ENCRYPTED_KEYS */
2407324073

2407424074
/* find footer */
24075-
footerEnd = XSTRNSTR(headerEnd, footer, (unsigned int)((const char*)buff +
24076-
sz - headerEnd));
24075+
footerEnd = XSTRNSTR(headerEnd, footer,
24076+
(size_t)((const char*)buff + sz - headerEnd));
2407724077
if (!footerEnd) {
2407824078
if (info)
2407924079
info->consumed = longSz; /* No more certs if no footer */

wolfcrypt/src/wc_port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4265,9 +4265,9 @@ time_t stm32_hal_time(time_t *t1)
42654265

42664266
#if (!defined(WOLFSSL_LEANPSK) && !defined(STRING_USER)) || \
42674267
defined(USE_WOLF_STRNSTR)
4268-
char* wolfSSL_strnstr(const char* s1, const char* s2, unsigned int n)
4268+
char* wolfSSL_strnstr(const char* s1, const char* s2, size_t n)
42694269
{
4270-
unsigned int s2_len = (unsigned int)XSTRLEN(s2);
4270+
size_t s2_len = XSTRLEN(s2);
42714271

42724272
if (s2_len == 0)
42734273
return (char *)(wc_ptr_t)s1;

wolfcrypt/src/wolfmath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ int mp_get_digit_count(const mp_int* a)
9898

9999
mp_digit mp_get_digit(const mp_int* a, int n)
100100
{
101-
if (a == NULL)
101+
if (a == NULL || n < 0)
102102
return 0;
103103

104-
return (n < 0 || (unsigned int)n >= (unsigned int)a->used) ? 0 : a->dp[n];
104+
return ((unsigned int)n >= (unsigned int)a->used) ? 0 : a->dp[n];
105105
}
106106

107107
#if defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)

wolfssl/wolfcrypt/wc_port.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,8 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
17271727

17281728
#if (!defined(WOLFSSL_LEANPSK) && !defined(STRING_USER)) || \
17291729
defined(USE_WOLF_STRNSTR)
1730-
WOLFSSL_TEST_VIS char* wolfSSL_strnstr(const char* s1, const char* s2, unsigned int n);
1730+
#include <stddef.h> /* for size_t */
1731+
WOLFSSL_TEST_VIS char* wolfSSL_strnstr(const char* s1, const char* s2, size_t n);
17311732
#endif
17321733

17331734
#ifndef FILE_BUFFER_SIZE

0 commit comments

Comments
 (0)