Coverage Report

Created: 2026-05-24 07:14

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-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "internal/e_os.h"
11
12
#if defined(__TANDEM) && defined(_SPT_MODEL_)
13
#include <spthread.h>
14
#include <spt_extensions.h> /* timeval */
15
#endif
16
17
#include "internal/cryptlib.h"
18
#include "internal/ssl_unwrap.h"
19
#include <openssl/rand.h>
20
#include "../ssl_local.h"
21
#include "statem_local.h"
22
#include <assert.h>
23
24
/*
25
 * This file implements the SSL/TLS/DTLS state machines.
26
 *
27
 * There are two primary state machines:
28
 *
29
 * 1) Message flow state machine
30
 * 2) Handshake state machine
31
 *
32
 * The Message flow state machine controls the reading and sending of messages
33
 * including handling of non-blocking IO events, flushing of the underlying
34
 * write BIO, handling unexpected messages, etc. It is itself broken into two
35
 * separate sub-state machines which control reading and writing respectively.
36
 *
37
 * The Handshake state machine keeps track of the current SSL/TLS handshake
38
 * state. Transitions of the handshake state are the result of events that
39
 * occur within the Message flow state machine.
40
 *
41
 * Overall it looks like this:
42
 *
43
 * ---------------------------------------------            -------------------
44
 * |                                           |            |                 |
45
 * | Message flow state machine                |            |                 |
46
 * |                                           |            |                 |
47
 * | -------------------- -------------------- | Transition | Handshake state |
48
 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event      | machine         |
49
 * | | sub-state        | | sub-state        | |----------->|                 |
50
 * | | machine for      | | machine for      | |            |                 |
51
 * | | reading messages | | writing messages | |            |                 |
52
 * | -------------------- -------------------- |            |                 |
53
 * |                                           |            |                 |
54
 * ---------------------------------------------            -------------------
55
 *
56
 */
57
58
/* Sub state machine return values */
59
typedef enum {
60
    /* Something bad happened or NBIO */
61
    SUB_STATE_ERROR,
62
    /* Sub state finished go to the next sub state */
63
    SUB_STATE_FINISHED,
64
    /* Sub state finished and handshake was completed */
65
    SUB_STATE_END_HANDSHAKE
66
} SUB_STATE_RETURN;
67
68
static int state_machine(SSL_CONNECTION *s, int server);
69
static void init_read_state_machine(SSL_CONNECTION *s);
70
static SUB_STATE_RETURN read_state_machine(SSL_CONNECTION *s);
71
static void init_write_state_machine(SSL_CONNECTION *s);
72
static SUB_STATE_RETURN write_state_machine(SSL_CONNECTION *s);
73
74
OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
75
201k
{
76
201k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
77
78
201k
    if (sc == NULL)
79
0
        return TLS_ST_BEFORE;
80
81
201k
    return sc->statem.hand_state;
82
201k
}
83
84
int SSL_in_init(const SSL *s)
85
90.4M
{
86
90.4M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
87
88
90.4M
    if (sc == NULL)
89
0
        return 0;
90
91
90.4M
    return sc->statem.in_init;
92
90.4M
}
93
94
int SSL_is_init_finished(const SSL *s)
95
162k
{
96
162k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
97
98
162k
    if (sc == NULL)
99
0
        return 0;
100
101
162k
    return !(sc->statem.in_init) && (sc->statem.hand_state == TLS_ST_OK);
102
162k
}
103
104
int SSL_in_before(const SSL *s)
105
43.2M
{
106
43.2M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
107
108
43.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
43.2M
    return (sc->statem.hand_state == TLS_ST_BEFORE)
119
195k
        && (sc->statem.state == MSG_FLOW_UNINITED);
120
43.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
522k
{
132
522k
    s->statem.state = MSG_FLOW_UNINITED;
133
522k
    s->statem.hand_state = TLS_ST_BEFORE;
134
522k
    ossl_statem_set_in_init(s, 1);
135
522k
    s->statem.no_cert_verify = 0;
136
522k
}
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
70.7k
{
149
    /* We shouldn't call SSLfatal() twice. Once is enough */
150
70.7k
    if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR)
151
7
        return;
152
70.7k
    ossl_statem_set_in_init(s, 1);
153
70.7k
    s->statem.state = MSG_FLOW_ERROR;
154
70.7k
    if (al != SSL_AD_NO_ALERT && s->rlayer.wrlmethod != NULL)
155
69.7k
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
156
70.7k
}
157
158
/*
159
 * Error reporting building block that's used instead of ERR_set_error().
160
 * In addition to what ERR_set_error() does, this puts the state machine
161
 * into an error state and sends an alert if appropriate.
162
 * This is a permanent error for the current connection.
163
 */
