ssl_test_ctx_test.c 9.0 KB
Newer Older
E
Emilia Kasper 已提交
1
/*
M
Matt Caswell 已提交
2
 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
E
Emilia Kasper 已提交
3
 *
R
Rich Salz 已提交
4 5 6
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
E
Emilia Kasper 已提交
7 8 9 10 11 12 13 14 15
 * https://www.openssl.org/source/license.html
 */

/*
 * Ideally, CONF should offer standard parsing methods and cover them
 * in tests. But since we have no CONF tests, we use a custom test for now.
 */

#include <stdio.h>
16
#include <string.h>
E
Emilia Kasper 已提交
17

R
Rich Salz 已提交
18
#include "internal/nelem.h"
E
Emilia Kasper 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include "ssl_test_ctx.h"
#include "testutil.h"
#include <openssl/e_os2.h>
#include <openssl/err.h>
#include <openssl/conf.h>
#include <openssl/ssl.h>

static CONF *conf = NULL;

typedef struct ssl_test_ctx_test_fixture {
    const char *test_case_name;
    const char *test_section;
    /* Expected parsed configuration. */
    SSL_TEST_CTX *expected_ctx;
} SSL_TEST_CTX_TEST_FIXTURE;

E
Emilia Kasper 已提交
35

R
Rich Salz 已提交
36 37
static int clientconf_eq(SSL_TEST_CLIENT_CONF *conf1,
                         SSL_TEST_CLIENT_CONF *conf2)
E
Emilia Kasper 已提交
38
{
R
Rich Salz 已提交
39 40 41 42
    if (!TEST_int_eq(conf1->verify_callback, conf2->verify_callback)
            || !TEST_int_eq(conf1->servername, conf2->servername)
            || !TEST_str_eq(conf1->npn_protocols, conf2->npn_protocols)
            || !TEST_str_eq(conf1->alpn_protocols, conf2->alpn_protocols)
43 44 45
            || !TEST_int_eq(conf1->ct_validation, conf2->ct_validation)
            || !TEST_int_eq(conf1->max_fragment_len_mode,
                            conf2->max_fragment_len_mode))
E
Emilia Kasper 已提交
46 47 48 49
        return 0;
    return 1;
}

R
Rich Salz 已提交
50 51
static int serverconf_eq(SSL_TEST_SERVER_CONF *serv,
                         SSL_TEST_SERVER_CONF *serv2)
E
Emilia Kasper 已提交
52
{
R
Rich Salz 已提交
53 54 55 56 57
    if (!TEST_int_eq(serv->servername_callback, serv2->servername_callback)
            || !TEST_str_eq(serv->npn_protocols, serv2->npn_protocols)
            || !TEST_str_eq(serv->alpn_protocols, serv2->alpn_protocols)
            || !TEST_int_eq(serv->broken_session_ticket,
                            serv2->broken_session_ticket)
T
Todd Short 已提交
58 59
            || !TEST_str_eq(serv->session_ticket_app_data,
                            serv2->session_ticket_app_data)
R
Rich Salz 已提交
60
            || !TEST_int_eq(serv->cert_status, serv2->cert_status))
E
Emilia Kasper 已提交
61 62 63 64
        return 0;
    return 1;
}

R
Rich Salz 已提交
65 66
static int extraconf_eq(SSL_TEST_EXTRA_CONF *extra,
                        SSL_TEST_EXTRA_CONF *extra2)
E
Emilia Kasper 已提交
67
{
R
Rich Salz 已提交
68 69 70 71 72
    if (!TEST_true(clientconf_eq(&extra->client, &extra2->client))
            || !TEST_true(serverconf_eq(&extra->server, &extra2->server))
            || !TEST_true(serverconf_eq(&extra->server2, &extra2->server2)))
        return 0;
    return 1;
E
Emilia Kasper 已提交
73 74
}

R
Rich Salz 已提交
75
static int testctx_eq(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
E
Emilia Kasper 已提交
76
{
R
Rich Salz 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    if (!TEST_int_eq(ctx->method, ctx2->method)
            || !TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode)
            || !TEST_int_eq(ctx->app_data_size, ctx2->app_data_size)
            || !TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size)
            || !extraconf_eq(&ctx->extra, &ctx2->extra)
            || !extraconf_eq(&ctx->resume_extra, &ctx2->resume_extra)
            || !TEST_int_eq(ctx->expected_result, ctx2->expected_result)
            || !TEST_int_eq(ctx->expected_client_alert,
                            ctx2->expected_client_alert)
            || !TEST_int_eq(ctx->expected_server_alert,
                            ctx2->expected_server_alert)
            || !TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol)
            || !TEST_int_eq(ctx->expected_servername, ctx2->expected_servername)
            || !TEST_int_eq(ctx->session_ticket_expected,
                            ctx2->session_ticket_expected)
            || !TEST_int_eq(ctx->compression_expected,
                            ctx2->compression_expected)
            || !TEST_str_eq(ctx->expected_npn_protocol,
                            ctx2->expected_npn_protocol)
            || !TEST_str_eq(ctx->expected_alpn_protocol,
                            ctx2->expected_alpn_protocol)
