@@ -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,64 @@ 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 && p_bn != NULL) {
13334+ /* g is NULL on a new DH so set0_pqg fails, p_bn not transferred */
13335+ if (wolfSSL_DH_set0_pqg(dh, p_bn, NULL, NULL) == 1) {
13336+ p_bn = NULL; /* ownership transferred only on success */
13337+ }
13338+ }
13339+ ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_FATAL_ERROR);
13340+ DH_free(dh);
13341+ dh = NULL;
13342+ wolfSSL_BN_free(p_bn);
13343+ p_bn = NULL;
13344+
13345+ /* Test ASN1/DER path through wolfSSL_CTX_SetTmpDH_buffer.
13346+ * Old code had cast-away-const + zero-size AllocDer bug. */
13347+ {
13348+ byte derBuf[4096];
13349+ XFILE f = XBADFILE;
13350+ int derSz = 0;
13351+
13352+ ExpectTrue((f = XFOPEN("./certs/dh4096.der", "rb")) != XBADFILE);
13353+ if (f != XBADFILE) {
13354+ derSz = (int)XFREAD(derBuf, 1, sizeof(derBuf), f);
13355+ XFCLOSE(f);
13356+ }
13357+ ExpectIntGT(derSz, 0);
13358+ ExpectIntEQ(wolfSSL_CTX_SetTmpDH_buffer(ctx, derBuf, (long)derSz,
13359+ WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
13360+ }
13361+
13362+ SSL_CTX_free(ctx);
13363+ #endif
13364+ return EXPECT_RESULT();
13365+ }
13366+
1330013367static int test_wolfSSL_ctrl(void)
1330113368{
1330213369 EXPECT_DECLS;
@@ -35461,6 +35528,7 @@ TEST_CASE testCases[] = {
3546135528 TEST_TLS13_DECLS,
3546235529
3546335530 TEST_DECL(test_wolfSSL_tmp_dh),
35531+ TEST_DECL(test_wolfSSL_tmp_dh_regression),
3546435532 TEST_DECL(test_wolfSSL_ctrl),
3546535533
3546635534 TEST_DECL(test_wolfSSL_get0_param),
0 commit comments