Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/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
164k
{
73
164k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
74
75
164k
    if (sc == NULL)
76
0
        return TLS_ST_BEFORE;
77
78
164k
    return sc->statem.hand_state;
79
164k
}
80
81
int SSL_in_init(const SSL *s)
82
96.0M
{
83
96.0M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
84
85
96.0M
    if (sc == NULL)
86
0
        return 0;
87
88
96.0M
    return sc->statem.in_init;
89
96.0M
}
90
91
int SSL_is_init_finished(const SSL *s)
92
112k
{
93
112k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
94
95
112k
    if (sc == NULL)
96
0
        return 0;
97
98
112k
    return !(sc->statem.in_init) && (sc->statem.hand_state == TLS_ST_OK);
99
112k
}
100
101
int SSL_in_before(const SSL *s)
102
45.3M
{
103
45.3M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
104
105
45.3M
    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
45.3M
    return (sc->statem.hand_state == TLS_ST_BEFORE)
116
169k
        && (sc->statem.state == MSG_FLOW_UNINITED);
117
45.3M
}
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
453k
{
129
453k
    s->statem.state = MSG_FLOW_UNINITED;
130
453k
    s->statem.hand_state = TLS_ST_BEFORE;
131
453k
    ossl_statem_set_in_init(s, 1);
132
453k
    s->statem.no_cert_verify = 0;
133
453k
}
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.14k
{
140
1.14k
    ossl_statem_set_in_init(s, 1);
141
1.14k
    s->statem.request_state = TLS_ST_SW_HELLO_REQ;
142
1.14k
}
143
144
void ossl_statem_send_fatal(SSL_CONNECTION *s, int al)
145
69.2k
{
146
    /* We shouldn't call SSLfatal() twice. Once is enough */
147
69.2k
    if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR)
148
0
      return;
149
69.2k
    ossl_statem_set_in_init(s, 1);
150
69.2k
    s->statem.state = MSG_FLOW_ERROR;
151
69.2k
    if (al != SSL_AD_NO_ALERT)
152
69.2k
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
153
69.2k
}
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
77.6k
{
164
77.6k
    va_list args;
165
166
77.6k
    va_start(args, fmt);
167
77.6k
    ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
168
77.6k
    va_end(args);
169
170
77.6k
    ossl_statem_send_fatal(s, al);
171
77.6k
}
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
32.7k
    do { \
180
32.7k
        if (!ossl_assert((s)->statem.in_init \
181
32.7k
                         && (s)->statem.state == MSG_FLOW_ERROR)) \
182
32.7k
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
183
32.7k
    } 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
249k
{
194
249k
    if (s->statem.state == MSG_FLOW_ERROR)
195
24
        return 1;
196
197
249k
    return 0;
198
249k
}
199
200
void ossl_statem_set_in_init(SSL_CONNECTION *s, int init)
201
634k
{
202
634k
    s->statem.in_init = init;
203
634k
    if (s->rlayer.rrlmethod != NULL && s->rlayer.rrlmethod->set_in_init != NULL)
204
87.6k
        s->rlayer.rrlmethod->set_in_init(s->rlayer.rrl, init);
205
634k
}
206
207
int ossl_statem_get_in_handshake(SSL_CONNECTION *s)
208
87.7M
{
209
87.7M
    return s->statem.in_handshake;
210
87.7M
}
211
212
void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand)
213
408
{
214
408
    if (inhand)
215
204
        s->statem.in_handshake++;
216
204
    else
217
204
        s->statem.in_handshake--;
218
408
}
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
134k
{
223
134k
    if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
224
2.45k
        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
23
        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
50.2M
{
244
50.2M
    if (sending == -1) {
245
35.3M
        if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
246
35.3M
                || s->statem.hand_state == TLS_ST_EARLY_DATA) {
247
896
            ossl_statem_set_in_init(s, 1);
248
896
            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
896
        }
256
35.3M
    } else if (!s->server) {
257
14.8M
        if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
258
0
                      || s->statem.hand_state == TLS_ST_EARLY_DATA)
259
0
                  && s->early_data_state != SSL_EARLY_DATA_WRITING)
260
14.8M
                || (!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
14.8M
    } else {
270
29.3k
        if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
271
13.8k
                && s->statem.hand_state == TLS_ST_EARLY_DATA)
272
0
            ossl_statem_set_in_init(s, 1);
273
29.3k
    }