164
void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason,
165
    const char *fmt, ...)
166
87.6k
{
167
87.6k
    va_list args;
168
169
87.6k
    va_start(args, fmt);
170
87.6k
    ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
171
87.6k
    va_end(args);
172
173
87.6k
    ossl_statem_send_fatal(s, al);
174
87.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
22.8k
    do {                                                             \
183
22.8k
        if (!ossl_assert((s)->statem.in_init                         \
184
22.8k
                && (s)->statem.state == MSG_FLOW_ERROR))             \
185
22.8k
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
186
22.8k
    } 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
335k
{
197
335k
    if (s->statem.state == MSG_FLOW_ERROR)
198
20
        return 1;
199
200
335k
    return 0;
201
335k
}
202
203
void ossl_statem_set_in_init(SSL_CONNECTION *s, int init)
204
720k
{
205
720k
    s->statem.in_init = init;
206
720k
    if (s->rlayer.rrlmethod != NULL && s->rlayer.rrlmethod->set_in_init != NULL)
207
124k
        s->rlayer.rrlmethod->set_in_init(s->rlayer.rrl, init);
208
720k
}
209
210
int ossl_statem_get_in_handshake(SSL_CONNECTION *s)
211
97.1M
{
212
97.1M
    return s->statem.in_handshake;
213
97.1M
}
214
215
void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand)
216
1.06k
{
217
1.06k
    if (inhand)
218
533
        s->statem.in_handshake++;
219
533
    else
220
533
        s->statem.in_handshake--;
221
1.06k
}
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.42k
        return 0;
228
229
130k
    if (!s->server
230
130k
        || s->statem.hand_state != TLS_ST_EARLY_DATA
231
130k
        || s->hello_retry_request == SSL_HRR_COMPLETE)
232
30
        return 0;
233
234
130k
    return 1;
235
130k
}
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
46.5M
{
247
46.5M
    if (sending == -1) {
248
33.3M
        if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
249
33.3M
            || s->statem.hand_state == TLS_ST_EARLY_DATA) {
250
711
            ossl_statem_set_in_init(s, 1);
251
711
            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
711
        }
259
33.3M
    } else if (!s->server) {
260
13.2M
        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
13.2M
            || (!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
13.2M
    } else {
272
17.5k
        if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
273
8.01k
            && s->statem.hand_state == TLS_ST_EARLY_DATA)
274
0
            ossl_statem_set_in_init(s, 1);
275
17.5k
    }
276
46.5M
    return 1;
277
46.5M
}
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
43.0M
{
295
43.0M
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
296
297
43.0M
    if (sc == NULL)
298
0
        return -1;
299
300
43.0M
    return state_machine(sc, 0);
301
43.0M
}
302
303
int ossl_statem_accept(SSL *s)
304
93.0k
{
305
93.0k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
306
307
93.0k
    if (sc == NULL)
308
0
        return -1;
309
310
93.0k
    return state_machine(sc, 1);
311
93.0k
}
312
313
typedef void (*info_cb)(const SSL *, int, int);
314
315
static info_cb get_callback(SSL_CONNECTION *s)
316
147M
{
317
147M
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
318
319
147M
    if (s->info_callback != NULL)
320
0
        return s->info_callback;
321
147M
    else if (sctx->info_callback != NULL)
322
0
        return sctx->info_callback;
323
324
147M
    return NULL;
325
147M
}
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
73.4M
{
357
73.4M
    BUF_MEM *buf = NULL;
358
73.4M
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
359
73.4M
    OSSL_STATEM *st = &s->statem;
360
73.4M
    int ret = -1;
361
73.4M
    int ssret;
362
73.4M
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
363
73.4M
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
364
365
73.4M
    if (st->state == MSG_FLOW_ERROR) {
366
        /* Shouldn't have been called if we're already in the error state */
367
4.22k
        return -1;
368
4.22k
    }
369
370
73.4M
    ERR_clear_error();
371
73.4M
    clear_sys_error();
372
373
73.4M
    cb = get_callback(s);
374
375
73.4M
    st->in_handshake++;
376
73.4M
    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
162k
        if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(ssl))
