Coverage Report

Created: 2026-07-12 07:21

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-2026 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
195k
{
76
195k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
77
78
195k
    if (sc == NULL)
79
0
        return TLS_ST_BEFORE;
80
81
195k
    return sc->statem.hand_state;
82
195k
}
83
84
int SSL_in_init(const SSL *s)
85
78.1M
{
86
78.1M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
87
88
78.1M
    if (sc == NULL)
89
0
        return 0;
90
91
78.1M
    return sc->statem.in_init;
92
78.1M
}
93
94
int SSL_is_init_finished(const SSL *s)
95
157k
{
96
157k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
97
98
157k
    if (sc == NULL)
99
0
        return 0;
100
101
157k
    return !(sc->statem.in_init) && (sc->statem.hand_state == TLS_ST_OK);
102
157k
}
103
104
int SSL_in_before(const SSL *s)
105
37.5M
{
106
37.5M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
107
108
37.5M
    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
37.5M
    return (sc->statem.hand_state == TLS_ST_BEFORE)
119
184k
        && (sc->statem.state == MSG_FLOW_UNINITED);
120
37.5M
}
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
496k
{
132
496k
    s->statem.state = MSG_FLOW_UNINITED;
133
496k
    s->statem.hand_state = TLS_ST_BEFORE;
134
496k
    ossl_statem_set_in_init(s, 1);
135
496k
    s->statem.no_cert_verify = 0;
136
496k
}
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.27k
{
143
1.27k
    ossl_statem_set_in_init(s, 1);
144
1.27k
    s->statem.request_state = TLS_ST_SW_HELLO_REQ;
145
1.27k
}
146
147
void ossl_statem_send_fatal(SSL_CONNECTION *s, int al)
148
66.1k
{
149
    /* We shouldn't call SSLfatal() twice. Once is enough */
150
66.1k
    if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR)
151
4
        return;
152
66.1k
    ossl_statem_set_in_init(s, 1);
153
66.1k
    s->statem.state = MSG_FLOW_ERROR;
154
66.1k
    if (al != SSL_AD_NO_ALERT && s->rlayer.wrlmethod != NULL)
155
65.2k
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
156
66.1k
}
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
82.6k
{
167
82.6k
    va_list args;
168
169
82.6k
    va_start(args, fmt);
170
82.6k
    ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
171
82.6k
    va_end(args);
172
173
82.6k
    ossl_statem_send_fatal(s, al);
174
82.6k
}
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
21.2k
    do {                                                             \
183
21.2k
        if (!ossl_assert((s)->statem.in_init                         \
184
21.2k
                && (s)->statem.state == MSG_FLOW_ERROR))             \
185
21.2k
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
186
21.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
334k
{
197
334k
    if (s->statem.state == MSG_FLOW_ERROR)
198
18
        return 1;
199
200
334k
    return 0;
201
334k
}
202
203
void ossl_statem_set_in_init(SSL_CONNECTION *s, int init)
204
696k
{
205
696k
    s->statem.in_init = init;
206
696k
    if (s->rlayer.rrlmethod != NULL && s->rlayer.rrlmethod->set_in_init != NULL)
207
117k
        s->rlayer.rrlmethod->set_in_init(s->rlayer.rrl, init);
208
696k
}
209
210
int ossl_statem_get_in_handshake(SSL_CONNECTION *s)
211
91.0M
{
212
91.0M
    return s->statem.in_handshake;
213
91.0M
}
214
215
void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand)
216
1.66k
{
217
1.66k
    if (inhand)
218
832
        s->statem.in_handshake++;
219
832
    else
220
832
        s->statem.in_handshake--;
221
1.66k
}
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
142k
{
226
142k
    if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
227
2.47k
        return 0;
228
229
139k
    if (!s->server
230
139k
        || s->statem.hand_state != TLS_ST_EARLY_DATA
231
139k
        || s->hello_retry_request == SSL_HRR_COMPLETE)
232
29
        return 0;
233
234
139k
    return 1;
235
139k
}
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
40.0M
{
247
40.0M
    if (sending == -1) {
248
27.0M
        if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
249
27.0M
            || s->statem.hand_state == TLS_ST_EARLY_DATA) {
250
640
            ossl_statem_set_in_init(s, 1);
251
640
            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
640
        }
259
27.0M
    } else if (!s->server) {
260
12.9M
        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.9M
            || (!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.9M
    } else {
272
13.8k
        if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
273
5.67k
            && s->statem.hand_state == TLS_ST_EARLY_DATA)
274
0
            ossl_statem_set_in_init(s, 1);
275
13.8k
    }
