]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/ssl_test.c
092e755b09538a1029e197f2d188a1da0fb53465
[thirdparty/openssl.git] / test / ssl_test.c
1 /*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
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
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <openssl/conf.h>
14 #include <openssl/err.h>
15 #include <openssl/ssl.h>
16
17 #include "handshake_helper.h"
18 #include "ssl_test_ctx.h"
19 #include "testutil.h"
20
21 static CONF *conf = NULL;
22
23 /* Currently the section names are of the form test-<number>, e.g. test-15. */
24 #define MAX_TESTCASE_NAME_LENGTH 100
25
26 static const char *print_alert(int alert)
27 {
28 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
29 }
30
31 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
32 {
33 if (!TEST_int_eq(result->result, test_ctx->expected_result)) {
34 TEST_info("ExpectedResult mismatch: expected %s, got %s.",
35 ssl_test_result_name(test_ctx->expected_result),
36 ssl_test_result_name(result->result));
37 return 0;
38 }
39 return 1;
40 }
41
42 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
43 {
44 if (!TEST_int_eq(result->client_alert_sent,
45 result->client_alert_received)) {
46 TEST_info("Client sent alert %s but server received %s.",
47 print_alert(result->client_alert_sent),
48 print_alert(result->client_alert_received));
49 /*
50 * We can't bail here because the peer doesn't always get far enough
51 * to process a received alert. Specifically, in protocol version
52 * negotiation tests, we have the following scenario.
53 * Client supports TLS v1.2 only; Server supports TLS v1.1.
54 * Client proposes TLS v1.2; server responds with 1.1;
55 * Client now sends a protocol alert, using TLS v1.2 in the header.
56 * The server, however, rejects the alert because of version mismatch
57 * in the record layer; therefore, the server appears to never
58 * receive the alert.
59 */
60 /* return 0; */
61 }
62
63 if (!TEST_int_eq(result->server_alert_sent,
64 result->server_alert_received)) {
65 TEST_info("Server sent alert %s but client received %s.",
66 print_alert(result->server_alert_sent),
67 print_alert(result->server_alert_received));
68 /* return 0; */
69 }
70
71 /* Tolerate an alert if one wasn't explicitly specified in the test. */
72 if (test_ctx->expected_client_alert
73 /*
74 * The info callback alert value is computed as
75 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
76 * where the low byte is the alert code and the high byte is other stuff.
77 */
78 && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
79 TEST_error("ClientAlert mismatch: expected %s, got %s.",
80 print_alert(test_ctx->expected_client_alert),
81 print_alert(result->client_alert_sent));
82 return 0;
83 }
84
85 if (test_ctx->expected_server_alert
86 && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
87 TEST_error("ServerAlert mismatch: expected %s, got %s.",
88 print_alert(test_ctx->expected_server_alert),
89 print_alert(result->server_alert_sent));
90 return 0;
91 }
92
93 if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1))
94 return 0;
95 if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1))
96 return 0;
97 return 1;
98 }
99
100 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
101 {
102 if (!TEST_int_eq(result->client_protocol, result->server_protocol)) {
103 TEST_info("Client has protocol %s but server has %s.",
104 ssl_protocol_name(result->client_protocol),
105 ssl_protocol_name(result->server_protocol));
106 return 0;
107 }
108
109 if (test_ctx->expected_protocol) {
110 if (!TEST_int_eq(result->client_protocol,
111 test_ctx->expected_protocol)) {
112 TEST_info("Protocol mismatch: expected %s, got %s.\n",
113 ssl_protocol_name(test_ctx->expected_protocol),
114 ssl_protocol_name(result->client_protocol));
115 return 0;
116 }
117 }
118 return 1;
119 }
120
121 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
122 {
123 if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) {
124 TEST_info("Client ServerName mismatch, expected %s, got %s.",
125 ssl_servername_name(test_ctx->expected_servername),
126 ssl_servername_name(result->servername));
127 return 0;
128 }
129 return 1;
130 }
131
132 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
133 {
134 if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
135 return 1;
136 if (!TEST_int_eq(result->session_ticket,
137 test_ctx->session_ticket_expected)) {
138 TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
139 ssl_session_ticket_name(test_ctx->session_ticket_expected),
140 ssl_session_ticket_name(result->session_ticket));
141 return 0;
142 }
143 return 1;
144 }
145
146 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
147 {
148 if (!TEST_int_eq(result->compression, test_ctx->compression_expected))
149 return 0;
150 return 1;
151 }
152 #ifndef OPENSSL_NO_NEXTPROTONEG
153 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
154 {
155 int ret = 1;
156 if (!TEST_str_eq(result->client_npn_negotiated,
157 result->server_npn_negotiated))
158 ret = 0;
159 if (!TEST_str_eq(test_ctx->expected_npn_protocol,
160 result->client_npn_negotiated))
161 ret = 0;
162 return ret;
163 }
164 #endif
165
166 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
167 {
168 int ret = 1;
169 if (!TEST_str_eq(result->client_alpn_negotiated,
170 result->server_alpn_negotiated))
171 ret = 0;
172 if (!TEST_str_eq(test_ctx->expected_alpn_protocol,
173 result->client_alpn_negotiated))
174 ret = 0;
175 return ret;
176 }
177
178 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
179 {
180 if (!TEST_int_eq(result->client_resumed, result->server_resumed))
181 return 0;
182 if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected))
183 return 0;
184 return 1;
185 }
186
187 static int check_nid(const char *name, int expected_nid, int nid)
188 {
189 if (expected_nid == 0 || expected_nid == nid)
190 return 1;
191 TEST_error("%s type mismatch, %s vs %s\n",
192 name, OBJ_nid2ln(expected_nid),
193 nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
194 return 0;
195 }
196
197 static void print_ca_names(STACK_OF(X509_NAME) *names)
198 {
199 BIO *err;
200 int i;
201
202 if (names == NULL || sk_X509_NAME_num(names) == 0) {
203 fprintf(stderr, " <empty>\n");
204 return;
205 }
206 err = BIO_new_fp(stderr, BIO_NOCLOSE);
207 for (i = 0; i < sk_X509_NAME_num(names); i++) {
208 X509_NAME_print_ex(err, sk_X509_NAME_value(names, i), 4,
209 XN_FLAG_ONELINE);
210 BIO_puts(err, "\n");
211 }
212 BIO_free(err);
213 }
214
215 static int check_ca_names(const char *name,
216 STACK_OF(X509_NAME) *expected_names,
217 STACK_OF(X509_NAME) *names)
218 {
219 int i;
220
221 if (expected_names == NULL)
222 return 1;
223 if (names == NULL || sk_X509_NAME_num(names) == 0) {
224 if (sk_X509_NAME_num(expected_names) == 0)
225 return 1;
226 goto err;
227 }
228 if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
229 goto err;
230 for (i = 0; i < sk_X509_NAME_num(names); i++) {
231 if (X509_NAME_cmp(sk_X509_NAME_value(names, i),
232 sk_X509_NAME_value(expected_names, i)) != 0) {
233 goto err;
234 }
235 }
236 return 1;
237 err:
238 fprintf(stderr, "%s: list mismatch\nExpected Names:\n", name);
239 print_ca_names(expected_names);
240 fprintf(stderr, "Received Names:\n");
241 print_ca_names(names);
242 return 0;
243 }
244
245 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
246 {
247 return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
248 result->tmp_key_type);
249 }
250
251 static int check_server_cert_type(HANDSHAKE_RESULT *result,
252 SSL_TEST_CTX *test_ctx)
253 {
254 return check_nid("Server certificate", test_ctx->expected_server_cert_type,
255 result->server_cert_type);
256 }
257
258 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
259 SSL_TEST_CTX *test_ctx)
260 {
261 return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
262 result->server_sign_hash);
263 }
264
265 static int check_server_sign_type(HANDSHAKE_RESULT *result,
266 SSL_TEST_CTX *test_ctx)
267 {
268 return check_nid("Server signing", test_ctx->expected_server_sign_type,
269 result->server_sign_type);
270 }
271
272 static int check_server_ca_names(HANDSHAKE_RESULT *result,
273 SSL_TEST_CTX *test_ctx)
274 {
275 return check_ca_names("Server CA names",
276 test_ctx->expected_server_ca_names,
277 result->server_ca_names);
278 }
279
280 static int check_client_cert_type(HANDSHAKE_RESULT *result,
281 SSL_TEST_CTX *test_ctx)
282 {
283 return check_nid("Client certificate", test_ctx->expected_client_cert_type,
284 result->client_cert_type);
285 }
286
287 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
288 SSL_TEST_CTX *test_ctx)
289 {
290 return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
291 result->client_sign_hash);
292 }
293
294 static int check_client_sign_type(HANDSHAKE_RESULT *result,
295 SSL_TEST_CTX *test_ctx)
296 {
297 return check_nid("Client signing", test_ctx->expected_client_sign_type,
298 result->client_sign_type);
299 }
300
301 static int check_client_ca_names(HANDSHAKE_RESULT *result,
302 SSL_TEST_CTX *test_ctx)
303 {
304 return check_ca_names("Client CA names",
305 test_ctx->expected_client_ca_names,
306 result->client_ca_names);
307 }
308
309 /*
310 * This could be further simplified by constructing an expected
311 * HANDSHAKE_RESULT, and implementing comparison methods for
312 * its fields.
313 */
314 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
315 {
316 int ret = 1;
317 ret &= check_result(result, test_ctx);
318 ret &= check_alerts(result, test_ctx);
319 if (result->result == SSL_TEST_SUCCESS) {
320 ret &= check_protocol(result, test_ctx);
321 ret &= check_servername(result, test_ctx);
322 ret &= check_session_ticket(result, test_ctx);
323 ret &= check_compression(result, test_ctx);
324 ret &= (result->session_ticket_do_not_call == 0);
325 #ifndef OPENSSL_NO_NEXTPROTONEG
326 ret &= check_npn(result, test_ctx);
327 #endif
328 ret &= check_alpn(result, test_ctx);
329 ret &= check_resumption(result, test_ctx);
330 ret &= check_tmp_key(result, test_ctx);
331 ret &= check_server_cert_type(result, test_ctx);
332 ret &= check_server_sign_hash(result, test_ctx);
333 ret &= check_server_sign_type(result, test_ctx);
334 ret &= check_server_ca_names(result, test_ctx);
335 ret &= check_client_cert_type(result, test_ctx);
336 ret &= check_client_sign_hash(result, test_ctx);
337 ret &= check_client_sign_type(result, test_ctx);
338 ret &= check_client_ca_names(result, test_ctx);
339 }
340 return ret;
341 }
342
343 static int test_handshake(int idx)
344 {
345 int ret = 0;
346 SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
347 *resume_server_ctx = NULL, *resume_client_ctx = NULL;
348 SSL_TEST_CTX *test_ctx = NULL;
349 HANDSHAKE_RESULT *result = NULL;
350 char test_app[MAX_TESTCASE_NAME_LENGTH];
351
352 BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
353
354 test_ctx = SSL_TEST_CTX_create(conf, test_app);
355 if (!TEST_ptr(test_ctx))
356 goto err;
357
358 #ifndef OPENSSL_NO_DTLS
359 if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
360 server_ctx = SSL_CTX_new(DTLS_server_method());
361 if (test_ctx->extra.server.servername_callback !=
362 SSL_TEST_SERVERNAME_CB_NONE) {
363 server2_ctx = SSL_CTX_new(DTLS_server_method());
364 TEST_check(server2_ctx != NULL);
365 }
366 client_ctx = SSL_CTX_new(DTLS_client_method());
367 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
368 resume_server_ctx = SSL_CTX_new(DTLS_server_method());
369 resume_client_ctx = SSL_CTX_new(DTLS_client_method());
370 TEST_check(resume_server_ctx != NULL);
371 TEST_check(resume_client_ctx != NULL);
372 }
373 }
374 #endif
375 if (test_ctx->method == SSL_TEST_METHOD_TLS) {
376 server_ctx = SSL_CTX_new(TLS_server_method());
377 /* SNI on resumption isn't supported/tested yet. */
378 if (test_ctx->extra.server.servername_callback !=
379 SSL_TEST_SERVERNAME_CB_NONE) {
380 server2_ctx = SSL_CTX_new(TLS_server_method());
381 TEST_check(server2_ctx != NULL);
382 }
383 client_ctx = SSL_CTX_new(TLS_client_method());
384
385 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
386 resume_server_ctx = SSL_CTX_new(TLS_server_method());
387 resume_client_ctx = SSL_CTX_new(TLS_client_method());
388 TEST_check(resume_server_ctx != NULL);
389 TEST_check(resume_client_ctx != NULL);
390 }
391 }
392
393 TEST_check(server_ctx != NULL);
394 TEST_check(client_ctx != NULL);
395
396 TEST_check(CONF_modules_load(conf, test_app, 0) > 0);
397
398 if (!SSL_CTX_config(server_ctx, "server")
399 || !SSL_CTX_config(client_ctx, "client")) {
400 goto err;
401 }
402
403 if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
404 goto err;
405 if (resume_server_ctx != NULL
406 && !SSL_CTX_config(resume_server_ctx, "resume-server"))
407 goto err;
408 if (resume_client_ctx != NULL
409 && !SSL_CTX_config(resume_client_ctx, "resume-client"))
410 goto err;
411
412 result = do_handshake(server_ctx, server2_ctx, client_ctx,
413 resume_server_ctx, resume_client_ctx, test_ctx);
414
415 ret = check_test(result, test_ctx);
416
417 err:
418 CONF_modules_unload(0);
419 SSL_CTX_free(server_ctx);
420 SSL_CTX_free(server2_ctx);
421 SSL_CTX_free(client_ctx);
422 SSL_CTX_free(resume_server_ctx);
423 SSL_CTX_free(resume_client_ctx);
424 SSL_TEST_CTX_free(test_ctx);
425 HANDSHAKE_RESULT_free(result);
426 return ret;
427 }
428
429 int test_main(int argc, char **argv)
430 {
431 int result = 0;
432 long num_tests;
433
434 if (argc != 2)
435 return 1;
436
437 conf = NCONF_new(NULL);
438 TEST_check(conf != NULL);
439
440 /* argv[1] should point to the test conf file */
441 TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
442
443 TEST_check(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
444
445 ADD_ALL_TESTS(test_handshake, (int)(num_tests));
446 result = run_tests(argv[0]);
447
448 NCONF_free(conf);
449 return result;
450 }