@@ -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
20562232static 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