Coverage Report

Created: 2025-12-31 06:58

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