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)
171 void ossl_statem_check_finish_init(SSL *s, int send)
174 if ((send && s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
175 && s->early_data_state != SSL_EARLY_DATA_WRITING)
176 || (!send && s->statem.hand_state == TLS_ST_EARLY_DATA))
177 ossl_statem_set_in_init(s, 1);
179 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
180 && s->statem.hand_state == TLS_ST_EARLY_DATA)
181 ossl_statem_set_in_init(s, 1);
185 void ossl_statem_set_hello_verify_done(SSL *s)
187 s->statem.state = MSG_FLOW_UNINITED;
188 s->statem.in_init = 1;
190 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
191 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
192 * calls to SSL_in_before() will return false. Also calls to
193 * SSL_state_string() and SSL_state_string_long() will return something
196 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
199 int ossl_statem_connect(SSL *s)
201 return state_machine(s, 0);
204 int ossl_statem_accept(SSL *s)
206 return state_machine(s, 1);
209 typedef void (*info_cb) (const SSL *, int, int);
211 static info_cb get_callback(SSL *s)
213 if (s->info_callback != NULL)
214 return s->info_callback;
215 else if (s->ctx->info_callback != NULL)
216 return s->ctx->info_callback;
222 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
223 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
224 * transitions are as follows:
226 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
228 * +-----------------------+
230 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
238 * We may exit at any point due to an error or NBIO event. If an NBIO event
239 * occurs then we restart at the point we left off when we are recalled.
240 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
242 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
243 * into that state at any point in the event that an irrecoverable error occurs.
245 * Valid return values are:
249 static int state_machine(SSL *s, int server)
252 unsigned long Time = (unsigned long)time(NULL);
253 void (*cb) (const SSL *ssl, int type, int val) = NULL;
254 OSSL_STATEM *st = &s->statem;
258 if (st->state == MSG_FLOW_ERROR) {
259 /* Shouldn't have been called if we're already in the error state */
263 RAND_add(&Time, sizeof(Time), 0);
267 cb = get_callback(s);
270 if (!SSL_in_init(s) || SSL_in_before(s)) {
274 #ifndef OPENSSL_NO_SCTP
275 if (SSL_IS_DTLS(s)) {
277 * Notify SCTP BIO socket to enter handshake mode and prevent stream
278 * identifier other than 0. Will be ignored if no SCTP is used.
280 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
281 st->in_handshake, NULL);
285 /* Initialise state machine */
286 if (st->state == MSG_FLOW_UNINITED
287 || st->state == MSG_FLOW_FINISHED) {
288 if (st->state == MSG_FLOW_UNINITED) {
289 st->hand_state = TLS_ST_BEFORE;
290 st->request_state = TLS_ST_BEFORE;
295 cb(s, SSL_CB_HANDSHAKE_START, 1);
297 if (SSL_IS_DTLS(s)) {
298 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
299 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
300 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
304 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
305 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
310 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
311 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
315 if (s->init_buf == NULL) {
316 if ((buf = BUF_MEM_new()) == NULL) {
319 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
326 if (!ssl3_setup_buffers(s)) {
332 * Should have been reset by tls_process_finished, too.
334 s->s3->change_cipher_spec = 0;
337 * Ok, we now need to push on a buffering BIO ...but not with
340 #ifndef OPENSSL_NO_SCTP
341 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
343 if (!ssl_init_wbio_buffer(s)) {
347 if ((SSL_in_before(s))
349 if (!tls_setup_handshake(s)) {
350 ossl_statem_set_error(s);
354 if (SSL_IS_FIRST_HANDSHAKE(s))
355 st->read_state_first_init = 1;
358 st->state = MSG_FLOW_WRITING;
359 init_write_state_machine(s);
362 while (st->state != MSG_FLOW_FINISHED) {
363 if (st->state == MSG_FLOW_READING) {
364 ssret = read_state_machine(s);
365 if (ssret == SUB_STATE_FINISHED) {
366 st->state = MSG_FLOW_WRITING;
367 init_write_state_machine(s);
372 } else if (st->state == MSG_FLOW_WRITING) {
373 ssret = write_state_machine(s);
374 if (ssret == SUB_STATE_FINISHED) {
375 st->state = MSG_FLOW_READING;
376 init_read_state_machine(s);
377 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
378 st->state = MSG_FLOW_FINISHED;
385 ossl_statem_set_error(s);
395 #ifndef OPENSSL_NO_SCTP
396 if (SSL_IS_DTLS(s)) {
398 * Notify SCTP BIO socket to leave handshake mode and allow stream
399 * identifier other than 0. Will be ignored if no SCTP is used.
401 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
402 st->in_handshake, NULL);
409 cb(s, SSL_CB_ACCEPT_EXIT, ret);
411 cb(s, SSL_CB_CONNECT_EXIT, ret);
417 * Initialise the MSG_FLOW_READING sub-state machine
419 static void init_read_state_machine(SSL *s)
421 OSSL_STATEM *st = &s->statem;
423 st->read_state = READ_STATE_HEADER;
426 static int grow_init_buf(SSL *s, size_t size) {
428 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
430 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
433 if (size < msg_offset)
436 s->init_msg = s->init_buf->data + msg_offset;
442 * This function implements the sub-state machine when the message flow is in
443 * MSG_FLOW_READING. The valid sub-states and transitions are:
445 * READ_STATE_HEADER <--+<-------------+
448 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
450 * +----------------------------+
452 * [SUB_STATE_FINISHED]
454 * READ_STATE_HEADER has the responsibility for reading in the message header
455 * and transitioning the state of the handshake state machine.
457 * READ_STATE_BODY reads in the rest of the message and then subsequently
460 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
461 * processing activity performed on the message may block.
463 * Any of the above states could result in an NBIO event occurring in which case
464 * control returns to the calling application. When this function is recalled we
465 * will resume in the same state where we left off.
467 static SUB_STATE_RETURN read_state_machine(SSL *s)
469 OSSL_STATEM *st = &s->statem;
472 int (*transition) (SSL *s, int mt);
474 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
475 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
476 size_t (*max_message_size) (SSL *s);
477 void (*cb) (const SSL *ssl, int type, int val) = NULL;
479 cb = get_callback(s);
482 transition = ossl_statem_server_read_transition;
483 process_message = ossl_statem_server_process_message;
484 max_message_size = ossl_statem_server_max_message_size;
485 post_process_message = ossl_statem_server_post_process_message;
487 transition = ossl_statem_client_read_transition;
488 process_message = ossl_statem_client_process_message;
489 max_message_size = ossl_statem_client_max_message_size;
490 post_process_message = ossl_statem_client_post_process_message;
493 if (st->read_state_first_init) {
495 st->read_state_first_init = 0;
499 switch (st->read_state) {
500 case READ_STATE_HEADER:
501 /* Get the state the peer wants to move to */
502 if (SSL_IS_DTLS(s)) {
504 * In DTLS we get the whole message in one go - header and body
506 ret = dtls_get_message(s, &mt, &len);
508 ret = tls_get_message_header(s, &mt);
512 /* Could be non-blocking IO */
513 return SUB_STATE_ERROR;
517 /* Notify callback of an impending state change */
519 cb(s, SSL_CB_ACCEPT_LOOP, 1);
521 cb(s, SSL_CB_CONNECT_LOOP, 1);
524 * Validate that we are allowed to move to the new state and move
525 * to that state if so
527 if (!transition(s, mt)) {
528 ossl_statem_set_error(s);
529 return SUB_STATE_ERROR;
532 if (s->s3->tmp.message_size > max_message_size(s)) {
533 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
534 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
535 return SUB_STATE_ERROR;
538 /* dtls_get_message already did this */
540 && s->s3->tmp.message_size > 0
541 && !grow_init_buf(s, s->s3->tmp.message_size
542 + SSL3_HM_HEADER_LENGTH)) {
543 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
544 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
545 return SUB_STATE_ERROR;
548 st->read_state = READ_STATE_BODY;
551 case READ_STATE_BODY:
552 if (!SSL_IS_DTLS(s)) {
553 /* We already got this above for DTLS */
554 ret = tls_get_message_body(s, &len);
556 /* Could be non-blocking IO */
557 return SUB_STATE_ERROR;
562 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
563 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
564 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
565 return SUB_STATE_ERROR;
567 ret = process_message(s, &pkt);
569 /* Discard the packet data */
573 case MSG_PROCESS_ERROR:
574 return SUB_STATE_ERROR;
576 case MSG_PROCESS_FINISHED_READING:
577 if (SSL_IS_DTLS(s)) {
580 return SUB_STATE_FINISHED;
582 case MSG_PROCESS_CONTINUE_PROCESSING:
583 st->read_state = READ_STATE_POST_PROCESS;
584 st->read_state_work = WORK_MORE_A;
588 st->read_state = READ_STATE_HEADER;
593 case READ_STATE_POST_PROCESS:
594 st->read_state_work = post_process_message(s, st->read_state_work);
595 switch (st->read_state_work) {
600 return SUB_STATE_ERROR;
602 case WORK_FINISHED_CONTINUE:
603 st->read_state = READ_STATE_HEADER;
606 case WORK_FINISHED_STOP:
607 if (SSL_IS_DTLS(s)) {
610 return SUB_STATE_FINISHED;
615 /* Shouldn't happen */
616 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
617 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
618 ossl_statem_set_error(s);
619 return SUB_STATE_ERROR;
625 * Send a previously constructed message to the peer.
627 static int statem_do_write(SSL *s)
629 OSSL_STATEM *st = &s->statem;
631 if (st->hand_state == TLS_ST_CW_CHANGE
632 || st->hand_state == TLS_ST_SW_CHANGE) {
634 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
636 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
638 return ssl_do_write(s);
643 * Initialise the MSG_FLOW_WRITING sub-state machine
645 static void init_write_state_machine(SSL *s)
647 OSSL_STATEM *st = &s->statem;
649 st->write_state = WRITE_STATE_TRANSITION;
653 * This function implements the sub-state machine when the message flow is in
654 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
656 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
659 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
665 * | WRITE_STATE_POST_WORK
669 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
671 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
672 * sending of the message. This could result in an NBIO event occurring in
673 * which case control returns to the calling application. When this function
674 * is recalled we will resume in the same state where we left off.
676 * WRITE_STATE_SEND sends the message and performs any work to be done after
679 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
680 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
681 * result in an NBIO event.
683 static SUB_STATE_RETURN write_state_machine(SSL *s)
685 OSSL_STATEM *st = &s->statem;
687 WRITE_TRAN(*transition) (SSL *s);
688 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
689 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
690 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
691 int (**confunc) (SSL *s, WPACKET *pkt),
693 void (*cb) (const SSL *ssl, int type, int val) = NULL;
694 int (*confunc) (SSL *s, WPACKET *pkt);
698 cb = get_callback(s);
701 transition = ossl_statem_server_write_transition;
702 pre_work = ossl_statem_server_pre_work;
703 post_work = ossl_statem_server_post_work;
704 get_construct_message_f = ossl_statem_server_construct_message;
706 transition = ossl_statem_client_write_transition;
707 pre_work = ossl_statem_client_pre_work;
708 post_work = ossl_statem_client_post_work;
709 get_construct_message_f = ossl_statem_client_construct_message;
713 switch (st->write_state) {
714 case WRITE_STATE_TRANSITION:
716 /* Notify callback of an impending state change */
718 cb(s, SSL_CB_ACCEPT_LOOP, 1);
720 cb(s, SSL_CB_CONNECT_LOOP, 1);
722 switch (transition(s)) {
723 case WRITE_TRAN_CONTINUE:
724 st->write_state = WRITE_STATE_PRE_WORK;
725 st->write_state_work = WORK_MORE_A;
728 case WRITE_TRAN_FINISHED:
729 return SUB_STATE_FINISHED;
732 case WRITE_TRAN_ERROR:
733 return SUB_STATE_ERROR;
737 case WRITE_STATE_PRE_WORK:
738 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
743 return SUB_STATE_ERROR;
745 case WORK_FINISHED_CONTINUE:
746 st->write_state = WRITE_STATE_SEND;
749 case WORK_FINISHED_STOP:
750 return SUB_STATE_END_HANDSHAKE;
752 if (!get_construct_message_f(s, &pkt, &confunc, &mt)) {
753 ossl_statem_set_error(s);
754 return SUB_STATE_ERROR;
756 if (mt == SSL3_MT_DUMMY) {
757 /* Skip construction and sending. This isn't a "real" state */
758 st->write_state = WRITE_STATE_POST_WORK;
759 st->write_state_work = WORK_MORE_A;
762 if (!WPACKET_init(&pkt, s->init_buf)
763 || !ssl_set_handshake_header(s, &pkt, mt)
764 || (confunc != NULL && !confunc(s, &pkt))
765 || !ssl_close_construct_packet(s, &pkt, mt)
766 || !WPACKET_finish(&pkt)) {
767 WPACKET_cleanup(&pkt);
768 ossl_statem_set_error(s);
769 return SUB_STATE_ERROR;
774 case WRITE_STATE_SEND:
775 if (SSL_IS_DTLS(s) && st->use_timer) {
776 dtls1_start_timer(s);
778 ret = statem_do_write(s);
780 return SUB_STATE_ERROR;
782 st->write_state = WRITE_STATE_POST_WORK;
783 st->write_state_work = WORK_MORE_A;
786 case WRITE_STATE_POST_WORK:
787 switch (st->write_state_work = post_work(s, st->write_state_work)) {
792 return SUB_STATE_ERROR;
794 case WORK_FINISHED_CONTINUE:
795 st->write_state = WRITE_STATE_TRANSITION;
798 case WORK_FINISHED_STOP:
799 return SUB_STATE_END_HANDSHAKE;
804 return SUB_STATE_ERROR;
810 * Flush the write BIO
812 int statem_flush(SSL *s)
814 s->rwstate = SSL_WRITING;
815 if (BIO_flush(s->wbio) <= 0) {
818 s->rwstate = SSL_NOTHING;
824 * Called by the record layer to determine whether application data is
825 * allowed to be received in the current handshake state or not.
828 * 1: Yes (application data allowed)
829 * 0: No (application data not allowed)
831 int ossl_statem_app_data_allowed(SSL *s)
833 OSSL_STATEM *st = &s->statem;
835 if (st->state == MSG_FLOW_UNINITED)
838 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
843 * If we're a server and we haven't got as far as writing our
844 * ServerHello yet then we allow app data
846 if (st->hand_state == TLS_ST_BEFORE
847 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
851 * If we're a client and we haven't read the ServerHello yet then we
854 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
861 #ifndef OPENSSL_NO_SCTP
863 * Set flag used by SCTP to determine whether we are in the read sock state
865 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
867 s->statem.in_sctp_read_sock = read_sock;
871 * Called by the record layer to determine whether we are in the read sock
875 * 1: Yes (we are in the read sock state)
876 * 0: No (we are not in the read sock state)
878 int ossl_statem_in_sctp_read_sock(SSL *s)
880 return s->statem.in_sctp_read_sock;