Skip to content

Commit d088fee

Browse files
committed
Add cipher suite filtering when downgrade is disabled
When wolfSSL_SetVersion() is called to set a specific TLS version, the downgrade flag is now set to 0. This causes wolfSSL_parse_cipher_list() to no longer preserve cipher suites from the other TLS version group. Previously, when using SSLv23 method and setting cipher suites for only one TLS version (e.g., TLS 1.2), the library would preserve any existing cipher suites from the other version (e.g., TLS 1.3) for OpenSSL API compatibility. With this change, if a specific version is set via wolfSSL_SetVersion(), only the cipher suites for that version are kept.
1 parent 11ddec3 commit d088fee

2 files changed

Lines changed: 191 additions & 1 deletion

File tree

src/ssl.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5487,6 +5487,8 @@ int wolfSSL_SetVersion(WOLFSSL* ssl, int version)
54875487
return BAD_FUNC_ARG;
54885488
}
54895489

5490+
ssl->options.downgrade = 0;
5491+
54905492
#ifdef NO_RSA
54915493
haveRSA = 0;
54925494
#endif
@@ -9680,7 +9682,15 @@ static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
96809682
* - SSL_CTX_set_ciphersuites for setting TLS 1.3 suites
96819683
* Since we direct both API here we attempt to provide API compatibility. If
96829684
* we only get suites from <= 1.2 or == 1.3 then we will only update those
9683-
* suites and keep the suites from the other group. */
9685+
* suites and keep the suites from the other group.
9686+
* If downgrade is disabled, skip preserving the other group's suites. */
9687+
if ((ssl != NULL && !ssl->options.downgrade) ||
9688+
(ctx != NULL && !ctx->method->downgrade)) {
9689+
/* Downgrade disabled - don't preserve other group's suites */
9690+
WC_FREE_VAR_EX(suitesCpy, NULL, DYNAMIC_TYPE_TMP_BUFFER);
9691+
return ret;
9692+
}
9693+
96849694
for (i = 0; i < suitesCpySz &&
96859695
suites->suiteSz <= (WOLFSSL_MAX_SUITE_SZ - SUITE_LEN); i += 2) {
96869696
/* Check for duplicates */

tests/api.c

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,182 @@ static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
20522052
return EXPECT_RESULT();
20532053
}
20542054