276
40.0M
    return 1;
277
40.0M
}
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
37.3M
{
295
37.3M
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
296
297
37.3M
    if (sc == NULL)
298
0
        return -1;
299
300
37.3M
    return state_machine(sc, 0);
301
37.3M
}
302
303
int ossl_statem_accept(SSL *s)
304
93.3k
{
305
93.3k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
306
307
93.3k
    if (sc == NULL)
308
0
        return -1;
309
310
93.3k
    return state_machine(sc, 1);
311
93.3k
}
312
313
typedef void (*info_cb)(const SSL *, int, int);
314
315
static info_cb get_callback(SSL_CONNECTION *s)
316
134M
{
317
134M
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
318
319
134M
    if (s->info_callback != NULL)
320
0
        return s->info_callback;
321
134M
    else if (sctx->info_callback != NULL)
322
0
        return sctx->info_callback;
323
324
134M
    return NULL;
325
134M
}
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
66.9M
{
357
66.9M
    BUF_MEM *buf = NULL;
358
66.9M
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
359
66.9M
    OSSL_STATEM *st = &s->statem;
360
66.9M
    int ret = -1;
361
66.9M
    int ssret;
362
66.9M
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
363
66.9M
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
364
365
66.9M
    if (st->state == MSG_FLOW_ERROR) {
366
        /* Shouldn't have been called if we're already in the error state */
367
3.89k
        return -1;
368
3.89k
    }
369
370
66.9M
    ERR_clear_error();
371
66.9M
    clear_sys_error();
372
373
66.9M
    cb = get_callback(s);
374
375
66.9M
    st->in_handshake++;
376
66.9M
    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
154k
        if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(ssl))
382
0
            return -1;
383
154k
    }
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
66.9M
    if (st->state == MSG_FLOW_UNINITED
397
66.8M
        || st->state == MSG_FLOW_FINISHED) {
398
228k
        if (st->state == MSG_FLOW_UNINITED) {
399
154k
            st->hand_state = TLS_ST_BEFORE;
400
154k
            st->request_state = TLS_ST_BEFORE;
401
154k
        }
402
403
228k
        s->server = server;
404
228k
        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
228k
        if (SSL_CONNECTION_IS_DTLS(s)) {
416
47.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
180k
        } else {
421
180k
            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
180k
        }
426
427
228k
        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
228k
        if (s->init_buf == NULL) {
433
228k
            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
228k
            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
228k
            s->init_buf = buf;
442
228k
            buf = NULL;
443
228k
        }
444
445
228k
        s->init_num = 0;
446
447
        /*
448
         * Should have been reset by tls_process_finished, too.
449
         */
450
228k
        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
228k
            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
228k
        if ((SSL_in_before(ssl))
465
154k
            || s->renegotiate) {
466
154k
            if (!tls_setup_handshake(s)) {
467
                /* SSLfatal() already called */
468
0
                goto end;
469
0
            }
470
471
154k
            if (SSL_IS_FIRST_HANDSHAKE(s))
472
154k
                st->read_state_first_init = 1;
473
154k
        }
474
475
228k
        st->state = MSG_FLOW_WRITING;
476
228k
        init_write_state_machine(s);
477
228k
    }
