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