2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL licenses, (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 typedef struct cipherlist_test_fixture {
25 const char *test_case_name;
28 } CIPHERLIST_TEST_FIXTURE;
31 static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture)
33 if (fixture != NULL) {
34 SSL_CTX_free(fixture->server);
35 SSL_CTX_free(fixture->client);
36 fixture->server = fixture->client = NULL;
37 OPENSSL_free(fixture);
41 static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name)
43 CIPHERLIST_TEST_FIXTURE *fixture;
45 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
47 fixture->test_case_name = test_case_name;
48 if (!TEST_ptr(fixture->server = SSL_CTX_new(TLS_server_method()))
49 || !TEST_ptr(fixture->client = SSL_CTX_new(TLS_client_method()))) {
57 * All ciphers in the DEFAULT cipherlist meet the default security level.
58 * However, default supported ciphers exclude SRP and PSK ciphersuites
59 * for which no callbacks have been set up.
61 * Supported ciphers also exclude TLSv1.2 ciphers if TLSv1.2 is disabled,
62 * and individual disabled algorithms. However, NO_RSA, NO_AES and NO_SHA
63 * are currently broken and should be considered mission impossible in libssl.
65 static const uint32_t default_ciphers_in_order[] = {
66 #ifndef OPENSSL_NO_TLS1_3
67 TLS1_3_CK_AES_256_GCM_SHA384,
68 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
69 TLS1_3_CK_CHACHA20_POLY1305_SHA256,
71 TLS1_3_CK_AES_128_GCM_SHA256,
73 #ifndef OPENSSL_NO_TLS1_2
74 # ifndef OPENSSL_NO_EC
75 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
76 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
78 # ifndef OPENSSL_NO_DH
79 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
82 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
83 # ifndef OPENSSL_NO_EC
84 TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
85 TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305,
87 # ifndef OPENSSL_NO_DH
88 TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305,
90 # endif /* !OPENSSL_NO_CHACHA && !OPENSSL_NO_POLY1305 */
92 # ifndef OPENSSL_NO_EC
93 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
94 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
96 # ifndef OPENSSL_NO_DH
97 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
99 # ifndef OPENSSL_NO_EC
100 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
101 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
103 # ifndef OPENSSL_NO_DH
104 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
106 # ifndef OPENSSL_NO_EC
107 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
108 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
110 # ifndef OPENSSL_NO_DH
111 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
113 #endif /* !OPENSSL_NO_TLS1_2 */
115 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
116 /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
117 # ifndef OPENSSL_NO_EC
118 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
119 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
121 #ifndef OPENSSL_NO_DH
122 TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
124 # ifndef OPENSSL_NO_EC
125 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
126 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
128 # ifndef OPENSSL_NO_DH
129 TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
131 #endif /* !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) */
133 #ifndef OPENSSL_NO_TLS1_2
134 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
135 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
137 #ifndef OPENSSL_NO_TLS1_2
138 TLS1_CK_RSA_WITH_AES_256_SHA256,
139 TLS1_CK_RSA_WITH_AES_128_SHA256,
141 #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
142 /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
143 TLS1_CK_RSA_WITH_AES_256_SHA,
144 TLS1_CK_RSA_WITH_AES_128_SHA,
148 static int test_default_cipherlist(SSL_CTX *ctx)
150 STACK_OF(SSL_CIPHER) *ciphers = NULL;
152 int i, ret = 0, num_expected_ciphers, num_ciphers;
153 uint32_t expected_cipher_id, cipher_id;
158 if (!TEST_ptr(ssl = SSL_new(ctx))
159 || !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl)))
162 num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order);
163 num_ciphers = sk_SSL_CIPHER_num(ciphers);
164 if (!TEST_int_eq(num_ciphers, num_expected_ciphers))
167 for (i = 0; i < num_ciphers; i++) {
168 expected_cipher_id = default_ciphers_in_order[i];
169 cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i));
170 if (!TEST_int_eq(cipher_id, expected_cipher_id)) {
171 TEST_info("Wrong cipher at position %d", i);
179 sk_SSL_CIPHER_free(ciphers);
184 static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture)
186 return fixture != NULL
187 && test_default_cipherlist(fixture->server)
188 && test_default_cipherlist(fixture->client);
191 #define SETUP_CIPHERLIST_TEST_FIXTURE() \
192 SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up)
194 #define EXECUTE_CIPHERLIST_TEST() \
195 EXECUTE_TEST(execute_test, tear_down)
197 static int test_default_cipherlist_implicit(void)
199 SETUP_CIPHERLIST_TEST_FIXTURE();
202 EXECUTE_CIPHERLIST_TEST();
206 static int test_default_cipherlist_explicit(void)
208 SETUP_CIPHERLIST_TEST_FIXTURE();
211 if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT"))
212 || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT")))
214 EXECUTE_CIPHERLIST_TEST();
220 ADD_TEST(test_default_cipherlist_implicit);
221 ADD_TEST(test_default_cipherlist_explicit);