382
0
            return -1;
383
162k
    }
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
73.4M
    if (st->state == MSG_FLOW_UNINITED
397
73.2M
        || st->state == MSG_FLOW_FINISHED) {
398
233k
        if (st->state == MSG_FLOW_UNINITED) {
399
162k
            st->hand_state = TLS_ST_BEFORE;
400
162k
            st->request_state = TLS_ST_BEFORE;
401
162k
        }
402
403
233k
        s->server = server;
404
233k
        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
233k
        if (SSL_CONNECTION_IS_DTLS(s)) {
416
49.9k
            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
183k
        } else {
421
183k
            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
183k
        }
426
427
233k
        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
233k
        if (s->init_buf == NULL) {
433
233k
            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
233k
            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
233k
            s->init_buf = buf;
442
233k
            buf = NULL;
443
233k
        }
444
445
233k
        s->init_num = 0;
446
447
        /*
448
         * Should have been reset by tls_process_finished, too.
449
         */
450
233k
        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
233k
            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
233k
        if ((SSL_in_before(ssl))
465
162k
            || s->renegotiate) {
466
162k
            if (!tls_setup_handshake(s)) {
467
                /* SSLfatal() already called */
468
0
                goto end;
469
0
            }
470
471
162k
            if (SSL_IS_FIRST_HANDSHAKE(s))
472
162k
                st->read_state_first_init = 1;
473
162k
        }
474
475
233k
        st->state = MSG_FLOW_WRITING;
476
233k
        init_write_state_machine(s);
477
233k
    }
478
479
73.8M
    while (st->state != MSG_FLOW_FINISHED) {
480
73.8M
        if (st->state == MSG_FLOW_READING) {
481
73.4M
            ssret = read_state_machine(s);
482
73.4M
            if (ssret == SUB_STATE_FINISHED) {
483
130k
                st->state = MSG_FLOW_WRITING;
484
130k
                init_write_state_machine(s);
485
73.3M
            } else {
486
                /* NBIO or error */
487
73.3M
                goto end;
488
73.3M
            }
489
73.4M
        } else if (st->state == MSG_FLOW_WRITING) {
490
363k
            ssret = write_state_machine(s);
491
363k
            if (ssret == SUB_STATE_FINISHED) {
492
281k
                st->state = MSG_FLOW_READING;
493
281k
                init_read_state_machine(s);
494
281k
            } else if (ssret == SUB_STATE_END_HANDSHAKE) {
495
80.8k
                st->state = MSG_FLOW_FINISHED;
496
80.8k
            } else {
497
                /* NBIO or error */
498
1.71k
                goto end;
499
1.71k
            }
500
363k
        } 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
73.8M
    }
507
508
80.8k
    ret = 1;
509
510
73.4M
end:
511
73.4M
    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
73.4M
    BUF_MEM_free(buf);
525
73.4M
    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
73.4M
    return ret;
532
80.8k
}
533
534
/*
535
 * Initialise the MSG_FLOW_READING sub-state machine
536
 */
