Coverage Report

Created: 2025-07-11 06:57

/src/openssl/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
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
    ossl_statem_set_in_init(s, 1);
135
0
    s->statem.no_cert_verify = 0;
136
0
}
137
138
/*
139
 * Set the state machine up ready for a renegotiation handshake
140
 */
141
void ossl_statem_set_renegotiate(SSL_CONNECTION *s)
142
0
{
143
0
    ossl_statem_set_in_init(s, 1);
144
0
    s->statem.request_state = TLS_ST_SW_HELLO_REQ;
145
0
}
146
147
void ossl_statem_send_fatal(SSL_CONNECTION *s, int al)
148
0
{
149
    /* We shouldn't call SSLfatal() twice. Once is enough */
150
0
    if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR)
151
0
      return;
152
0
    ossl_statem_set_in_init(s, 1);
153
0
    s->statem.state = MSG_FLOW_ERROR;
154
0
    if (al != SSL_AD_NO_ALERT)
155
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
156
0
}
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
0
{
167
0
    va_list args;
168
169
0
    va_start(args, fmt);
170
0
    ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
171
0
    va_end(args);
172
173
0
    ossl_statem_send_fatal(s, al);
174
0
}
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
0
    do { \
183
0
        if (!ossl_assert((s)->statem.in_init \
184
0
                         && (s)->statem.state == MSG_FLOW_ERROR)) \
185
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
186
0
    } 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
0
{
197
0
    if (s->statem.state == MSG_FLOW_ERROR)
198
0
        return 1;
199
200
0
    return 0;
201
0
}
202
203
void ossl_statem_set_in_init(SSL_CONNECTION *s, int init)
204
0
{
205
0
    s->statem.in_init = init;
206
0
    if (s->rlayer.rrlmethod != NULL && s->rlayer.rrlmethod->set_in_init != NULL)
207
0
        s->rlayer.rrlmethod->set_in_init(s->rlayer.rrl, init);
208
0
}
209
210
int ossl_statem_get_in_handshake(SSL_CONNECTION *s)
211
0
{
212
0
    return s->statem.in_handshake;
213
0
}
214
215
void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand)
216
0
{
217
0
    if (inhand)
218
0
        s->statem.in_handshake++;
219
0
    else
220
0
        s->statem.in_handshake--;
221
0
}
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
0
{
226
0
    if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
227
0
        return 0;
228
229
0
    if (!s->server
230
0
            || s->statem.hand_state != TLS_ST_EARLY_DATA
231
0
            || s->hello_retry_request == SSL_HRR_COMPLETE)
232
0
        return 0;
233
234
0
    return 1;
235
0
}
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
0
{
247
0
    if (sending == -1) {
248
0
        if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
249
0
                || s->statem.hand_state == TLS_ST_EARLY_DATA) {
250
0
            ossl_statem_set_in_init(s, 1);
251
0
            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
0
        }
259
0
    } else if (!s->server) {
260
0
        if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
261
0
                      || s->statem.hand_state == TLS_ST_EARLY_DATA)
262
0
                  && s->early_data_state != SSL_EARLY_DATA_WRITING)
263
0
                || (!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
0
    } else {
273
0
        if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
274
0
                && s->statem.hand_state == TLS_ST_EARLY_DATA)
275
0
            ossl_statem_set_in_init(s, 1);
276
0
    }
277
0
    return 1;
278
0
}
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
0
{
296
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
297
298
0
    if (sc == NULL)
299
0
        return -1;
300
301
0
    return state_machine(sc, 0);
302
0
}
303
304
int ossl_statem_accept(SSL *s)
305
0
{
306
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
307
308
0
    if (sc == NULL)
309
0
        return -1;
310
311
0
    return state_machine(sc, 1);
312
0
}
313
314
typedef void (*info_cb) (const SSL *, int, int);
315
316
static info_cb get_callback(SSL_CONNECTION *s)
317
0
{
318
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
319
320
0
    if (s->info_callback != NULL)
321
0
        return s->info_callback;
322
0
    else if (sctx->info_callback != NULL)
323
0
        return sctx->info_callback;
324
325
0
    return NULL;
326
0
}
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
0
{
358
0
    BUF_MEM *buf = NULL;
359
0
    void (*cb) (const SSL *ssl, int type, int val) = NULL;
360
0
    OSSL_STATEM *st = &s->statem;
361
0
    int ret = -1;
362
0
    int ssret;
363
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
364
0
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
365
366
0
    if (st->state == MSG_FLOW_ERROR) {
367
        /* Shouldn't have been called if we're already in the error state */
368
0
        return -1;
369
0
    }
370
371
0
    ERR_clear_error();
372
0
    clear_sys_error();
373
374
0
    cb = get_callback(s);
375
376
0
    st->in_handshake++;
377
0
    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
0
        if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(ssl))
