cipherlist_test.c 6.3 KB
Newer Older
E
Emilia Kasper 已提交
1
/*
P
Pauli 已提交
2
 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
E
Emilia Kasper 已提交
3 4 5 6 7 8 9 10 11
 *
 * Licensed under the OpenSSL licenses, (the "License");
 * 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>
P
Pauli 已提交
12
#include <string.h>
E
Emilia Kasper 已提交
13 14 15 16 17 18 19 20

#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>

R
Rich Salz 已提交
21
#include "internal/nelem.h"
E
Emilia Kasper 已提交
22 23 24 25 26 27 28 29 30
#include "testutil.h"

typedef struct cipherlist_test_fixture {
    const char *test_case_name;
    SSL_CTX *server;
    SSL_CTX *client;
} CIPHERLIST_TEST_FIXTURE;


P
Pauli 已提交
31
static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture)
E
Emilia Kasper 已提交
32
{
P
Pauli 已提交
33 34 35 36
    if (fixture != NULL) {
        SSL_CTX_free(fixture->server);
        SSL_CTX_free(fixture->client);
        fixture->server = fixture->client = NULL;
P
Pauli 已提交
37
        OPENSSL_free(fixture);
P
Pauli 已提交
38 39 40 41 42
    }
}

static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name)
{
P
Pauli 已提交
43
    CIPHERLIST_TEST_FIXTURE *fixture;
P
Pauli 已提交
44

P
Pauli 已提交
45 46 47 48 49 50
    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);
P
Pauli 已提交
51 52
        return NULL;
    }
P
Pauli 已提交
53
    return fixture;
E
Emilia Kasper 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}

/*
 * 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[] = {
#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

M
Matt Caswell 已提交
75
# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
E
Emilia Kasper 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#  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 */

108 109 110
#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
E
Emilia Kasper 已提交
111 112
    TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
    TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
113 114
# endif
 #ifndef OPENSSL_NO_DH
E
Emilia Kasper 已提交
115
    TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
116 117
# endif
# ifndef OPENSSL_NO_EC
E
Emilia Kasper 已提交
118 119
    TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
    TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
120 121
# endif
# ifndef OPENSSL_NO_DH
E
Emilia Kasper 已提交
122
    TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
123 124
# endif
#endif /* !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) */
E
Emilia Kasper 已提交
125 126 127 128

#ifndef OPENSSL_NO_TLS1_2
    TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
    TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
129 130
#endif
#ifndef OPENSSL_NO_TLS1_3
D
Dr. Stephen Henson 已提交
131
    TLS1_3_CK_AES_256_GCM_SHA384,
M
Matt Caswell 已提交
132
# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
D
Dr. Stephen Henson 已提交
133
    TLS1_3_CK_CHACHA20_POLY1305_SHA256,
M
Matt Caswell 已提交
134
# endif
135 136 137
    TLS1_3_CK_AES_128_GCM_SHA256,
#endif
#ifndef OPENSSL_NO_TLS1_2
E
Emilia Kasper 已提交
138 139 140
    TLS1_CK_RSA_WITH_AES_256_SHA256,
    TLS1_CK_RSA_WITH_AES_128_SHA256,
#endif
141 142
#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 */
E
Emilia Kasper 已提交
143 144
    TLS1_CK_RSA_WITH_AES_256_SHA,
    TLS1_CK_RSA_WITH_AES_128_SHA,
145
#endif
E
Emilia Kasper 已提交
146 147 148 149
};

static int test_default_cipherlist(SSL_CTX *ctx)
{
P
Pauli 已提交
150 151
    STACK_OF(SSL_CIPHER) *ciphers = NULL;
    SSL *ssl = NULL;
E
Emilia Kasper 已提交
152 153 154
    int i, ret = 0, num_expected_ciphers, num_ciphers;
    uint32_t expected_cipher_id, cipher_id;

P
Pauli 已提交
155 156 157 158 159 160
    if (ctx == NULL)
        return 0;

    if (!TEST_ptr(ssl = SSL_new(ctx))
            || !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl)))
        goto err;
E
Emilia Kasper 已提交
161 162 163

    num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order);
    num_ciphers = sk_SSL_CIPHER_num(ciphers);
P
Pauli 已提交
164
    if (!TEST_int_eq(num_ciphers, num_expected_ciphers))
E
Emilia Kasper 已提交
165 166 167 168 169
        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));
P
Pauli 已提交
170 171
        if (!TEST_int_eq(cipher_id, expected_cipher_id)) {
            TEST_info("Wrong cipher at position %d", i);
E
Emilia Kasper 已提交
172 173 174 175 176 177 178 179 180 181 182 183
            goto err;
        }
    }

    ret = 1;

 err:
    sk_SSL_CIPHER_free(ciphers);
    SSL_free(ssl);
    return ret;
}

P
Pauli 已提交
184
static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture)
E
Emilia Kasper 已提交
185
{
P
Pauli 已提交
186 187 188
    return fixture != NULL
        && test_default_cipherlist(fixture->server)
        && test_default_cipherlist(fixture->client);
E
Emilia Kasper 已提交
189 190 191
}

#define SETUP_CIPHERLIST_TEST_FIXTURE() \
192
    SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up)
E
Emilia Kasper 已提交
193 194 195 196

#define EXECUTE_CIPHERLIST_TEST() \
    EXECUTE_TEST(execute_test, tear_down)

197
static int test_default_cipherlist_implicit(void)
E
Emilia Kasper 已提交
198 199
{
    SETUP_CIPHERLIST_TEST_FIXTURE();
200 201
    if (fixture == NULL)
        return 0;
E
Emilia Kasper 已提交
202
    EXECUTE_CIPHERLIST_TEST();
203
    return result;
E
Emilia Kasper 已提交
204 205
}

206
static int test_default_cipherlist_explicit(void)
E
Emilia Kasper 已提交
207 208
{
    SETUP_CIPHERLIST_TEST_FIXTURE();
P
Pauli 已提交
209 210 211 212 213
    if (fixture == NULL)
        return 0;
    if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT"))
            || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT")))
        tear_down(fixture);
E
Emilia Kasper 已提交
214
    EXECUTE_CIPHERLIST_TEST();
215
    return result;
E
Emilia Kasper 已提交
216 217
}

218
int setup_tests()
E
Emilia Kasper 已提交
219 220 221
{
    ADD_TEST(test_default_cipherlist_implicit);
    ADD_TEST(test_default_cipherlist_explicit);
222
    return 1;
E
Emilia Kasper 已提交
223
}