537
static void init_read_state_machine(SSL_CONNECTION *s)
538
296k
{
539
296k
    OSSL_STATEM *st = &s->statem;
540
541
296k
    st->read_state = READ_STATE_HEADER;
542
296k
}
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.9M
{
572
31.9M
    OSSL_STATEM *st = &s->statem;
573
31.9M
    int ret, mt;
574
31.9M
    size_t len = 0;
575
31.9M
    int (*transition)(SSL_CONNECTION *s, int mt);
576
31.9M
    PACKET pkt;
577
31.9M
    MSG_PROCESS_RETURN (*process_message)(SSL_CONNECTION *s, PACKET *pkt);
578
31.9M
    WORK_STATE (*post_process_message)(SSL_CONNECTION *s, WORK_STATE wst);
579
31.9M
    size_t (*max_message_size)(SSL_CONNECTION *s);
580
31.9M
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
581
31.9M
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
582
583
31.9M
    cb = get_callback(s);
584
585
31.9M
    if (s->server) {
586
60.8k
        transition = ossl_statem_server_read_transition;
587
60.8k
        process_message = ossl_statem_server_process_message;
588
60.8k
        max_message_size = ossl_statem_server_max_message_size;
589
60.8k
        post_process_message = ossl_statem_server_post_process_message;
590
31.8M
    } else {
591
31.8M
        transition = ossl_statem_client_read_transition;
592
31.8M
        process_message = ossl_statem_client_process_message;
593
31.8M
        max_message_size = ossl_statem_client_max_message_size;
594
31.8M
        post_process_message = ossl_statem_client_post_process_message;
595
31.8M
    }
596
597
31.9M
    if (st->read_state_first_init) {
598
63.6k
        s->first_packet = 1;
599
63.6k
        st->read_state_first_init = 0;
600
63.6k
    }
601
602
32.0M
    while (1) {
603
32.0M
        switch (st->read_state) {
604
25.1M
        case READ_STATE_HEADER:
605
            /* Get the state the peer wants to move to */
606
25.1M
            if (SSL_CONNECTION_IS_DTLS(s)) {
607
                /*
608
                 * In DTLS we get the whole message in one go - header and body
609
                 */
610
44.9k
                ret = dtls_get_message(s, &mt);
611
25.0M
            } else {
612
25.0M
                ret = tls_get_message_header(s, &mt);
613
25.0M
            }
614
615
25.1M
            if (ret == 0) {
616
                /* Could be non-blocking IO */
617
24.9M
                return SUB_STATE_ERROR;
618
24.9M
            }
619
620
156k
            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
156k
            if (!transition(s, mt))
632
3.10k
                return SUB_STATE_ERROR;
633
634
153k
            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
153k
            st->read_state = READ_STATE_BODY;
641
            /* Fall through */
642
643
6.99M
        case READ_STATE_BODY:
644
6.99M
            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
33.4k
                ret = dtls_get_message_body(s, &len);
650
6.96M
            } else {
651
6.96M
                ret = tls_get_message_body(s, &len);
652
6.96M
            }
653
6.99M
            if (ret == 0) {
654
                /* Could be non-blocking IO */
655
6.84M
                return SUB_STATE_ERROR;
656
6.84M
            }
657
658
150k
            s->first_packet = 0;
659
150k
            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
150k
            ret = process_message(s, &pkt);
664
665
            /* Discard the packet data */
666
150k
            s->init_num = 0;
667
668
150k
            switch (ret) {
669
17.0k
            case MSG_PROCESS_ERROR:
670
17.0k
                check_fatal(s);
671
17.0k
                return SUB_STATE_ERROR;
672
673
43.6k
            case MSG_PROCESS_FINISHED_READING:
674
43.6k
                if (SSL_CONNECTION_IS_DTLS(s)) {
675
2.35k
                    dtls1_stop_timer(s);
676
2.35k
                }
677
43.6k
                return SUB_STATE_FINISHED;
678
679
35.5k
            case MSG_PROCESS_CONTINUE_PROCESSING:
680
35.5k
                st->read_state = READ_STATE_POST_PROCESS;
681
35.5k
                st->read_state_work = WORK_MORE_A;
682
35.5k
                break;
683
684
54.1k
            default:
685
54.1k
                st->read_state = READ_STATE_HEADER;
686
54.1k
                break;
687
150k
            }
688
89.6k
            break;
689
690
89.6k
        case READ_STATE_POST_PROCESS:
691
35.5k
            st->read_state_work = post_process_message(s, st->read_state_work);
692
35.5k
            switch (st->read_state_work) {
693
5.13k
            case WORK_ERROR:
694
5.13k
                check_fatal(s);
695
                /* Fall through */
696
5.13k
            case WORK_MORE_A:
697
5.13k
            case WORK_MORE_B:
698
5.13k
            case WORK_MORE_C:
699
5.13k
                return SUB_STATE_ERROR;
700
701
19.8k
            case WORK_FINISHED_CONTINUE:
702
19.8k
                st->read_state = READ_STATE_HEADER;
703
19.8k
                break;
704
705
0
            case WORK_FINISHED_SWAP:
706
10.5k
            case WORK_FINISHED_STOP:
707
10.5k
                if (SSL_CONNECTION_IS_DTLS(s)) {
708
4.76k
                    dtls1_stop_timer(s);
709
4.76k
                }
710
10.5k
                return SUB_STATE_FINISHED;
711
35.5k
            }
712
19.8k
            break;
713
714
19.8k
        default:
715
            /* Shouldn't happen */
716
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
717
0
            return SUB_STATE_ERROR;
718
32.0M
        }
719
32.0M
    }