383
0
            return -1;
384
0
    }
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
0
    if (st->state == MSG_FLOW_UNINITED
398
0
            || st->state == MSG_FLOW_FINISHED) {
399
0
        if (st->state == MSG_FLOW_UNINITED) {
400
0
            st->hand_state = TLS_ST_BEFORE;
401
0
            st->request_state = TLS_ST_BEFORE;
402
0
        }
403
404
0
        s->server = server;
405
0
        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
0
        if (SSL_CONNECTION_IS_DTLS(s)) {
417
0
            if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
418
0
                (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
0
        } else {
423
0
            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
0
        }
428
429
0
        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
0
        if (s->init_buf == NULL) {
435
0
            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
0
            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
0
            s->init_buf = buf;
444
0
            buf = NULL;
445
0
        }
446
447
0
        s->init_num = 0;
448
449
        /*
450
         * Should have been reset by tls_process_finished, too.
451
         */
452
0
        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
0
            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
0
        if ((SSL_in_before(ssl))
467
0
                || s->renegotiate) {
468
0
            if (!tls_setup_handshake(s)) {
469
                /* SSLfatal() already called */
470
0
                goto end;
471
0
            }
472
473
0
            if (SSL_IS_FIRST_HANDSHAKE(s))
474
0
                st->read_state_first_init = 1;
475
0
        }
476
477
0
        st->state = MSG_FLOW_WRITING;
478
0
        init_write_state_machine(s);
479
0
    }
480
481
0
    while (st->state != MSG_FLOW_FINISHED) {
482
0
        if (st->state == MSG_FLOW_READING) {
483
0
            ssret = read_state_machine(s);
484
0
            if (ssret == SUB_STATE_FINISHED) {
485
0
                st->state = MSG_FLOW_WRITING;
486
0
                init_write_state_machine(s);
487
0
            } else {
488
                /* NBIO or error */
489
0
                goto end;
490
0
            }
491
0
        } else if (st->state == MSG_FLOW_WRITING) {
492
0
            ssret = write_state_machine(s);
493
0
            if (ssret == SUB_STATE_FINISHED) {
494
0
                st->state = MSG_FLOW_READING;
495
0
                init_read_state_machine(s);
496
0
            } else if (ssret == SUB_STATE_END_HANDSHAKE) {
497
0
                st->state = MSG_FLOW_FINISHED;
498
0
            } else {
499
                /* NBIO or error */
500
0
                goto end;
501
0
            }
502
0
        } 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
0
    }
509
510
0
    ret = 1;
511
512
0
 end:
513
0
    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
0
    BUF_MEM_free(buf);
527
0
    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
0
    return ret;
534
0
}
535
536
/*
537
 * Initialise the MSG_FLOW_READING sub-state machine
538
 */
