2 * Copyright 2015-2021 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/cryptlib.h"
11 #include <openssl/rand.h>
12 #include "../ssl_local.h"
13 #include "statem_local.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
;
114 void ossl_statem_send_fatal(SSL
*s
, int al
)
116 /* We shouldn't call SSLfatal() twice. Once is enough */
117 if (s
->statem
.in_init
&& s
->statem
.state
== MSG_FLOW_ERROR
)
119 s
->statem
.in_init
= 1;
120 s
->statem
.state
= MSG_FLOW_ERROR
;
121 if (al
!= SSL_AD_NO_ALERT
122 && s
->statem
.enc_write_state
!= ENC_WRITE_STATE_INVALID
)
123 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
127 * Error reporting building block that's used instead of ERR_set_error().
128 * In addition to what ERR_set_error() does, this puts the state machine
129 * into an error state and sends an alert if appropriate.
130 * This is a permanent error for the current connection.
132 void ossl_statem_fatal(SSL
*s
, int al
, int reason
, const char *fmt
, ...)
137 ERR_vset_error(ERR_LIB_SSL
, reason
, fmt
, args
);
140 ossl_statem_send_fatal(s
, al
);
144 * This macro should only be called if we are already expecting to be in
145 * a fatal error state. We verify that we are, and set it if not (this would
148 #define check_fatal(s) \
150 if (!ossl_assert((s)->statem.in_init \
151 && (s)->statem.state == MSG_FLOW_ERROR)) \
152 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
156 * Discover whether the current connection is in the error state.
158 * Valid return values are:
162 int ossl_statem_in_error(const SSL
*s
)
164 if (s
->statem
.state
== MSG_FLOW_ERROR
)
170 void ossl_statem_set_in_init(SSL
*s
, int init
)
172 s
->statem
.in_init
= init
;
175 int ossl_statem_get_in_handshake(SSL
*s
)
177 return s
->statem
.in_handshake
;
180 void ossl_statem_set_in_handshake(SSL
*s
, int inhand
)
183 s
->statem
.in_handshake
++;
185 s
->statem
.in_handshake
--;
188 /* Are we in a sensible state to skip over unreadable early data? */
189 int ossl_statem_skip_early_data(SSL
*s
)
191 if (s
->ext
.early_data
!= SSL_EARLY_DATA_REJECTED
)
195 || s
->statem
.hand_state
!= TLS_ST_EARLY_DATA
196 || s
->hello_retry_request
== SSL_HRR_COMPLETE
)
203 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
204 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
205 * data state and whether we should attempt to move the handshake on if so.
206 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
207 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
210 void ossl_statem_check_finish_init(SSL
*s
, int sending
)
213 if (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
214 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
) {
215 ossl_statem_set_in_init(s
, 1);
216 if (s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
) {
218 * SSL_connect() or SSL_do_handshake() has been called directly.
219 * We don't allow any more writing of early data.
221 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
224 } else if (!s
->server
) {
225 if ((sending
&& (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
226 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
227 && s
->early_data_state
!= SSL_EARLY_DATA_WRITING
)
228 || (!sending
&& s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)) {
229 ossl_statem_set_in_init(s
, 1);
231 * SSL_write() has been called directly. We don't allow any more
232 * writing of early data.
234 if (sending
&& s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
)
235 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
238 if (s
->early_data_state
== SSL_EARLY_DATA_FINISHED_READING
239 && s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
240 ossl_statem_set_in_init(s
, 1);
244 void ossl_statem_set_hello_verify_done(SSL
*s
)
246 s
->statem
.state
= MSG_FLOW_UNINITED
;
247 s
->statem
.in_init
= 1;
249 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
250 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
251 * calls to SSL_in_before() will return false. Also calls to
252 * SSL_state_string() and SSL_state_string_long() will return something
255 s
->statem
.hand_state
= TLS_ST_SR_CLNT_HELLO
;
258 int ossl_statem_connect(SSL
*s
)
260 return state_machine(s
, 0);
263 int ossl_statem_accept(SSL
*s
)
265 return state_machine(s
, 1);
268 typedef void (*info_cb
) (const SSL
*, int, int);
270 static info_cb
get_callback(SSL
*s
)
272 if (s
->info_callback
!= NULL
)
273 return s
->info_callback
;
274 else if (s
->ctx
->info_callback
!= NULL
)
275 return s
->ctx
->info_callback
;
281 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
282 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
283 * transitions are as follows:
285 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
287 * +-----------------------+
289 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
297 * We may exit at any point due to an error or NBIO event. If an NBIO event
298 * occurs then we restart at the point we left off when we are recalled.
299 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
301 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
302 * into that state at any point in the event that an irrecoverable error occurs.
304 * Valid return values are:
308 static int state_machine(SSL
*s
, int server
)
311 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
312 OSSL_STATEM
*st
= &s
->statem
;
316 if (st
->state
== MSG_FLOW_ERROR
) {
317 /* Shouldn't have been called if we're already in the error state */
324 cb
= get_callback(s
);
327 if (!SSL_in_init(s
) || SSL_in_before(s
)) {
329 * If we are stateless then we already called SSL_clear() - don't do
330 * it again and clear the STATELESS flag itself.
332 if ((s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0 && !SSL_clear(s
))
335 #ifndef OPENSSL_NO_SCTP
336 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
338 * Notify SCTP BIO socket to enter handshake mode and prevent stream
339 * identifier other than 0.
341 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
342 st
->in_handshake
, NULL
);
346 /* Initialise state machine */
347 if (st
->state
== MSG_FLOW_UNINITED
348 || st
->state
== MSG_FLOW_FINISHED
) {
349 if (st
->state
== MSG_FLOW_UNINITED
) {
350 st
->hand_state
= TLS_ST_BEFORE
;
351 st
->request_state
= TLS_ST_BEFORE
;
356 if (SSL_IS_FIRST_HANDSHAKE(s
) || !SSL_IS_TLS13(s
))
357 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
361 * Fatal errors in this block don't send an alert because we have
362 * failed to even initialise properly. Sending an alert is probably
366 if (SSL_IS_DTLS(s
)) {
367 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00) &&
368 (server
|| (s
->version
& 0xff00) != (DTLS1_BAD_VER
& 0xff00))) {
369 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
373 if ((s
->version
>> 8) != SSL3_VERSION_MAJOR
) {
374 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
379 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
380 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
384 if (s
->init_buf
== NULL
) {
385 if ((buf
= BUF_MEM_new()) == NULL
) {
386 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
389 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
390 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
397 if (!ssl3_setup_buffers(s
)) {
398 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
404 * Should have been reset by tls_process_finished, too.
406 s
->s3
.change_cipher_spec
= 0;
409 * Ok, we now need to push on a buffering BIO ...but not with
412 #ifndef OPENSSL_NO_SCTP
413 if (!SSL_IS_DTLS(s
) || !BIO_dgram_is_sctp(SSL_get_wbio(s
)))
415 if (!ssl_init_wbio_buffer(s
)) {
416 SSLfatal(s
, SSL_AD_NO_ALERT
, ERR_R_INTERNAL_ERROR
);
420 if ((SSL_in_before(s
))
422 if (!tls_setup_handshake(s
)) {
423 /* SSLfatal() already called */
427 if (SSL_IS_FIRST_HANDSHAKE(s
))
428 st
->read_state_first_init
= 1;
431 st
->state
= MSG_FLOW_WRITING
;
432 init_write_state_machine(s
);
435 while (st
->state
!= MSG_FLOW_FINISHED
) {
436 if (st
->state
== MSG_FLOW_READING
) {
437 ssret
= read_state_machine(s
);
438 if (ssret
== SUB_STATE_FINISHED
) {
439 st
->state
= MSG_FLOW_WRITING
;
440 init_write_state_machine(s
);
445 } else if (st
->state
== MSG_FLOW_WRITING
) {
446 ssret
= write_state_machine(s
);
447 if (ssret
== SUB_STATE_FINISHED
) {
448 st
->state
= MSG_FLOW_READING
;
449 init_read_state_machine(s
);
450 } else if (ssret
== SUB_STATE_END_HANDSHAKE
) {
451 st
->state
= MSG_FLOW_FINISHED
;
459 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
469 #ifndef OPENSSL_NO_SCTP
470 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
472 * Notify SCTP BIO socket to leave handshake mode and allow stream
473 * identifier other than 0.
475 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
476 st
->in_handshake
, NULL
);
483 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
485 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
491 * Initialise the MSG_FLOW_READING sub-state machine
493 static void init_read_state_machine(SSL
*s
)
495 OSSL_STATEM
*st
= &s
->statem
;
497 st
->read_state
= READ_STATE_HEADER
;
500 static int grow_init_buf(SSL
*s
, size_t size
) {
502 size_t msg_offset
= (char *)s
->init_msg
- s
->init_buf
->data
;
504 if (!BUF_MEM_grow_clean(s
->init_buf
, (int)size
))
507 if (size
< msg_offset
)
510 s
->init_msg
= s
->init_buf
->data
+ msg_offset
;
516 * This function implements the sub-state machine when the message flow is in
517 * MSG_FLOW_READING. The valid sub-states and transitions are:
519 * READ_STATE_HEADER <--+<-------------+
522 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
524 * +----------------------------+
526 * [SUB_STATE_FINISHED]
528 * READ_STATE_HEADER has the responsibility for reading in the message header
529 * and transitioning the state of the handshake state machine.
531 * READ_STATE_BODY reads in the rest of the message and then subsequently
534 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
535 * processing activity performed on the message may block.
537 * Any of the above states could result in an NBIO event occurring in which case
538 * control returns to the calling application. When this function is recalled we
539 * will resume in the same state where we left off.
541 static SUB_STATE_RETURN
read_state_machine(SSL
*s
)
543 OSSL_STATEM
*st
= &s
->statem
;
546 int (*transition
) (SSL
*s
, int mt
);
548 MSG_PROCESS_RETURN(*process_message
) (SSL
*s
, PACKET
*pkt
);
549 WORK_STATE(*post_process_message
) (SSL
*s
, WORK_STATE wst
);
550 size_t (*max_message_size
) (SSL
*s
);
551 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
553 cb
= get_callback(s
);
556 transition
= ossl_statem_server_read_transition
;
557 process_message
= ossl_statem_server_process_message
;
558 max_message_size
= ossl_statem_server_max_message_size
;
559 post_process_message
= ossl_statem_server_post_process_message
;
561 transition
= ossl_statem_client_read_transition
;
562 process_message
= ossl_statem_client_process_message
;
563 max_message_size
= ossl_statem_client_max_message_size
;
564 post_process_message
= ossl_statem_client_post_process_message
;
567 if (st
->read_state_first_init
) {
569 st
->read_state_first_init
= 0;
573 switch (st
->read_state
) {
574 case READ_STATE_HEADER
:
575 /* Get the state the peer wants to move to */
576 if (SSL_IS_DTLS(s
)) {
578 * In DTLS we get the whole message in one go - header and body
580 ret
= dtls_get_message(s
, &mt
, &len
);
582 ret
= tls_get_message_header(s
, &mt
);
586 /* Could be non-blocking IO */
587 return SUB_STATE_ERROR
;
591 /* Notify callback of an impending state change */
593 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
595 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
598 * Validate that we are allowed to move to the new state and move
599 * to that state if so
601 if (!transition(s
, mt
))
602 return SUB_STATE_ERROR
;
604 if (s
->s3
.tmp
.message_size
> max_message_size(s
)) {
605 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
606 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
607 return SUB_STATE_ERROR
;
610 /* dtls_get_message already did this */
612 && s
->s3
.tmp
.message_size
> 0
613 && !grow_init_buf(s
, s
->s3
.tmp
.message_size
614 + SSL3_HM_HEADER_LENGTH
)) {
615 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BUF_LIB
);
616 return SUB_STATE_ERROR
;
619 st
->read_state
= READ_STATE_BODY
;
622 case READ_STATE_BODY
:
623 if (!SSL_IS_DTLS(s
)) {
624 /* We already got this above for DTLS */
625 ret
= tls_get_message_body(s
, &len
);
627 /* Could be non-blocking IO */
628 return SUB_STATE_ERROR
;
633 if (!PACKET_buf_init(&pkt
, s
->init_msg
, len
)) {
634 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
635 return SUB_STATE_ERROR
;
637 ret
= process_message(s
, &pkt
);
639 /* Discard the packet data */
643 case MSG_PROCESS_ERROR
:
645 return SUB_STATE_ERROR
;
647 case MSG_PROCESS_FINISHED_READING
:
648 if (SSL_IS_DTLS(s
)) {
651 return SUB_STATE_FINISHED
;
653 case MSG_PROCESS_CONTINUE_PROCESSING
:
654 st
->read_state
= READ_STATE_POST_PROCESS
;
655 st
->read_state_work
= WORK_MORE_A
;
659 st
->read_state
= READ_STATE_HEADER
;
664 case READ_STATE_POST_PROCESS
:
665 st
->read_state_work
= post_process_message(s
, st
->read_state_work
);
666 switch (st
->read_state_work
) {
673 return SUB_STATE_ERROR
;
675 case WORK_FINISHED_CONTINUE
:
676 st
->read_state
= READ_STATE_HEADER
;
679 case WORK_FINISHED_STOP
:
680 if (SSL_IS_DTLS(s
)) {
683 return SUB_STATE_FINISHED
;
688 /* Shouldn't happen */
689 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
690 return SUB_STATE_ERROR
;
696 * Send a previously constructed message to the peer.
698 static int statem_do_write(SSL
*s
)
700 OSSL_STATEM
*st
= &s
->statem
;
702 if (st
->hand_state
== TLS_ST_CW_CHANGE
703 || st
->hand_state
== TLS_ST_SW_CHANGE
) {
705 return dtls1_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
707 return ssl3_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
709 return ssl_do_write(s
);
714 * Initialise the MSG_FLOW_WRITING sub-state machine
716 static void init_write_state_machine(SSL
*s
)
718 OSSL_STATEM
*st
= &s
->statem
;
720 st
->write_state
= WRITE_STATE_TRANSITION
;
724 * This function implements the sub-state machine when the message flow is in
725 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
727 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
730 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
736 * | WRITE_STATE_POST_WORK
740 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
742 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
743 * sending of the message. This could result in an NBIO event occurring in
744 * which case control returns to the calling application. When this function
745 * is recalled we will resume in the same state where we left off.
747 * WRITE_STATE_SEND sends the message and performs any work to be done after
750 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
751 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
752 * result in an NBIO event.
754 static SUB_STATE_RETURN
write_state_machine(SSL
*s
)
756 OSSL_STATEM
*st
= &s
->statem
;
758 WRITE_TRAN(*transition
) (SSL
*s
);
759 WORK_STATE(*pre_work
) (SSL
*s
, WORK_STATE wst
);
760 WORK_STATE(*post_work
) (SSL
*s
, WORK_STATE wst
);
761 int (*get_construct_message_f
) (SSL
*s
, WPACKET
*pkt
,
762 int (**confunc
) (SSL
*s
, WPACKET
*pkt
),
764 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
765 int (*confunc
) (SSL
*s
, WPACKET
*pkt
);
769 cb
= get_callback(s
);
772 transition
= ossl_statem_server_write_transition
;
773 pre_work
= ossl_statem_server_pre_work
;
774 post_work
= ossl_statem_server_post_work
;
775 get_construct_message_f
= ossl_statem_server_construct_message
;
777 transition
= ossl_statem_client_write_transition
;
778 pre_work
= ossl_statem_client_pre_work
;
779 post_work
= ossl_statem_client_post_work
;
780 get_construct_message_f
= ossl_statem_client_construct_message
;
784 switch (st
->write_state
) {
785 case WRITE_STATE_TRANSITION
:
787 /* Notify callback of an impending state change */
789 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
791 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
793 switch (transition(s
)) {
794 case WRITE_TRAN_CONTINUE
:
795 st
->write_state
= WRITE_STATE_PRE_WORK
;
796 st
->write_state_work
= WORK_MORE_A
;
799 case WRITE_TRAN_FINISHED
:
800 return SUB_STATE_FINISHED
;
803 case WRITE_TRAN_ERROR
:
805 return SUB_STATE_ERROR
;
809 case WRITE_STATE_PRE_WORK
:
810 switch (st
->write_state_work
= pre_work(s
, st
->write_state_work
)) {
817 return SUB_STATE_ERROR
;
819 case WORK_FINISHED_CONTINUE
:
820 st
->write_state
= WRITE_STATE_SEND
;
823 case WORK_FINISHED_STOP
:
824 return SUB_STATE_END_HANDSHAKE
;
826 if (!get_construct_message_f(s
, &pkt
, &confunc
, &mt
)) {
827 /* SSLfatal() already called */
828 return SUB_STATE_ERROR
;
830 if (mt
== SSL3_MT_DUMMY
) {
831 /* Skip construction and sending. This isn't a "real" state */
832 st
->write_state
= WRITE_STATE_POST_WORK
;
833 st
->write_state_work
= WORK_MORE_A
;
836 if (!WPACKET_init(&pkt
, s
->init_buf
)
837 || !ssl_set_handshake_header(s
, &pkt
, mt
)) {
838 WPACKET_cleanup(&pkt
);
839 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
840 return SUB_STATE_ERROR
;
842 if (confunc
!= NULL
&& !confunc(s
, &pkt
)) {
843 WPACKET_cleanup(&pkt
);
845 return SUB_STATE_ERROR
;
847 if (!ssl_close_construct_packet(s
, &pkt
, mt
)
848 || !WPACKET_finish(&pkt
)) {
849 WPACKET_cleanup(&pkt
);
850 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
851 return SUB_STATE_ERROR
;
856 case WRITE_STATE_SEND
:
857 if (SSL_IS_DTLS(s
) && st
->use_timer
) {
858 dtls1_start_timer(s
);
860 ret
= statem_do_write(s
);
862 return SUB_STATE_ERROR
;
864 st
->write_state
= WRITE_STATE_POST_WORK
;
865 st
->write_state_work
= WORK_MORE_A
;
868 case WRITE_STATE_POST_WORK
:
869 switch (st
->write_state_work
= post_work(s
, st
->write_state_work
)) {
876 return SUB_STATE_ERROR
;
878 case WORK_FINISHED_CONTINUE
:
879 st
->write_state
= WRITE_STATE_TRANSITION
;
882 case WORK_FINISHED_STOP
:
883 return SUB_STATE_END_HANDSHAKE
;
888 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
889 return SUB_STATE_ERROR
;
895 * Flush the write BIO
897 int statem_flush(SSL
*s
)
899 s
->rwstate
= SSL_WRITING
;
900 if (BIO_flush(s
->wbio
) <= 0) {
903 s
->rwstate
= SSL_NOTHING
;
909 * Called by the record layer to determine whether application data is
910 * allowed to be received in the current handshake state or not.
913 * 1: Yes (application data allowed)
914 * 0: No (application data not allowed)
916 int ossl_statem_app_data_allowed(SSL
*s
)
918 OSSL_STATEM
*st
= &s
->statem
;
920 if (st
->state
== MSG_FLOW_UNINITED
)
923 if (!s
->s3
.in_read_app_data
|| (s
->s3
.total_renegotiations
== 0))
928 * If we're a server and we haven't got as far as writing our
929 * ServerHello yet then we allow app data
931 if (st
->hand_state
== TLS_ST_BEFORE
932 || st
->hand_state
== TLS_ST_SR_CLNT_HELLO
)
936 * If we're a client and we haven't read the ServerHello yet then we
939 if (st
->hand_state
== TLS_ST_CW_CLNT_HELLO
)
947 * This function returns 1 if TLS exporter is ready to export keying
948 * material, or 0 if otherwise.
950 int ossl_statem_export_allowed(SSL
*s
)
952 return s
->s3
.previous_server_finished_len
!= 0
953 && s
->statem
.hand_state
!= TLS_ST_SW_FINISHED
;
957 * Return 1 if early TLS exporter is ready to export keying material,
960 int ossl_statem_export_early_allowed(SSL
*s
)
963 * The early exporter secret is only present on the server if we
964 * have accepted early_data. It is present on the client as long
965 * as we have sent early_data.
967 return s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
968 || (!s
->server
&& s
->ext
.early_data
!= SSL_EARLY_DATA_NOT_SENT
);