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