Skip to content

Commit 0d6adc7

Browse files
committed
wolfcrypt: validate API input sizes
1 parent 63f0707 commit 0d6adc7

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

wolfcrypt/src/asn.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24855,7 +24855,14 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
2485524855
int wc_PemToDer(const unsigned char* buff, long longSz, int type,
2485624856
DerBuffer** pDer, void* heap, EncryptedInfo* info, int* keyFormat)
2485724857
{
24858-
int ret = PemToDer(buff, longSz, type, pDer, heap, info, keyFormat);
24858+
int ret;
24859+
24860+
if (buff == NULL || longSz <= 0) {
24861+
WOLFSSL_MSG("Bad pem der args");
24862+
return BAD_FUNC_ARG;
24863+
}
24864+
24865+
ret = PemToDer(buff, longSz, type, pDer, heap, info, keyFormat);
2485924866
#if defined(HAVE_PKCS8) || defined(HAVE_PKCS12)
2486024867
if (ret == 0 && type == PRIVATEKEY_TYPE) {
2486124868
DerBuffer* der = *pDer;

wolfcrypt/src/compress.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
221221
if (out == NULL || in == NULL) {
222222
return BAD_FUNC_ARG;
223223
}
224+
/* Cap input so the initial doubling and additive growth in the loop
225+
* cannot overflow word32 or the int return type. */
226+
if (inSz > (word32)(INT_MAX / 2)) {
227+
return BAD_FUNC_ARG;
228+
}
224229
i = (maxSz == 1)? 1 : 2; /* start with output buffer twice the size of input
225230
* unless max was set to 1 */
226231

@@ -229,7 +234,7 @@ int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
229234
/* Check for source > 64K on 16-bit machine: */
230235
if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E;
231236

232-
tmpSz = inSz * i;
237+
tmpSz = inSz * (word32)i;
233238
tmp = (byte*)XMALLOC(tmpSz, heap, memoryType);
234239
if (tmp == NULL)
235240
return MEMORY_E;
@@ -278,6 +283,11 @@ int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
278283
}
279284
i++;
280285

286+
if (tmpSz > (word32)INT_MAX - inSz) {
287+
WOLFSSL_MSG("Decompress buffer would exceed INT_MAX");
288+
result = DECOMPRESS_E;
289+
break;
290+
}
281291
newSz = tmpSz + inSz;
282292
newTmp = (byte*)XMALLOC(newSz, heap, memoryType);
283293
if (newTmp == NULL) {
@@ -295,13 +305,18 @@ int wc_DeCompressDynamic(byte** out, int maxSz, int memoryType,
295305
} while (result == Z_OK);
296306

297307
if (result == Z_STREAM_END) {
298-
result = (int)stream.total_out;
299-
*out = (byte*)XMALLOC(result, heap, memoryType);
300-
if (*out != NULL) {
301-
XMEMCPY(*out, tmp, result);
308+
if (stream.total_out > (uLong)INT_MAX) {
309+
result = DECOMPRESS_E;
302310
}
303311
else {
304-
result = MEMORY_E;
312+
result = (int)stream.total_out;
313+
*out = (byte*)XMALLOC(result, heap, memoryType);
314+
if (*out != NULL) {
315+
XMEMCPY(*out, tmp, result);
316+
}
317+
else {
318+
result = MEMORY_E;
319+
}
305320
}
306321
}
307322
else {

0 commit comments

Comments
 (0)