Add initial state machine rewrite code
[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 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
112 /*
113  * Clear the state machine state and reset back to MSG_FLOW_UNINITED
114  */
115 void statem_clear(SSL *s)
116 {
117     s->statem.state = MSG_FLOW_UNINITED;
118 }
119
120 /*
121  * Set the state machine up ready for a renegotiation handshake
122  */
123 void statem_set_renegotiate(SSL *s)
124 {
125     s->statem.state = MSG_FLOW_RENEGOTIATE;
126 }
127
128 /*
129  * Put the state machine into an error state. This is a permanent error for
130  * the current connection.
131  */
132 void statem_set_error(SSL *s)
133 {
134     s->statem.state = MSG_FLOW_ERROR;
135     /* TODO: This is temporary - remove me */
136     s->state = SSL_ST_ERR;
137 }
138
139 /*
140  * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
141  * MSG_FLOW_RENEGOTIATE state and finish in MSG_FLOW_FINISHED. Valid states and
142  * transitions are as follows:
143  *
144  * MSG_FLOW_UNINITED     MSG_FLOW_RENEGOTIATE
145  *        |                       |
146  *        +-----------------------+
147  *        v
148  * MSG_FLOW_WRITING <---> MSG_FLOW_READING
149  *        |
150  *        V
151  * MSG_FLOW_FINISHED
152  *        |
153  *        V
154  *    [SUCCESS]
155  *
156  * We may exit at any point due to an error or NBIO event. If an NBIO event
157  * occurs then we restart at the point we left off when we are recalled.
158  * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
159  *
160  * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
161  * into that state at any point in the event that an irrecoverable error occurs.
162  *
163  * Valid return values are:
164  *   1: Success
165  * <=0: NBIO or error
166  */
167 int state_machine(SSL *s, int server) {
168     BUF_MEM *buf = NULL;
169     unsigned long Time = (unsigned long)time(NULL);
170     void (*cb) (const SSL *ssl, int type, int val) = NULL;
171     STATEM *st = &s->statem;
172     int ret = -1;
173     int ssret;
174
175     if (st->state == MSG_FLOW_ERROR) {
176         /* Shouldn't have been called if we're already in the error state */
177         return -1;
178     }
179
180     RAND_add(&Time, sizeof(Time), 0);
181     ERR_clear_error();
182     clear_sys_error();
183
184     if (s->info_callback != NULL)
185         cb = s->info_callback;
186     else if (s->ctx->info_callback != NULL)
187         cb = s->ctx->info_callback;
188
189     s->in_handshake++;
190     if (!SSL_in_init(s) || SSL_in_before(s)) {
191         if (!SSL_clear(s))
192             return -1;
193     }
194
195 #ifndef OPENSSL_NO_HEARTBEATS
196     /*
197      * If we're awaiting a HeartbeatResponse, pretend we already got and
198      * don't await it anymore, because Heartbeats don't make sense during
199      * handshakes anyway.
200      */
201     if (s->tlsext_hb_pending) {
202         if (SSL_IS_DTLS(s))
203             dtls1_stop_timer(s);
204         s->tlsext_hb_pending = 0;
205         s->tlsext_hb_seq++;
206     }
207 #endif
208
209     /* Initialise state machine */
210
211     if (st->state == MSG_FLOW_RENEGOTIATE) {
212         s->renegotiate = 1;
213         if (!server)
214             s->ctx->stats.sess_connect_renegotiate++;
215     }
216
217     if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE) {
218         /* TODO: Temporary - fix this */
219         if (server)
220             s->state = SSL_ST_ACCEPT;
221         else
222             s->state = SSL_ST_CONNECT;
223
224         if (st->state == MSG_FLOW_UNINITED) {
225             st->hand_state = TLS_ST_BEFORE;
226         }
227
228         s->server = server;
229         if (cb != NULL)
230             cb(s, SSL_CB_HANDSHAKE_START, 1);
231
232         if (SSL_IS_DTLS(s)) {
233             if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
234                     (server
235                     || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
236                 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
237                 goto end;
238             }
239         } else {
240             if ((s->version >> 8) != SSL3_VERSION_MAJOR
241                     && s->version != TLS_ANY_VERSION) {
242                 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
243                 goto end;
244             }
245         }
246
247         if (s->version != TLS_ANY_VERSION &&
248                 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
249             SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
250             goto end;
251         }
252
253         if (server)
254             s->type = SSL_ST_ACCEPT;
255         else
256             s->type = SSL_ST_CONNECT;
257
258         if (s->init_buf == NULL) {
259             if ((buf = BUF_MEM_new()) == NULL) {
260                 goto end;
261             }
262             if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
263                 goto end;
264             }
265             s->init_buf = buf;
266             buf = NULL;
267         }
268
269         if (!ssl3_setup_buffers(s)) {
270             goto end;
271         }
272         s->init_num = 0;
273
274         /*
275          * Should have been reset by tls_process_finished, too.
276          */
277         s->s3->change_cipher_spec = 0;
278
279         if (!server || st->state != MSG_FLOW_RENEGOTIATE) {
280                 /*
281                  * Ok, we now need to push on a buffering BIO ...but not with
282                  * SCTP
283                  */
284 #ifndef OPENSSL_NO_SCTP
285                 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
286 #endif
287                     if (!ssl_init_wbio_buffer(s, server ? 1 : 0)) {
288                         goto end;
289                     }
290
291             ssl3_init_finished_mac(s);
292         }
293
294         if (server) {
295             if (st->state != MSG_FLOW_RENEGOTIATE) {
296                 s->ctx->stats.sess_accept++;
297             } else if (!s->s3->send_connection_binding &&
298                        !(s->options &
299                          SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
300                 /*
301                  * Server attempting to renegotiate with client that doesn't
302                  * support secure renegotiation.
303                  */
304                 SSLerr(SSL_F_STATE_MACHINE,
305                        SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
306                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
307                 statem_set_error(s);
308                 goto end;
309             } else {
310                 /*
311                  * s->state == SSL_ST_RENEGOTIATE, we will just send a
312                  * HelloRequest
313                  */
314                 s->ctx->stats.sess_accept_renegotiate++;
315             }
316         } else {
317             s->ctx->stats.sess_connect++;
318
319             /* mark client_random uninitialized */
320             memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
321             s->hit = 0;
322
323             s->s3->tmp.cert_request = 0;
324
325             if (SSL_IS_DTLS(s)) {
326                 st->use_timer = 1;
327             }
328         }
329
330         st->state = MSG_FLOW_WRITING;
331         init_write_state_machine(s);
332         st->read_state_first_init = 1;
333     }
334
335     while(st->state != MSG_FLOW_FINISHED) {
336         if(st->state == MSG_FLOW_READING) {
337             ssret = read_state_machine(s);
338             if (ssret == SUB_STATE_FINISHED) {
339                 st->state = MSG_FLOW_WRITING;
340                 init_write_state_machine(s);
341             } else {
342                 /* NBIO or error */
343                 goto end;
344             }
345         } else if (st->state == MSG_FLOW_WRITING) {
346             ssret = write_state_machine(s);
347             if (ssret == SUB_STATE_FINISHED) {
348                 st->state = MSG_FLOW_READING;
349                 init_read_state_machine(s);
350             } else if (ssret == SUB_STATE_END_HANDSHAKE) {
351                 st->state = MSG_FLOW_FINISHED;
352             } else {
353                 /* NBIO or error */
354                 goto end;
355             }
356         } else {
357             /* Error */
358             statem_set_error(s);
359             goto end;
360         }
361     }
362
363     st->state = MSG_FLOW_UNINITED;
364     ret = 1;
365
366  end:
367     s->in_handshake--;
368     BUF_MEM_free(buf);
369     if (cb != NULL) {
370         if (server)
371             cb(s, SSL_CB_ACCEPT_EXIT, ret);
372         else
373             cb(s, SSL_CB_CONNECT_EXIT, ret);
374     }
375     return ret;
376 }
377
378 /*
379  * Initialise the MSG_FLOW_READING sub-state machine
380  */
381 static void init_read_state_machine(SSL *s)
382 {
383     STATEM *st = &s->statem;
384
385     st->read_state = READ_STATE_HEADER;
386 }
387
388 /*
389  * This function implements the sub-state machine when the message flow is in
390  * MSG_FLOW_READING. The valid sub-states and transitions are:
391  *
392  * READ_STATE_HEADER <--+<-------------+
393  *        |             |              |
394  *        v             |              |
395  * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
396  *        |                            |
397  *        +----------------------------+
398  *        v
399  * [SUB_STATE_FINISHED]
400  *
401  * READ_STATE_HEADER has the responsibility for reading in the message header
402  * and transitioning the state of the handshake state machine.
403  *
404  * READ_STATE_BODY reads in the rest of the message and then subsequently
405  * processes it.
406  *
407  * READ_STATE_POST_PROCESS is an optional step that may occur if some post
408  * processing activity performed on the message may block.
409  *
410  * Any of the above states could result in an NBIO event occuring in which case
411  * control returns to the calling application. When this function is recalled we
412  * will resume in the same state where we left off.
413  */
414 static enum SUB_STATE_RETURN read_state_machine(SSL *s) {
415     STATEM *st = &s->statem;
416     int ret, mt;
417     unsigned long len;
418     int (*transition)(SSL *s, int mt);
419     enum MSG_PROCESS_RETURN (*process_message)(SSL *s, unsigned long n);
420     enum WORK_STATE (*post_process_message)(SSL *s, enum WORK_STATE wst);
421     unsigned long (*max_message_size)(SSL *s);
422     void (*cb) (const SSL *ssl, int type, int val) = NULL;
423
424     if (s->info_callback != NULL)
425         cb = s->info_callback;
426     else if (s->ctx->info_callback != NULL)
427         cb = s->ctx->info_callback;
428
429     if(s->server) {
430         /* TODO: Fill these in later when we've implemented them */
431         transition = NULL;
432         process_message = NULL;
433         post_process_message = NULL;
434         max_message_size = NULL;
435     } else {
436         /* TODO: Fill these in later when we've implemented them */
437         transition = NULL;
438         process_message = NULL;
439         post_process_message = NULL;
440         max_message_size = NULL;
441     }
442
443     if (st->read_state_first_init) {
444         s->first_packet = 1;
445         st->read_state_first_init = 0;
446     }
447
448     while(1) {
449         switch(st->read_state) {
450         case READ_STATE_HEADER:
451             s->init_num = 0;
452             /* Get the state the peer wants to move to */
453             ret = tls_get_message_header(s, &mt);
454
455             if (ret == 0) {
456                 /* Could be non-blocking IO */
457                 return SUB_STATE_ERROR;
458             }
459
460             if (cb != NULL) {
461                 /* Notify callback of an impending state change */
462                 if (s->server)
463                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
464                 else
465                     cb(s, SSL_CB_CONNECT_LOOP, 1);
466             }
467             /*
468              * Validate that we are allowed to move to the new state and move
469              * to that state if so
470              */
471             if(!transition(s, mt)) {
472                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
473                 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_UNEXPECTED_MESSAGE);
474                 return SUB_STATE_ERROR;
475             }
476
477             if (s->s3->tmp.message_size > max_message_size(s)) {
478                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
479                 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
480                 return SUB_STATE_ERROR;
481             }
482
483             st->read_state = READ_STATE_BODY;
484             /* Fall through */
485
486         case READ_STATE_BODY:
487             if (!SSL_IS_DTLS(s)) {
488                 /* We already got this above for DTLS */
489                 ret = tls_get_message_body(s, &len);
490                 if (ret == 0) {
491                     /* Could be non-blocking IO */
492                     return SUB_STATE_ERROR;
493                 }
494             }
495
496             s->first_packet = 0;
497             ret = process_message(s, len);
498             if (ret == MSG_PROCESS_ERROR) {
499                 return SUB_STATE_ERROR;
500             }
501
502             if (ret == MSG_PROCESS_FINISHED_READING) {
503                 if (SSL_IS_DTLS(s)) {
504                     dtls1_stop_timer(s);
505                 }
506                 return SUB_STATE_FINISHED;
507             }
508
509             if (ret == MSG_PROCESS_CONTINUE_PROCESSING) {
510                 st->read_state = READ_STATE_POST_PROCESS;
511                 st->read_state_work = WORK_MORE_A;
512             } else {
513                 st->read_state = READ_STATE_HEADER;
514             }
515             break;
516
517         case READ_STATE_POST_PROCESS:
518             st->read_state_work = post_process_message(s, st->read_state_work);
519             switch(st->read_state_work) {
520             default:
521                 return SUB_STATE_ERROR;
522
523             case WORK_FINISHED_CONTINUE:
524                 st->read_state = READ_STATE_HEADER;
525                 break;
526
527             case WORK_FINISHED_STOP:
528                 if (SSL_IS_DTLS(s)) {
529                     dtls1_stop_timer(s);
530                 }
531                 return SUB_STATE_FINISHED;
532             }
533             break;
534
535         default:
536             /* Shouldn't happen */
537             ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
538             SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
539             statem_set_error(s);
540             return SUB_STATE_ERROR;
541         }
542     }
543 }
544
545 /*
546  * Send a previously constructed message to the peer.
547  */
548 static int statem_do_write(SSL *s)
549 {
550     STATEM *st = &s->statem;
551
552     if (st->hand_state == TLS_ST_CW_CHANGE
553             || st->hand_state == TLS_ST_SW_CHANGE) {
554         if (SSL_IS_DTLS(s))
555             return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
556         else
557             return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
558     } else {
559         return ssl_do_write(s);
560     }
561 }
562
563 /*
564  * Initialise the MSG_FLOW_WRITING sub-state machine
565  */
566 static void init_write_state_machine(SSL *s)
567 {
568     STATEM *st = &s->statem;
569
570     st->write_state = WRITE_STATE_TRANSITION;
571 }
572
573 /*
574  * This function implements the sub-state machine when the message flow is in
575  * MSG_FLOW_WRITING. The valid sub-states and transitions are:
576  *
577  * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
578  * |             |
579  * |             v
580  * |      WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
581  * |             |
582  * |             v
583  * |       WRITE_STATE_SEND
584  * |             |
585  * |             v
586  * |     WRITE_STATE_POST_WORK
587  * |             |
588  * +-------------+
589  *
590  * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
591
592  * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
593  * sending of the message. This could result in an NBIO event occuring in
594  * which case control returns to the calling application. When this function
595  * is recalled we will resume in the same state where we left off.
596  *
597  * WRITE_STATE_SEND sends the message and performs any work to be done after
598  * sending.
599  *
600  * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
601  * message has been completed. As for WRITE_STATE_PRE_WORK this could also
602  * result in an NBIO event.
603  */
604 static enum SUB_STATE_RETURN write_state_machine(SSL *s)
605 {
606     STATEM *st = &s->statem;
607     int ret;
608     enum WRITE_TRAN (*transition)(SSL *s);
609     enum WORK_STATE (*pre_work)(SSL *s, enum WORK_STATE wst);
610     enum WORK_STATE (*post_work)(SSL *s, enum WORK_STATE wst);
611     int (*construct_message)(SSL *s);
612     void (*cb) (const SSL *ssl, int type, int val) = NULL;
613
614     if (s->info_callback != NULL)
615         cb = s->info_callback;
616     else if (s->ctx->info_callback != NULL)
617         cb = s->ctx->info_callback;
618
619     if(s->server) {
620         /* TODO: Fill these in later when we've implemented them */
621         transition = NULL;
622         pre_work = NULL;
623         post_work = NULL;
624         construct_message = NULL;
625     } else {
626         /* TODO: Fill these in later when we've implemented them */
627         transition = NULL;
628         pre_work = NULL;
629         post_work = NULL;
630         construct_message = NULL;
631     }
632
633     while(1) {
634         switch(st->write_state) {
635         case WRITE_STATE_TRANSITION:
636             if (cb != NULL) {
637                 /* Notify callback of an impending state change */
638                 if (s->server)
639                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
640                 else
641                     cb(s, SSL_CB_CONNECT_LOOP, 1);
642             }
643             switch(transition(s)) {
644             case WRITE_TRAN_CONTINUE:
645                 st->write_state = WRITE_STATE_PRE_WORK;
646                 st->write_state_work = WORK_MORE_A;
647                 break;
648
649             case WRITE_TRAN_FINISHED:
650                 return SUB_STATE_FINISHED;
651                 break;
652
653             default:
654                 return SUB_STATE_ERROR;
655             }
656             break;
657
658         case WRITE_STATE_PRE_WORK:
659             switch(st->write_state_work = pre_work(s, st->write_state_work)) {
660             default:
661                 return SUB_STATE_ERROR;
662
663             case WORK_FINISHED_CONTINUE:
664                 st->write_state = WRITE_STATE_SEND;
665                 break;
666
667             case WORK_FINISHED_STOP:
668                 return SUB_STATE_END_HANDSHAKE;
669             }
670             if(construct_message(s) == 0)
671                 return SUB_STATE_ERROR;
672
673             /* Fall through */
674
675         case WRITE_STATE_SEND:
676             if (SSL_IS_DTLS(s) && st->use_timer) {
677                 dtls1_start_timer(s);
678             }
679             ret = statem_do_write(s);
680             if (ret <= 0) {
681                 return SUB_STATE_ERROR;
682             }
683             st->write_state = WRITE_STATE_POST_WORK;
684             st->write_state_work = WORK_MORE_A;
685             /* Fall through */
686
687         case WRITE_STATE_POST_WORK:
688             switch(st->write_state_work = post_work(s, st->write_state_work)) {
689             default:
690                 return SUB_STATE_ERROR;
691
692             case WORK_FINISHED_CONTINUE:
693                 st->write_state = WRITE_STATE_TRANSITION;
694                 break;
695
696             case WORK_FINISHED_STOP:
697                 return SUB_STATE_END_HANDSHAKE;
698             }
699             break;
700
701         default:
702             return SUB_STATE_ERROR;
703         }
704     }
705 }
706
707 /*
708  * Called by the record layer to determine whether application data is
709  * allowed to be sent in the current handshake state or not.
710  *
711  * Return values are:
712  *   1: Yes (application data allowed)
713  *   0: No (application data not allowed)
714  */
715 int statem_client_app_data_allowed(SSL *s)
716 {
717     STATEM *st = &s->statem;
718
719     if(st->hand_state != TLS_ST_BEFORE &&
720             st->hand_state != TLS_ST_OK &&
721             st->hand_state != TLS_ST_CW_CLNT_HELLO)
722         return 0;
723
724     return 1;
725 }