Coverage Report

Created: 2025-06-13 06:58

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