Coverage Report

Created: 2025-12-31 06:58

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