539
static void init_read_state_machine(SSL_CONNECTION *s)
540
0
{
541
0
    OSSL_STATEM *st = &s->statem;
542
543
0
    st->read_state = READ_STATE_HEADER;
544
0
}
545
546
0
static int grow_init_buf(SSL_CONNECTION *s, size_t size) {
547
548
0
    size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
549
550
0
    if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
551
0
        return 0;
552
553
0
    if (size < msg_offset)
554
0
        return 0;
555
556
0
    s->init_msg = s->init_buf->data + msg_offset;
557
558
0
    return 1;
559
0
}
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
0
{
589
0
    OSSL_STATEM *st = &s->statem;
590
0
    int ret, mt;
591
0
    size_t len = 0;
592
0
    int (*transition) (SSL_CONNECTION *s, int mt);
593
0
    PACKET pkt;
594
0
    MSG_PROCESS_RETURN(*process_message) (SSL_CONNECTION *s, PACKET *pkt);
595
0
    WORK_STATE(*post_process_message) (SSL_CONNECTION *s, WORK_STATE wst);
596
0
    size_t (*max_message_size) (SSL_CONNECTION *s);
597
0
    void (*cb) (const SSL *ssl, int type, int val) = NULL;
598
0
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
599
600
0
    cb = get_callback(s);
601
602
0
    if (s->server) {
603
0
        transition = ossl_statem_server_read_transition;
604
0
        process_message = ossl_statem_server_process_message;
605
0
        max_message_size = ossl_statem_server_max_message_size;
606
0
        post_process_message = ossl_statem_server_post_process_message;
607
0
    } else {
608
0
        transition = ossl_statem_client_read_transition;
609
0
        process_message = ossl_statem_client_process_message;
610
0
        max_message_size = ossl_statem_client_max_message_size;
611
0
        post_process_message = ossl_statem_client_post_process_message;
612
0
    }
613
614
0
    if (st->read_state_first_init) {
615
0
        s->first_packet = 1;
616
0
        st->read_state_first_init = 0;
617
0
    }
618
619
0
    while (1) {
620
0
        switch (st->read_state) {
621
0
        case READ_STATE_HEADER:
622
            /* Get the state the peer wants to move to */
623
0
            if (SSL_CONNECTION_IS_DTLS(s)) {
624
                /*
625
                 * In DTLS we get the whole message in one go - header and body
626
                 */
627
0
                ret = dtls_get_message(s, &mt);
628
0
            } else {
629
0
                ret = tls_get_message_header(s, &mt);
630
0
            }
631
632
0
            if (ret == 0) {
633
                /* Could be non-blocking IO */
634
0
                return SUB_STATE_ERROR;
635
0
            }
636
637
0
            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
0
            if (!transition(s, mt))
649
0
                return SUB_STATE_ERROR;
650
651
0
            if (s->s3.tmp.message_size > max_message_size(s)) {
652
0
                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
653
0
                         SSL_R_EXCESSIVE_MESSAGE_SIZE);
654
0
                return SUB_STATE_ERROR;
655
0
            }
656
657
            /* dtls_get_message already did this */
658
0
            if (!SSL_CONNECTION_IS_DTLS(s)
659
0
                    && s->s3.tmp.message_size > 0
660
0
                    && !grow_init_buf(s, s->s3.tmp.message_size
661
0
                                         + 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
0
            st->read_state = READ_STATE_BODY;
667
            /* Fall through */
668
669
0
        case READ_STATE_BODY:
670
0
            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
0
                ret = dtls_get_message_body(s, &len);
676
0
            } else {
677
0
                ret = tls_get_message_body(s, &len);
678
0
            }
679
0
            if (ret == 0) {
680
                /* Could be non-blocking IO */
681
0
                return SUB_STATE_ERROR;
682
0
            }
683
684
0
            s->first_packet = 0;
685
0
            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
0
            ret = process_message(s, &pkt);
690
691
            /* Discard the packet data */
692
0
            s->init_num = 0;
693
694
0
            switch (ret) {
695
0
            case MSG_PROCESS_ERROR:
696
0
                check_fatal(s);
697
0
                return SUB_STATE_ERROR;
698
699
0
            case MSG_PROCESS_FINISHED_READING:
700
0
                if (SSL_CONNECTION_IS_DTLS(s)) {
701
0
                    dtls1_stop_timer(s);
702
0
                }
703
0
                return SUB_STATE_FINISHED;
704
705
0
            case MSG_PROCESS_CONTINUE_PROCESSING:
706
0
                st->read_state = READ_STATE_POST_PROCESS;
707
0
                st->read_state_work = WORK_MORE_A;
708
0
                break;
709
710
0
            default:
711
0
                st->read_state = READ_STATE_HEADER;
712
0
                break;
713
0
            }
714
0
            break;
715
716
0
        case READ_STATE_POST_PROCESS:
717
0
            st->read_state_work = post_process_message(s, st->read_state_work);
718
0
            switch (st->read_state_work) {
719
0
            case WORK_ERROR:
720
0
                check_fatal(s);
721
                /* Fall through */
722
0
            case WORK_MORE_A:
723
0
            case WORK_MORE_B:
724
0
            case WORK_MORE_C:
725
0
                return SUB_STATE_ERROR;
726
727
0
            case WORK_FINISHED_CONTINUE:
728
0
                st->read_state = READ_STATE_HEADER;
729
0
                break;
730
731
0
            case WORK_FINISHED_SWAP:
732
0
            case WORK_FINISHED_STOP:
733
0
                if (SSL_CONNECTION_IS_DTLS(s)) {
734
0
                    dtls1_stop_timer(s);
735
0
                }
736
0
                return SUB_STATE_FINISHED;
737
0
            }
738
0
            break;
739
740
0
        default:
741
            /* Shouldn't happen */
742
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
743
0
            return SUB_STATE_ERROR;
744
0
        }
745
0
    }
746
0
}
747
748
/*
749
 * Send a previously constructed message to the peer.
750
 */
