Skip to content

Commit 54e8e80

Browse files
committed
Added integer overflow protection to PKCS12
PKCS12_ConcatenateContent() could overflow.
1 parent 6b4fd43 commit 54e8e80

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

wolfcrypt/src/pkcs12.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ static byte* PKCS12_ConcatenateContent(WC_PKCS12* pkcs12,byte* mergedData,
11351135
{
11361136
byte* oldContent;
11371137
word32 oldContentSz;
1138+
word32 newSz;
11381139

11391140
(void)pkcs12;
11401141

@@ -1146,14 +1147,19 @@ static byte* PKCS12_ConcatenateContent(WC_PKCS12* pkcs12,byte* mergedData,
11461147
oldContentSz = *mergedSz;
11471148

11481149
/* re-allocate new buffer to fit appended data */
1149-
mergedData = (byte*)XMALLOC(oldContentSz + inSz, pkcs12->heap,
1150+
if (WC_SAFE_SUM_WORD32(oldContentSz, inSz, newSz) == 0) {
1151+
XFREE(oldContent, pkcs12->heap, DYNAMIC_TYPE_PKCS);
1152+
return NULL;
1153+
}
1154+
1155+
mergedData = (byte*)XMALLOC(newSz, pkcs12->heap,
11501156
DYNAMIC_TYPE_PKCS);
11511157
if (mergedData != NULL) {
11521158
if (oldContent != NULL) {
11531159
XMEMCPY(mergedData, oldContent, oldContentSz);
11541160
}
11551161
XMEMCPY(mergedData + oldContentSz, in, inSz);
1156-
*mergedSz += inSz;
1162+
*mergedSz = newSz;
11571163
}
11581164
XFREE(oldContent, pkcs12->heap, DYNAMIC_TYPE_PKCS);
11591165

0 commit comments

Comments
 (0)