2 * Copyright 2015-2025 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 #include "internal/e_os.h"
12 #if defined(__TANDEM) && defined(_SPT_MODEL_)
13 # include <spthread.h>
14 # include <spt_extensions.h> /* timeval */
17 #include "internal/cryptlib.h"
18 #include "internal/ssl_unwrap.h"
19 #include <openssl/rand.h>
20 #include "../ssl_local.h"
21 #include "statem_local.h"
25 * This file implements the SSL/TLS/DTLS state machines.
27 * There are two primary state machines:
29 * 1) Message flow state machine
30 * 2) Handshake state machine
32 * The Message flow state machine controls the reading and sending of messages
33 * including handling of non-blocking IO events, flushing of the underlying
34 * write BIO, handling unexpected messages, etc. It is itself broken into two
35 * separate sub-state machines which control reading and writing respectively.
37 * The Handshake state machine keeps track of the current SSL/TLS handshake
38 * state. Transitions of the handshake state are the result of events that
39 * occur within the Message flow state machine.
41 * Overall it looks like this:
43 * --------------------------------------------- -------------------
45 * | Message flow state machine | | |
47 * | -------------------- -------------------- | Transition | Handshake state |
48 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
49 * | | sub-state | | sub-state | |----------->| |
50 * | | machine for | | machine for | | | |
51 * | | reading messages | | writing messages | | | |
52 * | -------------------- -------------------- | | |
54 * --------------------------------------------- -------------------
58 /* Sub state machine return values */
60 /* Something bad happened or NBIO */
62 /* Sub state finished go to the next sub state */
64 /* Sub state finished and handshake was completed */
65 SUB_STATE_END_HANDSHAKE
68 static int state_machine(SSL_CONNECTION
*s
, int server
);
69 static void init_read_state_machine(SSL_CONNECTION
*s
);
70 static SUB_STATE_RETURN
read_state_machine(SSL_CONNECTION
*s
);
71 static void init_write_state_machine(SSL_CONNECTION
*s
);
72 static SUB_STATE_RETURN
write_state_machine(SSL_CONNECTION
*s
);
74 OSSL_HANDSHAKE_STATE
SSL_get_state(const SSL
*ssl
)
76 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
81 return sc
->statem
.hand_state
;
84 int SSL_in_init(const SSL
*s
)
86 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
91 return sc
->statem
.in_init
;
94 int SSL_is_init_finished(const SSL
*s
)
96 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
101 return !(sc
->statem
.in_init
) && (sc
->statem
.hand_state
== TLS_ST_OK
);
104 int SSL_in_before(const SSL
*s
)
106 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
112 * Historically being "in before" meant before anything had happened. In the
113 * current code though we remain in the "before" state for a while after we
114 * have started the handshake process (e.g. as a server waiting for the
115 * first message to arrive). There "in before" is taken to mean "in before"
116 * and not started any handshake process yet.
118 return (sc
->statem
.hand_state
== TLS_ST_BEFORE
)
119 && (sc
->statem
.state
== MSG_FLOW_UNINITED
);
122 OSSL_HANDSHAKE_STATE
ossl_statem_get_state(SSL_CONNECTION
*s
)
124 return s
!= NULL
? s
->statem
.hand_state
: TLS_ST_BEFORE
;
128 * Clear the state machine state and reset back to MSG_FLOW_UNINITED
130 void ossl_statem_clear(SSL_CONNECTION
*s
)
132 s
->statem
.state
= MSG_FLOW_UNINITED
;
133 s
->statem
.hand_state
= TLS_ST_BEFORE
;
134 ossl_statem_set_in_init(s
, 1);
135 s
->statem
.no_cert_verify
= 0;
139 * Set the state machine up ready for a renegotiation handshake
141 void ossl_statem_set_renegotiate(SSL_CONNECTION
*s
)
143 ossl_statem_set_in_init(s
, 1);
144 s
->statem
.request_state
= TLS_ST_SW_HELLO_REQ
;
147 void ossl_statem_send_fatal(SSL_CONNECTION
*s
, int al
)
149 /* We shouldn't call SSLfatal() twice. Once is enough */
150 if (s
->statem
.in_init
&& s
->statem
.state
== MSG_FLOW_ERROR
)
152 ossl_statem_set_in_init(s
, 1);
153 s
->statem
.state
= MSG_FLOW_ERROR
;
154 if (al
!= SSL_AD_NO_ALERT
)
155 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
159 * Error reporting building block that's used instead of ERR_set_error().
160 * In addition to what ERR_set_error() does, this puts the state machine
161 * into an error state and sends an alert if appropriate.
162 * This is a permanent error for the current connection.
164 void ossl_statem_fatal(SSL_CONNECTION
*s
, int al
, int reason
,
165 const char *fmt
, ...)
170 ERR_vset_error(ERR_LIB_SSL
, reason
, fmt
, args
);
173 ossl_statem_send_fatal(s
, al
);
177 * This macro should only be called if we are already expecting to be in
178 * a fatal error state. We verify that we are, and set it if not (this would
181 #define check_fatal(s) \
183 if (!ossl_assert((s)->statem.in_init \
184 && (s)->statem.state == MSG_FLOW_ERROR)) \
185 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
189 * Discover whether the current connection is in the error state.
191 * Valid return values are:
195 int ossl_statem_in_error(const SSL_CONNECTION
*s
)
197 if (s
->statem
.state
== MSG_FLOW_ERROR
)
203 void ossl_statem_set_in_init(SSL_CONNECTION
*s
, int init
)
205 s
->statem
.in_init
= init
;
206 if (s
->rlayer
.rrlmethod
!= NULL
&& s
->rlayer
.rrlmethod
->set_in_init
!= NULL
)
207 s
->rlayer
.rrlmethod
->set_in_init(s
->rlayer
.rrl
, init
);
210 int ossl_statem_get_in_handshake(SSL_CONNECTION
*s
)
212 return s
->statem
.in_handshake
;
215 void ossl_statem_set_in_handshake(SSL_CONNECTION
*s
, int inhand
)
218 s
->statem
.in_handshake
++;
220 s
->statem
.in_handshake
--;
223 /* Are we in a sensible state to skip over unreadable early data? */
224 int ossl_statem_skip_early_data(SSL_CONNECTION
*s
)
226 if (s
->ext
.early_data
!= SSL_EARLY_DATA_REJECTED
)
230 || s
->statem
.hand_state
!= TLS_ST_EARLY_DATA
231 || s
->hello_retry_request
== SSL_HRR_COMPLETE
)
238 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
239 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
240 * data state and whether we should attempt to move the handshake on if so.
241 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
242 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
245 int ossl_statem_check_finish_init(SSL_CONNECTION
*s
, int sending
)
248 if (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
249 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
) {
250 ossl_statem_set_in_init(s
, 1);
251 if (s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
) {
253 * SSL_connect() or SSL_do_handshake() has been called directly.
254 * We don't allow any more writing of early data.
256 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
259 } else if (!s
->server
) {
260 if ((sending
&& (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
261 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
262 && s
->early_data_state
!= SSL_EARLY_DATA_WRITING
)
263 || (!sending
&& s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)) {
264 ossl_statem_set_in_init(s
, 1);
266 * SSL_write() has been called directly. We don't allow any more
267 * writing of early data.
269 if (sending
&& s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
)
270 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
273 if (s
->early_data_state
== SSL_EARLY_DATA_FINISHED_READING
274 && s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
275 ossl_statem_set_in_init(s
, 1);
280 void ossl_statem_set_hello_verify_done(SSL_CONNECTION
*s
)
282 s
->statem
.state
= MSG_FLOW_UNINITED
;
283 ossl_statem_set_in_init(s
, 1);
285 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
286 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
287 * calls to SSL_in_before() will return false. Also calls to
288 * SSL_state_string() and SSL_state_string_long() will return something
291 s
->statem
.hand_state
= TLS_ST_SR_CLNT_HELLO
;
294 int ossl_statem_connect(SSL
*s
)
296 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
301 return state_machine(sc
, 0);
304 int ossl_statem_accept(SSL
*s
)
306 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
311 return state_machine(sc
, 1);
314 typedef void (*info_cb
) (const SSL
*, int, int);
316 static info_cb
get_callback(SSL_CONNECTION
*s
)
318 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
320 if (s
->info_callback
!= NULL
)
321 return s
->info_callback
;
322 else if (sctx
->info_callback
!= NULL
)
323 return sctx
->info_callback
;
329 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
330 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
331 * transitions are as follows:
333 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
335 * +-----------------------+
337 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
345 * We may exit at any point due to an error or NBIO event. If an NBIO event
346 * occurs then we restart at the point we left off when we are recalled.
347 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
349 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
350 * into that state at any point in the event that an irrecoverable error occurs.
352 * Valid return values are:
356 static int state_machine(SSL_CONNECTION
*s
, int server
)
359 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
360 OSSL_STATEM
*st
= &s
->statem
;
363 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
364 SSL
*ussl
= SSL_CONNECTION_GET_USER_SSL(s
);
366 if (st
->state
== MSG_FLOW_ERROR
) {
367 /* Shouldn't have been called if we're already in the error state */
374 cb
= get_callback(s
);
377 if (!SSL_in_init(ssl
) || SSL_in_before(ssl
)) {
379 * If we are stateless then we already called SSL_clear() - don't do
380 * it again and clear the STATELESS flag itself.
382 if ((s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0 && !SSL_clear(ssl
))
385 #ifndef OPENSSL_NO_SCTP
386 if (SSL_CONNECTION_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(ssl
))) {
388 * Notify SCTP BIO socket to enter handshake mode and prevent stream
389 * identifier other than 0.
391 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
392 st
->in_handshake
, NULL
);
396 /* Initialise state machine */
397 if (st
->state
== MSG_FLOW_UNINITED
398 || st
->state
== MSG_FLOW_FINISHED
) {
399 if (st
->state
== MSG_FLOW_UNINITED
) {
400 st
->hand_state
= TLS_ST_BEFORE
;
401 st
->request_state
= TLS_ST_BEFORE
;
406 if (SSL_IS_FIRST_HANDSHAKE(s
) || !SSL_CONNECTION_IS_TLS13(s
))
407 cb(ussl
, SSL_CB_HANDSHAKE_START
, 1);
411 * Fatal errors in this block don't send an alert because we have
412 * failed to even initialise properly. Sending an alert is probably
416 if (SSL_CONNECTION_IS_DTLS(s
)) {
417 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00) &&
418 (server
|| (s
->version
& 0xff00) != (DTLS1_BAD_VER
& 0xff00))) {
419 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
423 if ((s
->version
>> 8) != SSL3_VERSION_MAJOR
) {
424 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
429 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
430 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
434 if (s
->init_buf
== NULL
) {
435 if ((buf
= BUF_MEM_new()) == NULL
) {
436 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
439 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
440 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
450 * Should have been reset by tls_process_finished, too.
452 s
->s3
.change_cipher_spec
= 0;
455 * Ok, we now need to push on a buffering BIO ...but not with
458 #ifndef OPENSSL_NO_SCTP
459 if (!SSL_CONNECTION_IS_DTLS(s
) || !BIO_dgram_is_sctp(SSL_get_wbio(ssl
)))
461 if (!ssl_init_wbio_buffer(s
)) {
462 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
466 if ((SSL_in_before(ssl
))
468 if (!tls_setup_handshake(s
)) {
469 /* SSLfatal() already called */
473 if (SSL_IS_FIRST_HANDSHAKE(s
))
474 st
->read_state_first_init
= 1;
477 st
->state
= MSG_FLOW_WRITING
;
478 init_write_state_machine(s
);
481 while (st
->state
!= MSG_FLOW_FINISHED
) {
482 if (st
->state
== MSG_FLOW_READING
) {
483 ssret
= read_state_machine(s
);
484 if (ssret
== SUB_STATE_FINISHED
) {
485 st
->state
= MSG_FLOW_WRITING
;
486 init_write_state_machine(s
);
491 } else if (st
->state
== MSG_FLOW_WRITING
) {
492 ssret
= write_state_machine(s
);
493 if (ssret
== SUB_STATE_FINISHED
) {
494 st
->state
= MSG_FLOW_READING
;
495 init_read_state_machine(s
);
496 } else if (ssret
== SUB_STATE_END_HANDSHAKE
) {
497 st
->state
= MSG_FLOW_FINISHED
;
505 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
515 #ifndef OPENSSL_NO_SCTP
516 if (SSL_CONNECTION_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(ssl
))) {
518 * Notify SCTP BIO socket to leave handshake mode and allow stream
519 * identifier other than 0.
521 BIO_ctrl(SSL_get_wbio(ssl
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
522 st
->in_handshake
, NULL
);
529 cb(ussl
, SSL_CB_ACCEPT_EXIT
, ret
);
531 cb(ussl
, SSL_CB_CONNECT_EXIT
, ret
);
537 * Initialise the MSG_FLOW_READING sub-state machine
539 static void init_read_state_machine(SSL_CONNECTION
*s
)
541 OSSL_STATEM
*st
= &s
->statem
;
543 st
->read_state
= READ_STATE_HEADER
;
546 static int grow_init_buf(SSL_CONNECTION
*s
, size_t size
) {
548 size_t msg_offset
= (char *)s
->init_msg
- s
->init_buf
->data
;
550 if (!BUF_MEM_grow_clean(s
->init_buf
, (int)size
))
553 if (size
< msg_offset
)
556 s
->init_msg
= s
->init_buf
->data
+ msg_offset
;
562 * This function implements the sub-state machine when the message flow is in
563 * MSG_FLOW_READING. The valid sub-states and transitions are:
565 * READ_STATE_HEADER <--+<-------------+
568 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
570 * +----------------------------+
572 * [SUB_STATE_FINISHED]
574 * READ_STATE_HEADER has the responsibility for reading in the message header
575 * and transitioning the state of the handshake state machine.
577 * READ_STATE_BODY reads in the rest of the message and then subsequently
580 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
581 * processing activity performed on the message may block.
583 * Any of the above states could result in an NBIO event occurring in which case
584 * control returns to the calling application. When this function is recalled we
585 * will resume in the same state where we left off.
587 static SUB_STATE_RETURN
read_state_machine(SSL_CONNECTION
*s
)
589 OSSL_STATEM
*st
= &s
->statem
;
592 int (*transition
) (SSL_CONNECTION
*s
, int mt
);
594 MSG_PROCESS_RETURN(*process_message
) (SSL_CONNECTION
*s
, PACKET
*pkt
);
595 WORK_STATE(*post_process_message
) (SSL_CONNECTION
*s
, WORK_STATE wst
);
596 size_t (*max_message_size
) (SSL_CONNECTION
*s
);
597 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
598 SSL
*ssl
= SSL_CONNECTION_GET_USER_SSL(s
);
600 cb
= get_callback(s
);
603 transition
= ossl_statem_server_read_transition
;
604 process_message
= ossl_statem_server_process_message
;
605 max_message_size
= ossl_statem_server_max_message_size
;
606 post_process_message
= ossl_statem_server_post_process_message
;
608 transition
= ossl_statem_client_read_transition
;
609 process_message
= ossl_statem_client_process_message
;
610 max_message_size
= ossl_statem_client_max_message_size
;
611 post_process_message
= ossl_statem_client_post_process_message
;
614 if (st
->read_state_first_init
) {
616 st
->read_state_first_init
= 0;
620 switch (st
->read_state
) {
621 case READ_STATE_HEADER
:
622 /* Get the state the peer wants to move to */
623 if (SSL_CONNECTION_IS_DTLS(s
)) {
625 * In DTLS we get the whole message in one go - header and body
627 ret
= dtls_get_message(s
, &mt
);
629 ret
= tls_get_message_header(s
, &mt
);
633 /* Could be non-blocking IO */
634 return SUB_STATE_ERROR
;
638 /* Notify callback of an impending state change */
640 cb(ssl
, SSL_CB_ACCEPT_LOOP
, 1);
642 cb(ssl
, SSL_CB_CONNECT_LOOP
, 1);
645 * Validate that we are allowed to move to the new state and move
646 * to that state if so
648 if (!transition(s
, mt
))
649 return SUB_STATE_ERROR
;
651 if (s
->s3
.tmp
.message_size
> max_message_size(s
)) {
652 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
653 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
654 return SUB_STATE_ERROR
;
657 /* dtls_get_message already did this */
658 if (!SSL_CONNECTION_IS_DTLS(s
)
659 && s
->s3
.tmp
.message_size
> 0
660 && !grow_init_buf(s
, s
->s3
.tmp
.message_size
661 + SSL3_HM_HEADER_LENGTH
)) {
662 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BUF_LIB
);
663 return SUB_STATE_ERROR
;
666 st
->read_state
= READ_STATE_BODY
;
669 case READ_STATE_BODY
:
670 if (SSL_CONNECTION_IS_DTLS(s
)) {
672 * Actually we already have the body, but we give DTLS the
673 * opportunity to do any further processing.
675 ret
= dtls_get_message_body(s
, &len
);
677 ret
= tls_get_message_body(s
, &len
);
680 /* Could be non-blocking IO */
681 return SUB_STATE_ERROR
;
685 if (!PACKET_buf_init(&pkt
, s
->init_msg
, len
)) {
686 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
687 return SUB_STATE_ERROR
;
689 ret
= process_message(s
, &pkt
);
691 /* Discard the packet data */
695 case MSG_PROCESS_ERROR
:
697 return SUB_STATE_ERROR
;
699 case MSG_PROCESS_FINISHED_READING
:
700 if (SSL_CONNECTION_IS_DTLS(s
)) {
703 return SUB_STATE_FINISHED
;
705 case MSG_PROCESS_CONTINUE_PROCESSING
:
706 st
->read_state
= READ_STATE_POST_PROCESS
;
707 st
->read_state_work
= WORK_MORE_A
;
711 st
->read_state
= READ_STATE_HEADER
;
716 case READ_STATE_POST_PROCESS
:
717 st
->read_state_work
= post_process_message(s
, st
->read_state_work
);
718 switch (st
->read_state_work
) {
725 return SUB_STATE_ERROR
;
727 case WORK_FINISHED_CONTINUE
:
728 st
->read_state
= READ_STATE_HEADER
;
731 case WORK_FINISHED_SWAP
:
732 case WORK_FINISHED_STOP
:
733 if (SSL_CONNECTION_IS_DTLS(s
)) {
736 return SUB_STATE_FINISHED
;
741 /* Shouldn't happen */
742 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
743 return SUB_STATE_ERROR
;
749 * Send a previously constructed message to the peer.
751 static int statem_do_write(SSL_CONNECTION
*s
)
753 OSSL_STATEM
*st
= &s
->statem
;
755 if (st
->hand_state
== TLS_ST_CW_CHANGE
756 || st
->hand_state
== TLS_ST_SW_CHANGE
) {
757 if (SSL_CONNECTION_IS_DTLS(s
))
758 return dtls1_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
760 return ssl3_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
762 return ssl_do_write(s
);
767 * Initialise the MSG_FLOW_WRITING sub-state machine
769 static void init_write_state_machine(SSL_CONNECTION
*s
)
771 OSSL_STATEM
*st
= &s
->statem
;
773 st
->write_state
= WRITE_STATE_TRANSITION
;
777 * This function implements the sub-state machine when the message flow is in
778 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
780 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
783 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
789 * | WRITE_STATE_POST_WORK
793 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
795 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
796 * sending of the message. This could result in an NBIO event occurring in
797 * which case control returns to the calling application. When this function
798 * is recalled we will resume in the same state where we left off.
800 * WRITE_STATE_SEND sends the message and performs any work to be done after
803 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
804 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
805 * result in an NBIO event.
807 static SUB_STATE_RETURN
write_state_machine(SSL_CONNECTION
*s
)
809 OSSL_STATEM
*st
= &s
->statem
;
811 WRITE_TRAN(*transition
) (SSL_CONNECTION
*s
);
812 WORK_STATE(*pre_work
) (SSL_CONNECTION
*s
, WORK_STATE wst
);
813 WORK_STATE(*post_work
) (SSL_CONNECTION
*s
, WORK_STATE wst
);
814 int (*get_construct_message_f
) (SSL_CONNECTION
*s
,
815 CON_FUNC_RETURN (**confunc
) (SSL_CONNECTION
*s
,
818 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
819 CON_FUNC_RETURN (*confunc
) (SSL_CONNECTION
*s
, WPACKET
*pkt
);
822 SSL
*ssl
= SSL_CONNECTION_GET_USER_SSL(s
);
824 cb
= get_callback(s
);
827 transition
= ossl_statem_server_write_transition
;
828 pre_work
= ossl_statem_server_pre_work
;
829 post_work
= ossl_statem_server_post_work
;
830 get_construct_message_f
= ossl_statem_server_construct_message
;
832 transition
= ossl_statem_client_write_transition
;
833 pre_work
= ossl_statem_client_pre_work
;
834 post_work
= ossl_statem_client_post_work
;
835 get_construct_message_f
= ossl_statem_client_construct_message
;
839 switch (st
->write_state
) {
840 case WRITE_STATE_TRANSITION
:
842 /* Notify callback of an impending state change */
844 cb(ssl
, SSL_CB_ACCEPT_LOOP
, 1);
846 cb(ssl
, SSL_CB_CONNECT_LOOP
, 1);
848 switch (transition(s
)) {
849 case WRITE_TRAN_CONTINUE
:
850 st
->write_state
= WRITE_STATE_PRE_WORK
;
851 st
->write_state_work
= WORK_MORE_A
;
854 case WRITE_TRAN_FINISHED
:
855 return SUB_STATE_FINISHED
;
857 case WRITE_TRAN_ERROR
:
859 return SUB_STATE_ERROR
;
863 case WRITE_STATE_PRE_WORK
:
864 switch (st
->write_state_work
= pre_work(s
, st
->write_state_work
)) {
871 return SUB_STATE_ERROR
;
873 case WORK_FINISHED_CONTINUE
:
874 st
->write_state
= WRITE_STATE_SEND
;
877 case WORK_FINISHED_SWAP
:
878 return SUB_STATE_FINISHED
;
880 case WORK_FINISHED_STOP
:
881 return SUB_STATE_END_HANDSHAKE
;
883 if (!get_construct_message_f(s
, &confunc
, &mt
)) {
884 /* SSLfatal() already called */
885 return SUB_STATE_ERROR
;
887 if (mt
== SSL3_MT_DUMMY
) {
888 /* Skip construction and sending. This isn't a "real" state */
889 st
->write_state
= WRITE_STATE_POST_WORK
;
890 st
->write_state_work
= WORK_MORE_A
;
893 if (!WPACKET_init(&pkt
, s
->init_buf
)
894 || !ssl_set_handshake_header(s
, &pkt
, mt
)) {
895 WPACKET_cleanup(&pkt
);
896 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
897 return SUB_STATE_ERROR
;
899 if (confunc
!= NULL
) {
900 CON_FUNC_RETURN tmpret
;
902 tmpret
= confunc(s
, &pkt
);
903 if (tmpret
== CON_FUNC_ERROR
) {
904 WPACKET_cleanup(&pkt
);
906 return SUB_STATE_ERROR
;
907 } else if (tmpret
== CON_FUNC_DONT_SEND
) {
909 * The construction function decided not to construct the
910 * message after all and continue. Skip sending.
912 WPACKET_cleanup(&pkt
);
913 st
->write_state
= WRITE_STATE_POST_WORK
;
914 st
->write_state_work
= WORK_MORE_A
;
918 if (!ssl_close_construct_packet(s
, &pkt
, mt
)
919 || !WPACKET_finish(&pkt
)) {
920 WPACKET_cleanup(&pkt
);
921 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
922 return SUB_STATE_ERROR
;
927 case WRITE_STATE_SEND
:
928 if (SSL_CONNECTION_IS_DTLS(s
) && st
->use_timer
) {
929 dtls1_start_timer(s
);
931 ret
= statem_do_write(s
);
933 return SUB_STATE_ERROR
;
935 st
->write_state
= WRITE_STATE_POST_WORK
;
936 st
->write_state_work
= WORK_MORE_A
;
939 case WRITE_STATE_POST_WORK
:
940 switch (st
->write_state_work
= post_work(s
, st
->write_state_work
)) {
947 return SUB_STATE_ERROR
;
949 case WORK_FINISHED_CONTINUE
:
950 st
->write_state
= WRITE_STATE_TRANSITION
;
953 case WORK_FINISHED_SWAP
:
954 return SUB_STATE_FINISHED
;
956 case WORK_FINISHED_STOP
:
957 return SUB_STATE_END_HANDSHAKE
;
962 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
963 return SUB_STATE_ERROR
;
969 * Flush the write BIO
971 int statem_flush(SSL_CONNECTION
*s
)
973 s
->rwstate
= SSL_WRITING
;
974 if (BIO_flush(s
->wbio
) <= 0) {
977 s
->rwstate
= SSL_NOTHING
;
983 * Called by the record layer to determine whether application data is
984 * allowed to be received in the current handshake state or not.
987 * 1: Yes (application data allowed)
988 * 0: No (application data not allowed)
990 int ossl_statem_app_data_allowed(SSL_CONNECTION
*s
)
992 OSSL_STATEM
*st
= &s
->statem
;
994 if (st
->state
== MSG_FLOW_UNINITED
)
997 if (!s
->s3
.in_read_app_data
|| (s
->s3
.total_renegotiations
== 0))
1002 * If we're a server and we haven't got as far as writing our
1003 * ServerHello yet then we allow app data
1005 if (st
->hand_state
== TLS_ST_BEFORE
1006 || st
->hand_state
== TLS_ST_SR_CLNT_HELLO
)
1010 * If we're a client and we haven't read the ServerHello yet then we
1013 if (st
->hand_state
== TLS_ST_CW_CLNT_HELLO
)
1021 * This function returns 1 if TLS exporter is ready to export keying
1022 * material, or 0 if otherwise.
1024 int ossl_statem_export_allowed(SSL_CONNECTION
*s
)
1026 return s
->s3
.previous_server_finished_len
!= 0
1027 && s
->statem
.hand_state
!= TLS_ST_SW_FINISHED
;
1031 * Return 1 if early TLS exporter is ready to export keying material,
1032 * or 0 if otherwise.
1034 int ossl_statem_export_early_allowed(SSL_CONNECTION
*s
)
1037 * The early exporter secret is only present on the server if we
1038 * have accepted early_data. It is present on the client as long
1039 * as we have sent early_data.
1041 return s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
1042 || (!s
->server
&& s
->ext
.early_data
!= SSL_EARLY_DATA_NOT_SENT
);