2 * Copyright 2015-2018 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_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 ERR_raise(ERR_LIB_SSL
, reason
);
122 ERR_set_debug(file
, line
, NULL
); /* Override what ERR_raise set */
123 /* We shouldn't call SSLfatal() twice. Once is enough */
124 if (s
->statem
.in_init
&& s
->statem
.state
== MSG_FLOW_ERROR
)
126 s
->statem
.in_init
= 1;
127 s
->statem
.state
= MSG_FLOW_ERROR
;
128 if (al
!= SSL_AD_NO_ALERT
129 && s
->statem
.enc_write_state
!= ENC_WRITE_STATE_INVALID
)
130 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
134 * This macro should only be called if we are already expecting to be in
135 * a fatal error state. We verify that we are, and set it if not (this would
138 #define check_fatal(s, f) \
140 if (!ossl_assert((s)->statem.in_init \
141 && (s)->statem.state == MSG_FLOW_ERROR)) \
142 SSLfatal(s, SSL_AD_INTERNAL_ERROR, (f), \
143 SSL_R_MISSING_FATAL); \
147 * Discover whether the current connection is in the error state.
149 * Valid return values are:
153 int ossl_statem_in_error(const SSL
*s
)
155 if (s
->statem
.state
== MSG_FLOW_ERROR
)
161 void ossl_statem_set_in_init(SSL
*s
, int init
)
163 s
->statem
.in_init
= init
;
166 int ossl_statem_get_in_handshake(SSL
*s
)
168 return s
->statem
.in_handshake
;
171 void ossl_statem_set_in_handshake(SSL
*s
, int inhand
)
174 s
->statem
.in_handshake
++;
176 s
->statem
.in_handshake
--;
179 /* Are we in a sensible state to skip over unreadable early data? */
180 int ossl_statem_skip_early_data(SSL
*s
)
182 if (s
->ext
.early_data
!= SSL_EARLY_DATA_REJECTED
)
186 || s
->statem
.hand_state
!= TLS_ST_EARLY_DATA
187 || s
->hello_retry_request
== SSL_HRR_COMPLETE
)
194 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
195 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
196 * data state and whether we should attempt to move the handshake on if so.
197 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
198 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
201 void ossl_statem_check_finish_init(SSL
*s
, int sending
)
204 if (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
205 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
) {
206 ossl_statem_set_in_init(s
, 1);
207 if (s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
) {
209 * SSL_connect() or SSL_do_handshake() has been called directly.
210 * We don't allow any more writing of early data.
212 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
215 } else if (!s
->server
) {
216 if ((sending
&& (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
217 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
218 && s
->early_data_state
!= SSL_EARLY_DATA_WRITING
)
219 || (!sending
&& s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)) {
220 ossl_statem_set_in_init(s
, 1);
222 * SSL_write() has been called directly. We don't allow any more
223 * writing of early data.
225 if (sending
&& s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
)
226 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
229 if (s
->early_data_state
== SSL_EARLY_DATA_FINISHED_READING
230 && s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
231 ossl_statem_set_in_init(s
, 1);
235 void ossl_statem_set_hello_verify_done(SSL
*s
)
237 s
->statem
.state
= MSG_FLOW_UNINITED
;
238 s
->statem
.in_init
= 1;
240 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
241 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
242 * calls to SSL_in_before() will return false. Also calls to
243 * SSL_state_string() and SSL_state_string_long() will return something
246 s
->statem
.hand_state
= TLS_ST_SR_CLNT_HELLO
;
249 int ossl_statem_connect(SSL
*s
)
251 return state_machine(s
, 0);
254 int ossl_statem_accept(SSL
*s
)
256 return state_machine(s
, 1);
259 typedef void (*info_cb
) (const SSL
*, int, int);
261 static info_cb
get_callback(SSL
*s
)
263 if (s
->info_callback
!= NULL
)
264 return s
->info_callback
;
265 else if (s
->ctx
->info_callback
!= NULL
)
266 return s
->ctx
->info_callback
;
272 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
273 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
274 * transitions are as follows:
276 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
278 * +-----------------------+
280 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
288 * We may exit at any point due to an error or NBIO event. If an NBIO event
289 * occurs then we restart at the point we left off when we are recalled.
290 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
292 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
293 * into that state at any point in the event that an irrecoverable error occurs.
295 * Valid return values are:
299 static int state_machine(SSL
*s
, int server
)
302 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
303 OSSL_STATEM
*st
= &s
->statem
;
307 if (st
->state
== MSG_FLOW_ERROR
) {
308 /* Shouldn't have been called if we're already in the error state */
315 cb
= get_callback(s
);
318 if (!SSL_in_init(s
) || SSL_in_before(s
)) {
320 * If we are stateless then we already called SSL_clear() - don't do
321 * it again and clear the STATELESS flag itself.
323 if ((s
->s3
.flags
& TLS1_FLAGS_STATELESS
) == 0 && !SSL_clear(s
))
326 #ifndef OPENSSL_NO_SCTP
327 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
329 * Notify SCTP BIO socket to enter handshake mode and prevent stream
330 * identifier other than 0.
332 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
333 st
->in_handshake
, NULL
);
337 /* Initialise state machine */
338 if (st
->state
== MSG_FLOW_UNINITED
339 || st
->state
== MSG_FLOW_FINISHED
) {
340 if (st
->state
== MSG_FLOW_UNINITED
) {
341 st
->hand_state
= TLS_ST_BEFORE
;
342 st
->request_state
= TLS_ST_BEFORE
;
347 if (SSL_IS_FIRST_HANDSHAKE(s
) || !SSL_IS_TLS13(s
))
348 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
352 * Fatal errors in this block don't send an alert because we have
353 * failed to even initialise properly. Sending an alert is probably
357 if (SSL_IS_DTLS(s
)) {
358 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00) &&
359 (server
|| (s
->version
& 0xff00) != (DTLS1_BAD_VER
& 0xff00))) {
360 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
361 ERR_R_INTERNAL_ERROR
);
365 if ((s
->version
>> 8) != SSL3_VERSION_MAJOR
) {
366 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
367 ERR_R_INTERNAL_ERROR
);
372 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
373 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
374 ERR_R_INTERNAL_ERROR
);
378 if (s
->init_buf
== NULL
) {
379 if ((buf
= BUF_MEM_new()) == NULL
) {
380 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
381 ERR_R_INTERNAL_ERROR
);
384 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
385 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
386 ERR_R_INTERNAL_ERROR
);
393 if (!ssl3_setup_buffers(s
)) {
394 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
395 ERR_R_INTERNAL_ERROR
);
401 * Should have been reset by tls_process_finished, too.
403 s
->s3
.change_cipher_spec
= 0;
406 * Ok, we now need to push on a buffering BIO ...but not with
409 #ifndef OPENSSL_NO_SCTP
410 if (!SSL_IS_DTLS(s
) || !BIO_dgram_is_sctp(SSL_get_wbio(s
)))
412 if (!ssl_init_wbio_buffer(s
)) {
413 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
414 ERR_R_INTERNAL_ERROR
);
418 if ((SSL_in_before(s
))
420 if (!tls_setup_handshake(s
)) {
421 /* SSLfatal() already called */
425 if (SSL_IS_FIRST_HANDSHAKE(s
))
426 st
->read_state_first_init
= 1;
429 st
->state
= MSG_FLOW_WRITING
;
430 init_write_state_machine(s
);
433 while (st
->state
!= MSG_FLOW_FINISHED
) {
434 if (st
->state
== MSG_FLOW_READING
) {
435 ssret
= read_state_machine(s
);
436 if (ssret
== SUB_STATE_FINISHED
) {
437 st
->state
= MSG_FLOW_WRITING
;
438 init_write_state_machine(s
);
443 } else if (st
->state
== MSG_FLOW_WRITING
) {
444 ssret
= write_state_machine(s
);
445 if (ssret
== SUB_STATE_FINISHED
) {
446 st
->state
= MSG_FLOW_READING
;
447 init_read_state_machine(s
);
448 } else if (ssret
== SUB_STATE_END_HANDSHAKE
) {
449 st
->state
= MSG_FLOW_FINISHED
;
456 check_fatal(s
, SSL_F_STATE_MACHINE
);
457 SSLerr(SSL_F_STATE_MACHINE
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
467 #ifndef OPENSSL_NO_SCTP
468 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
470 * Notify SCTP BIO socket to leave handshake mode and allow stream
471 * identifier other than 0.
473 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
474 st
->in_handshake
, NULL
);
481 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
483 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
489 * Initialise the MSG_FLOW_READING sub-state machine
491 static void init_read_state_machine(SSL
*s
)
493 OSSL_STATEM
*st
= &s
->statem
;
495 st
->read_state
= READ_STATE_HEADER
;
498 static int grow_init_buf(SSL
*s
, size_t size
) {
500 size_t msg_offset
= (char *)s
->init_msg
- s
->init_buf
->data
;
502 if (!BUF_MEM_grow_clean(s
->init_buf
, (int)size
))
505 if (size
< msg_offset
)
508 s
->init_msg
= s
->init_buf
->data
+ msg_offset
;
514 * This function implements the sub-state machine when the message flow is in
515 * MSG_FLOW_READING. The valid sub-states and transitions are:
517 * READ_STATE_HEADER <--+<-------------+
520 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
522 * +----------------------------+
524 * [SUB_STATE_FINISHED]
526 * READ_STATE_HEADER has the responsibility for reading in the message header
527 * and transitioning the state of the handshake state machine.
529 * READ_STATE_BODY reads in the rest of the message and then subsequently
532 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
533 * processing activity performed on the message may block.
535 * Any of the above states could result in an NBIO event occurring in which case
536 * control returns to the calling application. When this function is recalled we
537 * will resume in the same state where we left off.
539 static SUB_STATE_RETURN
read_state_machine(SSL
*s
)
541 OSSL_STATEM
*st
= &s
->statem
;
544 int (*transition
) (SSL
*s
, int mt
);
546 MSG_PROCESS_RETURN(*process_message
) (SSL
*s
, PACKET
*pkt
);
547 WORK_STATE(*post_process_message
) (SSL
*s
, WORK_STATE wst
);
548 size_t (*max_message_size
) (SSL
*s
);
549 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
551 cb
= get_callback(s
);
554 transition
= ossl_statem_server_read_transition
;
555 process_message
= ossl_statem_server_process_message
;
556 max_message_size
= ossl_statem_server_max_message_size
;
557 post_process_message
= ossl_statem_server_post_process_message
;
559 transition
= ossl_statem_client_read_transition
;
560 process_message
= ossl_statem_client_process_message
;
561 max_message_size
= ossl_statem_client_max_message_size
;
562 post_process_message
= ossl_statem_client_post_process_message
;
565 if (st
->read_state_first_init
) {
567 st
->read_state_first_init
= 0;
571 switch (st
->read_state
) {
572 case READ_STATE_HEADER
:
573 /* Get the state the peer wants to move to */
574 if (SSL_IS_DTLS(s
)) {
576 * In DTLS we get the whole message in one go - header and body
578 ret
= dtls_get_message(s
, &mt
, &len
);
580 ret
= tls_get_message_header(s
, &mt
);
584 /* Could be non-blocking IO */
585 return SUB_STATE_ERROR
;
589 /* Notify callback of an impending state change */
591 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
593 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
596 * Validate that we are allowed to move to the new state and move
597 * to that state if so
599 if (!transition(s
, mt
))
600 return SUB_STATE_ERROR
;
602 if (s
->s3
.tmp
.message_size
> max_message_size(s
)) {
603 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_READ_STATE_MACHINE
,
604 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
605 return SUB_STATE_ERROR
;
608 /* dtls_get_message already did this */
610 && s
->s3
.tmp
.message_size
> 0
611 && !grow_init_buf(s
, s
->s3
.tmp
.message_size
612 + SSL3_HM_HEADER_LENGTH
)) {
613 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
615 return SUB_STATE_ERROR
;
618 st
->read_state
= READ_STATE_BODY
;
621 case READ_STATE_BODY
:
622 if (!SSL_IS_DTLS(s
)) {
623 /* We already got this above for DTLS */
624 ret
= tls_get_message_body(s
, &len
);
626 /* Could be non-blocking IO */
627 return SUB_STATE_ERROR
;
632 if (!PACKET_buf_init(&pkt
, s
->init_msg
, len
)) {
633 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
634 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
:
644 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
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
) {
668 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
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
, SSL_F_READ_STATE_MACHINE
,
690 ERR_R_INTERNAL_ERROR
);
691 return SUB_STATE_ERROR
;
697 * Send a previously constructed message to the peer.
699 static int statem_do_write(SSL
*s
)
701 OSSL_STATEM
*st
= &s
->statem
;
703 if (st
->hand_state
== TLS_ST_CW_CHANGE
704 || st
->hand_state
== TLS_ST_SW_CHANGE
) {
706 return dtls1_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
708 return ssl3_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
);
710 return ssl_do_write(s
);
715 * Initialise the MSG_FLOW_WRITING sub-state machine
717 static void init_write_state_machine(SSL
*s
)
719 OSSL_STATEM
*st
= &s
->statem
;
721 st
->write_state
= WRITE_STATE_TRANSITION
;
725 * This function implements the sub-state machine when the message flow is in
726 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
728 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
731 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
737 * | WRITE_STATE_POST_WORK
741 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
743 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
744 * sending of the message. This could result in an NBIO event occurring in
745 * which case control returns to the calling application. When this function
746 * is recalled we will resume in the same state where we left off.
748 * WRITE_STATE_SEND sends the message and performs any work to be done after
751 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
752 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
753 * result in an NBIO event.
755 static SUB_STATE_RETURN
write_state_machine(SSL
*s
)
757 OSSL_STATEM
*st
= &s
->statem
;
759 WRITE_TRAN(*transition
) (SSL
*s
);
760 WORK_STATE(*pre_work
) (SSL
*s
, WORK_STATE wst
);
761 WORK_STATE(*post_work
) (SSL
*s
, WORK_STATE wst
);
762 int (*get_construct_message_f
) (SSL
*s
, WPACKET
*pkt
,
763 int (**confunc
) (SSL
*s
, WPACKET
*pkt
),
765 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
766 int (*confunc
) (SSL
*s
, WPACKET
*pkt
);
770 cb
= get_callback(s
);
773 transition
= ossl_statem_server_write_transition
;
774 pre_work
= ossl_statem_server_pre_work
;
775 post_work
= ossl_statem_server_post_work
;
776 get_construct_message_f
= ossl_statem_server_construct_message
;
778 transition
= ossl_statem_client_write_transition
;
779 pre_work
= ossl_statem_client_pre_work
;
780 post_work
= ossl_statem_client_post_work
;
781 get_construct_message_f
= ossl_statem_client_construct_message
;
785 switch (st
->write_state
) {
786 case WRITE_STATE_TRANSITION
:
788 /* Notify callback of an impending state change */
790 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
792 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
794 switch (transition(s
)) {
795 case WRITE_TRAN_CONTINUE
:
796 st
->write_state
= WRITE_STATE_PRE_WORK
;
797 st
->write_state_work
= WORK_MORE_A
;
800 case WRITE_TRAN_FINISHED
:
801 return SUB_STATE_FINISHED
;
804 case WRITE_TRAN_ERROR
:
805 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
806 return SUB_STATE_ERROR
;
810 case WRITE_STATE_PRE_WORK
:
811 switch (st
->write_state_work
= pre_work(s
, st
->write_state_work
)) {
813 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
818 return SUB_STATE_ERROR
;
820 case WORK_FINISHED_CONTINUE
:
821 st
->write_state
= WRITE_STATE_SEND
;
824 case WORK_FINISHED_STOP
:
825 return SUB_STATE_END_HANDSHAKE
;
827 if (!get_construct_message_f(s
, &pkt
, &confunc
, &mt
)) {
828 /* SSLfatal() already called */
829 return SUB_STATE_ERROR
;
831 if (mt
== SSL3_MT_DUMMY
) {
832 /* Skip construction and sending. This isn't a "real" state */
833 st
->write_state
= WRITE_STATE_POST_WORK
;
834 st
->write_state_work
= WORK_MORE_A
;
837 if (!WPACKET_init(&pkt
, s
->init_buf
)
838 || !ssl_set_handshake_header(s
, &pkt
, mt
)) {
839 WPACKET_cleanup(&pkt
);
840 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
841 ERR_R_INTERNAL_ERROR
);
842 return SUB_STATE_ERROR
;
844 if (confunc
!= NULL
&& !confunc(s
, &pkt
)) {
845 WPACKET_cleanup(&pkt
);
846 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
847 return SUB_STATE_ERROR
;
849 if (!ssl_close_construct_packet(s
, &pkt
, mt
)
850 || !WPACKET_finish(&pkt
)) {
851 WPACKET_cleanup(&pkt
);
852 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
853 ERR_R_INTERNAL_ERROR
);
854 return SUB_STATE_ERROR
;
859 case WRITE_STATE_SEND
:
860 if (SSL_IS_DTLS(s
) && st
->use_timer
) {
861 dtls1_start_timer(s
);
863 ret
= statem_do_write(s
);
865 return SUB_STATE_ERROR
;
867 st
->write_state
= WRITE_STATE_POST_WORK
;
868 st
->write_state_work
= WORK_MORE_A
;
871 case WRITE_STATE_POST_WORK
:
872 switch (st
->write_state_work
= post_work(s
, st
->write_state_work
)) {
874 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
879 return SUB_STATE_ERROR
;
881 case WORK_FINISHED_CONTINUE
:
882 st
->write_state
= WRITE_STATE_TRANSITION
;
885 case WORK_FINISHED_STOP
:
886 return SUB_STATE_END_HANDSHAKE
;
891 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
892 ERR_R_INTERNAL_ERROR
);
893 return SUB_STATE_ERROR
;
899 * Flush the write BIO
901 int statem_flush(SSL
*s
)
903 s
->rwstate
= SSL_WRITING
;
904 if (BIO_flush(s
->wbio
) <= 0) {
907 s
->rwstate
= SSL_NOTHING
;
913 * Called by the record layer to determine whether application data is
914 * allowed to be received in the current handshake state or not.
917 * 1: Yes (application data allowed)
918 * 0: No (application data not allowed)
920 int ossl_statem_app_data_allowed(SSL
*s
)
922 OSSL_STATEM
*st
= &s
->statem
;
924 if (st
->state
== MSG_FLOW_UNINITED
)
927 if (!s
->s3
.in_read_app_data
|| (s
->s3
.total_renegotiations
== 0))
932 * If we're a server and we haven't got as far as writing our
933 * ServerHello yet then we allow app data
935 if (st
->hand_state
== TLS_ST_BEFORE
936 || st
->hand_state
== TLS_ST_SR_CLNT_HELLO
)
940 * If we're a client and we haven't read the ServerHello yet then we
943 if (st
->hand_state
== TLS_ST_CW_CLNT_HELLO
)
951 * This function returns 1 if TLS exporter is ready to export keying
952 * material, or 0 if otherwise.
954 int ossl_statem_export_allowed(SSL
*s
)
956 return s
->s3
.previous_server_finished_len
!= 0
957 && s
->statem
.hand_state
!= TLS_ST_SW_FINISHED
;
961 * Return 1 if early TLS exporter is ready to export keying material,
964 int ossl_statem_export_early_allowed(SSL
*s
)
967 * The early exporter secret is only present on the server if we
968 * have accepted early_data. It is present on the client as long
969 * as we have sent early_data.
971 return s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
972 || (!s
->server
&& s
->ext
.early_data
!= SSL_EARLY_DATA_NOT_SENT
);