1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License");
5*e0c4386eSCy Schubert * you may not use this file except in compliance with the License.
6*e0c4386eSCy Schubert * You may obtain a copy of the License at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert * or in the file LICENSE in the source distribution.
9*e0c4386eSCy Schubert */
10*e0c4386eSCy Schubert
11*e0c4386eSCy Schubert #include <stdio.h>
12*e0c4386eSCy Schubert #include <string.h>
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubert #include <openssl/opensslconf.h>
15*e0c4386eSCy Schubert #include <openssl/err.h>
16*e0c4386eSCy Schubert #include <openssl/e_os2.h>
17*e0c4386eSCy Schubert #include <openssl/ssl.h>
18*e0c4386eSCy Schubert #include <openssl/ssl3.h>
19*e0c4386eSCy Schubert #include <openssl/tls1.h>
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubert #include "internal/nelem.h"
22*e0c4386eSCy Schubert #include "testutil.h"
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert typedef struct cipherlist_test_fixture {
25*e0c4386eSCy Schubert const char *test_case_name;
26*e0c4386eSCy Schubert SSL_CTX *server;
27*e0c4386eSCy Schubert SSL_CTX *client;
28*e0c4386eSCy Schubert } CIPHERLIST_TEST_FIXTURE;
29*e0c4386eSCy Schubert
30*e0c4386eSCy Schubert
tear_down(CIPHERLIST_TEST_FIXTURE * fixture)31*e0c4386eSCy Schubert static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture)
32*e0c4386eSCy Schubert {
33*e0c4386eSCy Schubert if (fixture != NULL) {
34*e0c4386eSCy Schubert SSL_CTX_free(fixture->server);
35*e0c4386eSCy Schubert SSL_CTX_free(fixture->client);
36*e0c4386eSCy Schubert fixture->server = fixture->client = NULL;
37*e0c4386eSCy Schubert OPENSSL_free(fixture);
38*e0c4386eSCy Schubert }
39*e0c4386eSCy Schubert }
40*e0c4386eSCy Schubert
set_up(const char * const test_case_name)41*e0c4386eSCy Schubert static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name)
42*e0c4386eSCy Schubert {
43*e0c4386eSCy Schubert CIPHERLIST_TEST_FIXTURE *fixture;
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubert if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
46*e0c4386eSCy Schubert return NULL;
47*e0c4386eSCy Schubert fixture->test_case_name = test_case_name;
48*e0c4386eSCy Schubert if (!TEST_ptr(fixture->server = SSL_CTX_new(TLS_server_method()))
49*e0c4386eSCy Schubert || !TEST_ptr(fixture->client = SSL_CTX_new(TLS_client_method()))) {
50*e0c4386eSCy Schubert tear_down(fixture);
51*e0c4386eSCy Schubert return NULL;
52*e0c4386eSCy Schubert }
53*e0c4386eSCy Schubert return fixture;
54*e0c4386eSCy Schubert }
55*e0c4386eSCy Schubert
56*e0c4386eSCy Schubert /*
57*e0c4386eSCy Schubert * All ciphers in the DEFAULT cipherlist meet the default security level.
58*e0c4386eSCy Schubert * However, default supported ciphers exclude SRP and PSK ciphersuites
59*e0c4386eSCy Schubert * for which no callbacks have been set up.
60*e0c4386eSCy Schubert *
61*e0c4386eSCy Schubert * Supported ciphers also exclude TLSv1.2 ciphers if TLSv1.2 is disabled,
62*e0c4386eSCy Schubert * and individual disabled algorithms. However, NO_RSA, NO_AES and NO_SHA
63*e0c4386eSCy Schubert * are currently broken and should be considered mission impossible in libssl.
64*e0c4386eSCy Schubert */
65*e0c4386eSCy Schubert static const uint32_t default_ciphers_in_order[] = {
66*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_3
67*e0c4386eSCy Schubert TLS1_3_CK_AES_256_GCM_SHA384,
68*e0c4386eSCy Schubert # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
69*e0c4386eSCy Schubert TLS1_3_CK_CHACHA20_POLY1305_SHA256,
70*e0c4386eSCy Schubert # endif
71*e0c4386eSCy Schubert TLS1_3_CK_AES_128_GCM_SHA256,
72*e0c4386eSCy Schubert #endif
73*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
74*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
75*e0c4386eSCy Schubert TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
76*e0c4386eSCy Schubert TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
77*e0c4386eSCy Schubert # endif
78*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
79*e0c4386eSCy Schubert TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
80*e0c4386eSCy Schubert # endif
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
83*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
84*e0c4386eSCy Schubert TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
85*e0c4386eSCy Schubert TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305,
86*e0c4386eSCy Schubert # endif
87*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
88*e0c4386eSCy Schubert TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305,
89*e0c4386eSCy Schubert # endif
90*e0c4386eSCy Schubert # endif /* !OPENSSL_NO_CHACHA && !OPENSSL_NO_POLY1305 */
91*e0c4386eSCy Schubert
92*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
93*e0c4386eSCy Schubert TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
94*e0c4386eSCy Schubert TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
95*e0c4386eSCy Schubert # endif
96*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
97*e0c4386eSCy Schubert TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
98*e0c4386eSCy Schubert # endif
99*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
100*e0c4386eSCy Schubert TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
101*e0c4386eSCy Schubert TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
102*e0c4386eSCy Schubert # endif
103*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
104*e0c4386eSCy Schubert TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
105*e0c4386eSCy Schubert # endif
106*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
107*e0c4386eSCy Schubert TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
108*e0c4386eSCy Schubert TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
109*e0c4386eSCy Schubert # endif
110*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
111*e0c4386eSCy Schubert TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
112*e0c4386eSCy Schubert # endif
113*e0c4386eSCy Schubert #endif /* !OPENSSL_NO_TLS1_2 */
114*e0c4386eSCy Schubert
115*e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
116*e0c4386eSCy Schubert /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
117*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
118*e0c4386eSCy Schubert TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
119*e0c4386eSCy Schubert TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
120*e0c4386eSCy Schubert # endif
121*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
122*e0c4386eSCy Schubert TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
123*e0c4386eSCy Schubert # endif
124*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
125*e0c4386eSCy Schubert TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
126*e0c4386eSCy Schubert TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
127*e0c4386eSCy Schubert # endif
128*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
129*e0c4386eSCy Schubert TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
130*e0c4386eSCy Schubert # endif
131*e0c4386eSCy Schubert #endif /* !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) */
132*e0c4386eSCy Schubert
133*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
134*e0c4386eSCy Schubert TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
135*e0c4386eSCy Schubert TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
136*e0c4386eSCy Schubert #endif
137*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
138*e0c4386eSCy Schubert TLS1_CK_RSA_WITH_AES_256_SHA256,
139*e0c4386eSCy Schubert TLS1_CK_RSA_WITH_AES_128_SHA256,
140*e0c4386eSCy Schubert #endif
141*e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
142*e0c4386eSCy Schubert /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
143*e0c4386eSCy Schubert TLS1_CK_RSA_WITH_AES_256_SHA,
144*e0c4386eSCy Schubert TLS1_CK_RSA_WITH_AES_128_SHA,
145*e0c4386eSCy Schubert #endif
146*e0c4386eSCy Schubert };
147*e0c4386eSCy Schubert
test_default_cipherlist(SSL_CTX * ctx)148*e0c4386eSCy Schubert static int test_default_cipherlist(SSL_CTX *ctx)
149*e0c4386eSCy Schubert {
150*e0c4386eSCy Schubert STACK_OF(SSL_CIPHER) *ciphers = NULL;
151*e0c4386eSCy Schubert SSL *ssl = NULL;
152*e0c4386eSCy Schubert int i, ret = 0, num_expected_ciphers, num_ciphers;
153*e0c4386eSCy Schubert uint32_t expected_cipher_id, cipher_id;
154*e0c4386eSCy Schubert
155*e0c4386eSCy Schubert if (ctx == NULL)
156*e0c4386eSCy Schubert return 0;
157*e0c4386eSCy Schubert
158*e0c4386eSCy Schubert if (!TEST_ptr(ssl = SSL_new(ctx))
159*e0c4386eSCy Schubert || !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl)))
160*e0c4386eSCy Schubert goto err;
161*e0c4386eSCy Schubert
162*e0c4386eSCy Schubert num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order);
163*e0c4386eSCy Schubert num_ciphers = sk_SSL_CIPHER_num(ciphers);
164*e0c4386eSCy Schubert if (!TEST_int_eq(num_ciphers, num_expected_ciphers))
165*e0c4386eSCy Schubert goto err;
166*e0c4386eSCy Schubert
167*e0c4386eSCy Schubert for (i = 0; i < num_ciphers; i++) {
168*e0c4386eSCy Schubert expected_cipher_id = default_ciphers_in_order[i];
169*e0c4386eSCy Schubert cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i));
170*e0c4386eSCy Schubert if (!TEST_int_eq(cipher_id, expected_cipher_id)) {
171*e0c4386eSCy Schubert TEST_info("Wrong cipher at position %d", i);
172*e0c4386eSCy Schubert goto err;
173*e0c4386eSCy Schubert }
174*e0c4386eSCy Schubert }
175*e0c4386eSCy Schubert
176*e0c4386eSCy Schubert ret = 1;
177*e0c4386eSCy Schubert
178*e0c4386eSCy Schubert err:
179*e0c4386eSCy Schubert sk_SSL_CIPHER_free(ciphers);
180*e0c4386eSCy Schubert SSL_free(ssl);
181*e0c4386eSCy Schubert return ret;
182*e0c4386eSCy Schubert }
183*e0c4386eSCy Schubert
execute_test(CIPHERLIST_TEST_FIXTURE * fixture)184*e0c4386eSCy Schubert static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture)
185*e0c4386eSCy Schubert {
186*e0c4386eSCy Schubert return fixture != NULL
187*e0c4386eSCy Schubert && test_default_cipherlist(fixture->server)
188*e0c4386eSCy Schubert && test_default_cipherlist(fixture->client);
189*e0c4386eSCy Schubert }
190*e0c4386eSCy Schubert
191*e0c4386eSCy Schubert #define SETUP_CIPHERLIST_TEST_FIXTURE() \
192*e0c4386eSCy Schubert SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up)
193*e0c4386eSCy Schubert
194*e0c4386eSCy Schubert #define EXECUTE_CIPHERLIST_TEST() \
195*e0c4386eSCy Schubert EXECUTE_TEST(execute_test, tear_down)
196*e0c4386eSCy Schubert
test_default_cipherlist_implicit(void)197*e0c4386eSCy Schubert static int test_default_cipherlist_implicit(void)
198*e0c4386eSCy Schubert {
199*e0c4386eSCy Schubert SETUP_CIPHERLIST_TEST_FIXTURE();
200*e0c4386eSCy Schubert EXECUTE_CIPHERLIST_TEST();
201*e0c4386eSCy Schubert return result;
202*e0c4386eSCy Schubert }
203*e0c4386eSCy Schubert
test_default_cipherlist_explicit(void)204*e0c4386eSCy Schubert static int test_default_cipherlist_explicit(void)
205*e0c4386eSCy Schubert {
206*e0c4386eSCy Schubert SETUP_CIPHERLIST_TEST_FIXTURE();
207*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT"))
208*e0c4386eSCy Schubert || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT"))) {
209*e0c4386eSCy Schubert tear_down(fixture);
210*e0c4386eSCy Schubert fixture = NULL;
211*e0c4386eSCy Schubert }
212*e0c4386eSCy Schubert EXECUTE_CIPHERLIST_TEST();
213*e0c4386eSCy Schubert return result;
214*e0c4386eSCy Schubert }
215*e0c4386eSCy Schubert
216*e0c4386eSCy Schubert /* SSL_CTX_set_cipher_list() should fail if it clears all TLSv1.2 ciphers. */
test_default_cipherlist_clear(void)217*e0c4386eSCy Schubert static int test_default_cipherlist_clear(void)
218*e0c4386eSCy Schubert {
219*e0c4386eSCy Schubert SSL *s = NULL;
220*e0c4386eSCy Schubert SETUP_CIPHERLIST_TEST_FIXTURE();
221*e0c4386eSCy Schubert
222*e0c4386eSCy Schubert if (!TEST_int_eq(SSL_CTX_set_cipher_list(fixture->server, "no-such"), 0))
223*e0c4386eSCy Schubert goto end;
224*e0c4386eSCy Schubert
225*e0c4386eSCy Schubert if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH))
226*e0c4386eSCy Schubert goto end;
227*e0c4386eSCy Schubert
228*e0c4386eSCy Schubert s = SSL_new(fixture->client);
229*e0c4386eSCy Schubert
230*e0c4386eSCy Schubert if (!TEST_ptr(s))
231*e0c4386eSCy Schubert goto end;
232*e0c4386eSCy Schubert
233*e0c4386eSCy Schubert if (!TEST_int_eq(SSL_set_cipher_list(s, "no-such"), 0))
234*e0c4386eSCy Schubert goto end;
235*e0c4386eSCy Schubert
236*e0c4386eSCy Schubert if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
237*e0c4386eSCy Schubert SSL_R_NO_CIPHER_MATCH))
238*e0c4386eSCy Schubert goto end;
239*e0c4386eSCy Schubert
240*e0c4386eSCy Schubert result = 1;
241*e0c4386eSCy Schubert end:
242*e0c4386eSCy Schubert SSL_free(s);
243*e0c4386eSCy Schubert tear_down(fixture);
244*e0c4386eSCy Schubert return result;
245*e0c4386eSCy Schubert }
246*e0c4386eSCy Schubert
setup_tests(void)247*e0c4386eSCy Schubert int setup_tests(void)
248*e0c4386eSCy Schubert {
249*e0c4386eSCy Schubert ADD_TEST(test_default_cipherlist_implicit);
250*e0c4386eSCy Schubert ADD_TEST(test_default_cipherlist_explicit);
251*e0c4386eSCy Schubert ADD_TEST(test_default_cipherlist_clear);
252*e0c4386eSCy Schubert return 1;
253*e0c4386eSCy Schubert }
254