2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 #if defined(__TANDEM) && defined(_SPT_MODEL_)
11 # include <spthread.h>
12 # include <spt_extensions.h> /* timeval */
15 #include "internal/cryptlib.h"
16 #include <openssl/rand.h>
17 #include "../ssl_local.h"
18 #include "statem_local.h"
22 * This file implements the SSL/TLS/DTLS state machines.
24 * There are two primary state machines:
26 * 1) Message flow state machine
27 * 2) Handshake state machine
29 * The Message flow state machine controls the reading and sending of messages
30 * including handling of non-blocking IO events, flushing of the underlying
31 * write BIO, handling unexpected messages, etc. It is itself broken into two
32 * separate sub-state machines which control reading and writing respectively.
34 * The Handshake state machine keeps track of the current SSL/TLS handshake
35 * state. Transitions of the handshake state are the result of events that
36 * occur within the Message flow state machine.
38 * Overall it looks like this:
40 * --------------------------------------------- -------------------
42 * | Message flow state machine | | |
44 * | -------------------- -------------------- | Transition | Handshake state |
45 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
46 * | | sub-state | | sub-state | |----------->| |
47 * | | machine for | | machine for | | | |
48 * | | reading messages | | writing messages | | | |
49 * | -------------------- -------------------- | | |
51 * --------------------------------------------- -------------------
55 /* Sub state machine return values */
57 /* Something bad happened or NBIO */
59 /* Sub state finished go to the next sub state */
61 /* Sub state finished and handshake was completed */
62 SUB_STATE_END_HANDSHAKE
65 static int state_machine(SSL_CONNECTION
*s
, int server
);
66 static void init_read_state_machine(SSL_CONNECTION
*s
);
67 static SUB_STATE_RETURN
read_state_machine(SSL_CONNECTION
*s
);
68 static void init_write_state_machine(SSL_CONNECTION
*s
);
69 static SUB_STATE_RETURN
write_state_machine(SSL_CONNECTION
*s
);
71 OSSL_HANDSHAKE_STATE
SSL_get_state(const SSL
*ssl
)
73 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
78 return sc
->statem
.hand_state
;
81 int SSL_in_init(const SSL
*s
)
83 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
88 return sc
->statem
.in_init
;
91 int SSL_is_init_finished(const SSL
*s
)
93 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
98 return !(sc
->statem
.in_init
) && (sc
->statem
.hand_state
== TLS_ST_OK
);
101 int SSL_in_before(const SSL
*s
)
103 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
109 * Historically being "in before" meant before anything had happened. In the
110 * current code though we remain in the "before" state for a while after we
111 * have started the handshake process (e.g. as a server waiting for the
112 * first message to arrive). There "in before" is taken to mean "in before"
113 * and not started any handshake process yet.
115 return (sc
->statem
.hand_state
== TLS_ST_BEFORE
)
116 && (sc
->statem
.state
== MSG_FLOW_UNINITED
);
120 * Clear the state machine state and reset back to MSG_FLOW_UNINITED
122 void ossl_statem_clear(SSL_CONNECTION
*s
)
124 s
->statem
.state
= MSG_FLOW_UNINITED
;
125 s
->statem
.hand_state
= TLS_ST_BEFORE
;
126 s
->statem
.in_init
= 1;
127 s
->statem
.no_cert_verify
= 0;
131 * Set the state machine up ready for a renegotiation handshake
133 void ossl_statem_set_renegotiate(SSL_CONNECTION
*s
)
135 s
->statem
.in_init
= 1;
136 s
->statem
.request_state
= TLS_ST_SW_HELLO_REQ
;
139 void ossl_statem_send_fatal(SSL_CONNECTION
*s
, int al
)
141 /* We shouldn't call SSLfatal() twice. Once is enough */
142 if (s
->statem
.in_init
&& s
->statem
.state
== MSG_FLOW_ERROR
)
144 s
->statem
.in_init
= 1;
145 s
->statem
.state
= MSG_FLOW_ERROR
;
146 if (al
!= SSL_AD_NO_ALERT
147 && s
->statem
.enc_write_state
!= ENC_WRITE_STATE_INVALID
)
148 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
152 * Error reporting building block that's used instead of ERR_set_error().
153 * In addition to what ERR_set_error() does, this puts the state machine
154 * into an error state and sends an alert if appropriate.
155 * This is a permanent error for the current connection.
157 void ossl_statem_fatal(SSL_CONNECTION
*s
, int al
, int reason
,
158 const char *fmt
, ...)
163 ERR_vset_error(ERR_LIB_SSL
, reason
, fmt
, args
);
166 ossl_statem_send_fatal(s
, al
);
170 * This macro should only be called if we are already expecting to be in
171 * a fatal error state. We verify that we are, and set it if not (this would
174 #define check_fatal(s) \
176 if (!ossl_assert((s)->statem.in_init \
177 && (s)->statem.state == MSG_FLOW_ERROR)) \
178 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
182 * Discover whether the current connection is in the error state.
184 * Valid return values are:
188 int ossl_statem_in_error(const SSL_CONNECTION
*s
)
190 if (s
->statem
.state
== MSG_FLOW_ERROR
)
196 void ossl_statem_set_in_init(SSL_CONNECTION
*s
, int init
)
198 s
->statem
.in_init
= init
;
201 int ossl_statem_get_in_handshake(SSL_CONNECTION
*s
)
203 return s
->statem
.in_handshake
;
206 void ossl_statem_set_in_handshake(SSL_CONNECTION
*s
, int inhand
)
209 s
->statem
.in_handshake
++;
211 s
->statem
.in_handshake
--;
214 /* Are we in a sensible state to skip over unreadable early data? */
215 int ossl_statem_skip_early_data(SSL_CONNECTION
*s
)
217 if (s
->ext
.early_data
!= SSL_EARLY_DATA_REJECTED
)
221 || s
->statem
.hand_state
!= TLS_ST_EARLY_DATA
222 || s
->hello_retry_request
== SSL_HRR_COMPLETE
)
229 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
230 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
231 * data state and whether we should attempt to move the handshake on if so.
232 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
233 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
236 void ossl_statem_check_finish_init(SSL_CONNECTION
*s
, int sending
)
239 if (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
240 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
) {
241 ossl_statem_set_in_init(s
, 1);
242 if (s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
) {
244 * SSL_connect() or SSL_do_handshake() has been called directly.
245 * We don't allow any more writing of early data.
247 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
250 } else if (!s
->server
) {
251 if ((sending
&& (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
252 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
253 && s
->early_data_state
!= SSL_EARLY_DATA_WRITING
)
254 || (!sending
&& s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)) {
255 ossl_statem_set_in_init(s
, 1);
257 * SSL_write() has been called directly. We don't allow any more
258 * writing of early data.
260 if (sending
&& s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
)
261 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
264 if (s
->early_data_state
== SSL_EARLY_DATA_FINISHED_READING
265 && s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
266 ossl_statem_set_in_init(s
, 1);
270 void ossl_statem_set_hello_verify_done(SSL_CONNECTION
*s
)
272 s
->statem
.state
= MSG_FLOW_UNINITED
;
273 s
->statem
.in_init
= 1;
275 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
276 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
277 * calls to SSL_in_before() will return false. Also calls to
278 * SSL_state_string() and SSL_state_string_long() will return something
281 s
->statem
.hand_state
= TLS_ST_SR_CLNT_HELLO
;
284 int ossl_statem_connect(SSL
*s
)
286 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
291 return state_machine(sc
, 0);
294 int ossl_statem_accept(SSL
*s
)
296 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
301 return state_machine(sc
, 1);
304 typedef void (*info_cb
) (const SSL
*, int, int);
306 static info_cb
get_callback(SSL_CONNECTION
*s
)
308 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
310 if (s
->info_callback
!= NULL
)
311 return s
->info_callback
;
312 else if (sctx
->info_callback
!= NULL
)
313 return sctx
->info_callback
;
319 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
320 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
321 * transitions are as follows:
323 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
325 * +-----------------------+
327 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
335 * We may exit at any point due to an error or NBIO event. If an NBIO event
336 * occurs then we restart at the point we left off when we are recalled.
337 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
339 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
340 * into that state at any point in the event that an irrecoverable error occurs.
342 * Valid return values are:
346 static int state_machine(SSL_CONNECTION
*s
, int server
)
349 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
350 OSSL_STATEM
*st
= &s
->statem
;
353 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
355 if (st
->state
== MSG_FLOW_ERROR
) {
356 /* Shouldn't have been called if we're already in the error state */
363 cb
= get_callback(s
);
366 if (!SSL_in_init(ssl
) || SSL_in_before(ssl
)) {
368 * If we are stateless then we already called SSL_clear() - don't do
369 * it again and clear the STATELESS flag itself.
371 if ((s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0 && !SSL_clear(ssl
))
374 #ifndef OPENSSL_NO_SCTP
375 if (SSL_CONNECTION_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(ssl
))) {
377 * Notify SCTP BIO socket to enter handshake mode and prevent stream
378 * identifier other than 0.
380 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
381 st
->in_handshake
, NULL
);
385 /* Initialise state machine */
386 if (st
->state
== MSG_FLOW_UNINITED
387 || st
->state
== MSG_FLOW_FINISHED
) {
388 if (st
->state
== MSG_FLOW_UNINITED
) {
389 st
->hand_state
= TLS_ST_BEFORE
;
390 st
->request_state
= TLS_ST_BEFORE
;
395 if (SSL_IS_FIRST_HANDSHAKE(s
) || !SSL_CONNECTION_IS_TLS13(s
))
396 cb(ssl
, SSL_CB_HANDSHAKE_START
, 1);
400 * Fatal errors in this block don't send an alert because we have
401 * failed to even initialise properly. Sending an alert is probably
405 if (SSL_CONNECTION_IS_DTLS(s
)) {
406 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00) &&
407 (server
|| (s
->version
& 0xff00) != (DTLS1_BAD_VER
& 0xff00))) {
408 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
412 if ((s
->version
>> 8) != SSL3_VERSION_MAJOR
) {
413 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
418 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
419 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
423 if (s
->init_buf
== NULL
) {
424 if ((buf
= BUF_MEM_new()) == NULL
) {
425 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
428 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
429 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
436 if (!ssl3_setup_buffers(s
)) {
437 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
443 * Should have been reset by tls_process_finished, too.
445 s
->s3
.change_cipher_spec
= 0;
448 * Ok, we now need to push on a buffering BIO ...but not with
451 #ifndef OPENSSL_NO_SCTP
452 if (!SSL_CONNECTION_IS_DTLS(s
) || !BIO_dgram_is_sctp(SSL_get_wbio(ssl
)))
454 if (!ssl_init_wbio_buffer(s
)) {
455 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
459 if ((SSL_in_before(ssl
))
461 if (!tls_setup_handshake(s
)) {
462 /* SSLfatal() already called */
466 if (SSL_IS_FIRST_HANDSHAKE(s
))
467 st
->read_state_first_init
= 1;
470 st
->state
= MSG_FLOW_WRITING
;
471 init_write_state_machine(s
);
474 while (st
->state
!= MSG_FLOW_FINISHED
) {
475 if (st
->state
== MSG_FLOW_READING
) {
476 ssret
= read_state_machine(s
);
477 if (ssret
== SUB_STATE_FINISHED
) {
478 st
->state
= MSG_FLOW_WRITING
;
479 init_write_state_machine(s
);
484 } else if (st
->state
== MSG_FLOW_WRITING
) {
485 ssret
= write_state_machine(s
);
486 if (ssret
== SUB_STATE_FINISHED
) {
487 st
->state
= MSG_FLOW_READING
;
488 init_read_state_machine(s
);
489 } else if (ssret
== SUB_STATE_END_HANDSHAKE
) {
490 st
->state
= MSG_FLOW_FINISHED
;
498 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
508 #ifndef OPENSSL_NO_SCTP
509 if (SSL_CONNECTION_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(ssl
))) {
511 * Notify SCTP BIO socket to leave handshake mode and allow stream
512 * identifier other than 0.
514 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
515 st
->in_handshake
, NULL
);
522 cb(ssl
, SSL_CB_ACCEPT_EXIT
, ret
);
524 cb(ssl
, SSL_CB_CONNECT_EXIT
, ret
);
530 * Initialise the MSG_FLOW_READING sub-state machine
532 static void init_read_state_machine(SSL_CONNECTION
*s
)
534 OSSL_STATEM
*st
= &s
->statem
;
536 st
->read_state
= READ_STATE_HEADER
;
539 static int grow_init_buf(SSL_CONNECTION
*s
, size_t size
) {
541 size_t msg_offset
= (char *)s
->init_msg
- s
->init_buf
->data
;
543 if (!BUF_MEM_grow_clean(s
->init_buf
, (int)size
))
546 if (size
< msg_offset
)
549 s
->init_msg
= s
->init_buf
->data
+ msg_offset
;
555 * This function implements the sub-state machine when the message flow is in
556 * MSG_FLOW_READING. The valid sub-states and transitions are:
558 * READ_STATE_HEADER <--+<-------------+
561 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
563 * +----------------------------+
565 * [SUB_STATE_FINISHED]
567 * READ_STATE_HEADER has the responsibility for reading in the message header
568 * and transitioning the state of the handshake state machine.
570 * READ_STATE_BODY reads in the rest of the message and then subsequently
573 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
574 * processing activity performed on the message may block.
576 * Any of the above states could result in an NBIO event occurring in which case
577 * control returns to the calling application. When this function is recalled we
578 * will resume in the same state where we left off.
580 static SUB_STATE_RETURN
read_state_machine(SSL_CONNECTION
*s
)
582 OSSL_STATEM
*st
= &s
->statem
;
585 int (*transition
) (SSL_CONNECTION
*s
, int mt
);
587 MSG_PROCESS_RETURN(*process_message
) (SSL_CONNECTION
*s
, PACKET
*pkt
);
588 WORK_STATE(*post_process_message
) (SSL_CONNECTION
*s
, WORK_STATE wst
);
589 size_t (*max_message_size
) (SSL_CONNECTION
*s
);
590 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
591 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
593 cb
= get_callback(s
);
596 transition
= ossl_statem_server_read_transition
;
597 process_message
= ossl_statem_server_process_message
;
598 max_message_size
= ossl_statem_server_max_message_size
;
599 post_process_message
= ossl_statem_server_post_process_message
;
601 transition
= ossl_statem_client_read_transition
;
602 process_message
= ossl_statem_client_process_message
;
603 max_message_size
= ossl_statem_client_max_message_size
;
604 post_process_message
= ossl_statem_client_post_process_message
;
607 if (st
->read_state_first_init
) {
609 st
->read_state_first_init
= 0;
613 switch (st
->read_state
) {
614 case READ_STATE_HEADER
:
615 /* Get the state the peer wants to move to */
616 if (SSL_CONNECTION_IS_DTLS(s
)) {
618 * In DTLS we get the whole message in one go - header and body
620 ret
= dtls_get_message(s
, &mt
);
622 ret
= tls_get_message_header(s
, &mt
);
626 /* Could be non-blocking IO */
627 return SUB_STATE_ERROR
;
631 /* Notify callback of an impending state change */
633 cb(ssl
, SSL_CB_ACCEPT_LOOP
, 1);
635 cb(ssl
, SSL_CB_CONNECT_LOOP
, 1);
638 * Validate that we are allowed to move to the new state and move
639 * to that state if so
641 if (!transition(s
, mt
))
642 return SUB_STATE_ERROR
;
644 if (s
->s3
.tmp
.message_size
> max_message_size(s
)) {
645 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
646 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
647 return SUB_STATE_ERROR
;
650 /* dtls_get_message already did this */
651 if (!SSL_CONNECTION_IS_DTLS(s
)
652 && s
->s3
.tmp
.message_size
> 0
653 && !grow_init_buf(s
, s
->s3
.tmp
.message_size
654 + SSL3_HM_HEADER_LENGTH
)) {
655 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BUF_LIB
);
656 return SUB_STATE_ERROR
;
659 st
->read_state
= READ_STATE_BODY
;
662 case READ_STATE_BODY
:
663 if (SSL_CONNECTION_IS_DTLS(s
)) {
665 * Actually we already have the body, but we give DTLS the
666 * opportunity to do any further processing.
668 ret
= dtls_get_message_body(s
, &len
);
670 ret
= tls_get_message_body(s
, &len
);
673 /* Could be non-blocking IO */
674 return SUB_STATE_ERROR
;
678 if (!PACKET_buf_init(&pkt
, s
->init_msg
, len
)) {
679 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
680 return SUB_STATE_ERROR
;
682 ret
= process_message(s
, &pkt
);
684 /* Discard the packet data */
688 case MSG_PROCESS_ERROR
:
690 return SUB_STATE_ERROR
;
692 case MSG_PROCESS_FINISHED_READING
:
693 if (SSL_CONNECTION_IS_DTLS(s
)) {
696 return SUB_STATE_FINISHED
;
698 case MSG_PROCESS_CONTINUE_PROCESSING
:
699 st
->read_state
= READ_STATE_POST_PROCESS
;
700 st
->read_state_work
= WORK_MORE_A
;
704 st
->read_state
= READ_STATE_HEADER
;
709 case READ_STATE_POST_PROCESS
:
710 st
->read_state_work
= post_process_message(s
, st
->read_state_work
);
711 switch (st
->read_state_work
) {
718 return SUB_STATE_ERROR
;
720 case WORK_FINISHED_CONTINUE
:
721 st
->read_state
= READ_STATE_HEADER
;
724 case WORK_FINISHED_STOP
:
725 if (SSL_CONNECTION_IS_DTLS(s
)) {
728 return SUB_STATE_FINISHED
;
733 /* Shouldn't happen */
734 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
735 return SUB_STATE_ERROR
;
741 * Send a previously constructed message to the peer.
743 static int statem_do_write(SSL_CONNECTION
*s
)
745 OSSL_STATEM
*st
= &s
->statem
;
747 if (st
->hand_state
== TLS_ST_CW_CHANGE
748 || st
->hand_state
== TLS_ST_SW_CHANGE
) {
749 if (SSL_CONNECTION_IS_DTLS(s
))
750 return dtls1_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
752 return ssl3_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
754 return ssl_do_write(s
);
759 * Initialise the MSG_FLOW_WRITING sub-state machine
761 static void init_write_state_machine(SSL_CONNECTION
*s
)
763 OSSL_STATEM
*st
= &s
->statem
;
765 st
->write_state
= WRITE_STATE_TRANSITION
;
769 * This function implements the sub-state machine when the message flow is in
770 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
772 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
775 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
781 * | WRITE_STATE_POST_WORK
785 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
787 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
788 * sending of the message. This could result in an NBIO event occurring in
789 * which case control returns to the calling application. When this function
790 * is recalled we will resume in the same state where we left off.
792 * WRITE_STATE_SEND sends the message and performs any work to be done after
795 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
796 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
797 * result in an NBIO event.
799 static SUB_STATE_RETURN
write_state_machine(SSL_CONNECTION
*s
)
801 OSSL_STATEM
*st
= &s
->statem
;
803 WRITE_TRAN(*transition
) (SSL_CONNECTION
*s
);
804 WORK_STATE(*pre_work
) (SSL_CONNECTION
*s
, WORK_STATE wst
);
805 WORK_STATE(*post_work
) (SSL_CONNECTION
*s
, WORK_STATE wst
);
806 int (*get_construct_message_f
) (SSL_CONNECTION
*s
,
807 int (**confunc
) (SSL_CONNECTION
*s
,
810 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
811 int (*confunc
) (SSL_CONNECTION
*s
, WPACKET
*pkt
);
814 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
816 cb
= get_callback(s
);
819 transition
= ossl_statem_server_write_transition
;
820 pre_work
= ossl_statem_server_pre_work
;
821 post_work
= ossl_statem_server_post_work
;
822 get_construct_message_f
= ossl_statem_server_construct_message
;
824 transition
= ossl_statem_client_write_transition
;
825 pre_work
= ossl_statem_client_pre_work
;
826 post_work
= ossl_statem_client_post_work
;
827 get_construct_message_f
= ossl_statem_client_construct_message
;
831 switch (st
->write_state
) {
832 case WRITE_STATE_TRANSITION
:
834 /* Notify callback of an impending state change */
836 cb(ssl
, SSL_CB_ACCEPT_LOOP
, 1);
838 cb(ssl
, SSL_CB_CONNECT_LOOP
, 1);
840 switch (transition(s
)) {
841 case WRITE_TRAN_CONTINUE
:
842 st
->write_state
= WRITE_STATE_PRE_WORK
;
843 st
->write_state_work
= WORK_MORE_A
;
846 case WRITE_TRAN_FINISHED
:
847 return SUB_STATE_FINISHED
;
850 case WRITE_TRAN_ERROR
:
852 return SUB_STATE_ERROR
;
856 case WRITE_STATE_PRE_WORK
:
857 switch (st
->write_state_work
= pre_work(s
, st
->write_state_work
)) {
864 return SUB_STATE_ERROR
;
866 case WORK_FINISHED_CONTINUE
:
867 st
->write_state
= WRITE_STATE_SEND
;
870 case WORK_FINISHED_STOP
:
871 return SUB_STATE_END_HANDSHAKE
;
873 if (!get_construct_message_f(s
, &confunc
, &mt
)) {
874 /* SSLfatal() already called */
875 return SUB_STATE_ERROR
;
877 if (mt
== SSL3_MT_DUMMY
) {
878 /* Skip construction and sending. This isn't a "real" state */
879 st
->write_state
= WRITE_STATE_POST_WORK
;
880 st
->write_state_work
= WORK_MORE_A
;
883 if (!WPACKET_init(&pkt
, s
->init_buf
)
884 || !ssl_set_handshake_header(s
, &pkt
, mt
)) {
885 WPACKET_cleanup(&pkt
);
886 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
887 return SUB_STATE_ERROR
;
889 if (confunc
!= NULL
&& !confunc(s
, &pkt
)) {
890 WPACKET_cleanup(&pkt
);
892 return SUB_STATE_ERROR
;
894 if (!ssl_close_construct_packet(s
, &pkt
, mt
)
895 || !WPACKET_finish(&pkt
)) {
896 WPACKET_cleanup(&pkt
);
897 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
898 return SUB_STATE_ERROR
;
903 case WRITE_STATE_SEND
:
904 if (SSL_CONNECTION_IS_DTLS(s
) && st
->use_timer
) {
905 dtls1_start_timer(s
);
907 ret
= statem_do_write(s
);
909 return SUB_STATE_ERROR
;
911 st
->write_state
= WRITE_STATE_POST_WORK
;
912 st
->write_state_work
= WORK_MORE_A
;
915 case WRITE_STATE_POST_WORK
:
916 switch (st
->write_state_work
= post_work(s
, st
->write_state_work
)) {
923 return SUB_STATE_ERROR
;
925 case WORK_FINISHED_CONTINUE
:
926 st
->write_state
= WRITE_STATE_TRANSITION
;
929 case WORK_FINISHED_STOP
:
930 return SUB_STATE_END_HANDSHAKE
;
935 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
936 return SUB_STATE_ERROR
;
942 * Flush the write BIO
944 int statem_flush(SSL_CONNECTION
*s
)
946 s
->rwstate
= SSL_WRITING
;
947 if (BIO_flush(s
->wbio
) <= 0) {
950 s
->rwstate
= SSL_NOTHING
;
956 * Called by the record layer to determine whether application data is
957 * allowed to be received in the current handshake state or not.
960 * 1: Yes (application data allowed)
961 * 0: No (application data not allowed)
963 int ossl_statem_app_data_allowed(SSL_CONNECTION
*s
)
965 OSSL_STATEM
*st
= &s
->statem
;
967 if (st
->state
== MSG_FLOW_UNINITED
)
970 if (!s
->s3
.in_read_app_data
|| (s
->s3
.total_renegotiations
== 0))
975 * If we're a server and we haven't got as far as writing our
976 * ServerHello yet then we allow app data
978 if (st
->hand_state
== TLS_ST_BEFORE
979 || st
->hand_state
== TLS_ST_SR_CLNT_HELLO
)
983 * If we're a client and we haven't read the ServerHello yet then we
986 if (st
->hand_state
== TLS_ST_CW_CLNT_HELLO
)
994 * This function returns 1 if TLS exporter is ready to export keying
995 * material, or 0 if otherwise.
997 int ossl_statem_export_allowed(SSL_CONNECTION
*s
)
999 return s
->s3
.previous_server_finished_len
!= 0
1000 && s
->statem
.hand_state
!= TLS_ST_SW_FINISHED
;
1004 * Return 1 if early TLS exporter is ready to export keying material,
1005 * or 0 if otherwise.
1007 int ossl_statem_export_early_allowed(SSL_CONNECTION
*s
)
1010 * The early exporter secret is only present on the server if we
1011 * have accepted early_data. It is present on the client as long
1012 * as we have sent early_data.
1014 return s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
1015 || (!s
->server
&& s
->ext
.early_data
!= SSL_EARLY_DATA_NOT_SENT
);