Coverage Report

Created: 2024-05-21 06:52

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