]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/statem/statem.c
Prepare for WORK_MORE_C
[thirdparty/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 void ossl_statem_set_hello_verify_done(SSL *s)
155 {
156 s->statem.state = MSG_FLOW_UNINITED;
157 s->statem.in_init = 1;
158 /*
159 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
160 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
161 * calls to SSL_in_before() will return false. Also calls to
162 * SSL_state_string() and SSL_state_string_long() will return something
163 * sensible.
164 */
165 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
166 }
167
168 int ossl_statem_connect(SSL *s)
169 {
170 return state_machine(s, 0);
171 }
172
173 int ossl_statem_accept(SSL *s)
174 {
175 return state_machine(s, 1);
176 }
177
178 typedef void (*info_cb) (const SSL *, int, int);
179
180 static info_cb get_callback(SSL *s)
181 {
182 if (s->info_callback != NULL)
183 return s->info_callback;
184 else if (s->ctx->info_callback != NULL)
185 return s->ctx->info_callback;
186
187 return NULL;
188 }
189
190 /*
191 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
192 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
193 * transitions are as follows:
194 *
195 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
196 * | |
197 * +-----------------------+
198 * v
199 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
200 * |
201 * V
202 * MSG_FLOW_FINISHED
203 * |
204 * V
205 * [SUCCESS]
206 *
207 * We may exit at any point due to an error or NBIO event. If an NBIO event
208 * occurs then we restart at the point we left off when we are recalled.
209 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
210 *
211 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
212 * into that state at any point in the event that an irrecoverable error occurs.
213 *
214 * Valid return values are:
215 * 1: Success
216 * <=0: NBIO or error
217 */
218 static int state_machine(SSL *s, int server)
219 {
220 BUF_MEM *buf = NULL;
221 unsigned long Time = (unsigned long)time(NULL);
222 void (*cb) (const SSL *ssl, int type, int val) = NULL;
223 OSSL_STATEM *st = &s->statem;
224 int ret = -1;
225 int ssret;
226
227 if (st->state == MSG_FLOW_ERROR) {
228 /* Shouldn't have been called if we're already in the error state */
229 return -1;
230 }
231
232 RAND_add(&Time, sizeof(Time), 0);
233 ERR_clear_error();
234 clear_sys_error();
235
236 cb = get_callback(s);
237
238 st->in_handshake++;
239 if (!SSL_in_init(s) || SSL_in_before(s)) {
240 if (!SSL_clear(s))
241 return -1;
242 }
243 #ifndef OPENSSL_NO_SCTP
244 if (SSL_IS_DTLS(s)) {
245 /*
246 * Notify SCTP BIO socket to enter handshake mode and prevent stream
247 * identifier other than 0. Will be ignored if no SCTP is used.
248 */
249 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
250 st->in_handshake, NULL);
251 }
252 #endif
253
254 /* Initialise state machine */
255 if (st->state == MSG_FLOW_UNINITED
256 || st->state == MSG_FLOW_FINISHED) {
257 if (st->state == MSG_FLOW_UNINITED) {
258 st->hand_state = TLS_ST_BEFORE;
259 st->request_state = TLS_ST_BEFORE;
260 }
261
262 s->server = server;
263 if (cb != NULL)
264 cb(s, SSL_CB_HANDSHAKE_START, 1);
265
266 if (SSL_IS_DTLS(s)) {
267 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
268 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
269 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
270 goto end;
271 }
272 } else {
273 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
274 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
275 goto end;
276 }
277 }
278
279 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
280 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
281 goto end;
282 }
283
284 if (s->init_buf == NULL) {
285 if ((buf = BUF_MEM_new()) == NULL) {
286 goto end;
287 }
288 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
289 goto end;
290 }
291 s->init_buf = buf;
292 buf = NULL;
293 }
294
295 if (!ssl3_setup_buffers(s)) {
296 goto end;
297 }
298 s->init_num = 0;
299
300 /*
301 * Should have been reset by tls_process_finished, too.
302 */
303 s->s3->change_cipher_spec = 0;
304
305 /*
306 * Ok, we now need to push on a buffering BIO ...but not with
307 * SCTP
308 */
309 #ifndef OPENSSL_NO_SCTP
310 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
311 #endif
312 if (!ssl_init_wbio_buffer(s)) {
313 goto end;
314 }
315
316 if (SSL_IS_FIRST_HANDSHAKE(s) || s->renegotiate) {
317 if (!tls_setup_handshake(s)) {
318 ossl_statem_set_error(s);
319 goto end;
320 }
321
322 if (SSL_IS_FIRST_HANDSHAKE(s))
323 st->read_state_first_init = 1;
324 }
325
326 st->state = MSG_FLOW_WRITING;
327 init_write_state_machine(s);
328 }
329
330 while (st->state != MSG_FLOW_FINISHED) {
331 if (st->state == MSG_FLOW_READING) {
332 ssret = read_state_machine(s);
333 if (ssret == SUB_STATE_FINISHED) {
334 st->state = MSG_FLOW_WRITING;
335 init_write_state_machine(s);
336 } else {
337 /* NBIO or error */
338 goto end;
339 }
340 } else if (st->state == MSG_FLOW_WRITING) {
341 ssret = write_state_machine(s);
342 if (ssret == SUB_STATE_FINISHED) {
343 st->state = MSG_FLOW_READING;
344 init_read_state_machine(s);
345 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
346 st->state = MSG_FLOW_FINISHED;
347 } else {
348 /* NBIO or error */
349 goto end;
350 }
351 } else {
352 /* Error */
353 ossl_statem_set_error(s);
354 goto end;
355 }
356 }
357
358 ret = 1;
359
360 end:
361 st->in_handshake--;
362
363 #ifndef OPENSSL_NO_SCTP
364 if (SSL_IS_DTLS(s)) {
365 /*
366 * Notify SCTP BIO socket to leave handshake mode and allow stream
367 * identifier other than 0. Will be ignored if no SCTP is used.
368 */
369 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
370 st->in_handshake, NULL);
371 }
372 #endif
373
374 BUF_MEM_free(buf);
375 if (cb != NULL) {
376 if (server)
377 cb(s, SSL_CB_ACCEPT_EXIT, ret);
378 else
379 cb(s, SSL_CB_CONNECT_EXIT, ret);
380 }
381 return ret;
382 }
383
384 /*
385 * Initialise the MSG_FLOW_READING sub-state machine
386 */
387 static void init_read_state_machine(SSL *s)
388 {
389 OSSL_STATEM *st = &s->statem;
390
391 st->read_state = READ_STATE_HEADER;
392 }
393
394 static int grow_init_buf(SSL *s, size_t size) {
395
396 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
397
398 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
399 return 0;
400
401 if (size < msg_offset)
402 return 0;
403
404 s->init_msg = s->init_buf->data + msg_offset;
405
406 return 1;
407 }
408
409 /*
410 * This function implements the sub-state machine when the message flow is in
411 * MSG_FLOW_READING. The valid sub-states and transitions are:
412 *
413 * READ_STATE_HEADER <--+<-------------+
414 * | | |
415 * v | |
416 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
417 * | |
418 * +----------------------------+
419 * v
420 * [SUB_STATE_FINISHED]
421 *
422 * READ_STATE_HEADER has the responsibility for reading in the message header
423 * and transitioning the state of the handshake state machine.
424 *
425 * READ_STATE_BODY reads in the rest of the message and then subsequently
426 * processes it.
427 *
428 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
429 * processing activity performed on the message may block.
430 *
431 * Any of the above states could result in an NBIO event occurring in which case
432 * control returns to the calling application. When this function is recalled we
433 * will resume in the same state where we left off.
434 */
435 static SUB_STATE_RETURN read_state_machine(SSL *s)
436 {
437 OSSL_STATEM *st = &s->statem;
438 int ret, mt;
439 size_t len = 0;
440 int (*transition) (SSL *s, int mt);
441 PACKET pkt;
442 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
443 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
444 size_t (*max_message_size) (SSL *s);
445 void (*cb) (const SSL *ssl, int type, int val) = NULL;
446
447 cb = get_callback(s);
448
449 if (s->server) {
450 transition = ossl_statem_server_read_transition;
451 process_message = ossl_statem_server_process_message;
452 max_message_size = ossl_statem_server_max_message_size;
453 post_process_message = ossl_statem_server_post_process_message;
454 } else {
455 transition = ossl_statem_client_read_transition;
456 process_message = ossl_statem_client_process_message;
457 max_message_size = ossl_statem_client_max_message_size;
458 post_process_message = ossl_statem_client_post_process_message;
459 }
460
461 if (st->read_state_first_init) {
462 s->first_packet = 1;
463 st->read_state_first_init = 0;
464 }
465
466 while (1) {
467 switch (st->read_state) {
468 case READ_STATE_HEADER:
469 /* Get the state the peer wants to move to */
470 if (SSL_IS_DTLS(s)) {
471 /*
472 * In DTLS we get the whole message in one go - header and body
473 */
474 ret = dtls_get_message(s, &mt, &len);
475 } else {
476 ret = tls_get_message_header(s, &mt);
477 }
478
479 if (ret == 0) {
480 /* Could be non-blocking IO */
481 return SUB_STATE_ERROR;
482 }
483
484 if (cb != NULL) {
485 /* Notify callback of an impending state change */
486 if (s->server)
487 cb(s, SSL_CB_ACCEPT_LOOP, 1);
488 else
489 cb(s, SSL_CB_CONNECT_LOOP, 1);
490 }
491 /*
492 * Validate that we are allowed to move to the new state and move
493 * to that state if so
494 */
495 if (!transition(s, mt)) {
496 ossl_statem_set_error(s);
497 return SUB_STATE_ERROR;
498 }
499
500 if (s->s3->tmp.message_size > max_message_size(s)) {
501 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
502 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
503 return SUB_STATE_ERROR;
504 }
505
506 /* dtls_get_message already did this */
507 if (!SSL_IS_DTLS(s)
508 && s->s3->tmp.message_size > 0
509 && !grow_init_buf(s, s->s3->tmp.message_size
510 + SSL3_HM_HEADER_LENGTH)) {
511 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
512 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
513 return SUB_STATE_ERROR;
514 }
515
516 st->read_state = READ_STATE_BODY;
517 /* Fall through */
518
519 case READ_STATE_BODY:
520 if (!SSL_IS_DTLS(s)) {
521 /* We already got this above for DTLS */
522 ret = tls_get_message_body(s, &len);
523 if (ret == 0) {
524 /* Could be non-blocking IO */
525 return SUB_STATE_ERROR;
526 }
527 }
528
529 s->first_packet = 0;
530 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
531 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
532 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
533 return SUB_STATE_ERROR;
534 }
535 ret = process_message(s, &pkt);
536
537 /* Discard the packet data */
538 s->init_num = 0;
539
540 switch (ret) {
541 case MSG_PROCESS_ERROR:
542 return SUB_STATE_ERROR;
543
544 case MSG_PROCESS_FINISHED_READING:
545 if (SSL_IS_DTLS(s)) {
546 dtls1_stop_timer(s);
547 }
548 return SUB_STATE_FINISHED;
549
550 case MSG_PROCESS_CONTINUE_PROCESSING:
551 st->read_state = READ_STATE_POST_PROCESS;
552 st->read_state_work = WORK_MORE_A;
553 break;
554
555 default:
556 st->read_state = READ_STATE_HEADER;
557 break;
558 }
559 break;
560
561 case READ_STATE_POST_PROCESS:
562 st->read_state_work = post_process_message(s, st->read_state_work);
563 switch (st->read_state_work) {
564 case WORK_ERROR:
565 case WORK_MORE_A:
566 case WORK_MORE_B:
567 case WORK_MORE_C:
568 return SUB_STATE_ERROR;
569
570 case WORK_FINISHED_CONTINUE:
571 st->read_state = READ_STATE_HEADER;
572 break;
573
574 case WORK_FINISHED_STOP:
575 if (SSL_IS_DTLS(s)) {
576 dtls1_stop_timer(s);
577 }
578 return SUB_STATE_FINISHED;
579 }
580 break;
581
582 default:
583 /* Shouldn't happen */
584 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
585 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
586 ossl_statem_set_error(s);
587 return SUB_STATE_ERROR;
588 }
589 }
590 }
591
592 /*
593 * Send a previously constructed message to the peer.
594 */
595 static int statem_do_write(SSL *s)
596 {
597 OSSL_STATEM *st = &s->statem;
598
599 if (st->hand_state == TLS_ST_CW_CHANGE
600 || st->hand_state == TLS_ST_SW_CHANGE) {
601 if (SSL_IS_DTLS(s))
602 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
603 else
604 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
605 } else {
606 return ssl_do_write(s);
607 }
608 }
609
610 /*
611 * Initialise the MSG_FLOW_WRITING sub-state machine
612 */
613 static void init_write_state_machine(SSL *s)
614 {
615 OSSL_STATEM *st = &s->statem;
616
617 st->write_state = WRITE_STATE_TRANSITION;
618 }
619
620 /*
621 * This function implements the sub-state machine when the message flow is in
622 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
623 *
624 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
625 * | |
626 * | v
627 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
628 * | |
629 * | v
630 * | WRITE_STATE_SEND
631 * | |
632 * | v
633 * | WRITE_STATE_POST_WORK
634 * | |
635 * +-------------+
636 *
637 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
638
639 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
640 * sending of the message. This could result in an NBIO event occurring in
641 * which case control returns to the calling application. When this function
642 * is recalled we will resume in the same state where we left off.
643 *
644 * WRITE_STATE_SEND sends the message and performs any work to be done after
645 * sending.
646 *
647 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
648 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
649 * result in an NBIO event.
650 */
651 static SUB_STATE_RETURN write_state_machine(SSL *s)
652 {
653 OSSL_STATEM *st = &s->statem;
654 int ret;
655 WRITE_TRAN(*transition) (SSL *s);
656 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
657 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
658 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
659 int (**confunc) (SSL *s, WPACKET *pkt),
660 int *mt);
661 void (*cb) (const SSL *ssl, int type, int val) = NULL;
662 int (*confunc) (SSL *s, WPACKET *pkt);
663 int mt;
664 WPACKET pkt;
665
666 cb = get_callback(s);
667
668 if (s->server) {
669 transition = ossl_statem_server_write_transition;
670 pre_work = ossl_statem_server_pre_work;
671 post_work = ossl_statem_server_post_work;
672 get_construct_message_f = ossl_statem_server_construct_message;
673 } else {
674 transition = ossl_statem_client_write_transition;
675 pre_work = ossl_statem_client_pre_work;
676 post_work = ossl_statem_client_post_work;
677 get_construct_message_f = ossl_statem_client_construct_message;
678 }
679
680 while (1) {
681 switch (st->write_state) {
682 case WRITE_STATE_TRANSITION:
683 if (cb != NULL) {
684 /* Notify callback of an impending state change */
685 if (s->server)
686 cb(s, SSL_CB_ACCEPT_LOOP, 1);
687 else
688 cb(s, SSL_CB_CONNECT_LOOP, 1);
689 }
690 switch (transition(s)) {
691 case WRITE_TRAN_CONTINUE:
692 st->write_state = WRITE_STATE_PRE_WORK;
693 st->write_state_work = WORK_MORE_A;
694 break;
695
696 case WRITE_TRAN_FINISHED:
697 return SUB_STATE_FINISHED;
698 break;
699
700 case WRITE_TRAN_ERROR:
701 return SUB_STATE_ERROR;
702 }
703 break;
704
705 case WRITE_STATE_PRE_WORK:
706 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
707 case WORK_ERROR:
708 case WORK_MORE_A:
709 case WORK_MORE_B:
710 case WORK_MORE_C:
711 return SUB_STATE_ERROR;
712
713 case WORK_FINISHED_CONTINUE:
714 st->write_state = WRITE_STATE_SEND;
715 break;
716
717 case WORK_FINISHED_STOP:
718 return SUB_STATE_END_HANDSHAKE;
719 }
720 if (!WPACKET_init(&pkt, s->init_buf)
721 || !get_construct_message_f(s, &pkt, &confunc, &mt)
722 || !ssl_set_handshake_header(s, &pkt, mt)
723 || (confunc != NULL && !confunc(s, &pkt))
724 || !ssl_close_construct_packet(s, &pkt, mt)
725 || !WPACKET_finish(&pkt)) {
726 WPACKET_cleanup(&pkt);
727 ossl_statem_set_error(s);
728 return SUB_STATE_ERROR;
729 }
730
731 /* Fall through */
732
733 case WRITE_STATE_SEND:
734 if (SSL_IS_DTLS(s) && st->use_timer) {
735 dtls1_start_timer(s);
736 }
737 ret = statem_do_write(s);
738 if (ret <= 0) {
739 return SUB_STATE_ERROR;
740 }
741 st->write_state = WRITE_STATE_POST_WORK;
742 st->write_state_work = WORK_MORE_A;
743 /* Fall through */
744
745 case WRITE_STATE_POST_WORK:
746 switch (st->write_state_work = post_work(s, st->write_state_work)) {
747 case WORK_ERROR:
748 case WORK_MORE_A:
749 case WORK_MORE_B:
750 case WORK_MORE_C:
751 return SUB_STATE_ERROR;
752
753 case WORK_FINISHED_CONTINUE:
754 st->write_state = WRITE_STATE_TRANSITION;
755 break;
756
757 case WORK_FINISHED_STOP:
758 return SUB_STATE_END_HANDSHAKE;
759 }
760 break;
761
762 default:
763 return SUB_STATE_ERROR;
764 }
765 }
766 }
767
768 /*
769 * Flush the write BIO
770 */
771 int statem_flush(SSL *s)
772 {
773 s->rwstate = SSL_WRITING;
774 if (BIO_flush(s->wbio) <= 0) {
775 return 0;
776 }
777 s->rwstate = SSL_NOTHING;
778
779 return 1;
780 }
781
782 /*
783 * Called by the record layer to determine whether application data is
784 * allowed to be received in the current handshake state or not.
785 *
786 * Return values are:
787 * 1: Yes (application data allowed)
788 * 0: No (application data not allowed)
789 */
790 int ossl_statem_app_data_allowed(SSL *s)
791 {
792 OSSL_STATEM *st = &s->statem;
793
794 if (st->state == MSG_FLOW_UNINITED)
795 return 0;
796
797 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
798 return 0;
799
800 if (s->server) {
801 /*
802 * If we're a server and we haven't got as far as writing our
803 * ServerHello yet then we allow app data
804 */
805 if (st->hand_state == TLS_ST_BEFORE
806 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
807 return 1;
808 } else {
809 /*
810 * If we're a client and we haven't read the ServerHello yet then we
811 * allow app data
812 */
813 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
814 return 1;
815 }
816
817 return 0;
818 }
819
820 #ifndef OPENSSL_NO_SCTP
821 /*
822 * Set flag used by SCTP to determine whether we are in the read sock state
823 */
824 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
825 {
826 s->statem.in_sctp_read_sock = read_sock;
827 }
828
829 /*
830 * Called by the record layer to determine whether we are in the read sock
831 * state or not.
832 *
833 * Return values are:
834 * 1: Yes (we are in the read sock state)
835 * 0: No (we are not in the read sock state)
836 */
837 int ossl_statem_in_sctp_read_sock(SSL *s)
838 {
839 return s->statem.in_sctp_read_sock;
840 }
841 #endif