478
479
67.4M
    while (st->state != MSG_FLOW_FINISHED) {
480
67.3M
        if (st->state == MSG_FLOW_READING) {
481
67.0M
            ssret = read_state_machine(s);
482
67.0M
            if (ssret == SUB_STATE_FINISHED) {
483
129k
                st->state = MSG_FLOW_WRITING;
484
129k
                init_write_state_machine(s);
485
66.8M
            } else {
486
                /* NBIO or error */
487
66.8M
                goto end;
488
66.8M
            }
489
67.0M
        } else if (st->state == MSG_FLOW_WRITING) {
490
358k
            ssret = write_state_machine(s);
491
358k
            if (ssret == SUB_STATE_FINISHED) {
492
273k
                st->state = MSG_FLOW_READING;
493
273k
                init_read_state_machine(s);
494
273k
            } else if (ssret == SUB_STATE_END_HANDSHAKE) {
495
82.6k
                st->state = MSG_FLOW_FINISHED;
496
82.6k
            } else {
497
                /* NBIO or error */
498
1.65k
                goto end;
499
1.65k
            }
500
358k
        } 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
67.3M
    }
507
508
82.6k
    ret = 1;
509
510
66.9M
end:
511
66.9M
    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
66.9M
    BUF_MEM_free(buf);
525
66.9M
    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
66.9M
    return ret;
532
82.6k
}
533
534
/*
535
 * Initialise the MSG_FLOW_READING sub-state machine
536
 */
537
static void init_read_state_machine(SSL_CONNECTION *s)
538
287k
{
539
287k
    OSSL_STATEM *st = &s->statem;
540
541
287k
    st->read_state = READ_STATE_HEADER;
542
287k
}
543
544
/*
545
 * This function implements the sub-state machine when the message flow is in
546
 * MSG_FLOW_READING. The valid sub-states and transitions are:
547
 *
548
 * READ_STATE_HEADER <--+<-------------+
549
 *        |             |              |
550
 *        v             |              |
551
 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
552
 *        |                            |
553
 *        +----------------------------+
554
 *        v
555
 * [SUB_STATE_FINISHED]
556
 *
557
 * READ_STATE_HEADER has the responsibility for reading in the message header
558
 * and transitioning the state of the handshake state machine.
559
 *
560
 * READ_STATE_BODY reads in the rest of the message and then subsequently
561
 * processes it.
562
 *
563
 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
564
 * processing activity performed on the message may block.
565
 *
566
 * Any of the above states could result in an NBIO event occurring in which case
567
 * control returns to the calling application. When this function is recalled we
568
 * will resume in the same state where we left off.
569
 */
