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.expect_early_data)
160 if (s->statem.hand_state != TLS_ST_SW_FINISHED)
166 void ossl_statem_set_hello_verify_done(SSL *s)
168 s->statem.state = MSG_FLOW_UNINITED;
169 s->statem.in_init = 1;
171 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
172 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
173 * calls to SSL_in_before() will return false. Also calls to
174 * SSL_state_string() and SSL_state_string_long() will return something
177 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
180 int ossl_statem_connect(SSL *s)
182 return state_machine(s, 0);
185 int ossl_statem_accept(SSL *s)
187 return state_machine(s, 1);
190 typedef void (*info_cb) (const SSL *, int, int);
192 static info_cb get_callback(SSL *s)
194 if (s->info_callback != NULL)
195 return s->info_callback;
196 else if (s->ctx->info_callback != NULL)
197 return s->ctx->info_callback;
203 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
204 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
205 * transitions are as follows:
207 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
209 * +-----------------------+
211 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
219 * We may exit at any point due to an error or NBIO event. If an NBIO event
220 * occurs then we restart at the point we left off when we are recalled.
221 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
223 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
224 * into that state at any point in the event that an irrecoverable error occurs.
226 * Valid return values are:
230 static int state_machine(SSL *s, int server)
233 unsigned long Time = (unsigned long)time(NULL);
234 void (*cb) (const SSL *ssl, int type, int val) = NULL;
235 OSSL_STATEM *st = &s->statem;
239 if (st->state == MSG_FLOW_ERROR) {
240 /* Shouldn't have been called if we're already in the error state */
244 RAND_add(&Time, sizeof(Time), 0);
248 cb = get_callback(s);
251 if (!SSL_in_init(s) || SSL_in_before(s)) {
255 #ifndef OPENSSL_NO_SCTP
256 if (SSL_IS_DTLS(s)) {
258 * Notify SCTP BIO socket to enter handshake mode and prevent stream
259 * identifier other than 0. Will be ignored if no SCTP is used.
261 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
262 st->in_handshake, NULL);
266 /* Initialise state machine */
267 if (st->state == MSG_FLOW_UNINITED
268 || st->state == MSG_FLOW_FINISHED) {
269 if (st->state == MSG_FLOW_UNINITED) {
270 st->hand_state = TLS_ST_BEFORE;
271 st->request_state = TLS_ST_BEFORE;
276 cb(s, SSL_CB_HANDSHAKE_START, 1);
278 if (SSL_IS_DTLS(s)) {
279 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
280 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
281 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
285 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
286 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
291 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
292 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
296 if (s->init_buf == NULL) {
297 if ((buf = BUF_MEM_new()) == NULL) {
300 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
307 if (!ssl3_setup_buffers(s)) {
313 * Should have been reset by tls_process_finished, too.
315 s->s3->change_cipher_spec = 0;
318 * Ok, we now need to push on a buffering BIO ...but not with
321 #ifndef OPENSSL_NO_SCTP
322 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
324 if (!ssl_init_wbio_buffer(s)) {
328 if ((SSL_IS_FIRST_HANDSHAKE(s)
329 && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
331 if (!tls_setup_handshake(s)) {
332 ossl_statem_set_error(s);
336 if (SSL_IS_FIRST_HANDSHAKE(s))
337 st->read_state_first_init = 1;
340 st->state = MSG_FLOW_WRITING;
341 init_write_state_machine(s);
344 while (st->state != MSG_FLOW_FINISHED) {
345 if (st->state == MSG_FLOW_READING) {
346 ssret = read_state_machine(s);
347 if (ssret == SUB_STATE_FINISHED) {
348 st->state = MSG_FLOW_WRITING;
349 init_write_state_machine(s);
354 } else if (st->state == MSG_FLOW_WRITING) {
355 ssret = write_state_machine(s);
356 if (ssret == SUB_STATE_FINISHED) {
357 st->state = MSG_FLOW_READING;
358 init_read_state_machine(s);
359 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
360 st->state = MSG_FLOW_FINISHED;
367 ossl_statem_set_error(s);
377 #ifndef OPENSSL_NO_SCTP
378 if (SSL_IS_DTLS(s)) {
380 * Notify SCTP BIO socket to leave handshake mode and allow stream
381 * identifier other than 0. Will be ignored if no SCTP is used.
383 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
384 st->in_handshake, NULL);
391 cb(s, SSL_CB_ACCEPT_EXIT, ret);
393 cb(s, SSL_CB_CONNECT_EXIT, ret);
399 * Initialise the MSG_FLOW_READING sub-state machine
401 static void init_read_state_machine(SSL *s)
403 OSSL_STATEM *st = &s->statem;
405 st->read_state = READ_STATE_HEADER;
408 static int grow_init_buf(SSL *s, size_t size) {
410 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
412 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
415 if (size < msg_offset)
418 s->init_msg = s->init_buf->data + msg_offset;
424 * This function implements the sub-state machine when the message flow is in
425 * MSG_FLOW_READING. The valid sub-states and transitions are:
427 * READ_STATE_HEADER <--+<-------------+
430 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
432 * +----------------------------+
434 * [SUB_STATE_FINISHED]
436 * READ_STATE_HEADER has the responsibility for reading in the message header
437 * and transitioning the state of the handshake state machine.
439 * READ_STATE_BODY reads in the rest of the message and then subsequently
442 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
443 * processing activity performed on the message may block.
445 * Any of the above states could result in an NBIO event occurring in which case
446 * control returns to the calling application. When this function is recalled we
447 * will resume in the same state where we left off.
449 static SUB_STATE_RETURN read_state_machine(SSL *s)
451 OSSL_STATEM *st = &s->statem;
454 int (*transition) (SSL *s, int mt);
456 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
457 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
458 size_t (*max_message_size) (SSL *s);
459 void (*cb) (const SSL *ssl, int type, int val) = NULL;
461 cb = get_callback(s);
464 transition = ossl_statem_server_read_transition;
465 process_message = ossl_statem_server_process_message;
466 max_message_size = ossl_statem_server_max_message_size;
467 post_process_message = ossl_statem_server_post_process_message;
469 transition = ossl_statem_client_read_transition;
470 process_message = ossl_statem_client_process_message;
471 max_message_size = ossl_statem_client_max_message_size;
472 post_process_message = ossl_statem_client_post_process_message;
475 if (st->read_state_first_init) {
477 st->read_state_first_init = 0;
481 switch (st->read_state) {
482 case READ_STATE_HEADER:
483 /* Get the state the peer wants to move to */
484 if (SSL_IS_DTLS(s)) {
486 * In DTLS we get the whole message in one go - header and body
488 ret = dtls_get_message(s, &mt, &len);
490 ret = tls_get_message_header(s, &mt);
494 /* Could be non-blocking IO */
495 return SUB_STATE_ERROR;
499 /* Notify callback of an impending state change */
501 cb(s, SSL_CB_ACCEPT_LOOP, 1);
503 cb(s, SSL_CB_CONNECT_LOOP, 1);
506 * Validate that we are allowed to move to the new state and move
507 * to that state if so
509 if (!transition(s, mt)) {
510 ossl_statem_set_error(s);
511 return SUB_STATE_ERROR;
514 if (s->s3->tmp.message_size > max_message_size(s)) {
515 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
516 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
517 return SUB_STATE_ERROR;
520 /* dtls_get_message already did this */
522 && s->s3->tmp.message_size > 0
523 && !grow_init_buf(s, s->s3->tmp.message_size
524 + SSL3_HM_HEADER_LENGTH)) {
525 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
526 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
527 return SUB_STATE_ERROR;
530 st->read_state = READ_STATE_BODY;
533 case READ_STATE_BODY:
534 if (!SSL_IS_DTLS(s)) {
535 /* We already got this above for DTLS */
536 ret = tls_get_message_body(s, &len);
538 /* Could be non-blocking IO */
539 return SUB_STATE_ERROR;
544 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
545 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
546 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
547 return SUB_STATE_ERROR;
549 ret = process_message(s, &pkt);
551 /* Discard the packet data */
555 case MSG_PROCESS_ERROR:
556 return SUB_STATE_ERROR;
558 case MSG_PROCESS_FINISHED_READING:
559 if (SSL_IS_DTLS(s)) {
562 return SUB_STATE_FINISHED;
564 case MSG_PROCESS_CONTINUE_PROCESSING:
565 st->read_state = READ_STATE_POST_PROCESS;
566 st->read_state_work = WORK_MORE_A;
570 st->read_state = READ_STATE_HEADER;
575 case READ_STATE_POST_PROCESS:
576 st->read_state_work = post_process_message(s, st->read_state_work);
577 switch (st->read_state_work) {
582 return SUB_STATE_ERROR;
584 case WORK_FINISHED_CONTINUE:
585 st->read_state = READ_STATE_HEADER;
588 case WORK_FINISHED_STOP:
589 if (SSL_IS_DTLS(s)) {
592 return SUB_STATE_FINISHED;
597 /* Shouldn't happen */
598 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
599 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
600 ossl_statem_set_error(s);
601 return SUB_STATE_ERROR;
607 * Send a previously constructed message to the peer.
609 static int statem_do_write(SSL *s)
611 OSSL_STATEM *st = &s->statem;
613 if (st->hand_state == TLS_ST_CW_CHANGE
614 || st->hand_state == TLS_ST_SW_CHANGE) {
616 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
618 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
620 return ssl_do_write(s);
625 * Initialise the MSG_FLOW_WRITING sub-state machine
627 static void init_write_state_machine(SSL *s)
629 OSSL_STATEM *st = &s->statem;
631 st->write_state = WRITE_STATE_TRANSITION;
635 * This function implements the sub-state machine when the message flow is in
636 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
638 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
641 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
647 * | WRITE_STATE_POST_WORK
651 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
653 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
654 * sending of the message. This could result in an NBIO event occurring in
655 * which case control returns to the calling application. When this function
656 * is recalled we will resume in the same state where we left off.
658 * WRITE_STATE_SEND sends the message and performs any work to be done after
661 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
662 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
663 * result in an NBIO event.
665 static SUB_STATE_RETURN write_state_machine(SSL *s)
667 OSSL_STATEM *st = &s->statem;
669 WRITE_TRAN(*transition) (SSL *s);
670 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
671 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
672 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
673 int (**confunc) (SSL *s, WPACKET *pkt),
675 void (*cb) (const SSL *ssl, int type, int val) = NULL;
676 int (*confunc) (SSL *s, WPACKET *pkt);
680 cb = get_callback(s);
683 transition = ossl_statem_server_write_transition;
684 pre_work = ossl_statem_server_pre_work;
685 post_work = ossl_statem_server_post_work;
686 get_construct_message_f = ossl_statem_server_construct_message;
688 transition = ossl_statem_client_write_transition;
689 pre_work = ossl_statem_client_pre_work;
690 post_work = ossl_statem_client_post_work;
691 get_construct_message_f = ossl_statem_client_construct_message;
695 switch (st->write_state) {
696 case WRITE_STATE_TRANSITION:
698 /* Notify callback of an impending state change */
700 cb(s, SSL_CB_ACCEPT_LOOP, 1);
702 cb(s, SSL_CB_CONNECT_LOOP, 1);
704 switch (transition(s)) {
705 case WRITE_TRAN_CONTINUE:
706 st->write_state = WRITE_STATE_PRE_WORK;
707 st->write_state_work = WORK_MORE_A;
710 case WRITE_TRAN_FINISHED:
711 return SUB_STATE_FINISHED;
714 case WRITE_TRAN_ERROR:
715 return SUB_STATE_ERROR;
719 case WRITE_STATE_PRE_WORK:
720 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
725 return SUB_STATE_ERROR;
727 case WORK_FINISHED_CONTINUE:
728 st->write_state = WRITE_STATE_SEND;
731 case WORK_FINISHED_STOP:
732 return SUB_STATE_END_HANDSHAKE;
734 if (!WPACKET_init(&pkt, s->init_buf)
735 || !get_construct_message_f(s, &pkt, &confunc, &mt)
736 || !ssl_set_handshake_header(s, &pkt, mt)
737 || (confunc != NULL && !confunc(s, &pkt))
738 || !ssl_close_construct_packet(s, &pkt, mt)
739 || !WPACKET_finish(&pkt)) {
740 WPACKET_cleanup(&pkt);
741 ossl_statem_set_error(s);
742 return SUB_STATE_ERROR;
747 case WRITE_STATE_SEND:
748 if (SSL_IS_DTLS(s) && st->use_timer) {
749 dtls1_start_timer(s);
751 ret = statem_do_write(s);
753 return SUB_STATE_ERROR;
755 st->write_state = WRITE_STATE_POST_WORK;
756 st->write_state_work = WORK_MORE_A;
759 case WRITE_STATE_POST_WORK:
760 switch (st->write_state_work = post_work(s, st->write_state_work)) {
765 return SUB_STATE_ERROR;
767 case WORK_FINISHED_CONTINUE:
768 st->write_state = WRITE_STATE_TRANSITION;
771 case WORK_FINISHED_STOP:
772 return SUB_STATE_END_HANDSHAKE;
777 return SUB_STATE_ERROR;
783 * Flush the write BIO
785 int statem_flush(SSL *s)
787 s->rwstate = SSL_WRITING;
788 if (BIO_flush(s->wbio) <= 0) {
791 s->rwstate = SSL_NOTHING;
797 * Called by the record layer to determine whether application data is
798 * allowed to be received in the current handshake state or not.
801 * 1: Yes (application data allowed)
802 * 0: No (application data not allowed)
804 int ossl_statem_app_data_allowed(SSL *s)
806 OSSL_STATEM *st = &s->statem;
808 if (st->state == MSG_FLOW_UNINITED)
811 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
816 * If we're a server and we haven't got as far as writing our
817 * ServerHello yet then we allow app data
819 if (st->hand_state == TLS_ST_BEFORE
820 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
824 * If we're a client and we haven't read the ServerHello yet then we
827 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
834 #ifndef OPENSSL_NO_SCTP
836 * Set flag used by SCTP to determine whether we are in the read sock state
838 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
840 s->statem.in_sctp_read_sock = read_sock;
844 * Called by the record layer to determine whether we are in the read sock
848 * 1: Yes (we are in the read sock state)
849 * 0: No (we are not in the read sock state)
851 int ossl_statem_in_sctp_read_sock(SSL *s)
853 return s->statem.in_sctp_read_sock;