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