Coverage Report

Created: 2026-10-28 06:28

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
401k
{
76
401k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
77
78
401k
    if (sc == NULL)
79
0
        return TLS_ST_BEFORE;
80
81
401k
    return sc->statem.hand_state;
82
401k
}
83
84
int SSL_in_init(const SSL *s)
85
116M
{
86
116M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
87
88
116M
    if (sc == NULL)
89
0
        return 0;
90
91
116M
    return sc->statem.in_init;
92
116M
}
93
94
int SSL_is_init_finished(const SSL *s)
95
262k
{
96
262k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
97
98
262k
    if (sc == NULL)
99
0
        return 0;
100
101
262k
    return !(sc->statem.in_init) && (sc->statem.hand_state == TLS_ST_OK);
102
262k
}
103
104
int SSL_in_before(const SSL *s)
105
54.7M
{
106
54.7M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
107
108
54.7M
    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
54.7M
    return (sc->statem.hand_state == TLS_ST_BEFORE)
119
291k
        && (sc->statem.state == MSG_FLOW_UNINITED);
120
54.7M
}
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
517k
{
132
517k
    s->statem.state = MSG_FLOW_UNINITED;
133
517k
    s->statem.hand_state = TLS_ST_BEFORE;
134
517k
    ossl_statem_set_in_init(s, 1);
135
517k
    s->statem.no_cert_verify = 0;
136
517k
}
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.49k
{
143
1.49k
    ossl_statem_set_in_init(s, 1);
144
1.49k
    s->statem.request_state = TLS_ST_SW_HELLO_REQ;
145
1.49k
}
146
147
void ossl_statem_send_fatal(SSL_CONNECTION *s, int al)
148
86.4k
{
149
    /* We shouldn't call SSLfatal() twice. Once is enough */
150
86.4k
    if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR)
151
14
        return;
152
86.4k
    ossl_statem_set_in_init(s, 1);
153
86.4k
    s->statem.state = MSG_FLOW_ERROR;
154
86.4k
    if (al != SSL_AD_NO_ALERT && s->rlayer.wrlmethod != NULL)
155
84.5k
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
156
86.4k
}
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
84.6k
{
167
84.6k
    va_list args;
168
169
84.6k
    va_start(args, fmt);
170
84.6k
    ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
171
84.6k
    va_end(args);
172
173
84.6k
    ossl_statem_send_fatal(s, al);
174
84.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
20.9k
    do {                                                             \
183
20.9k
        if (!ossl_assert((s)->statem.in_init                         \
184
20.9k
                && (s)->statem.state == MSG_FLOW_ERROR))             \
185
20.9k
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
186
20.9k
    } 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
329k
{
197
329k
    if (s->statem.state == MSG_FLOW_ERROR)
198
0
        return 1;
199
200
329k
    return 0;
201
329k
}
202
203
void ossl_statem_set_in_init(SSL_CONNECTION *s, int init)
204
779k
{
205
779k
    s->statem.in_init = init;
206
779k
    if (s->rlayer.rrlmethod != NULL && s->rlayer.rrlmethod->set_in_init != NULL)
207
136k
        s->rlayer.rrlmethod->set_in_init(s->rlayer.rrl, init);
208
779k
}
209
210
int ossl_statem_get_in_handshake(SSL_CONNECTION *s)
211
92.6M
{
212
92.6M
    return s->statem.in_handshake;
213
92.6M
}
214
215
void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand)
216
8.50k
{
217
8.50k
    if (inhand)
218
4.25k
        s->statem.in_handshake++;
219
4.25k
    else
220
4.25k
        s->statem.in_handshake--;
221
8.50k
}
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
133k
{
226
133k
    if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
227
2.21k
        return 0;
228
229
131k
    if (!s->server
230
131k
        || s->statem.hand_state != TLS_ST_EARLY_DATA
231
131k
        || s->hello_retry_request == SSL_HRR_COMPLETE)
232
25
        return 0;
233
234
131k
    return 1;
235
131k
}
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
41.0M
{
247
41.0M
    if (sending == -1) {
248
30.6M
        if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
249
30.6M
            || s->statem.hand_state == TLS_ST_EARLY_DATA) {
250
595
            ossl_statem_set_in_init(s, 1);
251
595
            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
595
        }
259
30.6M
    } else if (!s->server) {
260
10.4M
        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
10.4M
            || (!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
10.4M
    } else {
272
8.60k
        if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
273
4.01k
            && s->statem.hand_state == TLS_ST_EARLY_DATA)
274
0
            ossl_statem_set_in_init(s, 1);
275
8.60k
    }
