Coverage Report

Created: 2025-06-13 06:58

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