2055+
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && \
2056+
!defined(WOLFSSL_NO_TLS12) && \
2057+
(!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
2058+
/* Helper function to check if TLS 1.3 suites exist in the suites list */
2059+
static int suites_has_tls13(const byte* suites, word16 suiteSz)
2060+
{
2061+
word16 i;
2062+
for (i = 0; i < suiteSz; i += 2) {
2063+
if (suites[i] == 0x13) { /* TLS13_BYTE */
2064+
return 1;
2065+
}
2066+
}
2067+
return 0;
2068+
}
2069+
2070+
/* Helper function to check if TLS 1.2 (non-1.3) suites exist in the suites list */
2071+
static int suites_has_tls12(const byte* suites, word16 suiteSz)
2072+
{
2073+
word16 i;
2074+
for (i = 0; i < suiteSz; i += 2) {
2075+
if (suites[i] != 0x13) { /* Not TLS13_BYTE */
2076+
return 1;
2077+
}
2078+
}
2079+
return 0;
2080+
}
2081+
#endif
2082+
2083+
/* Test 1: SSLv23 + set TLS 1.2 cipher -> TLS 1.3 suites should still be there */
2084+
static int test_wolfSSL_set_cipher_list_tls12_keeps_tls13(void)
2085+
{
2086+
EXPECT_DECLS;
2087+
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && \
2088+
!defined(WOLFSSL_NO_TLS12) && \
2089+
(!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
2090+
defined(HAVE_ECC)
2091+
WOLFSSL_CTX* ctx = NULL;
2092+
WOLFSSL* ssl = NULL;
2093+
2094+
#ifndef NO_WOLFSSL_CLIENT
2095+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2096+
#else
2097+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2098+
#endif
2099+
2100+
ExpectNotNull(ssl = wolfSSL_new(ctx));
2101+
2102+
/* Set only a TLS 1.2 cipher suite */
2103+
ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"),
2104+
WOLFSSL_SUCCESS);
2105+
2106+
/* TLS 1.3 suites should still be present (downgrade is enabled) */
2107+
ExpectNotNull(ssl->suites);
2108+
ExpectTrue(suites_has_tls13(ssl->suites->suites, ssl->suites->suiteSz));
2109+
/* The TLS 1.2 suite we set should also be there */
2110+
ExpectTrue(suites_has_tls12(ssl->suites->suites, ssl->suites->suiteSz));
2111+
2112+
wolfSSL_free(ssl);
2113+
wolfSSL_CTX_free(ctx);
2114+
#endif
2115+
return EXPECT_RESULT();
2116+
}
2117+
2118+
/* Test 2: SSLv23 + set TLS 1.3 cipher -> TLS 1.2 suites should still be there */
2119+
static int test_wolfSSL_set_cipher_list_tls13_keeps_tls12(void)
2120+
{
2121+
EXPECT_DECLS;
2122+
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && \
2123+
!defined(WOLFSSL_NO_TLS12) && \
2124+
(!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
2125+
WOLFSSL_CTX* ctx = NULL;
2126+
WOLFSSL* ssl = NULL;
2127+
2128+
#ifndef NO_WOLFSSL_CLIENT
2129+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2130+
#else
2131+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2132+
#endif
2133+
2134+
ExpectNotNull(ssl = wolfSSL_new(ctx));
2135+
2136+
/* Set only a TLS 1.3 cipher suite */
2137+
ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS_AES_128_GCM_SHA256"),
2138+
WOLFSSL_SUCCESS);
2139+
2140+
/* TLS 1.2 suites should still be present (downgrade is enabled) */
2141+
ExpectNotNull(ssl->suites);
2142+
ExpectTrue(suites_has_tls12(ssl->suites->suites, ssl->suites->suiteSz));
2143+
/* The TLS 1.3 suite we set should also be there */
2144+
ExpectTrue(suites_has_tls13(ssl->suites->suites, ssl->suites->suiteSz));
2145+
2146+
wolfSSL_free(ssl);
2147+
wolfSSL_CTX_free(ctx);
2148+
#endif
2149+
return EXPECT_RESULT();
2150+
}
2151+
2152+
/* Test 3: SSLv23 + SetVersion(TLS 1.2) + set TLS 1.2 cipher -> only that cipher */
2153+
static int test_wolfSSL_set_cipher_list_tls12_with_version(void)
2154+
{
2155+
EXPECT_DECLS;
2156+
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && \
2157+
!defined(WOLFSSL_NO_TLS12) && \
2158+
(!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
2159+
defined(HAVE_ECC)
2160+
WOLFSSL_CTX* ctx = NULL;
2161+
WOLFSSL* ssl = NULL;
2162+
2163+
#ifndef NO_WOLFSSL_CLIENT
2164+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2165+
#else
2166+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2167+
#endif
2168+
2169+
ExpectNotNull(ssl = wolfSSL_new(ctx));
2170+
2171+
/* Set protocol version to TLS 1.2 (this disables downgrade) */
2172+
ExpectIntEQ(wolfSSL_SetVersion(ssl, WOLFSSL_TLSV1_2), WOLFSSL_SUCCESS);
2173+
2174+
/* Set only a TLS 1.2 cipher suite */
2175+
ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"),
2176+
WOLFSSL_SUCCESS);
2177+
2178+
/* Should have only TLS 1.2 suites (no TLS 1.3) since downgrade is disabled */
2179+
ExpectNotNull(ssl->suites);
2180+
ExpectFalse(suites_has_tls13(ssl->suites->suites, ssl->suites->suiteSz));
2181+
/* Should have the TLS 1.2 suite we set */
2182+
ExpectTrue(suites_has_tls12(ssl->suites->suites, ssl->suites->suiteSz));
2183+
/* Should have exactly one cipher suite (2 bytes) */
2184+
ExpectIntEQ(ssl->suites->suiteSz, 2);
2185+
2186+
wolfSSL_free(ssl);
2187+
wolfSSL_CTX_free(ctx);
2188+
#endif
2189+
return EXPECT_RESULT();
2190+
}
2191+
2192+
/* Test 4: SSLv23 + SetVersion(TLS 1.3) + set TLS 1.3 cipher -> only that cipher */
2193+
static int test_wolfSSL_set_cipher_list_tls13_with_version(void)
2194+
{
2195+
EXPECT_DECLS;
2196+
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && \
2197+
!defined(WOLFSSL_NO_TLS12) && \
2198+
(!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
2199+
WOLFSSL_CTX* ctx = NULL;
2200+
WOLFSSL* ssl = NULL;
2201+
2202+
#ifndef NO_WOLFSSL_CLIENT
2203+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2204+
#else
2205+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2206+
#endif
2207+
2208+
ExpectNotNull(ssl = wolfSSL_new(ctx));
2209+
2210+
/* Set protocol version to TLS 1.3 (this disables downgrade) */
2211+
ExpectIntEQ(wolfSSL_SetVersion(ssl, WOLFSSL_TLSV1_3), WOLFSSL_SUCCESS);
2212+
2213+
/* Set only a TLS 1.3 cipher suite */
2214+
ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS_AES_128_GCM_SHA256"),
2215+
WOLFSSL_SUCCESS);
2216+
2217+
/* Should have only TLS 1.3 suites (no TLS 1.2) since downgrade is disabled */
2218+
ExpectNotNull(ssl->suites);
2219+
ExpectFalse(suites_has_tls12(ssl->suites->suites, ssl->suites->suiteSz));
2220+
/* Should have the TLS 1.3 suite we set */
2221+
ExpectTrue(suites_has_tls13(ssl->suites->suites, ssl->suites->suiteSz));
2222+
/* Should have exactly one cipher suite (2 bytes) */
2223+
ExpectIntEQ(ssl->suites->suiteSz, 2);
2224+
2225+
wolfSSL_free(ssl);
2226+
wolfSSL_CTX_free(ctx);
2227+
#endif
2228+
return EXPECT_RESULT();
2229+
}
2230+
20552231

20562232
static int test_wolfSSL_CTX_use_certificate(void)
20572233
{
@@ -31522,6 +31698,10 @@ TEST_CASE testCases[] = {
3152231698
TEST_DECL(test_SSL_CIPHER_get_xxx),
3152331699
TEST_DECL(test_wolfSSL_ERR_strings),
3152431700
TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
31701+
TEST_DECL(test_wolfSSL_set_cipher_list_tls12_keeps_tls13),
31702+
TEST_DECL(test_wolfSSL_set_cipher_list_tls13_keeps_tls12),
31703+
TEST_DECL(test_wolfSSL_set_cipher_list_tls12_with_version),
31704+
TEST_DECL(test_wolfSSL_set_cipher_list_tls13_with_version),
3152531705
TEST_DECL(test_wolfSSL_CTX_use_certificate),
3152631706
TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
3152731707
TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),

0 commit comments

Comments
 (0)