274
50.2M
}
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
45.1M
{
292
45.1M
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
293
294
45.1M
    if (sc == NULL)
295
0
        return -1;
296
297
45.1M
    return state_machine(sc, 0);
298
45.1M
}
299
300
int ossl_statem_accept(SSL *s)
301
57.1k
{
302
57.1k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
303
304
57.1k
    if (sc == NULL)
305
0
        return -1;
306
307
57.1k
    return state_machine(sc, 1);
308
57.1k
}
309
310
typedef void (*info_cb) (const SSL *, int, int);
311
312
static info_cb get_callback(SSL_CONNECTION *s)
313
129M
{
314
129M
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
315
316
129M
    if (s->info_callback != NULL)
317
0
        return s->info_callback;
318
129M
    else if (sctx->info_callback != NULL)
319
0
        return sctx->info_callback;
320
321
129M
    return NULL;
322
129M
}
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
64.5M
{
354
64.5M
    BUF_MEM *buf = NULL;
355
64.5M
    void (*cb) (const SSL *ssl, int type, int val) = NULL;
356
64.5M
    OSSL_STATEM *st = &s->statem;
357
64.5M
    int ret = -1;
358
64.5M
    int ssret;
359
64.5M
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
360
64.5M
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
361
362
64.5M
    if (st->state == MSG_FLOW_ERROR) {
363
        /* Shouldn't have been called if we're already in the error state */
364
4.76k
        return -1;
365
4.76k
    }
366
367
64.5M
    ERR_clear_error();
368
64.5M
    clear_sys_error();
369
370
64.5M
    cb = get_callback(s);
371
372
64.5M
    st->in_handshake++;
373
64.5M
    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
140k
        if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(ssl))
379
0
            return -1;
380
140k
    }
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
64.5M
    if (st->state == MSG_FLOW_UNINITED
394
64.3M
            || st->state == MSG_FLOW_FINISHED) {
395
207k
        if (st->state == MSG_FLOW_UNINITED) {
396
140k
            st->hand_state = TLS_ST_BEFORE;
397
140k
            st->request_state = TLS_ST_BEFORE;
398
140k
        }
399
400
207k
        s->server = server;
401
207k
        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
207k
        if (SSL_CONNECTION_IS_DTLS(s)) {
413
34.6k
            if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
414
0
                (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
172k
        } else {
419
172k
            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
172k
        }
424
425
207k
        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
207k
        if (s->init_buf == NULL) {
431
207k
            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
207k
            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
207k
            s->init_buf = buf;
440
207k
            buf = NULL;
441
207k
        }
442
443
207k
        s->init_num = 0;
444
445
        /*
446
         * Should have been reset by tls_process_finished, too.
447
         */
448
207k
        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
207k
            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
207k
        if ((SSL_in_before(ssl))
463
140k
                || s->renegotiate) {
464
140k
            if (!tls_setup_handshake(s)) {
465
                /* SSLfatal() already called */
466
0
                goto end;
467
0
            }
468
469
140k
            if (SSL_IS_FIRST_HANDSHAKE(s))
470
140k
                st->read_state_first_init = 1;
471
140k
        }
472
473
207k
        st->state = MSG_FLOW_WRITING;
474
207k
        init_write_state_machine(s);
475
207k
    }
476
477
64.9M
    while (st->state != MSG_FLOW_FINISHED) {
478
64.8M
        if (st->state == MSG_FLOW_READING) {
479
64.5M
            ssret = read_state_machine(s);
480
64.5M
            if (ssret == SUB_STATE_FINISHED) {
481
115k
                st->state = MSG_FLOW_WRITING;
482
115k
                init_write_state_machine(s);
483
64.4M
            } else {
484
                /* NBIO or error */
485
64.4M
                goto end;
486
64.4M
            }
487
64.5M
        } else if (st->state == MSG_FLOW_WRITING) {
488
322k
            ssret = write_state_machine(s);
489
322k
            if (ssret == SUB_STATE_FINISHED) {
490
245k
                st->state = MSG_FLOW_READING;
491
245k
                init_read_state_machine(s);
492
245k
            } else if (ssret == SUB_STATE_END_HANDSHAKE) {
493
75.4k
                st->state = MSG_FLOW_FINISHED;
494
75.4k
            } else {
495
                /* NBIO or error */
496
1.50k
                goto end;
497
1.50k
            }
498
322k
        } 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
64.8M
    }
505
506
75.4k
    ret = 1;
507
508
64.5M
 end:
509
64.5M
    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
64.5M
    BUF_MEM_free(buf);
523
64.5M
    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
64.5M
    return ret;
530
75.4k
}
531
532
/*
533
 * Initialise the MSG_FLOW_READING sub-state machine
534
 */
