Skip to content

Commit 3b9b9cd

Browse files
committed
Add DH regression test and incremement ref counter tests to api.c
1 parent 7d38e9c commit 3b9b9cd

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

tests/api.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3554,6 +3554,11 @@ static int test_wolfSSL_CTX_add1_chain_cert(void)
35543554
}
35553555

35563556
ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
3557+
/* add1 must increment ref count (was 1, now 2). Verifies the
3558+
* up_ref return value is assigned, not just compared. */
3559+
if (EXPECT_SUCCESS() && x509 != NULL) {
3560+
ExpectIntEQ(wolfSSL_RefCur(x509->ref), 2);
3561+
}
35573562
X509_free(x509);
35583563
x509 = NULL;
35593564
}
@@ -3573,6 +3578,10 @@ static int test_wolfSSL_CTX_add1_chain_cert(void)
35733578
}
35743579

35753580
ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
3581+
/* add1 must increment ref count (was 1, now 2) */
3582+
if (EXPECT_SUCCESS() && x509 != NULL) {
3583+
ExpectIntEQ(wolfSSL_RefCur(x509->ref), 2);
3584+
}
35763585
X509_free(x509);
35773586
x509 = NULL;
35783587
}
@@ -13297,6 +13306,62 @@ static int test_wolfSSL_tmp_dh(void)
1329713306
return EXPECT_RESULT();
1329813307
}
1329913308

13309+
/* Tests SSL_CTX_set_tmp_dh with single-operand failure (p set, g missing)
13310+
* and wolfSSL_CTX_SetTmpDH_buffer with WOLFSSL_FILETYPE_ASN1 DER input. */
13311+
static int test_wolfSSL_tmp_dh_regression(void)
13312+
{
13313+
EXPECT_DECLS;
13314+
#if defined(OPENSSL_EXTRA) && !defined(NO_DH) && !defined(NO_CERTS) && \
13315+
!defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_TLS) && \
13316+
!defined(NO_WOLFSSL_SERVER)
13317+
SSL_CTX* ctx = NULL;
13318+
DH* dh = NULL;
13319+
WOLFSSL_BIGNUM* p_bn = NULL;
13320+
13321+
ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
13322+
ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
13323+
WOLFSSL_FILETYPE_PEM));
13324+
ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
13325+
WOLFSSL_FILETYPE_PEM));
13326+
13327+
/* Test single-operand failure: DH with p but no g.
13328+
* Old (pSz < 0) && (gSz < 0) would have missed this since only g fails.
13329+
* Fixed (pSz <= 0) || (gSz <= 0) catches it. */
13330+
ExpectNotNull(dh = wolfSSL_DH_new());
13331+
ExpectNotNull(p_bn = wolfSSL_BN_new());
13332+
ExpectIntEQ(wolfSSL_BN_set_word(p_bn, 0xFFFF), 1);
13333+
if (dh != NULL) {
13334+
/* Set p but leave g as NULL to trigger single-operand failure */
13335+
wolfSSL_DH_set0_pqg(dh, p_bn, NULL, NULL);
13336+
p_bn = NULL; /* ownership transferred */
13337+
}
13338+
ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_FATAL_ERROR);
13339+
DH_free(dh);
13340+
dh = NULL;
13341+
13342+
/* Test ASN1/DER path through wolfSSL_CTX_SetTmpDH_buffer.
13343+
* Old code had cast-away-const + zero-size AllocDer bug. */
13344+
{
13345+
byte derBuf[4096];
13346+
XFILE f = XBADFILE;
13347+
int derSz = 0;
13348+
13349+
ExpectTrue((f = XFOPEN("./certs/dh4096.der", "rb")) != XBADFILE);
13350+
if (f != XBADFILE) {
13351+
derSz = (int)XFREAD(derBuf, 1, sizeof(derBuf), f);
13352+
XFCLOSE(f);
13353+
}
13354+
ExpectIntGT(derSz, 0);
13355+
ExpectIntEQ(wolfSSL_CTX_SetTmpDH_buffer(ctx, derBuf, (long)derSz,
13356+
WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
13357+
}
13358+
13359+
SSL_CTX_free(ctx);
13360+
wolfSSL_BN_free(p_bn);
13361+
#endif
13362+
return EXPECT_RESULT();
13363+
}
13364+
1330013365
static int test_wolfSSL_ctrl(void)
1330113366
{
1330213367
EXPECT_DECLS;
@@ -35461,6 +35526,7 @@ TEST_CASE testCases[] = {
3546135526
TEST_TLS13_DECLS,
3546235527

3546335528
TEST_DECL(test_wolfSSL_tmp_dh),
35529+
TEST_DECL(test_wolfSSL_tmp_dh_regression),
3546435530
TEST_DECL(test_wolfSSL_ctrl),
3546535531

3546635532
TEST_DECL(test_wolfSSL_get0_param),

0 commit comments

Comments
 (0)