c4f298e9454f8cb2557ff01621a699d1883cb578
[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 /* Parse the comma-separated list into TLS format. */
168 static void parse_protos(const char *protos, unsigned char **out, size_t *outlen)
169 {
170     size_t len, i, prefix;
171
172     len = strlen(protos);
173
174     /* Should never have reuse. */
175     OPENSSL_assert(*out == NULL);
176
177     /* Test values are small, so we omit length limit checks. */
178     *out = OPENSSL_malloc(len + 1);
179     OPENSSL_assert(*out != NULL);
180     *outlen = len + 1;
181
182     /*
183      * foo => '3', 'f', 'o', 'o'
184      * foo,bar => '3', 'f', 'o', 'o', '3', 'b', 'a', 'r'
185      */
186     memcpy(*out + 1, protos, len);
187
188     prefix = 0;
189     i = prefix + 1;
190     while (i <= len) {
191         if ((*out)[i] == ',') {
192             OPENSSL_assert(i - 1 - prefix > 0);
193             (*out)[prefix] = i - 1 - prefix;
194             prefix = i;
195         }
196         i++;
197     }
198     OPENSSL_assert(len - prefix > 0);
199     (*out)[prefix] = len - prefix;
200 }
201
202 /*
203  * The client SHOULD select the first protocol advertised by the server that it
204  * also supports.  In the event that the client doesn't support any of server's
205  * protocols, or the server doesn't advertise any, it SHOULD select the first
206  * protocol that it supports.
207  */
208 static int client_npn_cb(SSL *s, unsigned char **out, unsigned char *outlen,
209                          const unsigned char *in, unsigned int inlen,
210                          void *arg)
211 {
212     CTX_DATA *ctx_data = (CTX_DATA*)(arg);
213     int ret;
214
215     ret = SSL_select_next_proto(out, outlen, in, inlen,
216                                 ctx_data->npn_protocols,
217                                 ctx_data->npn_protocols_len);
218     /* Accept both OPENSSL_NPN_NEGOTIATED and OPENSSL_NPN_NO_OVERLAP. */
219     OPENSSL_assert(ret == OPENSSL_NPN_NEGOTIATED
220                    || ret == OPENSSL_NPN_NO_OVERLAP);
221     return SSL_TLSEXT_ERR_OK;
222 }
223
224 static int server_npn_cb(SSL *s, const unsigned char **data,
225                          unsigned int *len, void *arg)
226 {
227     CTX_DATA *ctx_data = (CTX_DATA*)(arg);
228     *data = ctx_data->npn_protocols;
229     *len = ctx_data->npn_protocols_len;
230     return SSL_TLSEXT_ERR_OK;
231 }
232
233 /*
234  * The server SHOULD select the most highly preferred protocol that it supports
235  * and that is also advertised by the client.  In the event that the server
236  * supports no protocols that the client advertises, then the server SHALL
237  * respond with a fatal "no_application_protocol" alert.
238  */
239 static int server_alpn_cb(SSL *s, const unsigned char **out,
240                           unsigned char *outlen, const unsigned char *in,
241                           unsigned int inlen, void *arg)
242 {
243     CTX_DATA *ctx_data = (CTX_DATA*)(arg);
244     int ret;
245
246     /* SSL_select_next_proto isn't const-correct... */
247     unsigned char *tmp_out;
248
249     /*
250      * The result points either to |in| or to |ctx_data->alpn_protocols|.
251      * The callback is allowed to point to |in| or to a long-lived buffer,
252      * so we can return directly without storing a copy.
253      */
254     ret = SSL_select_next_proto(&tmp_out, outlen,
255                                 ctx_data->alpn_protocols,
256                                 ctx_data->alpn_protocols_len, in, inlen);
257
258     *out = tmp_out;
259     /* Unlike NPN, we don't tolerate a mismatch. */
260     return ret == OPENSSL_NPN_NEGOTIATED ? SSL_TLSEXT_ERR_OK
261         : SSL_TLSEXT_ERR_NOACK;
262 }
263
264
265 /*
266  * Configure callbacks and other properties that can't be set directly
267  * in the server/client CONF.
268  */
269 static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
270                                     SSL_CTX *client_ctx,
271                                     const SSL_TEST_CTX *test_ctx,
272                                     CTX_DATA *server_ctx_data,
273                                     CTX_DATA *server2_ctx_data,
274                                     CTX_DATA *client_ctx_data)
275 {
276     unsigned char *ticket_keys;
277     size_t ticket_key_len;
278
279     switch (test_ctx->client_verify_callback) {
280     case SSL_TEST_VERIFY_ACCEPT_ALL:
281         SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_cb,
282                                          NULL);
283         break;
284     case SSL_TEST_VERIFY_REJECT_ALL:
285         SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_cb,
286                                          NULL);
287         break;
288     default:
289         break;
290     }
291
292     /* link the two contexts for SNI purposes */
293     switch (test_ctx->servername_callback) {
294     case SSL_TEST_SERVERNAME_IGNORE_MISMATCH:
295         SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb);
296         SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx);
297         break;
298     case SSL_TEST_SERVERNAME_REJECT_MISMATCH:
299         SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_reject_cb);
300         SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx);
301         break;
302     default:
303         break;
304     }
305
306     /*
307      * The initial_ctx/session_ctx always handles the encrypt/decrypt of the
308      * session ticket. This ticket_key callback is assigned to the second
309      * session (assigned via SNI), and should never be invoked
310      */
311     if (server2_ctx != NULL)
312         SSL_CTX_set_tlsext_ticket_key_cb(server2_ctx,
313                                          do_not_call_session_ticket_cb);
314
315     if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_BROKEN) {
316         SSL_CTX_set_tlsext_ticket_key_cb(server_ctx, broken_session_ticket_cb);
317     }
318     if (test_ctx->server_npn_protocols != NULL) {
319         parse_protos(test_ctx->server_npn_protocols,
320                      &server_ctx_data->npn_protocols,
321                      &server_ctx_data->npn_protocols_len);
322         SSL_CTX_set_next_protos_advertised_cb(server_ctx, server_npn_cb,
323                                               server_ctx_data);
324     }
325     if (test_ctx->server2_npn_protocols != NULL) {
326         parse_protos(test_ctx->server2_npn_protocols,
327                      &server2_ctx_data->npn_protocols,
328                      &server2_ctx_data->npn_protocols_len);
329         OPENSSL_assert(server2_ctx != NULL);
330         SSL_CTX_set_next_protos_advertised_cb(server2_ctx, server_npn_cb,
331                                               server2_ctx_data);
332     }
333     if (test_ctx->client_npn_protocols != NULL) {
334         parse_protos(test_ctx->client_npn_protocols,
335                      &client_ctx_data->npn_protocols,
336                      &client_ctx_data->npn_protocols_len);
337         SSL_CTX_set_next_proto_select_cb(client_ctx, client_npn_cb,
338                                          client_ctx_data);
339     }
340     if (test_ctx->server_alpn_protocols != NULL) {
341         parse_protos(test_ctx->server_alpn_protocols,
342                      &server_ctx_data->alpn_protocols,
343                      &server_ctx_data->alpn_protocols_len);
344         SSL_CTX_set_alpn_select_cb(server_ctx, server_alpn_cb, server_ctx_data);
345     }
346     if (test_ctx->server2_alpn_protocols != NULL) {
347         OPENSSL_assert(server2_ctx != NULL);
348         parse_protos(test_ctx->server2_alpn_protocols,
349                      &server2_ctx_data->alpn_protocols,
350                      &server2_ctx_data->alpn_protocols_len);
351         SSL_CTX_set_alpn_select_cb(server2_ctx, server_alpn_cb, server2_ctx_data);
352     }
353     if (test_ctx->client_alpn_protocols != NULL) {
354         unsigned char *alpn_protos = NULL;
355         size_t alpn_protos_len;
356         parse_protos(test_ctx->client_alpn_protocols,
357                      &alpn_protos, &alpn_protos_len);
358         /* Reversed return value convention... */
359         OPENSSL_assert(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos,
360                                                alpn_protos_len) == 0);
361         OPENSSL_free(alpn_protos);
362     }
363     /*
364      * Use fixed session ticket keys so that we can decrypt a ticket created with
365      * one CTX in another CTX. Don't address server2 for the moment.
366      */
367     ticket_key_len = SSL_CTX_set_tlsext_ticket_keys(server_ctx, NULL, 0);
368     ticket_keys = OPENSSL_zalloc(ticket_key_len);
369     OPENSSL_assert(ticket_keys != NULL);
370     OPENSSL_assert(SSL_CTX_set_tlsext_ticket_keys(server_ctx, ticket_keys,
371                                                   ticket_key_len) == 1);
372     OPENSSL_free(ticket_keys);
373 }
374
375 /* Configure per-SSL callbacks and other properties. */
376 static void configure_handshake_ssl(SSL *server, SSL *client,
377                                     const SSL_TEST_CTX *test_ctx)
378 {
379     if (test_ctx->servername != SSL_TEST_SERVERNAME_NONE)
380         SSL_set_tlsext_host_name(client,
381                                  ssl_servername_name(test_ctx->servername));
382 }
383
384
385 typedef enum {
386     PEER_SUCCESS,
387     PEER_RETRY,
388     PEER_ERROR
389 } peer_status_t;
390
391 /*
392  * RFC 5246 says:
393  *
394  * Note that as of TLS 1.1,
395  *     failure to properly close a connection no longer requires that a
396  *     session not be resumed.  This is a change from TLS 1.0 to conform
397  *     with widespread implementation practice.
398  *
399  * However,
400  * (a) OpenSSL requires that a connection be shutdown for all protocol versions.
401  * (b) We test lower versions, too.
402  * So we just implement shutdown. We do a full bidirectional shutdown so that we
403  * can compare sent and received close_notify alerts and get some test coverage
404  * for SSL_shutdown as a bonus.
405  */
406 static peer_status_t do_handshake_step(SSL *ssl, int shutdown)
407 {
408     int ret;
409
410     ret = shutdown ? SSL_shutdown(ssl) : SSL_do_handshake(ssl);
411
412     if (ret == 1) {
413         return PEER_SUCCESS;
414     } else if (ret == 0) {
415         return shutdown ? PEER_RETRY : PEER_ERROR;
416     } else {
417         int error = SSL_get_error(ssl, ret);
418         /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */
419         if (error == SSL_ERROR_WANT_READ)
420             return PEER_RETRY;
421         else
422             return PEER_ERROR;
423     }
424 }
425
426 typedef enum {
427     /* Both parties succeeded. */
428     HANDSHAKE_SUCCESS,
429     /* Client errored. */
430     CLIENT_ERROR,
431     /* Server errored. */
432     SERVER_ERROR,
433     /* Peers are in inconsistent state. */
434     INTERNAL_ERROR,
435     /* One or both peers not done. */
436     HANDSHAKE_RETRY
437 } handshake_status_t;
438
439 /*
440  * Determine the handshake outcome.
441  * last_status: the status of the peer to have acted last.
442  * previous_status: the status of the peer that didn't act last.
443  * client_spoke_last: 1 if the client went last.
444  */
445 static handshake_status_t handshake_status(peer_status_t last_status,
446                                            peer_status_t previous_status,
447                                            int client_spoke_last)
448 {
449     switch (last_status) {
450     case PEER_SUCCESS:
451         switch (previous_status) {
452         case PEER_SUCCESS:
453             /* Both succeeded. */
454             return HANDSHAKE_SUCCESS;
455         case PEER_RETRY:
456             /* Let the first peer finish. */
457             return HANDSHAKE_RETRY;
458         case PEER_ERROR:
459             /*
460              * Second peer succeeded despite the fact that the first peer
461              * already errored. This shouldn't happen.
462              */
463             return INTERNAL_ERROR;
464         }
465
466     case PEER_RETRY:
467         if (previous_status == PEER_RETRY) {
468             /* Neither peer is done. */
469             return HANDSHAKE_RETRY;
470         } else {
471             /*
472              * Deadlock: second peer is waiting for more input while first
473              * peer thinks they're done (no more input is coming).
474              */
475             return INTERNAL_ERROR;
476         }
477     case PEER_ERROR:
478         switch (previous_status) {
479         case PEER_SUCCESS:
480             /*
481              * First peer succeeded but second peer errored.
482              * TODO(emilia): we should be able to continue here (with some
483              * application data?) to ensure the first peer receives the
484              * alert / close_notify.
485              */
486             return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR;
487         case PEER_RETRY:
488             /* We errored; let the peer finish. */
489             return HANDSHAKE_RETRY;
490         case PEER_ERROR:
491             /* Both peers errored. Return the one that errored first. */
492             return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR;
493         }
494     }
495     /* Control should never reach here. */
496     return INTERNAL_ERROR;
497 }
498
499 /* Convert unsigned char buf's that shouldn't contain any NUL-bytes to char. */
500 static char *dup_str(const unsigned char *in, size_t len)
501 {
502     char *ret;
503
504     if(len == 0)
505         return NULL;
506
507     /* Assert that the string does not contain NUL-bytes. */
508     OPENSSL_assert(OPENSSL_strnlen((const char*)(in), len) == len);
509     ret = OPENSSL_strndup((const char*)(in), len);
510     OPENSSL_assert(ret != NULL);
511     return ret;
512 }
513
514 static HANDSHAKE_RESULT *do_handshake_internal(
515     SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx,
516     const SSL_TEST_CTX *test_ctx, SSL_SESSION *session_in,
517     SSL_SESSION **session_out)
518 {
519     SSL *server, *client;
520     BIO *client_to_server, *server_to_client;
521     HANDSHAKE_EX_DATA server_ex_data, client_ex_data;
522     CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data;
523     HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new();
524     int client_turn = 1, shutdown = 0;
525     peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
526     handshake_status_t status = HANDSHAKE_RETRY;
527     unsigned char* tick = NULL;
528     size_t tick_len = 0;
529     SSL_SESSION* sess = NULL;
530     const unsigned char *proto = NULL;
531     /* API dictates unsigned int rather than size_t. */
532     unsigned int proto_len = 0;
533
534     memset(&server_ctx_data, 0, sizeof(server_ctx_data));
535     memset(&server2_ctx_data, 0, sizeof(server2_ctx_data));
536     memset(&client_ctx_data, 0, sizeof(client_ctx_data));
537
538     configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, test_ctx,
539                             &server_ctx_data, &server2_ctx_data, &client_ctx_data);
540
541     server = SSL_new(server_ctx);
542     client = SSL_new(client_ctx);
543     OPENSSL_assert(server != NULL && client != NULL);
544
545     configure_handshake_ssl(server, client, test_ctx);
546     if (session_in != NULL) {
547         /* In case we're testing resumption without tickets. */
548         OPENSSL_assert(SSL_CTX_add_session(server_ctx, session_in));
549         OPENSSL_assert(SSL_set_session(client, session_in));
550     }
551
552     memset(&server_ex_data, 0, sizeof(server_ex_data));
553     memset(&client_ex_data, 0, sizeof(client_ex_data));
554
555     ret->result = SSL_TEST_INTERNAL_ERROR;
556
557     client_to_server = BIO_new(BIO_s_mem());
558     server_to_client = BIO_new(BIO_s_mem());
559
560     OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
561
562     /* Non-blocking bio. */
563     BIO_set_nbio(client_to_server, 1);
564     BIO_set_nbio(server_to_client, 1);
565
566     SSL_set_connect_state(client);
567     SSL_set_accept_state(server);
568
569     /* The bios are now owned by the SSL object. */
570     SSL_set_bio(client, server_to_client, client_to_server);
571     OPENSSL_assert(BIO_up_ref(server_to_client) > 0);
572     OPENSSL_assert(BIO_up_ref(client_to_server) > 0);
573     SSL_set_bio(server, client_to_server, server_to_client);
574
575     ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
576     OPENSSL_assert(ex_data_idx >= 0);
577
578     OPENSSL_assert(SSL_set_ex_data(server, ex_data_idx,
579                                    &server_ex_data) == 1);
580     OPENSSL_assert(SSL_set_ex_data(client, ex_data_idx,
581                                    &client_ex_data) == 1);
582
583     SSL_set_info_callback(server, &info_cb);
584     SSL_set_info_callback(client, &info_cb);
585
586     /*
587      * Half-duplex handshake loop.
588      * Client and server speak to each other synchronously in the same process.
589      * We use non-blocking BIOs, so whenever one peer blocks for read, it
590      * returns PEER_RETRY to indicate that it's the other peer's turn to write.
591      * The handshake succeeds once both peers have succeeded. If one peer
592      * errors out, we also let the other peer retry (and presumably fail).
593      */
594     for(;;) {
595         if (client_turn) {
596             client_status = do_handshake_step(client, shutdown);
597             status = handshake_status(client_status, server_status,
598                                       1 /* client went last */);
599         } else {
600             server_status = do_handshake_step(server, shutdown);
601             status = handshake_status(server_status, client_status,
602                                       0 /* server went last */);
603         }
604
605         switch (status) {
606         case HANDSHAKE_SUCCESS:
607             if (shutdown) {
608                 ret->result = SSL_TEST_SUCCESS;
609                 goto err;
610             } else {
611                 client_status = server_status = PEER_RETRY;
612                 shutdown = 1;
613                 client_turn = 1;
614                 break;
615             }
616         case CLIENT_ERROR:
617             ret->result = SSL_TEST_CLIENT_FAIL;
618             goto err;
619         case SERVER_ERROR:
620             ret->result = SSL_TEST_SERVER_FAIL;
621             goto err;
622         case INTERNAL_ERROR:
623             ret->result = SSL_TEST_INTERNAL_ERROR;
624             goto err;
625         case HANDSHAKE_RETRY:
626             /* Continue. */
627             client_turn ^= 1;
628             break;
629         }
630     }
631  err:
632     ret->server_alert_sent = server_ex_data.alert_sent;
633     ret->server_alert_received = client_ex_data.alert_received;
634     ret->client_alert_sent = client_ex_data.alert_sent;
635     ret->client_alert_received = server_ex_data.alert_received;
636     ret->server_protocol = SSL_version(server);
637     ret->client_protocol = SSL_version(client);
638     ret->servername = server_ex_data.servername;
639     if ((sess = SSL_get0_session(client)) != NULL)
640         SSL_SESSION_get0_ticket(sess, &tick, &tick_len);
641     if (tick == NULL || tick_len == 0)
642         ret->session_ticket = SSL_TEST_SESSION_TICKET_NO;
643     else
644         ret->session_ticket = SSL_TEST_SESSION_TICKET_YES;
645     ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;
646
647     SSL_get0_next_proto_negotiated(client, &proto, &proto_len);
648     ret->client_npn_negotiated = dup_str(proto, proto_len);
649
650     SSL_get0_next_proto_negotiated(server, &proto, &proto_len);
651     ret->server_npn_negotiated = dup_str(proto, proto_len);
652
653     SSL_get0_alpn_selected(client, &proto, &proto_len);
654     ret->client_alpn_negotiated = dup_str(proto, proto_len);
655
656     SSL_get0_alpn_selected(server, &proto, &proto_len);
657     ret->server_alpn_negotiated = dup_str(proto, proto_len);
658
659     ret->client_resumed = SSL_session_reused(client);
660     ret->server_resumed = SSL_session_reused(server);
661
662     if (session_out != NULL)
663         *session_out = SSL_get1_session(client);
664
665     ctx_data_free_data(&server_ctx_data);
666     ctx_data_free_data(&server2_ctx_data);
667     ctx_data_free_data(&client_ctx_data);
668
669     SSL_free(server);
670     SSL_free(client);
671     return ret;
672 }
673
674 HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
675                                SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx,
676                                const SSL_TEST_CTX *test_ctx)
677 {
678     HANDSHAKE_RESULT *result;
679     SSL_SESSION *session = NULL;
680
681     result = do_handshake_internal(server_ctx, server2_ctx, client_ctx,
682                                    test_ctx, NULL, &session);
683     if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_SIMPLE)
684         goto end;
685
686     OPENSSL_assert(test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME);
687
688     if (result->result != SSL_TEST_SUCCESS) {
689         result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED;
690         return result;
691     }
692
693     HANDSHAKE_RESULT_free(result);
694     /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */
695     result = do_handshake_internal(resume_server_ctx, NULL, client_ctx, test_ctx,
696                                    session, NULL);
697  end:
698     SSL_SESSION_free(session);
699     return result;
700 }