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_put_error(ERR_LIB_SSL
, func
, reason
, file
, line
);
122 /* We shouldn't call SSLfatal() twice. Once is enough */
123 if (s
->statem
.in_init
&& s
->statem
.state
== MSG_FLOW_ERROR
)
125 s
->statem
.in_init
= 1;
126 s
->statem
.state
= MSG_FLOW_ERROR
;
127 if (al
!= SSL_AD_NO_ALERT
128 && s
->statem
.enc_write_state
!= ENC_WRITE_STATE_INVALID
)
129 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
133 * This macro should only be called if we are already expecting to be in
134 * a fatal error state. We verify that we are, and set it if not (this would
137 #define check_fatal(s, f) \
139 if (!ossl_assert((s)->statem.in_init \
140 && (s)->statem.state == MSG_FLOW_ERROR)) \
141 SSLfatal(s, SSL_AD_INTERNAL_ERROR, (f), \
142 SSL_R_MISSING_FATAL); \
146 * Discover whether the current connection is in the error state.
148 * Valid return values are:
152 int ossl_statem_in_error(const SSL
*s
)
154 if (s
->statem
.state
== MSG_FLOW_ERROR
)
160 void ossl_statem_set_in_init(SSL
*s
, int init
)
162 s
->statem
.in_init
= init
;
165 int ossl_statem_get_in_handshake(SSL
*s
)
167 return s
->statem
.in_handshake
;
170 void ossl_statem_set_in_handshake(SSL
*s
, int inhand
)
173 s
->statem
.in_handshake
++;
175 s
->statem
.in_handshake
--;
178 /* Are we in a sensible state to skip over unreadable early data? */
179 int ossl_statem_skip_early_data(SSL
*s
)
181 if (s
->ext
.early_data
!= SSL_EARLY_DATA_REJECTED
)
185 || s
->statem
.hand_state
!= TLS_ST_EARLY_DATA
186 || s
->hello_retry_request
== SSL_HRR_COMPLETE
)
193 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
194 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
195 * data state and whether we should attempt to move the handshake on if so.
196 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
197 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
200 void ossl_statem_check_finish_init(SSL
*s
, int sending
)
203 if (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
204 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
) {
205 ossl_statem_set_in_init(s
, 1);
206 if (s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
) {
208 * SSL_connect() or SSL_do_handshake() has been called directly.
209 * We don't allow any more writing of early data.
211 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
214 } else if (!s
->server
) {
215 if ((sending
&& (s
->statem
.hand_state
== TLS_ST_PENDING_EARLY_DATA_END
216 || s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
217 && s
->early_data_state
!= SSL_EARLY_DATA_WRITING
)
218 || (!sending
&& s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)) {
219 ossl_statem_set_in_init(s
, 1);
221 * SSL_write() has been called directly. We don't allow any more
222 * writing of early data.
224 if (sending
&& s
->early_data_state
== SSL_EARLY_DATA_WRITE_RETRY
)
225 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_WRITING
;
228 if (s
->early_data_state
== SSL_EARLY_DATA_FINISHED_READING
229 && s
->statem
.hand_state
== TLS_ST_EARLY_DATA
)
230 ossl_statem_set_in_init(s
, 1);
234 void ossl_statem_set_hello_verify_done(SSL
*s
)
236 s
->statem
.state
= MSG_FLOW_UNINITED
;
237 s
->statem
.in_init
= 1;
239 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
240 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
241 * calls to SSL_in_before() will return false. Also calls to
242 * SSL_state_string() and SSL_state_string_long() will return something
245 s
->statem
.hand_state
= TLS_ST_SR_CLNT_HELLO
;
248 int ossl_statem_connect(SSL
*s
)
250 return state_machine(s
, 0);
253 int ossl_statem_accept(SSL
*s
)
255 return state_machine(s
, 1);
258 typedef void (*info_cb
) (const SSL
*, int, int);
260 static info_cb
get_callback(SSL
*s
)
262 if (s
->info_callback
!= NULL
)
263 return s
->info_callback
;
264 else if (s
->ctx
->info_callback
!= NULL
)
265 return s
->ctx
->info_callback
;
271 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
272 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
273 * transitions are as follows:
275 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
277 * +-----------------------+
279 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
287 * We may exit at any point due to an error or NBIO event. If an NBIO event
288 * occurs then we restart at the point we left off when we are recalled.
289 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
291 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
292 * into that state at any point in the event that an irrecoverable error occurs.
294 * Valid return values are:
298 static int state_machine(SSL
*s
, int server
)
301 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
302 OSSL_STATEM
*st
= &s
->statem
;
306 if (st
->state
== MSG_FLOW_ERROR
) {
307 /* Shouldn't have been called if we're already in the error state */
314 cb
= get_callback(s
);
317 if (!SSL_in_init(s
) || SSL_in_before(s
)) {
319 * If we are stateless then we already called SSL_clear() - don't do
320 * it again and clear the STATELESS flag itself.
322 if ((s
->s3
->flags
& TLS1_FLAGS_STATELESS
) == 0 && !SSL_clear(s
))
325 #ifndef OPENSSL_NO_SCTP
326 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
328 * Notify SCTP BIO socket to enter handshake mode and prevent stream
329 * identifier other than 0.
331 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
332 st
->in_handshake
, NULL
);
336 /* Initialise state machine */
337 if (st
->state
== MSG_FLOW_UNINITED
338 || st
->state
== MSG_FLOW_FINISHED
) {
339 if (st
->state
== MSG_FLOW_UNINITED
) {
340 st
->hand_state
= TLS_ST_BEFORE
;
341 st
->request_state
= TLS_ST_BEFORE
;
346 if (SSL_IS_FIRST_HANDSHAKE(s
) || !SSL_IS_TLS13(s
))
347 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
351 * Fatal errors in this block don't send an alert because we have
352 * failed to even initialise properly. Sending an alert is probably
356 if (SSL_IS_DTLS(s
)) {
357 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00) &&
358 (server
|| (s
->version
& 0xff00) != (DTLS1_BAD_VER
& 0xff00))) {
359 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
360 ERR_R_INTERNAL_ERROR
);
364 if ((s
->version
>> 8) != SSL3_VERSION_MAJOR
) {
365 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
366 ERR_R_INTERNAL_ERROR
);
371 if (!ssl_security(s
, SSL_SECOP_VERSION
, 0, s
->version
, NULL
)) {
372 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
373 ERR_R_INTERNAL_ERROR
);
377 if (s
->init_buf
== NULL
) {
378 if ((buf
= BUF_MEM_new()) == NULL
) {
379 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
380 ERR_R_INTERNAL_ERROR
);
383 if (!BUF_MEM_grow(buf
, SSL3_RT_MAX_PLAIN_LENGTH
)) {
384 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
385 ERR_R_INTERNAL_ERROR
);
392 if (!ssl3_setup_buffers(s
)) {
393 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
394 ERR_R_INTERNAL_ERROR
);
400 * Should have been reset by tls_process_finished, too.
402 s
->s3
->change_cipher_spec
= 0;
405 * Ok, we now need to push on a buffering BIO ...but not with
408 #ifndef OPENSSL_NO_SCTP
409 if (!SSL_IS_DTLS(s
) || !BIO_dgram_is_sctp(SSL_get_wbio(s
)))
411 if (!ssl_init_wbio_buffer(s
)) {
412 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_STATE_MACHINE
,
413 ERR_R_INTERNAL_ERROR
);
417 if ((SSL_in_before(s
))
419 if (!tls_setup_handshake(s
)) {
420 /* SSLfatal() already called */
424 if (SSL_IS_FIRST_HANDSHAKE(s
))
425 st
->read_state_first_init
= 1;
428 st
->state
= MSG_FLOW_WRITING
;
429 init_write_state_machine(s
);
432 while (st
->state
!= MSG_FLOW_FINISHED
) {
433 if (st
->state
== MSG_FLOW_READING
) {
434 ssret
= read_state_machine(s
);
435 if (ssret
== SUB_STATE_FINISHED
) {
436 st
->state
= MSG_FLOW_WRITING
;
437 init_write_state_machine(s
);
442 } else if (st
->state
== MSG_FLOW_WRITING
) {
443 ssret
= write_state_machine(s
);
444 if (ssret
== SUB_STATE_FINISHED
) {
445 st
->state
= MSG_FLOW_READING
;
446 init_read_state_machine(s
);
447 } else if (ssret
== SUB_STATE_END_HANDSHAKE
) {
448 st
->state
= MSG_FLOW_FINISHED
;
455 check_fatal(s
, SSL_F_STATE_MACHINE
);
456 SSLerr(SSL_F_STATE_MACHINE
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
466 #ifndef OPENSSL_NO_SCTP
467 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
469 * Notify SCTP BIO socket to leave handshake mode and allow stream
470 * identifier other than 0.
472 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE
,
473 st
->in_handshake
, NULL
);
480 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
482 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
488 * Initialise the MSG_FLOW_READING sub-state machine
490 static void init_read_state_machine(SSL
*s
)
492 OSSL_STATEM
*st
= &s
->statem
;
494 st
->read_state
= READ_STATE_HEADER
;
497 static int grow_init_buf(SSL
*s
, size_t size
) {
499 size_t msg_offset
= (char *)s
->init_msg
- s
->init_buf
->data
;
501 if (!BUF_MEM_grow_clean(s
->init_buf
, (int)size
))
504 if (size
< msg_offset
)
507 s
->init_msg
= s
->init_buf
->data
+ msg_offset
;
513 * This function implements the sub-state machine when the message flow is in
514 * MSG_FLOW_READING. The valid sub-states and transitions are:
516 * READ_STATE_HEADER <--+<-------------+
519 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
521 * +----------------------------+
523 * [SUB_STATE_FINISHED]
525 * READ_STATE_HEADER has the responsibility for reading in the message header
526 * and transitioning the state of the handshake state machine.
528 * READ_STATE_BODY reads in the rest of the message and then subsequently
531 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
532 * processing activity performed on the message may block.
534 * Any of the above states could result in an NBIO event occurring in which case
535 * control returns to the calling application. When this function is recalled we
536 * will resume in the same state where we left off.
538 static SUB_STATE_RETURN
read_state_machine(SSL
*s
)
540 OSSL_STATEM
*st
= &s
->statem
;
543 int (*transition
) (SSL
*s
, int mt
);
545 MSG_PROCESS_RETURN(*process_message
) (SSL
*s
, PACKET
*pkt
);
546 WORK_STATE(*post_process_message
) (SSL
*s
, WORK_STATE wst
);
547 size_t (*max_message_size
) (SSL
*s
);
548 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
550 cb
= get_callback(s
);
553 transition
= ossl_statem_server_read_transition
;
554 process_message
= ossl_statem_server_process_message
;
555 max_message_size
= ossl_statem_server_max_message_size
;
556 post_process_message
= ossl_statem_server_post_process_message
;
558 transition
= ossl_statem_client_read_transition
;
559 process_message
= ossl_statem_client_process_message
;
560 max_message_size
= ossl_statem_client_max_message_size
;
561 post_process_message
= ossl_statem_client_post_process_message
;
564 if (st
->read_state_first_init
) {
566 st
->read_state_first_init
= 0;
570 switch (st
->read_state
) {
571 case READ_STATE_HEADER
:
572 /* Get the state the peer wants to move to */
573 if (SSL_IS_DTLS(s
)) {
575 * In DTLS we get the whole message in one go - header and body
577 ret
= dtls_get_message(s
, &mt
, &len
);
579 ret
= tls_get_message_header(s
, &mt
);
583 /* Could be non-blocking IO */
584 return SUB_STATE_ERROR
;
588 /* Notify callback of an impending state change */
590 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
592 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
595 * Validate that we are allowed to move to the new state and move
596 * to that state if so
598 if (!transition(s
, mt
))
599 return SUB_STATE_ERROR
;
601 if (s
->s3
->tmp
.message_size
> max_message_size(s
)) {
602 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_READ_STATE_MACHINE
,
603 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
604 return SUB_STATE_ERROR
;
607 /* dtls_get_message already did this */
609 && s
->s3
->tmp
.message_size
> 0
610 && !grow_init_buf(s
, s
->s3
->tmp
.message_size
611 + SSL3_HM_HEADER_LENGTH
)) {
612 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
614 return SUB_STATE_ERROR
;
617 st
->read_state
= READ_STATE_BODY
;
620 case READ_STATE_BODY
:
621 if (!SSL_IS_DTLS(s
)) {
622 /* We already got this above for DTLS */
623 ret
= tls_get_message_body(s
, &len
);
625 /* Could be non-blocking IO */
626 return SUB_STATE_ERROR
;
631 if (!PACKET_buf_init(&pkt
, s
->init_msg
, len
)) {
632 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
633 ERR_R_INTERNAL_ERROR
);
634 return SUB_STATE_ERROR
;
636 ret
= process_message(s
, &pkt
);
638 /* Discard the packet data */
642 case MSG_PROCESS_ERROR
:
643 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
644 return SUB_STATE_ERROR
;
646 case MSG_PROCESS_FINISHED_READING
:
647 if (SSL_IS_DTLS(s
)) {
650 return SUB_STATE_FINISHED
;
652 case MSG_PROCESS_CONTINUE_PROCESSING
:
653 st
->read_state
= READ_STATE_POST_PROCESS
;
654 st
->read_state_work
= WORK_MORE_A
;
658 st
->read_state
= READ_STATE_HEADER
;
663 case READ_STATE_POST_PROCESS
:
664 st
->read_state_work
= post_process_message(s
, st
->read_state_work
);
665 switch (st
->read_state_work
) {
667 check_fatal(s
, SSL_F_READ_STATE_MACHINE
);
672 return SUB_STATE_ERROR
;
674 case WORK_FINISHED_CONTINUE
:
675 st
->read_state
= READ_STATE_HEADER
;
678 case WORK_FINISHED_STOP
:
679 if (SSL_IS_DTLS(s
)) {
682 return SUB_STATE_FINISHED
;
687 /* Shouldn't happen */
688 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_READ_STATE_MACHINE
,
689 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
:
804 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
805 return SUB_STATE_ERROR
;
809 case WRITE_STATE_PRE_WORK
:
810 switch (st
->write_state_work
= pre_work(s
, st
->write_state_work
)) {
812 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
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
, SSL_F_WRITE_STATE_MACHINE
,
840 ERR_R_INTERNAL_ERROR
);
841 return SUB_STATE_ERROR
;
843 if (confunc
!= NULL
&& !confunc(s
, &pkt
)) {
844 WPACKET_cleanup(&pkt
);
845 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
846 return SUB_STATE_ERROR
;
848 if (!ssl_close_construct_packet(s
, &pkt
, mt
)
849 || !WPACKET_finish(&pkt
)) {
850 WPACKET_cleanup(&pkt
);
851 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
852 ERR_R_INTERNAL_ERROR
);
853 return SUB_STATE_ERROR
;
858 case WRITE_STATE_SEND
:
859 if (SSL_IS_DTLS(s
) && st
->use_timer
) {
860 dtls1_start_timer(s
);
862 ret
= statem_do_write(s
);
864 return SUB_STATE_ERROR
;
866 st
->write_state
= WRITE_STATE_POST_WORK
;
867 st
->write_state_work
= WORK_MORE_A
;
870 case WRITE_STATE_POST_WORK
:
871 switch (st
->write_state_work
= post_work(s
, st
->write_state_work
)) {
873 check_fatal(s
, SSL_F_WRITE_STATE_MACHINE
);
878 return SUB_STATE_ERROR
;
880 case WORK_FINISHED_CONTINUE
:
881 st
->write_state
= WRITE_STATE_TRANSITION
;
884 case WORK_FINISHED_STOP
:
885 return SUB_STATE_END_HANDSHAKE
;
890 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_WRITE_STATE_MACHINE
,
891 ERR_R_INTERNAL_ERROR
);
892 return SUB_STATE_ERROR
;
898 * Flush the write BIO
900 int statem_flush(SSL
*s
)
902 s
->rwstate
= SSL_WRITING
;
903 if (BIO_flush(s
->wbio
) <= 0) {
906 s
->rwstate
= SSL_NOTHING
;
912 * Called by the record layer to determine whether application data is
913 * allowed to be received in the current handshake state or not.
916 * 1: Yes (application data allowed)
917 * 0: No (application data not allowed)
919 int ossl_statem_app_data_allowed(SSL
*s
)
921 OSSL_STATEM
*st
= &s
->statem
;
923 if (st
->state
== MSG_FLOW_UNINITED
)
926 if (!s
->s3
->in_read_app_data
|| (s
->s3
->total_renegotiations
== 0))
931 * If we're a server and we haven't got as far as writing our
932 * ServerHello yet then we allow app data
934 if (st
->hand_state
== TLS_ST_BEFORE
935 || st
->hand_state
== TLS_ST_SR_CLNT_HELLO
)
939 * If we're a client and we haven't read the ServerHello yet then we
942 if (st
->hand_state
== TLS_ST_CW_CLNT_HELLO
)
950 * This function returns 1 if TLS exporter is ready to export keying
951 * material, or 0 if otherwise.
953 int ossl_statem_export_allowed(SSL
*s
)
955 return s
->s3
->previous_server_finished_len
!= 0
956 && s
->statem
.hand_state
!= TLS_ST_SW_FINISHED
;
960 * Return 1 if early TLS exporter is ready to export keying material,
963 int ossl_statem_export_early_allowed(SSL
*s
)
966 * The early exporter secret is only present on the server if we
967 * have accepted early_data. It is present on the client as long
968 * as we have sent early_data.
970 return s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
971 || (!s
->server
&& s
->ext
.early_data
!= SSL_EARLY_DATA_NOT_SENT
);