Coverage Report

Created: 2026-04-01 06:39

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