Home | History | Annotate | Line # | Download | only in statem
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  *
      4  1.1  christos  * Licensed under the OpenSSL license (the "License").  You may not use
      5  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos  * in the file LICENSE in the source distribution or at
      7  1.1  christos  * https://www.openssl.org/source/license.html
      8  1.1  christos  */
      9  1.1  christos 
     10  1.1  christos #include "internal/cryptlib.h"
     11  1.1  christos #include <openssl/rand.h>
     12  1.1  christos #include "../ssl_local.h"
     13  1.1  christos #include "statem_local.h"
     14  1.1  christos #include <assert.h>
     15  1.1  christos 
     16  1.1  christos /*
     17  1.1  christos  * This file implements the SSL/TLS/DTLS state machines.
     18  1.1  christos  *
     19  1.1  christos  * There are two primary state machines:
     20  1.1  christos  *
     21  1.1  christos  * 1) Message flow state machine
     22  1.1  christos  * 2) Handshake state machine
     23  1.1  christos  *
     24  1.1  christos  * The Message flow state machine controls the reading and sending of messages
     25  1.1  christos  * including handling of non-blocking IO events, flushing of the underlying
     26  1.1  christos  * write BIO, handling unexpected messages, etc. It is itself broken into two
     27  1.1  christos  * separate sub-state machines which control reading and writing respectively.
     28  1.1  christos  *
     29  1.1  christos  * The Handshake state machine keeps track of the current SSL/TLS handshake
     30  1.1  christos  * state. Transitions of the handshake state are the result of events that
     31  1.1  christos  * occur within the Message flow state machine.
     32  1.1  christos  *
     33  1.1  christos  * Overall it looks like this:
     34  1.1  christos  *
     35  1.1  christos  * ---------------------------------------------            -------------------
     36  1.1  christos  * |                                           |            |                 |
     37  1.1  christos  * | Message flow state machine                |            |                 |
     38  1.1  christos  * |                                           |            |                 |
     39  1.1  christos  * | -------------------- -------------------- | Transition | Handshake state |
     40  1.1  christos  * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event      | machine         |
     41  1.1  christos  * | | sub-state        | | sub-state        | |----------->|                 |
     42  1.1  christos  * | | machine for      | | machine for      | |            |                 |
     43  1.1  christos  * | | reading messages | | writing messages | |            |                 |
     44  1.1  christos  * | -------------------- -------------------- |            |                 |
     45  1.1  christos  * |                                           |            |                 |
     46  1.1  christos  * ---------------------------------------------            -------------------
     47  1.1  christos  *
     48  1.1  christos  */
     49  1.1  christos 
     50  1.1  christos /* Sub state machine return values */
     51  1.1  christos typedef enum {
     52  1.1  christos     /* Something bad happened or NBIO */
     53  1.1  christos     SUB_STATE_ERROR,
     54  1.1  christos     /* Sub state finished go to the next sub state */
     55  1.1  christos     SUB_STATE_FINISHED,
     56  1.1  christos     /* Sub state finished and handshake was completed */
     57  1.1  christos     SUB_STATE_END_HANDSHAKE
     58  1.1  christos } SUB_STATE_RETURN;
     59  1.1  christos 
     60  1.1  christos static int state_machine(SSL *s, int server);
     61  1.1  christos static void init_read_state_machine(SSL *s);
     62  1.1  christos static SUB_STATE_RETURN read_state_machine(SSL *s);
     63  1.1  christos static void init_write_state_machine(SSL *s);
     64  1.1  christos static SUB_STATE_RETURN write_state_machine(SSL *s);
     65  1.1  christos 
     66  1.1  christos OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
     67  1.1  christos {
     68  1.1  christos     return ssl->statem.hand_state;
     69  1.1  christos }
     70  1.1  christos 
     71  1.1  christos int SSL_in_init(const SSL *s)
     72  1.1  christos {
     73  1.1  christos     return s->statem.in_init;
     74  1.1  christos }
     75  1.1  christos 
     76  1.1  christos int SSL_is_init_finished(const SSL *s)
     77  1.1  christos {
     78  1.1  christos     return !(s->statem.in_init) && (s->statem.hand_state == TLS_ST_OK);
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos int SSL_in_before(const SSL *s)
     82  1.1  christos {
     83  1.1  christos     /*
     84  1.1  christos      * Historically being "in before" meant before anything had happened. In the
     85  1.1  christos      * current code though we remain in the "before" state for a while after we
     86  1.1  christos      * have started the handshake process (e.g. as a server waiting for the
     87  1.1  christos      * first message to arrive). There "in before" is taken to mean "in before"
     88  1.1  christos      * and not started any handshake process yet.
     89  1.1  christos      */
     90  1.1  christos     return (s->statem.hand_state == TLS_ST_BEFORE)
     91  1.1  christos         && (s->statem.state == MSG_FLOW_UNINITED);
     92  1.1  christos }
     93  1.1  christos 
     94  1.1  christos /*
     95  1.1  christos  * Clear the state machine state and reset back to MSG_FLOW_UNINITED
     96  1.1  christos  */
     97  1.1  christos void ossl_statem_clear(SSL *s)
     98  1.1  christos {
     99  1.1  christos     s->statem.state = MSG_FLOW_UNINITED;
    100  1.1  christos     s->statem.hand_state = TLS_ST_BEFORE;
    101  1.1  christos     s->statem.in_init = 1;
    102  1.1  christos     s->statem.no_cert_verify = 0;
    103  1.1  christos }
    104  1.1  christos 
    105  1.1  christos /*
    106  1.1  christos  * Set the state machine up ready for a renegotiation handshake
    107  1.1  christos  */
    108  1.1  christos void ossl_statem_set_renegotiate(SSL *s)
    109  1.1  christos {
    110  1.1  christos     s->statem.in_init = 1;
    111  1.1  christos     s->statem.request_state = TLS_ST_SW_HELLO_REQ;
    112  1.1  christos }
    113  1.1  christos 
    114  1.1  christos /*
    115  1.1  christos  * Put the state machine into an error state and send an alert if appropriate.
    116  1.1  christos  * This is a permanent error for the current connection.
    117  1.1  christos  */
    118  1.1  christos void ossl_statem_fatal(SSL *s, int al, int func, int reason, const char *file,
    119  1.1  christos                        int line)
    120  1.1  christos {
    121  1.1  christos     ERR_put_error(ERR_LIB_SSL, func, reason, file, line);
    122  1.1  christos     /* We shouldn't call SSLfatal() twice. Once is enough */
    123  1.1  christos     if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR)
    124  1.1  christos       return;
    125  1.1  christos     s->statem.in_init = 1;
    126  1.1  christos     s->statem.state = MSG_FLOW_ERROR;
    127  1.1  christos     if (al != SSL_AD_NO_ALERT
    128  1.1  christos             && s->statem.enc_write_state != ENC_WRITE_STATE_INVALID)
    129  1.1  christos         ssl3_send_alert(s, SSL3_AL_FATAL, al);
    130  1.1  christos }
    131  1.1  christos 
    132  1.1  christos /*
    133  1.1  christos  * This macro should only be called if we are already expecting to be in
    134  1.1  christos  * a fatal error state. We verify that we are, and set it if not (this would
    135  1.1  christos  * indicate a bug).
    136  1.1  christos  */
    137  1.1  christos #define check_fatal(s, f) \
    138  1.1  christos     do { \
    139  1.1  christos         if (!ossl_assert((s)->statem.in_init \
    140  1.1  christos                          && (s)->statem.state == MSG_FLOW_ERROR)) \
    141  1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, (f), \
    142  1.1  christos                      SSL_R_MISSING_FATAL); \
    143  1.1  christos     } while (0)
    144  1.1  christos 
    145  1.1  christos /*
    146  1.1  christos  * Discover whether the current connection is in the error state.
    147  1.1  christos  *
    148  1.1  christos  * Valid return values are:
    149  1.1  christos  *   1: Yes
    150  1.1  christos  *   0: No
    151  1.1  christos  */
    152  1.1  christos int ossl_statem_in_error(const SSL *s)
    153  1.1  christos {
    154  1.1  christos     if (s->statem.state == MSG_FLOW_ERROR)
    155  1.1  christos         return 1;
    156  1.1  christos 
    157  1.1  christos     return 0;
    158  1.1  christos }
    159  1.1  christos 
    160  1.1  christos void ossl_statem_set_in_init(SSL *s, int init)
    161  1.1  christos {
    162  1.1  christos     s->statem.in_init = init;
    163  1.1  christos }
    164  1.1  christos 
    165  1.1  christos int ossl_statem_get_in_handshake(SSL *s)
    166  1.1  christos {
    167  1.1  christos     return s->statem.in_handshake;
    168  1.1  christos }
    169  1.1  christos 
    170  1.1  christos void ossl_statem_set_in_handshake(SSL *s, int inhand)
    171  1.1  christos {
    172  1.1  christos     if (inhand)
    173  1.1  christos         s->statem.in_handshake++;
    174  1.1  christos     else
    175  1.1  christos         s->statem.in_handshake--;
    176  1.1  christos }
    177  1.1  christos 
    178  1.1  christos /* Are we in a sensible state to skip over unreadable early data? */
    179  1.1  christos int ossl_statem_skip_early_data(SSL *s)
    180  1.1  christos {
    181  1.1  christos     if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
    182  1.1  christos         return 0;
    183  1.1  christos 
    184  1.1  christos     if (!s->server
    185  1.1  christos             || s->statem.hand_state != TLS_ST_EARLY_DATA
    186  1.1  christos             || s->hello_retry_request == SSL_HRR_COMPLETE)
    187  1.1  christos         return 0;
    188  1.1  christos 
    189  1.1  christos     return 1;
    190  1.1  christos }
    191  1.1  christos 
    192  1.1  christos /*
    193  1.1  christos  * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
    194  1.1  christos  * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
    195  1.1  christos  * data state and whether we should attempt to move the handshake on if so.
    196  1.1  christos  * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
    197  1.1  christos  * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
    198  1.1  christos  * or similar.
    199  1.1  christos  */
    200  1.1  christos void ossl_statem_check_finish_init(SSL *s, int sending)
    201  1.1  christos {
    202  1.1  christos     if (sending == -1) {
    203  1.1  christos         if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
    204  1.1  christos                 || s->statem.hand_state == TLS_ST_EARLY_DATA) {
    205  1.1  christos             ossl_statem_set_in_init(s, 1);
    206  1.1  christos             if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
    207  1.1  christos                 /*
    208  1.1  christos                  * SSL_connect() or SSL_do_handshake() has been called directly.
    209  1.1  christos                  * We don't allow any more writing of early data.
    210  1.1  christos                  */
    211  1.1  christos                 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
    212  1.1  christos             }
    213  1.1  christos         }
    214  1.1  christos     } else if (!s->server) {
    215  1.1  christos         if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
    216  1.1  christos                       || s->statem.hand_state == TLS_ST_EARLY_DATA)
    217  1.1  christos                   && s->early_data_state != SSL_EARLY_DATA_WRITING)
    218  1.1  christos                 || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) {
    219  1.1  christos             ossl_statem_set_in_init(s, 1);
    220  1.1  christos             /*
    221  1.1  christos              * SSL_write() has been called directly. We don't allow any more
    222  1.1  christos              * writing of early data.
    223  1.1  christos              */
    224  1.1  christos             if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY)
    225  1.1  christos                 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
    226  1.1  christos         }
    227  1.1  christos     } else {
    228  1.1  christos         if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
    229  1.1  christos                 && s->statem.hand_state == TLS_ST_EARLY_DATA)
    230  1.1  christos             ossl_statem_set_in_init(s, 1);
    231  1.1  christos     }
    232  1.1  christos }
    233  1.1  christos 
    234  1.1  christos void ossl_statem_set_hello_verify_done(SSL *s)
    235  1.1  christos {
    236  1.1  christos     s->statem.state = MSG_FLOW_UNINITED;
    237  1.1  christos     s->statem.in_init = 1;
    238  1.1  christos     /*
    239  1.1  christos      * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
    240  1.1  christos      * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
    241  1.1  christos      * calls to SSL_in_before() will return false. Also calls to
    242  1.1  christos      * SSL_state_string() and SSL_state_string_long() will return something
    243  1.1  christos      * sensible.
    244  1.1  christos      */
    245  1.1  christos     s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
    246  1.1  christos }
    247  1.1  christos 
    248  1.1  christos int ossl_statem_connect(SSL *s)
    249  1.1  christos {
    250  1.1  christos     return state_machine(s, 0);
    251  1.1  christos }
    252  1.1  christos 
    253  1.1  christos int ossl_statem_accept(SSL *s)
    254  1.1  christos {
    255  1.1  christos     return state_machine(s, 1);
    256  1.1  christos }
    257  1.1  christos 
    258  1.1  christos typedef void (*info_cb) (const SSL *, int, int);
    259  1.1  christos 
    260  1.1  christos static info_cb get_callback(SSL *s)
    261  1.1  christos {
    262  1.1  christos     if (s->info_callback != NULL)
    263  1.1  christos         return s->info_callback;
    264  1.1  christos     else if (s->ctx->info_callback != NULL)
    265  1.1  christos         return s->ctx->info_callback;
    266  1.1  christos 
    267  1.1  christos     return NULL;
    268  1.1  christos }
    269  1.1  christos 
    270  1.1  christos /*
    271  1.1  christos  * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
    272  1.1  christos  * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
    273  1.1  christos  * transitions are as follows:
    274  1.1  christos  *
    275  1.1  christos  * MSG_FLOW_UNINITED     MSG_FLOW_FINISHED
    276  1.1  christos  *        |                       |
    277  1.1  christos  *        +-----------------------+
    278  1.1  christos  *        v
    279  1.1  christos  * MSG_FLOW_WRITING <---> MSG_FLOW_READING
    280  1.1  christos  *        |
    281  1.1  christos  *        V
    282  1.1  christos  * MSG_FLOW_FINISHED
    283  1.1  christos  *        |
    284  1.1  christos  *        V
    285  1.1  christos  *    [SUCCESS]
    286  1.1  christos  *
    287  1.1  christos  * We may exit at any point due to an error or NBIO event. If an NBIO event
    288  1.1  christos  * occurs then we restart at the point we left off when we are recalled.
    289  1.1  christos  * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
    290  1.1  christos  *
    291  1.1  christos  * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
    292  1.1  christos  * into that state at any point in the event that an irrecoverable error occurs.
    293  1.1  christos  *
    294  1.1  christos  * Valid return values are:
    295  1.1  christos  *   1: Success
    296  1.1  christos  * <=0: NBIO or error
    297  1.1  christos  */
    298  1.1  christos static int state_machine(SSL *s, int server)
    299  1.1  christos {
    300  1.1  christos     BUF_MEM *buf = NULL;
    301  1.1  christos     void (*cb) (const SSL *ssl, int type, int val) = NULL;
    302  1.1  christos     OSSL_STATEM *st = &s->statem;
    303  1.1  christos     int ret = -1;
    304  1.1  christos     int ssret;
    305  1.1  christos 
    306  1.1  christos     if (st->state == MSG_FLOW_ERROR) {
    307  1.1  christos         /* Shouldn't have been called if we're already in the error state */
    308  1.1  christos         return -1;
    309  1.1  christos     }
    310  1.1  christos 
    311  1.1  christos     ERR_clear_error();
    312  1.1  christos     clear_sys_error();
    313  1.1  christos 
    314  1.1  christos     cb = get_callback(s);
    315  1.1  christos 
    316  1.1  christos     st->in_handshake++;
    317  1.1  christos     if (!SSL_in_init(s) || SSL_in_before(s)) {
    318  1.1  christos         /*
    319  1.1  christos          * If we are stateless then we already called SSL_clear() - don't do
    320  1.1  christos          * it again and clear the STATELESS flag itself.
    321  1.1  christos          */
    322  1.1  christos         if ((s->s3->flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(s))
    323  1.1  christos             return -1;
    324  1.1  christos     }
    325  1.1  christos #ifndef OPENSSL_NO_SCTP
    326  1.1  christos     if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
    327  1.1  christos         /*
    328  1.1  christos          * Notify SCTP BIO socket to enter handshake mode and prevent stream
    329  1.1  christos          * identifier other than 0.
    330  1.1  christos          */
    331  1.1  christos         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
    332  1.1  christos                  st->in_handshake, NULL);
    333  1.1  christos     }
    334  1.1  christos #endif
    335  1.1  christos 
    336  1.1  christos     /* Initialise state machine */
    337  1.1  christos     if (st->state == MSG_FLOW_UNINITED
    338  1.1  christos             || st->state == MSG_FLOW_FINISHED) {
    339  1.1  christos         if (st->state == MSG_FLOW_UNINITED) {
    340  1.1  christos             st->hand_state = TLS_ST_BEFORE;
    341  1.1  christos             st->request_state = TLS_ST_BEFORE;
    342  1.1  christos         }
    343  1.1  christos 
    344  1.1  christos         s->server = server;
    345  1.1  christos         if (cb != NULL) {
    346  1.1  christos             if (SSL_IS_FIRST_HANDSHAKE(s) || !SSL_IS_TLS13(s))
    347  1.1  christos                 cb(s, SSL_CB_HANDSHAKE_START, 1);
    348  1.1  christos         }
    349  1.1  christos 
    350  1.1  christos         /*
    351  1.1  christos          * Fatal errors in this block don't send an alert because we have
    352  1.1  christos          * failed to even initialise properly. Sending an alert is probably
    353  1.1  christos          * doomed to failure.
    354  1.1  christos          */
    355  1.1  christos 
    356  1.1  christos         if (SSL_IS_DTLS(s)) {
    357  1.1  christos             if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
    358  1.1  christos                 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
    359  1.1  christos                 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
    360  1.1  christos                          ERR_R_INTERNAL_ERROR);
    361  1.1  christos                 goto end;
    362  1.1  christos             }
    363  1.1  christos         } else {
    364  1.1  christos             if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
    365  1.1  christos                 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
    366  1.1  christos                          ERR_R_INTERNAL_ERROR);
    367  1.1  christos                 goto end;
    368  1.1  christos             }
    369  1.1  christos         }
    370  1.1  christos 
    371  1.1  christos         if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
    372  1.1  christos             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
    373  1.1  christos                      ERR_R_INTERNAL_ERROR);
    374  1.1  christos             goto end;
    375  1.1  christos         }
    376  1.1  christos 
    377  1.1  christos         if (s->init_buf == NULL) {
    378  1.1  christos             if ((buf = BUF_MEM_new()) == NULL) {
    379  1.1  christos                 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
    380  1.1  christos                          ERR_R_INTERNAL_ERROR);
    381  1.1  christos                 goto end;
    382  1.1  christos             }
    383  1.1  christos             if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
    384  1.1  christos                 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
    385  1.1  christos                          ERR_R_INTERNAL_ERROR);
    386  1.1  christos                 goto end;
    387  1.1  christos             }
    388  1.1  christos             s->init_buf = buf;
    389  1.1  christos             buf = NULL;
    390  1.1  christos         }
    391  1.1  christos 
    392  1.1  christos         if (!ssl3_setup_buffers(s)) {
    393  1.1  christos             SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
    394  1.1  christos                      ERR_R_INTERNAL_ERROR);
    395  1.1  christos             goto end;
    396  1.1  christos         }
    397  1.1  christos         s->init_num = 0;
    398  1.1  christos 
    399  1.1  christos         /*
    400  1.1  christos          * Should have been reset by tls_process_finished, too.
    401  1.1  christos          */
    402  1.1  christos         s->s3->change_cipher_spec = 0;
    403  1.1  christos 
    404  1.1  christos         /*
    405  1.1  christos          * Ok, we now need to push on a buffering BIO ...but not with
    406  1.1  christos          * SCTP
    407  1.1  christos          */
    408  1.1  christos #ifndef OPENSSL_NO_SCTP
    409  1.1  christos         if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
    410  1.1  christos #endif
    411  1.1  christos             if (!ssl_init_wbio_buffer(s)) {
    412  1.1  christos                 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_STATE_MACHINE,
    413  1.1  christos                          ERR_R_INTERNAL_ERROR);
    414  1.1  christos                 goto end;
    415  1.1  christos             }
    416  1.1  christos 
    417  1.1  christos         if ((SSL_in_before(s))
    418  1.1  christos                 || s->renegotiate) {
    419  1.1  christos             if (!tls_setup_handshake(s)) {
    420  1.1  christos                 /* SSLfatal() already called */
    421  1.1  christos                 goto end;
    422  1.1  christos             }
    423  1.1  christos 
    424  1.1  christos             if (SSL_IS_FIRST_HANDSHAKE(s))
    425  1.1  christos                 st->read_state_first_init = 1;
    426  1.1  christos         }
    427  1.1  christos 
    428  1.1  christos         st->state = MSG_FLOW_WRITING;
    429  1.1  christos         init_write_state_machine(s);
    430  1.1  christos     }
    431  1.1  christos 
    432  1.1  christos     while (st->state != MSG_FLOW_FINISHED) {
    433  1.1  christos         if (st->state == MSG_FLOW_READING) {
    434  1.1  christos             ssret = read_state_machine(s);
    435  1.1  christos             if (ssret == SUB_STATE_FINISHED) {
    436  1.1  christos                 st->state = MSG_FLOW_WRITING;
    437  1.1  christos                 init_write_state_machine(s);
    438  1.1  christos             } else {
    439  1.1  christos                 /* NBIO or error */
    440  1.1  christos                 goto end;
    441  1.1  christos             }
    442  1.1  christos         } else if (st->state == MSG_FLOW_WRITING) {
    443  1.1  christos             ssret = write_state_machine(s);
    444  1.1  christos             if (ssret == SUB_STATE_FINISHED) {
    445  1.1  christos                 st->state = MSG_FLOW_READING;
    446  1.1  christos                 init_read_state_machine(s);
    447  1.1  christos             } else if (ssret == SUB_STATE_END_HANDSHAKE) {
    448  1.1  christos                 st->state = MSG_FLOW_FINISHED;
    449  1.1  christos             } else {
    450  1.1  christos                 /* NBIO or error */
    451  1.1  christos                 goto end;
    452  1.1  christos             }
    453  1.1  christos         } else {
    454  1.1  christos             /* Error */
    455  1.1  christos             check_fatal(s, SSL_F_STATE_MACHINE);
    456  1.1  christos             SSLerr(SSL_F_STATE_MACHINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    457  1.1  christos             goto end;
    458  1.1  christos         }
    459  1.1  christos     }
    460  1.1  christos 
    461  1.1  christos     ret = 1;
    462  1.1  christos 
    463  1.1  christos  end:
    464  1.1  christos     st->in_handshake--;
    465  1.1  christos 
    466  1.1  christos #ifndef OPENSSL_NO_SCTP
    467  1.1  christos     if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
    468  1.1  christos         /*
    469  1.1  christos          * Notify SCTP BIO socket to leave handshake mode and allow stream
    470  1.1  christos          * identifier other than 0.
    471  1.1  christos          */
    472  1.1  christos         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
    473  1.1  christos                  st->in_handshake, NULL);
    474  1.1  christos     }
    475  1.1  christos #endif
    476  1.1  christos 
    477  1.1  christos     BUF_MEM_free(buf);
    478  1.1  christos     if (cb != NULL) {
    479  1.1  christos         if (server)
    480  1.1  christos             cb(s, SSL_CB_ACCEPT_EXIT, ret);
    481  1.1  christos         else
    482  1.1  christos             cb(s, SSL_CB_CONNECT_EXIT, ret);
    483  1.1  christos     }
    484  1.1  christos     return ret;
    485  1.1  christos }
    486  1.1  christos 
    487  1.1  christos /*
    488  1.1  christos  * Initialise the MSG_FLOW_READING sub-state machine
    489  1.1  christos  */
    490  1.1  christos static void init_read_state_machine(SSL *s)
    491  1.1  christos {
    492  1.1  christos     OSSL_STATEM *st = &s->statem;
    493  1.1  christos 
    494  1.1  christos     st->read_state = READ_STATE_HEADER;
    495  1.1  christos }
    496  1.1  christos 
    497  1.1  christos static int grow_init_buf(SSL *s, size_t size) {
    498  1.1  christos 
    499  1.1  christos     size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
    500  1.1  christos 
    501  1.1  christos     if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
    502  1.1  christos         return 0;
    503  1.1  christos 
    504  1.1  christos     if (size < msg_offset)
    505  1.1  christos         return 0;
    506  1.1  christos 
    507  1.1  christos     s->init_msg = s->init_buf->data + msg_offset;
    508  1.1  christos 
    509  1.1  christos     return 1;
    510  1.1  christos }
    511  1.1  christos 
    512  1.1  christos /*
    513  1.1  christos  * This function implements the sub-state machine when the message flow is in
    514  1.1  christos  * MSG_FLOW_READING. The valid sub-states and transitions are:
    515  1.1  christos  *
    516  1.1  christos  * READ_STATE_HEADER <--+<-------------+
    517  1.1  christos  *        |             |              |
    518  1.1  christos  *        v             |              |
    519  1.1  christos  * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
    520  1.1  christos  *        |                            |
    521  1.1  christos  *        +----------------------------+
    522  1.1  christos  *        v
    523  1.1  christos  * [SUB_STATE_FINISHED]
    524  1.1  christos  *
    525  1.1  christos  * READ_STATE_HEADER has the responsibility for reading in the message header
    526  1.1  christos  * and transitioning the state of the handshake state machine.
    527  1.1  christos  *
    528  1.1  christos  * READ_STATE_BODY reads in the rest of the message and then subsequently
    529  1.1  christos  * processes it.
    530  1.1  christos  *
    531  1.1  christos  * READ_STATE_POST_PROCESS is an optional step that may occur if some post
    532  1.1  christos  * processing activity performed on the message may block.
    533  1.1  christos  *
    534  1.1  christos  * Any of the above states could result in an NBIO event occurring in which case
    535  1.1  christos  * control returns to the calling application. When this function is recalled we
    536  1.1  christos  * will resume in the same state where we left off.
    537  1.1  christos  */
    538  1.1  christos static SUB_STATE_RETURN read_state_machine(SSL *s)
    539  1.1  christos {
    540  1.1  christos     OSSL_STATEM *st = &s->statem;
    541  1.1  christos     int ret, mt;
    542  1.1  christos     size_t len = 0;
    543  1.1  christos     int (*transition) (SSL *s, int mt);
    544  1.1  christos     PACKET pkt;
    545  1.1  christos     MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
    546  1.1  christos     WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
    547  1.1  christos     size_t (*max_message_size) (SSL *s);
    548  1.1  christos     void (*cb) (const SSL *ssl, int type, int val) = NULL;
    549  1.1  christos 
    550  1.1  christos     cb = get_callback(s);
    551  1.1  christos 
    552  1.1  christos     if (s->server) {
    553  1.1  christos         transition = ossl_statem_server_read_transition;
    554  1.1  christos         process_message = ossl_statem_server_process_message;
    555  1.1  christos         max_message_size = ossl_statem_server_max_message_size;
    556  1.1  christos         post_process_message = ossl_statem_server_post_process_message;
    557  1.1  christos     } else {
    558  1.1  christos         transition = ossl_statem_client_read_transition;
    559  1.1  christos         process_message = ossl_statem_client_process_message;
    560  1.1  christos         max_message_size = ossl_statem_client_max_message_size;
    561  1.1  christos         post_process_message = ossl_statem_client_post_process_message;
    562  1.1  christos     }
    563  1.1  christos 
    564  1.1  christos     if (st->read_state_first_init) {
    565  1.1  christos         s->first_packet = 1;
    566  1.1  christos         st->read_state_first_init = 0;
    567  1.1  christos     }
    568  1.1  christos 
    569  1.1  christos     while (1) {
    570  1.1  christos         switch (st->read_state) {
    571  1.1  christos         case READ_STATE_HEADER:
    572  1.1  christos             /* Get the state the peer wants to move to */
    573  1.1  christos             if (SSL_IS_DTLS(s)) {
    574  1.1  christos                 /*
    575  1.1  christos                  * In DTLS we get the whole message in one go - header and body
    576  1.1  christos                  */
    577  1.1  christos                 ret = dtls_get_message(s, &mt, &len);
    578  1.1  christos             } else {
    579  1.1  christos                 ret = tls_get_message_header(s, &mt);
    580  1.1  christos             }
    581  1.1  christos 
    582  1.1  christos             if (ret == 0) {
    583  1.1  christos                 /* Could be non-blocking IO */
    584  1.1  christos                 return SUB_STATE_ERROR;
    585  1.1  christos             }
    586  1.1  christos 
    587  1.1  christos             if (cb != NULL) {
    588  1.1  christos                 /* Notify callback of an impending state change */
    589  1.1  christos                 if (s->server)
    590  1.1  christos                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
    591  1.1  christos                 else
    592  1.1  christos                     cb(s, SSL_CB_CONNECT_LOOP, 1);
    593  1.1  christos             }
    594  1.1  christos             /*
    595  1.1  christos              * Validate that we are allowed to move to the new state and move
    596  1.1  christos              * to that state if so
    597  1.1  christos              */
    598  1.1  christos             if (!transition(s, mt))
    599  1.1  christos                 return SUB_STATE_ERROR;
    600  1.1  christos 
    601  1.1  christos             if (s->s3->tmp.message_size > max_message_size(s)) {
    602  1.1  christos                 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_READ_STATE_MACHINE,
    603  1.1  christos                          SSL_R_EXCESSIVE_MESSAGE_SIZE);
    604  1.1  christos                 return SUB_STATE_ERROR;
    605  1.1  christos             }
    606  1.1  christos 
    607  1.1  christos             /* dtls_get_message already did this */
    608  1.1  christos             if (!SSL_IS_DTLS(s)
    609  1.1  christos                     && s->s3->tmp.message_size > 0
    610  1.1  christos                     && !grow_init_buf(s, s->s3->tmp.message_size
    611  1.1  christos                                          + SSL3_HM_HEADER_LENGTH)) {
    612  1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_READ_STATE_MACHINE,
    613  1.1  christos                          ERR_R_BUF_LIB);
    614  1.1  christos                 return SUB_STATE_ERROR;
    615  1.1  christos             }
    616  1.1  christos 
    617  1.1  christos             st->read_state = READ_STATE_BODY;
    618  1.1  christos             /* Fall through */
    619  1.1  christos 
    620  1.1  christos         case READ_STATE_BODY:
    621  1.1  christos             if (!SSL_IS_DTLS(s)) {
    622  1.1  christos                 /* We already got this above for DTLS */
    623  1.1  christos                 ret = tls_get_message_body(s, &len);
    624  1.1  christos                 if (ret == 0) {
    625  1.1  christos                     /* Could be non-blocking IO */
    626  1.1  christos                     return SUB_STATE_ERROR;
    627  1.1  christos                 }
    628  1.1  christos             }
    629  1.1  christos 
    630  1.1  christos             s->first_packet = 0;
    631  1.1  christos             if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
    632  1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_READ_STATE_MACHINE,
    633  1.1  christos                          ERR_R_INTERNAL_ERROR);
    634  1.1  christos                 return SUB_STATE_ERROR;
    635  1.1  christos             }
    636  1.1  christos             ret = process_message(s, &pkt);
    637  1.1  christos 
    638  1.1  christos             /* Discard the packet data */
    639  1.1  christos             s->init_num = 0;
    640  1.1  christos 
    641  1.1  christos             switch (ret) {
    642  1.1  christos             case MSG_PROCESS_ERROR:
    643  1.1  christos                 check_fatal(s, SSL_F_READ_STATE_MACHINE);
    644  1.1  christos                 return SUB_STATE_ERROR;
    645  1.1  christos 
    646  1.1  christos             case MSG_PROCESS_FINISHED_READING:
    647  1.1  christos                 if (SSL_IS_DTLS(s)) {
    648  1.1  christos                     dtls1_stop_timer(s);
    649  1.1  christos                 }
    650  1.1  christos                 return SUB_STATE_FINISHED;
    651  1.1  christos 
    652  1.1  christos             case MSG_PROCESS_CONTINUE_PROCESSING:
    653  1.1  christos                 st->read_state = READ_STATE_POST_PROCESS;
    654  1.1  christos                 st->read_state_work = WORK_MORE_A;
    655  1.1  christos                 break;
    656  1.1  christos 
    657  1.1  christos             default:
    658  1.1  christos                 st->read_state = READ_STATE_HEADER;
    659  1.1  christos                 break;
    660  1.1  christos             }
    661  1.1  christos             break;
    662  1.1  christos 
    663  1.1  christos         case READ_STATE_POST_PROCESS:
    664  1.1  christos             st->read_state_work = post_process_message(s, st->read_state_work);
    665  1.1  christos             switch (st->read_state_work) {
    666  1.1  christos             case WORK_ERROR:
    667  1.1  christos                 check_fatal(s, SSL_F_READ_STATE_MACHINE);
    668  1.1  christos                 /* Fall through */
    669  1.1  christos             case WORK_MORE_A:
    670  1.1  christos             case WORK_MORE_B:
    671  1.1  christos             case WORK_MORE_C:
    672  1.1  christos                 return SUB_STATE_ERROR;
    673  1.1  christos 
    674  1.1  christos             case WORK_FINISHED_CONTINUE:
    675  1.1  christos                 st->read_state = READ_STATE_HEADER;
    676  1.1  christos                 break;
    677  1.1  christos 
    678  1.1  christos             case WORK_FINISHED_STOP:
    679  1.1  christos                 if (SSL_IS_DTLS(s)) {
    680  1.1  christos                     dtls1_stop_timer(s);
    681  1.1  christos                 }
    682  1.1  christos                 return SUB_STATE_FINISHED;
    683  1.1  christos             }
    684  1.1  christos             break;
    685  1.1  christos 
    686  1.1  christos         default:
    687  1.1  christos             /* Shouldn't happen */
    688  1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_READ_STATE_MACHINE,
    689  1.1  christos                      ERR_R_INTERNAL_ERROR);
    690  1.1  christos             return SUB_STATE_ERROR;
    691  1.1  christos         }
    692  1.1  christos     }
    693  1.1  christos }
    694  1.1  christos 
    695  1.1  christos /*
    696  1.1  christos  * Send a previously constructed message to the peer.
    697  1.1  christos  */
    698  1.1  christos static int statem_do_write(SSL *s)
    699  1.1  christos {
    700  1.1  christos     OSSL_STATEM *st = &s->statem;
    701  1.1  christos 
    702  1.1  christos     if (st->hand_state == TLS_ST_CW_CHANGE
    703  1.1  christos         || st->hand_state == TLS_ST_SW_CHANGE) {
    704  1.1  christos         if (SSL_IS_DTLS(s))
    705  1.1  christos             return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
    706  1.1  christos         else
    707  1.1  christos             return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
    708  1.1  christos     } else {
    709  1.1  christos         return ssl_do_write(s);
    710  1.1  christos     }
    711  1.1  christos }
    712  1.1  christos 
    713  1.1  christos /*
    714  1.1  christos  * Initialise the MSG_FLOW_WRITING sub-state machine
    715  1.1  christos  */
    716  1.1  christos static void init_write_state_machine(SSL *s)
    717  1.1  christos {
    718  1.1  christos     OSSL_STATEM *st = &s->statem;
    719  1.1  christos 
    720  1.1  christos     st->write_state = WRITE_STATE_TRANSITION;
    721  1.1  christos }
    722  1.1  christos 
    723  1.1  christos /*
    724  1.1  christos  * This function implements the sub-state machine when the message flow is in
    725  1.1  christos  * MSG_FLOW_WRITING. The valid sub-states and transitions are:
    726  1.1  christos  *
    727  1.1  christos  * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
    728  1.1  christos  * |             |
    729  1.1  christos  * |             v
    730  1.1  christos  * |      WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
    731  1.1  christos  * |             |
    732  1.1  christos  * |             v
    733  1.1  christos  * |       WRITE_STATE_SEND
    734  1.1  christos  * |             |
    735  1.1  christos  * |             v
    736  1.1  christos  * |     WRITE_STATE_POST_WORK
    737  1.1  christos  * |             |
    738  1.1  christos  * +-------------+
    739  1.1  christos  *
    740  1.1  christos  * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
    741  1.1  christos 
    742  1.1  christos  * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
    743  1.1  christos  * sending of the message. This could result in an NBIO event occurring in
    744  1.1  christos  * which case control returns to the calling application. When this function
    745  1.1  christos  * is recalled we will resume in the same state where we left off.
    746  1.1  christos  *
    747  1.1  christos  * WRITE_STATE_SEND sends the message and performs any work to be done after
    748  1.1  christos  * sending.
    749  1.1  christos  *
    750  1.1  christos  * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
    751  1.1  christos  * message has been completed. As for WRITE_STATE_PRE_WORK this could also
    752  1.1  christos  * result in an NBIO event.
    753  1.1  christos  */
    754  1.1  christos static SUB_STATE_RETURN write_state_machine(SSL *s)
    755  1.1  christos {
    756  1.1  christos     OSSL_STATEM *st = &s->statem;
    757  1.1  christos     int ret;
    758  1.1  christos     WRITE_TRAN(*transition) (SSL *s);
    759  1.1  christos     WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
    760  1.1  christos     WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
    761  1.1  christos     int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
    762  1.1  christos                                     int (**confunc) (SSL *s, WPACKET *pkt),
    763  1.1  christos                                     int *mt);
    764  1.1  christos     void (*cb) (const SSL *ssl, int type, int val) = NULL;
    765  1.1  christos     int (*confunc) (SSL *s, WPACKET *pkt);
    766  1.1  christos     int mt;
    767  1.1  christos     WPACKET pkt;
    768  1.1  christos 
    769  1.1  christos     cb = get_callback(s);
    770  1.1  christos 
    771  1.1  christos     if (s->server) {
    772  1.1  christos         transition = ossl_statem_server_write_transition;
    773  1.1  christos         pre_work = ossl_statem_server_pre_work;
    774  1.1  christos         post_work = ossl_statem_server_post_work;
    775  1.1  christos         get_construct_message_f = ossl_statem_server_construct_message;
    776  1.1  christos     } else {
    777  1.1  christos         transition = ossl_statem_client_write_transition;
    778  1.1  christos         pre_work = ossl_statem_client_pre_work;
    779  1.1  christos         post_work = ossl_statem_client_post_work;
    780  1.1  christos         get_construct_message_f = ossl_statem_client_construct_message;
    781  1.1  christos     }
    782  1.1  christos 
    783  1.1  christos     while (1) {
    784  1.1  christos         switch (st->write_state) {
    785  1.1  christos         case WRITE_STATE_TRANSITION:
    786  1.1  christos             if (cb != NULL) {
    787  1.1  christos                 /* Notify callback of an impending state change */
    788  1.1  christos                 if (s->server)
    789  1.1  christos                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
    790  1.1  christos                 else
    791  1.1  christos                     cb(s, SSL_CB_CONNECT_LOOP, 1);
    792  1.1  christos             }
    793  1.1  christos             switch (transition(s)) {
    794  1.1  christos             case WRITE_TRAN_CONTINUE:
    795  1.1  christos                 st->write_state = WRITE_STATE_PRE_WORK;
    796  1.1  christos                 st->write_state_work = WORK_MORE_A;
    797  1.1  christos                 break;
    798  1.1  christos 
    799  1.1  christos             case WRITE_TRAN_FINISHED:
    800  1.1  christos                 return SUB_STATE_FINISHED;
    801  1.1  christos                 break;
    802  1.1  christos 
    803  1.1  christos             case WRITE_TRAN_ERROR:
    804  1.1  christos                 check_fatal(s, SSL_F_WRITE_STATE_MACHINE);
    805  1.1  christos                 return SUB_STATE_ERROR;
    806  1.1  christos             }
    807  1.1  christos             break;
    808  1.1  christos 
    809  1.1  christos         case WRITE_STATE_PRE_WORK:
    810  1.1  christos             switch (st->write_state_work = pre_work(s, st->write_state_work)) {
    811  1.1  christos             case WORK_ERROR:
    812  1.1  christos                 check_fatal(s, SSL_F_WRITE_STATE_MACHINE);
    813  1.1  christos                 /* Fall through */
    814  1.1  christos             case WORK_MORE_A:
    815  1.1  christos             case WORK_MORE_B:
    816  1.1  christos             case WORK_MORE_C:
    817  1.1  christos                 return SUB_STATE_ERROR;
    818  1.1  christos 
    819  1.1  christos             case WORK_FINISHED_CONTINUE:
    820  1.1  christos                 st->write_state = WRITE_STATE_SEND;
    821  1.1  christos                 break;
    822  1.1  christos 
    823  1.1  christos             case WORK_FINISHED_STOP:
    824  1.1  christos                 return SUB_STATE_END_HANDSHAKE;
    825  1.1  christos             }
    826  1.1  christos             if (!get_construct_message_f(s, &pkt, &confunc, &mt)) {
    827  1.1  christos                 /* SSLfatal() already called */
    828  1.1  christos                 return SUB_STATE_ERROR;
    829  1.1  christos             }
    830  1.1  christos             if (mt == SSL3_MT_DUMMY) {
    831  1.1  christos                 /* Skip construction and sending. This isn't a "real" state */
    832  1.1  christos                 st->write_state = WRITE_STATE_POST_WORK;
    833  1.1  christos                 st->write_state_work = WORK_MORE_A;
    834  1.1  christos                 break;
    835  1.1  christos             }
    836  1.1  christos             if (!WPACKET_init(&pkt, s->init_buf)
    837  1.1  christos                     || !ssl_set_handshake_header(s, &pkt, mt)) {
    838  1.1  christos                 WPACKET_cleanup(&pkt);
    839  1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_WRITE_STATE_MACHINE,
    840  1.1  christos                          ERR_R_INTERNAL_ERROR);
    841  1.1  christos                 return SUB_STATE_ERROR;
    842  1.1  christos             }
    843  1.1  christos             if (confunc != NULL && !confunc(s, &pkt)) {
    844  1.1  christos                 WPACKET_cleanup(&pkt);
    845  1.1  christos                 check_fatal(s, SSL_F_WRITE_STATE_MACHINE);
    846  1.1  christos                 return SUB_STATE_ERROR;
    847  1.1  christos             }
    848  1.1  christos             if (!ssl_close_construct_packet(s, &pkt, mt)
    849  1.1  christos                     || !WPACKET_finish(&pkt)) {
    850  1.1  christos                 WPACKET_cleanup(&pkt);
    851  1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_WRITE_STATE_MACHINE,
    852  1.1  christos                          ERR_R_INTERNAL_ERROR);
    853  1.1  christos                 return SUB_STATE_ERROR;
    854  1.1  christos             }
    855  1.1  christos 
    856  1.1  christos             /* Fall through */
    857  1.1  christos 
    858  1.1  christos         case WRITE_STATE_SEND:
    859  1.1  christos             if (SSL_IS_DTLS(s) && st->use_timer) {
    860  1.1  christos                 dtls1_start_timer(s);
    861  1.1  christos             }
    862  1.1  christos             ret = statem_do_write(s);
    863  1.1  christos             if (ret <= 0) {
    864  1.1  christos                 return SUB_STATE_ERROR;
    865  1.1  christos             }
    866  1.1  christos             st->write_state = WRITE_STATE_POST_WORK;
    867  1.1  christos             st->write_state_work = WORK_MORE_A;
    868  1.1  christos             /* Fall through */
    869  1.1  christos 
    870  1.1  christos         case WRITE_STATE_POST_WORK:
    871  1.1  christos             switch (st->write_state_work = post_work(s, st->write_state_work)) {
    872  1.1  christos             case WORK_ERROR:
    873  1.1  christos                 check_fatal(s, SSL_F_WRITE_STATE_MACHINE);
    874  1.1  christos                 /* Fall through */
    875  1.1  christos             case WORK_MORE_A:
    876  1.1  christos             case WORK_MORE_B:
    877  1.1  christos             case WORK_MORE_C:
    878  1.1  christos                 return SUB_STATE_ERROR;
    879  1.1  christos 
    880  1.1  christos             case WORK_FINISHED_CONTINUE:
    881  1.1  christos                 st->write_state = WRITE_STATE_TRANSITION;
    882  1.1  christos                 break;
    883  1.1  christos 
    884  1.1  christos             case WORK_FINISHED_STOP:
    885  1.1  christos                 return SUB_STATE_END_HANDSHAKE;
    886  1.1  christos             }
    887  1.1  christos             break;
    888  1.1  christos 
    889  1.1  christos         default:
    890  1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_WRITE_STATE_MACHINE,
    891  1.1  christos                      ERR_R_INTERNAL_ERROR);
    892  1.1  christos             return SUB_STATE_ERROR;
    893  1.1  christos         }
    894  1.1  christos     }
    895  1.1  christos }
    896  1.1  christos 
    897  1.1  christos /*
    898  1.1  christos  * Flush the write BIO
    899  1.1  christos  */
    900  1.1  christos int statem_flush(SSL *s)
    901  1.1  christos {
    902  1.1  christos     s->rwstate = SSL_WRITING;
    903  1.1  christos     if (BIO_flush(s->wbio) <= 0) {
    904  1.1  christos         return 0;
    905  1.1  christos     }
    906  1.1  christos     s->rwstate = SSL_NOTHING;
    907  1.1  christos 
    908  1.1  christos     return 1;
    909  1.1  christos }
    910  1.1  christos 
    911  1.1  christos /*
    912  1.1  christos  * Called by the record layer to determine whether application data is
    913  1.1  christos  * allowed to be received in the current handshake state or not.
    914  1.1  christos  *
    915  1.1  christos  * Return values are:
    916  1.1  christos  *   1: Yes (application data allowed)
    917  1.1  christos  *   0: No (application data not allowed)
    918  1.1  christos  */
    919  1.1  christos int ossl_statem_app_data_allowed(SSL *s)
    920  1.1  christos {
    921  1.1  christos     OSSL_STATEM *st = &s->statem;
    922  1.1  christos 
    923  1.1  christos     if (st->state == MSG_FLOW_UNINITED)
    924  1.1  christos         return 0;
    925  1.1  christos 
    926  1.1  christos     if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
    927  1.1  christos         return 0;
    928  1.1  christos 
    929  1.1  christos     if (s->server) {
    930  1.1  christos         /*
    931  1.1  christos          * If we're a server and we haven't got as far as writing our
    932  1.1  christos          * ServerHello yet then we allow app data
    933  1.1  christos          */
    934  1.1  christos         if (st->hand_state == TLS_ST_BEFORE
    935  1.1  christos             || st->hand_state == TLS_ST_SR_CLNT_HELLO)
    936  1.1  christos             return 1;
    937  1.1  christos     } else {
    938  1.1  christos         /*
    939  1.1  christos          * If we're a client and we haven't read the ServerHello yet then we
    940  1.1  christos          * allow app data
    941  1.1  christos          */
    942  1.1  christos         if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
    943  1.1  christos             return 1;
    944  1.1  christos     }
    945  1.1  christos 
    946  1.1  christos     return 0;
    947  1.1  christos }
    948  1.1  christos 
    949  1.1  christos /*
    950  1.1  christos  * This function returns 1 if TLS exporter is ready to export keying
    951  1.1  christos  * material, or 0 if otherwise.
    952  1.1  christos  */
    953  1.1  christos int ossl_statem_export_allowed(SSL *s)
    954  1.1  christos {
    955  1.1  christos     return s->s3->previous_server_finished_len != 0
    956  1.1  christos            && s->statem.hand_state != TLS_ST_SW_FINISHED;
    957  1.1  christos }
    958  1.1  christos 
    959  1.1  christos /*
    960  1.1  christos  * Return 1 if early TLS exporter is ready to export keying material,
    961  1.1  christos  * or 0 if otherwise.
    962  1.1  christos  */
    963  1.1  christos int ossl_statem_export_early_allowed(SSL *s)
    964  1.1  christos {
    965  1.1  christos     /*
    966  1.1  christos      * The early exporter secret is only present on the server if we
    967  1.1  christos      * have accepted early_data. It is present on the client as long
    968  1.1  christos      * as we have sent early_data.
    969  1.1  christos      */
    970  1.1  christos     return s->ext.early_data == SSL_EARLY_DATA_ACCEPTED
    971  1.1  christos            || (!s->server && s->ext.early_data != SSL_EARLY_DATA_NOT_SENT);
    972  1.1  christos }
    973