535
static void init_read_state_machine(SSL_CONNECTION *s)
536
260k
{
537
260k
    OSSL_STATEM *st = &s->statem;
538
539
260k
    st->read_state = READ_STATE_HEADER;
540
260k
}
541
542
229k
static int grow_init_buf(SSL_CONNECTION *s, size_t size) {
543
544
229k
    size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
545
546
229k
    if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
547
0
        return 0;
548
549
229k
    if (size < msg_offset)
550
0
        return 0;
551
552
229k
    s->init_msg = s->init_buf->data + msg_offset;
553
554
229k
    return 1;
555
229k
}
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
45.2M
{
585
45.2M
    OSSL_STATEM *st = &s->statem;
586
45.2M
    int ret, mt;
587
45.2M
    size_t len = 0;
588
45.2M
    int (*transition) (SSL_CONNECTION *s, int mt);
589
45.2M
    PACKET pkt;
590
45.2M
    MSG_PROCESS_RETURN(*process_message) (SSL_CONNECTION *s, PACKET *pkt);
591
45.2M
    WORK_STATE(*post_process_message) (SSL_CONNECTION *s, WORK_STATE wst);
592
45.2M
    size_t (*max_message_size) (SSL_CONNECTION *s);
593
45.2M
    void (*cb) (const SSL *ssl, int type, int val) = NULL;
594
45.2M
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
595
596
45.2M
    cb = get_callback(s);
597
598
45.2M
    if (s->server) {
599
74.5k
        transition = ossl_statem_server_read_transition;
600
74.5k
        process_message = ossl_statem_server_process_message;
601
74.5k
        max_message_size = ossl_statem_server_max_message_size;
602
74.5k
        post_process_message = ossl_statem_server_post_process_message;
603
45.1M
    } else {
604
45.1M
        transition = ossl_statem_client_read_transition;
605
45.1M
        process_message = ossl_statem_client_process_message;
606
45.1M
        max_message_size = ossl_statem_client_max_message_size;
607
45.1M
        post_process_message = ossl_statem_client_post_process_message;
608
45.1M
    }
609
610
45.2M
    if (st->read_state_first_init) {
611
91.7k
        s->first_packet = 1;
612
91.7k
        st->read_state_first_init = 0;
613
91.7k
    }
614
615
45.3M
    while (1) {
616
45.3M
        switch (st->read_state) {
617
34.4M
        case READ_STATE_HEADER:
618
            /* Get the state the peer wants to move to */
619
34.4M
            if (SSL_CONNECTION_IS_DTLS(s)) {
620
                /*
621
                 * In DTLS we get the whole message in one go - header and body
622
                 */
623
37.7k
                ret = dtls_get_message(s, &mt);
624
34.3M
            } else {
625
34.3M
                ret = tls_get_message_header(s, &mt);
626
34.3M
            }
627
628
34.4M
            if (ret == 0) {
629
                /* Could be non-blocking IO */
630
34.2M
                return SUB_STATE_ERROR;
631
34.2M
            }
632
633
210k
            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
210k
            if (!transition(s, mt))
645
4.61k
                return SUB_STATE_ERROR;
646
647
205k
            if (s->s3.tmp.message_size > max_message_size(s)) {
648
504
                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
649
504
                         SSL_R_EXCESSIVE_MESSAGE_SIZE);
650
504
                return SUB_STATE_ERROR;
651
504
            }
652
653
            /* dtls_get_message already did this */
654
205k
            if (!SSL_CONNECTION_IS_DTLS(s)
655
178k
                    && s->s3.tmp.message_size > 0
656
146k
                    && !grow_init_buf(s, s->s3.tmp.message_size
657
146k
                                         + 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
205k
            st->read_state = READ_STATE_BODY;
663
            /* Fall through */
664
665
11.1M
        case READ_STATE_BODY:
666
11.1M
            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
26.8k
                ret = dtls_get_message_body(s, &len);
672
11.0M
            } else {
673
11.0M
                ret = tls_get_message_body(s, &len);
674
11.0M
            }
675
11.1M
            if (ret == 0) {
676
                /* Could be non-blocking IO */
677
10.9M
                return SUB_STATE_ERROR;
678
10.9M
            }
679
680
200k
            s->first_packet = 0;
681
200k
            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
200k
            ret = process_message(s, &pkt);
686
687
            /* Discard the packet data */
688
200k
            s->init_num = 0;
689
690
200k
            switch (ret) {
691
23.3k
            case MSG_PROCESS_ERROR:
692
23.3k
                check_fatal(s);
693
23.3k
                return SUB_STATE_ERROR;
694
695
49.9k
            case MSG_PROCESS_FINISHED_READING:
696
49.9k
                if (SSL_CONNECTION_IS_DTLS(s)) {
697
3.95k
                    dtls1_stop_timer(s);
698
3.95k
                }
699
49.9k
                return SUB_STATE_FINISHED;
700
701
50.0k
            case MSG_PROCESS_CONTINUE_PROCESSING:
702
50.0k
                st->read_state = READ_STATE_POST_PROCESS;
703
50.0k
                st->read_state_work = WORK_MORE_A;
704
50.0k
                break;
705
706
76.7k
            default:
707
76.7k
                st->read_state = READ_STATE_HEADER;
708
76.7k
                break;
709
200k
            }
710
126k
            break;
711
712
126k
        case READ_STATE_POST_PROCESS:
713
50.0k
            st->read_state_work = post_process_message(s, st->read_state_work);
714
50.0k
            switch (st->read_state_work) {
715
8.31k
            case WORK_ERROR:
716
8.31k
                check_fatal(s);
717
                /* Fall through */
718
8.31k
            case WORK_MORE_A:
719
8.31k
            case WORK_MORE_B:
720
8.31k
            case WORK_MORE_C:
721
8.31k
                return SUB_STATE_ERROR;
722
723
25.7k
            case WORK_FINISHED_CONTINUE:
724
25.7k
                st->read_state = READ_STATE_HEADER;
725
25.7k
                break;
726
727
15.9k
            case WORK_FINISHED_STOP:
728
15.9k
                if (SSL_CONNECTION_IS_DTLS(s)) {
729
5.07k
                    dtls1_stop_timer(s);
730
5.07k
                }
731
15.9k
                return SUB_STATE_FINISHED;
732
50.0k
            }
733
25.7k
            break;
734
735
25.7k
        default:
736
            /* Shouldn't happen */
737
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
738
0
            return SUB_STATE_ERROR;
739
45.3M
        }
740
45.3M
    }
741
45.2M
}
742
743
/*
744
 * Send a previously constructed message to the peer.
745
 */
