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