blob: d171ece2a0bd92b72eb5bfc69e4ff7cb15971685 [file] [log] [blame]
Matt Caswellf8e0a552015-07-29 14:23:56 +01001/*
Pauli677963e2017-08-18 13:52:46 +10002 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
Matt Caswellf8e0a552015-07-29 14:23:56 +01003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * 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
Matt Caswellf8e0a552015-07-29 14:23:56 +01008 */
9
Pauli677963e2017-08-18 13:52:46 +100010#include "e_os.h"
Pauli07016a82017-08-24 09:05:07 +100011#include <openssl/rand.h>
Matt Caswell8ba708e2015-09-11 10:48:59 +010012#include "../ssl_locl.h"
Matt Caswell61ae9352015-09-11 11:23:20 +010013#include "statem_locl.h"
Matt Caswellf8e0a552015-07-29 14:23:56 +010014
15/*
16 * This file implements the SSL/TLS/DTLS state machines.
17 *
18 * There are two primary state machines:
19 *
20 * 1) Message flow state machine
21 * 2) Handshake state machine
22 *
23 * The Message flow state machine controls the reading and sending of messages
24 * including handling of non-blocking IO events, flushing of the underlying
25 * write BIO, handling unexpected messages, etc. It is itself broken into two
26 * separate sub-state machines which control reading and writing respectively.
27 *
28 * The Handshake state machine keeps track of the current SSL/TLS handshake
29 * state. Transitions of the handshake state are the result of events that
30 * occur within the Message flow state machine.
31 *
32 * Overall it looks like this:
33 *
34 * --------------------------------------------- -------------------
35 * | | | |
36 * | Message flow state machine | | |
37 * | | | |
38 * | -------------------- -------------------- | Transition | Handshake state |
Matt Caswell61ae9352015-09-11 11:23:20 +010039 * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
Matt Caswellf8e0a552015-07-29 14:23:56 +010040 * | | sub-state | | sub-state | |----------->| |
41 * | | machine for | | machine for | | | |
42 * | | reading messages | | writing messages | | | |
43 * | -------------------- -------------------- | | |
44 * | | | |
45 * --------------------------------------------- -------------------
46 *
47 */
48
49/* Sub state machine return values */
Emilia Kaspera230b262016-08-05 19:03:17 +020050typedef enum {
Matt Caswellf8e0a552015-07-29 14:23:56 +010051 /* Something bad happened or NBIO */
52 SUB_STATE_ERROR,
53 /* Sub state finished go to the next sub state */
54 SUB_STATE_FINISHED,
55 /* Sub state finished and handshake was completed */
56 SUB_STATE_END_HANDSHAKE
Matt Caswelld78052c2015-10-05 11:03:27 +010057} SUB_STATE_RETURN;
Matt Caswellf8e0a552015-07-29 14:23:56 +010058
Matt Caswell87235882015-09-07 16:36:53 +010059static int state_machine(SSL *s, int server);
Matt Caswellf8e0a552015-07-29 14:23:56 +010060static void init_read_state_machine(SSL *s);
Matt Caswelld78052c2015-10-05 11:03:27 +010061static SUB_STATE_RETURN read_state_machine(SSL *s);
Matt Caswellf8e0a552015-07-29 14:23:56 +010062static void init_write_state_machine(SSL *s);
Matt Caswelld78052c2015-10-05 11:03:27 +010063static SUB_STATE_RETURN write_state_machine(SSL *s);
Matt Caswell49ae7422015-09-08 09:13:50 +010064
Matt Caswell5998e292015-10-05 10:49:15 +010065OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
Matt Caswell49ae7422015-09-08 09:13:50 +010066{
67 return ssl->statem.hand_state;
68}
69
Matt Caswell49ae7422015-09-08 09:13:50 +010070int SSL_in_init(SSL *s)
71{
72 return s->statem.in_init;
73}
74
75int SSL_is_init_finished(SSL *s)
76{
77 return !(s->statem.in_init) && (s->statem.hand_state == TLS_ST_OK);
78}
79
80int SSL_in_before(SSL *s)
81{
82 /*
83 * Historically being "in before" meant before anything had happened. In the
84 * current code though we remain in the "before" state for a while after we
85 * have started the handshake process (e.g. as a server waiting for the
86 * first message to arrive). There "in before" is taken to mean "in before"
87 * and not started any handshake process yet.
88 */
89 return (s->statem.hand_state == TLS_ST_BEFORE)
90 && (s->statem.state == MSG_FLOW_UNINITED);
91}
92
Matt Caswellf8e0a552015-07-29 14:23:56 +010093/*
94 * Clear the state machine state and reset back to MSG_FLOW_UNINITED
95 */
Matt Caswellfe3a3292015-10-05 10:39:54 +010096void ossl_statem_clear(SSL *s)
Matt Caswellf8e0a552015-07-29 14:23:56 +010097{
98 s->statem.state = MSG_FLOW_UNINITED;
Matt Caswell49ae7422015-09-08 09:13:50 +010099 s->statem.hand_state = TLS_ST_BEFORE;
100 s->statem.in_init = 1;
Matt Caswella71a4962015-10-05 10:44:41 +0100101 s->statem.no_cert_verify = 0;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100102}
103
104/*
105 * Set the state machine up ready for a renegotiation handshake
106 */
Matt Caswellfe3a3292015-10-05 10:39:54 +0100107void ossl_statem_set_renegotiate(SSL *s)
Matt Caswellf8e0a552015-07-29 14:23:56 +0100108{
Matt Caswellc64359d2015-09-10 09:11:41 +0100109 s->statem.in_init = 1;
Matt Caswell0386aad2017-01-10 14:58:17 +0000110 s->statem.request_state = TLS_ST_SW_HELLO_REQ;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100111}
112
113/*
114 * Put the state machine into an error state. This is a permanent error for
115 * the current connection.
116 */
Matt Caswellfe3a3292015-10-05 10:39:54 +0100117void ossl_statem_set_error(SSL *s)
Matt Caswellf8e0a552015-07-29 14:23:56 +0100118{
119 s->statem.state = MSG_FLOW_ERROR;
Matt Caswell49ae7422015-09-08 09:13:50 +0100120}
121
122/*
123 * Discover whether the current connection is in the error state.
124 *
125 * Valid return values are:
126 * 1: Yes
127 * 0: No
128 */
Matt Caswellfe3a3292015-10-05 10:39:54 +0100129int ossl_statem_in_error(const SSL *s)
Matt Caswell49ae7422015-09-08 09:13:50 +0100130{
131 if (s->statem.state == MSG_FLOW_ERROR)
132 return 1;
133
134 return 0;
135}
136
Matt Caswellfe3a3292015-10-05 10:39:54 +0100137void ossl_statem_set_in_init(SSL *s, int init)
Matt Caswell49ae7422015-09-08 09:13:50 +0100138{
139 s->statem.in_init = init;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100140}
141
Matt Caswell024f5432015-10-22 13:57:18 +0100142int ossl_statem_get_in_handshake(SSL *s)
143{
144 return s->statem.in_handshake;
145}
146
147void ossl_statem_set_in_handshake(SSL *s, int inhand)
148{
149 if (inhand)
150 s->statem.in_handshake++;
151 else
152 s->statem.in_handshake--;
153}
154
Matt Caswell0a87d0a2017-02-20 16:35:03 +0000155/* Are we in a sensible state to skip over unreadable early data? */
156int ossl_statem_skip_early_data(SSL *s)
157{
Matt Caswell1ea4d092017-02-22 13:01:48 +0000158 if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
Matt Caswell0a87d0a2017-02-20 16:35:03 +0000159 return 0;
160
Matt Caswelld4504fe2017-07-14 14:50:48 +0100161 if (!s->server || s->statem.hand_state != TLS_ST_EARLY_DATA)
162 return 0;
Matt Caswell0a87d0a2017-02-20 16:35:03 +0000163
164 return 1;
165}
166
Matt Caswell3eaa4172017-02-27 20:54:39 +0000167/*
168 * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
169 * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
170 * data state and whether we should attempt to move the handshake on if so.
Todd Shortd1186c32017-04-13 10:20:04 -0400171 * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
Matt Caswell3eaa4172017-02-27 20:54:39 +0000172 * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
173 * or similar.
174 */
Todd Shortd1186c32017-04-13 10:20:04 -0400175void ossl_statem_check_finish_init(SSL *s, int sending)
Matt Caswell564547e2017-02-25 15:34:07 +0000176{
Todd Shortd1186c32017-04-13 10:20:04 -0400177 if (sending == -1) {
Matt Caswell3eaa4172017-02-27 20:54:39 +0000178 if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
Matt Caswellef6c1912017-03-09 15:03:07 +0000179 || s->statem.hand_state == TLS_ST_EARLY_DATA) {
Matt Caswell3eaa4172017-02-27 20:54:39 +0000180 ossl_statem_set_in_init(s, 1);
Matt Caswellef6c1912017-03-09 15:03:07 +0000181 if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
182 /*
183 * SSL_connect() or SSL_do_handshake() has been called directly.
184 * We don't allow any more writing of early data.
185 */
186 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
187 }
188 }
Matt Caswell3eaa4172017-02-27 20:54:39 +0000189 } else if (!s->server) {
Todd Shortd1186c32017-04-13 10:20:04 -0400190 if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
Matt Caswellef6c1912017-03-09 15:03:07 +0000191 || s->statem.hand_state == TLS_ST_EARLY_DATA)
Matt Caswellf7e393b2017-02-27 11:19:57 +0000192 && s->early_data_state != SSL_EARLY_DATA_WRITING)
Todd Shortd1186c32017-04-13 10:20:04 -0400193 || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) {
Matt Caswelld7f87832017-02-25 15:59:44 +0000194 ossl_statem_set_in_init(s, 1);
Matt Caswellef6c1912017-03-09 15:03:07 +0000195 /*
196 * SSL_write() has been called directly. We don't allow any more
197 * writing of early data.
198 */
Todd Shortd1186c32017-04-13 10:20:04 -0400199 if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY)
Matt Caswellef6c1912017-03-09 15:03:07 +0000200 s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
201 }
Matt Caswellf7e393b2017-02-27 11:19:57 +0000202 } else {
203 if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
204 && s->statem.hand_state == TLS_ST_EARLY_DATA)
205 ossl_statem_set_in_init(s, 1);
Matt Caswelld7f87832017-02-25 15:59:44 +0000206 }
Matt Caswell564547e2017-02-25 15:34:07 +0000207}
208
Matt Caswell31fd10e2015-10-22 12:18:45 +0100209void ossl_statem_set_hello_verify_done(SSL *s)
210{
211 s->statem.state = MSG_FLOW_UNINITED;
212 s->statem.in_init = 1;
213 /*
214 * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
215 * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
216 * calls to SSL_in_before() will return false. Also calls to
217 * SSL_state_string() and SSL_state_string_long() will return something
218 * sensible.
219 */
220 s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
221}
222
Emilia Kaspera230b262016-08-05 19:03:17 +0200223int ossl_statem_connect(SSL *s)
224{
Matt Caswell87235882015-09-07 16:36:53 +0100225 return state_machine(s, 0);
226}
227
Matt Caswellfe3a3292015-10-05 10:39:54 +0100228int ossl_statem_accept(SSL *s)
Matt Caswellc130dd82015-09-04 13:51:49 +0100229{
230 return state_machine(s, 1);
231}
232
Emilia Kaspera230b262016-08-05 19:03:17 +0200233typedef void (*info_cb) (const SSL *, int, int);
234
235static info_cb get_callback(SSL *s)
Matt Caswell91eac8d2015-10-05 11:28:51 +0100236{
237 if (s->info_callback != NULL)
238 return s->info_callback;
239 else if (s->ctx->info_callback != NULL)
240 return s->ctx->info_callback;
241
242 return NULL;
243}
244
Matt Caswellf8e0a552015-07-29 14:23:56 +0100245/*
246 * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
Matt Caswellc7f47782017-01-10 23:02:28 +0000247 * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
Matt Caswellf8e0a552015-07-29 14:23:56 +0100248 * transitions are as follows:
249 *
Matt Caswellc7f47782017-01-10 23:02:28 +0000250 * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
Matt Caswellf8e0a552015-07-29 14:23:56 +0100251 * | |
252 * +-----------------------+
253 * v
254 * MSG_FLOW_WRITING <---> MSG_FLOW_READING
255 * |
256 * V
257 * MSG_FLOW_FINISHED
258 * |
259 * V
260 * [SUCCESS]
261 *
262 * We may exit at any point due to an error or NBIO event. If an NBIO event
263 * occurs then we restart at the point we left off when we are recalled.
264 * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
265 *
266 * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
267 * into that state at any point in the event that an irrecoverable error occurs.
268 *
269 * Valid return values are:
270 * 1: Success
271 * <=0: NBIO or error
272 */
Viktor Dukhovni4fa52142015-12-29 03:24:17 -0500273static int state_machine(SSL *s, int server)
274{
Matt Caswellf8e0a552015-07-29 14:23:56 +0100275 BUF_MEM *buf = NULL;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100276 void (*cb) (const SSL *ssl, int type, int val) = NULL;
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100277 OSSL_STATEM *st = &s->statem;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100278 int ret = -1;
279 int ssret;
280
281 if (st->state == MSG_FLOW_ERROR) {
282 /* Shouldn't have been called if we're already in the error state */
283 return -1;
284 }
285
Matt Caswellf8e0a552015-07-29 14:23:56 +0100286 ERR_clear_error();
287 clear_sys_error();
288
Matt Caswell91eac8d2015-10-05 11:28:51 +0100289 cb = get_callback(s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100290
Matt Caswell024f5432015-10-22 13:57:18 +0100291 st->in_handshake++;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100292 if (!SSL_in_init(s) || SSL_in_before(s)) {
293 if (!SSL_clear(s))
294 return -1;
295 }
Matt Caswell473483d2015-09-07 22:00:36 +0100296#ifndef OPENSSL_NO_SCTP
Matt Caswell99240872017-06-20 16:36:30 +0100297 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
Matt Caswell473483d2015-09-07 22:00:36 +0100298 /*
299 * Notify SCTP BIO socket to enter handshake mode and prevent stream
Matt Caswell99240872017-06-20 16:36:30 +0100300 * identifier other than 0.
Matt Caswell473483d2015-09-07 22:00:36 +0100301 */
302 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
Matt Caswell024f5432015-10-22 13:57:18 +0100303 st->in_handshake, NULL);
Matt Caswell473483d2015-09-07 22:00:36 +0100304 }
305#endif
306
Matt Caswellf8e0a552015-07-29 14:23:56 +0100307 /* Initialise state machine */
Matt Caswell0386aad2017-01-10 14:58:17 +0000308 if (st->state == MSG_FLOW_UNINITED
Matt Caswell0386aad2017-01-10 14:58:17 +0000309 || st->state == MSG_FLOW_FINISHED) {
Matt Caswellf8e0a552015-07-29 14:23:56 +0100310 if (st->state == MSG_FLOW_UNINITED) {
311 st->hand_state = TLS_ST_BEFORE;
Matt Caswell0386aad2017-01-10 14:58:17 +0000312 st->request_state = TLS_ST_BEFORE;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100313 }
314
315 s->server = server;
316 if (cb != NULL)
317 cb(s, SSL_CB_HANDSHAKE_START, 1);
318
319 if (SSL_IS_DTLS(s)) {
320 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
Emilia Kaspera230b262016-08-05 19:03:17 +0200321 (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
Matt Caswellf8e0a552015-07-29 14:23:56 +0100322 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
323 goto end;
324 }
325 } else {
Viktor Dukhovni4fa52142015-12-29 03:24:17 -0500326 if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
Matt Caswellf8e0a552015-07-29 14:23:56 +0100327 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
328 goto end;
329 }
330 }
331
Viktor Dukhovni4fa52142015-12-29 03:24:17 -0500332 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
333 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
334 goto end;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100335 }
336
Matt Caswellf8e0a552015-07-29 14:23:56 +0100337 if (s->init_buf == NULL) {
338 if ((buf = BUF_MEM_new()) == NULL) {
339 goto end;
340 }
341 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
342 goto end;
343 }
344 s->init_buf = buf;
345 buf = NULL;
346 }
347
348 if (!ssl3_setup_buffers(s)) {
349 goto end;
350 }
351 s->init_num = 0;
352
353 /*
354 * Should have been reset by tls_process_finished, too.
355 */
356 s->s3->change_cipher_spec = 0;
357
Matt Caswell46417562016-05-17 12:28:14 +0100358 /*
359 * Ok, we now need to push on a buffering BIO ...but not with
360 * SCTP
361 */
362#ifndef OPENSSL_NO_SCTP
363 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
364#endif
365 if (!ssl_init_wbio_buffer(s)) {
366 goto end;
367 }
368
Matt Caswellf7e393b2017-02-27 11:19:57 +0000369 if ((SSL_in_before(s))
Matt Caswell49e7fe12017-02-21 09:22:22 +0000370 || s->renegotiate) {
Matt Caswellc7f47782017-01-10 23:02:28 +0000371 if (!tls_setup_handshake(s)) {
372 ossl_statem_set_error(s);
373 goto end;
Matt Caswell2c4a0562016-06-03 11:59:19 +0100374 }
Matt Caswellf8e0a552015-07-29 14:23:56 +0100375
Matt Caswellc7f47782017-01-10 23:02:28 +0000376 if (SSL_IS_FIRST_HANDSHAKE(s))
377 st->read_state_first_init = 1;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100378 }
379
380 st->state = MSG_FLOW_WRITING;
381 init_write_state_machine(s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100382 }
383
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200384 while (st->state != MSG_FLOW_FINISHED) {
385 if (st->state == MSG_FLOW_READING) {
Matt Caswellf8e0a552015-07-29 14:23:56 +0100386 ssret = read_state_machine(s);
387 if (ssret == SUB_STATE_FINISHED) {
388 st->state = MSG_FLOW_WRITING;
389 init_write_state_machine(s);
390 } else {
391 /* NBIO or error */
392 goto end;
393 }
394 } else if (st->state == MSG_FLOW_WRITING) {
395 ssret = write_state_machine(s);
396 if (ssret == SUB_STATE_FINISHED) {
397 st->state = MSG_FLOW_READING;
398 init_read_state_machine(s);
399 } else if (ssret == SUB_STATE_END_HANDSHAKE) {
400 st->state = MSG_FLOW_FINISHED;
401 } else {
402 /* NBIO or error */
403 goto end;
404 }
405 } else {
406 /* Error */
Matt Caswellfe3a3292015-10-05 10:39:54 +0100407 ossl_statem_set_error(s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100408 goto end;
409 }
410 }
411
Matt Caswellf8e0a552015-07-29 14:23:56 +0100412 ret = 1;
413
414 end:
Matt Caswell024f5432015-10-22 13:57:18 +0100415 st->in_handshake--;
Matt Caswell473483d2015-09-07 22:00:36 +0100416
417#ifndef OPENSSL_NO_SCTP
Matt Caswell99240872017-06-20 16:36:30 +0100418 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
Matt Caswell473483d2015-09-07 22:00:36 +0100419 /*
420 * Notify SCTP BIO socket to leave handshake mode and allow stream
Matt Caswell99240872017-06-20 16:36:30 +0100421 * identifier other than 0.
Matt Caswell473483d2015-09-07 22:00:36 +0100422 */
423 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
Matt Caswell024f5432015-10-22 13:57:18 +0100424 st->in_handshake, NULL);
Matt Caswell473483d2015-09-07 22:00:36 +0100425 }
426#endif
427
Matt Caswellf8e0a552015-07-29 14:23:56 +0100428 BUF_MEM_free(buf);
429 if (cb != NULL) {
430 if (server)
431 cb(s, SSL_CB_ACCEPT_EXIT, ret);
432 else
433 cb(s, SSL_CB_CONNECT_EXIT, ret);
434 }
435 return ret;
436}
437
438/*
439 * Initialise the MSG_FLOW_READING sub-state machine
440 */
441static void init_read_state_machine(SSL *s)
442{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100443 OSSL_STATEM *st = &s->statem;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100444
445 st->read_state = READ_STATE_HEADER;
446}
447
Matt Caswell0d698f62016-09-23 16:58:11 +0100448static int grow_init_buf(SSL *s, size_t size) {
449
450 size_t msg_offset = (char *)s->init_msg - s->init_buf->data;
451
452 if (!BUF_MEM_grow_clean(s->init_buf, (int)size))
453 return 0;
454
455 if (size < msg_offset)
456 return 0;
457
458 s->init_msg = s->init_buf->data + msg_offset;
459
460 return 1;
461}
462
Matt Caswellf8e0a552015-07-29 14:23:56 +0100463/*
464 * This function implements the sub-state machine when the message flow is in
465 * MSG_FLOW_READING. The valid sub-states and transitions are:
466 *
467 * READ_STATE_HEADER <--+<-------------+
468 * | | |
469 * v | |
470 * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
471 * | |
472 * +----------------------------+
473 * v
474 * [SUB_STATE_FINISHED]
475 *
476 * READ_STATE_HEADER has the responsibility for reading in the message header
477 * and transitioning the state of the handshake state machine.
478 *
479 * READ_STATE_BODY reads in the rest of the message and then subsequently
480 * processes it.
481 *
482 * READ_STATE_POST_PROCESS is an optional step that may occur if some post
483 * processing activity performed on the message may block.
484 *
FdaSilvaYY0d4fb842016-02-05 15:23:54 -0500485 * Any of the above states could result in an NBIO event occurring in which case
Matt Caswellf8e0a552015-07-29 14:23:56 +0100486 * control returns to the calling application. When this function is recalled we
487 * will resume in the same state where we left off.
488 */
Emilia Kaspera230b262016-08-05 19:03:17 +0200489static SUB_STATE_RETURN read_state_machine(SSL *s)
490{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100491 OSSL_STATEM *st = &s->statem;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100492 int ret, mt;
Matt Caswelleda75752016-09-06 12:05:25 +0100493 size_t len = 0;
Emilia Kaspera230b262016-08-05 19:03:17 +0200494 int (*transition) (SSL *s, int mt);
Matt Caswell73999b62015-09-10 10:22:30 +0100495 PACKET pkt;
Emilia Kaspera230b262016-08-05 19:03:17 +0200496 MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt);
497 WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst);
Matt Caswelleda75752016-09-06 12:05:25 +0100498 size_t (*max_message_size) (SSL *s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100499 void (*cb) (const SSL *ssl, int type, int val) = NULL;
500
Matt Caswell91eac8d2015-10-05 11:28:51 +0100501 cb = get_callback(s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100502
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200503 if (s->server) {
Matt Caswell8481f582015-10-26 11:54:17 +0000504 transition = ossl_statem_server_read_transition;
505 process_message = ossl_statem_server_process_message;
506 max_message_size = ossl_statem_server_max_message_size;
507 post_process_message = ossl_statem_server_post_process_message;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100508 } else {
Matt Caswell8481f582015-10-26 11:54:17 +0000509 transition = ossl_statem_client_read_transition;
510 process_message = ossl_statem_client_process_message;
511 max_message_size = ossl_statem_client_max_message_size;
512 post_process_message = ossl_statem_client_post_process_message;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100513 }
514
515 if (st->read_state_first_init) {
516 s->first_packet = 1;
517 st->read_state_first_init = 0;
518 }
519
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200520 while (1) {
521 switch (st->read_state) {
Matt Caswellf8e0a552015-07-29 14:23:56 +0100522 case READ_STATE_HEADER:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100523 /* Get the state the peer wants to move to */
Matt Caswell76af3032015-08-11 11:41:03 +0100524 if (SSL_IS_DTLS(s)) {
525 /*
526 * In DTLS we get the whole message in one go - header and body
527 */
528 ret = dtls_get_message(s, &mt, &len);
529 } else {
530 ret = tls_get_message_header(s, &mt);
531 }
Matt Caswellf8e0a552015-07-29 14:23:56 +0100532
533 if (ret == 0) {
534 /* Could be non-blocking IO */
535 return SUB_STATE_ERROR;
536 }
537
538 if (cb != NULL) {
539 /* Notify callback of an impending state change */
540 if (s->server)
541 cb(s, SSL_CB_ACCEPT_LOOP, 1);
542 else
543 cb(s, SSL_CB_CONNECT_LOOP, 1);
544 }
545 /*
546 * Validate that we are allowed to move to the new state and move
547 * to that state if so
548 */
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200549 if (!transition(s, mt)) {
Matt Caswell672f3332016-06-22 19:43:46 +0100550 ossl_statem_set_error(s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100551 return SUB_STATE_ERROR;
552 }
553
554 if (s->s3->tmp.message_size > max_message_size(s)) {
555 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
556 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
557 return SUB_STATE_ERROR;
558 }
559
Matt Caswellc1ef7c92016-09-19 11:39:21 +0100560 /* dtls_get_message already did this */
561 if (!SSL_IS_DTLS(s)
562 && s->s3->tmp.message_size > 0
Matt Caswell0d698f62016-09-23 16:58:11 +0100563 && !grow_init_buf(s, s->s3->tmp.message_size
564 + SSL3_HM_HEADER_LENGTH)) {
Matt Caswellc1ef7c92016-09-19 11:39:21 +0100565 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Richard Levittea449b472016-09-22 10:15:02 +0200566 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB);
Matt Caswellc1ef7c92016-09-19 11:39:21 +0100567 return SUB_STATE_ERROR;
568 }
569
Matt Caswellf8e0a552015-07-29 14:23:56 +0100570 st->read_state = READ_STATE_BODY;
571 /* Fall through */
572
573 case READ_STATE_BODY:
574 if (!SSL_IS_DTLS(s)) {
575 /* We already got this above for DTLS */
576 ret = tls_get_message_body(s, &len);
577 if (ret == 0) {
578 /* Could be non-blocking IO */
579 return SUB_STATE_ERROR;
580 }
581 }
582
583 s->first_packet = 0;
Matt Caswell73999b62015-09-10 10:22:30 +0100584 if (!PACKET_buf_init(&pkt, s->init_msg, len)) {
585 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
586 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
587 return SUB_STATE_ERROR;
588 }
589 ret = process_message(s, &pkt);
Matt Caswell1689e7e2016-05-12 17:18:32 +0100590
591 /* Discard the packet data */
592 s->init_num = 0;
593
Alessandro Ghedini4f8a5f42016-09-14 00:51:02 +0100594 switch (ret) {
595 case MSG_PROCESS_ERROR:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100596 return SUB_STATE_ERROR;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100597
Alessandro Ghedini4f8a5f42016-09-14 00:51:02 +0100598 case MSG_PROCESS_FINISHED_READING:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100599 if (SSL_IS_DTLS(s)) {
600 dtls1_stop_timer(s);
601 }
602 return SUB_STATE_FINISHED;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100603
Alessandro Ghedini4f8a5f42016-09-14 00:51:02 +0100604 case MSG_PROCESS_CONTINUE_PROCESSING:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100605 st->read_state = READ_STATE_POST_PROCESS;
606 st->read_state_work = WORK_MORE_A;
Alessandro Ghedini4f8a5f42016-09-14 00:51:02 +0100607 break;
608
609 default:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100610 st->read_state = READ_STATE_HEADER;
Alessandro Ghedini4f8a5f42016-09-14 00:51:02 +0100611 break;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100612 }
613 break;
614
615 case READ_STATE_POST_PROCESS:
616 st->read_state_work = post_process_message(s, st->read_state_work);
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200617 switch (st->read_state_work) {
Rich Salzf3b3d7f2016-08-30 13:31:18 -0400618 case WORK_ERROR:
619 case WORK_MORE_A:
620 case WORK_MORE_B:
Benjamin Kadukddf97252017-02-06 15:33:28 -0600621 case WORK_MORE_C:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100622 return SUB_STATE_ERROR;
623
624 case WORK_FINISHED_CONTINUE:
625 st->read_state = READ_STATE_HEADER;
626 break;
627
628 case WORK_FINISHED_STOP:
629 if (SSL_IS_DTLS(s)) {
630 dtls1_stop_timer(s);
631 }
632 return SUB_STATE_FINISHED;
633 }
634 break;
635
636 default:
637 /* Shouldn't happen */
638 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
639 SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
Matt Caswellfe3a3292015-10-05 10:39:54 +0100640 ossl_statem_set_error(s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100641 return SUB_STATE_ERROR;
642 }
643 }
644}
645
646/*
647 * Send a previously constructed message to the peer.
648 */
649static int statem_do_write(SSL *s)
650{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100651 OSSL_STATEM *st = &s->statem;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100652
653 if (st->hand_state == TLS_ST_CW_CHANGE
Emilia Kaspera230b262016-08-05 19:03:17 +0200654 || st->hand_state == TLS_ST_SW_CHANGE) {
Matt Caswellf8e0a552015-07-29 14:23:56 +0100655 if (SSL_IS_DTLS(s))
656 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
657 else
658 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
659 } else {
660 return ssl_do_write(s);
661 }
662}
663
664/*
665 * Initialise the MSG_FLOW_WRITING sub-state machine
666 */
667static void init_write_state_machine(SSL *s)
668{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100669 OSSL_STATEM *st = &s->statem;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100670
671 st->write_state = WRITE_STATE_TRANSITION;
672}
673
674/*
675 * This function implements the sub-state machine when the message flow is in
676 * MSG_FLOW_WRITING. The valid sub-states and transitions are:
677 *
678 * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
679 * | |
680 * | v
681 * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
682 * | |
683 * | v
684 * | WRITE_STATE_SEND
685 * | |
686 * | v
687 * | WRITE_STATE_POST_WORK
688 * | |
689 * +-------------+
690 *
691 * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
692
693 * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
FdaSilvaYY0d4fb842016-02-05 15:23:54 -0500694 * sending of the message. This could result in an NBIO event occurring in
Matt Caswellf8e0a552015-07-29 14:23:56 +0100695 * which case control returns to the calling application. When this function
696 * is recalled we will resume in the same state where we left off.
697 *
698 * WRITE_STATE_SEND sends the message and performs any work to be done after
699 * sending.
700 *
701 * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
702 * message has been completed. As for WRITE_STATE_PRE_WORK this could also
703 * result in an NBIO event.
704 */
Matt Caswelld78052c2015-10-05 11:03:27 +0100705static SUB_STATE_RETURN write_state_machine(SSL *s)
Matt Caswellf8e0a552015-07-29 14:23:56 +0100706{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100707 OSSL_STATEM *st = &s->statem;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100708 int ret;
Emilia Kaspera230b262016-08-05 19:03:17 +0200709 WRITE_TRAN(*transition) (SSL *s);
710 WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);
711 WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);
Matt Caswell6392fb82016-09-30 11:17:57 +0100712 int (*get_construct_message_f) (SSL *s, WPACKET *pkt,
713 int (**confunc) (SSL *s, WPACKET *pkt),
714 int *mt);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100715 void (*cb) (const SSL *ssl, int type, int val) = NULL;
Matt Caswell6392fb82016-09-30 11:17:57 +0100716 int (*confunc) (SSL *s, WPACKET *pkt);
717 int mt;
Matt Caswell7cea05d2016-09-29 23:28:29 +0100718 WPACKET pkt;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100719
Matt Caswell91eac8d2015-10-05 11:28:51 +0100720 cb = get_callback(s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100721
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200722 if (s->server) {
Matt Caswell8481f582015-10-26 11:54:17 +0000723 transition = ossl_statem_server_write_transition;
724 pre_work = ossl_statem_server_pre_work;
725 post_work = ossl_statem_server_post_work;
Matt Caswell6392fb82016-09-30 11:17:57 +0100726 get_construct_message_f = ossl_statem_server_construct_message;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100727 } else {
Matt Caswell8481f582015-10-26 11:54:17 +0000728 transition = ossl_statem_client_write_transition;
729 pre_work = ossl_statem_client_pre_work;
730 post_work = ossl_statem_client_post_work;
Matt Caswell6392fb82016-09-30 11:17:57 +0100731 get_construct_message_f = ossl_statem_client_construct_message;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100732 }
733
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200734 while (1) {
735 switch (st->write_state) {
Matt Caswellf8e0a552015-07-29 14:23:56 +0100736 case WRITE_STATE_TRANSITION:
737 if (cb != NULL) {
738 /* Notify callback of an impending state change */
739 if (s->server)
740 cb(s, SSL_CB_ACCEPT_LOOP, 1);
741 else
742 cb(s, SSL_CB_CONNECT_LOOP, 1);
743 }
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200744 switch (transition(s)) {
Matt Caswellf8e0a552015-07-29 14:23:56 +0100745 case WRITE_TRAN_CONTINUE:
746 st->write_state = WRITE_STATE_PRE_WORK;
747 st->write_state_work = WORK_MORE_A;
748 break;
749
750 case WRITE_TRAN_FINISHED:
751 return SUB_STATE_FINISHED;
752 break;
753
Rich Salzf3b3d7f2016-08-30 13:31:18 -0400754 case WRITE_TRAN_ERROR:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100755 return SUB_STATE_ERROR;
756 }
757 break;
758
759 case WRITE_STATE_PRE_WORK:
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200760 switch (st->write_state_work = pre_work(s, st->write_state_work)) {
Rich Salzf3b3d7f2016-08-30 13:31:18 -0400761 case WORK_ERROR:
762 case WORK_MORE_A:
763 case WORK_MORE_B:
Benjamin Kadukddf97252017-02-06 15:33:28 -0600764 case WORK_MORE_C:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100765 return SUB_STATE_ERROR;
766
767 case WORK_FINISHED_CONTINUE:
768 st->write_state = WRITE_STATE_SEND;
769 break;
770
771 case WORK_FINISHED_STOP:
772 return SUB_STATE_END_HANDSHAKE;
773 }
Matt Caswellf7e393b2017-02-27 11:19:57 +0000774 if (!get_construct_message_f(s, &pkt, &confunc, &mt)) {
775 ossl_statem_set_error(s);
776 return SUB_STATE_ERROR;
777 }
778 if (mt == SSL3_MT_DUMMY) {
779 /* Skip construction and sending. This isn't a "real" state */
780 st->write_state = WRITE_STATE_POST_WORK;
781 st->write_state_work = WORK_MORE_A;
782 break;
783 }
Matt Caswell7cea05d2016-09-29 23:28:29 +0100784 if (!WPACKET_init(&pkt, s->init_buf)
Matt Caswell6392fb82016-09-30 11:17:57 +0100785 || !ssl_set_handshake_header(s, &pkt, mt)
786 || (confunc != NULL && !confunc(s, &pkt))
787 || !ssl_close_construct_packet(s, &pkt, mt)
Matt Caswell7cea05d2016-09-29 23:28:29 +0100788 || !WPACKET_finish(&pkt)) {
789 WPACKET_cleanup(&pkt);
790 ossl_statem_set_error(s);
Matt Caswellf8e0a552015-07-29 14:23:56 +0100791 return SUB_STATE_ERROR;
Matt Caswell7cea05d2016-09-29 23:28:29 +0100792 }
Matt Caswellf8e0a552015-07-29 14:23:56 +0100793
794 /* Fall through */
795
796 case WRITE_STATE_SEND:
797 if (SSL_IS_DTLS(s) && st->use_timer) {
798 dtls1_start_timer(s);
799 }
800 ret = statem_do_write(s);
801 if (ret <= 0) {
802 return SUB_STATE_ERROR;
803 }
804 st->write_state = WRITE_STATE_POST_WORK;
805 st->write_state_work = WORK_MORE_A;
806 /* Fall through */
807
808 case WRITE_STATE_POST_WORK:
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200809 switch (st->write_state_work = post_work(s, st->write_state_work)) {
Rich Salzf3b3d7f2016-08-30 13:31:18 -0400810 case WORK_ERROR:
811 case WORK_MORE_A:
812 case WORK_MORE_B:
Benjamin Kadukddf97252017-02-06 15:33:28 -0600813 case WORK_MORE_C:
Matt Caswellf8e0a552015-07-29 14:23:56 +0100814 return SUB_STATE_ERROR;
815
816 case WORK_FINISHED_CONTINUE:
817 st->write_state = WRITE_STATE_TRANSITION;
818 break;
819
820 case WORK_FINISHED_STOP:
821 return SUB_STATE_END_HANDSHAKE;
822 }
823 break;
824
825 default:
826 return SUB_STATE_ERROR;
827 }
828 }
829}
830
831/*
Matt Caswell87235882015-09-07 16:36:53 +0100832 * Flush the write BIO
833 */
Matt Caswell61ae9352015-09-11 11:23:20 +0100834int statem_flush(SSL *s)
Matt Caswell87235882015-09-07 16:36:53 +0100835{
836 s->rwstate = SSL_WRITING;
837 if (BIO_flush(s->wbio) <= 0) {
838 return 0;
839 }
840 s->rwstate = SSL_NOTHING;
841
842 return 1;
843}
844
845/*
Matt Caswellf8e0a552015-07-29 14:23:56 +0100846 * Called by the record layer to determine whether application data is
Matt Caswellc7f47782017-01-10 23:02:28 +0000847 * allowed to be received in the current handshake state or not.
Matt Caswellf8e0a552015-07-29 14:23:56 +0100848 *
849 * Return values are:
850 * 1: Yes (application data allowed)
851 * 0: No (application data not allowed)
852 */
Matt Caswellfe3a3292015-10-05 10:39:54 +0100853int ossl_statem_app_data_allowed(SSL *s)
Matt Caswellf8e0a552015-07-29 14:23:56 +0100854{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100855 OSSL_STATEM *st = &s->statem;
Matt Caswellf8e0a552015-07-29 14:23:56 +0100856
Matt Caswellc7f47782017-01-10 23:02:28 +0000857 if (st->state == MSG_FLOW_UNINITED)
Matt Caswell94836de2015-09-08 09:19:22 +0100858 return 0;
859
Matt Caswell87235882015-09-07 16:36:53 +0100860 if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
861 return 0;
862
Matt Caswell94836de2015-09-08 09:19:22 +0100863 if (s->server) {
864 /*
865 * If we're a server and we haven't got as far as writing our
866 * ServerHello yet then we allow app data
867 */
868 if (st->hand_state == TLS_ST_BEFORE
Emilia Kaspera230b262016-08-05 19:03:17 +0200869 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
Matt Caswell87235882015-09-07 16:36:53 +0100870 return 1;
Matt Caswell94836de2015-09-08 09:19:22 +0100871 } else {
872 /*
873 * If we're a client and we haven't read the ServerHello yet then we
874 * allow app data
875 */
876 if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
877 return 1;
Matt Caswell87235882015-09-07 16:36:53 +0100878 }
879
Matt Caswell87235882015-09-07 16:36:53 +0100880 return 0;
881}