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.state = MSG_FLOW_RENEGOTIATE;
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 void ossl_statem_set_hello_verify_done(SSL *s)
157 s->statem.state = MSG_FLOW_UNINITED;
158 s->statem.in_init = 1;
160 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
161 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
162 * calls to SSL_in_before() will return false. Also calls to
163 * SSL_state_string() and SSL_state_string_long() will return something
166 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
169 int ossl_statem_connect(SSL *s)
171 return state_machine(s, 0);
174 int ossl_statem_accept(SSL *s)
176 return state_machine(s, 1);
179 typedef void (*info_cb) (const SSL *, int, int);
181 static info_cb get_callback(SSL *s)
183 if (s->info_callback != NULL)
184 return s->info_callback;
185 else if (s->ctx->info_callback != NULL)
186 return s->ctx->info_callback;
192 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
193 * MSG_FLOW_RENEGOTIATE state and finish in MSG_FLOW_FINISHED. Valid states and
194 * transitions are as follows:
196 * MSG_FLOW_UNINITED MSG_FLOW_RENEGOTIATE
198 * +-----------------------+
200 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
208 * We may exit at any point due to an error or NBIO event. If an NBIO event
209 * occurs then we restart at the point we left off when we are recalled.
210 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
212 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
213 * into that state at any point in the event that an irrecoverable error occurs.
215 * Valid return values are:
219 static int state_machine(SSL *s, int server)
222 unsigned long Time = (unsigned long)time(NULL);
223 void (*cb) (const SSL *ssl, int type, int val) = NULL;
224 OSSL_STATEM *st = &s->statem;
228 if (st->state == MSG_FLOW_ERROR) {
229 /* Shouldn't have been called if we're already in the error state */
233 RAND_add(&Time, sizeof(Time), 0);
237 cb = get_callback(s);
240 if (!SSL_in_init(s) || SSL_in_before(s)) {
244 #ifndef OPENSSL_NO_SCTP
245 if (SSL_IS_DTLS(s)) {
247 * Notify SCTP BIO socket to enter handshake mode and prevent stream
248 * identifier other than 0. Will be ignored if no SCTP is used.
250 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
251 st->in_handshake, NULL);
255 /* Initialise state machine */
257 if (st->state == MSG_FLOW_RENEGOTIATE) {
260 s->ctx->stats.sess_connect_renegotiate++;
263 if (st->state == MSG_FLOW_UNINITED
264 || st->state == MSG_FLOW_RENEGOTIATE
265 || st->state == MSG_FLOW_FINISHED) {
266 if (st->state == MSG_FLOW_UNINITED) {
267 st->hand_state = TLS_ST_BEFORE;
268 st->request_state = TLS_ST_BEFORE;
273 cb(s, SSL_CB_HANDSHAKE_START, 1);
275 if (SSL_IS_DTLS(s)) {
276 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
277 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
278 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
282 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
283 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
288 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
289 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
293 if (s->init_buf == NULL) {
294 if ((buf = BUF_MEM_new()) == NULL) {
297 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
304 if (!ssl3_setup_buffers(s)) {
310 * Should have been reset by tls_process_finished, too.
312 s->s3->change_cipher_spec = 0;
315 * Ok, we now need to push on a buffering BIO ...but not with
318 #ifndef OPENSSL_NO_SCTP
319 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
321 if (!ssl_init_wbio_buffer(s)) {
325 if (!SSL_IS_TLS13(s)) {
326 if (!server || st->state != MSG_FLOW_RENEGOTIATE) {
327 if (!ssl3_init_finished_mac(s)) {
328 ossl_statem_set_error(s);
334 if (st->state != MSG_FLOW_RENEGOTIATE) {
335 s->ctx->stats.sess_accept++;
336 } else if (!s->s3->send_connection_binding &&
338 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
340 * Server attempting to renegotiate with client that doesn't
341 * support secure renegotiation.
343 SSLerr(SSL_F_STATE_MACHINE,
344 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
345 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
346 ossl_statem_set_error(s);
350 * st->state == MSG_FLOW_RENEGOTIATE, we will just send a
353 s->ctx->stats.sess_accept_renegotiate++;
355 s->s3->tmp.cert_request = 0;
358 s->ctx->stats.sess_connect++;
360 /* mark client_random uninitialized */
361 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
364 s->s3->tmp.cert_req = 0;
366 if (SSL_IS_DTLS(s)) {
371 st->read_state_first_init = 1;
374 st->state = MSG_FLOW_WRITING;
375 init_write_state_machine(s);
378 while (st->state != MSG_FLOW_FINISHED) {
379 if (st->state == MSG_FLOW_READING) {
380 ssret = read_state_machine(s);
381 if (ssret == SUB_STATE_FINISHED) {
382 st->state = MSG_FLOW_WRITING;
383 init_write_state_machine(s);
388 } else if (st->state == MSG_FLOW_WRITING) {
389 ssret = write_state_machine(s);
390 if (ssret == SUB_STATE_FINISHED) {
391 st->state = MSG_FLOW_READING;
392 init_read_state_machine(s);
393 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
394 st->state = MSG_FLOW_FINISHED;
401 ossl_statem_set_error(s);
411 #ifndef OPENSSL_NO_SCTP
412 if (SSL_IS_DTLS(s)) {
414 * Notify SCTP BIO socket to leave handshake mode and allow stream
415 * identifier other than 0. Will be ignored if no SCTP is used.
417 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
418 st->in_handshake, NULL);
425 cb(s, SSL_CB_ACCEPT_EXIT, ret);
427 cb(s, SSL_CB_CONNECT_EXIT, ret);
433 * Initialise the MSG_FLOW_READING sub-state machine
435 static void init_read_state_machine(SSL *s)
437 OSSL_STATEM *st = &s->statem;
439 st->read_state = READ_STATE_HEADER;
442 static int grow_init_buf(SSL *s, size_t size) {
444 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
446 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
449 if (size < msg_offset)
452 s->init_msg = s->init_buf->data + msg_offset;
458 * This function implements the sub-state machine when the message flow is in
459 * MSG_FLOW_READING. The valid sub-states and transitions are:
461 * READ_STATE_HEADER <--+<-------------+
464 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
466 * +----------------------------+
468 * [SUB_STATE_FINISHED]
470 * READ_STATE_HEADER has the responsibility for reading in the message header
471 * and transitioning the state of the handshake state machine.
473 * READ_STATE_BODY reads in the rest of the message and then subsequently
476 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
477 * processing activity performed on the message may block.
479 * Any of the above states could result in an NBIO event occurring in which case
480 * control returns to the calling application. When this function is recalled we
481 * will resume in the same state where we left off.
483 static SUB_STATE_RETURN read_state_machine(SSL *s)
485 OSSL_STATEM *st = &s->statem;
488 int (*transition) (SSL *s, int mt);
490 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
491 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
492 size_t (*max_message_size) (SSL *s);
493 void (*cb) (const SSL *ssl, int type, int val) = NULL;
495 cb = get_callback(s);
498 transition = ossl_statem_server_read_transition;
499 process_message = ossl_statem_server_process_message;
500 max_message_size = ossl_statem_server_max_message_size;
501 post_process_message = ossl_statem_server_post_process_message;
503 transition = ossl_statem_client_read_transition;
504 process_message = ossl_statem_client_process_message;
505 max_message_size = ossl_statem_client_max_message_size;
506 post_process_message = ossl_statem_client_post_process_message;
509 if (st->read_state_first_init) {
511 st->read_state_first_init = 0;
515 switch (st->read_state) {
516 case READ_STATE_HEADER:
517 /* Get the state the peer wants to move to */
518 if (SSL_IS_DTLS(s)) {
520 * In DTLS we get the whole message in one go - header and body
522 ret = dtls_get_message(s, &mt, &len);
524 ret = tls_get_message_header(s, &mt);
528 /* Could be non-blocking IO */
529 return SUB_STATE_ERROR;
533 /* Notify callback of an impending state change */
535 cb(s, SSL_CB_ACCEPT_LOOP, 1);
537 cb(s, SSL_CB_CONNECT_LOOP, 1);
540 * Validate that we are allowed to move to the new state and move
541 * to that state if so
543 if (!transition(s, mt)) {
544 ossl_statem_set_error(s);
545 return SUB_STATE_ERROR;
548 if (s->s3->tmp.message_size > max_message_size(s)) {
549 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
550 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
551 return SUB_STATE_ERROR;
554 /* dtls_get_message already did this */
556 && s->s3->tmp.message_size > 0
557 && !grow_init_buf(s, s->s3->tmp.message_size
558 + SSL3_HM_HEADER_LENGTH)) {
559 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
560 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
561 return SUB_STATE_ERROR;
564 st->read_state = READ_STATE_BODY;
567 case READ_STATE_BODY:
568 if (!SSL_IS_DTLS(s)) {
569 /* We already got this above for DTLS */
570 ret = tls_get_message_body(s, &len);
572 /* Could be non-blocking IO */
573 return SUB_STATE_ERROR;
578 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
579 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
580 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
581 return SUB_STATE_ERROR;
583 ret = process_message(s, &pkt);
585 /* Discard the packet data */
589 case MSG_PROCESS_ERROR:
590 return SUB_STATE_ERROR;
592 case MSG_PROCESS_FINISHED_READING:
593 if (SSL_IS_DTLS(s)) {
596 return SUB_STATE_FINISHED;
598 case MSG_PROCESS_CONTINUE_PROCESSING:
599 st->read_state = READ_STATE_POST_PROCESS;
600 st->read_state_work = WORK_MORE_A;
604 st->read_state = READ_STATE_HEADER;
609 case READ_STATE_POST_PROCESS:
610 st->read_state_work = post_process_message(s, st->read_state_work);
611 switch (st->read_state_work) {
615 return SUB_STATE_ERROR;
617 case WORK_FINISHED_CONTINUE:
618 st->read_state = READ_STATE_HEADER;
621 case WORK_FINISHED_STOP:
622 if (SSL_IS_DTLS(s)) {
625 return SUB_STATE_FINISHED;
630 /* Shouldn't happen */
631 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
632 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
633 ossl_statem_set_error(s);
634 return SUB_STATE_ERROR;
640 * Send a previously constructed message to the peer.
642 static int statem_do_write(SSL *s)
644 OSSL_STATEM *st = &s->statem;
646 if (st->hand_state == TLS_ST_CW_CHANGE
647 || st->hand_state == TLS_ST_SW_CHANGE) {
649 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
651 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
653 return ssl_do_write(s);
658 * Initialise the MSG_FLOW_WRITING sub-state machine
660 static void init_write_state_machine(SSL *s)
662 OSSL_STATEM *st = &s->statem;
664 st->write_state = WRITE_STATE_TRANSITION;
668 * This function implements the sub-state machine when the message flow is in
669 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
671 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
674 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
680 * | WRITE_STATE_POST_WORK
684 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
686 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
687 * sending of the message. This could result in an NBIO event occurring in
688 * which case control returns to the calling application. When this function
689 * is recalled we will resume in the same state where we left off.
691 * WRITE_STATE_SEND sends the message and performs any work to be done after
694 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
695 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
696 * result in an NBIO event.
698 static SUB_STATE_RETURN write_state_machine(SSL *s)
700 OSSL_STATEM *st = &s->statem;
702 WRITE_TRAN(*transition) (SSL *s);
703 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
704 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
705 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
706 int (**confunc) (SSL *s, WPACKET *pkt),
708 void (*cb) (const SSL *ssl, int type, int val) = NULL;
709 int (*confunc) (SSL *s, WPACKET *pkt);
713 cb = get_callback(s);
716 transition = ossl_statem_server_write_transition;
717 pre_work = ossl_statem_server_pre_work;
718 post_work = ossl_statem_server_post_work;
719 get_construct_message_f = ossl_statem_server_construct_message;
721 transition = ossl_statem_client_write_transition;
722 pre_work = ossl_statem_client_pre_work;
723 post_work = ossl_statem_client_post_work;
724 get_construct_message_f = ossl_statem_client_construct_message;
728 switch (st->write_state) {
729 case WRITE_STATE_TRANSITION:
731 /* Notify callback of an impending state change */
733 cb(s, SSL_CB_ACCEPT_LOOP, 1);
735 cb(s, SSL_CB_CONNECT_LOOP, 1);
737 switch (transition(s)) {
738 case WRITE_TRAN_CONTINUE:
739 st->write_state = WRITE_STATE_PRE_WORK;
740 st->write_state_work = WORK_MORE_A;
743 case WRITE_TRAN_FINISHED:
744 return SUB_STATE_FINISHED;
747 case WRITE_TRAN_ERROR:
748 return SUB_STATE_ERROR;
752 case WRITE_STATE_PRE_WORK:
753 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
757 return SUB_STATE_ERROR;
759 case WORK_FINISHED_CONTINUE:
760 st->write_state = WRITE_STATE_SEND;
763 case WORK_FINISHED_STOP:
764 return SUB_STATE_END_HANDSHAKE;
766 if (!WPACKET_init(&pkt, s->init_buf)
767 || !get_construct_message_f(s, &pkt, &confunc, &mt)
768 || !ssl_set_handshake_header(s, &pkt, mt)
769 || (confunc != NULL && !confunc(s, &pkt))
770 || !ssl_close_construct_packet(s, &pkt, mt)
771 || !WPACKET_finish(&pkt)) {
772 WPACKET_cleanup(&pkt);
773 ossl_statem_set_error(s);
774 return SUB_STATE_ERROR;
779 case WRITE_STATE_SEND:
780 if (SSL_IS_DTLS(s) && st->use_timer) {
781 dtls1_start_timer(s);
783 ret = statem_do_write(s);
785 return SUB_STATE_ERROR;
787 st->write_state = WRITE_STATE_POST_WORK;
788 st->write_state_work = WORK_MORE_A;
791 case WRITE_STATE_POST_WORK:
792 switch (st->write_state_work = post_work(s, st->write_state_work)) {
796 return SUB_STATE_ERROR;
798 case WORK_FINISHED_CONTINUE:
799 st->write_state = WRITE_STATE_TRANSITION;
802 case WORK_FINISHED_STOP:
803 return SUB_STATE_END_HANDSHAKE;
808 return SUB_STATE_ERROR;
814 * Flush the write BIO
816 int statem_flush(SSL *s)
818 s->rwstate = SSL_WRITING;
819 if (BIO_flush(s->wbio) <= 0) {
822 s->rwstate = SSL_NOTHING;
828 * Called by the record layer to determine whether application data is
829 * allowed to be sent in the current handshake state or not.
832 * 1: Yes (application data allowed)
833 * 0: No (application data not allowed)
835 int ossl_statem_app_data_allowed(SSL *s)
837 OSSL_STATEM *st = &s->statem;
839 if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE)
842 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
847 * If we're a server and we haven't got as far as writing our
848 * ServerHello yet then we allow app data
850 if (st->hand_state == TLS_ST_BEFORE
851 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
855 * If we're a client and we haven't read the ServerHello yet then we
858 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
865 #ifndef OPENSSL_NO_SCTP
867 * Set flag used by SCTP to determine whether we are in the read sock state
869 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
871 s->statem.in_sctp_read_sock = read_sock;
875 * Called by the record layer to determine whether we are in the read sock
879 * 1: Yes (we are in the read sock state)
880 * 0: No (we are not in the read sock state)
882 int ossl_statem_in_sctp_read_sock(SSL *s)
884 return s->statem.in_sctp_read_sock;