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