Coverage Report

Created: 2025-08-28 07:07

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