1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * Ideally, CONF should offer standard parsing methods and cover them
12e1051a39Sopenharmony_ci * in tests. But since we have no CONF tests, we use a custom test for now.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ci#include <stdio.h>
16e1051a39Sopenharmony_ci#include <string.h>
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ci#include "internal/nelem.h"
19e1051a39Sopenharmony_ci#include "helpers/ssl_test_ctx.h"
20e1051a39Sopenharmony_ci#include "testutil.h"
21e1051a39Sopenharmony_ci#include <openssl/e_os2.h>
22e1051a39Sopenharmony_ci#include <openssl/err.h>
23e1051a39Sopenharmony_ci#include <openssl/conf.h>
24e1051a39Sopenharmony_ci#include <openssl/ssl.h>
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_cistatic CONF *conf = NULL;
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_citypedef struct ssl_test_ctx_test_fixture {
29e1051a39Sopenharmony_ci    const char *test_case_name;
30e1051a39Sopenharmony_ci    const char *test_section;
31e1051a39Sopenharmony_ci    /* Expected parsed configuration. */
32e1051a39Sopenharmony_ci    SSL_TEST_CTX *expected_ctx;
33e1051a39Sopenharmony_ci} SSL_TEST_CTX_TEST_FIXTURE;
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_cistatic int clientconf_eq(SSL_TEST_CLIENT_CONF *conf1,
37e1051a39Sopenharmony_ci                         SSL_TEST_CLIENT_CONF *conf2)
38e1051a39Sopenharmony_ci{
39e1051a39Sopenharmony_ci    if (!TEST_int_eq(conf1->verify_callback, conf2->verify_callback)
40e1051a39Sopenharmony_ci            || !TEST_int_eq(conf1->servername, conf2->servername)
41e1051a39Sopenharmony_ci            || !TEST_str_eq(conf1->npn_protocols, conf2->npn_protocols)
42e1051a39Sopenharmony_ci            || !TEST_str_eq(conf1->alpn_protocols, conf2->alpn_protocols)
43e1051a39Sopenharmony_ci            || !TEST_int_eq(conf1->ct_validation, conf2->ct_validation)
44e1051a39Sopenharmony_ci            || !TEST_int_eq(conf1->max_fragment_len_mode,
45e1051a39Sopenharmony_ci                            conf2->max_fragment_len_mode))
46e1051a39Sopenharmony_ci        return 0;
47e1051a39Sopenharmony_ci    return 1;
48e1051a39Sopenharmony_ci}
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_cistatic int serverconf_eq(SSL_TEST_SERVER_CONF *serv,
51e1051a39Sopenharmony_ci                         SSL_TEST_SERVER_CONF *serv2)
52e1051a39Sopenharmony_ci{
53e1051a39Sopenharmony_ci    if (!TEST_int_eq(serv->servername_callback, serv2->servername_callback)
54e1051a39Sopenharmony_ci            || !TEST_str_eq(serv->npn_protocols, serv2->npn_protocols)
55e1051a39Sopenharmony_ci            || !TEST_str_eq(serv->alpn_protocols, serv2->alpn_protocols)
56e1051a39Sopenharmony_ci            || !TEST_int_eq(serv->broken_session_ticket,
57e1051a39Sopenharmony_ci                            serv2->broken_session_ticket)
58e1051a39Sopenharmony_ci            || !TEST_str_eq(serv->session_ticket_app_data,
59e1051a39Sopenharmony_ci                            serv2->session_ticket_app_data)
60e1051a39Sopenharmony_ci            || !TEST_int_eq(serv->cert_status, serv2->cert_status))
61e1051a39Sopenharmony_ci        return 0;
62e1051a39Sopenharmony_ci    return 1;
63e1051a39Sopenharmony_ci}
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_cistatic int extraconf_eq(SSL_TEST_EXTRA_CONF *extra,
66e1051a39Sopenharmony_ci                        SSL_TEST_EXTRA_CONF *extra2)
67e1051a39Sopenharmony_ci{
68e1051a39Sopenharmony_ci    if (!TEST_true(clientconf_eq(&extra->client, &extra2->client))
69e1051a39Sopenharmony_ci            || !TEST_true(serverconf_eq(&extra->server, &extra2->server))
70e1051a39Sopenharmony_ci            || !TEST_true(serverconf_eq(&extra->server2, &extra2->server2)))
71e1051a39Sopenharmony_ci        return 0;
72e1051a39Sopenharmony_ci    return 1;
73e1051a39Sopenharmony_ci}
74e1051a39Sopenharmony_ci
75e1051a39Sopenharmony_cistatic int testctx_eq(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
76e1051a39Sopenharmony_ci{
77e1051a39Sopenharmony_ci    if (!TEST_int_eq(ctx->method, ctx2->method)
78e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode)
79e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->app_data_size, ctx2->app_data_size)
80e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size)
81e1051a39Sopenharmony_ci            || !extraconf_eq(&ctx->extra, &ctx2->extra)
82e1051a39Sopenharmony_ci            || !extraconf_eq(&ctx->resume_extra, &ctx2->resume_extra)
83e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->expected_result, ctx2->expected_result)
84e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->expected_client_alert,
85e1051a39Sopenharmony_ci                            ctx2->expected_client_alert)
86e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->expected_server_alert,
87e1051a39Sopenharmony_ci                            ctx2->expected_server_alert)
88e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol)
89e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->expected_servername, ctx2->expected_servername)
90e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->session_ticket_expected,
91e1051a39Sopenharmony_ci                            ctx2->session_ticket_expected)
92e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->compression_expected,
93e1051a39Sopenharmony_ci                            ctx2->compression_expected)
94e1051a39Sopenharmony_ci            || !TEST_str_eq(ctx->expected_npn_protocol,
95e1051a39Sopenharmony_ci                            ctx2->expected_npn_protocol)
96e1051a39Sopenharmony_ci            || !TEST_str_eq(ctx->expected_alpn_protocol,
97e1051a39Sopenharmony_ci                            ctx2->expected_alpn_protocol)
98e1051a39Sopenharmony_ci            || !TEST_str_eq(ctx->expected_cipher,
99e1051a39Sopenharmony_ci                            ctx2->expected_cipher)
100e1051a39Sopenharmony_ci            || !TEST_str_eq(ctx->expected_session_ticket_app_data,
101e1051a39Sopenharmony_ci                            ctx2->expected_session_ticket_app_data)
102e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->resumption_expected,
103e1051a39Sopenharmony_ci                            ctx2->resumption_expected)
104e1051a39Sopenharmony_ci            || !TEST_int_eq(ctx->session_id_expected,
105e1051a39Sopenharmony_ci                            ctx2->session_id_expected))
106e1051a39Sopenharmony_ci        return 0;
107e1051a39Sopenharmony_ci    return 1;
108e1051a39Sopenharmony_ci}
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_cistatic SSL_TEST_CTX_TEST_FIXTURE *set_up(const char *const test_case_name)
111e1051a39Sopenharmony_ci{
112e1051a39Sopenharmony_ci    SSL_TEST_CTX_TEST_FIXTURE *fixture;
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
115e1051a39Sopenharmony_ci        return NULL;
116e1051a39Sopenharmony_ci    fixture->test_case_name = test_case_name;
117e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture->expected_ctx = SSL_TEST_CTX_new(NULL))) {
118e1051a39Sopenharmony_ci        OPENSSL_free(fixture);
119e1051a39Sopenharmony_ci        return NULL;
120e1051a39Sopenharmony_ci    }
121e1051a39Sopenharmony_ci    return fixture;
122e1051a39Sopenharmony_ci}
123e1051a39Sopenharmony_ci
124e1051a39Sopenharmony_cistatic int execute_test(SSL_TEST_CTX_TEST_FIXTURE *fixture)
125e1051a39Sopenharmony_ci{
126e1051a39Sopenharmony_ci    int success = 0;
127e1051a39Sopenharmony_ci    SSL_TEST_CTX *ctx;
128e1051a39Sopenharmony_ci
129e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = SSL_TEST_CTX_create(conf, fixture->test_section,
130e1051a39Sopenharmony_ci                                            fixture->expected_ctx->libctx))
131e1051a39Sopenharmony_ci            || !testctx_eq(ctx, fixture->expected_ctx))
132e1051a39Sopenharmony_ci        goto err;
133e1051a39Sopenharmony_ci
134e1051a39Sopenharmony_ci    success = 1;
135e1051a39Sopenharmony_ci err:
136e1051a39Sopenharmony_ci    SSL_TEST_CTX_free(ctx);
137e1051a39Sopenharmony_ci    return success;
138e1051a39Sopenharmony_ci}
139e1051a39Sopenharmony_ci
140e1051a39Sopenharmony_cistatic void tear_down(SSL_TEST_CTX_TEST_FIXTURE *fixture)
141e1051a39Sopenharmony_ci{
142e1051a39Sopenharmony_ci    SSL_TEST_CTX_free(fixture->expected_ctx);
143e1051a39Sopenharmony_ci    OPENSSL_free(fixture);
144e1051a39Sopenharmony_ci}
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci#define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
147e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up);
148e1051a39Sopenharmony_ci#define EXECUTE_SSL_TEST_CTX_TEST() \
149e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_test, tear_down)
150e1051a39Sopenharmony_ci
151e1051a39Sopenharmony_cistatic int test_empty_configuration(void)
152e1051a39Sopenharmony_ci{
153e1051a39Sopenharmony_ci    SETUP_SSL_TEST_CTX_TEST_FIXTURE();
154e1051a39Sopenharmony_ci    fixture->test_section = "ssltest_default";
155e1051a39Sopenharmony_ci    fixture->expected_ctx->expected_result = SSL_TEST_SUCCESS;
156e1051a39Sopenharmony_ci    EXECUTE_SSL_TEST_CTX_TEST();
157e1051a39Sopenharmony_ci    return result;
158e1051a39Sopenharmony_ci}
159e1051a39Sopenharmony_ci
160e1051a39Sopenharmony_cistatic int test_good_configuration(void)
161e1051a39Sopenharmony_ci{
162e1051a39Sopenharmony_ci    SETUP_SSL_TEST_CTX_TEST_FIXTURE();
163e1051a39Sopenharmony_ci    fixture->test_section = "ssltest_good";
164e1051a39Sopenharmony_ci    fixture->expected_ctx->method = SSL_TEST_METHOD_DTLS;
165e1051a39Sopenharmony_ci    fixture->expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
166e1051a39Sopenharmony_ci    fixture->expected_ctx->app_data_size = 1024;
167e1051a39Sopenharmony_ci    fixture->expected_ctx->max_fragment_size = 2048;
168e1051a39Sopenharmony_ci
169e1051a39Sopenharmony_ci    fixture->expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
170e1051a39Sopenharmony_ci    fixture->expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
171e1051a39Sopenharmony_ci    fixture->expected_ctx->expected_server_alert = 0;  /* No alert. */
172e1051a39Sopenharmony_ci    fixture->expected_ctx->expected_protocol = TLS1_1_VERSION;
173e1051a39Sopenharmony_ci    fixture->expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
174e1051a39Sopenharmony_ci    fixture->expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
175e1051a39Sopenharmony_ci    fixture->expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
176e1051a39Sopenharmony_ci    fixture->expected_ctx->session_id_expected = SSL_TEST_SESSION_ID_IGNORE;
177e1051a39Sopenharmony_ci    fixture->expected_ctx->resumption_expected = 1;
178e1051a39Sopenharmony_ci
179e1051a39Sopenharmony_ci    fixture->expected_ctx->extra.client.verify_callback =
180e1051a39Sopenharmony_ci        SSL_TEST_VERIFY_REJECT_ALL;
181e1051a39Sopenharmony_ci    fixture->expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
182e1051a39Sopenharmony_ci    fixture->expected_ctx->extra.client.npn_protocols =
183e1051a39Sopenharmony_ci        OPENSSL_strdup("foo,bar");
184e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture->expected_ctx->extra.client.npn_protocols))
185e1051a39Sopenharmony_ci        goto err;
186e1051a39Sopenharmony_ci    fixture->expected_ctx->extra.client.max_fragment_len_mode = 0;
187e1051a39Sopenharmony_ci
188e1051a39Sopenharmony_ci    fixture->expected_ctx->extra.server.servername_callback =
189e1051a39Sopenharmony_ci        SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
190e1051a39Sopenharmony_ci    fixture->expected_ctx->extra.server.broken_session_ticket = 1;
191e1051a39Sopenharmony_ci
192e1051a39Sopenharmony_ci    fixture->expected_ctx->resume_extra.server2.alpn_protocols =
193e1051a39Sopenharmony_ci        OPENSSL_strdup("baz");
194e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture->expected_ctx->resume_extra.server2.alpn_protocols))
195e1051a39Sopenharmony_ci        goto err;
196e1051a39Sopenharmony_ci
197e1051a39Sopenharmony_ci    fixture->expected_ctx->resume_extra.client.ct_validation =
198e1051a39Sopenharmony_ci        SSL_TEST_CT_VALIDATION_STRICT;
199e1051a39Sopenharmony_ci
200e1051a39Sopenharmony_ci    EXECUTE_SSL_TEST_CTX_TEST();
201e1051a39Sopenharmony_ci    return result;
202e1051a39Sopenharmony_ci
203e1051a39Sopenharmony_cierr:
204e1051a39Sopenharmony_ci    tear_down(fixture);
205e1051a39Sopenharmony_ci    return 0;
206e1051a39Sopenharmony_ci}
207e1051a39Sopenharmony_ci
208e1051a39Sopenharmony_cistatic const char *bad_configurations[] = {
209e1051a39Sopenharmony_ci    "ssltest_unknown_option",
210e1051a39Sopenharmony_ci    "ssltest_wrong_section",
211e1051a39Sopenharmony_ci    "ssltest_unknown_expected_result",
212e1051a39Sopenharmony_ci    "ssltest_unknown_alert",
213e1051a39Sopenharmony_ci    "ssltest_unknown_protocol",
214e1051a39Sopenharmony_ci    "ssltest_unknown_verify_callback",
215e1051a39Sopenharmony_ci    "ssltest_unknown_servername",
216e1051a39Sopenharmony_ci    "ssltest_unknown_servername_callback",
217e1051a39Sopenharmony_ci    "ssltest_unknown_session_ticket_expected",
218e1051a39Sopenharmony_ci    "ssltest_unknown_compression_expected",
219e1051a39Sopenharmony_ci    "ssltest_unknown_session_id_expected",
220e1051a39Sopenharmony_ci    "ssltest_unknown_method",
221e1051a39Sopenharmony_ci    "ssltest_unknown_handshake_mode",
222e1051a39Sopenharmony_ci    "ssltest_unknown_resumption_expected",
223e1051a39Sopenharmony_ci    "ssltest_unknown_ct_validation",
224e1051a39Sopenharmony_ci    "ssltest_invalid_max_fragment_len",
225e1051a39Sopenharmony_ci};
226e1051a39Sopenharmony_ci
227e1051a39Sopenharmony_cistatic int test_bad_configuration(int idx)
228e1051a39Sopenharmony_ci{
229e1051a39Sopenharmony_ci    SSL_TEST_CTX *ctx;
230e1051a39Sopenharmony_ci
231e1051a39Sopenharmony_ci    if (!TEST_ptr_null(ctx = SSL_TEST_CTX_create(conf,
232e1051a39Sopenharmony_ci                                                 bad_configurations[idx], NULL))) {
233e1051a39Sopenharmony_ci        SSL_TEST_CTX_free(ctx);
234e1051a39Sopenharmony_ci        return 0;
235e1051a39Sopenharmony_ci    }
236e1051a39Sopenharmony_ci
237e1051a39Sopenharmony_ci    return 1;
238e1051a39Sopenharmony_ci}
239e1051a39Sopenharmony_ci
240e1051a39Sopenharmony_ciOPT_TEST_DECLARE_USAGE("conf_file\n")
241e1051a39Sopenharmony_ci
242e1051a39Sopenharmony_ciint setup_tests(void)
243e1051a39Sopenharmony_ci{
244e1051a39Sopenharmony_ci    if (!test_skip_common_options()) {
245e1051a39Sopenharmony_ci        TEST_error("Error parsing test options\n");
246e1051a39Sopenharmony_ci        return 0;
247e1051a39Sopenharmony_ci    }
248e1051a39Sopenharmony_ci
249e1051a39Sopenharmony_ci    if (!TEST_ptr(conf = NCONF_new(NULL)))
250e1051a39Sopenharmony_ci        return 0;
251e1051a39Sopenharmony_ci    /* argument should point to test/ssl_test_ctx_test.cnf */
252e1051a39Sopenharmony_ci    if (!TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0))
253e1051a39Sopenharmony_ci        return 0;
254e1051a39Sopenharmony_ci
255e1051a39Sopenharmony_ci    ADD_TEST(test_empty_configuration);
256e1051a39Sopenharmony_ci    ADD_TEST(test_good_configuration);
257e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
258e1051a39Sopenharmony_ci    return 1;
259e1051a39Sopenharmony_ci}
260e1051a39Sopenharmony_ci
261e1051a39Sopenharmony_civoid cleanup_tests(void)
262e1051a39Sopenharmony_ci{
263e1051a39Sopenharmony_ci    NCONF_free(conf);
264e1051a39Sopenharmony_ci}
265