570
static SUB_STATE_RETURN read_state_machine(SSL_CONNECTION *s)
571
31.7M
{
572
31.7M
    OSSL_STATEM *st = &s->statem;
573
31.7M
    int ret, mt;
574
31.7M
    size_t len = 0;
575
31.7M
    int (*transition)(SSL_CONNECTION *s, int mt);
576
31.7M
    PACKET pkt;
577
31.7M
    MSG_PROCESS_RETURN (*process_message)(SSL_CONNECTION *s, PACKET *pkt);
578
31.7M
    WORK_STATE (*post_process_message)(SSL_CONNECTION *s, WORK_STATE wst);
579
31.7M
    size_t (*max_message_size)(SSL_CONNECTION *s);
580
31.7M
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
581
31.7M
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
582
583
31.7M
    cb = get_callback(s);
584
585
31.7M
    if (s->server) {
586
62.1k
        transition = ossl_statem_server_read_transition;
587
62.1k
        process_message = ossl_statem_server_process_message;
588
62.1k
        max_message_size = ossl_statem_server_max_message_size;
589
62.1k
        post_process_message = ossl_statem_server_post_process_message;
590
31.6M
    } else {
591
31.6M
        transition = ossl_statem_client_read_transition;
592
31.6M
        process_message = ossl_statem_client_process_message;
593
31.6M
        max_message_size = ossl_statem_client_max_message_size;
594
31.6M
        post_process_message = ossl_statem_client_post_process_message;
595
31.6M
    }
596
597
31.7M
    if (st->read_state_first_init) {
598
60.4k
        s->first_packet = 1;
599
60.4k
        st->read_state_first_init = 0;
600
60.4k
    }
601
602
31.8M
    while (1) {
603
31.8M
        switch (st->read_state) {
604
24.3M
        case READ_STATE_HEADER:
605
            /* Get the state the peer wants to move to */
606
24.3M
            if (SSL_CONNECTION_IS_DTLS(s)) {
607
                /*
608
                 * In DTLS we get the whole message in one go - header and body
609
                 */
610
40.8k
                ret = dtls_get_message(s, &mt);
611
24.2M
            } else {
612
24.2M
                ret = tls_get_message_header(s, &mt);
613
24.2M
            }
614
615
24.3M
            if (ret == 0) {
616
                /* Could be non-blocking IO */
617
24.1M
                return SUB_STATE_ERROR;
618
24.1M
            }
619
620
152k
            if (cb != NULL) {
621
                /* Notify callback of an impending state change */
622
0
                if (s->server)
623
0
                    cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
624
0
                else
625
0
                    cb(ssl, SSL_CB_CONNECT_LOOP, 1);
626
0
            }
627
            /*
628
             * Validate that we are allowed to move to the new state and move
629
             * to that state if so
630
             */
631
152k
            if (!transition(s, mt))
632
2.79k
                return SUB_STATE_ERROR;
633
634
149k
            if (s->s3.tmp.message_size > max_message_size(s)) {
635
325
                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
636
325
                    SSL_R_EXCESSIVE_MESSAGE_SIZE);
637
325
                return SUB_STATE_ERROR;
638
325
            }
639
640
149k
            st->read_state = READ_STATE_BODY;
641
            /* Fall through */
642
643
7.64M
        case READ_STATE_BODY:
644
7.64M
            if (SSL_CONNECTION_IS_DTLS(s)) {
645
                /*
646
                 * Actually we already have the body, but we give DTLS the
647
                 * opportunity to do any further processing.
648
                 */
649
30.0k
                ret = dtls_get_message_body(s, &len);
650
7.61M
            } else {
651
7.61M
                ret = tls_get_message_body(s, &len);
652
7.61M
            }
653
7.64M
            if (ret == 0) {
654
                /* Could be non-blocking IO */
655
7.50M
                return SUB_STATE_ERROR;
656
7.50M
            }
657
658
146k
            s->first_packet = 0;
659
146k
            if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
660
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
661
0
                return SUB_STATE_ERROR;
662
0
            }
663
146k
            ret = process_message(s, &pkt);
664
665
            /* Discard the packet data */
666
146k
            s->init_num = 0;
667
668
146k
            switch (ret) {
669
15.5k
            case MSG_PROCESS_ERROR:
670
15.5k
                check_fatal(s);
671
15.5k
                return SUB_STATE_ERROR;
672
673
46.1k
            case MSG_PROCESS_FINISHED_READING:
674
46.1k
                if (SSL_CONNECTION_IS_DTLS(s)) {
675
2.22k
                    dtls1_stop_timer(s);
676
2.22k
                }
677
46.1k
                return SUB_STATE_FINISHED;
678
679
33.4k
            case MSG_PROCESS_CONTINUE_PROCESSING:
680
33.4k
                st->read_state = READ_STATE_POST_PROCESS;
681
33.4k
                st->read_state_work = WORK_MORE_A;
682
33.4k
                break;
683
684
51.1k
            default:
685
51.1k
                st->read_state = READ_STATE_HEADER;
686
51.1k
                break;
687
146k
            }
688
84.5k
            break;
689
690
84.5k
        case READ_STATE_POST_PROCESS:
691
33.4k
            st->read_state_work = post_process_message(s, st->read_state_work);
692
33.4k
            switch (st->read_state_work) {
693
4.93k
            case WORK_ERROR:
694
4.93k
                check_fatal(s);
695
                /* Fall through */
696
4.93k
            case WORK_MORE_A:
697
4.93k
            case WORK_MORE_B:
698
4.93k
            case WORK_MORE_C:
699
4.93k
                return SUB_STATE_ERROR;
700
701
18.5k
            case WORK_FINISHED_CONTINUE:
702
18.5k
                st->read_state = READ_STATE_HEADER;
703
18.5k
                break;
704
705
0
            case WORK_FINISHED_SWAP:
706
9.93k
            case WORK_FINISHED_STOP:
707
9.93k
                if (SSL_CONNECTION_IS_DTLS(s)) {
708
4.37k
                    dtls1_stop_timer(s);
709
4.37k
                }
710
9.93k
                return SUB_STATE_FINISHED;
711
33.4k
            }
712
18.5k
            break;
713
714
18.5k
        default:
715
            /* Shouldn't happen */
716
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
717
0
            return SUB_STATE_ERROR;
718
31.8M
        }
