1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
* 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>
#include <string.h>
#include "e_os.h"
#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;
/* Returns 1 if the contexts are equal, 0 otherwise. */
static int SSL_TEST_CTX_equal(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
{
if (ctx->expected_result != ctx2->expected_result) {
fprintf(stderr, "ExpectedResult mismatch: %s vs %s.\n",
ssl_test_result_name(ctx->expected_result),
ssl_test_result_name(ctx2->expected_result));
return 0;
}
if (ctx->client_alert != ctx2->client_alert) {
fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n",
ssl_alert_name(ctx->client_alert),
ssl_alert_name(ctx2->client_alert));
return 0;
}
if (ctx->server_alert != ctx2->server_alert) {
fprintf(stderr, "ServerAlert mismatch: %s vs %s.\n",
ssl_alert_name(ctx->server_alert),
ssl_alert_name(ctx2->server_alert));
return 0;
}
if (ctx->protocol != ctx2->protocol) {
fprintf(stderr, "ClientAlert mismatch: %s vs %s.\n",
ssl_protocol_name(ctx->protocol),
ssl_protocol_name(ctx2->protocol));
return 0;
}
if (ctx->client_verify_callback != ctx2->client_verify_callback) {
fprintf(stderr, "ClientVerifyCallback mismatch: %s vs %s.\n",
ssl_verify_callback_name(ctx->client_verify_callback),
ssl_verify_callback_name(ctx2->client_verify_callback));
return 0;
}
if (ctx->servername != ctx2->servername) {
fprintf(stderr, "ServerName mismatch: %s vs %s.\n",
ssl_servername_name(ctx->servername),
ssl_servername_name(ctx2->servername));
return 0;
}
if (ctx->expected_servername != ctx2->expected_servername) {
fprintf(stderr, "ExpectedServerName mismatch: %s vs %s.\n",
ssl_servername_name(ctx->expected_servername),
ssl_servername_name(ctx2->expected_servername));
return 0;
}
if (ctx->servername_callback != ctx2->servername_callback) {
fprintf(stderr, "ServerNameCallback mismatch: %s vs %s.\n",
ssl_servername_callback_name(ctx->servername_callback),
ssl_servername_callback_name(ctx2->servername_callback));
return 0;
}
if (ctx->session_ticket_expected != ctx2->session_ticket_expected) {
fprintf(stderr, "SessionTicketExpected mismatch: %s vs %s.\n",
ssl_session_ticket_name(ctx->session_ticket_expected),
ssl_session_ticket_name(ctx2->session_ticket_expected));
return 0;
}
if (!strings_equal("ClientNPNProtocols", ctx->client_npn_protocols,
ctx2->client_npn_protocols))
return 0;
if (ctx->method != ctx2->method) {
fprintf(stderr, "Method mismatch: %s vs %s.\n",
ssl_test_method_name(ctx->method),
ssl_test_method_name(ctx2->method));
return 0;
}
if (!strings_equal("ServerNPNProtocols", ctx->server_npn_protocols,
ctx2->server_npn_protocols))
return 0;
if (!strings_equal("Server2NPNProtocols", ctx->server_npn_protocols,
ctx2->server_npn_protocols))
return 0;
if (!strings_equal("ExpectedNPNProtocol", ctx->expected_npn_protocol,
ctx2->expected_npn_protocol))
return 0;
if (!strings_equal("ClientALPNProtocols", ctx->client_alpn_protocols,
ctx2->client_alpn_protocols))
return 0;
if (!strings_equal("ServerALPNProtocols", ctx->server_alpn_protocols,
ctx2->server_alpn_protocols))
return 0;
if (!strings_equal("Server2ALPNProtocols", ctx->server_alpn_protocols,
ctx2->server_alpn_protocols))
return 0;
if (!strings_equal("ExpectedALPNProtocol", ctx->expected_alpn_protocol,
ctx2->expected_alpn_protocol))
return 0;
if (ctx->handshake_mode != ctx2->handshake_mode) {
fprintf(stderr, "HandshakeMode mismatch: %s vs %s.\n",
ssl_handshake_mode_name(ctx->handshake_mode),
ssl_handshake_mode_name(ctx2->handshake_mode));
return 0;
}
if (ctx->resumption_expected != ctx2->resumption_expected) {
fprintf(stderr, "ResumptionExpected mismatch: %d vs %d.\n",
ctx->resumption_expected, ctx2->resumption_expected);
return 0;
}
return 1;
}
static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
{
SSL_TEST_CTX_TEST_FIXTURE fixture;
fixture.test_case_name = test_case_name;
fixture.expected_ctx = SSL_TEST_CTX_new();
OPENSSL_assert(fixture.expected_ctx != NULL);
return fixture;
}
static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
{
int success = 0;
SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
if (ctx == NULL) {
fprintf(stderr, "Failed to parse good configuration %s.\n",
fixture.test_section);
goto err;
}
if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
goto err;
success = 1;
err:
SSL_TEST_CTX_free(ctx);
return success;
}
static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
{
SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
if (ctx != NULL) {
fprintf(stderr, "Parsing bad configuration %s succeeded.\n",
fixture.test_section);
SSL_TEST_CTX_free(ctx);
return 0;
}
return 1;
}
static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
{
SSL_TEST_CTX_free(fixture.expected_ctx);
ERR_print_errors_fp(stderr);
}
#define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up)
#define EXECUTE_SSL_TEST_CTX_TEST() \
EXECUTE_TEST(execute_test, tear_down)
#define EXECUTE_SSL_TEST_CTX_FAILURE_TEST() \
EXECUTE_TEST(execute_failure_test, tear_down)
static int test_empty_configuration()
{
SETUP_SSL_TEST_CTX_TEST_FIXTURE();
fixture.test_section = "ssltest_default";
fixture.expected_ctx->expected_result = SSL_TEST_SUCCESS;
EXECUTE_SSL_TEST_CTX_TEST();
}
static int test_good_configuration()
{
SETUP_SSL_TEST_CTX_TEST_FIXTURE();
fixture.test_section = "ssltest_good";
fixture.expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
fixture.expected_ctx->client_alert = SSL_AD_UNKNOWN_CA;
fixture.expected_ctx->server_alert = 0; /* No alert. */
fixture.expected_ctx->protocol = TLS1_1_VERSION;
fixture.expected_ctx->client_verify_callback = SSL_TEST_VERIFY_REJECT_ALL;
fixture.expected_ctx->servername = SSL_TEST_SERVERNAME_SERVER2;
fixture.expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
fixture.expected_ctx->servername_callback =
SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
fixture.expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
fixture.expected_ctx->method = SSL_TEST_METHOD_DTLS;
fixture.expected_ctx->client_npn_protocols = OPENSSL_strdup("foo,bar");
fixture.expected_ctx->server2_alpn_protocols = OPENSSL_strdup("baz");
OPENSSL_assert(fixture.expected_ctx->client_npn_protocols != NULL);
OPENSSL_assert(fixture.expected_ctx->server2_alpn_protocols != NULL);
fixture.expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
fixture.expected_ctx->resumption_expected = 1;
EXECUTE_SSL_TEST_CTX_TEST();
}
static const char *bad_configurations[] = {
"ssltest_unknown_option",
"ssltest_unknown_expected_result",
"ssltest_unknown_alert",
"ssltest_unknown_protocol",
"ssltest_unknown_verify_callback",
"ssltest_unknown_servername",
"ssltest_unknown_servername_callback",
"ssltest_unknown_session_ticket_expected",
"ssltest_unknown_method",
"ssltest_unknown_handshake_mode",
"ssltest_unknown_resumption_expected",
};
static int test_bad_configuration(int idx)
{
SETUP_SSL_TEST_CTX_TEST_FIXTURE();
fixture.test_section = bad_configurations[idx];
EXECUTE_SSL_TEST_CTX_FAILURE_TEST();
}
int main(int argc, char **argv)
{
int result = 0;
if (argc != 2)
return 1;
conf = NCONF_new(NULL);
OPENSSL_assert(conf != NULL);
/* argv[1] should point to test/ssl_test_ctx_test.conf */
OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
ADD_TEST(test_empty_configuration);
ADD_TEST(test_good_configuration);
ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
result = run_tests(argv[0]);
|