Skip to content

Commit eb2fb4a

Browse files
authored
Merge pull request #9699 from anhu/downg
Add cipher suite filtering when downgrade is disabled
2 parents f7b5f00 + 3aa758c commit eb2fb4a

File tree

2 files changed

+180
-1
lines changed

2 files changed

+180
-1
lines changed

src/ssl.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5514,6 +5514,8 @@ int wolfSSL_SetVersion(WOLFSSL* ssl, int version)
55145514
return BAD_FUNC_ARG;
55155515
}
55165516

5517+
ssl->options.downgrade = 0;
5518+
55175519
#ifdef NO_RSA
55185520
haveRSA = 0;
55195521
#endif
@@ -9737,7 +9739,15 @@ static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
97379739
* - SSL_CTX_set_ciphersuites for setting TLS 1.3 suites
97389740
* Since we direct both API here we attempt to provide API compatibility. If
97399741
* we only get suites from <= 1.2 or == 1.3 then we will only update those
9740-
* suites and keep the suites from the other group. */
9742+
* suites and keep the suites from the other group.
9743+
* If downgrade is disabled, skip preserving the other group's suites. */
9744+
if ((ssl != NULL && !ssl->options.downgrade) ||
9745+
(ctx != NULL && !ctx->method->downgrade)) {
9746+
/* Downgrade disabled - don't preserve other group's suites */
9747+
WC_FREE_VAR_EX(suitesCpy, NULL, DYNAMIC_TYPE_TMP_BUFFER);
9748+
return ret;
9749+
}
9750+
97419751
for (i = 0; i < suitesCpySz &&
97429752
suites->suiteSz <= (WOLFSSL_MAX_SUITE_SZ - SUITE_LEN); i += 2) {
97439753
/* Check for duplicates */

tests/api.c

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

20562221
static int test_wolfSSL_CTX_use_certificate(void)
20572222
{
@@ -31638,6 +31803,10 @@ TEST_CASE testCases[] = {
3163831803
TEST_DECL(test_SSL_CIPHER_get_xxx),
3163931804
TEST_DECL(test_wolfSSL_ERR_strings),
3164031805
TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
31806+
TEST_DECL(test_wolfSSL_set_cipher_list_tls12_keeps_tls13),
31807+
TEST_DECL(test_wolfSSL_set_cipher_list_tls13_keeps_tls12),
31808+
TEST_DECL(test_wolfSSL_set_cipher_list_tls12_with_version),
31809+
TEST_DECL(test_wolfSSL_set_cipher_list_tls13_with_version),
3164131810
TEST_DECL(test_wolfSSL_CTX_use_certificate),
3164231811
TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
3164331812
TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),

0 commit comments

Comments
 (0)