2 * Copyright 2015-2016 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 <openssl/rand.h>
11 #include "../ssl_locl.h"
12 #include "statem_locl.h"
15 * This file implements the SSL/TLS/DTLS state machines.
17 * There are two primary state machines:
19 * 1) Message flow state machine
20 * 2) Handshake state machine
22 * The Message flow state machine controls the reading and sending of messages
23 * including handling of non-blocking IO events, flushing of the underlying
24 * write BIO, handling unexpected messages, etc. It is itself broken into two
25 * separate sub-state machines which control reading and writing respectively.
27 * The Handshake state machine keeps track of the current SSL/TLS handshake
28 * state. Transitions of the handshake state are the result of events that
29 * occur within the Message flow state machine.
31 * Overall it looks like this:
33 * --------------------------------------------- -------------------
35 * | Message flow state machine | | |
37 * | -------------------- -------------------- | Transition | Handshake state |
38 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
39 * | | sub-state | | sub-state | |----------->| |
40 * | | machine for | | machine for | | | |
41 * | | reading messages | | writing messages | | | |
42 * | -------------------- -------------------- | | |
44 * --------------------------------------------- -------------------
48 /* Sub state machine return values */
50 /* Something bad happened or NBIO */
52 /* Sub state finished go to the next sub state */
54 /* Sub state finished and handshake was completed */
55 SUB_STATE_END_HANDSHAKE
58 static int state_machine(SSL *s, int server);
59 static void init_read_state_machine(SSL *s);
60 static SUB_STATE_RETURN read_state_machine(SSL *s);
61 static void init_write_state_machine(SSL *s);
62 static SUB_STATE_RETURN write_state_machine(SSL *s);
64 OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
66 return ssl->statem.hand_state;
69 int SSL_in_init(SSL *s)
71 return s->statem.in_init;
74 int SSL_is_init_finished(SSL *s)
76 return !(s->statem.in_init) && (s->statem.hand_state == TLS_ST_OK);
79 int SSL_in_before(SSL *s)
82 * Historically being "in before" meant before anything had happened. In the
83 * current code though we remain in the "before" state for a while after we
84 * have started the handshake process (e.g. as a server waiting for the
85 * first message to arrive). There "in before" is taken to mean "in before"
86 * and not started any handshake process yet.
88 return (s->statem.hand_state == TLS_ST_BEFORE)
89 && (s->statem.state == MSG_FLOW_UNINITED);
93 * Clear the state machine state and reset back to MSG_FLOW_UNINITED
95 void ossl_statem_clear(SSL *s)
97 s->statem.state = MSG_FLOW_UNINITED;
98 s->statem.hand_state = TLS_ST_BEFORE;
99 s->statem.in_init = 1;
100 s->statem.no_cert_verify = 0;
104 * Set the state machine up ready for a renegotiation handshake
106 void ossl_statem_set_renegotiate(SSL *s)
108 s->statem.in_init = 1;
109 s->statem.request_state = TLS_ST_SW_HELLO_REQ;
113 * Put the state machine into an error state. This is a permanent error for
114 * the current connection.
116 void ossl_statem_set_error(SSL *s)
118 s->statem.state = MSG_FLOW_ERROR;
122 * Discover whether the current connection is in the error state.
124 * Valid return values are:
128 int ossl_statem_in_error(const SSL *s)
130 if (s->statem.state == MSG_FLOW_ERROR)
136 void ossl_statem_set_in_init(SSL *s, int init)
138 s->statem.in_init = init;
141 int ossl_statem_get_in_handshake(SSL *s)
143 return s->statem.in_handshake;
146 void ossl_statem_set_in_handshake(SSL *s, int inhand)
149 s->statem.in_handshake++;
151 s->statem.in_handshake--;
154 /* Are we in a sensible state to skip over unreadable early data? */
155 int ossl_statem_skip_early_data(SSL *s)
157 if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
160 if (s->hello_retry_request) {
161 if (s->statem.hand_state != TLS_ST_SW_HELLO_RETRY_REQUEST)
164 if (!s->server || s->statem.hand_state != TLS_ST_EARLY_DATA)
172 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
173 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
174 * data state and whether we should attempt to move the handshake on if so.
175 * |send| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
176 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
179 void ossl_statem_check_finish_init(SSL *s, int send)
182 if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
183 || s->statem.hand_state == TLS_ST_EARLY_DATA)
184 ossl_statem_set_in_init(s, 1);
185 } else if (!s->server) {
186 if ((send && s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
187 && s->early_data_state != SSL_EARLY_DATA_WRITING)
188 || (!send && s->statem.hand_state == TLS_ST_EARLY_DATA))
189 ossl_statem_set_in_init(s, 1);
191 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
192 && s->statem.hand_state == TLS_ST_EARLY_DATA)
193 ossl_statem_set_in_init(s, 1);
197 void ossl_statem_set_hello_verify_done(SSL *s)
199 s->statem.state = MSG_FLOW_UNINITED;
200 s->statem.in_init = 1;
202 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
203 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
204 * calls to SSL_in_before() will return false. Also calls to
205 * SSL_state_string() and SSL_state_string_long() will return something
208 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
211 int ossl_statem_connect(SSL *s)
213 return state_machine(s, 0);
216 int ossl_statem_accept(SSL *s)
218 return state_machine(s, 1);
221 typedef void (*info_cb) (const SSL *, int, int);
223 static info_cb get_callback(SSL *s)
225 if (s->info_callback != NULL)
226 return s->info_callback;
227 else if (s->ctx->info_callback != NULL)
228 return s->ctx->info_callback;
234 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
235 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
236 * transitions are as follows:
238 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
240 * +-----------------------+
242 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
250 * We may exit at any point due to an error or NBIO event. If an NBIO event
251 * occurs then we restart at the point we left off when we are recalled.
252 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
254 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
255 * into that state at any point in the event that an irrecoverable error occurs.
257 * Valid return values are:
261 static int state_machine(SSL *s, int server)
264 unsigned long Time = (unsigned long)time(NULL);
265 void (*cb) (const SSL *ssl, int type, int val) = NULL;
266 OSSL_STATEM *st = &s->statem;
270 if (st->state == MSG_FLOW_ERROR) {
271 /* Shouldn't have been called if we're already in the error state */
275 RAND_add(&Time, sizeof(Time), 0);
279 cb = get_callback(s);
282 if (!SSL_in_init(s) || SSL_in_before(s)) {
286 #ifndef OPENSSL_NO_SCTP
287 if (SSL_IS_DTLS(s)) {
289 * Notify SCTP BIO socket to enter handshake mode and prevent stream
290 * identifier other than 0. Will be ignored if no SCTP is used.
292 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
293 st->in_handshake, NULL);
297 /* Initialise state machine */
298 if (st->state == MSG_FLOW_UNINITED
299 || st->state == MSG_FLOW_FINISHED) {
300 if (st->state == MSG_FLOW_UNINITED) {
301 st->hand_state = TLS_ST_BEFORE;
302 st->request_state = TLS_ST_BEFORE;
307 cb(s, SSL_CB_HANDSHAKE_START, 1);
309 if (SSL_IS_DTLS(s)) {
310 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
311 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
312 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
316 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
317 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
322 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
323 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
327 if (s->init_buf == NULL) {
328 if ((buf = BUF_MEM_new()) == NULL) {
331 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
338 if (!ssl3_setup_buffers(s)) {
344 * Should have been reset by tls_process_finished, too.
346 s->s3->change_cipher_spec = 0;
349 * Ok, we now need to push on a buffering BIO ...but not with
352 #ifndef OPENSSL_NO_SCTP
353 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
355 if (!ssl_init_wbio_buffer(s)) {
359 if ((SSL_in_before(s))
361 if (!tls_setup_handshake(s)) {
362 ossl_statem_set_error(s);
366 if (SSL_IS_FIRST_HANDSHAKE(s))
367 st->read_state_first_init = 1;
370 st->state = MSG_FLOW_WRITING;
371 init_write_state_machine(s);
374 while (st->state != MSG_FLOW_FINISHED) {
375 if (st->state == MSG_FLOW_READING) {
376 ssret = read_state_machine(s);
377 if (ssret == SUB_STATE_FINISHED) {
378 st->state = MSG_FLOW_WRITING;
379 init_write_state_machine(s);
384 } else if (st->state == MSG_FLOW_WRITING) {
385 ssret = write_state_machine(s);
386 if (ssret == SUB_STATE_FINISHED) {
387 st->state = MSG_FLOW_READING;
388 init_read_state_machine(s);
389 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
390 st->state = MSG_FLOW_FINISHED;
397 ossl_statem_set_error(s);
407 #ifndef OPENSSL_NO_SCTP
408 if (SSL_IS_DTLS(s)) {
410 * Notify SCTP BIO socket to leave handshake mode and allow stream
411 * identifier other than 0. Will be ignored if no SCTP is used.
413 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
414 st->in_handshake, NULL);
421 cb(s, SSL_CB_ACCEPT_EXIT, ret);
423 cb(s, SSL_CB_CONNECT_EXIT, ret);
429 * Initialise the MSG_FLOW_READING sub-state machine
431 static void init_read_state_machine(SSL *s)
433 OSSL_STATEM *st = &s->statem;
435 st->read_state = READ_STATE_HEADER;
438 static int grow_init_buf(SSL *s, size_t size) {
440 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
442 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
445 if (size < msg_offset)
448 s->init_msg = s->init_buf->data + msg_offset;
454 * This function implements the sub-state machine when the message flow is in
455 * MSG_FLOW_READING. The valid sub-states and transitions are:
457 * READ_STATE_HEADER <--+<-------------+
460 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
462 * +----------------------------+
464 * [SUB_STATE_FINISHED]
466 * READ_STATE_HEADER has the responsibility for reading in the message header
467 * and transitioning the state of the handshake state machine.
469 * READ_STATE_BODY reads in the rest of the message and then subsequently
472 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
473 * processing activity performed on the message may block.
475 * Any of the above states could result in an NBIO event occurring in which case
476 * control returns to the calling application. When this function is recalled we
477 * will resume in the same state where we left off.
479 static SUB_STATE_RETURN read_state_machine(SSL *s)
481 OSSL_STATEM *st = &s->statem;
484 int (*transition) (SSL *s, int mt);
486 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
487 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
488 size_t (*max_message_size) (SSL *s);
489 void (*cb) (const SSL *ssl, int type, int val) = NULL;
491 cb = get_callback(s);
494 transition = ossl_statem_server_read_transition;
495 process_message = ossl_statem_server_process_message;
496 max_message_size = ossl_statem_server_max_message_size;
497 post_process_message = ossl_statem_server_post_process_message;
499 transition = ossl_statem_client_read_transition;
500 process_message = ossl_statem_client_process_message;
501 max_message_size = ossl_statem_client_max_message_size;
502 post_process_message = ossl_statem_client_post_process_message;
505 if (st->read_state_first_init) {
507 st->read_state_first_init = 0;
511 switch (st->read_state) {
512 case READ_STATE_HEADER:
513 /* Get the state the peer wants to move to */
514 if (SSL_IS_DTLS(s)) {
516 * In DTLS we get the whole message in one go - header and body
518 ret = dtls_get_message(s, &mt, &len);
520 ret = tls_get_message_header(s, &mt);
524 /* Could be non-blocking IO */
525 return SUB_STATE_ERROR;
529 /* Notify callback of an impending state change */
531 cb(s, SSL_CB_ACCEPT_LOOP, 1);
533 cb(s, SSL_CB_CONNECT_LOOP, 1);
536 * Validate that we are allowed to move to the new state and move
537 * to that state if so
539 if (!transition(s, mt)) {
540 ossl_statem_set_error(s);
541 return SUB_STATE_ERROR;
544 if (s->s3->tmp.message_size > max_message_size(s)) {
545 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
546 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
547 return SUB_STATE_ERROR;
550 /* dtls_get_message already did this */
552 && s->s3->tmp.message_size > 0
553 && !grow_init_buf(s, s->s3->tmp.message_size
554 + SSL3_HM_HEADER_LENGTH)) {
555 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
556 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
557 return SUB_STATE_ERROR;
560 st->read_state = READ_STATE_BODY;
563 case READ_STATE_BODY:
564 if (!SSL_IS_DTLS(s)) {
565 /* We already got this above for DTLS */
566 ret = tls_get_message_body(s, &len);
568 /* Could be non-blocking IO */
569 return SUB_STATE_ERROR;
574 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
575 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
576 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
577 return SUB_STATE_ERROR;
579 ret = process_message(s, &pkt);
581 /* Discard the packet data */
585 case MSG_PROCESS_ERROR:
586 return SUB_STATE_ERROR;
588 case MSG_PROCESS_FINISHED_READING:
589 if (SSL_IS_DTLS(s)) {
592 return SUB_STATE_FINISHED;
594 case MSG_PROCESS_CONTINUE_PROCESSING:
595 st->read_state = READ_STATE_POST_PROCESS;
596 st->read_state_work = WORK_MORE_A;
600 st->read_state = READ_STATE_HEADER;
605 case READ_STATE_POST_PROCESS:
606 st->read_state_work = post_process_message(s, st->read_state_work);
607 switch (st->read_state_work) {
612 return SUB_STATE_ERROR;
614 case WORK_FINISHED_CONTINUE:
615 st->read_state = READ_STATE_HEADER;
618 case WORK_FINISHED_STOP:
619 if (SSL_IS_DTLS(s)) {
622 return SUB_STATE_FINISHED;
627 /* Shouldn't happen */
628 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
629 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
630 ossl_statem_set_error(s);
631 return SUB_STATE_ERROR;
637 * Send a previously constructed message to the peer.
639 static int statem_do_write(SSL *s)
641 OSSL_STATEM *st = &s->statem;
643 if (st->hand_state == TLS_ST_CW_CHANGE
644 || st->hand_state == TLS_ST_SW_CHANGE) {
646 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
648 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
650 return ssl_do_write(s);
655 * Initialise the MSG_FLOW_WRITING sub-state machine
657 static void init_write_state_machine(SSL *s)
659 OSSL_STATEM *st = &s->statem;
661 st->write_state = WRITE_STATE_TRANSITION;
665 * This function implements the sub-state machine when the message flow is in
666 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
668 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
671 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
677 * | WRITE_STATE_POST_WORK
681 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
683 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
684 * sending of the message. This could result in an NBIO event occurring in
685 * which case control returns to the calling application. When this function
686 * is recalled we will resume in the same state where we left off.
688 * WRITE_STATE_SEND sends the message and performs any work to be done after
691 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
692 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
693 * result in an NBIO event.
695 static SUB_STATE_RETURN write_state_machine(SSL *s)
697 OSSL_STATEM *st = &s->statem;
699 WRITE_TRAN(*transition) (SSL *s);
700 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
701 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
702 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
703 int (**confunc) (SSL *s, WPACKET *pkt),
705 void (*cb) (const SSL *ssl, int type, int val) = NULL;
706 int (*confunc) (SSL *s, WPACKET *pkt);
710 cb = get_callback(s);
713 transition = ossl_statem_server_write_transition;
714 pre_work = ossl_statem_server_pre_work;
715 post_work = ossl_statem_server_post_work;
716 get_construct_message_f = ossl_statem_server_construct_message;
718 transition = ossl_statem_client_write_transition;
719 pre_work = ossl_statem_client_pre_work;
720 post_work = ossl_statem_client_post_work;
721 get_construct_message_f = ossl_statem_client_construct_message;
725 switch (st->write_state) {
726 case WRITE_STATE_TRANSITION:
728 /* Notify callback of an impending state change */
730 cb(s, SSL_CB_ACCEPT_LOOP, 1);
732 cb(s, SSL_CB_CONNECT_LOOP, 1);
734 switch (transition(s)) {
735 case WRITE_TRAN_CONTINUE:
736 st->write_state = WRITE_STATE_PRE_WORK;
737 st->write_state_work = WORK_MORE_A;
740 case WRITE_TRAN_FINISHED:
741 return SUB_STATE_FINISHED;
744 case WRITE_TRAN_ERROR:
745 return SUB_STATE_ERROR;
749 case WRITE_STATE_PRE_WORK:
750 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
755 return SUB_STATE_ERROR;
757 case WORK_FINISHED_CONTINUE:
758 st->write_state = WRITE_STATE_SEND;
761 case WORK_FINISHED_STOP:
762 return SUB_STATE_END_HANDSHAKE;
764 if (!get_construct_message_f(s, &pkt, &confunc, &mt)) {
765 ossl_statem_set_error(s);
766 return SUB_STATE_ERROR;
768 if (mt == SSL3_MT_DUMMY) {
769 /* Skip construction and sending. This isn't a "real" state */
770 st->write_state = WRITE_STATE_POST_WORK;
771 st->write_state_work = WORK_MORE_A;
774 if (!WPACKET_init(&pkt, s->init_buf)
775 || !ssl_set_handshake_header(s, &pkt, mt)
776 || (confunc != NULL && !confunc(s, &pkt))
777 || !ssl_close_construct_packet(s, &pkt, mt)
778 || !WPACKET_finish(&pkt)) {
779 WPACKET_cleanup(&pkt);
780 ossl_statem_set_error(s);
781 return SUB_STATE_ERROR;
786 case WRITE_STATE_SEND:
787 if (SSL_IS_DTLS(s) && st->use_timer) {
788 dtls1_start_timer(s);
790 ret = statem_do_write(s);
792 return SUB_STATE_ERROR;
794 st->write_state = WRITE_STATE_POST_WORK;
795 st->write_state_work = WORK_MORE_A;
798 case WRITE_STATE_POST_WORK:
799 switch (st->write_state_work = post_work(s, st->write_state_work)) {
804 return SUB_STATE_ERROR;
806 case WORK_FINISHED_CONTINUE:
807 st->write_state = WRITE_STATE_TRANSITION;
810 case WORK_FINISHED_STOP:
811 return SUB_STATE_END_HANDSHAKE;
816 return SUB_STATE_ERROR;
822 * Flush the write BIO
824 int statem_flush(SSL *s)
826 s->rwstate = SSL_WRITING;
827 if (BIO_flush(s->wbio) <= 0) {
830 s->rwstate = SSL_NOTHING;
836 * Called by the record layer to determine whether application data is
837 * allowed to be received in the current handshake state or not.
840 * 1: Yes (application data allowed)
841 * 0: No (application data not allowed)
843 int ossl_statem_app_data_allowed(SSL *s)
845 OSSL_STATEM *st = &s->statem;
847 if (st->state == MSG_FLOW_UNINITED)
850 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
855 * If we're a server and we haven't got as far as writing our
856 * ServerHello yet then we allow app data
858 if (st->hand_state == TLS_ST_BEFORE
859 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
863 * If we're a client and we haven't read the ServerHello yet then we
866 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
873 #ifndef OPENSSL_NO_SCTP
875 * Set flag used by SCTP to determine whether we are in the read sock state
877 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
879 s->statem.in_sctp_read_sock = read_sock;
883 * Called by the record layer to determine whether we are in the read sock
887 * 1: Yes (we are in the read sock state)
888 * 0: No (we are not in the read sock state)
890 int ossl_statem_in_sctp_read_sock(SSL *s)
892 return s->statem.in_sctp_read_sock;