720
31.9M
}
721
722
/*
723
 * Send a previously constructed message to the peer.
724
 */
725
static int statem_do_write(SSL_CONNECTION *s)
726
303k
{
727
303k
    OSSL_STATEM *st = &s->statem;
728
729
303k
    if (st->hand_state == TLS_ST_CW_CHANGE
730
287k
        || st->hand_state == TLS_ST_SW_CHANGE) {
731
22.2k
        if (SSL_CONNECTION_IS_DTLS(s))
732
7.12k
            return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
733
15.1k
        else
734
15.1k
            return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
735
281k
    } else {
736
281k
        return ssl_do_write(s);
737
281k
    }
738
303k
}
739
740
/*
741
 * Initialise the MSG_FLOW_WRITING sub-state machine
742
 */
743
static void init_write_state_machine(SSL_CONNECTION *s)
744
379k
{
745
379k
    OSSL_STATEM *st = &s->statem;
746
747
379k
    st->write_state = WRITE_STATE_TRANSITION;
748
379k
}
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
257k
{
783
257k
    OSSL_STATEM *st = &s->statem;
784
257k
    int ret;
785
257k
    WRITE_TRAN (*transition)(SSL_CONNECTION *s);
786
257k
    WORK_STATE (*pre_work)(SSL_CONNECTION *s, WORK_STATE wst);
787
257k
    WORK_STATE (*post_work)(SSL_CONNECTION *s, WORK_STATE wst);
788
257k
    int (*get_construct_message_f)(SSL_CONNECTION *s,
789
257k
        CON_FUNC_RETURN (**confunc)(SSL_CONNECTION *s,
790
257k
            WPACKET *pkt),
791
257k
        int *mt);
792
257k
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
793
257k
    CON_FUNC_RETURN (*confunc)(SSL_CONNECTION *s, WPACKET *pkt);
794
257k
    int mt;
795
257k
    WPACKET pkt;
796
257k
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
797
798
257k
    cb = get_callback(s);
799
800
257k
    if (s->server) {
801
166k
        transition = ossl_statem_server_write_transition;
802
166k
        pre_work = ossl_statem_server_pre_work;
803
166k
        post_work = ossl_statem_server_post_work;
804
166k
        get_construct_message_f = ossl_statem_server_construct_message;
805
166k
    } else {
806
91.5k
        transition = ossl_statem_client_write_transition;
807
91.5k
        pre_work = ossl_statem_client_pre_work;
808
91.5k
        post_work = ossl_statem_client_post_work;
809
91.5k
        get_construct_message_f = ossl_statem_client_construct_message;
810
91.5k
    }
811
812
655k
    while (1) {
813
655k
        switch (st->write_state) {
814
419k
        case WRITE_STATE_TRANSITION:
815
419k
            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
419k
            switch (transition(s)) {
823
234k
            case WRITE_TRAN_CONTINUE:
824
234k
                st->write_state = WRITE_STATE_PRE_WORK;
825
234k
                st->write_state_work = WORK_MORE_A;
826
234k
                break;
827
828
185k
            case WRITE_TRAN_FINISHED:
829
185k
                return SUB_STATE_FINISHED;
830
831
0
            case WRITE_TRAN_ERROR:
832
0
                check_fatal(s);
833
0
                return SUB_STATE_ERROR;
834
419k
            }
835
234k
            break;
836
837
234k
        case WRITE_STATE_PRE_WORK:
838
234k
            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
163k
            case WORK_FINISHED_CONTINUE:
848
163k
                st->write_state = WRITE_STATE_SEND;
849
163k
                break;
850
851
0
            case WORK_FINISHED_SWAP:
852
0
                return SUB_STATE_FINISHED;
853
854
70.9k
            case WORK_FINISHED_STOP:
855
70.9k
                return SUB_STATE_END_HANDSHAKE;
856
234k
            }
857
163k
            if (!get_construct_message_f(s, &confunc, &mt)) {
858
                /* SSLfatal() already called */
859
0
                return SUB_STATE_ERROR;
860
0
            }
861
163k
            if (mt == SSL3_MT_DUMMY) {
862
                /* Skip construction and sending. This isn't a "real" state */
863
1.18k
                st->write_state = WRITE_STATE_POST_WORK;
864
1.18k
                st->write_state_work = WORK_MORE_A;
865
1.18k
                break;
866
1.18k
            }
867
161k
            if (!WPACKET_init(&pkt, s->init_buf)
868
161k
                || !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
161k
            if (confunc != NULL) {
874
161k
                CON_FUNC_RETURN tmpret;
875
876
161k
                tmpret = confunc(s, &pkt);
877
161k
                if (tmpret == CON_FUNC_ERROR) {
878
700
                    WPACKET_cleanup(&pkt);
879
700
                    check_fatal(s);
880
700
                    return SUB_STATE_ERROR;
881
161k
                } 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
161k
            }
892
161k
            if (!ssl_close_construct_packet(s, &pkt, mt)
893
161k
                || !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
161k
        case WRITE_STATE_SEND:
902
161k
            if (SSL_CONNECTION_IS_DTLS(s) && st->use_timer) {
903
51.5k
                dtls1_start_timer(s);
904
51.5k
            }
905
161k
            ret = statem_do_write(s);
906
161k
            if (ret <= 0) {
907
0
                return SUB_STATE_ERROR;
908
0
            }
909
161k
            st->write_state = WRITE_STATE_POST_WORK;
910
161k
            st->write_state_work = WORK_MORE_A;
911
            /* Fall through */
912
913
162k
        case WRITE_STATE_POST_WORK:
914
162k
            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
162k
            case WORK_FINISHED_CONTINUE:
924
162k
                st->write_state = WRITE_STATE_TRANSITION;
925
162k
                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
162k
            }
933
162k
            break;
934
935
162k
        default:
936
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
937
0
            return SUB_STATE_ERROR;
938
655k
        }
939
655k
    }
940
257k
}
941
942
/*
943
 * Flush the write BIO
944
 */
945
int statem_flush(SSL_CONNECTION *s)
946
189k
{
947
189k
    s->rwstate = SSL_WRITING;
948
189k
    if (BIO_flush(s->wbio) <= 0) {
949
0
        return 0;
950
0
    }
951
189k
    s->rwstate = SSL_NOTHING;
952
953
189k
    return 1;
954
189k
}
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
3.85k
{
966
3.85k
    OSSL_STATEM *st = &s->statem;
967
968
3.85k
    if (st->state == MSG_FLOW_UNINITED)
969
0
        return 0;
970
971
3.85k
    if (!s->s3.in_read_app_data || (s->s3.total_renegotiations == 0))
972
3.27k
        return 0;
973
974
575
    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
575
    } else {
983
        /*
984
         * If we're a client and we haven't read the ServerHello yet then we
985
         * allow app data
986
         */
987
575
        if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
988
533
            return 1;
989
575
    }
990
991
42
    return 0;
992
575
}
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
}