276
41.0M
    return 1;
277
41.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
54.3M
{
295
54.3M
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
296
297
54.3M
    if (sc == NULL)
298
0
        return -1;
299
300
54.3M
    return state_machine(sc, 0);
301
54.3M
}
302
303
int ossl_statem_accept(SSL *s)
304
118k
{
305
118k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
306
307
118k
    if (sc == NULL)
308
0
        return -1;
309
310
118k
    return state_machine(sc, 1);
311
118k
}
312
313
typedef void (*info_cb)(const SSL *, int, int);
314
315
static info_cb get_callback(SSL_CONNECTION *s)
316
129M
{
317
129M
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
318
319
129M
    if (s->info_callback != NULL)
320
0
        return s->info_callback;
321
129M
    else if (sctx->info_callback != NULL)
322
0
        return sctx->info_callback;
323
324
129M
    return NULL;
325
129M
}
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
47.9M
{
357
47.9M
    BUF_MEM *buf = NULL;
358
47.9M
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
359
47.9M
    OSSL_STATEM *st = &s->statem;
360
47.9M
    int ret = -1;
361
47.9M
    int ssret;
362
47.9M
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
363
47.9M
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
364
365
47.9M
    if (st->state == MSG_FLOW_ERROR) {
366
        /* Shouldn't have been called if we're already in the error state */
367
2.72k
        return -1;
368
2.72k
    }
369
370
47.9M
    ERR_clear_error();
371
47.9M
    clear_sys_error();
372
373
47.9M
    cb = get_callback(s);
374
375
47.9M
    st->in_handshake++;
376
47.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
114k
        if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(ssl))
382
0
            return -1;
383
114k
    }
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
47.9M
    if (st->state == MSG_FLOW_UNINITED
397
47.8M
        || st->state == MSG_FLOW_FINISHED) {
398
157k
        if (st->state == MSG_FLOW_UNINITED) {
399
114k
            st->hand_state = TLS_ST_BEFORE;
400
114k
            st->request_state = TLS_ST_BEFORE;
401
114k
        }
402
403
157k
        s->server = server;
404
157k
        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
157k
        if (SSL_CONNECTION_IS_DTLS(s)) {
416
33.6k
            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
124k
        } else {
421
124k
            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
124k
        }
426
427
157k
        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
157k
        if (s->init_buf == NULL) {
433
157k
            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
157k
            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
157k
            s->init_buf = buf;
442
157k
            buf = NULL;
443
157k
        }
444
445
157k
        s->init_num = 0;
446
447
        /*
448
         * Should have been reset by tls_process_finished, too.
449
         */
450
157k
        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
157k
            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
157k
        if ((SSL_in_before(ssl))
465
114k
            || s->renegotiate) {
466
114k
            if (!tls_setup_handshake(s)) {
467
                /* SSLfatal() already called */
468
0
                goto end;
469
0
            }
470
471
114k
            if (SSL_IS_FIRST_HANDSHAKE(s))
472
114k
                st->read_state_first_init = 1;
473
114k
        }
474
475
157k
        st->state = MSG_FLOW_WRITING;
476
157k
        init_write_state_machine(s);
477
157k
    }
478
479
48.2M
    while (st->state != MSG_FLOW_FINISHED) {
480
48.2M
        if (st->state == MSG_FLOW_READING) {
481
47.9M
            ssret = read_state_machine(s);
482
47.9M
            if (ssret == SUB_STATE_FINISHED) {
483
81.3k
                st->state = MSG_FLOW_WRITING;
484
81.3k
                init_write_state_machine(s);
485
47.9M
            } else {
486
                /* NBIO or error */
487
47.9M
                goto end;
488
47.9M
            }
489
47.9M
        } else if (st->state == MSG_FLOW_WRITING) {
490
239k
            ssret = write_state_machine(s);
491
239k
            if (ssret == SUB_STATE_FINISHED) {
492
188k
                st->state = MSG_FLOW_READING;
493
188k
                init_read_state_machine(s);
494
188k
            } else if (ssret == SUB_STATE_END_HANDSHAKE) {
495
49.4k
                st->state = MSG_FLOW_FINISHED;
496
49.4k
            } else {
497
                /* NBIO or error */
498
676
                goto end;
499
676
            }
500
239k
        } 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
48.2M
    }
507
508
49.4k
    ret = 1;
509
510
47.9M
end:
511
47.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
47.9M
    BUF_MEM_free(buf);