719
31.8M
    }
720
31.7M
}
721
722
/*
723
 * Send a previously constructed message to the peer.
724
 */
725
static int statem_do_write(SSL_CONNECTION *s)
726
289k
{
727
289k
    OSSL_STATEM *st = &s->statem;
728
729
289k
    if (st->hand_state == TLS_ST_CW_CHANGE
730
272k
        || st->hand_state == TLS_ST_SW_CHANGE) {
731
21.8k
        if (SSL_CONNECTION_IS_DTLS(s))
732
7.65k
            return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
733
14.1k
        else
734
14.1k
            return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
735
267k
    } else {
736
267k
        return ssl_do_write(s);
737
267k
    }
738
289k
}
739
740
/*
741
 * Initialise the MSG_FLOW_WRITING sub-state machine
742
 */
743
static void init_write_state_machine(SSL_CONNECTION *s)
744
372k
{
745
372k
    OSSL_STATEM *st = &s->statem;
746
747
372k
    st->write_state = WRITE_STATE_TRANSITION;
748
372k
}
749
750
/*
751
 * This function implements the sub-state machine when the message flow is in
752
 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
753
 *
754
 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
755
 * |             |
756
 * |             v
757
 * |      WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
758
 * |             |
759
 * |             v
760
 * |       WRITE_STATE_SEND
761
 * |             |
762
 * |             v
763
 * |     WRITE_STATE_POST_WORK
764
 * |             |
765
 * +-------------+
766
 *
767
 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
768
769
 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
770
 * sending of the message. This could result in an NBIO event occurring in
771
 * which case control returns to the calling application. When this function
772
 * is recalled we will resume in the same state where we left off.
773
 *
774
 * WRITE_STATE_SEND sends the message and performs any work to be done after
775
 * sending.
776
 *
777
 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
778
 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
779
 * result in an NBIO event.
780
 */
