]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/handshake_helper.c
Reorganize SSL test structures
[thirdparty/openssl.git] / test / handshake_helper.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 <string.h>
11
12 #include <openssl/bio.h>
13 #include <openssl/x509_vfy.h>
14 #include <openssl/ssl.h>
15
16 #include "handshake_helper.h"
17
18 HANDSHAKE_RESULT *HANDSHAKE_RESULT_new()
19 {
20 HANDSHAKE_RESULT *ret;
21 ret = OPENSSL_zalloc(sizeof(*ret));
22 OPENSSL_assert(ret != NULL);
23 return ret;
24 }
25
26 void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT *result)
27 {
28 OPENSSL_free(result->client_npn_negotiated);
29 OPENSSL_free(result->server_npn_negotiated);
30 OPENSSL_free(result->client_alpn_negotiated);
31 OPENSSL_free(result->server_alpn_negotiated);
32 OPENSSL_free(result);
33 }
34
35 /*
36 * Since there appears to be no way to extract the sent/received alert
37 * from the SSL object directly, we use the info callback and stash
38 * the result in ex_data.
39 */
40 typedef struct handshake_ex_data {
41 int alert_sent;
42 int alert_received;
43 int session_ticket_do_not_call;
44 ssl_servername_t servername;
45 } HANDSHAKE_EX_DATA;
46
47 typedef struct ctx_data {
48 unsigned char *npn_protocols;
49 size_t npn_protocols_len;
50 unsigned char *alpn_protocols;
51 size_t alpn_protocols_len;
52 } CTX_DATA;
53
54 /* |ctx_data| itself is stack-allocated. */
55 static void ctx_data_free_data(CTX_DATA *ctx_data)
56 {
57 OPENSSL_free(ctx_data->npn_protocols);
58 ctx_data->npn_protocols = NULL;
59 OPENSSL_free(ctx_data->alpn_protocols);
60 ctx_data->alpn_protocols = NULL;
61 }
62
63 static int ex_data_idx;
64
65 static void info_cb(const SSL *s, int where, int ret)
66 {
67 if (where & SSL_CB_ALERT) {
68 HANDSHAKE_EX_DATA *ex_data =
69 (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
70 if (where & SSL_CB_WRITE) {
71 ex_data->alert_sent = ret;
72 } else {
73 ex_data->alert_received = ret;
74 }
75 }
76 }
77
78 /* Select the appropriate server CTX.
79 * Returns SSL_TLSEXT_ERR_OK if a match was found.
80 * If |ignore| is 1, returns SSL_TLSEXT_ERR_NOACK on mismatch.
81 * Otherwise, returns SSL_TLSEXT_ERR_ALERT_FATAL on mismatch.
82 * An empty SNI extension also returns SSL_TSLEXT_ERR_NOACK.
83 */
84 static int select_server_ctx(SSL *s, void *arg, int ignore)
85 {
86 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
87 HANDSHAKE_EX_DATA *ex_data =
88 (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
89
90 if (servername == NULL) {
91 ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
92 return SSL_TLSEXT_ERR_NOACK;
93 }
94
95 if (strcmp(servername, "server2") == 0) {
96 SSL_CTX *new_ctx = (SSL_CTX*)arg;
97 SSL_set_SSL_CTX(s, new_ctx);
98 /*
99 * Copy over all the SSL_CTX options - reasonable behavior
100 * allows testing of cases where the options between two
101 * contexts differ/conflict
102 */
103 SSL_clear_options(s, 0xFFFFFFFFL);
104 SSL_set_options(s, SSL_CTX_get_options(new_ctx));
105
106 ex_data->servername = SSL_TEST_SERVERNAME_SERVER2;
107 return SSL_TLSEXT_ERR_OK;
108 } else if (strcmp(servername, "server1") == 0) {
109 ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
110 return SSL_TLSEXT_ERR_OK;
111 } else if (ignore) {
112 ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
113 return SSL_TLSEXT_ERR_NOACK;
114 } else {
115 /* Don't set an explicit alert, to test library defaults. */
116 return SSL_TLSEXT_ERR_ALERT_FATAL;
117 }
118 }
119
120 /*
121 * (RFC 6066):
122 * If the server understood the ClientHello extension but
123 * does not recognize the server name, the server SHOULD take one of two
124 * actions: either abort the handshake by sending a fatal-level
125 * unrecognized_name(112) alert or continue the handshake.
126 *
127 * This behaviour is up to the application to configure; we test both
128 * configurations to ensure the state machine propagates the result
129 * correctly.
130 */
131 static int servername_ignore_cb(SSL *s, int *ad, void *arg)
132 {
133 return select_server_ctx(s, arg, 1);
134 }
135
136 static int servername_reject_cb(SSL *s, int *ad, void *arg)
137 {
138 return select_server_ctx(s, arg, 0);
139 }
140
141 static int verify_reject_cb(X509_STORE_CTX *ctx, void *arg) {
142 X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION);
143 return 0;
144 }
145
146 static int verify_accept_cb(X509_STORE_CTX *ctx, void *arg) {
147 return 1;
148 }
149
150 static int broken_session_ticket_cb(SSL *s, unsigned char *key_name, unsigned char *iv,
151 EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc)
152 {
153 return 0;
154 }
155
156 static int do_not_call_session_ticket_cb(SSL *s, unsigned char *key_name,
157 unsigned char *iv,
158 EVP_CIPHER_CTX *ctx,
159 HMAC_CTX *hctx, int enc)
160 {
161 HANDSHAKE_EX_DATA *ex_data =
162 (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
163 ex_data->session_ticket_do_not_call = 1;
164 return 0;
165 }
166
167 #ifndef OPENSSL_NO_NEXTPROTONEG
168 /* Parse the comma-separated list into TLS format. */
169 static void parse_protos(const char *protos, unsigned char **out, size_t *outlen)
170 {
171 size_t len, i, prefix;
172
173 len = strlen(protos);
174
175 /* Should never have reuse. */
176 OPENSSL_assert(*out == NULL);
177
178 /* Test values are small, so we omit length limit checks. */
179 *out = OPENSSL_malloc(len + 1);
180 OPENSSL_assert(*out != NULL);
181 *outlen = len + 1;
182
183 /*
184 * foo => '3', 'f', 'o', 'o'
185 * foo,bar => '3', 'f', 'o', 'o', '3', 'b', 'a', 'r'
186 */
187 memcpy(*out + 1, protos, len);
188
189 prefix = 0;
190 i = prefix + 1;
191 while (i <= len) {
192 if ((*out)[i] == ',') {
193 OPENSSL_assert(i - 1 - prefix > 0);
194 (*out)[prefix] = i - 1 - prefix;
195 prefix = i;
196 }
197 i++;
198 }
199 OPENSSL_assert(len - prefix > 0);
200 (*out)[prefix] = len - prefix;
201 }
202
203 /*
204 * The client SHOULD select the first protocol advertised by the server that it
205 * also supports. In the event that the client doesn't support any of server's
206 * protocols, or the server doesn't advertise any, it SHOULD select the first
207 * protocol that it supports.
208 */
209 static int client_npn_cb(SSL *s, unsigned char **out, unsigned char *outlen,
210 const unsigned char *in, unsigned int inlen,
211 void *arg)
212 {
213 CTX_DATA *ctx_data = (CTX_DATA*)(arg);
214 int ret;
215
216 ret = SSL_select_next_proto(out, outlen, in, inlen,
217 ctx_data->npn_protocols,
218 ctx_data->npn_protocols_len);
219 /* Accept both OPENSSL_NPN_NEGOTIATED and OPENSSL_NPN_NO_OVERLAP. */
220 OPENSSL_assert(ret == OPENSSL_NPN_NEGOTIATED
221 || ret == OPENSSL_NPN_NO_OVERLAP);
222 return SSL_TLSEXT_ERR_OK;
223 }
224
225 static int server_npn_cb(SSL *s, const unsigned char **data,
226 unsigned int *len, void *arg)
227 {
228 CTX_DATA *ctx_data = (CTX_DATA*)(arg);
229 *data = ctx_data->npn_protocols;
230 *len = ctx_data->npn_protocols_len;
231 return SSL_TLSEXT_ERR_OK;
232 }
233
234 /*
235 * The server SHOULD select the most highly preferred protocol that it supports
236 * and that is also advertised by the client. In the event that the server
237 * supports no protocols that the client advertises, then the server SHALL
238 * respond with a fatal "no_application_protocol" alert.
239 */
240 static int server_alpn_cb(SSL *s, const unsigned char **out,
241 unsigned char *outlen, const unsigned char *in,
242 unsigned int inlen, void *arg)
243 {
244 CTX_DATA *ctx_data = (CTX_DATA*)(arg);
245 int ret;
246
247 /* SSL_select_next_proto isn't const-correct... */
248 unsigned char *tmp_out;
249
250 /*
251 * The result points either to |in| or to |ctx_data->alpn_protocols|.
252 * The callback is allowed to point to |in| or to a long-lived buffer,
253 * so we can return directly without storing a copy.
254 */
255 ret = SSL_select_next_proto(&tmp_out, outlen,
256 ctx_data->alpn_protocols,
257 ctx_data->alpn_protocols_len, in, inlen);
258
259 *out = tmp_out;
260 /* Unlike NPN, we don't tolerate a mismatch. */
261 return ret == OPENSSL_NPN_NEGOTIATED ? SSL_TLSEXT_ERR_OK
262 : SSL_TLSEXT_ERR_NOACK;
263 }
264 #endif
265
266 /*
267 * Configure callbacks and other properties that can't be set directly
268 * in the server/client CONF.
269 */
270 static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
271 SSL_CTX *client_ctx,
272 const SSL_TEST_EXTRA_CONF *extra,
273 CTX_DATA *server_ctx_data,
274 CTX_DATA *server2_ctx_data,
275 CTX_DATA *client_ctx_data)
276 {
277 unsigned char *ticket_keys;
278 size_t ticket_key_len;
279
280 switch (extra->client.verify_callback) {
281 case SSL_TEST_VERIFY_ACCEPT_ALL:
282 SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_cb,
283 NULL);
284 break;
285 case SSL_TEST_VERIFY_REJECT_ALL:
286 SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_cb,
287 NULL);
288 break;
289 default:
290 break;
291 }
292
293 /* link the two contexts for SNI purposes */
294 switch (extra->server.servername_callback) {
295 case SSL_TEST_SERVERNAME_IGNORE_MISMATCH:
296 SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb);
297 SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx);
298 break;
299 case SSL_TEST_SERVERNAME_REJECT_MISMATCH:
300 SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_reject_cb);
301 SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx);
302 break;
303 default:
304 break;
305 }
306
307 /*
308 * The initial_ctx/session_ctx always handles the encrypt/decrypt of the
309 * session ticket. This ticket_key callback is assigned to the second
310 * session (assigned via SNI), and should never be invoked
311 */
312 if (server2_ctx != NULL)
313 SSL_CTX_set_tlsext_ticket_key_cb(server2_ctx,
314 do_not_call_session_ticket_cb);
315
316 if (extra->server.broken_session_ticket) {
317 SSL_CTX_set_tlsext_ticket_key_cb(server_ctx, broken_session_ticket_cb);
318 }
319 #ifndef OPENSSL_NO_NEXTPROTONEG
320 if (extra->server.npn_protocols != NULL) {
321 parse_protos(extra->server.npn_protocols,
322 &server_ctx_data->npn_protocols,
323 &server_ctx_data->npn_protocols_len);
324 SSL_CTX_set_next_protos_advertised_cb(server_ctx, server_npn_cb,
325 server_ctx_data);
326 }
327 if (extra->server2.npn_protocols != NULL) {
328 parse_protos(extra->server2.npn_protocols,
329 &server2_ctx_data->npn_protocols,
330 &server2_ctx_data->npn_protocols_len);
331 OPENSSL_assert(server2_ctx != NULL);
332 SSL_CTX_set_next_protos_advertised_cb(server2_ctx, server_npn_cb,
333 server2_ctx_data);
334 }
335 if (extra->client.npn_protocols != NULL) {
336 parse_protos(extra->client.npn_protocols,
337 &client_ctx_data->npn_protocols,
338 &client_ctx_data->npn_protocols_len);
339 SSL_CTX_set_next_proto_select_cb(client_ctx, client_npn_cb,
340 client_ctx_data);
341 }
342 if (extra->server.alpn_protocols != NULL) {
343 parse_protos(extra->server.alpn_protocols,
344 &server_ctx_data->alpn_protocols,
345 &server_ctx_data->alpn_protocols_len);
346 SSL_CTX_set_alpn_select_cb(server_ctx, server_alpn_cb, server_ctx_data);
347 }
348 if (extra->server2.alpn_protocols != NULL) {
349 OPENSSL_assert(server2_ctx != NULL);
350 parse_protos(extra->server2.alpn_protocols,
351 &server2_ctx_data->alpn_protocols,
352 &server2_ctx_data->alpn_protocols_len);
353 SSL_CTX_set_alpn_select_cb(server2_ctx, server_alpn_cb, server2_ctx_data);
354 }
355 if (extra->client.alpn_protocols != NULL) {
356 unsigned char *alpn_protos = NULL;
357 size_t alpn_protos_len;
358 parse_protos(extra->client.alpn_protocols,
359 &alpn_protos, &alpn_protos_len);
360 /* Reversed return value convention... */
361 OPENSSL_assert(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos,
362 alpn_protos_len) == 0);
363 OPENSSL_free(alpn_protos);
364 }
365 #endif
366 /*
367 * Use fixed session ticket keys so that we can decrypt a ticket created with
368 * one CTX in another CTX. Don't address server2 for the moment.
369 */
370 ticket_key_len = SSL_CTX_set_tlsext_ticket_keys(server_ctx, NULL, 0);
371 ticket_keys = OPENSSL_zalloc(ticket_key_len);
372 OPENSSL_assert(ticket_keys != NULL);
373 OPENSSL_assert(SSL_CTX_set_tlsext_ticket_keys(server_ctx, ticket_keys,
374 ticket_key_len) == 1);
375 OPENSSL_free(ticket_keys);
376 }
377
378 /* Configure per-SSL callbacks and other properties. */
379 static void configure_handshake_ssl(SSL *server, SSL *client,
380 const SSL_TEST_EXTRA_CONF *extra)
381 {
382 if (extra->client.servername != SSL_TEST_SERVERNAME_NONE)
383 SSL_set_tlsext_host_name(client,
384 ssl_servername_name(extra->client.servername));
385 }
386
387
388 typedef enum {
389 PEER_SUCCESS,
390 PEER_RETRY,
391 PEER_ERROR
392 } peer_status_t;
393
394 /*
395 * RFC 5246 says:
396 *
397 * Note that as of TLS 1.1,
398 * failure to properly close a connection no longer requires that a
399 * session not be resumed. This is a change from TLS 1.0 to conform
400 * with widespread implementation practice.
401 *
402 * However,
403 * (a) OpenSSL requires that a connection be shutdown for all protocol versions.
404 * (b) We test lower versions, too.
405 * So we just implement shutdown. We do a full bidirectional shutdown so that we
406 * can compare sent and received close_notify alerts and get some test coverage
407 * for SSL_shutdown as a bonus.
408 */
409 static peer_status_t do_handshake_step(SSL *ssl, int shutdown)
410 {
411 int ret;
412
413 ret = shutdown ? SSL_shutdown(ssl) : SSL_do_handshake(ssl);
414
415 if (ret == 1) {
416 return PEER_SUCCESS;
417 } else if (ret == 0) {
418 return shutdown ? PEER_RETRY : PEER_ERROR;
419 } else {
420 int error = SSL_get_error(ssl, ret);
421 /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */
422 if (error == SSL_ERROR_WANT_READ)
423 return PEER_RETRY;
424 else
425 return PEER_ERROR;
426 }
427 }
428
429 typedef enum {
430 /* Both parties succeeded. */
431 HANDSHAKE_SUCCESS,
432 /* Client errored. */
433 CLIENT_ERROR,
434 /* Server errored. */
435 SERVER_ERROR,
436 /* Peers are in inconsistent state. */
437 INTERNAL_ERROR,
438 /* One or both peers not done. */
439 HANDSHAKE_RETRY
440 } handshake_status_t;
441
442 /*
443 * Determine the handshake outcome.
444 * last_status: the status of the peer to have acted last.
445 * previous_status: the status of the peer that didn't act last.
446 * client_spoke_last: 1 if the client went last.
447 */
448 static handshake_status_t handshake_status(peer_status_t last_status,
449 peer_status_t previous_status,
450 int client_spoke_last)
451 {
452 switch (last_status) {
453 case PEER_SUCCESS:
454 switch (previous_status) {
455 case PEER_SUCCESS:
456 /* Both succeeded. */
457 return HANDSHAKE_SUCCESS;
458 case PEER_RETRY:
459 /* Let the first peer finish. */
460 return HANDSHAKE_RETRY;
461 case PEER_ERROR:
462 /*
463 * Second peer succeeded despite the fact that the first peer
464 * already errored. This shouldn't happen.
465 */
466 return INTERNAL_ERROR;
467 }
468
469 case PEER_RETRY:
470 if (previous_status == PEER_RETRY) {
471 /* Neither peer is done. */
472 return HANDSHAKE_RETRY;
473 } else {
474 /*
475 * Deadlock: second peer is waiting for more input while first
476 * peer thinks they're done (no more input is coming).
477 */
478 return INTERNAL_ERROR;
479 }
480 case PEER_ERROR:
481 switch (previous_status) {
482 case PEER_SUCCESS:
483 /*
484 * First peer succeeded but second peer errored.
485 * TODO(emilia): we should be able to continue here (with some
486 * application data?) to ensure the first peer receives the
487 * alert / close_notify.
488 */
489 return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR;
490 case PEER_RETRY:
491 /* We errored; let the peer finish. */
492 return HANDSHAKE_RETRY;
493 case PEER_ERROR:
494 /* Both peers errored. Return the one that errored first. */
495 return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR;
496 }
497 }
498 /* Control should never reach here. */
499 return INTERNAL_ERROR;
500 }
501
502 #ifndef OPENSSL_NO_NEXTPROTONEG
503 /* Convert unsigned char buf's that shouldn't contain any NUL-bytes to char. */
504 static char *dup_str(const unsigned char *in, size_t len)
505 {
506 char *ret;
507
508 if(len == 0)
509 return NULL;
510
511 /* Assert that the string does not contain NUL-bytes. */
512 OPENSSL_assert(OPENSSL_strnlen((const char*)(in), len) == len);
513 ret = OPENSSL_strndup((const char*)(in), len);
514 OPENSSL_assert(ret != NULL);
515 return ret;
516 }
517 #endif
518
519 static HANDSHAKE_RESULT *do_handshake_internal(
520 SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx,
521 const SSL_TEST_EXTRA_CONF *extra, SSL_SESSION *session_in,
522 SSL_SESSION **session_out)
523 {
524 SSL *server, *client;
525 BIO *client_to_server, *server_to_client;
526 HANDSHAKE_EX_DATA server_ex_data, client_ex_data;
527 CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data;
528 HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new();
529 int client_turn = 1, shutdown = 0;
530 peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
531 handshake_status_t status = HANDSHAKE_RETRY;
532 unsigned char* tick = NULL;
533 size_t tick_len = 0;
534 SSL_SESSION* sess = NULL;
535 #ifndef OPENSSL_NO_NEXTPROTONEG
536 const unsigned char *proto = NULL;
537 /* API dictates unsigned int rather than size_t. */
538 unsigned int proto_len = 0;
539 #endif
540
541 memset(&server_ctx_data, 0, sizeof(server_ctx_data));
542 memset(&server2_ctx_data, 0, sizeof(server2_ctx_data));
543 memset(&client_ctx_data, 0, sizeof(client_ctx_data));
544
545 configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, extra,
546 &server_ctx_data, &server2_ctx_data, &client_ctx_data);
547
548 server = SSL_new(server_ctx);
549 client = SSL_new(client_ctx);
550 OPENSSL_assert(server != NULL && client != NULL);
551
552 configure_handshake_ssl(server, client, extra);
553 if (session_in != NULL) {
554 /* In case we're testing resumption without tickets. */
555 OPENSSL_assert(SSL_CTX_add_session(server_ctx, session_in));
556 OPENSSL_assert(SSL_set_session(client, session_in));
557 }
558
559 memset(&server_ex_data, 0, sizeof(server_ex_data));
560 memset(&client_ex_data, 0, sizeof(client_ex_data));
561
562 ret->result = SSL_TEST_INTERNAL_ERROR;
563
564 client_to_server = BIO_new(BIO_s_mem());
565 server_to_client = BIO_new(BIO_s_mem());
566
567 OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
568
569 /* Non-blocking bio. */
570 BIO_set_nbio(client_to_server, 1);
571 BIO_set_nbio(server_to_client, 1);
572
573 SSL_set_connect_state(client);
574 SSL_set_accept_state(server);
575
576 /* The bios are now owned by the SSL object. */
577 SSL_set_bio(client, server_to_client, client_to_server);
578 OPENSSL_assert(BIO_up_ref(server_to_client) > 0);
579 OPENSSL_assert(BIO_up_ref(client_to_server) > 0);
580 SSL_set_bio(server, client_to_server, server_to_client);
581
582 ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
583 OPENSSL_assert(ex_data_idx >= 0);
584
585 OPENSSL_assert(SSL_set_ex_data(server, ex_data_idx,
586 &server_ex_data) == 1);
587 OPENSSL_assert(SSL_set_ex_data(client, ex_data_idx,
588 &client_ex_data) == 1);
589
590 SSL_set_info_callback(server, &info_cb);
591 SSL_set_info_callback(client, &info_cb);
592
593 /*
594 * Half-duplex handshake loop.
595 * Client and server speak to each other synchronously in the same process.
596 * We use non-blocking BIOs, so whenever one peer blocks for read, it
597 * returns PEER_RETRY to indicate that it's the other peer's turn to write.
598 * The handshake succeeds once both peers have succeeded. If one peer
599 * errors out, we also let the other peer retry (and presumably fail).
600 */
601 for(;;) {
602 if (client_turn) {
603 client_status = do_handshake_step(client, shutdown);
604 status = handshake_status(client_status, server_status,
605 1 /* client went last */);
606 } else {
607 server_status = do_handshake_step(server, shutdown);
608 status = handshake_status(server_status, client_status,
609 0 /* server went last */);
610 }
611
612 switch (status) {
613 case HANDSHAKE_SUCCESS:
614 if (shutdown) {
615 ret->result = SSL_TEST_SUCCESS;
616 goto err;
617 } else {
618 client_status = server_status = PEER_RETRY;
619 shutdown = 1;
620 client_turn = 1;
621 break;
622 }
623 case CLIENT_ERROR:
624 ret->result = SSL_TEST_CLIENT_FAIL;
625 goto err;
626 case SERVER_ERROR:
627 ret->result = SSL_TEST_SERVER_FAIL;
628 goto err;
629 case INTERNAL_ERROR:
630 ret->result = SSL_TEST_INTERNAL_ERROR;
631 goto err;
632 case HANDSHAKE_RETRY:
633 /* Continue. */
634 client_turn ^= 1;
635 break;
636 }
637 }
638 err:
639 ret->server_alert_sent = server_ex_data.alert_sent;
640 ret->server_alert_received = client_ex_data.alert_received;
641 ret->client_alert_sent = client_ex_data.alert_sent;
642 ret->client_alert_received = server_ex_data.alert_received;
643 ret->server_protocol = SSL_version(server);
644 ret->client_protocol = SSL_version(client);
645 ret->servername = server_ex_data.servername;
646 if ((sess = SSL_get0_session(client)) != NULL)
647 SSL_SESSION_get0_ticket(sess, &tick, &tick_len);
648 if (tick == NULL || tick_len == 0)
649 ret->session_ticket = SSL_TEST_SESSION_TICKET_NO;
650 else
651 ret->session_ticket = SSL_TEST_SESSION_TICKET_YES;
652 ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;
653
654 #ifndef OPENSSL_NO_NEXTPROTONEG
655 SSL_get0_next_proto_negotiated(client, &proto, &proto_len);
656 ret->client_npn_negotiated = dup_str(proto, proto_len);
657
658 SSL_get0_next_proto_negotiated(server, &proto, &proto_len);
659 ret->server_npn_negotiated = dup_str(proto, proto_len);
660
661 SSL_get0_alpn_selected(client, &proto, &proto_len);
662 ret->client_alpn_negotiated = dup_str(proto, proto_len);
663
664 SSL_get0_alpn_selected(server, &proto, &proto_len);
665 ret->server_alpn_negotiated = dup_str(proto, proto_len);
666 #endif
667
668 ret->client_resumed = SSL_session_reused(client);
669 ret->server_resumed = SSL_session_reused(server);
670
671 if (session_out != NULL)
672 *session_out = SSL_get1_session(client);
673
674 ctx_data_free_data(&server_ctx_data);
675 ctx_data_free_data(&server2_ctx_data);
676 ctx_data_free_data(&client_ctx_data);
677
678 SSL_free(server);
679 SSL_free(client);
680 return ret;
681 }
682
683 HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
684 SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx,
685 SSL_CTX *resume_client_ctx,
686 const SSL_TEST_CTX *test_ctx)
687 {
688 HANDSHAKE_RESULT *result;
689 SSL_SESSION *session = NULL;
690
691 result = do_handshake_internal(server_ctx, server2_ctx, client_ctx,
692 &test_ctx->extra, NULL, &session);
693 if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_SIMPLE)
694 goto end;
695
696 OPENSSL_assert(test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME);
697
698 if (result->result != SSL_TEST_SUCCESS) {
699 result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED;
700 return result;
701 }
702
703 HANDSHAKE_RESULT_free(result);
704 /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */
705 result = do_handshake_internal(resume_server_ctx, NULL, resume_client_ctx,
706 &test_ctx->resume_extra, session, NULL);
707 end:
708 SSL_SESSION_free(session);
709 return result;
710 }