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->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
330 && s->early_data_state != SSL_EARLY_DATA_FINISHED_READING)
332 if (!tls_setup_handshake(s)) {
333 ossl_statem_set_error(s);
337 if (SSL_IS_FIRST_HANDSHAKE(s))
338 st->read_state_first_init = 1;
341 st->state = MSG_FLOW_WRITING;
342 init_write_state_machine(s);
345 while (st->state != MSG_FLOW_FINISHED) {
346 if (st->state == MSG_FLOW_READING) {
347 ssret = read_state_machine(s);
348 if (ssret == SUB_STATE_FINISHED) {
349 st->state = MSG_FLOW_WRITING;
350 init_write_state_machine(s);
355 } else if (st->state == MSG_FLOW_WRITING) {
356 ssret = write_state_machine(s);
357 if (ssret == SUB_STATE_FINISHED) {
358 st->state = MSG_FLOW_READING;
359 init_read_state_machine(s);
360 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
361 st->state = MSG_FLOW_FINISHED;
368 ossl_statem_set_error(s);
378 #ifndef OPENSSL_NO_SCTP
379 if (SSL_IS_DTLS(s)) {
381 * Notify SCTP BIO socket to leave handshake mode and allow stream
382 * identifier other than 0. Will be ignored if no SCTP is used.
384 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
385 st->in_handshake, NULL);
392 cb(s, SSL_CB_ACCEPT_EXIT, ret);
394 cb(s, SSL_CB_CONNECT_EXIT, ret);
400 * Initialise the MSG_FLOW_READING sub-state machine
402 static void init_read_state_machine(SSL *s)
404 OSSL_STATEM *st = &s->statem;
406 st->read_state = READ_STATE_HEADER;
409 static int grow_init_buf(SSL *s, size_t size) {
411 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
413 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
416 if (size < msg_offset)
419 s->init_msg = s->init_buf->data + msg_offset;
425 * This function implements the sub-state machine when the message flow is in
426 * MSG_FLOW_READING. The valid sub-states and transitions are:
428 * READ_STATE_HEADER <--+<-------------+
431 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
433 * +----------------------------+
435 * [SUB_STATE_FINISHED]
437 * READ_STATE_HEADER has the responsibility for reading in the message header
438 * and transitioning the state of the handshake state machine.
440 * READ_STATE_BODY reads in the rest of the message and then subsequently
443 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
444 * processing activity performed on the message may block.
446 * Any of the above states could result in an NBIO event occurring in which case
447 * control returns to the calling application. When this function is recalled we
448 * will resume in the same state where we left off.
450 static SUB_STATE_RETURN read_state_machine(SSL *s)
452 OSSL_STATEM *st = &s->statem;
455 int (*transition) (SSL *s, int mt);
457 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
458 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
459 size_t (*max_message_size) (SSL *s);
460 void (*cb) (const SSL *ssl, int type, int val) = NULL;
462 cb = get_callback(s);
465 transition = ossl_statem_server_read_transition;
466 process_message = ossl_statem_server_process_message;
467 max_message_size = ossl_statem_server_max_message_size;
468 post_process_message = ossl_statem_server_post_process_message;
470 transition = ossl_statem_client_read_transition;
471 process_message = ossl_statem_client_process_message;
472 max_message_size = ossl_statem_client_max_message_size;
473 post_process_message = ossl_statem_client_post_process_message;
476 if (st->read_state_first_init) {
478 st->read_state_first_init = 0;
482 switch (st->read_state) {
483 case READ_STATE_HEADER:
484 /* Get the state the peer wants to move to */
485 if (SSL_IS_DTLS(s)) {
487 * In DTLS we get the whole message in one go - header and body
489 ret = dtls_get_message(s, &mt, &len);
491 ret = tls_get_message_header(s, &mt);
495 /* Could be non-blocking IO */
496 return SUB_STATE_ERROR;
500 /* Notify callback of an impending state change */
502 cb(s, SSL_CB_ACCEPT_LOOP, 1);
504 cb(s, SSL_CB_CONNECT_LOOP, 1);
507 * Validate that we are allowed to move to the new state and move
508 * to that state if so
510 if (!transition(s, mt)) {
511 ossl_statem_set_error(s);
512 return SUB_STATE_ERROR;
515 if (s->s3->tmp.message_size > max_message_size(s)) {
516 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
517 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
518 return SUB_STATE_ERROR;
521 /* dtls_get_message already did this */
523 && s->s3->tmp.message_size > 0
524 && !grow_init_buf(s, s->s3->tmp.message_size
525 + SSL3_HM_HEADER_LENGTH)) {
526 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
527 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
528 return SUB_STATE_ERROR;
531 st->read_state = READ_STATE_BODY;
534 case READ_STATE_BODY:
535 if (!SSL_IS_DTLS(s)) {
536 /* We already got this above for DTLS */
537 ret = tls_get_message_body(s, &len);
539 /* Could be non-blocking IO */
540 return SUB_STATE_ERROR;
545 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
546 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
547 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
548 return SUB_STATE_ERROR;
550 ret = process_message(s, &pkt);
552 /* Discard the packet data */
556 case MSG_PROCESS_ERROR:
557 return SUB_STATE_ERROR;
559 case MSG_PROCESS_FINISHED_READING:
560 if (SSL_IS_DTLS(s)) {
563 return SUB_STATE_FINISHED;
565 case MSG_PROCESS_CONTINUE_PROCESSING:
566 st->read_state = READ_STATE_POST_PROCESS;
567 st->read_state_work = WORK_MORE_A;
571 st->read_state = READ_STATE_HEADER;
576 case READ_STATE_POST_PROCESS:
577 st->read_state_work = post_process_message(s, st->read_state_work);
578 switch (st->read_state_work) {
583 return SUB_STATE_ERROR;
585 case WORK_FINISHED_CONTINUE:
586 st->read_state = READ_STATE_HEADER;
589 case WORK_FINISHED_STOP:
590 if (SSL_IS_DTLS(s)) {
593 return SUB_STATE_FINISHED;
598 /* Shouldn't happen */
599 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
600 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
601 ossl_statem_set_error(s);
602 return SUB_STATE_ERROR;
608 * Send a previously constructed message to the peer.
610 static int statem_do_write(SSL *s)
612 OSSL_STATEM *st = &s->statem;
614 if (st->hand_state == TLS_ST_CW_CHANGE
615 || st->hand_state == TLS_ST_SW_CHANGE) {
617 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
619 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
621 return ssl_do_write(s);
626 * Initialise the MSG_FLOW_WRITING sub-state machine
628 static void init_write_state_machine(SSL *s)
630 OSSL_STATEM *st = &s->statem;
632 st->write_state = WRITE_STATE_TRANSITION;
636 * This function implements the sub-state machine when the message flow is in
637 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
639 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
642 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
648 * | WRITE_STATE_POST_WORK
652 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
654 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
655 * sending of the message. This could result in an NBIO event occurring in
656 * which case control returns to the calling application. When this function
657 * is recalled we will resume in the same state where we left off.
659 * WRITE_STATE_SEND sends the message and performs any work to be done after
662 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
663 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
664 * result in an NBIO event.
666 static SUB_STATE_RETURN write_state_machine(SSL *s)
668 OSSL_STATEM *st = &s->statem;
670 WRITE_TRAN(*transition) (SSL *s);
671 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
672 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
673 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
674 int (**confunc) (SSL *s, WPACKET *pkt),
676 void (*cb) (const SSL *ssl, int type, int val) = NULL;
677 int (*confunc) (SSL *s, WPACKET *pkt);
681 cb = get_callback(s);
684 transition = ossl_statem_server_write_transition;
685 pre_work = ossl_statem_server_pre_work;
686 post_work = ossl_statem_server_post_work;
687 get_construct_message_f = ossl_statem_server_construct_message;
689 transition = ossl_statem_client_write_transition;
690 pre_work = ossl_statem_client_pre_work;
691 post_work = ossl_statem_client_post_work;
692 get_construct_message_f = ossl_statem_client_construct_message;
696 switch (st->write_state) {
697 case WRITE_STATE_TRANSITION:
699 /* Notify callback of an impending state change */
701 cb(s, SSL_CB_ACCEPT_LOOP, 1);
703 cb(s, SSL_CB_CONNECT_LOOP, 1);
705 switch (transition(s)) {
706 case WRITE_TRAN_CONTINUE:
707 st->write_state = WRITE_STATE_PRE_WORK;
708 st->write_state_work = WORK_MORE_A;
711 case WRITE_TRAN_FINISHED:
712 return SUB_STATE_FINISHED;
715 case WRITE_TRAN_ERROR:
716 return SUB_STATE_ERROR;
720 case WRITE_STATE_PRE_WORK:
721 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
726 return SUB_STATE_ERROR;
728 case WORK_FINISHED_CONTINUE:
729 st->write_state = WRITE_STATE_SEND;
732 case WORK_FINISHED_STOP:
733 return SUB_STATE_END_HANDSHAKE;
735 if (!WPACKET_init(&pkt, s->init_buf)
736 || !get_construct_message_f(s, &pkt, &confunc, &mt)
737 || !ssl_set_handshake_header(s, &pkt, mt)
738 || (confunc != NULL && !confunc(s, &pkt))
739 || !ssl_close_construct_packet(s, &pkt, mt)
740 || !WPACKET_finish(&pkt)) {
741 WPACKET_cleanup(&pkt);
742 ossl_statem_set_error(s);
743 return SUB_STATE_ERROR;
748 case WRITE_STATE_SEND:
749 if (SSL_IS_DTLS(s) && st->use_timer) {
750 dtls1_start_timer(s);
752 ret = statem_do_write(s);
754 return SUB_STATE_ERROR;
756 st->write_state = WRITE_STATE_POST_WORK;
757 st->write_state_work = WORK_MORE_A;
760 case WRITE_STATE_POST_WORK:
761 switch (st->write_state_work = post_work(s, st->write_state_work)) {
766 return SUB_STATE_ERROR;
768 case WORK_FINISHED_CONTINUE:
769 st->write_state = WRITE_STATE_TRANSITION;
772 case WORK_FINISHED_STOP:
773 return SUB_STATE_END_HANDSHAKE;
778 return SUB_STATE_ERROR;
784 * Flush the write BIO
786 int statem_flush(SSL *s)
788 s->rwstate = SSL_WRITING;
789 if (BIO_flush(s->wbio) <= 0) {
792 s->rwstate = SSL_NOTHING;
798 * Called by the record layer to determine whether application data is
799 * allowed to be received in the current handshake state or not.
802 * 1: Yes (application data allowed)
803 * 0: No (application data not allowed)
805 int ossl_statem_app_data_allowed(SSL *s)
807 OSSL_STATEM *st = &s->statem;
809 if (st->state == MSG_FLOW_UNINITED)
812 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
817 * If we're a server and we haven't got as far as writing our
818 * ServerHello yet then we allow app data
820 if (st->hand_state == TLS_ST_BEFORE
821 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
825 * If we're a client and we haven't read the ServerHello yet then we
828 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
835 #ifndef OPENSSL_NO_SCTP
837 * Set flag used by SCTP to determine whether we are in the read sock state
839 void ossl_statem_set_sctp_read_sock(SSL *s, int read_sock)
841 s->statem.in_sctp_read_sock = read_sock;
845 * Called by the record layer to determine whether we are in the read sock
849 * 1: Yes (we are in the read sock state)
850 * 0: No (we are not in the read sock state)
852 int ossl_statem_in_sctp_read_sock(SSL *s)
854 return s->statem.in_sctp_read_sock;