Coverage Report

Created: 2026-08-18 07:24

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