2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
14 #include <openssl/opensslconf.h>
15 #include <openssl/err.h>
16 #include <openssl/e_os2.h>
17 #include <openssl/ssl.h>
18 #include <openssl/ssl3.h>
19 #include <openssl/tls1.h>
21 #include "internal/nelem.h"
24 DEFINE_STACK_OF_CONST(SSL_CIPHER)
26 typedef struct cipherlist_test_fixture {
27 const char *test_case_name;
30 } CIPHERLIST_TEST_FIXTURE;
33 static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture)
35 if (fixture != NULL) {
36 SSL_CTX_free(fixture->server);
37 SSL_CTX_free(fixture->client);
38 fixture->server = fixture->client = NULL;
39 OPENSSL_free(fixture);
43 static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name)
45 CIPHERLIST_TEST_FIXTURE *fixture;
47 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
49 fixture->test_case_name = test_case_name;
50 if (!TEST_ptr(fixture->server = SSL_CTX_new(TLS_server_method()))
51 || !TEST_ptr(fixture->client = SSL_CTX_new(TLS_client_method()))) {
59 * All ciphers in the DEFAULT cipherlist meet the default security level.
60 * However, default supported ciphers exclude SRP and PSK ciphersuites
61 * for which no callbacks have been set up.
63 * Supported ciphers also exclude TLSv1.2 ciphers if TLSv1.2 is disabled,
64 * and individual disabled algorithms. However, NO_RSA, NO_AES and NO_SHA
65 * are currently broken and should be considered mission impossible in libssl.
67 static const uint32_t default_ciphers_in_order[] = {
68 #ifndef OPENSSL_NO_TLS1_3
69 TLS1_3_CK_AES_256_GCM_SHA384,
70 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
71 TLS1_3_CK_CHACHA20_POLY1305_SHA256,
73 TLS1_3_CK_AES_128_GCM_SHA256,
75 #ifndef OPENSSL_NO_TLS1_2
76 # ifndef OPENSSL_NO_EC
77 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
78 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
80 # ifndef OPENSSL_NO_DH
81 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
84 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
85 # ifndef OPENSSL_NO_EC
86 TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
87 TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305,
89 # ifndef OPENSSL_NO_DH
90 TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305,
92 # endif /* !OPENSSL_NO_CHACHA && !OPENSSL_NO_POLY1305 */
94 # ifndef OPENSSL_NO_EC
95 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
96 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
98 # ifndef OPENSSL_NO_DH
99 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
101 # ifndef OPENSSL_NO_EC
102 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
103 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
105 # ifndef OPENSSL_NO_DH
106 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
108 # ifndef OPENSSL_NO_EC
109 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
110 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
112 # ifndef OPENSSL_NO_DH
113 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
115 #endif /* !OPENSSL_NO_TLS1_2 */
117 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
118 /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
119 # ifndef OPENSSL_NO_EC
120 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
121 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
123 #ifndef OPENSSL_NO_DH
124 TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
126 # ifndef OPENSSL_NO_EC
127 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
128 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
130 # ifndef OPENSSL_NO_DH
131 TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
133 #endif /* !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) */
135 #ifndef OPENSSL_NO_TLS1_2
136 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
137 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
139 #ifndef OPENSSL_NO_TLS1_2
140 TLS1_CK_RSA_WITH_AES_256_SHA256,
141 TLS1_CK_RSA_WITH_AES_128_SHA256,
143 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
144 /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
145 TLS1_CK_RSA_WITH_AES_256_SHA,
146 TLS1_CK_RSA_WITH_AES_128_SHA,
150 static int test_default_cipherlist(SSL_CTX *ctx)
152 STACK_OF(SSL_CIPHER) *ciphers = NULL;
154 int i, ret = 0, num_expected_ciphers, num_ciphers;
155 uint32_t expected_cipher_id, cipher_id;
160 if (!TEST_ptr(ssl = SSL_new(ctx))
161 || !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl)))
164 num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order);
165 num_ciphers = sk_SSL_CIPHER_num(ciphers);
166 if (!TEST_int_eq(num_ciphers, num_expected_ciphers))
169 for (i = 0; i < num_ciphers; i++) {
170 expected_cipher_id = default_ciphers_in_order[i];
171 cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i));
172 if (!TEST_int_eq(cipher_id, expected_cipher_id)) {
173 TEST_info("Wrong cipher at position %d", i);
181 sk_SSL_CIPHER_free(ciphers);
186 static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture)
188 return fixture != NULL
189 && test_default_cipherlist(fixture->server)
190 && test_default_cipherlist(fixture->client);
193 #define SETUP_CIPHERLIST_TEST_FIXTURE() \
194 SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up)
196 #define EXECUTE_CIPHERLIST_TEST() \
197 EXECUTE_TEST(execute_test, tear_down)
199 static int test_default_cipherlist_implicit(void)
201 SETUP_CIPHERLIST_TEST_FIXTURE();
204 EXECUTE_CIPHERLIST_TEST();
208 static int test_default_cipherlist_explicit(void)
210 SETUP_CIPHERLIST_TEST_FIXTURE();
213 if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT"))
214 || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT")))
216 EXECUTE_CIPHERLIST_TEST();
220 /* SSL_CTX_set_cipher_list() should fail if it clears all TLSv1.2 ciphers. */
221 static int test_default_cipherlist_clear(void)
223 SETUP_CIPHERLIST_TEST_FIXTURE();
229 if (!TEST_int_eq(SSL_CTX_set_cipher_list(fixture->server, "no-such"), 0))
232 if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH))
235 s = SSL_new(fixture->client);
240 if (!TEST_int_eq(SSL_set_cipher_list(s, "no-such"), 0))
243 if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
244 SSL_R_NO_CIPHER_MATCH))
254 int setup_tests(void)
256 ADD_TEST(test_default_cipherlist_implicit);
257 ADD_TEST(test_default_cipherlist_explicit);
258 ADD_TEST(test_default_cipherlist_clear);