98 99
            || !TEST_str_eq(ctx->expected_cipher,
                            ctx2->expected_cipher)
T
Todd Short 已提交
100 101
            || !TEST_str_eq(ctx->expected_session_ticket_app_data,
                            ctx2->expected_session_ticket_app_data)
R
Rich Salz 已提交
102
            || !TEST_int_eq(ctx->resumption_expected,
103 104 105
                            ctx2->resumption_expected)
            || !TEST_int_eq(ctx->session_id_expected,
                            ctx2->session_id_expected))
106
        return 0;
E
Emilia Kasper 已提交
107 108 109
    return 1;
}

P
Pauli 已提交
110
static SSL_TEST_CTX_TEST_FIXTURE *set_up(const char *const test_case_name)
E
Emilia Kasper 已提交
111
{
P
Pauli 已提交
112 113 114 115 116 117 118 119 120
    SSL_TEST_CTX_TEST_FIXTURE *fixture;

    if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
        return NULL;
    fixture->test_case_name = test_case_name;
    if (!TEST_ptr(fixture->expected_ctx = SSL_TEST_CTX_new())) {
        OPENSSL_free(fixture);
        return NULL;
    }
E
Emilia Kasper 已提交
121 122 123
    return fixture;
}

P
Pauli 已提交
124
static int execute_test(SSL_TEST_CTX_TEST_FIXTURE *fixture)
E
Emilia Kasper 已提交
125
{
E
Emilia Kasper 已提交
126
    int success = 0;
R
Rich Salz 已提交
127
    SSL_TEST_CTX *ctx;
E
Emilia Kasper 已提交
128

P
Pauli 已提交
129 130
    if (!TEST_ptr(ctx = SSL_TEST_CTX_create(conf, fixture->test_section))
            || !testctx_eq(ctx, fixture->expected_ctx))
E
Emilia Kasper 已提交
131 132
        goto err;

E
Emilia Kasper 已提交
133
    success = 1;
E
Emilia Kasper 已提交
134 135
 err:
    SSL_TEST_CTX_free(ctx);
E
Emilia Kasper 已提交
136
    return success;
E
Emilia Kasper 已提交
137 138
}

P
Pauli 已提交
139
static void tear_down(SSL_TEST_CTX_TEST_FIXTURE *fixture)
E
Emilia Kasper 已提交
140
{
P
Pauli 已提交
141 142
    SSL_TEST_CTX_free(fixture->expected_ctx);
    OPENSSL_free(fixture);
E
Emilia Kasper 已提交
143 144
}

R
Rich Salz 已提交
145
#define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
146
    SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up);
R
Rich Salz 已提交
147
#define EXECUTE_SSL_TEST_CTX_TEST() \
E
Emilia Kasper 已提交
148 149
    EXECUTE_TEST(execute_test, tear_down)

150
static int test_empty_configuration(void)
E
Emilia Kasper 已提交
151 152
{
    SETUP_SSL_TEST_CTX_TEST_FIXTURE();
153 154
    if (fixture == NULL)
        return 0;
P
Pauli 已提交
155 156
    fixture->test_section = "ssltest_default";
    fixture->expected_ctx->expected_result = SSL_TEST_SUCCESS;
E
Emilia Kasper 已提交
157
    EXECUTE_SSL_TEST_CTX_TEST();
158
    return result;
E
Emilia Kasper 已提交
159 160
}

