2 * Copyright 2015-2017 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
11 #include <openssl/rand.h>
12 #include "../ssl_locl.h"
13 #include "statem_locl.h"
16 * This file implements the SSL/TLS/DTLS state machines.
18 * There are two primary state machines:
20 * 1) Message flow state machine
21 * 2) Handshake state machine
23 * The Message flow state machine controls the reading and sending of messages
24 * including handling of non-blocking IO events, flushing of the underlying
25 * write BIO, handling unexpected messages, etc. It is itself broken into two
26 * separate sub-state machines which control reading and writing respectively.
28 * The Handshake state machine keeps track of the current SSL/TLS handshake
29 * state. Transitions of the handshake state are the result of events that
30 * occur within the Message flow state machine.
32 * Overall it looks like this:
34 * --------------------------------------------- -------------------
36 * | Message flow state machine | | |
38 * | -------------------- -------------------- | Transition | Handshake state |
39 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
40 * | | sub-state | | sub-state | |----------->| |
41 * | | machine for | | machine for | | | |
42 * | | reading messages | | writing messages | | | |
43 * | -------------------- -------------------- | | |
45 * --------------------------------------------- -------------------
49 /* Sub state machine return values */
51 /* Something bad happened or NBIO */
53 /* Sub state finished go to the next sub state */
55 /* Sub state finished and handshake was completed */
56 SUB_STATE_END_HANDSHAKE
59 static int state_machine(SSL *s, int server);
60 static void init_read_state_machine(SSL *s);
61 static SUB_STATE_RETURN read_state_machine(SSL *s);
62 static void init_write_state_machine(SSL *s);
63 static SUB_STATE_RETURN write_state_machine(SSL *s);
65 OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
67 return ssl->statem.hand_state;
70 int SSL_in_init(SSL *s)
72 return s->statem.in_init;
75 int SSL_is_init_finished(SSL *s)
77 return !(s->statem.in_init) && (s->statem.hand_state == TLS_ST_OK);
80 int SSL_in_before(SSL *s)
83 * Historically being "in before" meant before anything had happened. In the
84 * current code though we remain in the "before" state for a while after we
85 * have started the handshake process (e.g. as a server waiting for the
86 * first message to arrive). There "in before" is taken to mean "in before"
87 * and not started any handshake process yet.
89 return (s->statem.hand_state == TLS_ST_BEFORE)
90 && (s->statem.state == MSG_FLOW_UNINITED);
94 * Clear the state machine state and reset back to MSG_FLOW_UNINITED
96 void ossl_statem_clear(SSL *s)
98 s->statem.state = MSG_FLOW_UNINITED;
99 s->statem.hand_state = TLS_ST_BEFORE;
100 s->statem.in_init = 1;
101 s->statem.no_cert_verify = 0;
105 * Set the state machine up ready for a renegotiation handshake
107 void ossl_statem_set_renegotiate(SSL *s)
109 s->statem.in_init = 1;
110 s->statem.request_state = TLS_ST_SW_HELLO_REQ;
114 * Put the state machine into an error state. This is a permanent error for
115 * the current connection.
117 void ossl_statem_set_error(SSL *s)
119 s->statem.state = MSG_FLOW_ERROR;
123 * Discover whether the current connection is in the error state.
125 * Valid return values are:
129 int ossl_statem_in_error(const SSL *s)
131 if (s->statem.state == MSG_FLOW_ERROR)
137 void ossl_statem_set_in_init(SSL *s, int init)
139 s->statem.in_init = init;
142 int ossl_statem_get_in_handshake(SSL *s)
144 return s->statem.in_handshake;
147 void ossl_statem_set_in_handshake(SSL *s, int inhand)
150 s->statem.in_handshake++;
152 s->statem.in_handshake--;
155 /* Are we in a sensible state to skip over unreadable early data? */
156 int ossl_statem_skip_early_data(SSL *s)
158 if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
161 if (!s->server || s->statem.hand_state != TLS_ST_EARLY_DATA)
168 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
169 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
170 * data state and whether we should attempt to move the handshake on if so.
171 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
172 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
175 void ossl_statem_check_finish_init(SSL *s, int sending)
178 if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
179 || s->statem.hand_state == TLS_ST_EARLY_DATA) {
180 ossl_statem_set_in_init(s, 1);
181 if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
183 * SSL_connect() or SSL_do_handshake() has been called directly.
184 * We don't allow any more writing of early data.
186 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
189 } else if (!s->server) {
190 if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
191 || s->statem.hand_state == TLS_ST_EARLY_DATA)
192 && s->early_data_state != SSL_EARLY_DATA_WRITING)
193 || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) {
194 ossl_statem_set_in_init(s, 1);
196 * SSL_write() has been called directly. We don't allow any more
197 * writing of early data.
199 if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY)
200 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
203 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
204 && s->statem.hand_state == TLS_ST_EARLY_DATA)
205 ossl_statem_set_in_init(s, 1);
209 void ossl_statem_set_hello_verify_done(SSL *s)
211 s->statem.state = MSG_FLOW_UNINITED;
212 s->statem.in_init = 1;
214 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
215 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
216 * calls to SSL_in_before() will return false. Also calls to
217 * SSL_state_string() and SSL_state_string_long() will return something
220 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
223 int ossl_statem_connect(SSL *s)
225 return state_machine(s, 0);
228 int ossl_statem_accept(SSL *s)
230 return state_machine(s, 1);
233 typedef void (*info_cb) (const SSL *, int, int);
235 static info_cb get_callback(SSL *s)
237 if (s->info_callback != NULL)
238 return s->info_callback;
239 else if (s->ctx->info_callback != NULL)
240 return s->ctx->info_callback;
246 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
247 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
248 * transitions are as follows:
250 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
252 * +-----------------------+
254 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
262 * We may exit at any point due to an error or NBIO event. If an NBIO event
263 * occurs then we restart at the point we left off when we are recalled.
264 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
266 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
267 * into that state at any point in the event that an irrecoverable error occurs.
269 * Valid return values are:
273 static int state_machine(SSL *s, int server)
276 void (*cb) (const SSL *ssl, int type, int val) = NULL;
277 OSSL_STATEM *st = &s->statem;
281 if (st->state == MSG_FLOW_ERROR) {
282 /* Shouldn't have been called if we're already in the error state */
289 cb = get_callback(s);
292 if (!SSL_in_init(s) || SSL_in_before(s)) {
296 #ifndef OPENSSL_NO_SCTP
297 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
299 * Notify SCTP BIO socket to enter handshake mode and prevent stream
300 * identifier other than 0.
302 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
303 st->in_handshake, NULL);
307 /* Initialise state machine */
308 if (st->state == MSG_FLOW_UNINITED
309 || st->state == MSG_FLOW_FINISHED) {
310 if (st->state == MSG_FLOW_UNINITED) {
311 st->hand_state = TLS_ST_BEFORE;
312 st->request_state = TLS_ST_BEFORE;
317 cb(s, SSL_CB_HANDSHAKE_START, 1);
319 if (SSL_IS_DTLS(s)) {
320 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
321 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
322 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
326 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
327 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
332 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
333 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
337 if (s->init_buf == NULL) {
338 if ((buf = BUF_MEM_new()) == NULL) {
341 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
348 if (!ssl3_setup_buffers(s)) {
354 * Should have been reset by tls_process_finished, too.
356 s->s3->change_cipher_spec = 0;
359 * Ok, we now need to push on a buffering BIO ...but not with
362 #ifndef OPENSSL_NO_SCTP
363 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
365 if (!ssl_init_wbio_buffer(s)) {
369 if ((SSL_in_before(s))
371 if (!tls_setup_handshake(s)) {
372 ossl_statem_set_error(s);
376 if (SSL_IS_FIRST_HANDSHAKE(s))
377 st->read_state_first_init = 1;
380 st->state = MSG_FLOW_WRITING;
381 init_write_state_machine(s);
384 while (st->state != MSG_FLOW_FINISHED) {
385 if (st->state == MSG_FLOW_READING) {
386 ssret = read_state_machine(s);
387 if (ssret == SUB_STATE_FINISHED) {
388 st->state = MSG_FLOW_WRITING;
389 init_write_state_machine(s);
394 } else if (st->state == MSG_FLOW_WRITING) {
395 ssret = write_state_machine(s);
396 if (ssret == SUB_STATE_FINISHED) {
397 st->state = MSG_FLOW_READING;
398 init_read_state_machine(s);
399 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
400 st->state = MSG_FLOW_FINISHED;
407 ossl_statem_set_error(s);
417 #ifndef OPENSSL_NO_SCTP
418 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
420 * Notify SCTP BIO socket to leave handshake mode and allow stream
421 * identifier other than 0.
423 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
424 st->in_handshake, NULL);
431 cb(s, SSL_CB_ACCEPT_EXIT, ret);
433 cb(s, SSL_CB_CONNECT_EXIT, ret);
439 * Initialise the MSG_FLOW_READING sub-state machine
441 static void init_read_state_machine(SSL *s)
443 OSSL_STATEM *st = &s->statem;
445 st->read_state = READ_STATE_HEADER;
448 static int grow_init_buf(SSL *s, size_t size) {
450 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
452 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
455 if (size < msg_offset)
458 s->init_msg = s->init_buf->data + msg_offset;
464 * This function implements the sub-state machine when the message flow is in
465 * MSG_FLOW_READING. The valid sub-states and transitions are:
467 * READ_STATE_HEADER <--+<-------------+
470 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
472 * +----------------------------+
474 * [SUB_STATE_FINISHED]
476 * READ_STATE_HEADER has the responsibility for reading in the message header
477 * and transitioning the state of the handshake state machine.
479 * READ_STATE_BODY reads in the rest of the message and then subsequently
482 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
483 * processing activity performed on the message may block.
485 * Any of the above states could result in an NBIO event occurring in which case
486 * control returns to the calling application. When this function is recalled we
487 * will resume in the same state where we left off.
489 static SUB_STATE_RETURN read_state_machine(SSL *s)
491 OSSL_STATEM *st = &s->statem;
494 int (*transition) (SSL *s, int mt);
496 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
497 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
498 size_t (*max_message_size) (SSL *s);
499 void (*cb) (const SSL *ssl, int type, int val) = NULL;
501 cb = get_callback(s);
504 transition = ossl_statem_server_read_transition;
505 process_message = ossl_statem_server_process_message;
506 max_message_size = ossl_statem_server_max_message_size;
507 post_process_message = ossl_statem_server_post_process_message;
509 transition = ossl_statem_client_read_transition;
510 process_message = ossl_statem_client_process_message;
511 max_message_size = ossl_statem_client_max_message_size;
512 post_process_message = ossl_statem_client_post_process_message;
515 if (st->read_state_first_init) {
517 st->read_state_first_init = 0;
521 switch (st->read_state) {
522 case READ_STATE_HEADER:
523 /* Get the state the peer wants to move to */
524 if (SSL_IS_DTLS(s)) {
526 * In DTLS we get the whole message in one go - header and body
528 ret = dtls_get_message(s, &mt, &len);
530 ret = tls_get_message_header(s, &mt);
534 /* Could be non-blocking IO */
535 return SUB_STATE_ERROR;
539 /* Notify callback of an impending state change */
541 cb(s, SSL_CB_ACCEPT_LOOP, 1);
543 cb(s, SSL_CB_CONNECT_LOOP, 1);
546 * Validate that we are allowed to move to the new state and move
547 * to that state if so
549 if (!transition(s, mt)) {
550 ossl_statem_set_error(s);
551 return SUB_STATE_ERROR;
554 if (s->s3->tmp.message_size > max_message_size(s)) {
555 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
556 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
557 return SUB_STATE_ERROR;
560 /* dtls_get_message already did this */
562 && s->s3->tmp.message_size > 0
563 && !grow_init_buf(s, s->s3->tmp.message_size
564 + SSL3_HM_HEADER_LENGTH)) {
565 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
566 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
567 return SUB_STATE_ERROR;
570 st->read_state = READ_STATE_BODY;
573 case READ_STATE_BODY:
574 if (!SSL_IS_DTLS(s)) {
575 /* We already got this above for DTLS */
576 ret = tls_get_message_body(s, &len);
578 /* Could be non-blocking IO */
579 return SUB_STATE_ERROR;
584 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
585 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
586 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
587 return SUB_STATE_ERROR;
589 ret = process_message(s, &pkt);
591 /* Discard the packet data */
595 case MSG_PROCESS_ERROR:
596 return SUB_STATE_ERROR;
598 case MSG_PROCESS_FINISHED_READING:
599 if (SSL_IS_DTLS(s)) {
602 return SUB_STATE_FINISHED;
604 case MSG_PROCESS_CONTINUE_PROCESSING:
605 st->read_state = READ_STATE_POST_PROCESS;
606 st->read_state_work = WORK_MORE_A;
610 st->read_state = READ_STATE_HEADER;
615 case READ_STATE_POST_PROCESS:
616 st->read_state_work = post_process_message(s, st->read_state_work);
617 switch (st->read_state_work) {
622 return SUB_STATE_ERROR;
624 case WORK_FINISHED_CONTINUE:
625 st->read_state = READ_STATE_HEADER;
628 case WORK_FINISHED_STOP:
629 if (SSL_IS_DTLS(s)) {
632 return SUB_STATE_FINISHED;
637 /* Shouldn't happen */
638 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
639 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
640 ossl_statem_set_error(s);
641 return SUB_STATE_ERROR;
647 * Send a previously constructed message to the peer.
649 static int statem_do_write(SSL *s)
651 OSSL_STATEM *st = &s->statem;
653 if (st->hand_state == TLS_ST_CW_CHANGE
654 || st->hand_state == TLS_ST_SW_CHANGE) {
656 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
658 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
660 return ssl_do_write(s);
665 * Initialise the MSG_FLOW_WRITING sub-state machine
667 static void init_write_state_machine(SSL *s)
669 OSSL_STATEM *st = &s->statem;
671 st->write_state = WRITE_STATE_TRANSITION;
675 * This function implements the sub-state machine when the message flow is in
676 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
678 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
681 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
687 * | WRITE_STATE_POST_WORK
691 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
693 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
694 * sending of the message. This could result in an NBIO event occurring in
695 * which case control returns to the calling application. When this function
696 * is recalled we will resume in the same state where we left off.
698 * WRITE_STATE_SEND sends the message and performs any work to be done after
701 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
702 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
703 * result in an NBIO event.
705 static SUB_STATE_RETURN write_state_machine(SSL *s)
707 OSSL_STATEM *st = &s->statem;
709 WRITE_TRAN(*transition) (SSL *s);
710 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
711 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
712 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
713 int (**confunc) (SSL *s, WPACKET *pkt),
715 void (*cb) (const SSL *ssl, int type, int val) = NULL;
716 int (*confunc) (SSL *s, WPACKET *pkt);
720 cb = get_callback(s);
723 transition = ossl_statem_server_write_transition;
724 pre_work = ossl_statem_server_pre_work;
725 post_work = ossl_statem_server_post_work;
726 get_construct_message_f = ossl_statem_server_construct_message;
728 transition = ossl_statem_client_write_transition;
729 pre_work = ossl_statem_client_pre_work;
730 post_work = ossl_statem_client_post_work;
731 get_construct_message_f = ossl_statem_client_construct_message;
735 switch (st->write_state) {
736 case WRITE_STATE_TRANSITION:
738 /* Notify callback of an impending state change */
740 cb(s, SSL_CB_ACCEPT_LOOP, 1);
742 cb(s, SSL_CB_CONNECT_LOOP, 1);
744 switch (transition(s)) {
745 case WRITE_TRAN_CONTINUE:
746 st->write_state = WRITE_STATE_PRE_WORK;
747 st->write_state_work = WORK_MORE_A;
750 case WRITE_TRAN_FINISHED:
751 return SUB_STATE_FINISHED;
754 case WRITE_TRAN_ERROR:
755 return SUB_STATE_ERROR;
759 case WRITE_STATE_PRE_WORK:
760 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
765 return SUB_STATE_ERROR;
767 case WORK_FINISHED_CONTINUE:
768 st->write_state = WRITE_STATE_SEND;
771 case WORK_FINISHED_STOP:
772 return SUB_STATE_END_HANDSHAKE;
774 if (!get_construct_message_f(s, &pkt, &confunc, &mt)) {
775 ossl_statem_set_error(s);
776 return SUB_STATE_ERROR;
778 if (mt == SSL3_MT_DUMMY) {
779 /* Skip construction and sending. This isn't a "real" state */
780 st->write_state = WRITE_STATE_POST_WORK;
781 st->write_state_work = WORK_MORE_A;
784 if (!WPACKET_init(&pkt, s->init_buf)
785 || !ssl_set_handshake_header(s, &pkt, mt)
786 || (confunc != NULL && !confunc(s, &pkt))
787 || !ssl_close_construct_packet(s, &pkt, mt)
788 || !WPACKET_finish(&pkt)) {
789 WPACKET_cleanup(&pkt);
790 ossl_statem_set_error(s);
791 return SUB_STATE_ERROR;
796 case WRITE_STATE_SEND:
797 if (SSL_IS_DTLS(s) && st->use_timer) {
798 dtls1_start_timer(s);
800 ret = statem_do_write(s);
802 return SUB_STATE_ERROR;
804 st->write_state = WRITE_STATE_POST_WORK;
805 st->write_state_work = WORK_MORE_A;
808 case WRITE_STATE_POST_WORK:
809 switch (st->write_state_work = post_work(s, st->write_state_work)) {
814 return SUB_STATE_ERROR;
816 case WORK_FINISHED_CONTINUE:
817 st->write_state = WRITE_STATE_TRANSITION;
820 case WORK_FINISHED_STOP:
821 return SUB_STATE_END_HANDSHAKE;
826 return SUB_STATE_ERROR;
832 * Flush the write BIO
834 int statem_flush(SSL *s)
836 s->rwstate = SSL_WRITING;
837 if (BIO_flush(s->wbio) <= 0) {
840 s->rwstate = SSL_NOTHING;
846 * Called by the record layer to determine whether application data is
847 * allowed to be received in the current handshake state or not.
850 * 1: Yes (application data allowed)
851 * 0: No (application data not allowed)
853 int ossl_statem_app_data_allowed(SSL *s)
855 OSSL_STATEM *st = &s->statem;
857 if (st->state == MSG_FLOW_UNINITED)
860 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
865 * If we're a server and we haven't got as far as writing our
866 * ServerHello yet then we allow app data
868 if (st->hand_state == TLS_ST_BEFORE
869 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
873 * If we're a client and we haven't read the ServerHello yet then we
876 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)