2 * Copyright 2015-2018 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"
17 * This file implements the SSL/TLS/DTLS state machines.
19 * There are two primary state machines:
21 * 1) Message flow state machine
22 * 2) Handshake state machine
24 * The Message flow state machine controls the reading and sending of messages
25 * including handling of non-blocking IO events, flushing of the underlying
26 * write BIO, handling unexpected messages, etc. It is itself broken into two
27 * separate sub-state machines which control reading and writing respectively.
29 * The Handshake state machine keeps track of the current SSL/TLS handshake
30 * state. Transitions of the handshake state are the result of events that
31 * occur within the Message flow state machine.
33 * Overall it looks like this:
35 * --------------------------------------------- -------------------
37 * | Message flow state machine | | |
39 * | -------------------- -------------------- | Transition | Handshake state |
40 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
41 * | | sub-state | | sub-state | |----------->| |
42 * | | machine for | | machine for | | | |
43 * | | reading messages | | writing messages | | | |
44 * | -------------------- -------------------- | | |
46 * --------------------------------------------- -------------------
50 /* Sub state machine return values */
52 /* Something bad happened or NBIO */
54 /* Sub state finished go to the next sub state */
56 /* Sub state finished and handshake was completed */
57 SUB_STATE_END_HANDSHAKE
60 static int state_machine(SSL
*s
, int server
);
61 static void init_read_state_machine(SSL
*s
);
62 static SUB_STATE_RETURN
read_state_machine(SSL
*s
);
63 static void init_write_state_machine(SSL
*s
);
64 static SUB_STATE_RETURN
write_state_machine(SSL
*s
);
66 OSSL_HANDSHAKE_STATE
SSL_get_state(const SSL
*ssl
)
68 return ssl
->statem
.hand_state
;
71 int SSL_in_init(const SSL
*s
)
73 return s
->statem
.in_init
;
76 int SSL_is_init_finished(const SSL
*s
)
78 return !(s
->statem
.in_init
) && (s
->statem
.hand_state
== TLS_ST_OK
);
81 int SSL_in_before(const SSL
*s
)
84 * Historically being "in before" meant before anything had happened. In the
85 * current code though we remain in the "before" state for a while after we
86 * have started the handshake process (e.g. as a server waiting for the
87 * first message to arrive). There "in before" is taken to mean "in before"
88 * and not started any handshake process yet.
90 return (s
->statem
.hand_state
== TLS_ST_BEFORE
)
91 && (s
->statem
.state
== MSG_FLOW_UNINITED
);
95 * Clear the state machine state and reset back to MSG_FLOW_UNINITED
97 void ossl_statem_clear(SSL
*s
)
99 s
->statem
.state
= MSG_FLOW_UNINITED
;
100 s
->statem
.hand_state
= TLS_ST_BEFORE
;
101 s
->statem
.in_init
= 1;
102 s
->statem
.no_cert_verify
= 0;
106 * Set the state machine up ready for a renegotiation handshake
108 void ossl_statem_set_renegotiate(SSL
*s
)
110 s
->statem
.in_init
= 1;
111 s
->statem
.request_state
= TLS_ST_SW_HELLO_REQ
;
115 * Put the state machine into an error state and send an alert if appropriate.
116 * This is a permanent error for the current connection.
118 void ossl_statem_fatal(SSL
*s
, int al
, int func
, int reason
, const char *file
,
121 /* We shouldn't call SSLfatal() twice. Once is enough */
122 assert(s
->statem
.state
!= MSG_FLOW_ERROR
);
123 s
->statem
.in_init
= 1;
124 s
->statem
.state
= MSG_FLOW_ERROR
;
125 ERR_put_error(ERR_LIB_SSL
, func
, reason
, file
, line
);
126 if (al
!= SSL_AD_NO_ALERT
&& !s
->statem
.invalid_enc_write_ctx
)
127 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
131 * This macro should only be called if we are already expecting to be in
132 * a fatal error state. We verify that we are, and set it if not (this would
135 #define check_fatal(s, f) \
137 if (!ossl_assert((s)->statem.in_init \
138 && (s)->statem.state == MSG_FLOW_ERROR)) \
139 SSLfatal(s, SSL_AD_INTERNAL_ERROR, (f), \
140 SSL_R_MISSING_FATAL); \
144 * Discover whether the current connection is in the error state.
146 * Valid return values are:
150 int ossl_statem_in_error(const SSL
*s
)
152 if (s
->statem
.state
== MSG_FLOW_ERROR
)
158 void ossl_statem_set_in_init(SSL
*s
, int init
)
160 s
->statem
.in_init
= init
;
163 int ossl_statem_get_in_handshake(SSL
*s
)
165 return s
->statem
.in_handshake
;
168 void ossl_statem_set_in_handshake(SSL
*s
, int inhand
)
171 s
->statem
.in_handshake
++;
173 s
->statem
.in_handshake
--;
176 /* Are we in a sensible state to skip over unreadable early data? */
177 int ossl_statem_skip_early_data(SSL
*s
)
179 if (s
->ext
.early_data
!= SSL_EARLY_DATA_REJECTED
)
183 || s
->statem
.hand_state
!= TLS_ST_EARLY_DATA
184 || s
->hello_retry_request
== SSL_HRR_COMPLETE
)
191 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
192 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
193 * data state and whether we should attempt to move the handshake on if so.
194 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
195 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
198 void ossl_statem_check_finish_init(SSL
*s
, int sending
)
201 if (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
202 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
) {
203 ossl_statem_set_in_init(s
, 1);
204 if (s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
) {
206 * SSL_connect() or SSL_do_handshake() has been called directly.
207 * We don't allow any more writing of early data.
209 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
212 } else if (!s
->server
) {
213 if ((sending
&& (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
214 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
215 && s
->early_data_state
!= SSL_EARLY_DATA_WRITING
)
216 || (!sending
&& s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)) {
217 ossl_statem_set_in_init(s
, 1);
219 * SSL_write() has been called directly. We don't allow any more
220 * writing of early data.
222 if (sending
&& s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
)
223 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
226 if (s
->early_data_state
== SSL_EARLY_DATA_FINISHED_READING
227 && s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
228 ossl_statem_set_in_init(s
, 1);
232 void ossl_statem_set_hello_verify_done(SSL
*s
)
234 s
->statem
.state
= MSG_FLOW_UNINITED
;
235 s
->statem
.in_init
= 1;
237 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
238 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
239 * calls to SSL_in_before() will return false. Also calls to
240 * SSL_state_string() and SSL_state_string_long() will return something
243 s
->statem
.hand_state
= TLS_ST_SR_CLNT_HELLO
;
246 int ossl_statem_connect(SSL
*s
)
248 return state_machine(s
, 0);
251 int ossl_statem_accept(SSL
*s
)
253 return state_machine(s
, 1);
256 typedef void (*info_cb
) (const SSL
*, int, int);
258 static info_cb
get_callback(SSL
*s
)
260 if (s
->info_callback
!= NULL
)
261 return s
->info_callback
;
262 else if (s
->ctx
->info_callback
!= NULL
)
263 return s
->ctx
->info_callback
;
269 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
270 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
271 * transitions are as follows:
273 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
275 * +-----------------------+
277 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
285 * We may exit at any point due to an error or NBIO event. If an NBIO event
286 * occurs then we restart at the point we left off when we are recalled.
287 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
289 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
290 * into that state at any point in the event that an irrecoverable error occurs.
292 * Valid return values are:
296 static int state_machine(SSL
*s
, int server
)
299 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
300 OSSL_STATEM
*st
= &s
->statem
;
304 if (st
->state
== MSG_FLOW_ERROR
) {
305 /* Shouldn't have been called if we're already in the error state */
312 cb
= get_callback(s
);
315 if (!SSL_in_init(s
) || SSL_in_before(s
)) {
317 * If we are stateless then we already called SSL_clear() - don't do
318 * it again and clear the STATELESS flag itself.
320 if ((s
->s3
->flags
& TLS1_FLAGS_STATELESS
) == 0 && !SSL_clear(s
))
323 #ifndef OPENSSL_NO_SCTP
324 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
326 * Notify SCTP BIO socket to enter handshake mode and prevent stream
327 * identifier other than 0.
329 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
330 st
->in_handshake
, NULL
);
334 /* Initialise state machine */
335 if (st
->state
== MSG_FLOW_UNINITED
336 || st
->state
== MSG_FLOW_FINISHED
) {
337 if (st
->state
== MSG_FLOW_UNINITED
) {
338 st
->hand_state
= TLS_ST_BEFORE
;
339 st
->request_state
= TLS_ST_BEFORE
;
344 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
347 * Fatal errors in this block don't send an alert because we have
348 * failed to even initialise properly. Sending an alert is probably
352 if (SSL_IS_DTLS(s
)) {
353 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00) &&
354 (server
|| (s
->version
& 0xff00) != (DTLS1_BAD_VER
& 0xff00))) {
355 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
356 ERR_R_INTERNAL_ERROR
);
360 if ((s
->version
>> 8) != SSL3_VERSION_MAJOR
) {
361 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
362 ERR_R_INTERNAL_ERROR
);
367 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
368 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
369 ERR_R_INTERNAL_ERROR
);
373 if (s
->init_buf
== NULL
) {
374 if ((buf
= BUF_MEM_new()) == NULL
) {
375 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
376 ERR_R_INTERNAL_ERROR
);
379 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
380 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
381 ERR_R_INTERNAL_ERROR
);
388 if (!ssl3_setup_buffers(s
)) {
389 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
390 ERR_R_INTERNAL_ERROR
);
396 * Should have been reset by tls_process_finished, too.
398 s
->s3
->change_cipher_spec
= 0;
401 * Ok, we now need to push on a buffering BIO ...but not with
404 #ifndef OPENSSL_NO_SCTP
405 if (!SSL_IS_DTLS(s
) || !BIO_dgram_is_sctp(SSL_get_wbio(s
)))
407 if (!ssl_init_wbio_buffer(s
)) {
408 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
409 ERR_R_INTERNAL_ERROR
);
413 if ((SSL_in_before(s
))
415 if (!tls_setup_handshake(s
)) {
416 /* SSLfatal() already called */
420 if (SSL_IS_FIRST_HANDSHAKE(s
))
421 st
->read_state_first_init
= 1;
424 st
->state
= MSG_FLOW_WRITING
;
425 init_write_state_machine(s
);
428 while (st
->state
!= MSG_FLOW_FINISHED
) {
429 if (st
->state
== MSG_FLOW_READING
) {
430 ssret
= read_state_machine(s
);
431 if (ssret
== SUB_STATE_FINISHED
) {
432 st
->state
= MSG_FLOW_WRITING
;
433 init_write_state_machine(s
);
438 } else if (st
->state
== MSG_FLOW_WRITING
) {
439 ssret
= write_state_machine(s
);
440 if (ssret
== SUB_STATE_FINISHED
) {
441 st
->state
= MSG_FLOW_READING
;
442 init_read_state_machine(s
);
443 } else if (ssret
== SUB_STATE_END_HANDSHAKE
) {
444 st
->state
= MSG_FLOW_FINISHED
;
451 check_fatal(s
, SSL_F_STATE_MACHINE
);
452 SSLerr(SSL_F_STATE_MACHINE
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
462 #ifndef OPENSSL_NO_SCTP
463 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
465 * Notify SCTP BIO socket to leave handshake mode and allow stream
466 * identifier other than 0.
468 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
469 st
->in_handshake
, NULL
);
476 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
478 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
484 * Initialise the MSG_FLOW_READING sub-state machine
486 static void init_read_state_machine(SSL
*s
)
488 OSSL_STATEM
*st
= &s
->statem
;
490 st
->read_state
= READ_STATE_HEADER
;
493 static int grow_init_buf(SSL
*s
, size_t size
) {
495 size_t msg_offset
= (char *)s
->init_msg
- s
->init_buf
->data
;
497 if (!BUF_MEM_grow_clean(s
->init_buf
, (int)size
))
500 if (size
< msg_offset
)
503 s
->init_msg
= s
->init_buf
->data
+ msg_offset
;
509 * This function implements the sub-state machine when the message flow is in
510 * MSG_FLOW_READING. The valid sub-states and transitions are:
512 * READ_STATE_HEADER <--+<-------------+
515 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
517 * +----------------------------+
519 * [SUB_STATE_FINISHED]
521 * READ_STATE_HEADER has the responsibility for reading in the message header
522 * and transitioning the state of the handshake state machine.
524 * READ_STATE_BODY reads in the rest of the message and then subsequently
527 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
528 * processing activity performed on the message may block.
530 * Any of the above states could result in an NBIO event occurring in which case
531 * control returns to the calling application. When this function is recalled we
532 * will resume in the same state where we left off.
534 static SUB_STATE_RETURN
read_state_machine(SSL
*s
)
536 OSSL_STATEM
*st
= &s
->statem
;
539 int (*transition
) (SSL
*s
, int mt
);
541 MSG_PROCESS_RETURN(*process_message
) (SSL
*s
, PACKET
*pkt
);
542 WORK_STATE(*post_process_message
) (SSL
*s
, WORK_STATE wst
);
543 size_t (*max_message_size
) (SSL
*s
);
544 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
546 cb
= get_callback(s
);
549 transition
= ossl_statem_server_read_transition
;
550 process_message
= ossl_statem_server_process_message
;
551 max_message_size
= ossl_statem_server_max_message_size
;
552 post_process_message
= ossl_statem_server_post_process_message
;
554 transition
= ossl_statem_client_read_transition
;
555 process_message
= ossl_statem_client_process_message
;
556 max_message_size
= ossl_statem_client_max_message_size
;
557 post_process_message
= ossl_statem_client_post_process_message
;
560 if (st
->read_state_first_init
) {
562 st
->read_state_first_init
= 0;
566 switch (st
->read_state
) {
567 case READ_STATE_HEADER
:
568 /* Get the state the peer wants to move to */
569 if (SSL_IS_DTLS(s
)) {
571 * In DTLS we get the whole message in one go - header and body
573 ret
= dtls_get_message(s
, &mt
, &len
);
575 ret
= tls_get_message_header(s
, &mt
);
579 /* Could be non-blocking IO */
580 return SUB_STATE_ERROR
;
584 /* Notify callback of an impending state change */
586 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
588 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
591 * Validate that we are allowed to move to the new state and move
592 * to that state if so
594 if (!transition(s
, mt
))
595 return SUB_STATE_ERROR
;
597 if (s
->s3
->tmp
.message_size
> max_message_size(s
)) {
598 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_READ_STATE_MACHINE
,
599 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
600 return SUB_STATE_ERROR
;
603 /* dtls_get_message already did this */
605 && s
->s3
->tmp
.message_size
> 0
606 && !grow_init_buf(s
, s
->s3
->tmp
.message_size
607 + SSL3_HM_HEADER_LENGTH
)) {
608 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
610 return SUB_STATE_ERROR
;
613 st
->read_state
= READ_STATE_BODY
;
616 case READ_STATE_BODY
:
617 if (!SSL_IS_DTLS(s
)) {
618 /* We already got this above for DTLS */
619 ret
= tls_get_message_body(s
, &len
);
621 /* Could be non-blocking IO */
622 return SUB_STATE_ERROR
;
627 if (!PACKET_buf_init(&pkt
, s
->init_msg
, len
)) {
628 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
629 ERR_R_INTERNAL_ERROR
);
630 return SUB_STATE_ERROR
;
632 ret
= process_message(s
, &pkt
);
634 /* Discard the packet data */
638 case MSG_PROCESS_ERROR
:
639 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
640 return SUB_STATE_ERROR
;
642 case MSG_PROCESS_FINISHED_READING
:
643 if (SSL_IS_DTLS(s
)) {
646 return SUB_STATE_FINISHED
;
648 case MSG_PROCESS_CONTINUE_PROCESSING
:
649 st
->read_state
= READ_STATE_POST_PROCESS
;
650 st
->read_state_work
= WORK_MORE_A
;
654 st
->read_state
= READ_STATE_HEADER
;
659 case READ_STATE_POST_PROCESS
:
660 st
->read_state_work
= post_process_message(s
, st
->read_state_work
);
661 switch (st
->read_state_work
) {
663 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
668 return SUB_STATE_ERROR
;
670 case WORK_FINISHED_CONTINUE
:
671 st
->read_state
= READ_STATE_HEADER
;
674 case WORK_FINISHED_STOP
:
675 if (SSL_IS_DTLS(s
)) {
678 return SUB_STATE_FINISHED
;
683 /* Shouldn't happen */
684 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
685 ERR_R_INTERNAL_ERROR
);
686 return SUB_STATE_ERROR
;
692 * Send a previously constructed message to the peer.
694 static int statem_do_write(SSL
*s
)
696 OSSL_STATEM
*st
= &s
->statem
;
698 if (st
->hand_state
== TLS_ST_CW_CHANGE
699 || st
->hand_state
== TLS_ST_SW_CHANGE
) {
701 return dtls1_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
703 return ssl3_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
705 return ssl_do_write(s
);
710 * Initialise the MSG_FLOW_WRITING sub-state machine
712 static void init_write_state_machine(SSL
*s
)
714 OSSL_STATEM
*st
= &s
->statem
;
716 st
->write_state
= WRITE_STATE_TRANSITION
;
720 * This function implements the sub-state machine when the message flow is in
721 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
723 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
726 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
732 * | WRITE_STATE_POST_WORK
736 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
738 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
739 * sending of the message. This could result in an NBIO event occurring in
740 * which case control returns to the calling application. When this function
741 * is recalled we will resume in the same state where we left off.
743 * WRITE_STATE_SEND sends the message and performs any work to be done after
746 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
747 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
748 * result in an NBIO event.
750 static SUB_STATE_RETURN
write_state_machine(SSL
*s
)
752 OSSL_STATEM
*st
= &s
->statem
;
754 WRITE_TRAN(*transition
) (SSL
*s
);
755 WORK_STATE(*pre_work
) (SSL
*s
, WORK_STATE wst
);
756 WORK_STATE(*post_work
) (SSL
*s
, WORK_STATE wst
);
757 int (*get_construct_message_f
) (SSL
*s
, WPACKET
*pkt
,
758 int (**confunc
) (SSL
*s
, WPACKET
*pkt
),
760 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
761 int (*confunc
) (SSL
*s
, WPACKET
*pkt
);
765 cb
= get_callback(s
);
768 transition
= ossl_statem_server_write_transition
;
769 pre_work
= ossl_statem_server_pre_work
;
770 post_work
= ossl_statem_server_post_work
;
771 get_construct_message_f
= ossl_statem_server_construct_message
;
773 transition
= ossl_statem_client_write_transition
;
774 pre_work
= ossl_statem_client_pre_work
;
775 post_work
= ossl_statem_client_post_work
;
776 get_construct_message_f
= ossl_statem_client_construct_message
;
780 switch (st
->write_state
) {
781 case WRITE_STATE_TRANSITION
:
783 /* Notify callback of an impending state change */
785 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
787 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
789 switch (transition(s
)) {
790 case WRITE_TRAN_CONTINUE
:
791 st
->write_state
= WRITE_STATE_PRE_WORK
;
792 st
->write_state_work
= WORK_MORE_A
;
795 case WRITE_TRAN_FINISHED
:
796 return SUB_STATE_FINISHED
;
799 case WRITE_TRAN_ERROR
:
800 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
801 return SUB_STATE_ERROR
;
805 case WRITE_STATE_PRE_WORK
:
806 switch (st
->write_state_work
= pre_work(s
, st
->write_state_work
)) {
808 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
813 return SUB_STATE_ERROR
;
815 case WORK_FINISHED_CONTINUE
:
816 st
->write_state
= WRITE_STATE_SEND
;
819 case WORK_FINISHED_STOP
:
820 return SUB_STATE_END_HANDSHAKE
;
822 if (!get_construct_message_f(s
, &pkt
, &confunc
, &mt
)) {
823 /* SSLfatal() already called */
824 return SUB_STATE_ERROR
;
826 if (mt
== SSL3_MT_DUMMY
) {
827 /* Skip construction and sending. This isn't a "real" state */
828 st
->write_state
= WRITE_STATE_POST_WORK
;
829 st
->write_state_work
= WORK_MORE_A
;
832 if (!WPACKET_init(&pkt
, s
->init_buf
)
833 || !ssl_set_handshake_header(s
, &pkt
, mt
)) {
834 WPACKET_cleanup(&pkt
);
835 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
836 ERR_R_INTERNAL_ERROR
);
837 return SUB_STATE_ERROR
;
839 if (confunc
!= NULL
&& !confunc(s
, &pkt
)) {
840 WPACKET_cleanup(&pkt
);
841 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
842 return SUB_STATE_ERROR
;
844 if (!ssl_close_construct_packet(s
, &pkt
, mt
)
845 || !WPACKET_finish(&pkt
)) {
846 WPACKET_cleanup(&pkt
);
847 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
848 ERR_R_INTERNAL_ERROR
);
849 return SUB_STATE_ERROR
;
854 case WRITE_STATE_SEND
:
855 if (SSL_IS_DTLS(s
) && st
->use_timer
) {
856 dtls1_start_timer(s
);
858 ret
= statem_do_write(s
);
860 return SUB_STATE_ERROR
;
862 st
->write_state
= WRITE_STATE_POST_WORK
;
863 st
->write_state_work
= WORK_MORE_A
;
866 case WRITE_STATE_POST_WORK
:
867 switch (st
->write_state_work
= post_work(s
, st
->write_state_work
)) {
869 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
874 return SUB_STATE_ERROR
;
876 case WORK_FINISHED_CONTINUE
:
877 st
->write_state
= WRITE_STATE_TRANSITION
;
880 case WORK_FINISHED_STOP
:
881 return SUB_STATE_END_HANDSHAKE
;
886 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
887 ERR_R_INTERNAL_ERROR
);
888 return SUB_STATE_ERROR
;
894 * Flush the write BIO
896 int statem_flush(SSL
*s
)
898 s
->rwstate
= SSL_WRITING
;
899 if (BIO_flush(s
->wbio
) <= 0) {
902 s
->rwstate
= SSL_NOTHING
;
908 * Called by the record layer to determine whether application data is
909 * allowed to be received in the current handshake state or not.
912 * 1: Yes (application data allowed)
913 * 0: No (application data not allowed)
915 int ossl_statem_app_data_allowed(SSL
*s
)
917 OSSL_STATEM
*st
= &s
->statem
;
919 if (st
->state
== MSG_FLOW_UNINITED
)
922 if (!s
->s3
->in_read_app_data
|| (s
->s3
->total_renegotiations
== 0))
927 * If we're a server and we haven't got as far as writing our
928 * ServerHello yet then we allow app data
930 if (st
->hand_state
== TLS_ST_BEFORE
931 || st
->hand_state
== TLS_ST_SR_CLNT_HELLO
)
935 * If we're a client and we haven't read the ServerHello yet then we
938 if (st
->hand_state
== TLS_ST_CW_CLNT_HELLO
)
946 * This function returns 1 if TLS exporter is ready to export keying
947 * material, or 0 if otherwise.
949 int ossl_statem_export_allowed(SSL
*s
)
951 return s
->s3
->previous_server_finished_len
!= 0
952 && s
->statem
.hand_state
!= TLS_ST_SW_FINISHED
;
956 * Return 1 if early TLS exporter is ready to export keying material,
959 int ossl_statem_export_early_allowed(SSL
*s
)
962 * The early exporter secret is only present on the server if we
963 * have accepted early_data. It is present on the client as long
964 * as we have sent early_data.
966 return s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
967 || (!s
->server
&& s
->ext
.early_data
!= SSL_EARLY_DATA_NOT_SENT
);