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
13 #include <openssl/conf.h>
14 #include <openssl/err.h>
15 #include <openssl/ssl.h>
17 #include "handshake_helper.h"
18 #include "ssl_test_ctx.h"
21 static CONF *conf = NULL;
23 /* Currently the section names are of the form test-<number>, e.g. test-15. */
24 #define MAX_TESTCASE_NAME_LENGTH 100
26 static const char *print_alert(int alert)
28 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
31 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
33 if (result->result != test_ctx->expected_result) {
34 fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
35 ssl_test_result_name(test_ctx->expected_result),
36 ssl_test_result_name(result->result));
42 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
44 if (result->client_alert_sent != result->client_alert_received) {
45 fprintf(stderr, "Client sent alert %s but server received %s\n.",
46 print_alert(result->client_alert_sent),
47 print_alert(result->client_alert_received));
49 * We can't bail here because the peer doesn't always get far enough
50 * to process a received alert. Specifically, in protocol version
51 * negotiation tests, we have the following scenario.
52 * Client supports TLS v1.2 only; Server supports TLS v1.1.
53 * Client proposes TLS v1.2; server responds with 1.1;
54 * Client now sends a protocol alert, using TLS v1.2 in the header.
55 * The server, however, rejects the alert because of version mismatch
56 * in the record layer; therefore, the server appears to never
62 if (result->server_alert_sent != result->server_alert_received) {
63 fprintf(stderr, "Server sent alert %s but client received %s\n.",
64 print_alert(result->server_alert_sent),
65 print_alert(result->server_alert_received));
69 /* Tolerate an alert if one wasn't explicitly specified in the test. */
70 if (test_ctx->expected_client_alert
72 * The info callback alert value is computed as
73 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
74 * where the low byte is the alert code and the high byte is other stuff.
76 && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
77 fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
78 print_alert(test_ctx->expected_client_alert),
79 print_alert(result->client_alert_sent));
83 if (test_ctx->expected_server_alert
84 && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
85 fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
86 print_alert(test_ctx->expected_server_alert),
87 print_alert(result->server_alert_sent));
91 if (result->client_num_fatal_alerts_sent > 1) {
92 fprintf(stderr, "Client sent %d fatal alerts.\n",
93 result->client_num_fatal_alerts_sent);
96 if (result->server_num_fatal_alerts_sent > 1) {
97 fprintf(stderr, "Server sent %d alerts.\n",
98 result->server_num_fatal_alerts_sent);
104 static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
106 if (result->client_protocol != result->server_protocol) {
107 fprintf(stderr, "Client has protocol %s but server has %s\n.",
108 ssl_protocol_name(result->client_protocol),
109 ssl_protocol_name(result->server_protocol));
113 if (test_ctx->expected_protocol) {
114 if (result->client_protocol != test_ctx->expected_protocol) {
115 fprintf(stderr, "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 (result->servername != test_ctx->expected_servername) {
127 fprintf(stderr, "Client ServerName mismatch, expected %s, got %s\n.",
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 (result->session_ticket != test_ctx->session_ticket_expected) {
140 fprintf(stderr, "Client SessionTicketExpected mismatch, expected %s, got %s\n.",
141 ssl_session_ticket_name(test_ctx->session_ticket_expected),
142 ssl_session_ticket_name(result->session_ticket));
148 #ifndef OPENSSL_NO_NEXTPROTONEG
149 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
152 ret &= strings_equal("NPN Negotiated (client vs server)",
153 result->client_npn_negotiated,
154 result->server_npn_negotiated);
155 ret &= strings_equal("ExpectedNPNProtocol",
156 test_ctx->expected_npn_protocol,
157 result->client_npn_negotiated);
162 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
165 ret &= strings_equal("ALPN Negotiated (client vs server)",
166 result->client_alpn_negotiated,
167 result->server_alpn_negotiated);
168 ret &= strings_equal("ExpectedALPNProtocol",
169 test_ctx->expected_alpn_protocol,
170 result->client_alpn_negotiated);
174 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
176 if (result->client_resumed != result->server_resumed) {
177 fprintf(stderr, "Resumption mismatch (client vs server): %d vs %d\n",
178 result->client_resumed, result->server_resumed);
181 if (result->client_resumed != test_ctx->resumption_expected) {
182 fprintf(stderr, "ResumptionExpected mismatch: %d vs %d\n",
183 test_ctx->resumption_expected, result->client_resumed);
190 * This could be further simplified by constructing an expected
191 * HANDSHAKE_RESULT, and implementing comparison methods for
194 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
197 ret &= check_result(result, test_ctx);
198 ret &= check_alerts(result, test_ctx);
199 if (result->result == SSL_TEST_SUCCESS) {
200 ret &= check_protocol(result, test_ctx);
201 ret &= check_servername(result, test_ctx);
202 ret &= check_session_ticket(result, test_ctx);
203 ret &= (result->session_ticket_do_not_call == 0);
204 #ifndef OPENSSL_NO_NEXTPROTONEG
205 ret &= check_npn(result, test_ctx);
207 ret &= check_alpn(result, test_ctx);
208 ret &= check_resumption(result, test_ctx);
213 static int test_handshake(int idx)
216 SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
217 *resume_server_ctx = NULL, *resume_client_ctx = NULL;
218 SSL_TEST_CTX *test_ctx = NULL;
219 HANDSHAKE_RESULT *result = NULL;
220 char test_app[MAX_TESTCASE_NAME_LENGTH];
222 BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
224 test_ctx = SSL_TEST_CTX_create(conf, test_app);
225 if (test_ctx == NULL)
228 #ifndef OPENSSL_NO_DTLS
229 if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
230 server_ctx = SSL_CTX_new(DTLS_server_method());
231 if (test_ctx->extra.server.servername_callback !=
232 SSL_TEST_SERVERNAME_CB_NONE) {
233 server2_ctx = SSL_CTX_new(DTLS_server_method());
234 TEST_check(server2_ctx != NULL);
236 client_ctx = SSL_CTX_new(DTLS_client_method());
237 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
238 resume_server_ctx = SSL_CTX_new(DTLS_server_method());
239 resume_client_ctx = SSL_CTX_new(DTLS_client_method());
240 TEST_check(resume_server_ctx != NULL);
241 TEST_check(resume_client_ctx != NULL);
245 if (test_ctx->method == SSL_TEST_METHOD_TLS) {
246 server_ctx = SSL_CTX_new(TLS_server_method());
247 /* SNI on resumption isn't supported/tested yet. */
248 if (test_ctx->extra.server.servername_callback !=
249 SSL_TEST_SERVERNAME_CB_NONE) {
250 server2_ctx = SSL_CTX_new(TLS_server_method());
251 TEST_check(server2_ctx != NULL);
253 client_ctx = SSL_CTX_new(TLS_client_method());
255 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
256 resume_server_ctx = SSL_CTX_new(TLS_server_method());
257 resume_client_ctx = SSL_CTX_new(TLS_client_method());
258 TEST_check(resume_server_ctx != NULL);
259 TEST_check(resume_client_ctx != NULL);
263 TEST_check(server_ctx != NULL);
264 TEST_check(client_ctx != NULL);
266 TEST_check(CONF_modules_load(conf, test_app, 0) > 0);
268 if (!SSL_CTX_config(server_ctx, "server")
269 || !SSL_CTX_config(client_ctx, "client")) {
273 if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
275 if (resume_server_ctx != NULL
276 && !SSL_CTX_config(resume_server_ctx, "resume-server"))
278 if (resume_client_ctx != NULL
279 && !SSL_CTX_config(resume_client_ctx, "resume-client"))
282 result = do_handshake(server_ctx, server2_ctx, client_ctx,
283 resume_server_ctx, resume_client_ctx, test_ctx);
285 ret = check_test(result, test_ctx);
288 CONF_modules_unload(0);
289 SSL_CTX_free(server_ctx);
290 SSL_CTX_free(server2_ctx);
291 SSL_CTX_free(client_ctx);
292 SSL_CTX_free(resume_server_ctx);
293 SSL_CTX_free(resume_client_ctx);
294 SSL_TEST_CTX_free(test_ctx);
295 HANDSHAKE_RESULT_free(result);
299 int main(int argc, char **argv)
307 conf = NCONF_new(NULL);
308 TEST_check(conf != NULL);
310 /* argv[1] should point to the test conf file */
311 TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
313 TEST_check(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
315 ADD_ALL_TESTS(test_handshake, (int)(num_tests));
316 result = run_tests(argv[0]);