781
static SUB_STATE_RETURN write_state_machine(SSL_CONNECTION *s)
782
255k
{
783
255k
    OSSL_STATEM *st = &s->statem;
784
255k
    int ret;
785
255k
    WRITE_TRAN (*transition)(SSL_CONNECTION *s);
786
255k
    WORK_STATE (*pre_work)(SSL_CONNECTION *s, WORK_STATE wst);
787
255k
    WORK_STATE (*post_work)(SSL_CONNECTION *s, WORK_STATE wst);
788
255k
    int (*get_construct_message_f)(SSL_CONNECTION *s,
789
255k
        CON_FUNC_RETURN (**confunc)(SSL_CONNECTION *s,
790
255k
            WPACKET *pkt),
791
255k
        int *mt);
792
255k
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
793
255k
    CON_FUNC_RETURN (*confunc)(SSL_CONNECTION *s, WPACKET *pkt);
794
255k
    int mt;
795
255k
    WPACKET pkt;
796
255k
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
797
798
255k
    cb = get_callback(s);
799
800
255k
    if (s->server) {
801
168k
        transition = ossl_statem_server_write_transition;
802
168k
        pre_work = ossl_statem_server_pre_work;
803
168k
        post_work = ossl_statem_server_post_work;
804
168k
        get_construct_message_f = ossl_statem_server_construct_message;
805
168k
    } else {
806
87.4k
        transition = ossl_statem_client_write_transition;
807
87.4k
        pre_work = ossl_statem_client_pre_work;
808
87.4k
        post_work = ossl_statem_client_post_work;
809
87.4k
        get_construct_message_f = ossl_statem_client_construct_message;
810
87.4k
    }
811
812
639k
    while (1) {
813
639k
        switch (st->write_state) {
814
410k
        case WRITE_STATE_TRANSITION:
815
410k
            if (cb != NULL) {
816
                /* Notify callback of an impending state change */
817
0
                if (s->server)
818
0
                    cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
819
0
                else
820
0
                    cb(ssl, SSL_CB_CONNECT_LOOP, 1);
821
0
            }
822
410k
            switch (transition(s)) {
823
228k
            case WRITE_TRAN_CONTINUE:
824
228k
                st->write_state = WRITE_STATE_PRE_WORK;
825
228k
                st->write_state_work = WORK_MORE_A;
826
228k
                break;
827
828
181k
            case WRITE_TRAN_FINISHED:
829
181k
                return SUB_STATE_FINISHED;
830
831
0
            case WRITE_TRAN_ERROR:
832
0
                check_fatal(s);
833
0
                return SUB_STATE_ERROR;
834
410k
            }
835
228k
            break;
836
837
228k
        case WRITE_STATE_PRE_WORK:
838
228k
            switch (st->write_state_work = pre_work(s, st->write_state_work)) {
839
0
            case WORK_ERROR:
840
0
                check_fatal(s);
841
                /* Fall through */
842
0
            case WORK_MORE_A:
843
0
            case WORK_MORE_B:
844
0
            case WORK_MORE_C:
845
0
                return SUB_STATE_ERROR;
846
847
155k
            case WORK_FINISHED_CONTINUE:
848
155k
                st->write_state = WRITE_STATE_SEND;
849
155k
                break;
850
851
0
            case WORK_FINISHED_SWAP:
852
0
                return SUB_STATE_FINISHED;
853
854
72.9k
            case WORK_FINISHED_STOP:
855
72.9k
                return SUB_STATE_END_HANDSHAKE;
856
228k
            }
857
155k
            if (!get_construct_message_f(s, &confunc, &mt)) {
858
                /* SSLfatal() already called */
859
0
                return SUB_STATE_ERROR;
860
0
            }
861
155k
            if (mt == SSL3_MT_DUMMY) {
862
                /* Skip construction and sending. This isn't a "real" state */
863
1.12k
                st->write_state = WRITE_STATE_POST_WORK;
864
1.12k
                st->write_state_work = WORK_MORE_A;
865
1.12k
                break;
866
1.12k
            }
867
154k
            if (!WPACKET_init(&pkt, s->init_buf)
868
154k
                || !ssl_set_handshake_header(s, &pkt, mt)) {
869
0
                WPACKET_cleanup(&pkt);
870
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
871
0
                return SUB_STATE_ERROR;
872
0
            }
873
154k
            if (confunc != NULL) {
874
154k
                CON_FUNC_RETURN tmpret;
875
876
154k
                tmpret = confunc(s, &pkt);
877
154k
                if (tmpret == CON_FUNC_ERROR) {
878
721
                    WPACKET_cleanup(&pkt);
879
721
                    check_fatal(s);
880
721
                    return SUB_STATE_ERROR;
881
153k
                } else if (tmpret == CON_FUNC_DONT_SEND) {
882
                    /*
883
                     * The construction function decided not to construct the
884
                     * message after all and continue. Skip sending.
885
                     */
886
0
                    WPACKET_cleanup(&pkt);
887
0
                    st->write_state = WRITE_STATE_POST_WORK;
888
0
                    st->write_state_work = WORK_MORE_A;
889
0
                    break;
890
0
                } /* else success */
891
154k
            }
892
153k
            if (!ssl_close_construct_packet(s, &pkt, mt)
893
153k
                || !WPACKET_finish(&pkt)) {
894
0
                WPACKET_cleanup(&pkt);
895
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
896
0
                return SUB_STATE_ERROR;
897
0
            }
898
899
            /* Fall through */
900
901
153k
        case WRITE_STATE_SEND:
902
153k
            if (SSL_CONNECTION_IS_DTLS(s) && st->use_timer) {
903
50.6k
                dtls1_start_timer(s);
904
50.6k
            }
905
153k
            ret = statem_do_write(s);
906
153k
            if (ret <= 0) {
907
0
                return SUB_STATE_ERROR;
908
0
            }
909
153k
            st->write_state = WRITE_STATE_POST_WORK;
910
153k
            st->write_state_work = WORK_MORE_A;
911
            /* Fall through */
912
913
154k
        case WRITE_STATE_POST_WORK:
914
154k
            switch (st->write_state_work = post_work(s, st->write_state_work)) {
915
6
            case WORK_ERROR:
916
6
                check_fatal(s);
917
                /* Fall through */
918
6
            case WORK_MORE_A:
919
6
            case WORK_MORE_B:
920
6
            case WORK_MORE_C:
921
6
                return SUB_STATE_ERROR;
922
923
154k
            case WORK_FINISHED_CONTINUE:
924
154k
                st->write_state = WRITE_STATE_TRANSITION;
925
154k
                break;
926
927
0
            case WORK_FINISHED_SWAP:
928
0
                return SUB_STATE_FINISHED;
929
930
0
            case WORK_FINISHED_STOP:
931
0
                return SUB_STATE_END_HANDSHAKE;
932
154k
            }
933
154k
            break;
934
935
154k
        default:
936
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
937
0
            return SUB_STATE_ERROR;
938
639k
        }
939
639k
    }
940
255k
}
941
942
/*
943
 * Flush the write BIO
944
 */
