Coverage Report

Created: 2026-09-12 06:55

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