Skip to content

Commit 1c90154

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 7de624f commit 1c90154

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
@@ -1272,6 +1272,8 @@ OcspResponse* wolfSSL_d2i_OCSP_RESPONSE(OcspResponse** response,
12721272

12731273
if (data == NULL)
12741274
return NULL;
1275+
if (*data == NULL)
1276+
return NULL;
12751277
if (len <= 0)
12761278
return NULL;
12771279

src/pk_ec.c

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

50025002
/* Validate parameter. */
5003-
if (pp == NULL) {
5003+
if (pp == NULL || *pp == NULL) {
50045004
err = 1;
50055005
}
50065006
if ((!err) && (len <= 0)) {

tests/api.c

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

25172517
} /* END test_wolfSSL_CTX_use_certificate_buffer */
25182518

2519+
static int test_ProcessBuffer_negative_size(void)
2520+
{
2521+
EXPECT_DECLS;
2522+
#if !defined(NO_CERTS) && !defined(NO_TLS) && !defined(NO_WOLFSSL_SERVER) && \
2523+
defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
2524+
WOLFSSL_CTX* ctx = NULL;
2525+
2526+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2527+
2528+
ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
2529+
server_cert_der_2048, -1, WOLFSSL_FILETYPE_ASN1),
2530+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2531+
2532+
ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
2533+
server_cert_der_2048, sizeof_server_cert_der_2048,
2534+
WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2535+
2536+
wolfSSL_CTX_free(ctx);
2537+
#endif
2538+
return EXPECT_RESULT();
2539+
}
2540+
25192541
static int test_wolfSSL_use_certificate_buffer(void)
25202542
{
25212543
EXPECT_DECLS;
@@ -12159,6 +12181,12 @@ static int test_wc_PemToDer(void)
1215912181

1216012182
XMEMSET(&info, 0, sizeof(info));
1216112183

12184+
{
12185+
const byte dummy = 'X';
12186+
ExpectIntEQ(wc_PemToDer(&dummy, -1, CERT_TYPE, &pDer, NULL,
12187+
&info, &eccKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12188+
}
12189+
1216212190
ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
1216312191
ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
1216412192
&info, &eccKey), 0);
@@ -12332,6 +12360,10 @@ static int test_wc_KeyPemToDer(void)
1233212360
ExpectIntEQ(wc_KeyPemToDer(cert_buf, 0, (byte*)&cert_der, cert_sz, ""),
1233312361
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1233412362

12363+
/* Bad arg: NULL der buffer with negative pemSz (NULL-deref guard). */
12364+
ExpectIntEQ(wc_KeyPemToDer(cert_buf, -1, NULL, 0, ""),
12365+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12366+
1233512367
/* Test normal operation */
1233612368
cert_dersz = cert_sz; /* DER will be smaller than PEM */
1233712369
ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
@@ -23478,6 +23510,13 @@ static int test_wc_SetIssueBuffer(void)
2347823510

2347923511
ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
2348023512

23513+
/* Negative-size rejection: pin both wc_SetIssuerBuffer and
23514+
* wc_SetSubjectBuffer (representatives for the seven wc_Set* siblings). */
23515+
ExpectIntEQ(wc_SetIssuerBuffer(&forgedCert, peerCertBuf, -1),
23516+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
23517+
ExpectIntEQ(wc_SetSubjectBuffer(&forgedCert, peerCertBuf, -1),
23518+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
23519+
2348123520
wolfSSL_FreeX509(x509);
2348223521
#endif
2348323522
return EXPECT_RESULT();
@@ -27379,6 +27418,9 @@ static int test_wolfSSL_CTX_LoadCRL_largeCRLnum(void)
2737927418
WOLFSSL_SUCCESS);
2738027419
AssertIntEQ(XMEMCMP(
2738127420
crlInfo.crlNumber, exp_crlnum, XSTRLEN(exp_crlnum)), 0);
27421+
ExpectIntEQ(wolfSSL_CertManagerGetCRLInfo(
27422+
cm, &crlInfo, crlLrgCrlNumBuff, -1, WOLFSSL_FILETYPE_PEM),
27423+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2738227424
/* Expect to fail loading CRL because of >21 octets CRL number */
2738327425
ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_lrgcrlnum2,
2738427426
WOLFSSL_FILETYPE_PEM),
@@ -40624,6 +40666,7 @@ TEST_CASE testCases[] = {
4062440666
TEST_DECL(test_wolfSSL_CTX_use_certificate),
4062540667
TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
4062640668
TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
40669+
TEST_DECL(test_ProcessBuffer_negative_size),
4062740670
TEST_DECL(test_wolfSSL_use_certificate_buffer),
4062840671
TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
4062940672
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
@@ -1240,7 +1240,7 @@ static WOLFSSL_EVP_PKEY* d2i_evp_pkey(int type, WOLFSSL_EVP_PKEY** out,
12401240
(void)opt;
12411241

12421242
/* Validate parameters. */
1243-
if (in == NULL || inSz < 0) {
1243+
if (in == NULL || *in == NULL || inSz <= 0) {
12441244
WOLFSSL_MSG("Bad argument");
12451245
return NULL;
12461246
}

0 commit comments

Comments
 (0)