746
static int statem_do_write(SSL_CONNECTION *s)
747
256k
{
748
256k
    OSSL_STATEM *st = &s->statem;
749
750
256k
    if (st->hand_state == TLS_ST_CW_CHANGE
751
244k
        || st->hand_state == TLS_ST_SW_CHANGE) {
752
16.6k
        if (SSL_CONNECTION_IS_DTLS(s))
753
2.69k
            return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
754
13.9k
        else
755
13.9k
            return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
756
239k
    } else {
757
239k
        return ssl_do_write(s);
758
239k
    }
759
256k
}
760
761
/*
762
 * Initialise the MSG_FLOW_WRITING sub-state machine
763
 */
764
static void init_write_state_machine(SSL_CONNECTION *s)
765
337k
{
766
337k
    OSSL_STATEM *st = &s->statem;
767
768
337k
    st->write_state = WRITE_STATE_TRANSITION;
769
337k
}
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
176k
{
804
176k
    OSSL_STATEM *st = &s->statem;
805
176k
    int ret;
806
176k
    WRITE_TRAN(*transition) (SSL_CONNECTION *s);
807
176k
    WORK_STATE(*pre_work) (SSL_CONNECTION *s, WORK_STATE wst);
808
176k
    WORK_STATE(*post_work) (SSL_CONNECTION *s, WORK_STATE wst);
809
176k
    int (*get_construct_message_f) (SSL_CONNECTION *s,
810
176k
                                    CON_FUNC_RETURN (**confunc) (SSL_CONNECTION *s,
811
176k
                                                                 WPACKET *pkt),
812
176k
                                    int *mt);
813
176k
    void (*cb) (const SSL *ssl, int type, int val) = NULL;
814
176k
    CON_FUNC_RETURN (*confunc) (SSL_CONNECTION *s, WPACKET *pkt);
815
176k
    int mt;
816
176k
    WPACKET pkt;
817
176k
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
818
819
176k
    cb = get_callback(s);
820
821
176k
    if (s->server) {
822
94.4k
        transition = ossl_statem_server_write_transition;
823
94.4k
        pre_work = ossl_statem_server_pre_work;
824
94.4k
        post_work = ossl_statem_server_post_work;
825
94.4k
        get_construct_message_f = ossl_statem_server_construct_message;
826
94.4k
    } else {
827
82.4k
        transition = ossl_statem_client_write_transition;
828
82.4k
        pre_work = ossl_statem_client_pre_work;
829
82.4k
        post_work = ossl_statem_client_post_work;
830
82.4k
        get_construct_message_f = ossl_statem_client_construct_message;
831
82.4k
    }
832
833
491k
    while (1) {
834
491k
        switch (st->write_state) {
835
313k
        case WRITE_STATE_TRANSITION:
836
313k
            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
313k
            switch (transition(s)) {
844
176k
            case WRITE_TRAN_CONTINUE:
845
176k
                st->write_state = WRITE_STATE_PRE_WORK;
846
176k
                st->write_state_work = WORK_MORE_A;
847
176k
                break;
848
849
136k
            case WRITE_TRAN_FINISHED:
850
136k
                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
313k
            }
857
176k
            break;
858
859
176k
        case WRITE_STATE_PRE_WORK:
860
176k
            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
137k
            case WORK_FINISHED_CONTINUE:
870
137k
                st->write_state = WRITE_STATE_SEND;
871
137k
                break;
872
873
39.2k
            case WORK_FINISHED_STOP:
874
39.2k
                return SUB_STATE_END_HANDSHAKE;
875
176k
            }
876
137k
            if (!get_construct_message_f(s, &confunc, &mt)) {
877
                /* SSLfatal() already called */
878
0
                return SUB_STATE_ERROR;
879
0
            }
880
137k
            if (mt == SSL3_MT_DUMMY) {
881
                /* Skip construction and sending. This isn't a "real" state */
882
1.03k
                st->write_state = WRITE_STATE_POST_WORK;
883
1.03k
                st->write_state_work = WORK_MORE_A;
884
1.03k
                break;
885
1.03k
            }
886
136k
            if (!WPACKET_init(&pkt, s->init_buf)
887
136k
                    || !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
136k
            if (confunc != NULL) {
893
136k
                CON_FUNC_RETURN tmpret;
894
895
136k
                tmpret = confunc(s, &pkt);
896
136k
                if (tmpret == CON_FUNC_ERROR) {
897
1.08k
                    WPACKET_cleanup(&pkt);
898
1.08k
                    check_fatal(s);
899
1.08k
                    return SUB_STATE_ERROR;
900
135k
                } 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
136k
            }
911
135k
            if (!ssl_close_construct_packet(s, &pkt, mt)
912
135k
                    || !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
135k
        case WRITE_STATE_SEND:
921
135k
            if (SSL_CONNECTION_IS_DTLS(s) && st->use_timer) {
922
30.2k
                dtls1_start_timer(s);
923
30.2k
            }
924
135k
            ret = statem_do_write(s);
925
135k
            if (ret <= 0) {
926
0
                return SUB_STATE_ERROR;
927
0
            }
928
135k
            st->write_state = WRITE_STATE_POST_WORK;
929
135k
            st->write_state_work = WORK_MORE_A;
930
            /* Fall through */
931
932
136k
        case WRITE_STATE_POST_WORK:
933
136k
            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
136k
            case WORK_FINISHED_CONTINUE:
943
136k
                st->write_state = WRITE_STATE_TRANSITION;
944
136k
                break;
945
946
0
            case WORK_FINISHED_STOP:
947
0
                return SUB_STATE_END_HANDSHAKE;
948
136k
            }
949
136k
            break;
950
951
136k
        default:
952
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
953
0
            return SUB_STATE_ERROR;
954
491k
        }
955
491k
    }
956
176k
}
957
958
/*
959
 * Flush the write BIO
960
 */
961
int statem_flush(SSL_CONNECTION *s)
962
162k
{
963
162k
    s->rwstate = SSL_WRITING;
964
162k
    if (BIO_flush(s->wbio) <= 0) {
965
0
        return 0;
966
0
    }
967
162k
    s->rwstate = SSL_NOTHING;
968
969
162k
    return 1;
970
162k
}
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
3.90k
{
982
3.90k
    OSSL_STATEM *st = &s->statem;
983
984
3.90k
    if (st->state == MSG_FLOW_UNINITED)
985
0
        return 0;
986
987
3.90k
    if (!s->s3.in_read_app_data || (s->s3.total_renegotiations == 0))
988
3.67k
        return 0;
989
990
226
    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
226
    } else {
999
        /*
1000
         * If we're a client and we haven't read the ServerHello yet then we
1001
         * allow app data
1002
         */
1003
226
        if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
1004
204
            return 1;
1005
226
    }
1006
1007
22
    return 0;
1008
226
}
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
}