Coverage Report

Created: 2025-08-28 07:07

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