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;
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 void ossl_statem_set_hello_verify_done(SSL *s)
156 s->statem.state = MSG_FLOW_UNINITED;
157 s->statem.in_init = 1;
159 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
160 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
161 * calls to SSL_in_before() will return false. Also calls to
162 * SSL_state_string() and SSL_state_string_long() will return something
165 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
168 int ossl_statem_connect(SSL *s)
170 return state_machine(s, 0);
173 int ossl_statem_accept(SSL *s)
175 return state_machine(s, 1);
178 typedef void (*info_cb) (const SSL *, int, int);
180 static info_cb get_callback(SSL *s)
182 if (s->info_callback != NULL)
183 return s->info_callback;
184 else if (s->ctx->info_callback != NULL)
185 return s->ctx->info_callback;
191 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
192 * MSG_FLOW_RENEGOTIATE state and finish in MSG_FLOW_FINISHED. Valid states and
193 * transitions are as follows:
195 * MSG_FLOW_UNINITED MSG_FLOW_RENEGOTIATE
197 * +-----------------------+
199 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
207 * We may exit at any point due to an error or NBIO event. If an NBIO event
208 * occurs then we restart at the point we left off when we are recalled.
209 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
211 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
212 * into that state at any point in the event that an irrecoverable error occurs.
214 * Valid return values are:
218 static int state_machine(SSL *s, int server)
221 unsigned long Time = (unsigned long)time(NULL);
222 void (*cb) (const SSL *ssl, int type, int val) = NULL;
223 OSSL_STATEM *st = &s->statem;
227 if (st->state == MSG_FLOW_ERROR) {
228 /* Shouldn't have been called if we're already in the error state */
232 RAND_add(&Time, sizeof(Time), 0);
236 cb = get_callback(s);
239 if (!SSL_in_init(s) || SSL_in_before(s)) {
243 #ifndef OPENSSL_NO_SCTP
244 if (SSL_IS_DTLS(s)) {
246 * Notify SCTP BIO socket to enter handshake mode and prevent stream
247 * identifier other than 0. Will be ignored if no SCTP is used.
249 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
250 st->in_handshake, NULL);
254 /* Initialise state machine */
256 if (st->state == MSG_FLOW_RENEGOTIATE) {
259 s->ctx->stats.sess_connect_renegotiate++;
262 if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE) {
263 if (st->state == MSG_FLOW_UNINITED) {
264 st->hand_state = TLS_ST_BEFORE;
269 cb(s, SSL_CB_HANDSHAKE_START, 1);
271 if (SSL_IS_DTLS(s)) {
272 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
273 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
274 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
278 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
279 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
284 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
285 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
289 if (s->init_buf == NULL) {
290 if ((buf = BUF_MEM_new()) == NULL) {
293 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
300 if (!ssl3_setup_buffers(s)) {
306 * Should have been reset by tls_process_finished, too.
308 s->s3->change_cipher_spec = 0;
311 * Ok, we now need to push on a buffering BIO ...but not with
314 #ifndef OPENSSL_NO_SCTP
315 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
317 if (!ssl_init_wbio_buffer(s)) {
321 if (!server || st->state != MSG_FLOW_RENEGOTIATE) {
322 if (!ssl3_init_finished_mac(s)) {
323 ossl_statem_set_error(s);
329 if (st->state != MSG_FLOW_RENEGOTIATE) {
330 s->ctx->stats.sess_accept++;
331 } else if (!s->s3->send_connection_binding &&
333 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
335 * Server attempting to renegotiate with client that doesn't
336 * support secure renegotiation.
338 SSLerr(SSL_F_STATE_MACHINE,
339 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
340 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
341 ossl_statem_set_error(s);
345 * st->state == MSG_FLOW_RENEGOTIATE, we will just send a
348 s->ctx->stats.sess_accept_renegotiate++;
351 s->ctx->stats.sess_connect++;
353 /* mark client_random uninitialized */
354 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
357 s->s3->tmp.cert_request = 0;
358 s->s3->tmp.cert_req = 0;
360 if (SSL_IS_DTLS(s)) {
365 st->state = MSG_FLOW_WRITING;
366 init_write_state_machine(s);
367 st->read_state_first_init = 1;
370 while (st->state != MSG_FLOW_FINISHED) {
371 if (st->state == MSG_FLOW_READING) {
372 ssret = read_state_machine(s);
373 if (ssret == SUB_STATE_FINISHED) {
374 st->state = MSG_FLOW_WRITING;
375 init_write_state_machine(s);
380 } else if (st->state == MSG_FLOW_WRITING) {
381 ssret = write_state_machine(s);
382 if (ssret == SUB_STATE_FINISHED) {
383 st->state = MSG_FLOW_READING;
384 init_read_state_machine(s);
385 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
386 st->state = MSG_FLOW_FINISHED;
393 ossl_statem_set_error(s);
398 st->state = MSG_FLOW_UNINITED;
404 #ifndef OPENSSL_NO_SCTP
405 if (SSL_IS_DTLS(s)) {
407 * Notify SCTP BIO socket to leave handshake mode and allow stream
408 * identifier other than 0. Will be ignored if no SCTP is used.
410 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
411 st->in_handshake, NULL);
418 cb(s, SSL_CB_ACCEPT_EXIT, ret);
420 cb(s, SSL_CB_CONNECT_EXIT, ret);
426 * Initialise the MSG_FLOW_READING sub-state machine
428 static void init_read_state_machine(SSL *s)
430 OSSL_STATEM *st = &s->statem;
432 st->read_state = READ_STATE_HEADER;
435 static int grow_init_buf(SSL *s, size_t size) {
437 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
439 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
442 if (size < msg_offset)
445 s->init_msg = s->init_buf->data + msg_offset;
451 * This function implements the sub-state machine when the message flow is in
452 * MSG_FLOW_READING. The valid sub-states and transitions are:
454 * READ_STATE_HEADER <--+<-------------+
457 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
459 * +----------------------------+
461 * [SUB_STATE_FINISHED]
463 * READ_STATE_HEADER has the responsibility for reading in the message header
464 * and transitioning the state of the handshake state machine.
466 * READ_STATE_BODY reads in the rest of the message and then subsequently
469 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
470 * processing activity performed on the message may block.
472 * Any of the above states could result in an NBIO event occurring in which case
473 * control returns to the calling application. When this function is recalled we
474 * will resume in the same state where we left off.
476 static SUB_STATE_RETURN read_state_machine(SSL *s)
478 OSSL_STATEM *st = &s->statem;
481 int (*transition) (SSL *s, int mt);
483 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
484 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
485 size_t (*max_message_size) (SSL *s);
486 void (*cb) (const SSL *ssl, int type, int val) = NULL;
488 cb = get_callback(s);
491 transition = ossl_statem_server_read_transition;
492 process_message = ossl_statem_server_process_message;
493 max_message_size = ossl_statem_server_max_message_size;
494 post_process_message = ossl_statem_server_post_process_message;
496 transition = ossl_statem_client_read_transition;
497 process_message = ossl_statem_client_process_message;
498 max_message_size = ossl_statem_client_max_message_size;
499 post_process_message = ossl_statem_client_post_process_message;
502 if (st->read_state_first_init) {
504 st->read_state_first_init = 0;
508 switch (st->read_state) {
509 case READ_STATE_HEADER:
510 /* Get the state the peer wants to move to */
511 if (SSL_IS_DTLS(s)) {
513 * In DTLS we get the whole message in one go - header and body
515 ret = dtls_get_message(s, &mt, &len);
517 ret = tls_get_message_header(s, &mt);
521 /* Could be non-blocking IO */
522 return SUB_STATE_ERROR;
526 /* Notify callback of an impending state change */
528 cb(s, SSL_CB_ACCEPT_LOOP, 1);
530 cb(s, SSL_CB_CONNECT_LOOP, 1);
533 * Validate that we are allowed to move to the new state and move
534 * to that state if so
536 if (!transition(s, mt)) {
537 ossl_statem_set_error(s);
538 return SUB_STATE_ERROR;
541 if (s->s3->tmp.message_size > max_message_size(s)) {
542 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
543 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
544 return SUB_STATE_ERROR;
547 /* dtls_get_message already did this */
549 && s->s3->tmp.message_size > 0
550 && !grow_init_buf(s, s->s3->tmp.message_size
551 + SSL3_HM_HEADER_LENGTH)) {
552 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
553 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
554 return SUB_STATE_ERROR;
557 st->read_state = READ_STATE_BODY;
560 case READ_STATE_BODY:
561 if (!SSL_IS_DTLS(s)) {
562 /* We already got this above for DTLS */
563 ret = tls_get_message_body(s, &len);
565 /* Could be non-blocking IO */
566 return SUB_STATE_ERROR;
571 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
572 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
573 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
574 return SUB_STATE_ERROR;
576 ret = process_message(s, &pkt);
578 /* Discard the packet data */
582 case MSG_PROCESS_ERROR:
583 return SUB_STATE_ERROR;
585 case MSG_PROCESS_FINISHED_READING:
586 if (SSL_IS_DTLS(s)) {
589 return SUB_STATE_FINISHED;
591 case MSG_PROCESS_CONTINUE_PROCESSING:
592 st->read_state = READ_STATE_POST_PROCESS;
593 st->read_state_work = WORK_MORE_A;
597 st->read_state = READ_STATE_HEADER;
602 case READ_STATE_POST_PROCESS:
603 st->read_state_work = post_process_message(s, st->read_state_work);
604 switch (st->read_state_work) {
608 return SUB_STATE_ERROR;
610 case WORK_FINISHED_CONTINUE:
611 st->read_state = READ_STATE_HEADER;
614 case WORK_FINISHED_STOP:
615 if (SSL_IS_DTLS(s)) {
618 return SUB_STATE_FINISHED;
623 /* Shouldn't happen */
624 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
625 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
626 ossl_statem_set_error(s);
627 return SUB_STATE_ERROR;
633 * Send a previously constructed message to the peer.
635 static int statem_do_write(SSL *s)
637 OSSL_STATEM *st = &s->statem;
639 if (st->hand_state == TLS_ST_CW_CHANGE
640 || st->hand_state == TLS_ST_SW_CHANGE) {
642 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
644 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
646 return ssl_do_write(s);
651 * Initialise the MSG_FLOW_WRITING sub-state machine
653 static void init_write_state_machine(SSL *s)
655 OSSL_STATEM *st = &s->statem;
657 st->write_state = WRITE_STATE_TRANSITION;
661 * This function implements the sub-state machine when the message flow is in
662 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
664 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
667 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
673 * | WRITE_STATE_POST_WORK
677 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
679 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
680 * sending of the message. This could result in an NBIO event occurring in
681 * which case control returns to the calling application. When this function
682 * is recalled we will resume in the same state where we left off.
684 * WRITE_STATE_SEND sends the message and performs any work to be done after
687 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
688 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
689 * result in an NBIO event.
691 static SUB_STATE_RETURN write_state_machine(SSL *s)
693 OSSL_STATEM *st = &s->statem;
695 WRITE_TRAN(*transition) (SSL *s);
696 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
697 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
698 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
699 int (**confunc) (SSL *s, WPACKET *pkt),
701 void (*cb) (const SSL *ssl, int type, int val) = NULL;
702 int (*confunc) (SSL *s, WPACKET *pkt);
706 cb = get_callback(s);
709 transition = ossl_statem_server_write_transition;
710 pre_work = ossl_statem_server_pre_work;
711 post_work = ossl_statem_server_post_work;
712 get_construct_message_f = ossl_statem_server_construct_message;
714 transition = ossl_statem_client_write_transition;
715 pre_work = ossl_statem_client_pre_work;
716 post_work = ossl_statem_client_post_work;
717 get_construct_message_f = ossl_statem_client_construct_message;
721 switch (st->write_state) {
722 case WRITE_STATE_TRANSITION:
724 /* Notify callback of an impending state change */
726 cb(s, SSL_CB_ACCEPT_LOOP, 1);
728 cb(s, SSL_CB_CONNECT_LOOP, 1);
730 switch (transition(s)) {
731 case WRITE_TRAN_CONTINUE:
732 st->write_state = WRITE_STATE_PRE_WORK;
733 st->write_state_work = WORK_MORE_A;
736 case WRITE_TRAN_FINISHED:
737 return SUB_STATE_FINISHED;
740 case WRITE_TRAN_ERROR:
741 return SUB_STATE_ERROR;
745 case WRITE_STATE_PRE_WORK:
746 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
750 return SUB_STATE_ERROR;
752 case WORK_FINISHED_CONTINUE:
753 st->write_state = WRITE_STATE_SEND;
756 case WORK_FINISHED_STOP:
757 return SUB_STATE_END_HANDSHAKE;
759 if (!WPACKET_init(&pkt, s->init_buf)
760 || !get_construct_message_f(s, &pkt, &confunc, &mt)
761 || !ssl_set_handshake_header(s, &pkt, mt)
762 || (confunc != NULL && !confunc(s, &pkt))
763 || !ssl_close_construct_packet(s, &pkt, mt)
764 || !WPACKET_finish(&pkt)) {
765 WPACKET_cleanup(&pkt);
766 ossl_statem_set_error(s);
767 return SUB_STATE_ERROR;
772 case WRITE_STATE_SEND:
773 if (SSL_IS_DTLS(s) && st->use_timer) {
774 dtls1_start_timer(s);
776 ret = statem_do_write(s);
778 return SUB_STATE_ERROR;
780 st->write_state = WRITE_STATE_POST_WORK;
781 st->write_state_work = WORK_MORE_A;
784 case WRITE_STATE_POST_WORK:
785 switch (st->write_state_work = post_work(s, st->write_state_work)) {
789 return SUB_STATE_ERROR;
791 case WORK_FINISHED_CONTINUE:
792 st->write_state = WRITE_STATE_TRANSITION;
795 case WORK_FINISHED_STOP:
796 return SUB_STATE_END_HANDSHAKE;
801 return SUB_STATE_ERROR;
807 * Flush the write BIO
809 int statem_flush(SSL *s)
811 s->rwstate = SSL_WRITING;
812 if (BIO_flush(s->wbio) <= 0) {
815 s->rwstate = SSL_NOTHING;
821 * Called by the record layer to determine whether application data is
822 * allowed to be sent in the current handshake state or not.
825 * 1: Yes (application data allowed)
826 * 0: No (application data not allowed)
828 int ossl_statem_app_data_allowed(SSL *s)
830 OSSL_STATEM *st = &s->statem;
832 if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE)
835 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
840 * If we're a server and we haven't got as far as writing our
841 * ServerHello yet then we allow app data
843 if (st->hand_state == TLS_ST_BEFORE
844 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
848 * If we're a client and we haven't read the ServerHello yet then we
851 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
858 #ifndef OPENSSL_NO_SCTP
860 * Set flag used by SCTP to determine whether we are in the read sock state
862 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
864 s->statem.in_sctp_read_sock = read_sock;
868 * Called by the record layer to determine whether we are in the read sock
872 * 1: Yes (we are in the read sock state)
873 * 0: No (we are not in the read sock state)
875 int ossl_statem_in_sctp_read_sock(SSL *s)
877 return s->statem.in_sctp_read_sock;