Skip to content

Commit 5ad6097

Browse files
authored
Merge pull request #10168 from night1rider/zd-21534
Address bug fixes sent in by ZD 21534
2 parents 8fd896a + 8cc02d8 commit 5ad6097

2 files changed

Lines changed: 142 additions & 11 deletions

File tree

src/ssl_load.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ static int ProcessBufferCertPublicKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
16721672
ECC_KEY_SIZE_E);
16731673
}
16741674
break;
1675-
#endif /* HAVE_ED25519 */
1675+
#endif /* WOLFSSL_SM2 && WOLFSSL_SM3 */
16761676
#ifdef HAVE_ED25519
16771677
case ED25519k:
16781678
keyType = ed25519_sa_algo;
@@ -1882,7 +1882,7 @@ static int ProcessBufferCertAltPublicKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
18821882
ECC_KEY_SIZE_E);
18831883
}
18841884
break;
1885-
#endif /* HAVE_ED25519 */
1885+
#endif /* WOLFSSL_SM2 && WOLFSSL_SM3 */
18861886
#ifdef HAVE_ED25519
18871887
case ED25519k:
18881888
keyType = ed25519_sa_algo;
@@ -4684,7 +4684,7 @@ int wolfSSL_use_AltPrivateKey_Id(WOLFSSL* ssl, const unsigned char* id, long sz,
46844684
#endif
46854685
}
46864686
if (AllocDer(&ssl->buffers.altKey, (word32)sz, ALT_PRIVATEKEY_TYPE,
4687-
ssl->heap) == 0) {
4687+
ssl->heap) != 0) {
46884688
ret = 0;
46894689
}
46904690
}
@@ -4732,7 +4732,7 @@ int wolfSSL_use_AltPrivateKey_Label(WOLFSSL* ssl, const char* label, int devId)
47324732
#endif
47334733
}
47344734
if (AllocDer(&ssl->buffers.altKey, (word32)sz, ALT_PRIVATEKEY_TYPE,
4735-
ssl->heap) == 0) {
4735+
ssl->heap) != 0) {
47364736
ret = 0;
47374737
}
47384738
}
@@ -5202,7 +5202,7 @@ int wolfSSL_add1_chain_cert(WOLFSSL* ssl, WOLFSSL_X509* x509)
52025202
}
52035203

