2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
13 #include <openssl/conf.h>
14 #include <openssl/err.h>
15 #include <openssl/ssl.h>
16 #include <openssl/provider.h>
18 #include "handshake_helper.h"
19 #include "ssl_test_ctx.h"
22 static CONF *conf = NULL;
23 static OSSL_PROVIDER *defctxnull = NULL, *thisprov = NULL;
24 static OPENSSL_CTX *libctx = NULL;
26 /* Currently the section names are of the form test-<number>, e.g. test-15. */
27 #define MAX_TESTCASE_NAME_LENGTH 100
29 static const char *print_alert(int alert)
31 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
34 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
36 if (!TEST_int_eq(result->result, test_ctx->expected_result)) {
37 TEST_info("ExpectedResult mismatch: expected %s, got %s.",
38 ssl_test_result_name(test_ctx->expected_result),
39 ssl_test_result_name(result->result));
45 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
47 if (!TEST_int_eq(result->client_alert_sent,
48 result->client_alert_received)) {
49 TEST_info("Client sent alert %s but server received %s.",
50 print_alert(result->client_alert_sent),
51 print_alert(result->client_alert_received));
53 * We can't bail here because the peer doesn't always get far enough
54 * to process a received alert. Specifically, in protocol version
55 * negotiation tests, we have the following scenario.
56 * Client supports TLS v1.2 only; Server supports TLS v1.1.
57 * Client proposes TLS v1.2; server responds with 1.1;
58 * Client now sends a protocol alert, using TLS v1.2 in the header.
59 * The server, however, rejects the alert because of version mismatch
60 * in the record layer; therefore, the server appears to never
66 if (!TEST_int_eq(result->server_alert_sent,
67 result->server_alert_received)) {
68 TEST_info("Server sent alert %s but client received %s.",
69 print_alert(result->server_alert_sent),
70 print_alert(result->server_alert_received));
74 /* Tolerate an alert if one wasn't explicitly specified in the test. */
75 if (test_ctx->expected_client_alert
77 * The info callback alert value is computed as
78 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
79 * where the low byte is the alert code and the high byte is other stuff.
81 && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
82 TEST_error("ClientAlert mismatch: expected %s, got %s.",
83 print_alert(test_ctx->expected_client_alert),
84 print_alert(result->client_alert_sent));
88 if (test_ctx->expected_server_alert
89 && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
90 TEST_error("ServerAlert mismatch: expected %s, got %s.",
91 print_alert(test_ctx->expected_server_alert),
92 print_alert(result->server_alert_sent));
96 if (!TEST_int_le(result->client_num_fatal_alerts_sent, 1))
98 if (!TEST_int_le(result->server_num_fatal_alerts_sent, 1))
103 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
105 if (!TEST_int_eq(result->client_protocol, result->server_protocol)) {
106 TEST_info("Client has protocol %s but server has %s.",
107 ssl_protocol_name(result->client_protocol),
108 ssl_protocol_name(result->server_protocol));
112 if (test_ctx->expected_protocol) {
113 if (!TEST_int_eq(result->client_protocol,
114 test_ctx->expected_protocol)) {
115 TEST_info("Protocol mismatch: expected %s, got %s.\n",
116 ssl_protocol_name(test_ctx->expected_protocol),
117 ssl_protocol_name(result->client_protocol));
124 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
126 if (!TEST_int_eq(result->servername, test_ctx->expected_servername)) {
127 TEST_info("Client ServerName mismatch, expected %s, got %s.",
128 ssl_servername_name(test_ctx->expected_servername),
129 ssl_servername_name(result->servername));
135 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
137 if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
139 if (!TEST_int_eq(result->session_ticket,
140 test_ctx->session_ticket_expected)) {
141 TEST_info("Client SessionTicketExpected mismatch, expected %s, got %s.",
142 ssl_session_ticket_name(test_ctx->session_ticket_expected),
143 ssl_session_ticket_name(result->session_ticket));
149 static int check_session_id(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
151 if (test_ctx->session_id_expected == SSL_TEST_SESSION_ID_IGNORE)
153 if (!TEST_int_eq(result->session_id, test_ctx->session_id_expected)) {
154 TEST_info("Client SessionIdExpected mismatch, expected %s, got %s\n.",
155 ssl_session_id_name(test_ctx->session_id_expected),
156 ssl_session_id_name(result->session_id));
162 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
164 if (!TEST_int_eq(result->compression, test_ctx->compression_expected))
168 #ifndef OPENSSL_NO_NEXTPROTONEG
169 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
172 if (!TEST_str_eq(result->client_npn_negotiated,
173 result->server_npn_negotiated))
175 if (!TEST_str_eq(test_ctx->expected_npn_protocol,
176 result->client_npn_negotiated))
182 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
185 if (!TEST_str_eq(result->client_alpn_negotiated,
186 result->server_alpn_negotiated))
188 if (!TEST_str_eq(test_ctx->expected_alpn_protocol,
189 result->client_alpn_negotiated))
194 static int check_session_ticket_app_data(HANDSHAKE_RESULT *result,
195 SSL_TEST_CTX *test_ctx)
197 size_t result_len = 0;
198 size_t expected_len = 0;
200 /* consider empty and NULL strings to be the same */
201 if (result->result_session_ticket_app_data != NULL)
202 result_len = strlen(result->result_session_ticket_app_data);
203 if (test_ctx->expected_session_ticket_app_data != NULL)
204 expected_len = strlen(test_ctx->expected_session_ticket_app_data);
205 if (result_len == 0 && expected_len == 0)
208 if (!TEST_str_eq(result->result_session_ticket_app_data,
209 test_ctx->expected_session_ticket_app_data))
215 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
217 if (!TEST_int_eq(result->client_resumed, result->server_resumed))
219 if (!TEST_int_eq(result->client_resumed, test_ctx->resumption_expected))
224 static int check_nid(const char *name, int expected_nid, int nid)
226 if (expected_nid == 0 || expected_nid == nid)
228 TEST_error("%s type mismatch, %s vs %s\n",
229 name, OBJ_nid2ln(expected_nid),
230 nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
234 static void print_ca_names(STACK_OF(X509_NAME) *names)
238 if (names == NULL || sk_X509_NAME_num(names) == 0) {
239 TEST_note(" <empty>");
242 for (i = 0; i < sk_X509_NAME_num(names); i++) {
243 X509_NAME_print_ex(bio_err, sk_X509_NAME_value(names, i), 4,
245 BIO_puts(bio_err, "\n");
249 static int check_ca_names(const char *name,
250 STACK_OF(X509_NAME) *expected_names,
251 STACK_OF(X509_NAME) *names)
255 if (expected_names == NULL)
257 if (names == NULL || sk_X509_NAME_num(names) == 0) {
258 if (TEST_int_eq(sk_X509_NAME_num(expected_names), 0))
262 if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
264 for (i = 0; i < sk_X509_NAME_num(names); i++) {
265 if (!TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(names, i),
266 sk_X509_NAME_value(expected_names, i)),
273 TEST_info("%s: list mismatch", name);
274 TEST_note("Expected Names:");
275 print_ca_names(expected_names);
276 TEST_note("Received Names:");
277 print_ca_names(names);
281 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
283 return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
284 result->tmp_key_type);
287 static int check_server_cert_type(HANDSHAKE_RESULT *result,
288 SSL_TEST_CTX *test_ctx)
290 return check_nid("Server certificate", test_ctx->expected_server_cert_type,
291 result->server_cert_type);
294 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
295 SSL_TEST_CTX *test_ctx)
297 return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
298 result->server_sign_hash);
301 static int check_server_sign_type(HANDSHAKE_RESULT *result,
302 SSL_TEST_CTX *test_ctx)
304 return check_nid("Server signing", test_ctx->expected_server_sign_type,
305 result->server_sign_type);
308 static int check_server_ca_names(HANDSHAKE_RESULT *result,
309 SSL_TEST_CTX *test_ctx)
311 return check_ca_names("Server CA names",
312 test_ctx->expected_server_ca_names,
313 result->server_ca_names);
316 static int check_client_cert_type(HANDSHAKE_RESULT *result,
317 SSL_TEST_CTX *test_ctx)
319 return check_nid("Client certificate", test_ctx->expected_client_cert_type,
320 result->client_cert_type);
323 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
324 SSL_TEST_CTX *test_ctx)
326 return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
327 result->client_sign_hash);
330 static int check_client_sign_type(HANDSHAKE_RESULT *result,
331 SSL_TEST_CTX *test_ctx)
333 return check_nid("Client signing", test_ctx->expected_client_sign_type,
334 result->client_sign_type);
337 static int check_client_ca_names(HANDSHAKE_RESULT *result,
338 SSL_TEST_CTX *test_ctx)
340 return check_ca_names("Client CA names",
341 test_ctx->expected_client_ca_names,
342 result->client_ca_names);
345 static int check_cipher(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
347 if (test_ctx->expected_cipher == NULL)
349 if (!TEST_ptr(result->cipher))
351 if (!TEST_str_eq(test_ctx->expected_cipher,
358 * This could be further simplified by constructing an expected
359 * HANDSHAKE_RESULT, and implementing comparison methods for
362 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
365 ret &= check_result(result, test_ctx);
366 ret &= check_alerts(result, test_ctx);
367 if (result->result == SSL_TEST_SUCCESS) {
368 ret &= check_protocol(result, test_ctx);
369 ret &= check_servername(result, test_ctx);
370 ret &= check_session_ticket(result, test_ctx);
371 ret &= check_compression(result, test_ctx);
372 ret &= check_session_id(result, test_ctx);
373 ret &= (result->session_ticket_do_not_call == 0);
374 #ifndef OPENSSL_NO_NEXTPROTONEG
375 ret &= check_npn(result, test_ctx);
377 ret &= check_cipher(result, test_ctx);
378 ret &= check_alpn(result, test_ctx);
379 ret &= check_session_ticket_app_data(result, test_ctx);
380 ret &= check_resumption(result, test_ctx);
381 ret &= check_tmp_key(result, test_ctx);
382 ret &= check_server_cert_type(result, test_ctx);
383 ret &= check_server_sign_hash(result, test_ctx);
384 ret &= check_server_sign_type(result, test_ctx);
385 ret &= check_server_ca_names(result, test_ctx);
386 ret &= check_client_cert_type(result, test_ctx);
387 ret &= check_client_sign_hash(result, test_ctx);
388 ret &= check_client_sign_type(result, test_ctx);
389 ret &= check_client_ca_names(result, test_ctx);
394 static int test_handshake(int idx)
397 SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
398 *resume_server_ctx = NULL, *resume_client_ctx = NULL;
399 SSL_TEST_CTX *test_ctx = NULL;
400 HANDSHAKE_RESULT *result = NULL;
401 char test_app[MAX_TESTCASE_NAME_LENGTH];
403 BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
405 test_ctx = SSL_TEST_CTX_create(conf, test_app);
406 if (!TEST_ptr(test_ctx))
409 #ifndef OPENSSL_NO_DTLS
410 if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
411 server_ctx = SSL_CTX_new_with_libctx(libctx, NULL, DTLS_server_method());
412 if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
414 if (test_ctx->extra.server.servername_callback !=
415 SSL_TEST_SERVERNAME_CB_NONE) {
416 if (!TEST_ptr(server2_ctx =
417 SSL_CTX_new_with_libctx(libctx, NULL,
418 DTLS_server_method())))
421 client_ctx = SSL_CTX_new_with_libctx(libctx, NULL, DTLS_client_method());
422 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
424 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
425 resume_server_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
426 DTLS_server_method());
427 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)))
429 resume_client_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
430 DTLS_client_method());
431 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
433 if (!TEST_ptr(resume_server_ctx)
434 || !TEST_ptr(resume_client_ctx))
439 if (test_ctx->method == SSL_TEST_METHOD_TLS) {
440 server_ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_server_method());
441 if (!TEST_true(SSL_CTX_set_max_proto_version(server_ctx, 0)))
443 /* SNI on resumption isn't supported/tested yet. */
444 if (test_ctx->extra.server.servername_callback !=
445 SSL_TEST_SERVERNAME_CB_NONE) {
446 if (!TEST_ptr(server2_ctx =
447 SSL_CTX_new_with_libctx(libctx, NULL,
448 TLS_server_method())))
450 if (!TEST_true(SSL_CTX_set_max_proto_version(server2_ctx, 0)))
453 client_ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_client_method());
454 if (!TEST_true(SSL_CTX_set_max_proto_version(client_ctx, 0)))
457 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
458 resume_server_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
459 TLS_server_method());
460 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_server_ctx, 0)))
462 resume_client_ctx = SSL_CTX_new_with_libctx(libctx, NULL,
463 TLS_client_method());
464 if (!TEST_true(SSL_CTX_set_max_proto_version(resume_client_ctx, 0)))
466 if (!TEST_ptr(resume_server_ctx)
467 || !TEST_ptr(resume_client_ctx))
472 #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
473 if (!TEST_true(OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL)))
477 if (!TEST_ptr(server_ctx)
478 || !TEST_ptr(client_ctx)
479 || !TEST_int_gt(CONF_modules_load(conf, test_app, 0), 0))
482 if (!SSL_CTX_config(server_ctx, "server")
483 || !SSL_CTX_config(client_ctx, "client")) {
487 if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
489 if (resume_server_ctx != NULL
490 && !SSL_CTX_config(resume_server_ctx, "resume-server"))
492 if (resume_client_ctx != NULL
493 && !SSL_CTX_config(resume_client_ctx, "resume-client"))
496 result = do_handshake(server_ctx, server2_ctx, client_ctx,
497 resume_server_ctx, resume_client_ctx, test_ctx);
500 ret = check_test(result, test_ctx);
503 CONF_modules_unload(0);
504 SSL_CTX_free(server_ctx);
505 SSL_CTX_free(server2_ctx);
506 SSL_CTX_free(client_ctx);
507 SSL_CTX_free(resume_server_ctx);
508 SSL_CTX_free(resume_client_ctx);
509 SSL_TEST_CTX_free(test_ctx);
510 HANDSHAKE_RESULT_free(result);
514 OPT_TEST_DECLARE_USAGE("conf_file modulename [fips_conf_file]\n")
516 int setup_tests(void)
519 const char *modulename;
521 if (!test_skip_common_options()) {
522 TEST_error("Error parsing test options\n");
526 if (!TEST_ptr(conf = NCONF_new(NULL))
527 /* argv[1] should point to the test conf file */
528 || !TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0)
529 || !TEST_int_ne(NCONF_get_number_e(conf, NULL, "num_tests",
533 if (!TEST_ptr(modulename = test_get_argument(1)))
536 if (strcmp(modulename, "none") != 0) {
537 const char *configfile = test_get_argument(2);
539 defctxnull = OSSL_PROVIDER_load(NULL, "null");
540 libctx = OPENSSL_CTX_new();
541 if (!TEST_ptr(libctx))
544 if (configfile != NULL
545 && !TEST_true(OPENSSL_CTX_load_config(libctx, configfile)))
548 thisprov = OSSL_PROVIDER_load(libctx, modulename);
549 if (!TEST_ptr(thisprov))
553 ADD_ALL_TESTS(test_handshake, (int)num_tests);
557 void cleanup_tests(void)
560 OSSL_PROVIDER_unload(defctxnull);
561 OSSL_PROVIDER_unload(thisprov);
562 OPENSSL_CTX_free(libctx);