Coverage Report

Created: 2025-12-31 06:58

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