52045204
/* Increase reference count on X509 object before adding. */
5205-
if ((ret == 1) && ((ret == wolfSSL_X509_up_ref(x509)) == 1)) {
5205+
if ((ret == 1) && ((ret = wolfSSL_X509_up_ref(x509)) == 1)) {
52065206
/* Add this to the chain. */
52075207
if ((ret = wolfSSL_add0_chain_cert(ssl, x509)) != 1) {
52085208
/* Decrease reference count on error as not stored. */
@@ -5864,7 +5864,7 @@ long wolfSSL_CTX_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL_DH* dh)
58645864
pSz = wolfSSL_BN_bn2bin(dh->p, p);
58655865
gSz = wolfSSL_BN_bn2bin(dh->g, g);
58665866
/* Check encoding worked. */
5867-
if ((pSz < 0) && (gSz < 0)) {
5867+
if ((pSz <= 0) || (gSz <= 0)) {
58685868
ret = WOLFSSL_FATAL_ERROR;
58695869
}
58705870
}
@@ -5930,12 +5930,10 @@ static int ws_ctx_ssl_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
59305930

59315931
/* PemToDer allocates its own DER buffer. */
59325932
if ((res == 1) && (format != WOLFSSL_FILETYPE_PEM)) {
5933-
/* Create an empty DER buffer. */
5934-
ret = AllocDer(&der, 0, DH_PARAM_TYPE, heap);
5933+
/* Create a DER buffer and copy in the encoded DH parameters. */
5934+
ret = AllocDer(&der, (word32)sz, DH_PARAM_TYPE, heap);
59355935
if (ret == 0) {
5936-
/* Assign encoded DH parameters to DER buffer. */
5937-
der->buffer = (byte*)buf;
5938-
der->length = (word32)sz;
5936+
XMEMCPY(der->buffer, buf, (word32)sz);
59395937
}
59405938
else {
59415939
res = ret;

tests/api.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,68 @@ static int test_dual_alg_ecdsa_mldsa(void)
16961696
return EXPECT_RESULT();
16971697
}
16981698

1699+
/* Test wolfSSL_use_AltPrivateKey_Id.
1700+
* Verify that a valid key ID can be set successfully. Guards against an
1701+
* inverted AllocDer return check (== 0 vs != 0) that would treat successful
1702+
* allocation as failure. */
1703+
static int test_wolfSSL_use_AltPrivateKey_Id(void)
1704+
{
1705+
EXPECT_DECLS;
1706+
#if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_TLS) && \
1707+
!defined(NO_WOLFSSL_CLIENT)
1708+
WOLFSSL_CTX* ctx = NULL;
1709+
WOLFSSL* ssl = NULL;
1710+
const unsigned char id[] = { 0x01, 0x02, 0x03, 0x04 };
1711+
1712+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
1713+
ExpectNotNull(ssl = wolfSSL_new(ctx));
1714+
1715+
/* Negative tests. */
1716+
ExpectIntEQ(wolfSSL_use_AltPrivateKey_Id(NULL, id, sizeof(id),
1717+
INVALID_DEVID), 0);
1718+
ExpectIntEQ(wolfSSL_use_AltPrivateKey_Id(ssl, NULL, sizeof(id),
1719+
INVALID_DEVID), 0);
1720+
1721+
/* Positive test - valid ID should succeed. */
1722+
ExpectIntEQ(wolfSSL_use_AltPrivateKey_Id(ssl, id, sizeof(id),
1723+
INVALID_DEVID), 1);
1724+
1725+
wolfSSL_free(ssl);
1726+
wolfSSL_CTX_free(ctx);
1727+
#endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_TLS && !NO_WOLFSSL_CLIENT */
1728+
return EXPECT_RESULT();
1729+
}
1730+
1731+
/* Test wolfSSL_use_AltPrivateKey_Label.
1732+
* Verify that a valid key label can be set successfully. Guards against an
1733+
* inverted AllocDer return check (== 0 vs != 0) that would treat successful
1734+
* allocation as failure. */
1735+
static int test_wolfSSL_use_AltPrivateKey_Label(void)
1736+
{
1737+
EXPECT_DECLS;
1738+
#if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_TLS) && \
1739+
!defined(NO_WOLFSSL_CLIENT)
1740+
WOLFSSL_CTX* ctx = NULL;
1741+
WOLFSSL* ssl = NULL;
1742+
1743+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
1744+
ExpectNotNull(ssl = wolfSSL_new(ctx));
1745+
1746+
/* Negative tests. */
1747+
ExpectIntEQ(wolfSSL_use_AltPrivateKey_Label(NULL, "label", INVALID_DEVID),
1748+
0);
1749+
ExpectIntEQ(wolfSSL_use_AltPrivateKey_Label(ssl, NULL, INVALID_DEVID), 0);
1750+
1751+
/* Positive test - valid label should succeed. */
1752+
ExpectIntEQ(wolfSSL_use_AltPrivateKey_Label(ssl, "test_label",
1753+
INVALID_DEVID), 1);
1754+
1755+
wolfSSL_free(ssl);
1756+
wolfSSL_CTX_free(ctx);
1757+
#endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_TLS && !NO_WOLFSSL_CLIENT */
1758+
return EXPECT_RESULT();
1759+
}
1760+
16991761

17001762
/*----------------------------------------------------------------------------*
17011763
| Context
@@ -3505,6 +3567,11 @@ static int test_wolfSSL_CTX_add1_chain_cert(void)
35053567
}
35063568

35073569
ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
3570+
/* add1 must increment ref count (was 1, now 2). Verifies the
3571+
* up_ref return value is assigned, not just compared. */
3572+
if (EXPECT_SUCCESS() && x509 != NULL) {
3573+
ExpectIntEQ(wolfSSL_RefCur(x509->ref), 2);
3574+
}
35083575
X509_free(x509);
35093576
x509 = NULL;
35103577
}
@@ -3524,6 +3591,10 @@ static int test_wolfSSL_CTX_add1_chain_cert(void)
35243591
}
35253592

35263593
ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
3594+
/* add1 must increment ref count (was 1, now 2) */
3595+
if (EXPECT_SUCCESS() && x509 != NULL) {
3596+
ExpectIntEQ(wolfSSL_RefCur(x509->ref), 2);
3597+
}
35273598
X509_free(x509);
35283599
x509 = NULL;
35293600
}
@@ -13248,6 +13319,64 @@ static int test_wolfSSL_tmp_dh(void)
1324813319
return EXPECT_RESULT();
1324913320
}
1325013321

