Skip to content

Commit 671876b

Browse files
committed
Add d2i NULL-deref guards and regression tests
Add `*pp == NULL` checks to three d2i wrappers to prevent NULL deref on public OpenSSL-compat APIs: - d2i_evp_pkey (reachable via wolfSSL_d2i_PublicKey/PrivateKey) - wolfSSL_d2i_OCSP_RESPONSE - wolfSSL_d2i_ECDSA_SIG (template-ASN crash) Also add regression tests for the existing PR fixes: ProcessBuffer negative-size, PemToDer family negative-pemSz, GetCRLInfo negative-sz, and wc_Set*Buffer derSz<0.
1 parent 2264212 commit 671876b

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/ocsp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,8 @@ OcspResponse* wolfSSL_d2i_OCSP_RESPONSE(OcspResponse** response,
12441244

12451245
if (data == NULL)
12461246
return NULL;
1247+
if (*data == NULL)
1248+
return NULL;
12471249
if (len <= 0)
12481250
return NULL;
12491251

src/pk_ec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4980,7 +4980,7 @@ WOLFSSL_ECDSA_SIG* wolfSSL_d2i_ECDSA_SIG(WOLFSSL_ECDSA_SIG** sig,
49804980
WOLFSSL_ECDSA_SIG *s = NULL;
49814981

49824982
/* Validate parameter. */
4983-
if (pp == NULL) {
4983+
if (pp == NULL || *pp == NULL) {
49844984
err = 1;
49854985
}
49864986
if ((!err) && (len <= 0)) {

tests/api.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,28 @@ static int test_wolfSSL_CTX_use_certificate_buffer(void)
24352435

24362436
} /* END test_wolfSSL_CTX_use_certificate_buffer */
24372437

2438+
static int test_ProcessBuffer_negative_size(void)
2439+
{
2440+
EXPECT_DECLS;
2441+
#if !defined(NO_CERTS) && !defined(NO_TLS) && !defined(NO_WOLFSSL_SERVER) && \
2442+
defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
2443+
WOLFSSL_CTX* ctx = NULL;
2444+
2445+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2446+
2447+
ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
2448+
server_cert_der_2048, -1, WOLFSSL_FILETYPE_ASN1),
2449+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2450+
2451+
ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
2452+
server_cert_der_2048, sizeof_server_cert_der_2048,
2453+
WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2454+
2455+
wolfSSL_CTX_free(ctx);
2456+
#endif
2457+
return EXPECT_RESULT();
2458+
}
2459+
24382460
static int test_wolfSSL_use_certificate_buffer(void)
24392461
{
24402462
EXPECT_DECLS;
@@ -12110,6 +12132,12 @@ static int test_wc_PemToDer(void)
1211012132

1211112133
XMEMSET(&info, 0, sizeof(info));
1211212134

12135+
{
12136+
const byte dummy = 'X';
12137+
ExpectIntEQ(wc_PemToDer(&dummy, -1, CERT_TYPE, &pDer, NULL,
12138+
&info, &eccKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12139+
}
12140+
1211312141
ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
1211412142
ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
1211512143
&info, &eccKey), 0);
@@ -12270,6 +12298,10 @@ static int test_wc_KeyPemToDer(void)
1227012298
ExpectIntEQ(wc_KeyPemToDer(cert_buf, 0, (byte*)&cert_der, cert_sz, ""),
1227112299
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1227212300

12301+
/* Bad arg: NULL der buffer with negative pemSz (NULL-deref guard). */
12302+
ExpectIntEQ(wc_KeyPemToDer(cert_buf, -1, NULL, 0, ""),
12303+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12304+
1227312305
/* Test normal operation */
1227412306
cert_dersz = cert_sz; /* DER will be smaller than PEM */
1227512307
ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
@@ -21692,6 +21724,13 @@ static int test_wc_SetIssueBuffer(void)
2169221724

2169321725
ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
2169421726

21727+
/* Negative-size rejection: pin both wc_SetIssuerBuffer and
21728+
* wc_SetSubjectBuffer (representatives for the seven wc_Set* siblings). */
21729+
ExpectIntEQ(wc_SetIssuerBuffer(&forgedCert, peerCertBuf, -1),
21730+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
21731+
ExpectIntEQ(wc_SetSubjectBuffer(&forgedCert, peerCertBuf, -1),
21732+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
21733+
2169521734
wolfSSL_FreeX509(x509);
2169621735
#endif
2169721736
return EXPECT_RESULT();
@@ -25540,6 +25579,9 @@ static int test_wolfSSL_CTX_LoadCRL_largeCRLnum(void)
2554025579
WOLFSSL_SUCCESS);
2554125580
AssertIntEQ(XMEMCMP(
2554225581
crlInfo.crlNumber, exp_crlnum, XSTRLEN(exp_crlnum)), 0);
25582+
ExpectIntEQ(wolfSSL_CertManagerGetCRLInfo(
25583+
cm, &crlInfo, crlLrgCrlNumBuff, -1, WOLFSSL_FILETYPE_PEM),
25584+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2554325585
/* Expect to fail loading CRL because of >21 octets CRL number */
2554425586
ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_lrgcrlnum2,
2554525587
WOLFSSL_FILETYPE_PEM),
@@ -38032,6 +38074,7 @@ TEST_CASE testCases[] = {
3803238074
TEST_DECL(test_wolfSSL_CTX_use_certificate),
3803338075
TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
3803438076
TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
38077+
TEST_DECL(test_ProcessBuffer_negative_size),
3803538078
TEST_DECL(test_wolfSSL_use_certificate_buffer),
3803638079
TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
3803738080
TEST_DECL(test_wolfSSL_CTX_use_RSAPrivateKey_file),

wolfcrypt/src/evp_pk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ static WOLFSSL_EVP_PKEY* d2i_evp_pkey(int type, WOLFSSL_EVP_PKEY** out,
12281228
(void)opt;
12291229

12301230
/* Validate parameters. */
1231-
if (in == NULL || inSz < 0) {
1231+
if (in == NULL || *in == NULL || inSz <= 0) {
12321232
WOLFSSL_MSG("Bad argument");
12331233
return NULL;
12341234
}

0 commit comments

Comments
 (0)