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