2016-04-06 22:03:06 +08:00
|
|
|
/*
|
2021-04-08 20:04:41 +08:00
|
|
|
* Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2016-04-06 22:03:06 +08:00
|
|
|
*
|
2018-12-06 20:05:25 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License");
|
2016-04-06 22:03:06 +08:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
* or in the file LICENSE in the source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-06-22 12:00:55 +08:00
|
|
|
#include <string.h>
|
2016-04-06 22:03:06 +08:00
|
|
|
|
|
|
|
#include <openssl/opensslconf.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/e_os2.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/ssl3.h>
|
|
|
|
#include <openssl/tls1.h>
|
|
|
|
|
2017-08-22 20:35:43 +08:00
|
|
|
#include "internal/nelem.h"
|
2016-04-06 22:03:06 +08:00
|
|
|
#include "testutil.h"
|
|
|
|
|
|
|
|
typedef struct cipherlist_test_fixture {
|
|
|
|
const char *test_case_name;
|
|
|
|
SSL_CTX *server;
|
|
|
|
SSL_CTX *client;
|
|
|
|
} CIPHERLIST_TEST_FIXTURE;
|
|
|
|
|
|
|
|
|
2017-06-22 12:00:55 +08:00
|
|
|
static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture)
|
2016-04-06 22:03:06 +08:00
|
|
|
{
|
2017-06-22 12:00:55 +08:00
|
|
|
if (fixture != NULL) {
|
|
|
|
SSL_CTX_free(fixture->server);
|
|
|
|
SSL_CTX_free(fixture->client);
|
|
|
|
fixture->server = fixture->client = NULL;
|
2017-08-02 11:48:29 +08:00
|
|
|
OPENSSL_free(fixture);
|
2017-06-22 12:00:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name)
|
|
|
|
{
|
2017-08-02 11:48:29 +08:00
|
|
|
CIPHERLIST_TEST_FIXTURE *fixture;
|
2017-06-22 12:00:55 +08:00
|
|
|
|
2017-08-02 11:48:29 +08:00
|
|
|
if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
|
|
|
|
return NULL;
|
|
|
|
fixture->test_case_name = test_case_name;
|
|
|
|
if (!TEST_ptr(fixture->server = SSL_CTX_new(TLS_server_method()))
|
|
|
|
|| !TEST_ptr(fixture->client = SSL_CTX_new(TLS_client_method()))) {
|
|
|
|
tear_down(fixture);
|
2017-06-22 12:00:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-02 11:48:29 +08:00
|
|
|
return fixture;
|
2016-04-06 22:03:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All ciphers in the DEFAULT cipherlist meet the default security level.
|
|
|
|
* However, default supported ciphers exclude SRP and PSK ciphersuites
|
|
|
|
* for which no callbacks have been set up.
|
|
|
|
*
|
|
|
|
* Supported ciphers also exclude TLSv1.2 ciphers if TLSv1.2 is disabled,
|
|
|
|
* and individual disabled algorithms. However, NO_RSA, NO_AES and NO_SHA
|
|
|
|
* are currently broken and should be considered mission impossible in libssl.
|
|
|
|
*/
|
|
|
|
static const uint32_t default_ciphers_in_order[] = {
|
2018-02-16 19:26:02 +08:00
|
|
|
#ifndef OPENSSL_NO_TLS1_3
|
|
|
|
TLS1_3_CK_AES_256_GCM_SHA384,
|
|
|
|
# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
|
|
|
|
TLS1_3_CK_CHACHA20_POLY1305_SHA256,
|
|
|
|
# endif
|
|
|
|
TLS1_3_CK_AES_128_GCM_SHA256,
|
|
|
|
#endif
|
2016-04-06 22:03:06 +08:00
|
|
|
#ifndef OPENSSL_NO_TLS1_2
|
|
|
|
# ifndef OPENSSL_NO_EC
|
|
|
|
TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_DH
|
|
|
|
TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
# endif
|
|
|
|
|
2018-02-08 23:45:35 +08:00
|
|
|
# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
|
2016-04-06 22:03:06 +08:00
|
|
|
# ifndef OPENSSL_NO_EC
|
|
|
|
TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
|
|
|
TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_DH
|
|
|
|
TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305,
|
|
|
|
# endif
|
|
|
|
# endif /* !OPENSSL_NO_CHACHA && !OPENSSL_NO_POLY1305 */
|
|
|
|
|
|
|
|
# ifndef OPENSSL_NO_EC
|
|
|
|
TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_DH
|
|
|
|
TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_EC
|
|
|
|
TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
|
|
|
|
TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
|
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_DH
|
|
|
|
TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
|
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_EC
|
|
|
|
TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
|
|
|
|
TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
|
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_DH
|
|
|
|
TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
|
|
|
|
# endif
|
|
|
|
#endif /* !OPENSSL_NO_TLS1_2 */
|
|
|
|
|
2018-02-09 18:19:14 +08:00
|
|
|
#if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
|
|
|
|
/* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
|
|
|
|
# ifndef OPENSSL_NO_EC
|
2016-04-06 22:03:06 +08:00
|
|
|
TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
|
|
TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
2018-02-09 18:19:14 +08:00
|
|
|
# endif
|
|
|
|
#ifndef OPENSSL_NO_DH
|
2016-04-06 22:03:06 +08:00
|
|
|
TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
|
2018-02-09 18:19:14 +08:00
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_EC
|
2016-04-06 22:03:06 +08:00
|
|
|
TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
2018-02-09 18:19:14 +08:00
|
|
|
# endif
|
|
|
|
# ifndef OPENSSL_NO_DH
|
2016-04-06 22:03:06 +08:00
|
|
|
TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
|
2018-02-09 18:19:14 +08:00
|
|
|
# endif
|
|
|
|
#endif /* !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) */
|
2016-04-06 22:03:06 +08:00
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_TLS1_2
|
|
|
|
TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
|
2016-10-22 00:39:33 +08:00
|
|
|
#endif
|
|
|
|
#ifndef OPENSSL_NO_TLS1_2
|
2016-04-06 22:03:06 +08:00
|
|
|
TLS1_CK_RSA_WITH_AES_256_SHA256,
|
|
|
|
TLS1_CK_RSA_WITH_AES_128_SHA256,
|
|
|
|
#endif
|
2018-02-09 18:19:14 +08:00
|
|
|
#if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
|
|
|
|
/* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
|
2016-04-06 22:03:06 +08:00
|
|
|
TLS1_CK_RSA_WITH_AES_256_SHA,
|
|
|
|
TLS1_CK_RSA_WITH_AES_128_SHA,
|
2018-02-09 18:19:14 +08:00
|
|
|
#endif
|
2016-04-06 22:03:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int test_default_cipherlist(SSL_CTX *ctx)
|
|
|
|
{
|
2017-06-22 12:00:55 +08:00
|
|
|
STACK_OF(SSL_CIPHER) *ciphers = NULL;
|
|
|
|
SSL *ssl = NULL;
|
2016-04-06 22:03:06 +08:00
|
|
|
int i, ret = 0, num_expected_ciphers, num_ciphers;
|
|
|
|
uint32_t expected_cipher_id, cipher_id;
|
|
|
|
|
2017-06-22 12:00:55 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!TEST_ptr(ssl = SSL_new(ctx))
|
|
|
|
|| !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl)))
|
|
|
|
goto err;
|
2016-04-06 22:03:06 +08:00
|
|
|
|
|
|
|
num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order);
|
|
|
|
num_ciphers = sk_SSL_CIPHER_num(ciphers);
|
2017-03-22 12:27:55 +08:00
|
|
|
if (!TEST_int_eq(num_ciphers, num_expected_ciphers))
|
2016-04-06 22:03:06 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (i = 0; i < num_ciphers; i++) {
|
|
|
|
expected_cipher_id = default_ciphers_in_order[i];
|
|
|
|
cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i));
|
2017-03-22 12:27:55 +08:00
|
|
|
if (!TEST_int_eq(cipher_id, expected_cipher_id)) {
|
|
|
|
TEST_info("Wrong cipher at position %d", i);
|
2016-04-06 22:03:06 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
sk_SSL_CIPHER_free(ciphers);
|
|
|
|
SSL_free(ssl);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:00:55 +08:00
|
|
|
static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture)
|
2016-04-06 22:03:06 +08:00
|
|
|
{
|
2017-06-22 12:00:55 +08:00
|
|
|
return fixture != NULL
|
|
|
|
&& test_default_cipherlist(fixture->server)
|
|
|
|
&& test_default_cipherlist(fixture->client);
|
2016-04-06 22:03:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SETUP_CIPHERLIST_TEST_FIXTURE() \
|
2017-08-04 08:49:38 +08:00
|
|
|
SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up)
|
2016-04-06 22:03:06 +08:00
|
|
|
|
|
|
|
#define EXECUTE_CIPHERLIST_TEST() \
|
|
|
|
EXECUTE_TEST(execute_test, tear_down)
|
|
|
|
|
2017-08-16 05:39:03 +08:00
|
|
|
static int test_default_cipherlist_implicit(void)
|
2016-04-06 22:03:06 +08:00
|
|
|
{
|
|
|
|
SETUP_CIPHERLIST_TEST_FIXTURE();
|
|
|
|
EXECUTE_CIPHERLIST_TEST();
|
2017-08-04 08:49:38 +08:00
|
|
|
return result;
|
2016-04-06 22:03:06 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:39:03 +08:00
|
|
|
static int test_default_cipherlist_explicit(void)
|
2016-04-06 22:03:06 +08:00
|
|
|
{
|
|
|
|
SETUP_CIPHERLIST_TEST_FIXTURE();
|
2017-06-22 12:00:55 +08:00
|
|
|
if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT"))
|
2021-03-18 12:08:06 +08:00
|
|
|
|| !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT"))) {
|
2017-06-22 12:00:55 +08:00
|
|
|
tear_down(fixture);
|
2021-03-18 12:08:06 +08:00
|
|
|
fixture = NULL;
|
|
|
|
}
|
2016-04-06 22:03:06 +08:00
|
|
|
EXECUTE_CIPHERLIST_TEST();
|
2017-08-04 08:49:38 +08:00
|
|
|
return result;
|
2016-04-06 22:03:06 +08:00
|
|
|
}
|
|
|
|
|
2018-11-27 05:58:52 +08:00
|
|
|
/* SSL_CTX_set_cipher_list() should fail if it clears all TLSv1.2 ciphers. */
|
|
|
|
static int test_default_cipherlist_clear(void)
|
|
|
|
{
|
|
|
|
SSL *s = NULL;
|
2020-08-13 02:24:26 +08:00
|
|
|
SETUP_CIPHERLIST_TEST_FIXTURE();
|
2018-11-27 05:58:52 +08:00
|
|
|
|
|
|
|
if (!TEST_int_eq(SSL_CTX_set_cipher_list(fixture->server, "no-such"), 0))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
s = SSL_new(fixture->client);
|
|
|
|
|
|
|
|
if (!TEST_ptr(s))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (!TEST_int_eq(SSL_set_cipher_list(s, "no-such"), 0))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
|
|
|
|
SSL_R_NO_CIPHER_MATCH))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
result = 1;
|
|
|
|
end:
|
|
|
|
SSL_free(s);
|
|
|
|
tear_down(fixture);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-09 23:09:50 +08:00
|
|
|
int setup_tests(void)
|
2016-04-06 22:03:06 +08:00
|
|
|
{
|
|
|
|
ADD_TEST(test_default_cipherlist_implicit);
|
|
|
|
ADD_TEST(test_default_cipherlist_explicit);
|
2018-11-27 05:58:52 +08:00
|
|
|
ADD_TEST(test_default_cipherlist_clear);
|
2017-07-18 09:48:27 +08:00
|
|
|
return 1;
|
2016-04-06 22:03:06 +08:00
|
|
|
}
|