751
static int statem_do_write(SSL_CONNECTION *s)
752
0
{
753
0
    OSSL_STATEM *st = &s->statem;
754
755
0
    if (st->hand_state == TLS_ST_CW_CHANGE
756
0
        || st->hand_state == TLS_ST_SW_CHANGE) {
757
0
        if (SSL_CONNECTION_IS_DTLS(s))
758
0
            return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
759
0
        else
760
0
            return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
761
0
    } else {
762
0
        return ssl_do_write(s);
763
0
    }
764
0
}
765
766
/*
767
 * Initialise the MSG_FLOW_WRITING sub-state machine
768
 */
769
static void init_write_state_machine(SSL_CONNECTION *s)
770
0
{
771
0
    OSSL_STATEM *st = &s->statem;
772
773
0
    st->write_state = WRITE_STATE_TRANSITION;
774
0
}
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
0
{
809
0
    OSSL_STATEM *st = &s->statem;
810
0
    int ret;
811
0
    WRITE_TRAN(*transition) (SSL_CONNECTION *s);
812
0
    WORK_STATE(*pre_work) (SSL_CONNECTION *s, WORK_STATE wst);
813
0
    WORK_STATE(*post_work) (SSL_CONNECTION *s, WORK_STATE wst);
814
0
    int (*get_construct_message_f) (SSL_CONNECTION *s,
815
0
                                    CON_FUNC_RETURN (**confunc) (SSL_CONNECTION *s,
816
0
                                                                 WPACKET *pkt),
817
0
                                    int *mt);
818
0
    void (*cb) (const SSL *ssl, int type, int val) = NULL;
819
0
    CON_FUNC_RETURN (*confunc) (SSL_CONNECTION *s, WPACKET *pkt);
820
0
    int mt;
821
0
    WPACKET pkt;
822
0
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
823
824
0
    cb = get_callback(s);
825
826
0
    if (s->server) {
827
0
        transition = ossl_statem_server_write_transition;
828
0
        pre_work = ossl_statem_server_pre_work;
829
0
        post_work = ossl_statem_server_post_work;
830
0
        get_construct_message_f = ossl_statem_server_construct_message;
831
0
    } else {
832
0
        transition = ossl_statem_client_write_transition;
833
0
        pre_work = ossl_statem_client_pre_work;
834
0
        post_work = ossl_statem_client_post_work;
835
0
        get_construct_message_f = ossl_statem_client_construct_message;
836
0
    }
837
838
0
    while (1) {
839
0
        switch (st->write_state) {
840
0
        case WRITE_STATE_TRANSITION:
841
0
            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
0
            switch (transition(s)) {
849
0
            case WRITE_TRAN_CONTINUE:
850
0
                st->write_state = WRITE_STATE_PRE_WORK;
851
0
                st->write_state_work = WORK_MORE_A;
852
0
                break;
853
854
0
            case WRITE_TRAN_FINISHED:
855
0
                return SUB_STATE_FINISHED;
856
857
0
            case WRITE_TRAN_ERROR:
858
0
                check_fatal(s);
859
0
                return SUB_STATE_ERROR;
860
0
            }
861
0
            break;
862
863
0
        case WRITE_STATE_PRE_WORK:
864
0
            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
0
            case WORK_FINISHED_CONTINUE:
874
0
                st->write_state = WRITE_STATE_SEND;
875
0
                break;
876
877
0
            case WORK_FINISHED_SWAP:
878
0
                return SUB_STATE_FINISHED;
879
880
0
            case WORK_FINISHED_STOP:
881
0
                return SUB_STATE_END_HANDSHAKE;
882
0
            }
883
0
            if (!get_construct_message_f(s, &confunc, &mt)) {
884
                /* SSLfatal() already called */
885
0
                return SUB_STATE_ERROR;
886
0
            }
887
0
            if (mt == SSL3_MT_DUMMY) {
888
                /* Skip construction and sending. This isn't a "real" state */
889
0
                st->write_state = WRITE_STATE_POST_WORK;
890
0
                st->write_state_work = WORK_MORE_A;
891
0
                break;
892
0
            }
893
0
            if (!WPACKET_init(&pkt, s->init_buf)
894
0
                    || !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
0
            if (confunc != NULL) {
900
0
                CON_FUNC_RETURN tmpret;
901
902
0
                tmpret = confunc(s, &pkt);
903
0
                if (tmpret == CON_FUNC_ERROR) {
904
0
                    WPACKET_cleanup(&pkt);
905
0
                    check_fatal(s);
906
0
                    return SUB_STATE_ERROR;
907
0
                } 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
0
            }
918
0
            if (!ssl_close_construct_packet(s, &pkt, mt)
919
0
                    || !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
0
        case WRITE_STATE_SEND:
928
0
            if (SSL_CONNECTION_IS_DTLS(s) && st->use_timer) {
929
0
                dtls1_start_timer(s);
930
0
            }
931
0
            ret = statem_do_write(s);
932
0
            if (ret <= 0) {
933
0
                return SUB_STATE_ERROR;
934
0
            }
935
0
            st->write_state = WRITE_STATE_POST_WORK;
936
0
            st->write_state_work = WORK_MORE_A;
937
            /* Fall through */
938
939
0
        case WRITE_STATE_POST_WORK:
940
0
            switch (st->write_state_work = post_work(s, st->write_state_work)) {
941
0
            case WORK_ERROR:
942
0
                check_fatal(s);
943
                /* Fall through */
944
0
            case WORK_MORE_A:
945
0
            case WORK_MORE_B:
946
0
            case WORK_MORE_C:
947
0
                return SUB_STATE_ERROR;
948
949
0
            case WORK_FINISHED_CONTINUE:
950
0
                st->write_state = WRITE_STATE_TRANSITION;
951
0
                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
0
            }
959
0
            break;
960
961
0
        default:
962
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
963
0
            return SUB_STATE_ERROR;
964
0
        }
965
0
    }
966
0
}
967
968
/*
969
 * Flush the write BIO
970
 */
971
int statem_flush(SSL_CONNECTION *s)
972
0
{
973
0
    s->rwstate = SSL_WRITING;
974
0
    if (BIO_flush(s->wbio) <= 0) {
975
0
        return 0;
976
0
    }
977
0
    s->rwstate = SSL_NOTHING;
978
979
0
    return 1;
980
0
}
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
0
{
992
0
    OSSL_STATEM *st = &s->statem;
993
994
0
    if (st->state == MSG_FLOW_UNINITED)
995
0
        return 0;
996
997
0
    if (!s->s3.in_read_app_data || (s->s3.total_renegotiations == 0))
998
0
        return 0;
999
1000
0
    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
0
    } else {
1009
        /*
1010
         * If we're a client and we haven't read the ServerHello yet then we
1011
         * allow app data
1012
         */
1013
0
        if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
1014
0
            return 1;
1015
0
    }
1016
1017
0
    return 0;
1018
0
}
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
}