945
int statem_flush(SSL_CONNECTION *s)
946
180k
{
947
180k
    s->rwstate = SSL_WRITING;
948
180k
    if (BIO_flush(s->wbio) <= 0) {
949
0
        return 0;
950
0
    }
951
180k
    s->rwstate = SSL_NOTHING;
952
953
180k
    return 1;
954
180k
}
955
956
/*
957
 * Called by the record layer to determine whether application data is
958
 * allowed to be received in the current handshake state or not.
959
 *
960
 * Return values are:
961
 *   1: Yes (application data allowed)
962
 *   0: No (application data not allowed)
963
 */
964
int ossl_statem_app_data_allowed(SSL_CONNECTION *s)
965
4.68k
{
966
4.68k
    OSSL_STATEM *st = &s->statem;
967
968
4.68k
    if (st->state == MSG_FLOW_UNINITED)
969
0
        return 0;
970
971
4.68k
    if (!s->s3.in_read_app_data || (s->s3.total_renegotiations == 0))
972
3.81k
        return 0;
973
974
878
    if (s->server) {
975
        /*
976
         * If we're a server and we haven't got as far as writing our
977
         * ServerHello yet then we allow app data
978
         */
979
0
        if (st->hand_state == TLS_ST_BEFORE
980
0
            || st->hand_state == TLS_ST_SR_CLNT_HELLO)
981
0
            return 1;
982
878
    } else {
983
        /*
984
         * If we're a client and we haven't read the ServerHello yet then we
985
         * allow app data
986
         */
987
878
        if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
988
832
            return 1;
989
878
    }
990
991
46
    return 0;
992
878
}
993
994
/*
995
 * This function returns 1 if TLS exporter is ready to export keying
996
 * material, or 0 if otherwise.
997
 */
998
int ossl_statem_export_allowed(SSL_CONNECTION *s)
999
0
{
1000
0
    return s->s3.previous_server_finished_len != 0
1001
0
        && s->statem.hand_state != TLS_ST_SW_FINISHED;
1002
0
}
1003
1004
/*
1005
 * Return 1 if early TLS exporter is ready to export keying material,
1006
 * or 0 if otherwise.
1007
 */
1008
int ossl_statem_export_early_allowed(SSL_CONNECTION *s)
1009
0
{
1010
    /*
1011
     * The early exporter secret is only present on the server if we
1012
     * have accepted early_data. It is present on the client as long
1013
     * as we have sent early_data.
1014
     */
1015
0
    return s->ext.early_data == SSL_EARLY_DATA_ACCEPTED
1016
0
        || (!s->server && s->ext.early_data != SSL_EARLY_DATA_NOT_SENT);
1017
0
}