13322+
/* Tests SSL_CTX_set_tmp_dh with single-operand failure (p set, g missing)
13323+
* and wolfSSL_CTX_SetTmpDH_buffer with WOLFSSL_FILETYPE_ASN1 DER input. */
13324+
static int test_wolfSSL_tmp_dh_regression(void)
13325+
{
13326+
EXPECT_DECLS;
13327+
#if defined(OPENSSL_EXTRA) && !defined(NO_DH) && !defined(NO_CERTS) && \
13328+
!defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_TLS) && \
13329+
!defined(NO_WOLFSSL_SERVER)
13330+
SSL_CTX* ctx = NULL;
13331+
13332+
ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
13333+
ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
13334+
WOLFSSL_FILETYPE_PEM));
13335+
ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
13336+
WOLFSSL_FILETYPE_PEM));
13337+
13338+
#if defined(OPENSSL_ALL) || \
13339+
(defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L)
13340+
{
13341+
/* Test single-operand failure: DH with p but no g. */
13342+
DH* dh = NULL;
13343+
WOLFSSL_BIGNUM* p_bn = NULL;
13344+
13345+
ExpectNotNull(dh = wolfSSL_DH_new());
13346+
ExpectNotNull(p_bn = wolfSSL_BN_new());
13347+
ExpectIntEQ(wolfSSL_BN_set_word(p_bn, 0xFFFF), 1);
13348+
if (dh != NULL && p_bn != NULL) {
13349+
if (wolfSSL_DH_set0_pqg(dh, p_bn, NULL, NULL) == 1) {
13350+
p_bn = NULL; /* ownership transferred on success */
13351+
}
13352+
}
13353+
ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_FATAL_ERROR);
13354+
DH_free(dh);
13355+
wolfSSL_BN_free(p_bn);
13356+
}
13357+
#endif
13358+
13359+
/* Test ASN1/DER path through wolfSSL_CTX_SetTmpDH_buffer. */
13360+
{
13361+
byte derBuf[4096];
13362+
XFILE f = XBADFILE;
13363+
int derSz = 0;
13364+
13365+
ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
13366+
if (f != XBADFILE) {
13367+
derSz = (int)XFREAD(derBuf, 1, sizeof(derBuf), f);
13368+
XFCLOSE(f);
13369+
}
13370+
ExpectIntGT(derSz, 0);
13371+
ExpectIntEQ(wolfSSL_CTX_SetTmpDH_buffer(ctx, derBuf, (long)derSz,
13372+
WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
13373+
}
13374+
13375+
SSL_CTX_free(ctx);
13376+
#endif
13377+
return EXPECT_RESULT();
13378+
}
13379+
1325113380
static int test_wolfSSL_ctrl(void)
1325213381
{
1325313382
EXPECT_DECLS;
@@ -35313,6 +35442,9 @@ TEST_CASE testCases[] = {
3531335442

3531435443
TEST_DECL(test_dual_alg_ecdsa_mldsa),
3531535444

35445+
TEST_DECL(test_wolfSSL_use_AltPrivateKey_Id),
35446+
TEST_DECL(test_wolfSSL_use_AltPrivateKey_Label),
35447+
3531635448
/*********************************
3531735449
* OpenSSL compatibility API tests
3531835450
*********************************/
@@ -35584,6 +35716,7 @@ TEST_CASE testCases[] = {
3558435716
TEST_TLS13_DECLS,
3558535717

3558635718
TEST_DECL(test_wolfSSL_tmp_dh),
35719+
TEST_DECL(test_wolfSSL_tmp_dh_regression),
3558735720
TEST_DECL(test_wolfSSL_ctrl),
3558835721

3558935722
TEST_DECL(test_wolfSSL_get0_param),

0 commit comments

Comments
 (0)