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"
20 #include "test_main_custom.h"
22 static CONF *conf = NULL;
24 /* Currently the section names are of the form test-<number>, e.g. test-15. */
25 #define MAX_TESTCASE_NAME_LENGTH 100
27 static const char *print_alert(int alert)
29 return alert ? SSL_alert_desc_string_long(alert) : "no alert";
32 static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
34 if (result->result != test_ctx->expected_result) {
35 fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
36 ssl_test_result_name(test_ctx->expected_result),
37 ssl_test_result_name(result->result));
43 static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
45 if (result->client_alert_sent != result->client_alert_received) {
46 fprintf(stderr, "Client sent alert %s but server received %s\n.",
47 print_alert(result->client_alert_sent),
48 print_alert(result->client_alert_received));
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
63 if (result->server_alert_sent != result->server_alert_received) {
64 fprintf(stderr, "Server sent alert %s but client received %s\n.",
65 print_alert(result->server_alert_sent),
66 print_alert(result->server_alert_received));
70 /* Tolerate an alert if one wasn't explicitly specified in the test. */
71 if (test_ctx->expected_client_alert
73 * The info callback alert value is computed as
74 * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
75 * where the low byte is the alert code and the high byte is other stuff.
77 && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
78 fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
79 print_alert(test_ctx->expected_client_alert),
80 print_alert(result->client_alert_sent));
84 if (test_ctx->expected_server_alert
85 && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
86 fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
87 print_alert(test_ctx->expected_server_alert),
88 print_alert(result->server_alert_sent));
92 if (result->client_num_fatal_alerts_sent > 1) {
93 fprintf(stderr, "Client sent %d fatal alerts.\n",
94 result->client_num_fatal_alerts_sent);
97 if (result->server_num_fatal_alerts_sent > 1) {
98 fprintf(stderr, "Server sent %d alerts.\n",
99 result->server_num_fatal_alerts_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->expected_protocol) {
115 if (result->client_protocol != test_ctx->expected_protocol) {
116 fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
117 ssl_protocol_name(test_ctx->expected_protocol),
118 ssl_protocol_name(result->client_protocol));
125 static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
127 if (result->servername != test_ctx->expected_servername) {
128 fprintf(stderr, "Client ServerName mismatch, expected %s, got %s\n.",
129 ssl_servername_name(test_ctx->expected_servername),
130 ssl_servername_name(result->servername));
136 static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
138 if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
140 if (result->session_ticket != test_ctx->session_ticket_expected) {
141 fprintf(stderr, "Client SessionTicketExpected mismatch, expected %s, got %s\n.",
142 ssl_session_ticket_name(test_ctx->session_ticket_expected),
143 ssl_session_ticket_name(result->session_ticket));
149 static int check_compression(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
151 if (result->compression != test_ctx->compression_expected) {
152 fprintf(stderr, "Client CompressionExpected mismatch, expected %d, got %d\n.",
153 test_ctx->compression_expected,
154 result->compression);
159 #ifndef OPENSSL_NO_NEXTPROTONEG
160 static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
163 ret &= strings_equal("NPN Negotiated (client vs server)",
164 result->client_npn_negotiated,
165 result->server_npn_negotiated);
166 ret &= strings_equal("ExpectedNPNProtocol",
167 test_ctx->expected_npn_protocol,
168 result->client_npn_negotiated);
173 static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
176 ret &= strings_equal("ALPN Negotiated (client vs server)",
177 result->client_alpn_negotiated,
178 result->server_alpn_negotiated);
179 ret &= strings_equal("ExpectedALPNProtocol",
180 test_ctx->expected_alpn_protocol,
181 result->client_alpn_negotiated);
185 static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
187 if (result->client_resumed != result->server_resumed) {
188 fprintf(stderr, "Resumption mismatch (client vs server): %d vs %d\n",
189 result->client_resumed, result->server_resumed);
192 if (result->client_resumed != test_ctx->resumption_expected) {
193 fprintf(stderr, "ResumptionExpected mismatch: %d vs %d\n",
194 test_ctx->resumption_expected, result->client_resumed);
200 static int check_nid(const char *name, int expected_nid, int nid)
202 if (expected_nid == 0 || expected_nid == nid)
204 fprintf(stderr, "%s type mismatch, %s vs %s\n",
205 name, OBJ_nid2ln(expected_nid),
206 nid == NID_undef ? "absent" : OBJ_nid2ln(nid));
210 static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
212 return check_nid("Tmp key", test_ctx->expected_tmp_key_type,
213 result->tmp_key_type);
216 static int check_server_cert_type(HANDSHAKE_RESULT *result,
217 SSL_TEST_CTX *test_ctx)
219 return check_nid("Server certificate", test_ctx->expected_server_cert_type,
220 result->server_cert_type);
223 static int check_server_sign_hash(HANDSHAKE_RESULT *result,
224 SSL_TEST_CTX *test_ctx)
226 return check_nid("Server signing hash", test_ctx->expected_server_sign_hash,
227 result->server_sign_hash);
230 static int check_server_sign_type(HANDSHAKE_RESULT *result,
231 SSL_TEST_CTX *test_ctx)
233 return check_nid("Server signing", test_ctx->expected_server_sign_type,
234 result->server_sign_type);
237 static int check_client_cert_type(HANDSHAKE_RESULT *result,
238 SSL_TEST_CTX *test_ctx)
240 return check_nid("Client certificate", test_ctx->expected_client_cert_type,
241 result->client_cert_type);
244 static int check_client_sign_hash(HANDSHAKE_RESULT *result,
245 SSL_TEST_CTX *test_ctx)
247 return check_nid("Client signing hash", test_ctx->expected_client_sign_hash,
248 result->client_sign_hash);
251 static int check_client_sign_type(HANDSHAKE_RESULT *result,
252 SSL_TEST_CTX *test_ctx)
254 return check_nid("Client signing", test_ctx->expected_client_sign_type,
255 result->client_sign_type);
258 static void print_ca_names(STACK_OF(X509_NAME) *names)
263 if (names == NULL || sk_X509_NAME_num(names) == 0) {
264 fprintf(stderr, " <empty>\n");
267 err = BIO_new_fp(stderr, BIO_NOCLOSE);
268 for (i = 0; i < sk_X509_NAME_num(names); i++) {
269 X509_NAME_print_ex(err, sk_X509_NAME_value(names, i), 4,
276 static int check_ca_names(const char *name,
277 STACK_OF(X509_NAME) *expected_names,
278 STACK_OF(X509_NAME) *names)
282 if (expected_names == NULL)
284 if (names == NULL || sk_X509_NAME_num(names) == 0) {
285 if (sk_X509_NAME_num(expected_names) == 0)
289 if (sk_X509_NAME_num(names) != sk_X509_NAME_num(expected_names))
291 for (i = 0; i < sk_X509_NAME_num(names); i++) {
292 if (X509_NAME_cmp(sk_X509_NAME_value(names, i),
293 sk_X509_NAME_value(expected_names, i)) != 0) {
299 fprintf(stderr, "%s: list mismatch\nExpected Names:\n", name);
300 print_ca_names(expected_names);
301 fprintf(stderr, "Received Names:\n");
302 print_ca_names(names);
306 static int check_client_ca_names(HANDSHAKE_RESULT *result,
307 SSL_TEST_CTX *test_ctx)
309 return check_ca_names("Client CA names",
310 test_ctx->expected_client_ca_names,
311 result->client_ca_names);
315 * This could be further simplified by constructing an expected
316 * HANDSHAKE_RESULT, and implementing comparison methods for
319 static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
322 ret &= check_result(result, test_ctx);
323 ret &= check_alerts(result, test_ctx);
324 if (result->result == SSL_TEST_SUCCESS) {
325 ret &= check_protocol(result, test_ctx);
326 ret &= check_servername(result, test_ctx);
327 ret &= check_session_ticket(result, test_ctx);
328 ret &= check_compression(result, test_ctx);
329 ret &= (result->session_ticket_do_not_call == 0);
330 #ifndef OPENSSL_NO_NEXTPROTONEG
331 ret &= check_npn(result, test_ctx);
333 ret &= check_alpn(result, test_ctx);
334 ret &= check_resumption(result, test_ctx);
335 ret &= check_tmp_key(result, test_ctx);
336 ret &= check_server_cert_type(result, test_ctx);
337 ret &= check_server_sign_hash(result, test_ctx);
338 ret &= check_server_sign_type(result, test_ctx);
339 ret &= check_client_cert_type(result, test_ctx);
340 ret &= check_client_sign_hash(result, test_ctx);
341 ret &= check_client_sign_type(result, test_ctx);
342 ret &= check_client_ca_names(result, test_ctx);
347 static int test_handshake(int idx)
350 SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
351 *resume_server_ctx = NULL, *resume_client_ctx = NULL;
352 SSL_TEST_CTX *test_ctx = NULL;
353 HANDSHAKE_RESULT *result = NULL;
354 char test_app[MAX_TESTCASE_NAME_LENGTH];
356 BIO_snprintf(test_app, sizeof(test_app), "test-%d", idx);
358 test_ctx = SSL_TEST_CTX_create(conf, test_app);
359 if (test_ctx == NULL)
362 #ifndef OPENSSL_NO_DTLS
363 if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
364 server_ctx = SSL_CTX_new(DTLS_server_method());
365 if (test_ctx->extra.server.servername_callback !=
366 SSL_TEST_SERVERNAME_CB_NONE) {
367 server2_ctx = SSL_CTX_new(DTLS_server_method());
368 TEST_check(server2_ctx != NULL);
370 client_ctx = SSL_CTX_new(DTLS_client_method());
371 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
372 resume_server_ctx = SSL_CTX_new(DTLS_server_method());
373 resume_client_ctx = SSL_CTX_new(DTLS_client_method());
374 TEST_check(resume_server_ctx != NULL);
375 TEST_check(resume_client_ctx != NULL);
379 if (test_ctx->method == SSL_TEST_METHOD_TLS) {
380 server_ctx = SSL_CTX_new(TLS_server_method());
381 /* SNI on resumption isn't supported/tested yet. */
382 if (test_ctx->extra.server.servername_callback !=
383 SSL_TEST_SERVERNAME_CB_NONE) {
384 server2_ctx = SSL_CTX_new(TLS_server_method());
385 TEST_check(server2_ctx != NULL);
387 client_ctx = SSL_CTX_new(TLS_client_method());
389 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
390 resume_server_ctx = SSL_CTX_new(TLS_server_method());
391 resume_client_ctx = SSL_CTX_new(TLS_client_method());
392 TEST_check(resume_server_ctx != NULL);
393 TEST_check(resume_client_ctx != NULL);
397 TEST_check(server_ctx != NULL);
398 TEST_check(client_ctx != NULL);
400 TEST_check(CONF_modules_load(conf, test_app, 0) > 0);
402 if (!SSL_CTX_config(server_ctx, "server")
403 || !SSL_CTX_config(client_ctx, "client")) {
407 if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
409 if (resume_server_ctx != NULL
410 && !SSL_CTX_config(resume_server_ctx, "resume-server"))
412 if (resume_client_ctx != NULL
413 && !SSL_CTX_config(resume_client_ctx, "resume-client"))
416 result = do_handshake(server_ctx, server2_ctx, client_ctx,
417 resume_server_ctx, resume_client_ctx, test_ctx);
419 ret = check_test(result, test_ctx);
422 CONF_modules_unload(0);
423 SSL_CTX_free(server_ctx);
424 SSL_CTX_free(server2_ctx);
425 SSL_CTX_free(client_ctx);
426 SSL_CTX_free(resume_server_ctx);
427 SSL_CTX_free(resume_client_ctx);
428 SSL_TEST_CTX_free(test_ctx);
429 HANDSHAKE_RESULT_free(result);
433 int test_main(int argc, char **argv)
441 conf = NCONF_new(NULL);
442 TEST_check(conf != NULL);
444 /* argv[1] should point to the test conf file */
445 TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
447 TEST_check(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
449 ADD_ALL_TESTS(test_handshake, (int)(num_tests));
450 result = run_tests(argv[0]);