161
static int test_good_configuration(void)
E
Emilia Kasper 已提交
162 163
{
    SETUP_SSL_TEST_CTX_TEST_FIXTURE();
164 165
    if (fixture == NULL)
        return 0;
P
Pauli 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
    fixture->test_section = "ssltest_good";
    fixture->expected_ctx->method = SSL_TEST_METHOD_DTLS;
    fixture->expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
    fixture->expected_ctx->app_data_size = 1024;
    fixture->expected_ctx->max_fragment_size = 2048;

    fixture->expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
    fixture->expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
    fixture->expected_ctx->expected_server_alert = 0;  /* No alert. */
    fixture->expected_ctx->expected_protocol = TLS1_1_VERSION;
    fixture->expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
    fixture->expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
    fixture->expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
179
    fixture->expected_ctx->session_id_expected = SSL_TEST_SESSION_ID_IGNORE;
P
Pauli 已提交
180 181 182
    fixture->expected_ctx->resumption_expected = 1;

    fixture->expected_ctx->extra.client.verify_callback =
E
Emilia Kasper 已提交
183
        SSL_TEST_VERIFY_REJECT_ALL;
P
Pauli 已提交
184 185
    fixture->expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
    fixture->expected_ctx->extra.client.npn_protocols =
E
Emilia Kasper 已提交
186
        OPENSSL_strdup("foo,bar");
P
Pauli 已提交
187
    if (!TEST_ptr(fixture->expected_ctx->extra.client.npn_protocols))
P
Pauli 已提交
188
        goto err;
189
    fixture->expected_ctx->extra.client.max_fragment_len_mode = 0;
E
Emilia Kasper 已提交
190

P
Pauli 已提交
191
    fixture->expected_ctx->extra.server.servername_callback =
E
Emilia Kasper 已提交
192
        SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
P
Pauli 已提交
193
    fixture->expected_ctx->extra.server.broken_session_ticket = 1;
E
Emilia Kasper 已提交
194

P
Pauli 已提交
195
    fixture->expected_ctx->resume_extra.server2.alpn_protocols =
E
Emilia Kasper 已提交
196
        OPENSSL_strdup("baz");
P
Pauli 已提交
197
    if (!TEST_ptr(fixture->expected_ctx->resume_extra.server2.alpn_protocols))
P
Pauli 已提交
198
        goto err;
E
Emilia Kasper 已提交
199

P
Pauli 已提交
200
    fixture->expected_ctx->resume_extra.client.ct_validation =
201 202
        SSL_TEST_CT_VALIDATION_STRICT;

E
Emilia Kasper 已提交
203
    EXECUTE_SSL_TEST_CTX_TEST();
204
    return result;
P
Pauli 已提交
205 206 207 208

err:
    tear_down(fixture);
    return 0;
E
Emilia Kasper 已提交
209 210 211 212
}

static const char *bad_configurations[] = {
    "ssltest_unknown_option",
E
Emilia Kasper 已提交
213
    "ssltest_wrong_section",
E
Emilia Kasper 已提交
214 215 216
    "ssltest_unknown_expected_result",
    "ssltest_unknown_alert",
    "ssltest_unknown_protocol",
217
    "ssltest_unknown_verify_callback",
T
Todd Short 已提交
218
    "ssltest_unknown_servername",
219
    "ssltest_unknown_servername_callback",
T
Todd Short 已提交
220
    "ssltest_unknown_session_ticket_expected",
M
Matt Caswell 已提交
221
    "ssltest_unknown_compression_expected",
222
    "ssltest_unknown_session_id_expected",
223
    "ssltest_unknown_method",
224 225
    "ssltest_unknown_handshake_mode",
    "ssltest_unknown_resumption_expected",
226
    "ssltest_unknown_ct_validation",
227
    "ssltest_invalid_max_fragment_len",
E
Emilia Kasper 已提交
228 229 230 231
};

static int test_bad_configuration(int idx)
{
R
Rich Salz 已提交
232
    SSL_TEST_CTX *ctx;
233

R
Rich Salz 已提交
234 235
    if (!TEST_ptr_null(ctx = SSL_TEST_CTX_create(conf,
                                                 bad_configurations[idx]))) {
E
Emilia Kasper 已提交
236 237 238 239 240
        SSL_TEST_CTX_free(ctx);
        return 0;
    }

    return 1;
E
Emilia Kasper 已提交
241 242
}

243
int setup_tests(void)
E
Emilia Kasper 已提交
244
{
245 246 247 248 249 250
    if (!TEST_ptr(conf = NCONF_new(NULL)))
        return 0;
    /* argument should point to test/ssl_test_ctx_test.conf */
    if (!TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)) {
        TEST_note("Missing file argument");
        return 0;
R
Rich Salz 已提交
251
    }
E
Emilia Kasper 已提交
252 253 254 255

    ADD_TEST(test_empty_configuration);
    ADD_TEST(test_good_configuration);
    ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
256 257
    return 1;
}
E
Emilia Kasper 已提交
258

259 260
void cleanup_tests(void)
{
E
Emilia Kasper 已提交
261 262
    NCONF_free(conf);
}