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 #ifndef OPENSSL_NO_HEARTBEATS
256 * If we're awaiting a HeartbeatResponse, pretend we already got and
257 * don't await it anymore, because Heartbeats don't make sense during
260 if (s->tlsext_hb_pending) {
263 s->tlsext_hb_pending = 0;
268 /* Initialise state machine */
270 if (st->state == MSG_FLOW_RENEGOTIATE) {
273 s->ctx->stats.sess_connect_renegotiate++;
276 if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE) {
277 if (st->state == MSG_FLOW_UNINITED) {
278 st->hand_state = TLS_ST_BEFORE;
283 cb(s, SSL_CB_HANDSHAKE_START, 1);
285 if (SSL_IS_DTLS(s)) {
286 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
287 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
288 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
292 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
293 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
298 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
299 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
303 if (s->init_buf == NULL) {
304 if ((buf = BUF_MEM_new()) == NULL) {
307 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
314 if (!ssl3_setup_buffers(s)) {
320 * Should have been reset by tls_process_finished, too.
322 s->s3->change_cipher_spec = 0;
325 * Ok, we now need to push on a buffering BIO ...but not with
328 #ifndef OPENSSL_NO_SCTP
329 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
331 if (!ssl_init_wbio_buffer(s)) {
335 if (!server || st->state != MSG_FLOW_RENEGOTIATE) {
336 if (!ssl3_init_finished_mac(s)) {
337 ossl_statem_set_error(s);
343 if (st->state != MSG_FLOW_RENEGOTIATE) {
344 s->ctx->stats.sess_accept++;
345 } else if (!s->s3->send_connection_binding &&
347 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
349 * Server attempting to renegotiate with client that doesn't
350 * support secure renegotiation.
352 SSLerr(SSL_F_STATE_MACHINE,
353 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
354 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
355 ossl_statem_set_error(s);
359 * st->state == MSG_FLOW_RENEGOTIATE, we will just send a
362 s->ctx->stats.sess_accept_renegotiate++;
365 s->s3->tmp.cert_request = 0;
367 s->ctx->stats.sess_connect++;
369 /* mark client_random uninitialized */
370 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
373 s->s3->tmp.cert_req = 0;
375 if (SSL_IS_DTLS(s)) {
380 st->state = MSG_FLOW_WRITING;
381 init_write_state_machine(s);
382 st->read_state_first_init = 1;
385 while (st->state != MSG_FLOW_FINISHED) {
386 if (st->state == MSG_FLOW_READING) {
387 ssret = read_state_machine(s);
388 if (ssret == SUB_STATE_FINISHED) {
389 st->state = MSG_FLOW_WRITING;
390 init_write_state_machine(s);
395 } else if (st->state == MSG_FLOW_WRITING) {
396 ssret = write_state_machine(s);
397 if (ssret == SUB_STATE_FINISHED) {
398 st->state = MSG_FLOW_READING;
399 init_read_state_machine(s);
400 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
401 st->state = MSG_FLOW_FINISHED;
408 ossl_statem_set_error(s);
413 st->state = MSG_FLOW_UNINITED;
419 #ifndef OPENSSL_NO_SCTP
420 if (SSL_IS_DTLS(s)) {
422 * Notify SCTP BIO socket to leave handshake mode and allow stream
423 * identifier other than 0. Will be ignored if no SCTP is used.
425 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
426 st->in_handshake, NULL);
433 cb(s, SSL_CB_ACCEPT_EXIT, ret);
435 cb(s, SSL_CB_CONNECT_EXIT, ret);
441 * Initialise the MSG_FLOW_READING sub-state machine
443 static void init_read_state_machine(SSL *s)
445 OSSL_STATEM *st = &s->statem;
447 st->read_state = READ_STATE_HEADER;
450 static int grow_init_buf(SSL *s, size_t size) {
452 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
454 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
457 if (size < msg_offset)
460 s->init_msg = s->init_buf->data + msg_offset;
466 * This function implements the sub-state machine when the message flow is in
467 * MSG_FLOW_READING. The valid sub-states and transitions are:
469 * READ_STATE_HEADER <--+<-------------+
472 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
474 * +----------------------------+
476 * [SUB_STATE_FINISHED]
478 * READ_STATE_HEADER has the responsibility for reading in the message header
479 * and transitioning the state of the handshake state machine.
481 * READ_STATE_BODY reads in the rest of the message and then subsequently
484 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
485 * processing activity performed on the message may block.
487 * Any of the above states could result in an NBIO event occurring in which case
488 * control returns to the calling application. When this function is recalled we
489 * will resume in the same state where we left off.
491 static SUB_STATE_RETURN read_state_machine(SSL *s)
493 OSSL_STATEM *st = &s->statem;
495 unsigned long len = 0;
496 int (*transition) (SSL *s, int mt);
498 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
499 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
500 unsigned long (*max_message_size) (SSL *s);
501 void (*cb) (const SSL *ssl, int type, int val) = NULL;
503 cb = get_callback(s);
506 transition = ossl_statem_server_read_transition;
507 process_message = ossl_statem_server_process_message;
508 max_message_size = ossl_statem_server_max_message_size;
509 post_process_message = ossl_statem_server_post_process_message;
511 transition = ossl_statem_client_read_transition;
512 process_message = ossl_statem_client_process_message;
513 max_message_size = ossl_statem_client_max_message_size;
514 post_process_message = ossl_statem_client_post_process_message;
517 if (st->read_state_first_init) {
519 st->read_state_first_init = 0;
523 switch (st->read_state) {
524 case READ_STATE_HEADER:
525 /* Get the state the peer wants to move to */
526 if (SSL_IS_DTLS(s)) {
528 * In DTLS we get the whole message in one go - header and body
530 ret = dtls_get_message(s, &mt, &len);
532 ret = tls_get_message_header(s, &mt);
536 /* Could be non-blocking IO */
537 return SUB_STATE_ERROR;
541 /* Notify callback of an impending state change */
543 cb(s, SSL_CB_ACCEPT_LOOP, 1);
545 cb(s, SSL_CB_CONNECT_LOOP, 1);
548 * Validate that we are allowed to move to the new state and move
549 * to that state if so
551 if (!transition(s, mt)) {
552 ossl_statem_set_error(s);
553 return SUB_STATE_ERROR;
556 if (s->s3->tmp.message_size > max_message_size(s)) {
557 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
558 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
559 return SUB_STATE_ERROR;
562 /* dtls_get_message already did this */
564 && s->s3->tmp.message_size > 0
565 && !grow_init_buf(s, s->s3->tmp.message_size
566 + SSL3_HM_HEADER_LENGTH)) {
567 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
568 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
569 return SUB_STATE_ERROR;
572 st->read_state = READ_STATE_BODY;
575 case READ_STATE_BODY:
576 if (!SSL_IS_DTLS(s)) {
577 /* We already got this above for DTLS */
578 ret = tls_get_message_body(s, &len);
580 /* Could be non-blocking IO */
581 return SUB_STATE_ERROR;
586 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
587 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
588 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
589 return SUB_STATE_ERROR;
591 ret = process_message(s, &pkt);
593 /* Discard the packet data */
597 case MSG_PROCESS_ERROR:
598 return SUB_STATE_ERROR;
600 case MSG_PROCESS_FINISHED_READING:
601 if (SSL_IS_DTLS(s)) {
604 return SUB_STATE_FINISHED;
606 case MSG_PROCESS_CONTINUE_PROCESSING:
607 st->read_state = READ_STATE_POST_PROCESS;
608 st->read_state_work = WORK_MORE_A;
612 st->read_state = READ_STATE_HEADER;
617 case READ_STATE_POST_PROCESS:
618 st->read_state_work = post_process_message(s, st->read_state_work);
619 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 (*construct_message) (SSL *s);
712 void (*cb) (const SSL *ssl, int type, int val) = NULL;
714 cb = get_callback(s);
717 transition = ossl_statem_server_write_transition;
718 pre_work = ossl_statem_server_pre_work;
719 post_work = ossl_statem_server_post_work;
720 construct_message = ossl_statem_server_construct_message;
722 transition = ossl_statem_client_write_transition;
723 pre_work = ossl_statem_client_pre_work;
724 post_work = ossl_statem_client_post_work;
725 construct_message = ossl_statem_client_construct_message;
729 switch (st->write_state) {
730 case WRITE_STATE_TRANSITION:
732 /* Notify callback of an impending state change */
734 cb(s, SSL_CB_ACCEPT_LOOP, 1);
736 cb(s, SSL_CB_CONNECT_LOOP, 1);
738 switch (transition(s)) {
739 case WRITE_TRAN_CONTINUE:
740 st->write_state = WRITE_STATE_PRE_WORK;
741 st->write_state_work = WORK_MORE_A;
744 case WRITE_TRAN_FINISHED:
745 return SUB_STATE_FINISHED;
749 return SUB_STATE_ERROR;
753 case WRITE_STATE_PRE_WORK:
754 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
756 return SUB_STATE_ERROR;
758 case WORK_FINISHED_CONTINUE:
759 st->write_state = WRITE_STATE_SEND;
762 case WORK_FINISHED_STOP:
763 return SUB_STATE_END_HANDSHAKE;
765 if (construct_message(s) == 0)
766 return SUB_STATE_ERROR;
770 case WRITE_STATE_SEND:
771 if (SSL_IS_DTLS(s) && st->use_timer) {
772 dtls1_start_timer(s);
774 ret = statem_do_write(s);
776 return SUB_STATE_ERROR;
778 st->write_state = WRITE_STATE_POST_WORK;
779 st->write_state_work = WORK_MORE_A;
782 case WRITE_STATE_POST_WORK:
783 switch (st->write_state_work = post_work(s, st->write_state_work)) {
785 return SUB_STATE_ERROR;
787 case WORK_FINISHED_CONTINUE:
788 st->write_state = WRITE_STATE_TRANSITION;
791 case WORK_FINISHED_STOP:
792 return SUB_STATE_END_HANDSHAKE;
797 return SUB_STATE_ERROR;
803 * Flush the write BIO
805 int statem_flush(SSL *s)
807 s->rwstate = SSL_WRITING;
808 if (BIO_flush(s->wbio) <= 0) {
811 s->rwstate = SSL_NOTHING;
817 * Called by the record layer to determine whether application data is
818 * allowed to be sent in the current handshake state or not.
821 * 1: Yes (application data allowed)
822 * 0: No (application data not allowed)
824 int ossl_statem_app_data_allowed(SSL *s)
826 OSSL_STATEM *st = &s->statem;
828 if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE)
831 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
836 * If we're a server and we haven't got as far as writing our
837 * ServerHello yet then we allow app data
839 if (st->hand_state == TLS_ST_BEFORE
840 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
844 * If we're a client and we haven't read the ServerHello yet then we
847 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
854 #ifndef OPENSSL_NO_SCTP
856 * Set flag used by SCTP to determine whether we are in the read sock state
858 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
860 s->statem.in_sctp_read_sock = read_sock;
864 * Called by the record layer to determine whether we are in the read sock
868 * 1: Yes (we are in the read sock state)
869 * 0: No (we are not in the read sock state)
871 int ossl_statem_in_sctp_read_sock(SSL *s)
873 return s->statem.in_sctp_read_sock;