2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #if defined(__TANDEM) && defined(_SPT_MODEL_)
11 # include <spthread.h>
12 # include <spt_extensions.h> /* timeval */
15 #include "internal/cryptlib.h"
16 #include <openssl/rand.h>
17 #include "../ssl_local.h"
18 #include "statem_local.h"
22 * This file implements the SSL/TLS/DTLS state machines.
24 * There are two primary state machines:
26 * 1) Message flow state machine
27 * 2) Handshake state machine
29 * The Message flow state machine controls the reading and sending of messages
30 * including handling of non-blocking IO events, flushing of the underlying
31 * write BIO, handling unexpected messages, etc. It is itself broken into two
32 * separate sub-state machines which control reading and writing respectively.
34 * The Handshake state machine keeps track of the current SSL/TLS handshake
35 * state. Transitions of the handshake state are the result of events that
36 * occur within the Message flow state machine.
38 * Overall it looks like this:
40 * --------------------------------------------- -------------------
42 * | Message flow state machine | | |
44 * | -------------------- -------------------- | Transition | Handshake state |
45 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
46 * | | sub-state | | sub-state | |----------->| |
47 * | | machine for | | machine for | | | |
48 * | | reading messages | | writing messages | | | |
49 * | -------------------- -------------------- | | |
51 * --------------------------------------------- -------------------
55 /* Sub state machine return values */
57 /* Something bad happened or NBIO */
59 /* Sub state finished go to the next sub state */
61 /* Sub state finished and handshake was completed */
62 SUB_STATE_END_HANDSHAKE
65 static int state_machine(SSL_CONNECTION *s, int server);
66 static void init_read_state_machine(SSL_CONNECTION *s);
67 static SUB_STATE_RETURN read_state_machine(SSL_CONNECTION *s);
68 static void init_write_state_machine(SSL_CONNECTION *s);
69 static SUB_STATE_RETURN write_state_machine(SSL_CONNECTION *s);
71 OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
73 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
78 return sc->statem.hand_state;
81 int SSL_in_init(const SSL *s)
83 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
88 return sc->statem.in_init;
91 int SSL_is_init_finished(const SSL *s)
93 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
98 return !(sc->statem.in_init) && (sc->statem.hand_state == TLS_ST_OK);
101 int SSL_in_before(const SSL *s)
103 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
109 * Historically being "in before" meant before anything had happened. In the
110 * current code though we remain in the "before" state for a while after we
111 * have started the handshake process (e.g. as a server waiting for the
112 * first message to arrive). There "in before" is taken to mean "in before"
113 * and not started any handshake process yet.
115 return (sc->statem.hand_state == TLS_ST_BEFORE)
116 && (sc->statem.state == MSG_FLOW_UNINITED);
119 OSSL_HANDSHAKE_STATE ossl_statem_get_state(SSL_CONNECTION *s)
121 return s != NULL ? s->statem.hand_state : TLS_ST_BEFORE;
125 * Clear the state machine state and reset back to MSG_FLOW_UNINITED
127 void ossl_statem_clear(SSL_CONNECTION *s)
129 s->statem.state = MSG_FLOW_UNINITED;
130 s->statem.hand_state = TLS_ST_BEFORE;
131 ossl_statem_set_in_init(s, 1);
132 s->statem.no_cert_verify = 0;
133 s->statem.ignore_fatal = 0;
137 * Set the state machine up ready for a renegotiation handshake
139 void ossl_statem_set_renegotiate(SSL_CONNECTION *s)
141 ossl_statem_set_in_init(s, 1);
142 s->statem.request_state = TLS_ST_SW_HELLO_REQ;
145 void ossl_statem_send_fatal(SSL_CONNECTION *s, int al)
148 * Some public APIs may call internal functions that fatal error,
149 * which doesn't make sense outside the state machine. Those APIs
150 * that can handle a failure set this flag to avoid errors sending
151 * alerts. Example: getting a wire-formatted certificate for
154 if (s->statem.ignore_fatal)
156 /* We shouldn't call SSLfatal() twice. Once is enough */
157 if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR)
159 ossl_statem_set_in_init(s, 1);
160 s->statem.state = MSG_FLOW_ERROR;
161 if (al != SSL_AD_NO_ALERT)
162 ssl3_send_alert(s, SSL3_AL_FATAL, al);
166 * Error reporting building block that's used instead of ERR_set_error().
167 * In addition to what ERR_set_error() does, this puts the state machine
168 * into an error state and sends an alert if appropriate.
169 * This is a permanent error for the current connection.
171 void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason,
172 const char *fmt, ...)
177 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
180 ossl_statem_send_fatal(s, al);
184 * This macro should only be called if we are already expecting to be in
185 * a fatal error state. We verify that we are, and set it if not (this would
188 #define check_fatal(s) \
190 if (!ossl_assert((s)->statem.in_init \
191 && (s)->statem.state == MSG_FLOW_ERROR)) \
192 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
196 * Discover whether the current connection is in the error state.
198 * Valid return values are:
202 int ossl_statem_in_error(const SSL_CONNECTION *s)
204 if (s->statem.state == MSG_FLOW_ERROR)
210 void ossl_statem_set_in_init(SSL_CONNECTION *s, int init)
212 s->statem.in_init = init;
213 if (s->rlayer.rrlmethod != NULL && s->rlayer.rrlmethod->set_in_init != NULL)
214 s->rlayer.rrlmethod->set_in_init(s->rlayer.rrl, init);
217 int ossl_statem_get_in_handshake(SSL_CONNECTION *s)
219 return s->statem.in_handshake;
222 void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand)
225 s->statem.in_handshake++;
227 s->statem.in_handshake--;
230 /* Are we in a sensible state to skip over unreadable early data? */
231 int ossl_statem_skip_early_data(SSL_CONNECTION *s)
233 if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
237 || s->statem.hand_state != TLS_ST_EARLY_DATA
238 || s->hello_retry_request == SSL_HRR_COMPLETE)
245 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
246 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
247 * data state and whether we should attempt to move the handshake on if so.
248 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
249 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
252 void ossl_statem_check_finish_init(SSL_CONNECTION *s, int sending)
255 if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
256 || s->statem.hand_state == TLS_ST_EARLY_DATA) {
257 ossl_statem_set_in_init(s, 1);
258 if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
260 * SSL_connect() or SSL_do_handshake() has been called directly.
261 * We don't allow any more writing of early data.
263 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
266 } else if (!s->server) {
267 if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
268 || s->statem.hand_state == TLS_ST_EARLY_DATA)
269 && s->early_data_state != SSL_EARLY_DATA_WRITING)
270 || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) {
271 ossl_statem_set_in_init(s, 1);
273 * SSL_write() has been called directly. We don't allow any more
274 * writing of early data.
276 if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY)
277 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
280 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
281 && s->statem.hand_state == TLS_ST_EARLY_DATA)
282 ossl_statem_set_in_init(s, 1);
286 void ossl_statem_set_hello_verify_done(SSL_CONNECTION *s)
288 s->statem.state = MSG_FLOW_UNINITED;
289 ossl_statem_set_in_init(s, 1);
291 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
292 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
293 * calls to SSL_in_before() will return false. Also calls to
294 * SSL_state_string() and SSL_state_string_long() will return something
297 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
300 int ossl_statem_connect(SSL *s)
302 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
307 return state_machine(sc, 0);
310 int ossl_statem_accept(SSL *s)
312 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
317 return state_machine(sc, 1);
320 typedef void (*info_cb) (const SSL *, int, int);
322 static info_cb get_callback(SSL_CONNECTION *s)
324 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
326 if (s->info_callback != NULL)
327 return s->info_callback;
328 else if (sctx->info_callback != NULL)
329 return sctx->info_callback;
335 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
336 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
337 * transitions are as follows:
339 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
341 * +-----------------------+
343 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
351 * We may exit at any point due to an error or NBIO event. If an NBIO event
352 * occurs then we restart at the point we left off when we are recalled.
353 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
355 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
356 * into that state at any point in the event that an irrecoverable error occurs.
358 * Valid return values are:
362 static int state_machine(SSL_CONNECTION *s, int server)
365 void (*cb) (const SSL *ssl, int type, int val) = NULL;
366 OSSL_STATEM *st = &s->statem;
369 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
371 if (st->state == MSG_FLOW_ERROR) {
372 /* Shouldn't have been called if we're already in the error state */
379 cb = get_callback(s);
382 if (!SSL_in_init(ssl) || SSL_in_before(ssl)) {
384 * If we are stateless then we already called SSL_clear() - don't do
385 * it again and clear the STATELESS flag itself.
387 if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(ssl))
390 #ifndef OPENSSL_NO_SCTP
391 if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) {
393 * Notify SCTP BIO socket to enter handshake mode and prevent stream
394 * identifier other than 0.
396 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
397 st->in_handshake, NULL);
401 /* Initialise state machine */
402 if (st->state == MSG_FLOW_UNINITED
403 || st->state == MSG_FLOW_FINISHED) {
404 if (st->state == MSG_FLOW_UNINITED) {
405 st->hand_state = TLS_ST_BEFORE;
406 st->request_state = TLS_ST_BEFORE;
411 if (SSL_IS_FIRST_HANDSHAKE(s) || !SSL_CONNECTION_IS_TLS13(s))
412 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
416 * Fatal errors in this block don't send an alert because we have
417 * failed to even initialise properly. Sending an alert is probably
421 if (SSL_CONNECTION_IS_DTLS(s)) {
422 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
423 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
424 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
428 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
429 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
434 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
435 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
439 if (s->init_buf == NULL) {
440 if ((buf = BUF_MEM_new()) == NULL) {
441 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
444 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
445 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
452 if (!ssl3_setup_buffers(s)) {
453 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
459 * Should have been reset by tls_process_finished, too.
461 s->s3.change_cipher_spec = 0;
464 * Ok, we now need to push on a buffering BIO ...but not with
467 #ifndef OPENSSL_NO_SCTP
468 if (!SSL_CONNECTION_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(ssl)))
470 if (!ssl_init_wbio_buffer(s)) {
471 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
475 if ((SSL_in_before(ssl))
477 if (!tls_setup_handshake(s)) {
478 /* SSLfatal() already called */
482 if (SSL_IS_FIRST_HANDSHAKE(s))
483 st->read_state_first_init = 1;
486 st->state = MSG_FLOW_WRITING;
487 init_write_state_machine(s);
490 while (st->state != MSG_FLOW_FINISHED) {
491 if (st->state == MSG_FLOW_READING) {
492 ssret = read_state_machine(s);
493 if (ssret == SUB_STATE_FINISHED) {
494 st->state = MSG_FLOW_WRITING;
495 init_write_state_machine(s);
500 } else if (st->state == MSG_FLOW_WRITING) {
501 ssret = write_state_machine(s);
502 if (ssret == SUB_STATE_FINISHED) {
503 st->state = MSG_FLOW_READING;
504 init_read_state_machine(s);
505 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
506 st->state = MSG_FLOW_FINISHED;
514 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
524 #ifndef OPENSSL_NO_SCTP
525 if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) {
527 * Notify SCTP BIO socket to leave handshake mode and allow stream
528 * identifier other than 0.
530 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
531 st->in_handshake, NULL);
538 cb(ssl, SSL_CB_ACCEPT_EXIT, ret);
540 cb(ssl, SSL_CB_CONNECT_EXIT, ret);
546 * Initialise the MSG_FLOW_READING sub-state machine
548 static void init_read_state_machine(SSL_CONNECTION *s)
550 OSSL_STATEM *st = &s->statem;
552 st->read_state = READ_STATE_HEADER;
555 static int grow_init_buf(SSL_CONNECTION *s, size_t size) {
557 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
559 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
562 if (size < msg_offset)
565 s->init_msg = s->init_buf->data + msg_offset;
571 * This function implements the sub-state machine when the message flow is in
572 * MSG_FLOW_READING. The valid sub-states and transitions are:
574 * READ_STATE_HEADER <--+<-------------+
577 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
579 * +----------------------------+
581 * [SUB_STATE_FINISHED]
583 * READ_STATE_HEADER has the responsibility for reading in the message header
584 * and transitioning the state of the handshake state machine.
586 * READ_STATE_BODY reads in the rest of the message and then subsequently
589 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
590 * processing activity performed on the message may block.
592 * Any of the above states could result in an NBIO event occurring in which case
593 * control returns to the calling application. When this function is recalled we
594 * will resume in the same state where we left off.
596 static SUB_STATE_RETURN read_state_machine(SSL_CONNECTION *s)
598 OSSL_STATEM *st = &s->statem;
601 int (*transition) (SSL_CONNECTION *s, int mt);
603 MSG_PROCESS_RETURN(*process_message) (SSL_CONNECTION *s, PACKET *pkt);
604 WORK_STATE(*post_process_message) (SSL_CONNECTION *s, WORK_STATE wst);
605 size_t (*max_message_size) (SSL_CONNECTION *s);
606 void (*cb) (const SSL *ssl, int type, int val) = NULL;
607 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
609 cb = get_callback(s);
612 transition = ossl_statem_server_read_transition;
613 process_message = ossl_statem_server_process_message;
614 max_message_size = ossl_statem_server_max_message_size;
615 post_process_message = ossl_statem_server_post_process_message;
617 transition = ossl_statem_client_read_transition;
618 process_message = ossl_statem_client_process_message;
619 max_message_size = ossl_statem_client_max_message_size;
620 post_process_message = ossl_statem_client_post_process_message;
623 if (st->read_state_first_init) {
625 st->read_state_first_init = 0;
629 switch (st->read_state) {
630 case READ_STATE_HEADER:
631 /* Get the state the peer wants to move to */
632 if (SSL_CONNECTION_IS_DTLS(s)) {
634 * In DTLS we get the whole message in one go - header and body
636 ret = dtls_get_message(s, &mt);
638 ret = tls_get_message_header(s, &mt);
642 /* Could be non-blocking IO */
643 return SUB_STATE_ERROR;
647 /* Notify callback of an impending state change */
649 cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
651 cb(ssl, SSL_CB_CONNECT_LOOP, 1);
654 * Validate that we are allowed to move to the new state and move
655 * to that state if so
657 if (!transition(s, mt))
658 return SUB_STATE_ERROR;
660 if (s->s3.tmp.message_size > max_message_size(s)) {
661 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
662 SSL_R_EXCESSIVE_MESSAGE_SIZE);
663 return SUB_STATE_ERROR;
666 /* dtls_get_message already did this */
667 if (!SSL_CONNECTION_IS_DTLS(s)
668 && s->s3.tmp.message_size > 0
669 && !grow_init_buf(s, s->s3.tmp.message_size
670 + SSL3_HM_HEADER_LENGTH)) {
671 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB);
672 return SUB_STATE_ERROR;
675 st->read_state = READ_STATE_BODY;
678 case READ_STATE_BODY:
679 if (SSL_CONNECTION_IS_DTLS(s)) {
681 * Actually we already have the body, but we give DTLS the
682 * opportunity to do any further processing.
684 ret = dtls_get_message_body(s, &len);
686 ret = tls_get_message_body(s, &len);
689 /* Could be non-blocking IO */
690 return SUB_STATE_ERROR;
694 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
695 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
696 return SUB_STATE_ERROR;
698 ret = process_message(s, &pkt);
700 /* Discard the packet data */
704 case MSG_PROCESS_ERROR:
706 return SUB_STATE_ERROR;
708 case MSG_PROCESS_FINISHED_READING:
709 if (SSL_CONNECTION_IS_DTLS(s)) {
712 return SUB_STATE_FINISHED;
714 case MSG_PROCESS_CONTINUE_PROCESSING:
715 st->read_state = READ_STATE_POST_PROCESS;
716 st->read_state_work = WORK_MORE_A;
720 st->read_state = READ_STATE_HEADER;
725 case READ_STATE_POST_PROCESS:
726 st->read_state_work = post_process_message(s, st->read_state_work);
727 switch (st->read_state_work) {
734 return SUB_STATE_ERROR;
736 case WORK_FINISHED_CONTINUE:
737 st->read_state = READ_STATE_HEADER;
740 case WORK_FINISHED_STOP:
741 if (SSL_CONNECTION_IS_DTLS(s)) {
744 return SUB_STATE_FINISHED;
749 /* Shouldn't happen */
750 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
751 return SUB_STATE_ERROR;
757 * Send a previously constructed message to the peer.
759 static int statem_do_write(SSL_CONNECTION *s)
761 OSSL_STATEM *st = &s->statem;
763 if (st->hand_state == TLS_ST_CW_CHANGE
764 || st->hand_state == TLS_ST_SW_CHANGE) {
765 if (SSL_CONNECTION_IS_DTLS(s))
766 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
768 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
770 return ssl_do_write(s);
775 * Initialise the MSG_FLOW_WRITING sub-state machine
777 static void init_write_state_machine(SSL_CONNECTION *s)
779 OSSL_STATEM *st = &s->statem;
781 st->write_state = WRITE_STATE_TRANSITION;
785 * This function implements the sub-state machine when the message flow is in
786 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
788 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
791 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
797 * | WRITE_STATE_POST_WORK
801 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
803 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
804 * sending of the message. This could result in an NBIO event occurring in
805 * which case control returns to the calling application. When this function
806 * is recalled we will resume in the same state where we left off.
808 * WRITE_STATE_SEND sends the message and performs any work to be done after
811 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
812 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
813 * result in an NBIO event.
815 static SUB_STATE_RETURN write_state_machine(SSL_CONNECTION *s)
817 OSSL_STATEM *st = &s->statem;
819 WRITE_TRAN(*transition) (SSL_CONNECTION *s);
820 WORK_STATE(*pre_work) (SSL_CONNECTION *s, WORK_STATE wst);
821 WORK_STATE(*post_work) (SSL_CONNECTION *s, WORK_STATE wst);
822 int (*get_construct_message_f) (SSL_CONNECTION *s,
823 CON_FUNC_RETURN (**confunc) (SSL_CONNECTION *s,
826 void (*cb) (const SSL *ssl, int type, int val) = NULL;
827 CON_FUNC_RETURN (*confunc) (SSL_CONNECTION *s, WPACKET *pkt);
830 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
832 cb = get_callback(s);
835 transition = ossl_statem_server_write_transition;
836 pre_work = ossl_statem_server_pre_work;
837 post_work = ossl_statem_server_post_work;
838 get_construct_message_f = ossl_statem_server_construct_message;
840 transition = ossl_statem_client_write_transition;
841 pre_work = ossl_statem_client_pre_work;
842 post_work = ossl_statem_client_post_work;
843 get_construct_message_f = ossl_statem_client_construct_message;
847 switch (st->write_state) {
848 case WRITE_STATE_TRANSITION:
850 /* Notify callback of an impending state change */
852 cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
854 cb(ssl, SSL_CB_CONNECT_LOOP, 1);
856 switch (transition(s)) {
857 case WRITE_TRAN_CONTINUE:
858 st->write_state = WRITE_STATE_PRE_WORK;
859 st->write_state_work = WORK_MORE_A;
862 case WRITE_TRAN_FINISHED:
863 return SUB_STATE_FINISHED;
866 case WRITE_TRAN_ERROR:
868 return SUB_STATE_ERROR;
872 case WRITE_STATE_PRE_WORK:
873 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
880 return SUB_STATE_ERROR;
882 case WORK_FINISHED_CONTINUE:
883 st->write_state = WRITE_STATE_SEND;
886 case WORK_FINISHED_STOP:
887 return SUB_STATE_END_HANDSHAKE;
889 if (!get_construct_message_f(s, &confunc, &mt)) {
890 /* SSLfatal() already called */
891 return SUB_STATE_ERROR;
893 if (mt == SSL3_MT_DUMMY) {
894 /* Skip construction and sending. This isn't a "real" state */
895 st->write_state = WRITE_STATE_POST_WORK;
896 st->write_state_work = WORK_MORE_A;
899 if (!WPACKET_init(&pkt, s->init_buf)
900 || !ssl_set_handshake_header(s, &pkt, mt)) {
901 WPACKET_cleanup(&pkt);
902 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
903 return SUB_STATE_ERROR;
905 if (confunc != NULL) {
906 CON_FUNC_RETURN tmpret;
908 tmpret = confunc(s, &pkt);
909 if (tmpret == CON_FUNC_ERROR) {
910 WPACKET_cleanup(&pkt);
912 return SUB_STATE_ERROR;
913 } else if (tmpret == CON_FUNC_DONT_SEND) {
915 * The construction function decided not to construct the
916 * message after all and continue. Skip sending.
918 WPACKET_cleanup(&pkt);
919 st->write_state = WRITE_STATE_POST_WORK;
920 st->write_state_work = WORK_MORE_A;
924 if (!ssl_close_construct_packet(s, &pkt, mt)
925 || !WPACKET_finish(&pkt)) {
926 WPACKET_cleanup(&pkt);
927 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
928 return SUB_STATE_ERROR;
933 case WRITE_STATE_SEND:
934 if (SSL_CONNECTION_IS_DTLS(s) && st->use_timer) {
935 dtls1_start_timer(s);
937 ret = statem_do_write(s);
939 return SUB_STATE_ERROR;
941 st->write_state = WRITE_STATE_POST_WORK;
942 st->write_state_work = WORK_MORE_A;
945 case WRITE_STATE_POST_WORK:
946 switch (st->write_state_work = post_work(s, st->write_state_work)) {
953 return SUB_STATE_ERROR;
955 case WORK_FINISHED_CONTINUE:
956 st->write_state = WRITE_STATE_TRANSITION;
959 case WORK_FINISHED_STOP:
960 return SUB_STATE_END_HANDSHAKE;
965 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
966 return SUB_STATE_ERROR;
972 * Flush the write BIO
974 int statem_flush(SSL_CONNECTION *s)
976 s->rwstate = SSL_WRITING;
977 if (BIO_flush(s->wbio) <= 0) {
980 s->rwstate = SSL_NOTHING;
986 * Called by the record layer to determine whether application data is
987 * allowed to be received in the current handshake state or not.
990 * 1: Yes (application data allowed)
991 * 0: No (application data not allowed)
993 int ossl_statem_app_data_allowed(SSL_CONNECTION *s)
995 OSSL_STATEM *st = &s->statem;
997 if (st->state == MSG_FLOW_UNINITED)
1000 if (!s->s3.in_read_app_data || (s->s3.total_renegotiations == 0))
1005 * If we're a server and we haven't got as far as writing our
1006 * ServerHello yet then we allow app data
1008 if (st->hand_state == TLS_ST_BEFORE
1009 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
1013 * If we're a client and we haven't read the ServerHello yet then we
1016 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
1024 * This function returns 1 if TLS exporter is ready to export keying
1025 * material, or 0 if otherwise.
1027 int ossl_statem_export_allowed(SSL_CONNECTION *s)
1029 return s->s3.previous_server_finished_len != 0
1030 && s->statem.hand_state != TLS_ST_SW_FINISHED;
1034 * Return 1 if early TLS exporter is ready to export keying material,
1035 * or 0 if otherwise.
1037 int ossl_statem_export_early_allowed(SSL_CONNECTION *s)
1040 * The early exporter secret is only present on the server if we
1041 * have accepted early_data. It is present on the client as long
1042 * as we have sent early_data.
1044 return s->ext.early_data == SSL_EARLY_DATA_ACCEPTED
1045 || (!s->server && s->ext.early_data != SSL_EARLY_DATA_NOT_SENT);