Skip to content

Commit 8771059

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 fcd52ae commit 8771059

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/ocsp.c

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

12401240
if (data == NULL)
12411241
return NULL;
1242+
if (*data == NULL)
1243+
return NULL;
12421244
if (len <= 0)
12431245
return NULL;
12441246

src/pk_ec.c

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

49784978
/* Validate parameter. */
4979-
if (pp == NULL) {
4979+
if (pp == NULL || *pp == NULL) {
49804980
err = 1;
49814981
}
49824982
if ((!err) && (len <= 0)) {

tests/api.c

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

23722372
} /* END test_wolfSSL_CTX_use_certificate_buffer */
23732373

2374+
static int test_ProcessBuffer_negative_size(void)
2375+
{
2376+
EXPECT_DECLS;
2377+
#if !defined(NO_CERTS) && !defined(NO_TLS) && !defined(NO_WOLFSSL_SERVER) && \
2378+
defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
2379+
WOLFSSL_CTX* ctx = NULL;
2380+
2381+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2382+
2383+
ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
2384+
server_cert_der_2048, -1, WOLFSSL_FILETYPE_ASN1),
2385+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2386+
2387+
ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
2388+
server_cert_der_2048, sizeof_server_cert_der_2048,
2389+
WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2390+
2391+
wolfSSL_CTX_free(ctx);
2392+
#endif
2393+
return EXPECT_RESULT();
2394+
}
2395+
23742396
static int test_wolfSSL_use_certificate_buffer(void)
23752397
{
23762398
EXPECT_DECLS;
@@ -11704,6 +11726,12 @@ static int test_wc_PemToDer(void)
1170411726

1170511727
XMEMSET(&info, 0, sizeof(info));
1170611728

11729+
{
11730+
const byte dummy = 'X';
11731+
ExpectIntEQ(wc_PemToDer(&dummy, -1, CERT_TYPE, &pDer, NULL,
11732+
&info, &eccKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
11733+
}
11734+
1170711735
ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
1170811736
ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
1170911737
&info, &eccKey), 0);
@@ -11798,6 +11826,8 @@ static int test_wc_CertPemToDer(void)
1179811826
(int)cert_dersz, CERT_TYPE), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1179911827
ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
1180011828
CERT_TYPE), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
11829+
ExpectIntEQ(wc_CertPemToDer(cert_buf, -1, cert_der, (int)cert_dersz,
11830+
CERT_TYPE), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1180111831

1180211832
if (cert_der != NULL)
1180311833
free(cert_der);
@@ -11854,6 +11884,10 @@ static int test_wc_KeyPemToDer(void)
1185411884
ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
1185511885
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1185611886

11887+
/* Bad arg: negative pemSz */
11888+
ExpectIntEQ(wc_KeyPemToDer(cert_buf, -1, NULL, 0, ""),
11889+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
11890+
1185711891
/* Test normal operation */
1185811892
cert_dersz = cert_sz; /* DER will be smaller than PEM */
1185911893
ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
@@ -11897,6 +11931,9 @@ static int test_wc_PubKeyPemToDer(void)
1189711931
ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
1189811932
cert_dersz = cert_sz; /* DER will be smaller than PEM */
1189911933
ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
11934+
/* Bad arg: negative pemSz */
11935+
ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, -1, cert_der, (int)cert_dersz),
11936+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1190011937
ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
1190111938
(int)cert_dersz), 0);
1190211939
if (cert_der != NULL) {
@@ -21142,6 +21179,13 @@ static int test_wc_SetIssueBuffer(void)
2114221179

2114321180
ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
2114421181

21182+
/* Negative-size rejection: pin both wc_SetIssuerBuffer and
21183+
* wc_SetSubjectBuffer (representatives for the seven wc_Set* siblings). */
21184+
ExpectIntEQ(wc_SetIssuerBuffer(&forgedCert, peerCertBuf, -1),
21185+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
21186+
ExpectIntEQ(wc_SetSubjectBuffer(&forgedCert, peerCertBuf, -1),
21187+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
21188+
2114521189
wolfSSL_FreeX509(x509);
2114621190
#endif
2114721191
return EXPECT_RESULT();
@@ -24357,6 +24401,9 @@ static int test_wolfSSL_CTX_LoadCRL_largeCRLnum(void)
2435724401
WOLFSSL_SUCCESS);
2435824402
AssertIntEQ(XMEMCMP(
2435924403
crlInfo.crlNumber, exp_crlnum, XSTRLEN(exp_crlnum)), 0);
24404+
ExpectIntEQ(wolfSSL_CertManagerGetCRLInfo(
24405+
cm, &crlInfo, crlLrgCrlNumBuff, -1, WOLFSSL_FILETYPE_PEM),
24406+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2436024407
/* Expect to fail loading CRL because of >21 octets CRL number */
2436124408
ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_lrgcrlnum2,
2436224409
WOLFSSL_FILETYPE_PEM),
@@ -35652,6 +35699,7 @@ TEST_CASE testCases[] = {
3565235699
TEST_DECL(test_wolfSSL_CTX_use_certificate),
3565335700
TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
3565435701
TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
35702+
TEST_DECL(test_ProcessBuffer_negative_size),
3565535703
TEST_DECL(test_wolfSSL_use_certificate_buffer),
3565635704
TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
3565735705
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
@@ -902,7 +902,7 @@ static WOLFSSL_EVP_PKEY* d2i_evp_pkey(int type, WOLFSSL_EVP_PKEY** out,
902902
(void)opt;
903903

904904
/* Validate parameters. */
905-
if (in == NULL || inSz < 0) {
905+
if (in == NULL || *in == NULL || inSz <= 0) {
906906
WOLFSSL_MSG("Bad argument");
907907
return NULL;
908908
}

0 commit comments

Comments
 (0)