Coverage Report

Created: 2026-07-23 06:28

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