Coverage Report

Created: 2018-08-29 13:53

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