Skip early_data if appropriate after a HelloRetryRequest
[openssl.git] / ssl / statem / statem.c
1 /*
2  * Copyright 2015-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 <openssl/rand.h>
11 #include "../ssl_locl.h"
12 #include "statem_locl.h"
13
14 /*
15  * This file implements the SSL/TLS/DTLS state machines.
16  *
17  * There are two primary state machines:
18  *
19  * 1) Message flow state machine
20  * 2) Handshake state machine
21  *
22  * The Message flow state machine controls the reading and sending of messages
23  * including handling of non-blocking IO events, flushing of the underlying
24  * write BIO, handling unexpected messages, etc. It is itself broken into two
25  * separate sub-state machines which control reading and writing respectively.
26  *
27  * The Handshake state machine keeps track of the current SSL/TLS handshake
28  * state. Transitions of the handshake state are the result of events that
29  * occur within the Message flow state machine.
30  *
31  * Overall it looks like this:
32  *
33  * ---------------------------------------------            -------------------
34  * |                                           |            |                 |
35  * | Message flow state machine                |            |                 |
36  * |                                           |            |                 |
37  * | -------------------- -------------------- | Transition | Handshake state |
38  * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event      | machine         |
39  * | | sub-state        | | sub-state        | |----------->|                 |
40  * | | machine for      | | machine for      | |            |                 |
41  * | | reading messages | | writing messages | |            |                 |
42  * | -------------------- -------------------- |            |                 |
43  * |                                           |            |                 |
44  * ---------------------------------------------            -------------------
45  *
46  */
47
48 /* Sub state machine return values */
49 typedef enum {
50     /* Something bad happened or NBIO */
51     SUB_STATE_ERROR,
52     /* Sub state finished go to the next sub state */
53     SUB_STATE_FINISHED,
54     /* Sub state finished and handshake was completed */
55     SUB_STATE_END_HANDSHAKE
56 } SUB_STATE_RETURN;
57
58 static int state_machine(SSL *s, int server);
59 static void init_read_state_machine(SSL *s);
60 static SUB_STATE_RETURN read_state_machine(SSL *s);
61 static void init_write_state_machine(SSL *s);
62 static SUB_STATE_RETURN write_state_machine(SSL *s);
63
64 OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
65 {
66     return ssl->statem.hand_state;
67 }
68
69 int SSL_in_init(SSL *s)
70 {
71     return s->statem.in_init;
72 }
73
74 int SSL_is_init_finished(SSL *s)
75 {
76     return !(s->statem.in_init) && (s->statem.hand_state == TLS_ST_OK);
77 }
78
79 int SSL_in_before(SSL *s)
80 {
81     /*
82      * Historically being "in before" meant before anything had happened. In the
83      * current code though we remain in the "before" state for a while after we
84      * have started the handshake process (e.g. as a server waiting for the
85      * first message to arrive). There "in before" is taken to mean "in before"
86      * and not started any handshake process yet.
87      */
88     return (s->statem.hand_state == TLS_ST_BEFORE)
89         && (s->statem.state == MSG_FLOW_UNINITED);
90 }
91
92 /*
93  * Clear the state machine state and reset back to MSG_FLOW_UNINITED
94  */
95 void ossl_statem_clear(SSL *s)
96 {
97     s->statem.state = MSG_FLOW_UNINITED;
98     s->statem.hand_state = TLS_ST_BEFORE;
99     s->statem.in_init = 1;
100     s->statem.no_cert_verify = 0;
101 }
102
103 /*
104  * Set the state machine up ready for a renegotiation handshake
105  */
106 void ossl_statem_set_renegotiate(SSL *s)
107 {
108     s->statem.in_init = 1;
109     s->statem.request_state = TLS_ST_SW_HELLO_REQ;
110 }
111
112 /*
113  * Put the state machine into an error state. This is a permanent error for
114  * the current connection.
115  */
116 void ossl_statem_set_error(SSL *s)
117 {
118     s->statem.state = MSG_FLOW_ERROR;
119 }
120
121 /*
122  * Discover whether the current connection is in the error state.
123  *
124  * Valid return values are:
125  *   1: Yes
126  *   0: No
127  */
128 int ossl_statem_in_error(const SSL *s)
129 {
130     if (s->statem.state == MSG_FLOW_ERROR)
131         return 1;
132
133     return 0;
134 }
135
136 void ossl_statem_set_in_init(SSL *s, int init)
137 {
138     s->statem.in_init = init;
139 }
140
141 int ossl_statem_get_in_handshake(SSL *s)
142 {
143     return s->statem.in_handshake;
144 }
145
146 void ossl_statem_set_in_handshake(SSL *s, int inhand)
147 {
148     if (inhand)
149         s->statem.in_handshake++;
150     else
151         s->statem.in_handshake--;
152 }
153
154 /* Are we in a sensible state to skip over unreadable early data? */
155 int ossl_statem_skip_early_data(SSL *s)
156 {
157     if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
158         return 0;
159
160     if (s->hello_retry_request) {
161         if (s->statem.hand_state != TLS_ST_SW_HELLO_RETRY_REQUEST)
162             return 0;
163     } else {
164         if (s->statem.hand_state != TLS_ST_SW_FINISHED)
165             return 0;
166     }
167
168     return 1;
169 }
170
171 void ossl_statem_set_hello_verify_done(SSL *s)
172 {
173     s->statem.state = MSG_FLOW_UNINITED;
174     s->statem.in_init = 1;
175     /*
176      * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
177      * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
178      * calls to SSL_in_before() will return false. Also calls to
179      * SSL_state_string() and SSL_state_string_long() will return something
180      * sensible.
181      */
182     s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
183 }
184
185 int ossl_statem_connect(SSL *s)
186 {
187     return state_machine(s, 0);
188 }
189
190 int ossl_statem_accept(SSL *s)
191 {
192     return state_machine(s, 1);
193 }
194
195 typedef void (*info_cb) (const SSL *, int, int);
196
197 static info_cb get_callback(SSL *s)
198 {
199     if (s->info_callback != NULL)
200         return s->info_callback;
201     else if (s->ctx->info_callback != NULL)
202         return s->ctx->info_callback;
203
204     return NULL;
205 }
206
207 /*
208  * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
209  * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
210  * transitions are as follows:
211  *
212  * MSG_FLOW_UNINITED     MSG_FLOW_FINISHED
213  *        |                       |
214  *        +-----------------------+
215  *        v
216  * MSG_FLOW_WRITING <---> MSG_FLOW_READING
217  *        |
218  *        V
219  * MSG_FLOW_FINISHED
220  *        |
221  *        V
222  *    [SUCCESS]
223  *
224  * We may exit at any point due to an error or NBIO event. If an NBIO event
225  * occurs then we restart at the point we left off when we are recalled.
226  * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
227  *
228  * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
229  * into that state at any point in the event that an irrecoverable error occurs.
230  *
231  * Valid return values are:
232  *   1: Success
233  * <=0: NBIO or error
234  */
235 static int state_machine(SSL *s, int server)
236 {
237     BUF_MEM *buf = NULL;
238     unsigned long Time = (unsigned long)time(NULL);
239     void (*cb) (const SSL *ssl, int type, int val) = NULL;
240     OSSL_STATEM *st = &s->statem;
241     int ret = -1;
242     int ssret;
243
244     if (st->state == MSG_FLOW_ERROR) {
245         /* Shouldn't have been called if we're already in the error state */
246         return -1;
247     }
248
249     RAND_add(&Time, sizeof(Time), 0);
250     ERR_clear_error();
251     clear_sys_error();
252
253     cb = get_callback(s);
254
255     st->in_handshake++;
256     if (!SSL_in_init(s) || SSL_in_before(s)) {
257         if (!SSL_clear(s))
258             return -1;
259     }
260 #ifndef OPENSSL_NO_SCTP
261     if (SSL_IS_DTLS(s)) {
262         /*
263          * Notify SCTP BIO socket to enter handshake mode and prevent stream
264          * identifier other than 0. Will be ignored if no SCTP is used.
265          */
266         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
267                  st->in_handshake, NULL);
268     }
269 #endif
270
271     /* Initialise state machine */
272     if (st->state == MSG_FLOW_UNINITED
273             || st->state == MSG_FLOW_FINISHED) {
274         if (st->state == MSG_FLOW_UNINITED) {
275             st->hand_state = TLS_ST_BEFORE;
276             st->request_state = TLS_ST_BEFORE;
277         }
278
279         s->server = server;
280         if (cb != NULL)
281             cb(s, SSL_CB_HANDSHAKE_START, 1);
282
283         if (SSL_IS_DTLS(s)) {
284             if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
285                 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
286                 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
287                 goto end;
288             }
289         } else {
290             if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
291                 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
292                 goto end;
293             }
294         }
295
296         if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
297             SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
298             goto end;
299         }
300
301         if (s->init_buf == NULL) {
302             if ((buf = BUF_MEM_new()) == NULL) {
303                 goto end;
304             }
305             if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
306                 goto end;
307             }
308             s->init_buf = buf;
309             buf = NULL;
310         }
311
312         if (!ssl3_setup_buffers(s)) {
313             goto end;
314         }
315         s->init_num = 0;
316
317         /*
318          * Should have been reset by tls_process_finished, too.
319          */
320         s->s3->change_cipher_spec = 0;
321
322         /*
323          * Ok, we now need to push on a buffering BIO ...but not with
324          * SCTP
325          */
326 #ifndef OPENSSL_NO_SCTP
327         if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
328 #endif
329             if (!ssl_init_wbio_buffer(s)) {
330                 goto end;
331             }
332
333         if ((SSL_IS_FIRST_HANDSHAKE(s)
334                     && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING
335                     && s->early_data_state != SSL_EARLY_DATA_FINISHED_READING)
336                 || s->renegotiate) {
337             if (!tls_setup_handshake(s)) {
338                 ossl_statem_set_error(s);
339                 goto end;
340             }
341
342             if (SSL_IS_FIRST_HANDSHAKE(s))
343                 st->read_state_first_init = 1;
344         }
345
346         st->state = MSG_FLOW_WRITING;
347         init_write_state_machine(s);
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             ossl_statem_set_error(s);
374             goto end;
375         }
376     }
377
378     ret = 1;
379
380  end:
381     st->in_handshake--;
382
383 #ifndef OPENSSL_NO_SCTP
384     if (SSL_IS_DTLS(s)) {
385         /*
386          * Notify SCTP BIO socket to leave handshake mode and allow stream
387          * identifier other than 0. Will be ignored if no SCTP is used.
388          */
389         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
390                  st->in_handshake, NULL);
391     }
392 #endif
393
394     BUF_MEM_free(buf);
395     if (cb != NULL) {
396         if (server)
397             cb(s, SSL_CB_ACCEPT_EXIT, ret);
398         else
399             cb(s, SSL_CB_CONNECT_EXIT, ret);
400     }
401     return ret;
402 }
403
404 /*
405  * Initialise the MSG_FLOW_READING sub-state machine
406  */
407 static void init_read_state_machine(SSL *s)
408 {
409     OSSL_STATEM *st = &s->statem;
410
411     st->read_state = READ_STATE_HEADER;
412 }
413
414 static int grow_init_buf(SSL *s, size_t size) {
415
416     size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
417
418     if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
419         return 0;
420
421     if (size < msg_offset)
422         return 0;
423
424     s->init_msg = s->init_buf->data + msg_offset;
425
426     return 1;
427 }
428
429 /*
430  * This function implements the sub-state machine when the message flow is in
431  * MSG_FLOW_READING. The valid sub-states and transitions are:
432  *
433  * READ_STATE_HEADER <--+<-------------+
434  *        |             |              |
435  *        v             |              |
436  * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
437  *        |                            |
438  *        +----------------------------+
439  *        v
440  * [SUB_STATE_FINISHED]
441  *
442  * READ_STATE_HEADER has the responsibility for reading in the message header
443  * and transitioning the state of the handshake state machine.
444  *
445  * READ_STATE_BODY reads in the rest of the message and then subsequently
446  * processes it.
447  *
448  * READ_STATE_POST_PROCESS is an optional step that may occur if some post
449  * processing activity performed on the message may block.
450  *
451  * Any of the above states could result in an NBIO event occurring in which case
452  * control returns to the calling application. When this function is recalled we
453  * will resume in the same state where we left off.
454  */
455 static SUB_STATE_RETURN read_state_machine(SSL *s)
456 {
457     OSSL_STATEM *st = &s->statem;
458     int ret, mt;
459     size_t len = 0;
460     int (*transition) (SSL *s, int mt);
461     PACKET pkt;
462     MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
463     WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
464     size_t (*max_message_size) (SSL *s);
465     void (*cb) (const SSL *ssl, int type, int val) = NULL;
466
467     cb = get_callback(s);
468
469     if (s->server) {
470         transition = ossl_statem_server_read_transition;
471         process_message = ossl_statem_server_process_message;
472         max_message_size = ossl_statem_server_max_message_size;
473         post_process_message = ossl_statem_server_post_process_message;
474     } else {
475         transition = ossl_statem_client_read_transition;
476         process_message = ossl_statem_client_process_message;
477         max_message_size = ossl_statem_client_max_message_size;
478         post_process_message = ossl_statem_client_post_process_message;
479     }
480
481     if (st->read_state_first_init) {
482         s->first_packet = 1;
483         st->read_state_first_init = 0;
484     }
485
486     while (1) {
487         switch (st->read_state) {
488         case READ_STATE_HEADER:
489             /* Get the state the peer wants to move to */
490             if (SSL_IS_DTLS(s)) {
491                 /*
492                  * In DTLS we get the whole message in one go - header and body
493                  */
494                 ret = dtls_get_message(s, &mt, &len);
495             } else {
496                 ret = tls_get_message_header(s, &mt);
497             }
498
499             if (ret == 0) {
500                 /* Could be non-blocking IO */
501                 return SUB_STATE_ERROR;
502             }
503
504             if (cb != NULL) {
505                 /* Notify callback of an impending state change */
506                 if (s->server)
507                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
508                 else
509                     cb(s, SSL_CB_CONNECT_LOOP, 1);
510             }
511             /*
512              * Validate that we are allowed to move to the new state and move
513              * to that state if so
514              */
515             if (!transition(s, mt)) {
516                 ossl_statem_set_error(s);
517                 return SUB_STATE_ERROR;
518             }
519
520             if (s->s3->tmp.message_size > max_message_size(s)) {
521                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
522                 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
523                 return SUB_STATE_ERROR;
524             }
525
526             /* dtls_get_message already did this */
527             if (!SSL_IS_DTLS(s)
528                     && s->s3->tmp.message_size > 0
529                     && !grow_init_buf(s, s->s3->tmp.message_size
530                                          + SSL3_HM_HEADER_LENGTH)) {
531                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
532                 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
533                 return SUB_STATE_ERROR;
534             }
535
536             st->read_state = READ_STATE_BODY;
537             /* Fall through */
538
539         case READ_STATE_BODY:
540             if (!SSL_IS_DTLS(s)) {
541                 /* We already got this above for DTLS */
542                 ret = tls_get_message_body(s, &len);
543                 if (ret == 0) {
544                     /* Could be non-blocking IO */
545                     return SUB_STATE_ERROR;
546                 }
547             }
548
549             s->first_packet = 0;
550             if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
551                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
552                 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
553                 return SUB_STATE_ERROR;
554             }
555             ret = process_message(s, &pkt);
556
557             /* Discard the packet data */
558             s->init_num = 0;
559
560             switch (ret) {
561             case MSG_PROCESS_ERROR:
562                 return SUB_STATE_ERROR;
563
564             case MSG_PROCESS_FINISHED_READING:
565                 if (SSL_IS_DTLS(s)) {
566                     dtls1_stop_timer(s);
567                 }
568                 return SUB_STATE_FINISHED;
569
570             case MSG_PROCESS_CONTINUE_PROCESSING:
571                 st->read_state = READ_STATE_POST_PROCESS;
572                 st->read_state_work = WORK_MORE_A;
573                 break;
574
575             default:
576                 st->read_state = READ_STATE_HEADER;
577                 break;
578             }
579             break;
580
581         case READ_STATE_POST_PROCESS:
582             st->read_state_work = post_process_message(s, st->read_state_work);
583             switch (st->read_state_work) {
584             case WORK_ERROR:
585             case WORK_MORE_A:
586             case WORK_MORE_B:
587             case WORK_MORE_C:
588                 return SUB_STATE_ERROR;
589
590             case WORK_FINISHED_CONTINUE:
591                 st->read_state = READ_STATE_HEADER;
592                 break;
593
594             case WORK_FINISHED_STOP:
595                 if (SSL_IS_DTLS(s)) {
596                     dtls1_stop_timer(s);
597                 }
598                 return SUB_STATE_FINISHED;
599             }
600             break;
601
602         default:
603             /* Shouldn't happen */
604             ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
605             SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
606             ossl_statem_set_error(s);
607             return SUB_STATE_ERROR;
608         }
609     }
610 }
611
612 /*
613  * Send a previously constructed message to the peer.
614  */
615 static int statem_do_write(SSL *s)
616 {
617     OSSL_STATEM *st = &s->statem;
618
619     if (st->hand_state == TLS_ST_CW_CHANGE
620         || st->hand_state == TLS_ST_SW_CHANGE) {
621         if (SSL_IS_DTLS(s))
622             return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
623         else
624             return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
625     } else {
626         return ssl_do_write(s);
627     }
628 }
629
630 /*
631  * Initialise the MSG_FLOW_WRITING sub-state machine
632  */
633 static void init_write_state_machine(SSL *s)
634 {
635     OSSL_STATEM *st = &s->statem;
636
637     st->write_state = WRITE_STATE_TRANSITION;
638 }
639
640 /*
641  * This function implements the sub-state machine when the message flow is in
642  * MSG_FLOW_WRITING. The valid sub-states and transitions are:
643  *
644  * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
645  * |             |
646  * |             v
647  * |      WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
648  * |             |
649  * |             v
650  * |       WRITE_STATE_SEND
651  * |             |
652  * |             v
653  * |     WRITE_STATE_POST_WORK
654  * |             |
655  * +-------------+
656  *
657  * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
658
659  * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
660  * sending of the message. This could result in an NBIO event occurring in
661  * which case control returns to the calling application. When this function
662  * is recalled we will resume in the same state where we left off.
663  *
664  * WRITE_STATE_SEND sends the message and performs any work to be done after
665  * sending.
666  *
667  * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
668  * message has been completed. As for WRITE_STATE_PRE_WORK this could also
669  * result in an NBIO event.
670  */
671 static SUB_STATE_RETURN write_state_machine(SSL *s)
672 {
673     OSSL_STATEM *st = &s->statem;
674     int ret;
675     WRITE_TRAN(*transition) (SSL *s);
676     WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
677     WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
678     int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
679                                     int (**confunc) (SSL *s, WPACKET *pkt),
680                                     int *mt);
681     void (*cb) (const SSL *ssl, int type, int val) = NULL;
682     int (*confunc) (SSL *s, WPACKET *pkt);
683     int mt;
684     WPACKET pkt;
685
686     cb = get_callback(s);
687
688     if (s->server) {
689         transition = ossl_statem_server_write_transition;
690         pre_work = ossl_statem_server_pre_work;
691         post_work = ossl_statem_server_post_work;
692         get_construct_message_f = ossl_statem_server_construct_message;
693     } else {
694         transition = ossl_statem_client_write_transition;
695         pre_work = ossl_statem_client_pre_work;
696         post_work = ossl_statem_client_post_work;
697         get_construct_message_f = ossl_statem_client_construct_message;
698     }
699
700     while (1) {
701         switch (st->write_state) {
702         case WRITE_STATE_TRANSITION:
703             if (cb != NULL) {
704                 /* Notify callback of an impending state change */
705                 if (s->server)
706                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
707                 else
708                     cb(s, SSL_CB_CONNECT_LOOP, 1);
709             }
710             switch (transition(s)) {
711             case WRITE_TRAN_CONTINUE:
712                 st->write_state = WRITE_STATE_PRE_WORK;
713                 st->write_state_work = WORK_MORE_A;
714                 break;
715
716             case WRITE_TRAN_FINISHED:
717                 return SUB_STATE_FINISHED;
718                 break;
719
720             case WRITE_TRAN_ERROR:
721                 return SUB_STATE_ERROR;
722             }
723             break;
724
725         case WRITE_STATE_PRE_WORK:
726             switch (st->write_state_work = pre_work(s, st->write_state_work)) {
727             case WORK_ERROR:
728             case WORK_MORE_A:
729             case WORK_MORE_B:
730             case WORK_MORE_C:
731                 return SUB_STATE_ERROR;
732
733             case WORK_FINISHED_CONTINUE:
734                 st->write_state = WRITE_STATE_SEND;
735                 break;
736
737             case WORK_FINISHED_STOP:
738                 return SUB_STATE_END_HANDSHAKE;
739             }
740             if (!WPACKET_init(&pkt, s->init_buf)
741                     || !get_construct_message_f(s, &pkt, &confunc, &mt)
742                     || !ssl_set_handshake_header(s, &pkt, mt)
743                     || (confunc != NULL && !confunc(s, &pkt))
744                     || !ssl_close_construct_packet(s, &pkt, mt)
745                     || !WPACKET_finish(&pkt)) {
746                 WPACKET_cleanup(&pkt);
747                 ossl_statem_set_error(s);
748                 return SUB_STATE_ERROR;
749             }
750
751             /* Fall through */
752
753         case WRITE_STATE_SEND:
754             if (SSL_IS_DTLS(s) && st->use_timer) {
755                 dtls1_start_timer(s);
756             }
757             ret = statem_do_write(s);
758             if (ret <= 0) {
759                 return SUB_STATE_ERROR;
760             }
761             st->write_state = WRITE_STATE_POST_WORK;
762             st->write_state_work = WORK_MORE_A;
763             /* Fall through */
764
765         case WRITE_STATE_POST_WORK:
766             switch (st->write_state_work = post_work(s, st->write_state_work)) {
767             case WORK_ERROR:
768             case WORK_MORE_A:
769             case WORK_MORE_B:
770             case WORK_MORE_C:
771                 return SUB_STATE_ERROR;
772
773             case WORK_FINISHED_CONTINUE:
774                 st->write_state = WRITE_STATE_TRANSITION;
775                 break;
776
777             case WORK_FINISHED_STOP:
778                 return SUB_STATE_END_HANDSHAKE;
779             }
780             break;
781
782         default:
783             return SUB_STATE_ERROR;
784         }
785     }
786 }
787
788 /*
789  * Flush the write BIO
790  */
791 int statem_flush(SSL *s)
792 {
793     s->rwstate = SSL_WRITING;
794     if (BIO_flush(s->wbio) <= 0) {
795         return 0;
796     }
797     s->rwstate = SSL_NOTHING;
798
799     return 1;
800 }
801
802 /*
803  * Called by the record layer to determine whether application data is
804  * allowed to be received in the current handshake state or not.
805  *
806  * Return values are:
807  *   1: Yes (application data allowed)
808  *   0: No (application data not allowed)
809  */
810 int ossl_statem_app_data_allowed(SSL *s)
811 {
812     OSSL_STATEM *st = &s->statem;
813
814     if (st->state == MSG_FLOW_UNINITED)
815         return 0;
816
817     if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
818         return 0;
819
820     if (s->server) {
821         /*
822          * If we're a server and we haven't got as far as writing our
823          * ServerHello yet then we allow app data
824          */
825         if (st->hand_state == TLS_ST_BEFORE
826             || st->hand_state == TLS_ST_SR_CLNT_HELLO)
827             return 1;
828     } else {
829         /*
830          * If we're a client and we haven't read the ServerHello yet then we
831          * allow app data
832          */
833         if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
834             return 1;
835     }
836
837     return 0;
838 }
839
840 #ifndef OPENSSL_NO_SCTP
841 /*
842  * Set flag used by SCTP to determine whether we are in the read sock state
843  */
844 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
845 {
846     s->statem.in_sctp_read_sock = read_sock;
847 }
848
849 /*
850  * Called by the record layer to determine whether we are in the read sock
851  * state or not.
852  *
853  * Return values are:
854  *   1: Yes (we are in the read sock state)
855  *   0: No (we are not in the read sock state)
856  */
857 int ossl_statem_in_sctp_read_sock(SSL *s)
858 {
859     return s->statem.in_sctp_read_sock;
860 }
861 #endif