2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/conf.h>
13 #include <openssl/err.h>
14 #include <openssl/ssl.h>
16 #include "handshake_helper.h"
17 #include "ssl_test_ctx.h"
20 static CONF *conf = NULL;
22 /* Currently the section names are of the form test-<number>, e.g. test-15. */
23 #define MAX_TESTCASE_NAME_LENGTH 100
25 typedef struct ssl_test_ctx_test_fixture {
26 const char *test_case_name;
27 char test_app[MAX_TESTCASE_NAME_LENGTH];
30 static SSL_TEST_FIXTURE set_up(const char *const test_case_name)
32 SSL_TEST_FIXTURE fixture;
33 fixture.test_case_name = test_case_name;
37 static const char *print_alert(int alert)
39 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
42 static int check_result(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
44 if (result.result != test_ctx->expected_result) {
45 fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
46 ssl_test_result_name(test_ctx->expected_result),
47 ssl_test_result_name(result.result));
53 static int check_alerts(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
55 if (result.client_alert_sent != result.client_alert_received) {
56 fprintf(stderr, "Client sent alert %s but server received %s\n.",
57 print_alert(result.client_alert_sent),
58 print_alert(result.client_alert_received));
60 * We can't bail here because the peer doesn't always get far enough
61 * to process a received alert. Specifically, in protocol version
62 * negotiation tests, we have the following scenario.
63 * Client supports TLS v1.2 only; Server supports TLS v1.1.
64 * Client proposes TLS v1.2; server responds with 1.1;
65 * Client now sends a protocol alert, using TLS v1.2 in the header.
66 * The server, however, rejects the alert because of version mismatch
67 * in the record layer; therefore, the server appears to never
73 if (result.server_alert_sent != result.server_alert_received) {
74 fprintf(stderr, "Server sent alert %s but client received %s\n.",
75 print_alert(result.server_alert_sent),
76 print_alert(result.server_alert_received));
80 /* Tolerate an alert if one wasn't explicitly specified in the test. */
81 if (test_ctx->client_alert
83 * The info callback alert value is computed as
84 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
85 * where the low byte is the alert code and the high byte is other stuff.
87 && (result.client_alert_sent & 0xff) != test_ctx->client_alert) {
88 fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
89 print_alert(test_ctx->client_alert),
90 print_alert(result.client_alert_sent));
94 if (test_ctx->server_alert
95 && (result.server_alert_sent & 0xff) != test_ctx->server_alert) {
96 fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
97 print_alert(test_ctx->server_alert),
98 print_alert(result.server_alert_sent));
105 static int check_protocol(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
107 if (result.client_protocol != result.server_protocol) {
108 fprintf(stderr, "Client has protocol %s but server has %s\n.",
109 ssl_protocol_name(result.client_protocol),
110 ssl_protocol_name(result.server_protocol));
114 if (test_ctx->protocol) {
115 if (result.client_protocol != test_ctx->protocol) {
116 fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
117 ssl_protocol_name(test_ctx->protocol),
118 ssl_protocol_name(result.client_protocol));
126 * This could be further simplified by constructing an expected
127 * HANDSHAKE_RESULT, and implementing comparison methods for
130 static int check_test(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
133 ret &= check_result(result, test_ctx);
134 ret &= check_alerts(result, test_ctx);
135 if (result.result == SSL_TEST_SUCCESS)
136 ret &= check_protocol(result, test_ctx);
140 static int execute_test(SSL_TEST_FIXTURE fixture)
143 SSL_CTX *server_ctx = NULL, *client_ctx = NULL;
144 SSL_TEST_CTX *test_ctx = NULL;
145 HANDSHAKE_RESULT result;
147 server_ctx = SSL_CTX_new(TLS_server_method());
148 client_ctx = SSL_CTX_new(TLS_client_method());
149 OPENSSL_assert(server_ctx != NULL && client_ctx != NULL);
151 OPENSSL_assert(CONF_modules_load(conf, fixture.test_app, 0) > 0);
153 if (!SSL_CTX_config(server_ctx, "server")
154 || !SSL_CTX_config(client_ctx, "client")) {
158 test_ctx = SSL_TEST_CTX_create(conf, fixture.test_app);
159 if (test_ctx == NULL)
162 result = do_handshake(server_ctx, client_ctx, test_ctx);
164 ret = check_test(result, test_ctx);
167 CONF_modules_unload(0);
168 SSL_CTX_free(server_ctx);
169 SSL_CTX_free(client_ctx);
170 SSL_TEST_CTX_free(test_ctx);
172 ERR_print_errors_fp(stderr);
176 static void tear_down(SSL_TEST_FIXTURE fixture)
180 #define SETUP_SSL_TEST_FIXTURE() \
181 SETUP_TEST_FIXTURE(SSL_TEST_FIXTURE, set_up)
182 #define EXECUTE_SSL_TEST() \
183 EXECUTE_TEST(execute_test, tear_down)
185 static int test_handshake(int idx)
187 SETUP_SSL_TEST_FIXTURE();
188 BIO_snprintf(fixture.test_app, sizeof(fixture.test_app),
193 int main(int argc, char **argv)
201 conf = NCONF_new(NULL);
202 OPENSSL_assert(conf != NULL);
204 /* argv[1] should point to the test conf file */
205 OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
207 OPENSSL_assert(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
209 ADD_ALL_TESTS(test_handshake, (int)(num_tests));
210 result = run_tests(argv[0]);