Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 1 | /* |
Pauli | 677963e | 2017-08-18 13:52:46 +1000 | [diff] [blame] | 2 | * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved. |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 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 |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Pauli | 677963e | 2017-08-18 13:52:46 +1000 | [diff] [blame] | 10 | #include "e_os.h" |
Pauli | 07016a8 | 2017-08-24 09:05:07 +1000 | [diff] [blame^] | 11 | #include <openssl/rand.h> |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 12 | #include "../ssl_locl.h" |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 13 | #include "statem_locl.h" |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 14 | |
| 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 Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 39 | * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine | |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 40 | * | | 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 Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 50 | typedef enum { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 51 | /* 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 Caswell | d78052c | 2015-10-05 11:03:27 +0100 | [diff] [blame] | 57 | } SUB_STATE_RETURN; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 58 | |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 59 | static int state_machine(SSL *s, int server); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 60 | static void init_read_state_machine(SSL *s); |
Matt Caswell | d78052c | 2015-10-05 11:03:27 +0100 | [diff] [blame] | 61 | static SUB_STATE_RETURN read_state_machine(SSL *s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 62 | static void init_write_state_machine(SSL *s); |
Matt Caswell | d78052c | 2015-10-05 11:03:27 +0100 | [diff] [blame] | 63 | static SUB_STATE_RETURN write_state_machine(SSL *s); |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 64 | |
Matt Caswell | 5998e29 | 2015-10-05 10:49:15 +0100 | [diff] [blame] | 65 | OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl) |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 66 | { |
| 67 | return ssl->statem.hand_state; |
| 68 | } |
| 69 | |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 70 | int SSL_in_init(SSL *s) |
| 71 | { |
| 72 | return s->statem.in_init; |
| 73 | } |
| 74 | |
| 75 | int SSL_is_init_finished(SSL *s) |
| 76 | { |
| 77 | return !(s->statem.in_init) && (s->statem.hand_state == TLS_ST_OK); |
| 78 | } |
| 79 | |
| 80 | int 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Clear the state machine state and reset back to MSG_FLOW_UNINITED |
| 95 | */ |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 96 | void ossl_statem_clear(SSL *s) |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 97 | { |
| 98 | s->statem.state = MSG_FLOW_UNINITED; |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 99 | s->statem.hand_state = TLS_ST_BEFORE; |
| 100 | s->statem.in_init = 1; |
Matt Caswell | a71a496 | 2015-10-05 10:44:41 +0100 | [diff] [blame] | 101 | s->statem.no_cert_verify = 0; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Set the state machine up ready for a renegotiation handshake |
| 106 | */ |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 107 | void ossl_statem_set_renegotiate(SSL *s) |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 108 | { |
Matt Caswell | c64359d | 2015-09-10 09:11:41 +0100 | [diff] [blame] | 109 | s->statem.in_init = 1; |
Matt Caswell | 0386aad | 2017-01-10 14:58:17 +0000 | [diff] [blame] | 110 | s->statem.request_state = TLS_ST_SW_HELLO_REQ; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Put the state machine into an error state. This is a permanent error for |
| 115 | * the current connection. |
| 116 | */ |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 117 | void ossl_statem_set_error(SSL *s) |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 118 | { |
| 119 | s->statem.state = MSG_FLOW_ERROR; |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 120 | } |
| 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 Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 129 | int ossl_statem_in_error(const SSL *s) |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 130 | { |
| 131 | if (s->statem.state == MSG_FLOW_ERROR) |
| 132 | return 1; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 137 | void ossl_statem_set_in_init(SSL *s, int init) |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 138 | { |
| 139 | s->statem.in_init = init; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Matt Caswell | 024f543 | 2015-10-22 13:57:18 +0100 | [diff] [blame] | 142 | int ossl_statem_get_in_handshake(SSL *s) |
| 143 | { |
| 144 | return s->statem.in_handshake; |
| 145 | } |
| 146 | |
| 147 | void 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 Caswell | 0a87d0a | 2017-02-20 16:35:03 +0000 | [diff] [blame] | 155 | /* Are we in a sensible state to skip over unreadable early data? */ |
| 156 | int ossl_statem_skip_early_data(SSL *s) |
| 157 | { |
Matt Caswell | 1ea4d09 | 2017-02-22 13:01:48 +0000 | [diff] [blame] | 158 | if (s->ext.early_data != SSL_EARLY_DATA_REJECTED) |
Matt Caswell | 0a87d0a | 2017-02-20 16:35:03 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | |
Matt Caswell | d4504fe | 2017-07-14 14:50:48 +0100 | [diff] [blame] | 161 | if (!s->server || s->statem.hand_state != TLS_ST_EARLY_DATA) |
| 162 | return 0; |
Matt Caswell | 0a87d0a | 2017-02-20 16:35:03 +0000 | [diff] [blame] | 163 | |
| 164 | return 1; |
| 165 | } |
| 166 | |
Matt Caswell | 3eaa417 | 2017-02-27 20:54:39 +0000 | [diff] [blame] | 167 | /* |
| 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 Short | d1186c3 | 2017-04-13 10:20:04 -0400 | [diff] [blame] | 171 | * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are |
Matt Caswell | 3eaa417 | 2017-02-27 20:54:39 +0000 | [diff] [blame] | 172 | * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake() |
| 173 | * or similar. |
| 174 | */ |
Todd Short | d1186c3 | 2017-04-13 10:20:04 -0400 | [diff] [blame] | 175 | void ossl_statem_check_finish_init(SSL *s, int sending) |
Matt Caswell | 564547e | 2017-02-25 15:34:07 +0000 | [diff] [blame] | 176 | { |
Todd Short | d1186c3 | 2017-04-13 10:20:04 -0400 | [diff] [blame] | 177 | if (sending == -1) { |
Matt Caswell | 3eaa417 | 2017-02-27 20:54:39 +0000 | [diff] [blame] | 178 | if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 179 | || s->statem.hand_state == TLS_ST_EARLY_DATA) { |
Matt Caswell | 3eaa417 | 2017-02-27 20:54:39 +0000 | [diff] [blame] | 180 | ossl_statem_set_in_init(s, 1); |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 181 | 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 Caswell | 3eaa417 | 2017-02-27 20:54:39 +0000 | [diff] [blame] | 189 | } else if (!s->server) { |
Todd Short | d1186c3 | 2017-04-13 10:20:04 -0400 | [diff] [blame] | 190 | if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 191 | || s->statem.hand_state == TLS_ST_EARLY_DATA) |
Matt Caswell | f7e393b | 2017-02-27 11:19:57 +0000 | [diff] [blame] | 192 | && s->early_data_state != SSL_EARLY_DATA_WRITING) |
Todd Short | d1186c3 | 2017-04-13 10:20:04 -0400 | [diff] [blame] | 193 | || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) { |
Matt Caswell | d7f8783 | 2017-02-25 15:59:44 +0000 | [diff] [blame] | 194 | ossl_statem_set_in_init(s, 1); |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 195 | /* |
| 196 | * SSL_write() has been called directly. We don't allow any more |
| 197 | * writing of early data. |
| 198 | */ |
Todd Short | d1186c3 | 2017-04-13 10:20:04 -0400 | [diff] [blame] | 199 | if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 200 | s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING; |
| 201 | } |
Matt Caswell | f7e393b | 2017-02-27 11:19:57 +0000 | [diff] [blame] | 202 | } 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 Caswell | d7f8783 | 2017-02-25 15:59:44 +0000 | [diff] [blame] | 206 | } |
Matt Caswell | 564547e | 2017-02-25 15:34:07 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Matt Caswell | 31fd10e | 2015-10-22 12:18:45 +0100 | [diff] [blame] | 209 | void 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 Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 223 | int ossl_statem_connect(SSL *s) |
| 224 | { |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 225 | return state_machine(s, 0); |
| 226 | } |
| 227 | |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 228 | int ossl_statem_accept(SSL *s) |
Matt Caswell | c130dd8 | 2015-09-04 13:51:49 +0100 | [diff] [blame] | 229 | { |
| 230 | return state_machine(s, 1); |
| 231 | } |
| 232 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 233 | typedef void (*info_cb) (const SSL *, int, int); |
| 234 | |
| 235 | static info_cb get_callback(SSL *s) |
Matt Caswell | 91eac8d | 2015-10-05 11:28:51 +0100 | [diff] [blame] | 236 | { |
| 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 245 | /* |
| 246 | * The main message flow state machine. We start in the MSG_FLOW_UNINITED or |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 247 | * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 248 | * transitions are as follows: |
| 249 | * |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 250 | * MSG_FLOW_UNINITED MSG_FLOW_FINISHED |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 251 | * | | |
| 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 Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 273 | static int state_machine(SSL *s, int server) |
| 274 | { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 275 | BUF_MEM *buf = NULL; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 276 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 277 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 278 | 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 286 | ERR_clear_error(); |
| 287 | clear_sys_error(); |
| 288 | |
Matt Caswell | 91eac8d | 2015-10-05 11:28:51 +0100 | [diff] [blame] | 289 | cb = get_callback(s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 290 | |
Matt Caswell | 024f543 | 2015-10-22 13:57:18 +0100 | [diff] [blame] | 291 | st->in_handshake++; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 292 | if (!SSL_in_init(s) || SSL_in_before(s)) { |
| 293 | if (!SSL_clear(s)) |
| 294 | return -1; |
| 295 | } |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 296 | #ifndef OPENSSL_NO_SCTP |
Matt Caswell | 9924087 | 2017-06-20 16:36:30 +0100 | [diff] [blame] | 297 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) { |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 298 | /* |
| 299 | * Notify SCTP BIO socket to enter handshake mode and prevent stream |
Matt Caswell | 9924087 | 2017-06-20 16:36:30 +0100 | [diff] [blame] | 300 | * identifier other than 0. |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 301 | */ |
| 302 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, |
Matt Caswell | 024f543 | 2015-10-22 13:57:18 +0100 | [diff] [blame] | 303 | st->in_handshake, NULL); |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 304 | } |
| 305 | #endif |
| 306 | |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 307 | /* Initialise state machine */ |
Matt Caswell | 0386aad | 2017-01-10 14:58:17 +0000 | [diff] [blame] | 308 | if (st->state == MSG_FLOW_UNINITED |
Matt Caswell | 0386aad | 2017-01-10 14:58:17 +0000 | [diff] [blame] | 309 | || st->state == MSG_FLOW_FINISHED) { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 310 | if (st->state == MSG_FLOW_UNINITED) { |
| 311 | st->hand_state = TLS_ST_BEFORE; |
Matt Caswell | 0386aad | 2017-01-10 14:58:17 +0000 | [diff] [blame] | 312 | st->request_state = TLS_ST_BEFORE; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 313 | } |
| 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 Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 321 | (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 322 | SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR); |
| 323 | goto end; |
| 324 | } |
| 325 | } else { |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 326 | if ((s->version >> 8) != SSL3_VERSION_MAJOR) { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 327 | SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR); |
| 328 | goto end; |
| 329 | } |
| 330 | } |
| 331 | |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 332 | 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 335 | } |
| 336 | |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 337 | 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 Caswell | 4641756 | 2016-05-17 12:28:14 +0100 | [diff] [blame] | 358 | /* |
| 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 Caswell | f7e393b | 2017-02-27 11:19:57 +0000 | [diff] [blame] | 369 | if ((SSL_in_before(s)) |
Matt Caswell | 49e7fe1 | 2017-02-21 09:22:22 +0000 | [diff] [blame] | 370 | || s->renegotiate) { |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 371 | if (!tls_setup_handshake(s)) { |
| 372 | ossl_statem_set_error(s); |
| 373 | goto end; |
Matt Caswell | 2c4a056 | 2016-06-03 11:59:19 +0100 | [diff] [blame] | 374 | } |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 375 | |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 376 | if (SSL_IS_FIRST_HANDSHAKE(s)) |
| 377 | st->read_state_first_init = 1; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | st->state = MSG_FLOW_WRITING; |
| 381 | init_write_state_machine(s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 382 | } |
| 383 | |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 384 | while (st->state != MSG_FLOW_FINISHED) { |
| 385 | if (st->state == MSG_FLOW_READING) { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 386 | 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 Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 407 | ossl_statem_set_error(s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 408 | goto end; |
| 409 | } |
| 410 | } |
| 411 | |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 412 | ret = 1; |
| 413 | |
| 414 | end: |
Matt Caswell | 024f543 | 2015-10-22 13:57:18 +0100 | [diff] [blame] | 415 | st->in_handshake--; |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 416 | |
| 417 | #ifndef OPENSSL_NO_SCTP |
Matt Caswell | 9924087 | 2017-06-20 16:36:30 +0100 | [diff] [blame] | 418 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) { |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 419 | /* |
| 420 | * Notify SCTP BIO socket to leave handshake mode and allow stream |
Matt Caswell | 9924087 | 2017-06-20 16:36:30 +0100 | [diff] [blame] | 421 | * identifier other than 0. |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 422 | */ |
| 423 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, |
Matt Caswell | 024f543 | 2015-10-22 13:57:18 +0100 | [diff] [blame] | 424 | st->in_handshake, NULL); |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 425 | } |
| 426 | #endif |
| 427 | |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 428 | 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 | */ |
| 441 | static void init_read_state_machine(SSL *s) |
| 442 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 443 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 444 | |
| 445 | st->read_state = READ_STATE_HEADER; |
| 446 | } |
| 447 | |
Matt Caswell | 0d698f6 | 2016-09-23 16:58:11 +0100 | [diff] [blame] | 448 | static 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 463 | /* |
| 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 | * |
FdaSilvaYY | 0d4fb84 | 2016-02-05 15:23:54 -0500 | [diff] [blame] | 485 | * Any of the above states could result in an NBIO event occurring in which case |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 486 | * 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 Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 489 | static SUB_STATE_RETURN read_state_machine(SSL *s) |
| 490 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 491 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 492 | int ret, mt; |
Matt Caswell | eda7575 | 2016-09-06 12:05:25 +0100 | [diff] [blame] | 493 | size_t len = 0; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 494 | int (*transition) (SSL *s, int mt); |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 495 | PACKET pkt; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 496 | MSG_PROCESS_RETURN(*process_message) (SSL *s, PACKET *pkt); |
| 497 | WORK_STATE(*post_process_message) (SSL *s, WORK_STATE wst); |
Matt Caswell | eda7575 | 2016-09-06 12:05:25 +0100 | [diff] [blame] | 498 | size_t (*max_message_size) (SSL *s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 499 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
| 500 | |
Matt Caswell | 91eac8d | 2015-10-05 11:28:51 +0100 | [diff] [blame] | 501 | cb = get_callback(s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 502 | |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 503 | if (s->server) { |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 504 | 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 508 | } else { |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 509 | 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | if (st->read_state_first_init) { |
| 516 | s->first_packet = 1; |
| 517 | st->read_state_first_init = 0; |
| 518 | } |
| 519 | |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 520 | while (1) { |
| 521 | switch (st->read_state) { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 522 | case READ_STATE_HEADER: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 523 | /* Get the state the peer wants to move to */ |
Matt Caswell | 76af303 | 2015-08-11 11:41:03 +0100 | [diff] [blame] | 524 | 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 532 | |
| 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 | */ |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 549 | if (!transition(s, mt)) { |
Matt Caswell | 672f333 | 2016-06-22 19:43:46 +0100 | [diff] [blame] | 550 | ossl_statem_set_error(s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 551 | 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 Caswell | c1ef7c9 | 2016-09-19 11:39:21 +0100 | [diff] [blame] | 560 | /* dtls_get_message already did this */ |
| 561 | if (!SSL_IS_DTLS(s) |
| 562 | && s->s3->tmp.message_size > 0 |
Matt Caswell | 0d698f6 | 2016-09-23 16:58:11 +0100 | [diff] [blame] | 563 | && !grow_init_buf(s, s->s3->tmp.message_size |
| 564 | + SSL3_HM_HEADER_LENGTH)) { |
Matt Caswell | c1ef7c9 | 2016-09-19 11:39:21 +0100 | [diff] [blame] | 565 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Richard Levitte | a449b47 | 2016-09-22 10:15:02 +0200 | [diff] [blame] | 566 | SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_BUF_LIB); |
Matt Caswell | c1ef7c9 | 2016-09-19 11:39:21 +0100 | [diff] [blame] | 567 | return SUB_STATE_ERROR; |
| 568 | } |
| 569 | |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 570 | 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 Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 584 | 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 Caswell | 1689e7e | 2016-05-12 17:18:32 +0100 | [diff] [blame] | 590 | |
| 591 | /* Discard the packet data */ |
| 592 | s->init_num = 0; |
| 593 | |
Alessandro Ghedini | 4f8a5f4 | 2016-09-14 00:51:02 +0100 | [diff] [blame] | 594 | switch (ret) { |
| 595 | case MSG_PROCESS_ERROR: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 596 | return SUB_STATE_ERROR; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 597 | |
Alessandro Ghedini | 4f8a5f4 | 2016-09-14 00:51:02 +0100 | [diff] [blame] | 598 | case MSG_PROCESS_FINISHED_READING: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 599 | if (SSL_IS_DTLS(s)) { |
| 600 | dtls1_stop_timer(s); |
| 601 | } |
| 602 | return SUB_STATE_FINISHED; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 603 | |
Alessandro Ghedini | 4f8a5f4 | 2016-09-14 00:51:02 +0100 | [diff] [blame] | 604 | case MSG_PROCESS_CONTINUE_PROCESSING: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 605 | st->read_state = READ_STATE_POST_PROCESS; |
| 606 | st->read_state_work = WORK_MORE_A; |
Alessandro Ghedini | 4f8a5f4 | 2016-09-14 00:51:02 +0100 | [diff] [blame] | 607 | break; |
| 608 | |
| 609 | default: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 610 | st->read_state = READ_STATE_HEADER; |
Alessandro Ghedini | 4f8a5f4 | 2016-09-14 00:51:02 +0100 | [diff] [blame] | 611 | break; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 612 | } |
| 613 | break; |
| 614 | |
| 615 | case READ_STATE_POST_PROCESS: |
| 616 | st->read_state_work = post_process_message(s, st->read_state_work); |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 617 | switch (st->read_state_work) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 618 | case WORK_ERROR: |
| 619 | case WORK_MORE_A: |
| 620 | case WORK_MORE_B: |
Benjamin Kaduk | ddf9725 | 2017-02-06 15:33:28 -0600 | [diff] [blame] | 621 | case WORK_MORE_C: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 622 | 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 Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 640 | ossl_statem_set_error(s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 641 | return SUB_STATE_ERROR; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Send a previously constructed message to the peer. |
| 648 | */ |
| 649 | static int statem_do_write(SSL *s) |
| 650 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 651 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 652 | |
| 653 | if (st->hand_state == TLS_ST_CW_CHANGE |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 654 | || st->hand_state == TLS_ST_SW_CHANGE) { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 655 | 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 | */ |
| 667 | static void init_write_state_machine(SSL *s) |
| 668 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 669 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 670 | |
| 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 |
FdaSilvaYY | 0d4fb84 | 2016-02-05 15:23:54 -0500 | [diff] [blame] | 694 | * sending of the message. This could result in an NBIO event occurring in |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 695 | * 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 Caswell | d78052c | 2015-10-05 11:03:27 +0100 | [diff] [blame] | 705 | static SUB_STATE_RETURN write_state_machine(SSL *s) |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 706 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 707 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 708 | int ret; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 709 | 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 Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 712 | int (*get_construct_message_f) (SSL *s, WPACKET *pkt, |
| 713 | int (**confunc) (SSL *s, WPACKET *pkt), |
| 714 | int *mt); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 715 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 716 | int (*confunc) (SSL *s, WPACKET *pkt); |
| 717 | int mt; |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 718 | WPACKET pkt; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 719 | |
Matt Caswell | 91eac8d | 2015-10-05 11:28:51 +0100 | [diff] [blame] | 720 | cb = get_callback(s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 721 | |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 722 | if (s->server) { |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 723 | transition = ossl_statem_server_write_transition; |
| 724 | pre_work = ossl_statem_server_pre_work; |
| 725 | post_work = ossl_statem_server_post_work; |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 726 | get_construct_message_f = ossl_statem_server_construct_message; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 727 | } else { |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 728 | transition = ossl_statem_client_write_transition; |
| 729 | pre_work = ossl_statem_client_pre_work; |
| 730 | post_work = ossl_statem_client_post_work; |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 731 | get_construct_message_f = ossl_statem_client_construct_message; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 732 | } |
| 733 | |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 734 | while (1) { |
| 735 | switch (st->write_state) { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 736 | 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 | } |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 744 | switch (transition(s)) { |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 745 | 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 Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 754 | case WRITE_TRAN_ERROR: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 755 | return SUB_STATE_ERROR; |
| 756 | } |
| 757 | break; |
| 758 | |
| 759 | case WRITE_STATE_PRE_WORK: |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 760 | switch (st->write_state_work = pre_work(s, st->write_state_work)) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 761 | case WORK_ERROR: |
| 762 | case WORK_MORE_A: |
| 763 | case WORK_MORE_B: |
Benjamin Kaduk | ddf9725 | 2017-02-06 15:33:28 -0600 | [diff] [blame] | 764 | case WORK_MORE_C: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 765 | 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 Caswell | f7e393b | 2017-02-27 11:19:57 +0000 | [diff] [blame] | 774 | 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 Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 784 | if (!WPACKET_init(&pkt, s->init_buf) |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 785 | || !ssl_set_handshake_header(s, &pkt, mt) |
| 786 | || (confunc != NULL && !confunc(s, &pkt)) |
| 787 | || !ssl_close_construct_packet(s, &pkt, mt) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 788 | || !WPACKET_finish(&pkt)) { |
| 789 | WPACKET_cleanup(&pkt); |
| 790 | ossl_statem_set_error(s); |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 791 | return SUB_STATE_ERROR; |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 792 | } |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 793 | |
| 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: |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 809 | switch (st->write_state_work = post_work(s, st->write_state_work)) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 810 | case WORK_ERROR: |
| 811 | case WORK_MORE_A: |
| 812 | case WORK_MORE_B: |
Benjamin Kaduk | ddf9725 | 2017-02-06 15:33:28 -0600 | [diff] [blame] | 813 | case WORK_MORE_C: |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 814 | 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 Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 832 | * Flush the write BIO |
| 833 | */ |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 834 | int statem_flush(SSL *s) |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 835 | { |
| 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 Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 846 | * Called by the record layer to determine whether application data is |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 847 | * allowed to be received in the current handshake state or not. |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 848 | * |
| 849 | * Return values are: |
| 850 | * 1: Yes (application data allowed) |
| 851 | * 0: No (application data not allowed) |
| 852 | */ |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 853 | int ossl_statem_app_data_allowed(SSL *s) |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 854 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 855 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | f8e0a55 | 2015-07-29 14:23:56 +0100 | [diff] [blame] | 856 | |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 857 | if (st->state == MSG_FLOW_UNINITED) |
Matt Caswell | 94836de | 2015-09-08 09:19:22 +0100 | [diff] [blame] | 858 | return 0; |
| 859 | |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 860 | if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0)) |
| 861 | return 0; |
| 862 | |
Matt Caswell | 94836de | 2015-09-08 09:19:22 +0100 | [diff] [blame] | 863 | 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 Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 869 | || st->hand_state == TLS_ST_SR_CLNT_HELLO) |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 870 | return 1; |
Matt Caswell | 94836de | 2015-09-08 09:19:22 +0100 | [diff] [blame] | 871 | } 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 Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 878 | } |
| 879 | |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 880 | return 0; |
| 881 | } |