Skip to content

Commit 329ddea

Browse files
committed
tests: cover input validation fixes
1 parent 818912e commit 329ddea

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

tests/api.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12119,6 +12119,19 @@ static int test_wc_PemToDer(void)
1211912119
XFREE(cert_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1212012120
}
1212112121
#endif
12122+
/* NULL buff, zero size, and negative size must be rejected up front. The
12123+
* pre-fix code cast longSz to word32, so a negative value drove an
12124+
* over-read inside PemToDer. */
12125+
{
12126+
const byte stub[] = "x";
12127+
DerBuffer* badDer = NULL;
12128+
ExpectIntEQ(wc_PemToDer(NULL, 100, CERT_TYPE, &badDer, NULL, &info,
12129+
&eccKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12130+
ExpectIntEQ(wc_PemToDer(stub, 0, CERT_TYPE, &badDer, NULL, &info,
12131+
&eccKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12132+
ExpectIntEQ(wc_PemToDer(stub, -1, CERT_TYPE, &badDer, NULL, &info,
12133+
&eccKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12134+
}
1212212135
#endif
1212312136
return EXPECT_RESULT();
1212412137
}

tests/api/test_camellia.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,38 @@ int test_wc_CamelliaSetIV(void)
107107
return EXPECT_RESULT();
108108
} /* END test_wc_CamelliaSetIV*/
109109

110+
/*
111+
* Test wc_CamelliaFree zeroes the key schedule and is NULL safe.
112+
*/
113+
int test_wc_CamelliaFree(void)
114+
{
115+
EXPECT_DECLS;
116+
#ifdef HAVE_CAMELLIA
117+
wc_Camellia camellia;
118+
static const byte key[] = {
119+
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
120+
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
121+
};
122+
static const byte iv[] = {
123+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
124+
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
125+
};
126+
byte zero[sizeof(camellia)];
127+
128+
XMEMSET(zero, 0, sizeof(zero));
129+
130+
/* NULL is safe. */
131+
wc_CamelliaFree(NULL);
132+
133+
/* After SetKey the schedule is populated; Free must wipe it. */
134+
ExpectIntEQ(wc_CamelliaSetKey(&camellia, key, (word32)sizeof(key), iv), 0);
135+
ExpectIntNE(XMEMCMP(&camellia, zero, sizeof(camellia)), 0);
136+
wc_CamelliaFree(&camellia);
137+
ExpectIntEQ(XMEMCMP(&camellia, zero, sizeof(camellia)), 0);
138+
#endif
139+
return EXPECT_RESULT();
140+
} /* END test_wc_CamelliaFree */
141+
110142
/*
111143
* Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
112144
*/

tests/api/test_camellia.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626

2727
int test_wc_CamelliaSetKey(void);
2828
int test_wc_CamelliaSetIV(void);
29+
int test_wc_CamelliaFree(void);
2930
int test_wc_CamelliaEncryptDecryptDirect(void);
3031
int test_wc_CamelliaCbcEncryptDecrypt(void);
3132
int test_wc_CamelliaCbc_MonteCarlo(void);
3233

3334
#define TEST_CAMELLIA_DECLS \
3435
TEST_DECL_GROUP("camellia", test_wc_CamelliaSetKey), \
3536
TEST_DECL_GROUP("camellia", test_wc_CamelliaSetIV), \
37+
TEST_DECL_GROUP("camellia", test_wc_CamelliaFree), \
3638
TEST_DECL_GROUP("camellia", test_wc_CamelliaEncryptDecryptDirect), \
3739
TEST_DECL_GROUP("camellia", test_wc_CamelliaCbcEncryptDecrypt), \
3840
TEST_DECL_GROUP("camellia", test_wc_CamelliaCbc_MonteCarlo)

tests/api/test_pkcs7.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5028,6 +5028,14 @@ int test_wc_PKCS7_DecodeCompressedData(void)
50285028
ExpectNotNull(decompressed);
50295029
ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
50305030
XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
5031+
decompressed = NULL;
5032+
5033+
/* inSz that would overflow on the initial 'tmpSz = inSz * 2' must be
5034+
* rejected up front rather than handed to XMALLOC. */
5035+
ExpectIntEQ(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
5036+
out, ((word32)INT_MAX / 2) + 1, 0, heap),
5037+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
5038+
ExpectNull(decompressed);
50315039

50325040
if (cert_buf != NULL)
50335041
XFREE(cert_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);

0 commit comments

Comments
 (0)