525
47.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
47.9M
    return ret;
532
49.4k
}
533
534
/*
535
 * Initialise the MSG_FLOW_READING sub-state machine
536
 */
537
static void init_read_state_machine(SSL_CONNECTION *s)
538
305k
{
539
305k
    OSSL_STATEM *st = &s->statem;
540
541
305k
    st->read_state = READ_STATE_HEADER;
542
305k
}
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
30.4M
{
572
30.4M
    OSSL_STATEM *st = &s->statem;
573
30.4M
    int ret, mt;
574
30.4M
    size_t len = 0;
575
30.4M
    int (*transition)(SSL_CONNECTION *s, int mt);
576
30.4M
    PACKET pkt;
577
30.4M
    MSG_PROCESS_RETURN (*process_message)(SSL_CONNECTION *s, PACKET *pkt);
578
30.4M
    WORK_STATE (*post_process_message)(SSL_CONNECTION *s, WORK_STATE wst);
579
30.4M
    size_t (*max_message_size)(SSL_CONNECTION *s);
580
30.4M
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
581
30.4M
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
582
583
30.4M
    cb = get_callback(s);
584
585
30.4M
    if (s->server) {
586
63.3k
        transition = ossl_statem_server_read_transition;
587
63.3k
        process_message = ossl_statem_server_process_message;
588
63.3k
        max_message_size = ossl_statem_server_max_message_size;
589
63.3k
        post_process_message = ossl_statem_server_post_process_message;
590
30.4M
    } else {
591
30.4M
        transition = ossl_statem_client_read_transition;
592
30.4M
        process_message = ossl_statem_client_process_message;
593
30.4M
        max_message_size = ossl_statem_client_max_message_size;
594
30.4M
        post_process_message = ossl_statem_client_post_process_message;
595
30.4M
    }
596
597
30.4M
    if (st->read_state_first_init) {
598
58.4k
        s->first_packet = 1;
599
58.4k
        st->read_state_first_init = 0;
600
58.4k
    }
601
602
30.5M
    while (1) {
603
30.5M
        switch (st->read_state) {
604
23.1M
        case READ_STATE_HEADER:
605
            /* Get the state the peer wants to move to */
606
23.1M
            if (SSL_CONNECTION_IS_DTLS(s)) {
607
                /*
608
                 * In DTLS we get the whole message in one go - header and body
609
                 */
610
41.7k
                ret = dtls_get_message(s, &mt);
611
23.0M
            } else {
612
23.0M
                ret = tls_get_message_header(s, &mt);
613
23.0M
            }
614
615
23.1M
            if (ret == 0) {
616
                /* Could be non-blocking IO */
617
22.9M
                return SUB_STATE_ERROR;
618
22.9M
            }
619
620
150k
            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
150k
            if (!transition(s, mt))
632
2.74k
                return SUB_STATE_ERROR;
633
634
147k
            if (s->s3.tmp.message_size > max_message_size(s)) {
635
336
                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
636
336
                    SSL_R_EXCESSIVE_MESSAGE_SIZE);
637
336
                return SUB_STATE_ERROR;
638
336
            }
639
640
147k
            st->read_state = READ_STATE_BODY;
641
            /* Fall through */
642
643
7.55M
        case READ_STATE_BODY:
644
7.55M
            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.7k
                ret = dtls_get_message_body(s, &len);
650
7.52M
            } else {
651
7.52M
                ret = tls_get_message_body(s, &len);
652
7.52M
            }
653
7.55M
            if (ret == 0) {
654
                /* Could be non-blocking IO */
655
7.41M
                return SUB_STATE_ERROR;
656
7.41M
            }
657
658
144k
            s->first_packet = 0;
659
144k
            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
144k
            ret = process_message(s, &pkt);
664
665
            /* Discard the packet data */
666
144k
            s->init_num = 0;
667
668
144k
            switch (ret) {
669
15.0k
            case MSG_PROCESS_ERROR:
670
15.0k
                check_fatal(s);
671
15.0k
                return SUB_STATE_ERROR;
672
673
47.8k
            case MSG_PROCESS_FINISHED_READING:
674
47.8k
                if (SSL_CONNECTION_IS_DTLS(s)) {
675
2.37k
                    dtls1_stop_timer(s);
676
2.37k
                }
677
47.8k
                return SUB_STATE_FINISHED;
678
679
31.7k
            case MSG_PROCESS_CONTINUE_PROCESSING:
680
31.7k
                st->read_state = READ_STATE_POST_PROCESS;
681
31.7k
                st->read_state_work = WORK_MORE_A;
682
31.7k
                break;
683
684
49.7k
            default:
685
49.7k
                st->read_state = READ_STATE_HEADER;
686
49.7k
                break;
687
144k
            }
688
81.5k
            break;
689
690
81.5k
        case READ_STATE_POST_PROCESS:
691
31.7k
            st->read_state_work = post_process_message(s, st->read_state_work);
692
31.7k
            switch (st->read_state_work) {
693
4.84k
            case WORK_ERROR:
694
4.84k
                check_fatal(s);
695
                /* Fall through */
696
4.84k
            case WORK_MORE_A:
697
4.84k
            case WORK_MORE_B:
698
4.84k
            case WORK_MORE_C:
699
4.84k
                return SUB_STATE_ERROR;
700
701
17.3k
            case WORK_FINISHED_CONTINUE:
702
17.3k
                st->read_state = READ_STATE_HEADER;
703
17.3k
                break;
704
705
0
            case WORK_FINISHED_SWAP:
706
9.60k
            case WORK_FINISHED_STOP:
707
9.60k
                if (SSL_CONNECTION_IS_DTLS(s)) {
708
4.29k
                    dtls1_stop_timer(s);
709
4.29k
                }
710
9.60k
                return SUB_STATE_FINISHED;
711
31.7k
            }
712
17.3k
            break;
713
714
17.3k
        default:
715
            /* Shouldn't happen */
716
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
717
0
            return SUB_STATE_ERROR;
718
30.5M
        }
