2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
11 #include <openssl/rand.h>
12 #include "../ssl_locl.h"
13 #include "statem_locl.h"
16 * This file implements the SSL/TLS/DTLS state machines.
18 * There are two primary state machines:
20 * 1) Message flow state machine
21 * 2) Handshake state machine
23 * The Message flow state machine controls the reading and sending of messages
24 * including handling of non-blocking IO events, flushing of the underlying
25 * write BIO, handling unexpected messages, etc. It is itself broken into two
26 * separate sub-state machines which control reading and writing respectively.
28 * The Handshake state machine keeps track of the current SSL/TLS handshake
29 * state. Transitions of the handshake state are the result of events that
30 * occur within the Message flow state machine.
32 * Overall it looks like this:
34 * --------------------------------------------- -------------------
36 * | Message flow state machine | | |
38 * | -------------------- -------------------- | Transition | Handshake state |
39 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
40 * | | sub-state | | sub-state | |----------->| |
41 * | | machine for | | machine for | | | |
42 * | | reading messages | | writing messages | | | |
43 * | -------------------- -------------------- | | |
45 * --------------------------------------------- -------------------
49 /* Sub state machine return values */
51 /* Something bad happened or NBIO */
53 /* Sub state finished go to the next sub state */
55 /* Sub state finished and handshake was completed */
56 SUB_STATE_END_HANDSHAKE
59 static int state_machine(SSL
*s
, int server
);
60 static void init_read_state_machine(SSL
*s
);
61 static SUB_STATE_RETURN
read_state_machine(SSL
*s
);
62 static void init_write_state_machine(SSL
*s
);
63 static SUB_STATE_RETURN
write_state_machine(SSL
*s
);
65 OSSL_HANDSHAKE_STATE
SSL_get_state(const SSL
*ssl
)
67 return ssl
->statem
.hand_state
;
70 int SSL_in_init(SSL
*s
)
72 return s
->statem
.in_init
;
75 int SSL_is_init_finished(SSL
*s
)
77 return !(s
->statem
.in_init
) && (s
->statem
.hand_state
== TLS_ST_OK
);
80 int SSL_in_before(SSL
*s
)
83 * Historically being "in before" meant before anything had happened. In the
84 * current code though we remain in the "before" state for a while after we
85 * have started the handshake process (e.g. as a server waiting for the
86 * first message to arrive). There "in before" is taken to mean "in before"
87 * and not started any handshake process yet.
89 return (s
->statem
.hand_state
== TLS_ST_BEFORE
)
90 && (s
->statem
.state
== MSG_FLOW_UNINITED
);
94 * Clear the state machine state and reset back to MSG_FLOW_UNINITED
96 void ossl_statem_clear(SSL
*s
)
98 s
->statem
.state
= MSG_FLOW_UNINITED
;
99 s
->statem
.hand_state
= TLS_ST_BEFORE
;
100 s
->statem
.in_init
= 1;
101 s
->statem
.no_cert_verify
= 0;
105 * Set the state machine up ready for a renegotiation handshake
107 void ossl_statem_set_renegotiate(SSL
*s
)
109 s
->statem
.in_init
= 1;
110 s
->statem
.request_state
= TLS_ST_SW_HELLO_REQ
;
114 * Put the state machine into an error state and send an alert if appropriate.
115 * This is a permanent error for the current connection.
117 void ossl_statem_fatal(SSL
*s
, int al
, int func
, int reason
, const char *file
,
120 s
->statem
.in_init
= 1;
121 s
->statem
.state
= MSG_FLOW_ERROR
;
122 ERR_put_error(ERR_LIB_SSL
, func
, reason
, file
, line
);
123 if (al
!= SSL_AD_NO_ALERT
)
124 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
128 * This macro should only be called if we are already expecting to be in
129 * a fatal error state. We verify that we are, and set it if not (this would
132 #define check_fatal(s, f) \
134 if (!ossl_assert((s)->statem.in_init \
135 || (s)->statem.state != MSG_FLOW_ERROR)) \
136 SSLfatal(s, SSL_AD_INTERNAL_ERROR, (f), \
137 SSL_R_MISSING_FATAL); \
141 * Discover whether the current connection is in the error state.
143 * Valid return values are:
147 int ossl_statem_in_error(const SSL
*s
)
149 if (s
->statem
.state
== MSG_FLOW_ERROR
)
155 void ossl_statem_set_in_init(SSL
*s
, int init
)
157 s
->statem
.in_init
= init
;
160 int ossl_statem_get_in_handshake(SSL
*s
)
162 return s
->statem
.in_handshake
;
165 void ossl_statem_set_in_handshake(SSL
*s
, int inhand
)
168 s
->statem
.in_handshake
++;
170 s
->statem
.in_handshake
--;
173 /* Are we in a sensible state to skip over unreadable early data? */
174 int ossl_statem_skip_early_data(SSL
*s
)
176 if (s
->ext
.early_data
!= SSL_EARLY_DATA_REJECTED
)
179 if (!s
->server
|| s
->statem
.hand_state
!= TLS_ST_EARLY_DATA
)
186 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
187 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
188 * data state and whether we should attempt to move the handshake on if so.
189 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
190 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
193 void ossl_statem_check_finish_init(SSL
*s
, int sending
)
196 if (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
197 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
) {
198 ossl_statem_set_in_init(s
, 1);
199 if (s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
) {
201 * SSL_connect() or SSL_do_handshake() has been called directly.
202 * We don't allow any more writing of early data.
204 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
207 } else if (!s
->server
) {
208 if ((sending
&& (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
209 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
210 && s
->early_data_state
!= SSL_EARLY_DATA_WRITING
)
211 || (!sending
&& s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)) {
212 ossl_statem_set_in_init(s
, 1);
214 * SSL_write() has been called directly. We don't allow any more
215 * writing of early data.
217 if (sending
&& s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
)
218 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
221 if (s
->early_data_state
== SSL_EARLY_DATA_FINISHED_READING
222 && s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
223 ossl_statem_set_in_init(s
, 1);
227 void ossl_statem_set_hello_verify_done(SSL
*s
)
229 s
->statem
.state
= MSG_FLOW_UNINITED
;
230 s
->statem
.in_init
= 1;
232 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
233 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
234 * calls to SSL_in_before() will return false. Also calls to
235 * SSL_state_string() and SSL_state_string_long() will return something
238 s
->statem
.hand_state
= TLS_ST_SR_CLNT_HELLO
;
241 int ossl_statem_connect(SSL
*s
)
243 return state_machine(s
, 0);
246 int ossl_statem_accept(SSL
*s
)
248 return state_machine(s
, 1);
251 typedef void (*info_cb
) (const SSL
*, int, int);
253 static info_cb
get_callback(SSL
*s
)
255 if (s
->info_callback
!= NULL
)
256 return s
->info_callback
;
257 else if (s
->ctx
->info_callback
!= NULL
)
258 return s
->ctx
->info_callback
;
264 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
265 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
266 * transitions are as follows:
268 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
270 * +-----------------------+
272 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
280 * We may exit at any point due to an error or NBIO event. If an NBIO event
281 * occurs then we restart at the point we left off when we are recalled.
282 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
284 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
285 * into that state at any point in the event that an irrecoverable error occurs.
287 * Valid return values are:
291 static int state_machine(SSL
*s
, int server
)
294 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
295 OSSL_STATEM
*st
= &s
->statem
;
299 if (st
->state
== MSG_FLOW_ERROR
) {
300 /* Shouldn't have been called if we're already in the error state */
307 cb
= get_callback(s
);
310 if (!SSL_in_init(s
) || SSL_in_before(s
)) {
314 #ifndef OPENSSL_NO_SCTP
315 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
317 * Notify SCTP BIO socket to enter handshake mode and prevent stream
318 * identifier other than 0.
320 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
321 st
->in_handshake
, NULL
);
325 /* Initialise state machine */
326 if (st
->state
== MSG_FLOW_UNINITED
327 || st
->state
== MSG_FLOW_FINISHED
) {
328 if (st
->state
== MSG_FLOW_UNINITED
) {
329 st
->hand_state
= TLS_ST_BEFORE
;
330 st
->request_state
= TLS_ST_BEFORE
;
335 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
338 * Fatal errors in this block don't send an alert because we have
339 * failed to even initialise properly. Sending an alert is probably
343 if (SSL_IS_DTLS(s
)) {
344 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00) &&
345 (server
|| (s
->version
& 0xff00) != (DTLS1_BAD_VER
& 0xff00))) {
346 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
347 ERR_R_INTERNAL_ERROR
);
351 if ((s
->version
>> 8) != SSL3_VERSION_MAJOR
) {
352 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
353 ERR_R_INTERNAL_ERROR
);
358 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
359 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
360 ERR_R_INTERNAL_ERROR
);
364 if (s
->init_buf
== NULL
) {
365 if ((buf
= BUF_MEM_new()) == NULL
) {
366 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
367 ERR_R_INTERNAL_ERROR
);
370 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
371 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
372 ERR_R_INTERNAL_ERROR
);
379 if (!ssl3_setup_buffers(s
)) {
380 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
381 ERR_R_INTERNAL_ERROR
);
387 * Should have been reset by tls_process_finished, too.
389 s
->s3
->change_cipher_spec
= 0;
392 * Ok, we now need to push on a buffering BIO ...but not with
395 #ifndef OPENSSL_NO_SCTP
396 if (!SSL_IS_DTLS(s
) || !BIO_dgram_is_sctp(SSL_get_wbio(s
)))
398 if (!ssl_init_wbio_buffer(s
)) {
399 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
400 ERR_R_INTERNAL_ERROR
);
404 if ((SSL_in_before(s
))
406 if (!tls_setup_handshake(s
)) {
407 /* SSLfatal() already called */
411 if (SSL_IS_FIRST_HANDSHAKE(s
))
412 st
->read_state_first_init
= 1;
415 st
->state
= MSG_FLOW_WRITING
;
416 init_write_state_machine(s
);
419 while (st
->state
!= MSG_FLOW_FINISHED
) {
420 if (st
->state
== MSG_FLOW_READING
) {
421 ssret
= read_state_machine(s
);
422 if (ssret
== SUB_STATE_FINISHED
) {
423 st
->state
= MSG_FLOW_WRITING
;
424 init_write_state_machine(s
);
429 } else if (st
->state
== MSG_FLOW_WRITING
) {
430 ssret
= write_state_machine(s
);
431 if (ssret
== SUB_STATE_FINISHED
) {
432 st
->state
= MSG_FLOW_READING
;
433 init_read_state_machine(s
);
434 } else if (ssret
== SUB_STATE_END_HANDSHAKE
) {
435 st
->state
= MSG_FLOW_FINISHED
;
442 check_fatal(s
, SSL_F_STATE_MACHINE
);
443 SSLerr(SSL_F_STATE_MACHINE
, ERR_R_INTERNAL_ERROR
);
453 #ifndef OPENSSL_NO_SCTP
454 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
456 * Notify SCTP BIO socket to leave handshake mode and allow stream
457 * identifier other than 0.
459 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
460 st
->in_handshake
, NULL
);
467 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
469 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
475 * Initialise the MSG_FLOW_READING sub-state machine
477 static void init_read_state_machine(SSL
*s
)
479 OSSL_STATEM
*st
= &s
->statem
;
481 st
->read_state
= READ_STATE_HEADER
;
484 static int grow_init_buf(SSL
*s
, size_t size
) {
486 size_t msg_offset
= (char *)s
->init_msg
- s
->init_buf
->data
;
488 if (!BUF_MEM_grow_clean(s
->init_buf
, (int)size
))
491 if (size
< msg_offset
)
494 s
->init_msg
= s
->init_buf
->data
+ msg_offset
;
500 * This function implements the sub-state machine when the message flow is in
501 * MSG_FLOW_READING. The valid sub-states and transitions are:
503 * READ_STATE_HEADER <--+<-------------+
506 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
508 * +----------------------------+
510 * [SUB_STATE_FINISHED]
512 * READ_STATE_HEADER has the responsibility for reading in the message header
513 * and transitioning the state of the handshake state machine.
515 * READ_STATE_BODY reads in the rest of the message and then subsequently
518 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
519 * processing activity performed on the message may block.
521 * Any of the above states could result in an NBIO event occurring in which case
522 * control returns to the calling application. When this function is recalled we
523 * will resume in the same state where we left off.
525 static SUB_STATE_RETURN
read_state_machine(SSL
*s
)
527 OSSL_STATEM
*st
= &s
->statem
;
530 int (*transition
) (SSL
*s
, int mt
);
532 MSG_PROCESS_RETURN(*process_message
) (SSL
*s
, PACKET
*pkt
);
533 WORK_STATE(*post_process_message
) (SSL
*s
, WORK_STATE wst
);
534 size_t (*max_message_size
) (SSL
*s
);
535 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
537 cb
= get_callback(s
);
540 transition
= ossl_statem_server_read_transition
;
541 process_message
= ossl_statem_server_process_message
;
542 max_message_size
= ossl_statem_server_max_message_size
;
543 post_process_message
= ossl_statem_server_post_process_message
;
545 transition
= ossl_statem_client_read_transition
;
546 process_message
= ossl_statem_client_process_message
;
547 max_message_size
= ossl_statem_client_max_message_size
;
548 post_process_message
= ossl_statem_client_post_process_message
;
551 if (st
->read_state_first_init
) {
553 st
->read_state_first_init
= 0;
557 switch (st
->read_state
) {
558 case READ_STATE_HEADER
:
559 /* Get the state the peer wants to move to */
560 if (SSL_IS_DTLS(s
)) {
562 * In DTLS we get the whole message in one go - header and body
564 ret
= dtls_get_message(s
, &mt
, &len
);
566 ret
= tls_get_message_header(s
, &mt
);
570 /* Could be non-blocking IO */
571 return SUB_STATE_ERROR
;
575 /* Notify callback of an impending state change */
577 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
579 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
582 * Validate that we are allowed to move to the new state and move
583 * to that state if so
585 if (!transition(s
, mt
)) {
586 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
587 return SUB_STATE_ERROR
;
590 if (s
->s3
->tmp
.message_size
> max_message_size(s
)) {
591 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_READ_STATE_MACHINE
,
592 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
593 return SUB_STATE_ERROR
;
596 /* dtls_get_message already did this */
598 && s
->s3
->tmp
.message_size
> 0
599 && !grow_init_buf(s
, s
->s3
->tmp
.message_size
600 + SSL3_HM_HEADER_LENGTH
)) {
601 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
603 return SUB_STATE_ERROR
;
606 st
->read_state
= READ_STATE_BODY
;
609 case READ_STATE_BODY
:
610 if (!SSL_IS_DTLS(s
)) {
611 /* We already got this above for DTLS */
612 ret
= tls_get_message_body(s
, &len
);
614 /* Could be non-blocking IO */
615 return SUB_STATE_ERROR
;
620 if (!PACKET_buf_init(&pkt
, s
->init_msg
, len
)) {
621 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
622 ERR_R_INTERNAL_ERROR
);
623 return SUB_STATE_ERROR
;
625 ret
= process_message(s
, &pkt
);
627 /* Discard the packet data */
631 case MSG_PROCESS_ERROR
:
632 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
633 return SUB_STATE_ERROR
;
635 case MSG_PROCESS_FINISHED_READING
:
636 if (SSL_IS_DTLS(s
)) {
639 return SUB_STATE_FINISHED
;
641 case MSG_PROCESS_CONTINUE_PROCESSING
:
642 st
->read_state
= READ_STATE_POST_PROCESS
;
643 st
->read_state_work
= WORK_MORE_A
;
647 st
->read_state
= READ_STATE_HEADER
;
652 case READ_STATE_POST_PROCESS
:
653 st
->read_state_work
= post_process_message(s
, st
->read_state_work
);
654 switch (st
->read_state_work
) {
656 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
661 return SUB_STATE_ERROR
;
663 case WORK_FINISHED_CONTINUE
:
664 st
->read_state
= READ_STATE_HEADER
;
667 case WORK_FINISHED_STOP
:
668 if (SSL_IS_DTLS(s
)) {
671 return SUB_STATE_FINISHED
;
676 /* Shouldn't happen */
677 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
678 ERR_R_INTERNAL_ERROR
);
679 return SUB_STATE_ERROR
;
685 * Send a previously constructed message to the peer.
687 static int statem_do_write(SSL
*s
)
689 OSSL_STATEM
*st
= &s
->statem
;
691 if (st
->hand_state
== TLS_ST_CW_CHANGE
692 || st
->hand_state
== TLS_ST_SW_CHANGE
) {
694 return dtls1_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
696 return ssl3_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
698 return ssl_do_write(s
);
703 * Initialise the MSG_FLOW_WRITING sub-state machine
705 static void init_write_state_machine(SSL
*s
)
707 OSSL_STATEM
*st
= &s
->statem
;
709 st
->write_state
= WRITE_STATE_TRANSITION
;
713 * This function implements the sub-state machine when the message flow is in
714 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
716 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
719 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
725 * | WRITE_STATE_POST_WORK
729 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
731 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
732 * sending of the message. This could result in an NBIO event occurring in
733 * which case control returns to the calling application. When this function
734 * is recalled we will resume in the same state where we left off.
736 * WRITE_STATE_SEND sends the message and performs any work to be done after
739 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
740 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
741 * result in an NBIO event.
743 static SUB_STATE_RETURN
write_state_machine(SSL
*s
)
745 OSSL_STATEM
*st
= &s
->statem
;
747 WRITE_TRAN(*transition
) (SSL
*s
);
748 WORK_STATE(*pre_work
) (SSL
*s
, WORK_STATE wst
);
749 WORK_STATE(*post_work
) (SSL
*s
, WORK_STATE wst
);
750 int (*get_construct_message_f
) (SSL
*s
, WPACKET
*pkt
,
751 int (**confunc
) (SSL
*s
, WPACKET
*pkt
),
753 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
754 int (*confunc
) (SSL
*s
, WPACKET
*pkt
);
758 cb
= get_callback(s
);
761 transition
= ossl_statem_server_write_transition
;
762 pre_work
= ossl_statem_server_pre_work
;
763 post_work
= ossl_statem_server_post_work
;
764 get_construct_message_f
= ossl_statem_server_construct_message
;
766 transition
= ossl_statem_client_write_transition
;
767 pre_work
= ossl_statem_client_pre_work
;
768 post_work
= ossl_statem_client_post_work
;
769 get_construct_message_f
= ossl_statem_client_construct_message
;
773 switch (st
->write_state
) {
774 case WRITE_STATE_TRANSITION
:
776 /* Notify callback of an impending state change */
778 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
780 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
782 switch (transition(s
)) {
783 case WRITE_TRAN_CONTINUE
:
784 st
->write_state
= WRITE_STATE_PRE_WORK
;
785 st
->write_state_work
= WORK_MORE_A
;
788 case WRITE_TRAN_FINISHED
:
789 return SUB_STATE_FINISHED
;
792 case WRITE_TRAN_ERROR
:
793 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
794 return SUB_STATE_ERROR
;
798 case WRITE_STATE_PRE_WORK
:
799 switch (st
->write_state_work
= pre_work(s
, st
->write_state_work
)) {
801 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
806 return SUB_STATE_ERROR
;
808 case WORK_FINISHED_CONTINUE
:
809 st
->write_state
= WRITE_STATE_SEND
;
812 case WORK_FINISHED_STOP
:
813 return SUB_STATE_END_HANDSHAKE
;
815 if (!get_construct_message_f(s
, &pkt
, &confunc
, &mt
)) {
816 /* SSLfatal() already called */
817 return SUB_STATE_ERROR
;
819 if (mt
== SSL3_MT_DUMMY
) {
820 /* Skip construction and sending. This isn't a "real" state */
821 st
->write_state
= WRITE_STATE_POST_WORK
;
822 st
->write_state_work
= WORK_MORE_A
;
825 if (!WPACKET_init(&pkt
, s
->init_buf
)
826 || !ssl_set_handshake_header(s
, &pkt
, mt
)) {
827 WPACKET_cleanup(&pkt
);
828 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
829 ERR_R_INTERNAL_ERROR
);
830 return SUB_STATE_ERROR
;
832 if (confunc
!= NULL
&& !confunc(s
, &pkt
)) {
833 WPACKET_cleanup(&pkt
);
834 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
835 return SUB_STATE_ERROR
;
837 if (!ssl_close_construct_packet(s
, &pkt
, mt
)
838 || !WPACKET_finish(&pkt
)) {
839 WPACKET_cleanup(&pkt
);
840 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
841 ERR_R_INTERNAL_ERROR
);
842 return SUB_STATE_ERROR
;
847 case WRITE_STATE_SEND
:
848 if (SSL_IS_DTLS(s
) && st
->use_timer
) {
849 dtls1_start_timer(s
);
851 ret
= statem_do_write(s
);
853 return SUB_STATE_ERROR
;
855 st
->write_state
= WRITE_STATE_POST_WORK
;
856 st
->write_state_work
= WORK_MORE_A
;
859 case WRITE_STATE_POST_WORK
:
860 switch (st
->write_state_work
= post_work(s
, st
->write_state_work
)) {
862 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
867 return SUB_STATE_ERROR
;
869 case WORK_FINISHED_CONTINUE
:
870 st
->write_state
= WRITE_STATE_TRANSITION
;
873 case WORK_FINISHED_STOP
:
874 return SUB_STATE_END_HANDSHAKE
;
879 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
880 ERR_R_INTERNAL_ERROR
);
881 return SUB_STATE_ERROR
;
887 * Flush the write BIO
889 int statem_flush(SSL
*s
)
891 s
->rwstate
= SSL_WRITING
;
892 if (BIO_flush(s
->wbio
) <= 0) {
895 s
->rwstate
= SSL_NOTHING
;
901 * Called by the record layer to determine whether application data is
902 * allowed to be received in the current handshake state or not.
905 * 1: Yes (application data allowed)
906 * 0: No (application data not allowed)
908 int ossl_statem_app_data_allowed(SSL
*s
)
910 OSSL_STATEM
*st
= &s
->statem
;
912 if (st
->state
== MSG_FLOW_UNINITED
)
915 if (!s
->s3
->in_read_app_data
|| (s
->s3
->total_renegotiations
== 0))
920 * If we're a server and we haven't got as far as writing our
921 * ServerHello yet then we allow app data
923 if (st
->hand_state
== TLS_ST_BEFORE
924 || st
->hand_state
== TLS_ST_SR_CLNT_HELLO
)
928 * If we're a client and we haven't read the ServerHello yet then we
931 if (st
->hand_state
== TLS_ST_CW_CLNT_HELLO
)