2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 /* We shouldn't call SSLfatal() twice. Once is enough */
122 assert(s->statem.state != MSG_FLOW_ERROR);
123 s->statem.in_init = 1;
124 s->statem.state = MSG_FLOW_ERROR;
125 ERR_put_error(ERR_LIB_SSL, func, reason, file, line);
126 if (al != SSL_AD_NO_ALERT
127 && s->statem.enc_write_state != ENC_WRITE_STATE_INVALID)
128 ssl3_send_alert(s, SSL3_AL_FATAL, al);
132 * This macro should only be called if we are already expecting to be in
133 * a fatal error state. We verify that we are, and set it if not (this would
136 #define check_fatal(s, f) \
138 if (!ossl_assert((s)->statem.in_init \
139 && (s)->statem.state == MSG_FLOW_ERROR)) \
140 SSLfatal(s, SSL_AD_INTERNAL_ERROR, (f), \
141 SSL_R_MISSING_FATAL); \
145 * Discover whether the current connection is in the error state.
147 * Valid return values are:
151 int ossl_statem_in_error(const SSL *s)
153 if (s->statem.state == MSG_FLOW_ERROR)
159 void ossl_statem_set_in_init(SSL *s, int init)
161 s->statem.in_init = init;
164 int ossl_statem_get_in_handshake(SSL *s)
166 return s->statem.in_handshake;
169 void ossl_statem_set_in_handshake(SSL *s, int inhand)
172 s->statem.in_handshake++;
174 s->statem.in_handshake--;
177 /* Are we in a sensible state to skip over unreadable early data? */
178 int ossl_statem_skip_early_data(SSL *s)
180 if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
184 || s->statem.hand_state != TLS_ST_EARLY_DATA
185 || s->hello_retry_request == SSL_HRR_COMPLETE)
192 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
193 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
194 * data state and whether we should attempt to move the handshake on if so.
195 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
196 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
199 void ossl_statem_check_finish_init(SSL *s, int sending)
202 if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
203 || s->statem.hand_state == TLS_ST_EARLY_DATA) {
204 ossl_statem_set_in_init(s, 1);
205 if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
207 * SSL_connect() or SSL_do_handshake() has been called directly.
208 * We don't allow any more writing of early data.
210 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
213 } else if (!s->server) {
214 if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
215 || s->statem.hand_state == TLS_ST_EARLY_DATA)
216 && s->early_data_state != SSL_EARLY_DATA_WRITING)
217 || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) {
218 ossl_statem_set_in_init(s, 1);
220 * SSL_write() has been called directly. We don't allow any more
221 * writing of early data.
223 if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY)
224 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
227 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
228 && s->statem.hand_state == TLS_ST_EARLY_DATA)
229 ossl_statem_set_in_init(s, 1);
233 void ossl_statem_set_hello_verify_done(SSL *s)
235 s->statem.state = MSG_FLOW_UNINITED;
236 s->statem.in_init = 1;
238 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
239 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
240 * calls to SSL_in_before() will return false. Also calls to
241 * SSL_state_string() and SSL_state_string_long() will return something
244 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
247 int ossl_statem_connect(SSL *s)
249 return state_machine(s, 0);
252 int ossl_statem_accept(SSL *s)
254 return state_machine(s, 1);
257 typedef void (*info_cb) (const SSL *, int, int);
259 static info_cb get_callback(SSL *s)
261 if (s->info_callback != NULL)
262 return s->info_callback;
263 else if (s->ctx->info_callback != NULL)
264 return s->ctx->info_callback;
270 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
271 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
272 * transitions are as follows:
274 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
276 * +-----------------------+
278 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
286 * We may exit at any point due to an error or NBIO event. If an NBIO event
287 * occurs then we restart at the point we left off when we are recalled.
288 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
290 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
291 * into that state at any point in the event that an irrecoverable error occurs.
293 * Valid return values are:
297 static int state_machine(SSL *s, int server)
300 void (*cb) (const SSL *ssl, int type, int val) = NULL;
301 OSSL_STATEM *st = &s->statem;
305 if (st->state == MSG_FLOW_ERROR) {
306 /* Shouldn't have been called if we're already in the error state */
313 cb = get_callback(s);
316 if (!SSL_in_init(s) || SSL_in_before(s)) {
318 * If we are stateless then we already called SSL_clear() - don't do
319 * it again and clear the STATELESS flag itself.
321 if ((s->s3->flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(s))
324 #ifndef OPENSSL_NO_SCTP
325 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
327 * Notify SCTP BIO socket to enter handshake mode and prevent stream
328 * identifier other than 0.
330 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
331 st->in_handshake, NULL);
335 /* Initialise state machine */
336 if (st->state == MSG_FLOW_UNINITED
337 || st->state == MSG_FLOW_FINISHED) {
338 if (st->state == MSG_FLOW_UNINITED) {
339 st->hand_state = TLS_ST_BEFORE;
340 st->request_state = TLS_ST_BEFORE;
345 cb(s, SSL_CB_HANDSHAKE_START, 1);
348 * Fatal errors in this block don't send an alert because we have
349 * failed to even initialise properly. Sending an alert is probably
353 if (SSL_IS_DTLS(s)) {
354 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
355 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
356 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
357 ERR_R_INTERNAL_ERROR);
361 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
362 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
363 ERR_R_INTERNAL_ERROR);
368 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
369 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
370 ERR_R_INTERNAL_ERROR);
374 if (s->init_buf == NULL) {
375 if ((buf = BUF_MEM_new()) == NULL) {
376 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
377 ERR_R_INTERNAL_ERROR);
380 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
381 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
382 ERR_R_INTERNAL_ERROR);
389 if (!ssl3_setup_buffers(s)) {
390 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
391 ERR_R_INTERNAL_ERROR);
397 * Should have been reset by tls_process_finished, too.
399 s->s3->change_cipher_spec = 0;
402 * Ok, we now need to push on a buffering BIO ...but not with
405 #ifndef OPENSSL_NO_SCTP
406 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
408 if (!ssl_init_wbio_buffer(s)) {
409 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
410 ERR_R_INTERNAL_ERROR);
414 if ((SSL_in_before(s))
416 if (!tls_setup_handshake(s)) {
417 /* SSLfatal() already called */
421 if (SSL_IS_FIRST_HANDSHAKE(s))
422 st->read_state_first_init = 1;
425 st->state = MSG_FLOW_WRITING;
426 init_write_state_machine(s);
429 while (st->state != MSG_FLOW_FINISHED) {
430 if (st->state == MSG_FLOW_READING) {
431 ssret = read_state_machine(s);
432 if (ssret == SUB_STATE_FINISHED) {
433 st->state = MSG_FLOW_WRITING;
434 init_write_state_machine(s);
439 } else if (st->state == MSG_FLOW_WRITING) {
440 ssret = write_state_machine(s);
441 if (ssret == SUB_STATE_FINISHED) {
442 st->state = MSG_FLOW_READING;
443 init_read_state_machine(s);
444 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
445 st->state = MSG_FLOW_FINISHED;
452 check_fatal(s, SSL_F_STATE_MACHINE);
453 SSLerr(SSL_F_STATE_MACHINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
463 #ifndef OPENSSL_NO_SCTP
464 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
466 * Notify SCTP BIO socket to leave handshake mode and allow stream
467 * identifier other than 0.
469 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
470 st->in_handshake, NULL);
477 cb(s, SSL_CB_ACCEPT_EXIT, ret);
479 cb(s, SSL_CB_CONNECT_EXIT, ret);
485 * Initialise the MSG_FLOW_READING sub-state machine
487 static void init_read_state_machine(SSL *s)
489 OSSL_STATEM *st = &s->statem;
491 st->read_state = READ_STATE_HEADER;
494 static int grow_init_buf(SSL *s, size_t size) {
496 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
498 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
501 if (size < msg_offset)
504 s->init_msg = s->init_buf->data + msg_offset;
510 * This function implements the sub-state machine when the message flow is in
511 * MSG_FLOW_READING. The valid sub-states and transitions are:
513 * READ_STATE_HEADER <--+<-------------+
516 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
518 * +----------------------------+
520 * [SUB_STATE_FINISHED]
522 * READ_STATE_HEADER has the responsibility for reading in the message header
523 * and transitioning the state of the handshake state machine.
525 * READ_STATE_BODY reads in the rest of the message and then subsequently
528 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
529 * processing activity performed on the message may block.
531 * Any of the above states could result in an NBIO event occurring in which case
532 * control returns to the calling application. When this function is recalled we
533 * will resume in the same state where we left off.
535 static SUB_STATE_RETURN read_state_machine(SSL *s)
537 OSSL_STATEM *st = &s->statem;
540 int (*transition) (SSL *s, int mt);
542 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
543 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
544 size_t (*max_message_size) (SSL *s);
545 void (*cb) (const SSL *ssl, int type, int val) = NULL;
547 cb = get_callback(s);
550 transition = ossl_statem_server_read_transition;
551 process_message = ossl_statem_server_process_message;
552 max_message_size = ossl_statem_server_max_message_size;
553 post_process_message = ossl_statem_server_post_process_message;
555 transition = ossl_statem_client_read_transition;
556 process_message = ossl_statem_client_process_message;
557 max_message_size = ossl_statem_client_max_message_size;
558 post_process_message = ossl_statem_client_post_process_message;
561 if (st->read_state_first_init) {
563 st->read_state_first_init = 0;
567 switch (st->read_state) {
568 case READ_STATE_HEADER:
569 /* Get the state the peer wants to move to */
570 if (SSL_IS_DTLS(s)) {
572 * In DTLS we get the whole message in one go - header and body
574 ret = dtls_get_message(s, &mt, &len);
576 ret = tls_get_message_header(s, &mt);
580 /* Could be non-blocking IO */
581 return SUB_STATE_ERROR;
585 /* Notify callback of an impending state change */
587 cb(s, SSL_CB_ACCEPT_LOOP, 1);
589 cb(s, SSL_CB_CONNECT_LOOP, 1);
592 * Validate that we are allowed to move to the new state and move
593 * to that state if so
595 if (!transition(s, mt))
596 return SUB_STATE_ERROR;
598 if (s->s3->tmp.message_size > max_message_size(s)) {
599 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_READ_STATE_MACHINE,
600 SSL_R_EXCESSIVE_MESSAGE_SIZE);
601 return SUB_STATE_ERROR;
604 /* dtls_get_message already did this */
606 && s->s3->tmp.message_size > 0
607 && !grow_init_buf(s, s->s3->tmp.message_size
608 + SSL3_HM_HEADER_LENGTH)) {
609 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_READ_STATE_MACHINE,
611 return SUB_STATE_ERROR;
614 st->read_state = READ_STATE_BODY;
617 case READ_STATE_BODY:
618 if (!SSL_IS_DTLS(s)) {
619 /* We already got this above for DTLS */
620 ret = tls_get_message_body(s, &len);
622 /* Could be non-blocking IO */
623 return SUB_STATE_ERROR;
628 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
629 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_READ_STATE_MACHINE,
630 ERR_R_INTERNAL_ERROR);
631 return SUB_STATE_ERROR;
633 ret = process_message(s, &pkt);
635 /* Discard the packet data */
639 case MSG_PROCESS_ERROR:
640 check_fatal(s, SSL_F_READ_STATE_MACHINE);
641 return SUB_STATE_ERROR;
643 case MSG_PROCESS_FINISHED_READING:
644 if (SSL_IS_DTLS(s)) {
647 return SUB_STATE_FINISHED;
649 case MSG_PROCESS_CONTINUE_PROCESSING:
650 st->read_state = READ_STATE_POST_PROCESS;
651 st->read_state_work = WORK_MORE_A;
655 st->read_state = READ_STATE_HEADER;
660 case READ_STATE_POST_PROCESS:
661 st->read_state_work = post_process_message(s, st->read_state_work);
662 switch (st->read_state_work) {
664 check_fatal(s, SSL_F_READ_STATE_MACHINE);
669 return SUB_STATE_ERROR;
671 case WORK_FINISHED_CONTINUE:
672 st->read_state = READ_STATE_HEADER;
675 case WORK_FINISHED_STOP:
676 if (SSL_IS_DTLS(s)) {
679 return SUB_STATE_FINISHED;
684 /* Shouldn't happen */
685 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_READ_STATE_MACHINE,
686 ERR_R_INTERNAL_ERROR);
687 return SUB_STATE_ERROR;
693 * Send a previously constructed message to the peer.
695 static int statem_do_write(SSL *s)
697 OSSL_STATEM *st = &s->statem;
699 if (st->hand_state == TLS_ST_CW_CHANGE
700 || st->hand_state == TLS_ST_SW_CHANGE) {
702 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
704 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
706 return ssl_do_write(s);
711 * Initialise the MSG_FLOW_WRITING sub-state machine
713 static void init_write_state_machine(SSL *s)
715 OSSL_STATEM *st = &s->statem;
717 st->write_state = WRITE_STATE_TRANSITION;
721 * This function implements the sub-state machine when the message flow is in
722 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
724 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
727 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
733 * | WRITE_STATE_POST_WORK
737 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
739 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
740 * sending of the message. This could result in an NBIO event occurring in
741 * which case control returns to the calling application. When this function
742 * is recalled we will resume in the same state where we left off.
744 * WRITE_STATE_SEND sends the message and performs any work to be done after
747 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
748 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
749 * result in an NBIO event.
751 static SUB_STATE_RETURN write_state_machine(SSL *s)
753 OSSL_STATEM *st = &s->statem;
755 WRITE_TRAN(*transition) (SSL *s);
756 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
757 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
758 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
759 int (**confunc) (SSL *s, WPACKET *pkt),
761 void (*cb) (const SSL *ssl, int type, int val) = NULL;
762 int (*confunc) (SSL *s, WPACKET *pkt);
766 cb = get_callback(s);
769 transition = ossl_statem_server_write_transition;
770 pre_work = ossl_statem_server_pre_work;
771 post_work = ossl_statem_server_post_work;
772 get_construct_message_f = ossl_statem_server_construct_message;
774 transition = ossl_statem_client_write_transition;
775 pre_work = ossl_statem_client_pre_work;
776 post_work = ossl_statem_client_post_work;
777 get_construct_message_f = ossl_statem_client_construct_message;
781 switch (st->write_state) {
782 case WRITE_STATE_TRANSITION:
784 /* Notify callback of an impending state change */
786 cb(s, SSL_CB_ACCEPT_LOOP, 1);
788 cb(s, SSL_CB_CONNECT_LOOP, 1);
790 switch (transition(s)) {
791 case WRITE_TRAN_CONTINUE:
792 st->write_state = WRITE_STATE_PRE_WORK;
793 st->write_state_work = WORK_MORE_A;
796 case WRITE_TRAN_FINISHED:
797 return SUB_STATE_FINISHED;
800 case WRITE_TRAN_ERROR:
801 check_fatal(s, SSL_F_WRITE_STATE_MACHINE);
802 return SUB_STATE_ERROR;
806 case WRITE_STATE_PRE_WORK:
807 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
809 check_fatal(s, SSL_F_WRITE_STATE_MACHINE);
814 return SUB_STATE_ERROR;
816 case WORK_FINISHED_CONTINUE:
817 st->write_state = WRITE_STATE_SEND;
820 case WORK_FINISHED_STOP:
821 return SUB_STATE_END_HANDSHAKE;
823 if (!get_construct_message_f(s, &pkt, &confunc, &mt)) {
824 /* SSLfatal() already called */
825 return SUB_STATE_ERROR;
827 if (mt == SSL3_MT_DUMMY) {
828 /* Skip construction and sending. This isn't a "real" state */
829 st->write_state = WRITE_STATE_POST_WORK;
830 st->write_state_work = WORK_MORE_A;
833 if (!WPACKET_init(&pkt, s->init_buf)
834 || !ssl_set_handshake_header(s, &pkt, mt)) {
835 WPACKET_cleanup(&pkt);
836 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_WRITE_STATE_MACHINE,
837 ERR_R_INTERNAL_ERROR);
838 return SUB_STATE_ERROR;
840 if (confunc != NULL && !confunc(s, &pkt)) {
841 WPACKET_cleanup(&pkt);
842 check_fatal(s, SSL_F_WRITE_STATE_MACHINE);
843 return SUB_STATE_ERROR;
845 if (!ssl_close_construct_packet(s, &pkt, mt)
846 || !WPACKET_finish(&pkt)) {
847 WPACKET_cleanup(&pkt);
848 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_WRITE_STATE_MACHINE,
849 ERR_R_INTERNAL_ERROR);
850 return SUB_STATE_ERROR;
855 case WRITE_STATE_SEND:
856 if (SSL_IS_DTLS(s) && st->use_timer) {
857 dtls1_start_timer(s);
859 ret = statem_do_write(s);
861 return SUB_STATE_ERROR;
863 st->write_state = WRITE_STATE_POST_WORK;
864 st->write_state_work = WORK_MORE_A;
867 case WRITE_STATE_POST_WORK:
868 switch (st->write_state_work = post_work(s, st->write_state_work)) {
870 check_fatal(s, SSL_F_WRITE_STATE_MACHINE);
875 return SUB_STATE_ERROR;
877 case WORK_FINISHED_CONTINUE:
878 st->write_state = WRITE_STATE_TRANSITION;
881 case WORK_FINISHED_STOP:
882 return SUB_STATE_END_HANDSHAKE;
887 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_WRITE_STATE_MACHINE,
888 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);