719
30.5M
    }
720
30.4M
}
721
722
/*
723
 * Send a previously constructed message to the peer.
724
 */
725
static int statem_do_write(SSL_CONNECTION *s)
726
192k
{
727
192k
    OSSL_STATEM *st = &s->statem;
728
729
192k
    if (st->hand_state == TLS_ST_CW_CHANGE
730
182k
        || st->hand_state == TLS_ST_SW_CHANGE) {
731
13.6k
        if (SSL_CONNECTION_IS_DTLS(s))
732
3.97k
            return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
733
9.63k
        else
734
9.63k
            return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
735
178k
    } else {
736
178k
        return ssl_do_write(s);
737
178k
    }
738
192k
}
739
740
/*
741
 * Initialise the MSG_FLOW_WRITING sub-state machine
742
 */
743
static void init_write_state_machine(SSL_CONNECTION *s)
744
397k
{
745
397k
    OSSL_STATEM *st = &s->statem;
746
747
397k
    st->write_state = WRITE_STATE_TRANSITION;
748
397k
}
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
347k
{
783
347k
    OSSL_STATEM *st = &s->statem;
784
347k
    int ret;
785
347k
    WRITE_TRAN (*transition)(SSL_CONNECTION *s);
786
347k
    WORK_STATE (*pre_work)(SSL_CONNECTION *s, WORK_STATE wst);
787
347k
    WORK_STATE (*post_work)(SSL_CONNECTION *s, WORK_STATE wst);
788
347k
    int (*get_construct_message_f)(SSL_CONNECTION *s,
789
347k
        CON_FUNC_RETURN (**confunc)(SSL_CONNECTION *s,
790
347k
            WPACKET *pkt),
791
347k
        int *mt);
792
347k
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
793
347k
    CON_FUNC_RETURN (*confunc)(SSL_CONNECTION *s, WPACKET *pkt);
794
347k
    int mt;
795
347k
    WPACKET pkt;
796
347k
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
797
798
347k
    cb = get_callback(s);
799
800
347k
    if (s->server) {
801
210k
        transition = ossl_statem_server_write_transition;
802
210k
        pre_work = ossl_statem_server_pre_work;
803
210k
        post_work = ossl_statem_server_post_work;
804
210k
        get_construct_message_f = ossl_statem_server_construct_message;
805
210k
    } else {
806
137k
        transition = ossl_statem_client_write_transition;
807
137k
        pre_work = ossl_statem_client_pre_work;
808
137k
        post_work = ossl_statem_client_post_work;
809
137k
        get_construct_message_f = ossl_statem_client_construct_message;
810
137k
    }
811
812
925k
    while (1) {
813
925k
        switch (st->write_state) {
814
591k
        case WRITE_STATE_TRANSITION:
815
591k
            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
591k
            switch (transition(s)) {
823
331k
            case WRITE_TRAN_CONTINUE:
824
331k
                st->write_state = WRITE_STATE_PRE_WORK;
825
331k
                st->write_state_work = WORK_MORE_A;
826
331k
                break;
827
828
260k
            case WRITE_TRAN_FINISHED:
829
260k
                return SUB_STATE_FINISHED;
830
831
0
            case WRITE_TRAN_ERROR:
832
0
                check_fatal(s);
833
0
                return SUB_STATE_ERROR;
834
591k
            }
835
331k
            break;
836
837
331k
        case WRITE_STATE_PRE_WORK:
838
331k
            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
245k
            case WORK_FINISHED_CONTINUE:
848
245k
                st->write_state = WRITE_STATE_SEND;
849
245k
                break;
850
851
0
            case WORK_FINISHED_SWAP:
852
0
                return SUB_STATE_FINISHED;
853
854
86.5k
            case WORK_FINISHED_STOP:
855
86.5k
                return SUB_STATE_END_HANDSHAKE;
856
331k
            }
857
245k
            if (!get_construct_message_f(s, &confunc, &mt)) {
858
                /* SSLfatal() already called */
859
0
                return SUB_STATE_ERROR;
860
0
            }
861
245k
            if (mt == SSL3_MT_DUMMY) {
862
                /* Skip construction and sending. This isn't a "real" state */
863
1.79k
                st->write_state = WRITE_STATE_POST_WORK;
864
1.79k
                st->write_state_work = WORK_MORE_A;
865
1.79k
                break;
866
1.79k
            }
867
243k
            if (!WPACKET_init(&pkt, s->init_buf)
868
243k
                || !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
243k
            if (confunc != NULL) {
874
243k
                CON_FUNC_RETURN tmpret;
875
876
243k
                tmpret = confunc(s, &pkt);
877
243k
                if (tmpret == CON_FUNC_ERROR) {
878
979
                    WPACKET_cleanup(&pkt);
879
979
                    check_fatal(s);
880
979
                    return SUB_STATE_ERROR;
881
242k
                } 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
243k
            }
892
242k
            if (!ssl_close_construct_packet(s, &pkt, mt)
893
242k
                || !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
242k
        case WRITE_STATE_SEND:
902
242k
            if (SSL_CONNECTION_IS_DTLS(s) && st->use_timer) {
903
81.4k
                dtls1_start_timer(s);
904
81.4k
            }
905
242k
            ret = statem_do_write(s);
906
242k
            if (ret <= 0) {
907
0
                return SUB_STATE_ERROR;
908
0
            }
909
242k
            st->write_state = WRITE_STATE_POST_WORK;
910
242k
            st->write_state_work = WORK_MORE_A;
911
            /* Fall through */
912
913
244k
        case WRITE_STATE_POST_WORK:
914
244k
            switch (st->write_state_work = post_work(s, st->write_state_work)) {
915
2
            case WORK_ERROR:
916
2
                check_fatal(s);
917
                /* Fall through */
918
2
            case WORK_MORE_A:
919
2
            case WORK_MORE_B:
920
2
            case WORK_MORE_C:
921
2
                return SUB_STATE_ERROR;
922
923
244k
            case WORK_FINISHED_CONTINUE:
924
244k
                st->write_state = WRITE_STATE_TRANSITION;
925
244k
                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
244k
            }
933
244k
            break;
934
935
244k
        default:
936
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
937
0
            return SUB_STATE_ERROR;
938
925k
        }
939
925k
    }
940
347k
}
941
942
/*
943
 * Flush the write BIO
944
 */
945
int statem_flush(SSL_CONNECTION *s)
946
187k
{
947
187k
    s->rwstate = SSL_WRITING;
948
187k
    if (BIO_flush(s->wbio) <= 0) {
949
0
        return 0;
950
0
    }
951
187k
    s->rwstate = SSL_NOTHING;
952
953
187k
    return 1;
954
187k
}
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
7.72k
{
966
7.72k
    OSSL_STATEM *st = &s->statem;
967
968
7.72k
    if (st->state == MSG_FLOW_UNINITED)
969
0
        return 0;
970
971
7.72k
    if (!s->s3.in_read_app_data || (s->s3.total_renegotiations == 0))
972
3.43k
        return 0;
973
974
4.29k
    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
4.29k
    } else {
983
        /*
984
         * If we're a client and we haven't read the ServerHello yet then we
985
         * allow app data
986
         */
987
4.29k
        if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
988
4.25k
            return 1;
989
4.29k
    }
990
991
38
    return 0;
992
4.29k
}
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
}