f1df6a30daebe3273aab67d3113f32ed492fe15b
[openssl.git] / ssl / statem.c
1 /* ssl/statem.c */
2 /*
3  * Written by Matt Caswell for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2015 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@openssl.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <openssl/rand.h>
60 #include "ssl_locl.h"
61
62 /*
63  * This file implements the SSL/TLS/DTLS state machines.
64  *
65  * There are two primary state machines:
66  *
67  * 1) Message flow state machine
68  * 2) Handshake state machine
69  *
70  * The Message flow state machine controls the reading and sending of messages
71  * including handling of non-blocking IO events, flushing of the underlying
72  * write BIO, handling unexpected messages, etc. It is itself broken into two
73  * separate sub-state machines which control reading and writing respectively.
74  *
75  * The Handshake state machine keeps track of the current SSL/TLS handshake
76  * state. Transitions of the handshake state are the result of events that
77  * occur within the Message flow state machine.
78  *
79  * Overall it looks like this:
80  *
81  * ---------------------------------------------            -------------------
82  * |                                           |            |                 |
83  * | Message flow state machine                |            |                 |
84  * |                                           |            |                 |
85  * | -------------------- -------------------- | Transition | Handshake state |
86  * | | MSG_FLOW_READING    | | MSG_FLOW_WRITING    | | Event      | machine         |
87  * | | sub-state        | | sub-state        | |----------->|                 |
88  * | | machine for      | | machine for      | |            |                 |
89  * | | reading messages | | writing messages | |            |                 |
90  * | -------------------- -------------------- |            |                 |
91  * |                                           |            |                 |
92  * ---------------------------------------------            -------------------
93  *
94  */
95
96 /* Sub state machine return values */
97 enum SUB_STATE_RETURN {
98     /* Something bad happened or NBIO */
99     SUB_STATE_ERROR,
100     /* Sub state finished go to the next sub state */
101     SUB_STATE_FINISHED,
102     /* Sub state finished and handshake was completed */
103     SUB_STATE_END_HANDSHAKE
104 };
105
106 static int state_machine(SSL *s, int server);
107 static void init_read_state_machine(SSL *s);
108 static enum SUB_STATE_RETURN read_state_machine(SSL *s);
109 static void init_write_state_machine(SSL *s);
110 static enum SUB_STATE_RETURN write_state_machine(SSL *s);
111 static inline int cert_req_allowed(SSL *s);
112 static inline int key_exchange_skip_allowed(SSL *s);
113 static int client_read_transition(SSL *s, int mt);
114 static enum WRITE_TRAN client_write_transition(SSL *s);
115 static enum WORK_STATE client_pre_work(SSL *s, enum WORK_STATE wst);
116 static enum WORK_STATE client_post_work(SSL *s, enum WORK_STATE wst);
117 static int client_construct_message(SSL *s);
118 static unsigned long client_max_message_size(SSL *s);
119 static enum MSG_PROCESS_RETURN client_process_message(SSL *s,
120                                                       unsigned long len);
121 static enum WORK_STATE client_post_process_message(SSL *s, enum WORK_STATE wst);
122
123 /*
124  * Clear the state machine state and reset back to MSG_FLOW_UNINITED
125  */
126 void statem_clear(SSL *s)
127 {
128     s->statem.state = MSG_FLOW_UNINITED;
129 }
130
131 /*
132  * Set the state machine up ready for a renegotiation handshake
133  */
134 void statem_set_renegotiate(SSL *s)
135 {
136     s->statem.state = MSG_FLOW_RENEGOTIATE;
137 }
138
139 /*
140  * Put the state machine into an error state. This is a permanent error for
141  * the current connection.
142  */
143 void statem_set_error(SSL *s)
144 {
145     s->statem.state = MSG_FLOW_ERROR;
146     /* TODO: This is temporary - remove me */
147     s->state = SSL_ST_ERR;
148 }
149
150 int ssl3_connect(SSL *s) {
151     return state_machine(s, 0);
152 }
153
154 /*
155  * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
156  * MSG_FLOW_RENEGOTIATE state and finish in MSG_FLOW_FINISHED. Valid states and
157  * transitions are as follows:
158  *
159  * MSG_FLOW_UNINITED     MSG_FLOW_RENEGOTIATE
160  *        |                       |
161  *        +-----------------------+
162  *        v
163  * MSG_FLOW_WRITING <---> MSG_FLOW_READING
164  *        |
165  *        V
166  * MSG_FLOW_FINISHED
167  *        |
168  *        V
169  *    [SUCCESS]
170  *
171  * We may exit at any point due to an error or NBIO event. If an NBIO event
172  * occurs then we restart at the point we left off when we are recalled.
173  * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
174  *
175  * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
176  * into that state at any point in the event that an irrecoverable error occurs.
177  *
178  * Valid return values are:
179  *   1: Success
180  * <=0: NBIO or error
181  */
182 static int state_machine(SSL *s, int server) {
183     BUF_MEM *buf = NULL;
184     unsigned long Time = (unsigned long)time(NULL);
185     void (*cb) (const SSL *ssl, int type, int val) = NULL;
186     STATEM *st = &s->statem;
187     int ret = -1;
188     int ssret;
189
190     if (st->state == MSG_FLOW_ERROR) {
191         /* Shouldn't have been called if we're already in the error state */
192         return -1;
193     }
194
195     RAND_add(&Time, sizeof(Time), 0);
196     ERR_clear_error();
197     clear_sys_error();
198
199     if (s->info_callback != NULL)
200         cb = s->info_callback;
201     else if (s->ctx->info_callback != NULL)
202         cb = s->ctx->info_callback;
203
204     s->in_handshake++;
205     if (!SSL_in_init(s) || SSL_in_before(s)) {
206         if (!SSL_clear(s))
207             return -1;
208     }
209
210 #ifndef OPENSSL_NO_HEARTBEATS
211     /*
212      * If we're awaiting a HeartbeatResponse, pretend we already got and
213      * don't await it anymore, because Heartbeats don't make sense during
214      * handshakes anyway.
215      */
216     if (s->tlsext_hb_pending) {
217         if (SSL_IS_DTLS(s))
218             dtls1_stop_timer(s);
219         s->tlsext_hb_pending = 0;
220         s->tlsext_hb_seq++;
221     }
222 #endif
223
224     /* Initialise state machine */
225
226     if (st->state == MSG_FLOW_RENEGOTIATE) {
227         s->renegotiate = 1;
228         if (!server)
229             s->ctx->stats.sess_connect_renegotiate++;
230     }
231
232     if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE) {
233         /* TODO: Temporary - fix this */
234         if (server)
235             s->state = SSL_ST_ACCEPT;
236         else
237             s->state = SSL_ST_CONNECT;
238
239         if (st->state == MSG_FLOW_UNINITED) {
240             st->hand_state = TLS_ST_BEFORE;
241         }
242
243         s->server = server;
244         if (cb != NULL)
245             cb(s, SSL_CB_HANDSHAKE_START, 1);
246
247         if (SSL_IS_DTLS(s)) {
248             if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
249                     (server
250                     || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
251                 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
252                 goto end;
253             }
254         } else {
255             if ((s->version >> 8) != SSL3_VERSION_MAJOR
256                     && s->version != TLS_ANY_VERSION) {
257                 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
258                 goto end;
259             }
260         }
261
262         if (s->version != TLS_ANY_VERSION &&
263                 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
264             SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
265             goto end;
266         }
267
268         if (server)
269             s->type = SSL_ST_ACCEPT;
270         else
271             s->type = SSL_ST_CONNECT;
272
273         if (s->init_buf == NULL) {
274             if ((buf = BUF_MEM_new()) == NULL) {
275                 goto end;
276             }
277             if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
278                 goto end;
279             }
280             s->init_buf = buf;
281             buf = NULL;
282         }
283
284         if (!ssl3_setup_buffers(s)) {
285             goto end;
286         }
287         s->init_num = 0;
288
289         /*
290          * Should have been reset by tls_process_finished, too.
291          */
292         s->s3->change_cipher_spec = 0;
293
294         if (!server || st->state != MSG_FLOW_RENEGOTIATE) {
295                 /*
296                  * Ok, we now need to push on a buffering BIO ...but not with
297                  * SCTP
298                  */
299 #ifndef OPENSSL_NO_SCTP
300                 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
301 #endif
302                     if (!ssl_init_wbio_buffer(s, server ? 1 : 0)) {
303                         goto end;
304                     }
305
306             ssl3_init_finished_mac(s);
307         }
308
309         if (server) {
310             if (st->state != MSG_FLOW_RENEGOTIATE) {
311                 s->ctx->stats.sess_accept++;
312             } else if (!s->s3->send_connection_binding &&
313                        !(s->options &
314                          SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
315                 /*
316                  * Server attempting to renegotiate with client that doesn't
317                  * support secure renegotiation.
318                  */
319                 SSLerr(SSL_F_STATE_MACHINE,
320                        SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
321                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
322                 statem_set_error(s);
323                 goto end;
324             } else {
325                 /*
326                  * s->state == SSL_ST_RENEGOTIATE, we will just send a
327                  * HelloRequest
328                  */
329                 s->ctx->stats.sess_accept_renegotiate++;
330             }
331         } else {
332             s->ctx->stats.sess_connect++;
333
334             /* mark client_random uninitialized */
335             memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
336             s->hit = 0;
337
338             s->s3->tmp.cert_request = 0;
339
340             if (SSL_IS_DTLS(s)) {
341                 st->use_timer = 1;
342             }
343         }
344
345         st->state = MSG_FLOW_WRITING;
346         init_write_state_machine(s);
347         st->read_state_first_init = 1;
348     }
349
350     while(st->state != MSG_FLOW_FINISHED) {
351         if(st->state == MSG_FLOW_READING) {
352             ssret = read_state_machine(s);
353             if (ssret == SUB_STATE_FINISHED) {
354                 st->state = MSG_FLOW_WRITING;
355                 init_write_state_machine(s);
356             } else {
357                 /* NBIO or error */
358                 goto end;
359             }
360         } else if (st->state == MSG_FLOW_WRITING) {
361             ssret = write_state_machine(s);
362             if (ssret == SUB_STATE_FINISHED) {
363                 st->state = MSG_FLOW_READING;
364                 init_read_state_machine(s);
365             } else if (ssret == SUB_STATE_END_HANDSHAKE) {
366                 st->state = MSG_FLOW_FINISHED;
367             } else {
368                 /* NBIO or error */
369                 goto end;
370             }
371         } else {
372             /* Error */
373             statem_set_error(s);
374             goto end;
375         }
376     }
377
378     st->state = MSG_FLOW_UNINITED;
379     ret = 1;
380
381  end:
382     s->in_handshake--;
383     BUF_MEM_free(buf);
384     if (cb != NULL) {
385         if (server)
386             cb(s, SSL_CB_ACCEPT_EXIT, ret);
387         else
388             cb(s, SSL_CB_CONNECT_EXIT, ret);
389     }
390     return ret;
391 }
392
393 /*
394  * Initialise the MSG_FLOW_READING sub-state machine
395  */
396 static void init_read_state_machine(SSL *s)
397 {
398     STATEM *st = &s->statem;
399
400     st->read_state = READ_STATE_HEADER;
401 }
402
403 /*
404  * This function implements the sub-state machine when the message flow is in
405  * MSG_FLOW_READING. The valid sub-states and transitions are:
406  *
407  * READ_STATE_HEADER <--+<-------------+
408  *        |             |              |
409  *        v             |              |
410  * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
411  *        |                            |
412  *        +----------------------------+
413  *        v
414  * [SUB_STATE_FINISHED]
415  *
416  * READ_STATE_HEADER has the responsibility for reading in the message header
417  * and transitioning the state of the handshake state machine.
418  *
419  * READ_STATE_BODY reads in the rest of the message and then subsequently
420  * processes it.
421  *
422  * READ_STATE_POST_PROCESS is an optional step that may occur if some post
423  * processing activity performed on the message may block.
424  *
425  * Any of the above states could result in an NBIO event occuring in which case
426  * control returns to the calling application. When this function is recalled we
427  * will resume in the same state where we left off.
428  */
429 static enum SUB_STATE_RETURN read_state_machine(SSL *s) {
430     STATEM *st = &s->statem;
431     int ret, mt;
432     unsigned long len;
433     int (*transition)(SSL *s, int mt);
434     enum MSG_PROCESS_RETURN (*process_message)(SSL *s, unsigned long n);
435     enum WORK_STATE (*post_process_message)(SSL *s, enum WORK_STATE wst);
436     unsigned long (*max_message_size)(SSL *s);
437     void (*cb) (const SSL *ssl, int type, int val) = NULL;
438
439     if (s->info_callback != NULL)
440         cb = s->info_callback;
441     else if (s->ctx->info_callback != NULL)
442         cb = s->ctx->info_callback;
443
444     if(s->server) {
445         /* TODO: Fill these in later when we've implemented them */
446         transition = NULL;
447         process_message = NULL;
448         post_process_message = NULL;
449         max_message_size = NULL;
450     } else {
451         transition = client_read_transition;
452         process_message = client_process_message;
453         max_message_size = client_max_message_size;
454         post_process_message = client_post_process_message;
455     }
456
457     if (st->read_state_first_init) {
458         s->first_packet = 1;
459         st->read_state_first_init = 0;
460     }
461
462     while(1) {
463         switch(st->read_state) {
464         case READ_STATE_HEADER:
465             s->init_num = 0;
466             /* Get the state the peer wants to move to */
467             if (SSL_IS_DTLS(s)) {
468                 /*
469                  * In DTLS we get the whole message in one go - header and body
470                  */
471                 ret = dtls_get_message(s, &mt, &len);
472             } else {
473                 ret = tls_get_message_header(s, &mt);
474             }
475
476             if (ret == 0) {
477                 /* Could be non-blocking IO */
478                 return SUB_STATE_ERROR;
479             }
480
481             if (cb != NULL) {
482                 /* Notify callback of an impending state change */
483                 if (s->server)
484                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
485                 else
486                     cb(s, SSL_CB_CONNECT_LOOP, 1);
487             }
488             /*
489              * Validate that we are allowed to move to the new state and move
490              * to that state if so
491              */
492             if(!transition(s, mt)) {
493                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
494                 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_UNEXPECTED_MESSAGE);
495                 return SUB_STATE_ERROR;
496             }
497
498             if (s->s3->tmp.message_size > max_message_size(s)) {
499                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
500                 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
501                 return SUB_STATE_ERROR;
502             }
503
504             st->read_state = READ_STATE_BODY;
505             /* Fall through */
506
507         case READ_STATE_BODY:
508             if (!SSL_IS_DTLS(s)) {
509                 /* We already got this above for DTLS */
510                 ret = tls_get_message_body(s, &len);
511                 if (ret == 0) {
512                     /* Could be non-blocking IO */
513                     return SUB_STATE_ERROR;
514                 }
515             }
516
517             s->first_packet = 0;
518             ret = process_message(s, len);
519             if (ret == MSG_PROCESS_ERROR) {
520                 return SUB_STATE_ERROR;
521             }
522
523             if (ret == MSG_PROCESS_FINISHED_READING) {
524                 if (SSL_IS_DTLS(s)) {
525                     dtls1_stop_timer(s);
526                 }
527                 return SUB_STATE_FINISHED;
528             }
529
530             if (ret == MSG_PROCESS_CONTINUE_PROCESSING) {
531                 st->read_state = READ_STATE_POST_PROCESS;
532                 st->read_state_work = WORK_MORE_A;
533             } else {
534                 st->read_state = READ_STATE_HEADER;
535             }
536             break;
537
538         case READ_STATE_POST_PROCESS:
539             st->read_state_work = post_process_message(s, st->read_state_work);
540             switch(st->read_state_work) {
541             default:
542                 return SUB_STATE_ERROR;
543
544             case WORK_FINISHED_CONTINUE:
545                 st->read_state = READ_STATE_HEADER;
546                 break;
547
548             case WORK_FINISHED_STOP:
549                 if (SSL_IS_DTLS(s)) {
550                     dtls1_stop_timer(s);
551                 }
552                 return SUB_STATE_FINISHED;
553             }
554             break;
555
556         default:
557             /* Shouldn't happen */
558             ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
559             SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
560             statem_set_error(s);
561             return SUB_STATE_ERROR;
562         }
563     }
564 }
565
566 /*
567  * Send a previously constructed message to the peer.
568  */
569 static int statem_do_write(SSL *s)
570 {
571     STATEM *st = &s->statem;
572
573     if (st->hand_state == TLS_ST_CW_CHANGE
574             || st->hand_state == TLS_ST_SW_CHANGE) {
575         if (SSL_IS_DTLS(s))
576             return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
577         else
578             return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
579     } else {
580         return ssl_do_write(s);
581     }
582 }
583
584 /*
585  * Initialise the MSG_FLOW_WRITING sub-state machine
586  */
587 static void init_write_state_machine(SSL *s)
588 {
589     STATEM *st = &s->statem;
590
591     st->write_state = WRITE_STATE_TRANSITION;
592 }
593
594 /*
595  * This function implements the sub-state machine when the message flow is in
596  * MSG_FLOW_WRITING. The valid sub-states and transitions are:
597  *
598  * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
599  * |             |
600  * |             v
601  * |      WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
602  * |             |
603  * |             v
604  * |       WRITE_STATE_SEND
605  * |             |
606  * |             v
607  * |     WRITE_STATE_POST_WORK
608  * |             |
609  * +-------------+
610  *
611  * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
612
613  * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
614  * sending of the message. This could result in an NBIO event occuring in
615  * which case control returns to the calling application. When this function
616  * is recalled we will resume in the same state where we left off.
617  *
618  * WRITE_STATE_SEND sends the message and performs any work to be done after
619  * sending.
620  *
621  * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
622  * message has been completed. As for WRITE_STATE_PRE_WORK this could also
623  * result in an NBIO event.
624  */
625 static enum SUB_STATE_RETURN write_state_machine(SSL *s)
626 {
627     STATEM *st = &s->statem;
628     int ret;
629     enum WRITE_TRAN (*transition)(SSL *s);
630     enum WORK_STATE (*pre_work)(SSL *s, enum WORK_STATE wst);
631     enum WORK_STATE (*post_work)(SSL *s, enum WORK_STATE wst);
632     int (*construct_message)(SSL *s);
633     void (*cb) (const SSL *ssl, int type, int val) = NULL;
634
635     if (s->info_callback != NULL)
636         cb = s->info_callback;
637     else if (s->ctx->info_callback != NULL)
638         cb = s->ctx->info_callback;
639
640     if(s->server) {
641         /* TODO: Fill these in later when we've implemented them */
642         transition = NULL;
643         pre_work = NULL;
644         post_work = NULL;
645         construct_message = NULL;
646     } else {
647         transition = client_write_transition;
648         pre_work = client_pre_work;
649         post_work = client_post_work;
650         construct_message = client_construct_message;
651     }
652
653     while(1) {
654         switch(st->write_state) {
655         case WRITE_STATE_TRANSITION:
656             if (cb != NULL) {
657                 /* Notify callback of an impending state change */
658                 if (s->server)
659                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
660                 else
661                     cb(s, SSL_CB_CONNECT_LOOP, 1);
662             }
663             switch(transition(s)) {
664             case WRITE_TRAN_CONTINUE:
665                 st->write_state = WRITE_STATE_PRE_WORK;
666                 st->write_state_work = WORK_MORE_A;
667                 break;
668
669             case WRITE_TRAN_FINISHED:
670                 return SUB_STATE_FINISHED;
671                 break;
672
673             default:
674                 return SUB_STATE_ERROR;
675             }
676             break;
677
678         case WRITE_STATE_PRE_WORK:
679             switch(st->write_state_work = pre_work(s, st->write_state_work)) {
680             default:
681                 return SUB_STATE_ERROR;
682
683             case WORK_FINISHED_CONTINUE:
684                 st->write_state = WRITE_STATE_SEND;
685                 break;
686
687             case WORK_FINISHED_STOP:
688                 return SUB_STATE_END_HANDSHAKE;
689             }
690             if(construct_message(s) == 0)
691                 return SUB_STATE_ERROR;
692
693             /* Fall through */
694
695         case WRITE_STATE_SEND:
696             if (SSL_IS_DTLS(s) && st->use_timer) {
697                 dtls1_start_timer(s);
698             }
699             ret = statem_do_write(s);
700             if (ret <= 0) {
701                 return SUB_STATE_ERROR;
702             }
703             st->write_state = WRITE_STATE_POST_WORK;
704             st->write_state_work = WORK_MORE_A;
705             /* Fall through */
706
707         case WRITE_STATE_POST_WORK:
708             switch(st->write_state_work = post_work(s, st->write_state_work)) {
709             default:
710                 return SUB_STATE_ERROR;
711
712             case WORK_FINISHED_CONTINUE:
713                 st->write_state = WRITE_STATE_TRANSITION;
714                 break;
715
716             case WORK_FINISHED_STOP:
717                 return SUB_STATE_END_HANDSHAKE;
718             }
719             break;
720
721         default:
722             return SUB_STATE_ERROR;
723         }
724     }
725 }
726
727 /*
728  * Flush the write BIO
729  */
730 static int statem_flush(SSL *s)
731 {
732     s->rwstate = SSL_WRITING;
733     if (BIO_flush(s->wbio) <= 0) {
734         return 0;
735     }
736     s->rwstate = SSL_NOTHING;
737
738     return 1;
739 }
740
741 /*
742  * Called by the record layer to determine whether application data is
743  * allowed to be sent in the current handshake state or not.
744  *
745  * Return values are:
746  *   1: Yes (application data allowed)
747  *   0: No (application data not allowed)
748  */
749 int statem_app_data_allowed(SSL *s)
750 {
751     STATEM *st = &s->statem;
752
753     if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
754         return 0;
755
756     if (!s->server) {
757         if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE)
758             return 0;
759
760         if(st->hand_state == TLS_ST_CW_CLNT_HELLO)
761             return 1;
762
763         return 0;
764     }
765
766     /*
767      * This is the old check for code still using the old state machine. This
768      * will be removed by later commits
769      */
770     if (((s->state & SSL_ST_CONNECT) && SSL_IS_DTLS(s) &&
771           (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
772           (s->state <= SSL3_ST_CR_SRVR_HELLO_A)) ||
773          ((s->state & SSL_ST_ACCEPT) &&
774                (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
775                (s->state >= SSL3_ST_SR_CLNT_HELLO_A))
776         )
777         return 1;
778
779     return 0;
780 }
781
782
783 #ifndef OPENSSL_NO_SCTP
784 /*
785  * Set flag used by SCTP to determine whether we are in the read sock state
786  */
787 void statem_set_sctp_read_sock(SSL *s, int read_sock)
788 {
789     s->statem.in_sctp_read_sock = read_sock;
790 }
791
792 /*
793  * Called by the record layer to determine whether we are in the read sock
794  * state or not.
795  *
796  * Return values are:
797  *   1: Yes (we are in the read sock state)
798  *   0: No (we are not in the read sock state)
799  */
800 int statem_in_sctp_read_sock(SSL *s)
801 {
802     return s->statem.in_sctp_read_sock;
803 }
804 #endif
805
806 /*
807  * Is a CertificateRequest message allowed at the moment or not?
808  *
809  *  Return values are:
810  *  1: Yes
811  *  0: No
812  */
813 static inline int cert_req_allowed(SSL *s)
814 {
815     /* TLS does not like anon-DH with client cert */
816     if (s->version > SSL3_VERSION
817             && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
818         return 0;
819
820     return 1;
821 }
822
823 /*
824  * Are we allowed to skip the ServerKeyExchange message?
825  *
826  *  Return values are:
827  *  1: Yes
828  *  0: No
829  */
830 static inline int key_exchange_skip_allowed(SSL *s)
831 {
832     long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
833
834     /*
835      * Can't skip server key exchange if this is an ephemeral
836      * ciphersuite.
837      */
838     if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
839         return 0;
840     }
841
842     return 1;
843 }
844
845 /*
846  * client_read_transition() encapsulates the logic for the allowed handshake
847  * state transitions when the client is reading messages from the server. The
848  * message type that the server has sent is provided in |mt|. The current state
849  * is in |s->statem.hand_state|.
850  *
851  *  Return values are:
852  *  1: Success (transition allowed)
853  *  0: Error (transition not allowed)
854  */
855 static int client_read_transition(SSL *s, int mt)
856 {
857     STATEM *st = &s->statem;
858
859     switch(st->hand_state) {
860     case TLS_ST_CW_CLNT_HELLO:
861         if (mt == SSL3_MT_SERVER_HELLO) {
862             st->hand_state = TLS_ST_CR_SRVR_HELLO;
863             return 1;
864         }
865
866         if (SSL_IS_DTLS(s)) {
867             if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
868                 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
869                 return 1;
870             }
871         }
872         break;
873
874     case TLS_ST_CR_SRVR_HELLO:
875         if (s->hit) {
876             if (s->tlsext_ticket_expected) {
877                 if (mt == SSL3_MT_NEWSESSION_TICKET) {
878                     st->hand_state = TLS_ST_CR_SESSION_TICKET;
879                     return 1;
880                 }
881             } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
882                 st->hand_state = TLS_ST_CR_CHANGE;
883                 return 1;
884             }
885         } else {
886             if (!(s->s3->tmp.new_cipher->algorithm_auth
887                         & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
888                 if (mt == SSL3_MT_CERTIFICATE) {
889                     st->hand_state = TLS_ST_CR_CERT;
890                     return 1;
891                 }
892             } else {
893                 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
894                     st->hand_state = TLS_ST_CR_KEY_EXCH;
895                     return 1;
896                 } else if (key_exchange_skip_allowed(s)) {
897                     if (mt == SSL3_MT_CERTIFICATE_REQUEST
898                             && cert_req_allowed(s)) {
899                         st->hand_state = TLS_ST_CR_CERT_REQ;
900                         return 1;
901                     } else if (mt == SSL3_MT_SERVER_DONE) {
902                         st->hand_state = TLS_ST_CR_SRVR_DONE;
903                         return 1;
904                     }
905                 }
906             }
907         }
908         break;
909
910     case TLS_ST_CR_CERT:
911         if (s->tlsext_status_expected) {
912             if (mt == SSL3_MT_CERTIFICATE_STATUS) {
913                 st->hand_state = TLS_ST_CR_CERT_STATUS;
914                 return 1;
915             }
916         } else {
917             if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
918                 st->hand_state = TLS_ST_CR_KEY_EXCH;
919                 return 1;
920             } else if (key_exchange_skip_allowed(s)) {
921                 if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
922                     st->hand_state = TLS_ST_CR_CERT_REQ;
923                     return 1;
924                 } else if (mt == SSL3_MT_SERVER_DONE) {
925                     st->hand_state = TLS_ST_CR_SRVR_DONE;
926                     return 1;
927                 }
928             }
929         }
930         break;
931
932     case TLS_ST_CR_CERT_STATUS:
933         if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
934             st->hand_state = TLS_ST_CR_KEY_EXCH;
935             return 1;
936         } else if (key_exchange_skip_allowed(s)) {
937             if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
938                 st->hand_state = TLS_ST_CR_CERT_REQ;
939                 return 1;
940             } else if (mt == SSL3_MT_SERVER_DONE) {
941                 st->hand_state = TLS_ST_CR_SRVR_DONE;
942                 return 1;
943             }
944         }
945         break;
946
947     case TLS_ST_CR_KEY_EXCH:
948         if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
949             st->hand_state = TLS_ST_CR_CERT_REQ;
950             return 1;
951         } else if (mt == SSL3_MT_SERVER_DONE) {
952             st->hand_state = TLS_ST_CR_SRVR_DONE;
953             return 1;
954         }
955         break;
956
957     case TLS_ST_CR_CERT_REQ:
958         if (mt == SSL3_MT_SERVER_DONE) {
959             st->hand_state = TLS_ST_CR_SRVR_DONE;
960             return 1;
961         }
962         break;
963
964     case TLS_ST_CW_FINISHED:
965         if (mt == SSL3_MT_NEWSESSION_TICKET && s->tlsext_ticket_expected) {
966             st->hand_state = TLS_ST_CR_SESSION_TICKET;
967             return 1;
968         } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
969             st->hand_state = TLS_ST_CR_CHANGE;
970             return 1;
971         }
972         break;
973
974     case TLS_ST_CR_SESSION_TICKET:
975         if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
976             st->hand_state = TLS_ST_CR_CHANGE;
977             return 1;
978         }
979         break;
980
981     case TLS_ST_CR_CHANGE:
982         if (mt == SSL3_MT_FINISHED) {
983             st->hand_state = TLS_ST_CR_FINISHED;
984             return 1;
985         }
986         break;
987
988     default:
989         break;
990     }
991
992     /* No valid transition found */
993     return 0;
994 }
995
996 /*
997  * client_write_transition() works out what handshake state to move to next
998  * when the client is writing messages to be sent to the server.
999  */
1000 static enum WRITE_TRAN client_write_transition(SSL *s)
1001 {
1002     STATEM *st = &s->statem;
1003
1004     switch(st->hand_state) {
1005         case TLS_ST_OK:
1006             /* Renegotiation - fall through */
1007         case TLS_ST_BEFORE:
1008             st->hand_state = TLS_ST_CW_CLNT_HELLO;
1009             return WRITE_TRAN_CONTINUE;
1010
1011         case TLS_ST_CW_CLNT_HELLO:
1012             /*
1013              * No transition at the end of writing because we don't know what
1014              * we will be sent
1015              */
1016             return WRITE_TRAN_FINISHED;
1017
1018         case TLS_ST_CR_SRVR_DONE:
1019             if (s->s3->tmp.cert_req)
1020                 st->hand_state = TLS_ST_CW_CERT;
1021             else
1022                 st->hand_state = TLS_ST_CW_KEY_EXCH;
1023             return WRITE_TRAN_CONTINUE;
1024
1025         case TLS_ST_CW_CERT:
1026             st->hand_state = TLS_ST_CW_KEY_EXCH;
1027             return WRITE_TRAN_CONTINUE;
1028
1029         case TLS_ST_CW_KEY_EXCH:
1030             /*
1031              * For TLS, cert_req is set to 2, so a cert chain of nothing is
1032              * sent, but no verify packet is sent
1033              */
1034             /*
1035              * XXX: For now, we do not support client authentication in ECDH
1036              * cipher suites with ECDH (rather than ECDSA) certificates. We
1037              * need to skip the certificate verify message when client's
1038              * ECDH public key is sent inside the client certificate.
1039              */
1040             if (s->s3->tmp.cert_req == 1) {
1041                 st->hand_state = TLS_ST_CW_CERT_VRFY;
1042             } else {
1043                 st->hand_state = TLS_ST_CW_CHANGE;
1044             }
1045             if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
1046                 st->hand_state = TLS_ST_CW_CHANGE;
1047             }
1048             return WRITE_TRAN_CONTINUE;
1049
1050         case TLS_ST_CW_CERT_VRFY:
1051             st->hand_state = TLS_ST_CW_CHANGE;
1052             return WRITE_TRAN_CONTINUE;
1053
1054         case TLS_ST_CW_CHANGE:
1055 #if defined(OPENSSL_NO_NEXTPROTONEG)
1056             st->hand_state = TLS_ST_CW_FINISHED;
1057 #else
1058             if (s->s3->next_proto_neg_seen)
1059                 st->hand_state = TLS_ST_CW_NEXT_PROTO;
1060             else
1061                 st->hand_state = TLS_ST_CW_FINISHED;
1062 #endif
1063             return WRITE_TRAN_CONTINUE;
1064
1065 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1066         case TLS_ST_CW_NEXT_PROTO:
1067             st->hand_state = TLS_ST_CW_FINISHED;
1068             return WRITE_TRAN_CONTINUE;
1069 #endif
1070
1071         case TLS_ST_CW_FINISHED:
1072             if (s->hit) {
1073                 st->hand_state = TLS_ST_OK;
1074                 /* TODO: This needs removing */
1075                 s->state = SSL_ST_OK;
1076                 return WRITE_TRAN_CONTINUE;
1077             } else {
1078                 return WRITE_TRAN_FINISHED;
1079             }
1080
1081         case TLS_ST_CR_FINISHED:
1082             if (s->hit) {
1083                 st->hand_state = TLS_ST_CW_CHANGE;
1084                 return WRITE_TRAN_CONTINUE;
1085             } else {
1086                 st->hand_state = TLS_ST_OK;
1087                 /* TODO: This needs removing */
1088                 s->state = SSL_ST_OK;
1089                 return WRITE_TRAN_CONTINUE;
1090             }
1091
1092         default:
1093             /* Shouldn't happen */
1094             return WRITE_TRAN_ERROR;
1095     }
1096 }
1097
1098 /*
1099  * Perform any pre work that needs to be done prior to sending a message from
1100  * the client to the server.
1101  */
1102 static enum WORK_STATE client_pre_work(SSL *s, enum WORK_STATE wst)
1103 {
1104     STATEM *st = &s->statem;
1105
1106     switch(st->hand_state) {
1107     case TLS_ST_CW_CLNT_HELLO:
1108         s->shutdown = 0;
1109         break;
1110
1111     case TLS_ST_CW_CERT:
1112         return tls_prepare_client_certificate(s, wst);
1113
1114     case TLS_ST_OK:
1115         return tls_finish_handshake(s, wst);
1116
1117     default:
1118         /* No pre work to be done */
1119         break;
1120     }
1121
1122     return WORK_FINISHED_CONTINUE;
1123 }
1124
1125 /*
1126  * Perform any work that needs to be done after sending a message from the
1127  * client to the server.
1128  */
1129 static enum WORK_STATE client_post_work(SSL *s, enum WORK_STATE wst)
1130 {
1131     STATEM *st = &s->statem;
1132
1133     s->init_num = 0;
1134
1135     switch(st->hand_state) {
1136     case TLS_ST_CW_CLNT_HELLO:
1137         /* turn on buffering for the next lot of output */
1138         if (s->bbio != s->wbio)
1139             s->wbio = BIO_push(s->bbio, s->wbio);
1140         break;
1141
1142     case TLS_ST_CW_KEY_EXCH:
1143         if (tls_client_key_exchange_post_work(s) == 0)
1144             return WORK_ERROR;
1145         break;
1146
1147     case TLS_ST_CW_CHANGE:
1148         s->session->cipher = s->s3->tmp.new_cipher;
1149 #ifdef OPENSSL_NO_COMP
1150         s->session->compress_meth = 0;
1151 #else
1152         if (s->s3->tmp.new_compression == NULL)
1153             s->session->compress_meth = 0;
1154         else
1155             s->session->compress_meth = s->s3->tmp.new_compression->id;
1156 #endif
1157         if (!s->method->ssl3_enc->setup_key_block(s))
1158             return WORK_ERROR;
1159
1160         if (!s->method->ssl3_enc->change_cipher_state(s,
1161                                                       SSL3_CHANGE_CIPHER_CLIENT_WRITE))
1162             return WORK_ERROR;
1163
1164         if (SSL_IS_DTLS(s)) {
1165 #ifndef OPENSSL_NO_SCTP
1166             if (s->hit) {
1167                 /*
1168                  * Change to new shared key of SCTP-Auth, will be ignored if
1169                  * no SCTP used.
1170                  */
1171                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
1172                          0, NULL);
1173             }
1174 #endif
1175
1176             dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
1177         }
1178         break;
1179
1180     case TLS_ST_CW_FINISHED:
1181 #ifndef OPENSSL_NO_SCTP
1182         if (SSL_IS_DTLS(s) && s->hit == 0) {
1183             /*
1184              * Change to new shared key of SCTP-Auth, will be ignored if
1185              * no SCTP used.
1186              */
1187             BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
1188                      0, NULL);
1189         }
1190 #endif
1191         if (statem_flush(s) != 1)
1192             return WORK_MORE_B;
1193
1194         if (s->hit && tls_finish_handshake(s, WORK_MORE_A) != 1)
1195                 return WORK_ERROR;
1196         break;
1197
1198     default:
1199         /* No post work to be done */
1200         break;
1201     }
1202
1203     return WORK_FINISHED_CONTINUE;
1204 }
1205
1206 /*
1207  * Construct a message to be sent from the client to the server.
1208  *
1209  * Valid return values are:
1210  *   1: Success
1211  *   0: Error
1212  */
1213 static int client_construct_message(SSL *s)
1214 {
1215     STATEM *st = &s->statem;
1216
1217     switch(st->hand_state) {
1218     case TLS_ST_CW_CLNT_HELLO:
1219         return tls_construct_client_hello(s);
1220
1221     case TLS_ST_CW_CERT:
1222         return tls_construct_client_certificate(s);
1223
1224     case TLS_ST_CW_KEY_EXCH:
1225         return tls_construct_client_key_exchange(s);
1226
1227     case TLS_ST_CW_CERT_VRFY:
1228         return tls_construct_client_verify(s);
1229
1230     case TLS_ST_CW_CHANGE:
1231         return tls_construct_change_cipher_spec(s);
1232
1233 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1234     case TLS_ST_CW_NEXT_PROTO:
1235         return tls_construct_next_proto(s);
1236 #endif
1237     case TLS_ST_CW_FINISHED:
1238         return tls_construct_finished(s,
1239                                       s->method->
1240                                       ssl3_enc->client_finished_label,
1241                                       s->method->
1242                                       ssl3_enc->client_finished_label_len);
1243
1244     default:
1245         /* Shouldn't happen */
1246         break;
1247     }
1248
1249     return 0;
1250 }
1251
1252 /* The spec allows for a longer length than this, but we limit it */
1253 #define SERVER_HELLO_MAX_LENGTH         20000
1254 #define SERVER_KEY_EXCH_MAX_LENGTH      102400
1255 #define SERVER_HELLO_DONE_MAX_LENGTH    0
1256 #define CCS_MAX_LENGTH                  1
1257 /* Max should actually be 36 but we are generous */
1258 #define FINISHED_MAX_LENGTH             64
1259
1260 /*
1261  * Returns the maximum allowed length for the current message that we are
1262  * reading. Excludes the message header.
1263  */
1264 static unsigned long client_max_message_size(SSL *s)
1265 {
1266     STATEM *st = &s->statem;
1267
1268     switch(st->hand_state) {
1269         case TLS_ST_CR_SRVR_HELLO:
1270             return SERVER_HELLO_MAX_LENGTH;
1271
1272         case TLS_ST_CR_CERT:
1273             return s->max_cert_list;
1274
1275         case TLS_ST_CR_CERT_STATUS:
1276             return SSL3_RT_MAX_PLAIN_LENGTH;
1277
1278         case TLS_ST_CR_KEY_EXCH:
1279             return SERVER_KEY_EXCH_MAX_LENGTH;
1280
1281         case TLS_ST_CR_CERT_REQ:
1282             return SSL3_RT_MAX_PLAIN_LENGTH;
1283
1284         case TLS_ST_CR_SRVR_DONE:
1285             return SERVER_HELLO_DONE_MAX_LENGTH;
1286
1287         case TLS_ST_CR_CHANGE:
1288             return CCS_MAX_LENGTH;
1289
1290         case TLS_ST_CR_SESSION_TICKET:
1291             return SSL3_RT_MAX_PLAIN_LENGTH;
1292
1293         case TLS_ST_CR_FINISHED:
1294             return FINISHED_MAX_LENGTH;
1295
1296         default:
1297             /* Shouldn't happen */
1298             break;
1299     }
1300
1301     return 0;
1302 }
1303
1304 /*
1305  * Process a message that the client has been received from the server.
1306  */
1307 static enum MSG_PROCESS_RETURN client_process_message(SSL *s, unsigned long len)
1308 {
1309     STATEM *st = &s->statem;
1310
1311     switch(st->hand_state) {
1312         case TLS_ST_CR_SRVR_HELLO:
1313             return tls_process_server_hello(s, len);
1314
1315         case TLS_ST_CR_CERT:
1316             return tls_process_server_certificate(s, len);
1317
1318         case TLS_ST_CR_CERT_STATUS:
1319             return tls_process_cert_status(s, len);
1320
1321         case TLS_ST_CR_KEY_EXCH:
1322             return tls_process_key_exchange(s, len);
1323
1324         case TLS_ST_CR_CERT_REQ:
1325             return tls_process_certificate_request(s, len);
1326
1327         case TLS_ST_CR_SRVR_DONE:
1328             return tls_process_server_done(s, len);
1329
1330         case TLS_ST_CR_CHANGE:
1331             return tls_process_change_cipher_spec(s, len);
1332
1333         case TLS_ST_CR_SESSION_TICKET:
1334             return tls_process_new_session_ticket(s, len);
1335
1336         case TLS_ST_CR_FINISHED:
1337             return tls_process_finished(s, len);
1338
1339         default:
1340             /* Shouldn't happen */
1341             break;
1342     }
1343
1344     return MSG_PROCESS_ERROR;
1345 }
1346
1347 /*
1348  * Perform any further processing required following the receipt of a message
1349  * from the server
1350  */
1351 static enum WORK_STATE client_post_process_message(SSL *s, enum WORK_STATE wst)
1352 {
1353     STATEM *st = &s->statem;
1354
1355     switch(st->hand_state) {
1356 #ifndef OPENSSL_NO_SCTP
1357     case TLS_ST_CR_SRVR_DONE:
1358         /* We only get here if we are using SCTP and we are renegotiating */
1359         if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
1360             s->s3->in_read_app_data = 2;
1361             s->rwstate = SSL_READING;
1362             BIO_clear_retry_flags(SSL_get_rbio(s));
1363             BIO_set_retry_read(SSL_get_rbio(s));
1364             statem_set_sctp_read_sock(s, 1);
1365             return WORK_MORE_A;
1366         }
1367         statem_set_sctp_read_sock(s, 0);
1368         return WORK_FINISHED_STOP;
1369 #endif
1370
1371     case TLS_ST_CR_FINISHED:
1372         if (!s->hit)
1373             return tls_finish_handshake(s, wst);
1374         else
1375             return WORK_FINISHED_STOP;
1376     default:
1377         break;
1378     }
1379
1380     /* Shouldn't happen */
1381     return WORK_ERROR;
1382 }