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->server || s->statem.hand_state != TLS_ST_EARLY_DATA)
167 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
168 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
169 * data state and whether we should attempt to move the handshake on if so.
170 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
171 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
174 void ossl_statem_check_finish_init(SSL *s, int sending)
177 if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
178 || s->statem.hand_state == TLS_ST_EARLY_DATA) {
179 ossl_statem_set_in_init(s, 1);
180 if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
182 * SSL_connect() or SSL_do_handshake() has been called directly.
183 * We don't allow any more writing of early data.
185 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
188 } else if (!s->server) {
189 if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
190 || s->statem.hand_state == TLS_ST_EARLY_DATA)
191 && s->early_data_state != SSL_EARLY_DATA_WRITING)
192 || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) {
193 ossl_statem_set_in_init(s, 1);
195 * SSL_write() has been called directly. We don't allow any more
196 * writing of early data.
198 if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY)
199 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
202 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
203 && s->statem.hand_state == TLS_ST_EARLY_DATA)
204 ossl_statem_set_in_init(s, 1);
208 void ossl_statem_set_hello_verify_done(SSL *s)
210 s->statem.state = MSG_FLOW_UNINITED;
211 s->statem.in_init = 1;
213 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
214 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
215 * calls to SSL_in_before() will return false. Also calls to
216 * SSL_state_string() and SSL_state_string_long() will return something
219 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
222 int ossl_statem_connect(SSL *s)
224 return state_machine(s, 0);
227 int ossl_statem_accept(SSL *s)
229 return state_machine(s, 1);
232 typedef void (*info_cb) (const SSL *, int, int);
234 static info_cb get_callback(SSL *s)
236 if (s->info_callback != NULL)
237 return s->info_callback;
238 else if (s->ctx->info_callback != NULL)
239 return s->ctx->info_callback;
245 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
246 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
247 * transitions are as follows:
249 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
251 * +-----------------------+
253 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
261 * We may exit at any point due to an error or NBIO event. If an NBIO event
262 * occurs then we restart at the point we left off when we are recalled.
263 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
265 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
266 * into that state at any point in the event that an irrecoverable error occurs.
268 * Valid return values are:
272 static int state_machine(SSL *s, int server)
275 void (*cb) (const SSL *ssl, int type, int val) = NULL;
276 OSSL_STATEM *st = &s->statem;
280 if (st->state == MSG_FLOW_ERROR) {
281 /* Shouldn't have been called if we're already in the error state */
288 cb = get_callback(s);
291 if (!SSL_in_init(s) || SSL_in_before(s)) {
295 #ifndef OPENSSL_NO_SCTP
296 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
298 * Notify SCTP BIO socket to enter handshake mode and prevent stream
299 * identifier other than 0.
301 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
302 st->in_handshake, NULL);
306 /* Initialise state machine */
307 if (st->state == MSG_FLOW_UNINITED
308 || st->state == MSG_FLOW_FINISHED) {
309 if (st->state == MSG_FLOW_UNINITED) {
310 st->hand_state = TLS_ST_BEFORE;
311 st->request_state = TLS_ST_BEFORE;
316 cb(s, SSL_CB_HANDSHAKE_START, 1);
318 if (SSL_IS_DTLS(s)) {
319 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
320 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
321 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
325 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
326 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
331 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
332 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
336 if (s->init_buf == NULL) {
337 if ((buf = BUF_MEM_new()) == NULL) {
340 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
347 if (!ssl3_setup_buffers(s)) {
353 * Should have been reset by tls_process_finished, too.
355 s->s3->change_cipher_spec = 0;
358 * Ok, we now need to push on a buffering BIO ...but not with
361 #ifndef OPENSSL_NO_SCTP
362 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
364 if (!ssl_init_wbio_buffer(s)) {
368 if ((SSL_in_before(s))
370 if (!tls_setup_handshake(s)) {
371 ossl_statem_set_error(s);
375 if (SSL_IS_FIRST_HANDSHAKE(s))
376 st->read_state_first_init = 1;
379 st->state = MSG_FLOW_WRITING;
380 init_write_state_machine(s);
383 while (st->state != MSG_FLOW_FINISHED) {
384 if (st->state == MSG_FLOW_READING) {
385 ssret = read_state_machine(s);
386 if (ssret == SUB_STATE_FINISHED) {
387 st->state = MSG_FLOW_WRITING;
388 init_write_state_machine(s);
393 } else if (st->state == MSG_FLOW_WRITING) {
394 ssret = write_state_machine(s);
395 if (ssret == SUB_STATE_FINISHED) {
396 st->state = MSG_FLOW_READING;
397 init_read_state_machine(s);
398 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
399 st->state = MSG_FLOW_FINISHED;
406 ossl_statem_set_error(s);
416 #ifndef OPENSSL_NO_SCTP
417 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
419 * Notify SCTP BIO socket to leave handshake mode and allow stream
420 * identifier other than 0.
422 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
423 st->in_handshake, NULL);
430 cb(s, SSL_CB_ACCEPT_EXIT, ret);
432 cb(s, SSL_CB_CONNECT_EXIT, ret);
438 * Initialise the MSG_FLOW_READING sub-state machine
440 static void init_read_state_machine(SSL *s)
442 OSSL_STATEM *st = &s->statem;
444 st->read_state = READ_STATE_HEADER;
447 static int grow_init_buf(SSL *s, size_t size) {
449 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
451 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
454 if (size < msg_offset)
457 s->init_msg = s->init_buf->data + msg_offset;
463 * This function implements the sub-state machine when the message flow is in
464 * MSG_FLOW_READING. The valid sub-states and transitions are:
466 * READ_STATE_HEADER <--+<-------------+
469 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
471 * +----------------------------+
473 * [SUB_STATE_FINISHED]
475 * READ_STATE_HEADER has the responsibility for reading in the message header
476 * and transitioning the state of the handshake state machine.
478 * READ_STATE_BODY reads in the rest of the message and then subsequently
481 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
482 * processing activity performed on the message may block.
484 * Any of the above states could result in an NBIO event occurring in which case
485 * control returns to the calling application. When this function is recalled we
486 * will resume in the same state where we left off.
488 static SUB_STATE_RETURN read_state_machine(SSL *s)
490 OSSL_STATEM *st = &s->statem;
493 int (*transition) (SSL *s, int mt);
495 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
496 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
497 size_t (*max_message_size) (SSL *s);
498 void (*cb) (const SSL *ssl, int type, int val) = NULL;
500 cb = get_callback(s);
503 transition = ossl_statem_server_read_transition;
504 process_message = ossl_statem_server_process_message;
505 max_message_size = ossl_statem_server_max_message_size;
506 post_process_message = ossl_statem_server_post_process_message;
508 transition = ossl_statem_client_read_transition;
509 process_message = ossl_statem_client_process_message;
510 max_message_size = ossl_statem_client_max_message_size;
511 post_process_message = ossl_statem_client_post_process_message;
514 if (st->read_state_first_init) {
516 st->read_state_first_init = 0;
520 switch (st->read_state) {
521 case READ_STATE_HEADER:
522 /* Get the state the peer wants to move to */
523 if (SSL_IS_DTLS(s)) {
525 * In DTLS we get the whole message in one go - header and body
527 ret = dtls_get_message(s, &mt, &len);
529 ret = tls_get_message_header(s, &mt);
533 /* Could be non-blocking IO */
534 return SUB_STATE_ERROR;
538 /* Notify callback of an impending state change */
540 cb(s, SSL_CB_ACCEPT_LOOP, 1);
542 cb(s, SSL_CB_CONNECT_LOOP, 1);
545 * Validate that we are allowed to move to the new state and move
546 * to that state if so
548 if (!transition(s, mt)) {
549 ossl_statem_set_error(s);
550 return SUB_STATE_ERROR;
553 if (s->s3->tmp.message_size > max_message_size(s)) {
554 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
555 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
556 return SUB_STATE_ERROR;
559 /* dtls_get_message already did this */
561 && s->s3->tmp.message_size > 0
562 && !grow_init_buf(s, s->s3->tmp.message_size
563 + SSL3_HM_HEADER_LENGTH)) {
564 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
565 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
566 return SUB_STATE_ERROR;
569 st->read_state = READ_STATE_BODY;
572 case READ_STATE_BODY:
573 if (!SSL_IS_DTLS(s)) {
574 /* We already got this above for DTLS */
575 ret = tls_get_message_body(s, &len);
577 /* Could be non-blocking IO */
578 return SUB_STATE_ERROR;
583 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
584 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
585 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
586 return SUB_STATE_ERROR;
588 ret = process_message(s, &pkt);
590 /* Discard the packet data */
594 case MSG_PROCESS_ERROR:
595 return SUB_STATE_ERROR;
597 case MSG_PROCESS_FINISHED_READING:
598 if (SSL_IS_DTLS(s)) {
601 return SUB_STATE_FINISHED;
603 case MSG_PROCESS_CONTINUE_PROCESSING:
604 st->read_state = READ_STATE_POST_PROCESS;
605 st->read_state_work = WORK_MORE_A;
609 st->read_state = READ_STATE_HEADER;
614 case READ_STATE_POST_PROCESS:
615 st->read_state_work = post_process_message(s, st->read_state_work);
616 switch (st->read_state_work) {
621 return SUB_STATE_ERROR;
623 case WORK_FINISHED_CONTINUE:
624 st->read_state = READ_STATE_HEADER;
627 case WORK_FINISHED_STOP:
628 if (SSL_IS_DTLS(s)) {
631 return SUB_STATE_FINISHED;
636 /* Shouldn't happen */
637 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
638 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
639 ossl_statem_set_error(s);
640 return SUB_STATE_ERROR;
646 * Send a previously constructed message to the peer.
648 static int statem_do_write(SSL *s)
650 OSSL_STATEM *st = &s->statem;
652 if (st->hand_state == TLS_ST_CW_CHANGE
653 || st->hand_state == TLS_ST_SW_CHANGE) {
655 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
657 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
659 return ssl_do_write(s);
664 * Initialise the MSG_FLOW_WRITING sub-state machine
666 static void init_write_state_machine(SSL *s)
668 OSSL_STATEM *st = &s->statem;
670 st->write_state = WRITE_STATE_TRANSITION;
674 * This function implements the sub-state machine when the message flow is in
675 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
677 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
680 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
686 * | WRITE_STATE_POST_WORK
690 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
692 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
693 * sending of the message. This could result in an NBIO event occurring in
694 * which case control returns to the calling application. When this function
695 * is recalled we will resume in the same state where we left off.
697 * WRITE_STATE_SEND sends the message and performs any work to be done after
700 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
701 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
702 * result in an NBIO event.
704 static SUB_STATE_RETURN write_state_machine(SSL *s)
706 OSSL_STATEM *st = &s->statem;
708 WRITE_TRAN(*transition) (SSL *s);
709 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
710 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
711 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
712 int (**confunc) (SSL *s, WPACKET *pkt),
714 void (*cb) (const SSL *ssl, int type, int val) = NULL;
715 int (*confunc) (SSL *s, WPACKET *pkt);
719 cb = get_callback(s);
722 transition = ossl_statem_server_write_transition;
723 pre_work = ossl_statem_server_pre_work;
724 post_work = ossl_statem_server_post_work;
725 get_construct_message_f = ossl_statem_server_construct_message;
727 transition = ossl_statem_client_write_transition;
728 pre_work = ossl_statem_client_pre_work;
729 post_work = ossl_statem_client_post_work;
730 get_construct_message_f = ossl_statem_client_construct_message;
734 switch (st->write_state) {
735 case WRITE_STATE_TRANSITION:
737 /* Notify callback of an impending state change */
739 cb(s, SSL_CB_ACCEPT_LOOP, 1);
741 cb(s, SSL_CB_CONNECT_LOOP, 1);
743 switch (transition(s)) {
744 case WRITE_TRAN_CONTINUE:
745 st->write_state = WRITE_STATE_PRE_WORK;
746 st->write_state_work = WORK_MORE_A;
749 case WRITE_TRAN_FINISHED:
750 return SUB_STATE_FINISHED;
753 case WRITE_TRAN_ERROR:
754 return SUB_STATE_ERROR;
758 case WRITE_STATE_PRE_WORK:
759 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
764 return SUB_STATE_ERROR;
766 case WORK_FINISHED_CONTINUE:
767 st->write_state = WRITE_STATE_SEND;
770 case WORK_FINISHED_STOP:
771 return SUB_STATE_END_HANDSHAKE;
773 if (!get_construct_message_f(s, &pkt, &confunc, &mt)) {
774 ossl_statem_set_error(s);
775 return SUB_STATE_ERROR;
777 if (mt == SSL3_MT_DUMMY) {
778 /* Skip construction and sending. This isn't a "real" state */
779 st->write_state = WRITE_STATE_POST_WORK;
780 st->write_state_work = WORK_MORE_A;
783 if (!WPACKET_init(&pkt, s->init_buf)
784 || !ssl_set_handshake_header(s, &pkt, mt)
785 || (confunc != NULL && !confunc(s, &pkt))
786 || !ssl_close_construct_packet(s, &pkt, mt)
787 || !WPACKET_finish(&pkt)) {
788 WPACKET_cleanup(&pkt);
789 ossl_statem_set_error(s);
790 return SUB_STATE_ERROR;
795 case WRITE_STATE_SEND:
796 if (SSL_IS_DTLS(s) && st->use_timer) {
797 dtls1_start_timer(s);
799 ret = statem_do_write(s);
801 return SUB_STATE_ERROR;
803 st->write_state = WRITE_STATE_POST_WORK;
804 st->write_state_work = WORK_MORE_A;
807 case WRITE_STATE_POST_WORK:
808 switch (st->write_state_work = post_work(s, st->write_state_work)) {
813 return SUB_STATE_ERROR;
815 case WORK_FINISHED_CONTINUE:
816 st->write_state = WRITE_STATE_TRANSITION;
819 case WORK_FINISHED_STOP:
820 return SUB_STATE_END_HANDSHAKE;
825 return SUB_STATE_ERROR;
831 * Flush the write BIO
833 int statem_flush(SSL *s)
835 s->rwstate = SSL_WRITING;
836 if (BIO_flush(s->wbio) <= 0) {
839 s->rwstate = SSL_NOTHING;
845 * Called by the record layer to determine whether application data is
846 * allowed to be received in the current handshake state or not.
849 * 1: Yes (application data allowed)
850 * 0: No (application data not allowed)
852 int ossl_statem_app_data_allowed(SSL *s)
854 OSSL_STATEM *st = &s->statem;
856 if (st->state == MSG_FLOW_UNINITED)
859 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
864 * If we're a server and we haven't got as far as writing our
865 * ServerHello yet then we allow app data
867 if (st->hand_state == TLS_ST_BEFORE
868 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
872 * If we're a client and we haven't read the ServerHello yet then we
875 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)