1 /* 2 * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 #ifndef OSSL_INTERNAL_STATEM_H 10 # define OSSL_INTERNAL_STATEM_H 11 12 /***************************************************************************** 13 * * 14 * These enums should be considered PRIVATE to the state machine. No * 15 * non-state machine code should need to use these * 16 * * 17 *****************************************************************************/ 18 /* 19 * Valid return codes used for functions performing work prior to or after 20 * sending or receiving a message 21 */ 22 typedef enum { 23 /* Something went wrong */ 24 WORK_ERROR, 25 /* We're done working and there shouldn't be anything else to do after */ 26 WORK_FINISHED_STOP, 27 /* We're done working move onto the next thing */ 28 WORK_FINISHED_CONTINUE, 29 /* We're done writing, start reading (or vice versa) */ 30 WORK_FINISHED_SWAP, 31 /* We're working on phase A */ 32 WORK_MORE_A, 33 /* We're working on phase B */ 34 WORK_MORE_B, 35 /* We're working on phase C */ 36 WORK_MORE_C 37 } WORK_STATE; 38 39 /* Write transition return codes */ 40 typedef enum { 41 /* Something went wrong */ 42 WRITE_TRAN_ERROR, 43 /* A transition was successfully completed and we should continue */ 44 WRITE_TRAN_CONTINUE, 45 /* There is no more write work to be done */ 46 WRITE_TRAN_FINISHED 47 } WRITE_TRAN; 48 49 /* Message flow states */ 50 typedef enum { 51 /* No handshake in progress */ 52 MSG_FLOW_UNINITED, 53 /* A permanent error with this connection */ 54 MSG_FLOW_ERROR, 55 /* We are reading messages */ 56 MSG_FLOW_READING, 57 /* We are writing messages */ 58 MSG_FLOW_WRITING, 59 /* Handshake has finished */ 60 MSG_FLOW_FINISHED 61 } MSG_FLOW_STATE; 62 63 /* Read states */ 64 typedef enum { 65 READ_STATE_HEADER, 66 READ_STATE_BODY, 67 READ_STATE_POST_PROCESS 68 } READ_STATE; 69 70 /* Write states */ 71 typedef enum { 72 WRITE_STATE_TRANSITION, 73 WRITE_STATE_PRE_WORK, 74 WRITE_STATE_SEND, 75 WRITE_STATE_POST_WORK 76 } WRITE_STATE; 77 78 typedef enum { 79 CON_FUNC_ERROR = 0, 80 CON_FUNC_SUCCESS, 81 CON_FUNC_DONT_SEND 82 } CON_FUNC_RETURN; 83 84 typedef int (*ossl_statem_mutate_handshake_cb)(const unsigned char *msgin, 85 size_t inlen, 86 unsigned char **msgout, 87 size_t *outlen, 88 void *arg); 89 90 typedef void (*ossl_statem_finish_mutate_handshake_cb)(void *arg); 91 92 /***************************************************************************** 93 * * 94 * This structure should be considered "opaque" to anything outside of the * 95 * state machine. No non-state machine code should be accessing the members * 96 * of this structure. * 97 * * 98 *****************************************************************************/ 99 100 struct ossl_statem_st { 101 MSG_FLOW_STATE state; 102 WRITE_STATE write_state; 103 WORK_STATE write_state_work; 104 READ_STATE read_state; 105 WORK_STATE read_state_work; 106 OSSL_HANDSHAKE_STATE hand_state; 107 /* The handshake state requested by an API call (e.g. HelloRequest) */ 108 OSSL_HANDSHAKE_STATE request_state; 109 int in_init; 110 int read_state_first_init; 111 /* true when we are actually in SSL_accept() or SSL_connect() */ 112 int in_handshake; 113 /* 114 * True when are processing a "real" handshake that needs cleaning up (not 115 * just a HelloRequest or similar). 116 */ 117 int cleanuphand; 118 /* Should we skip the CertificateVerify message? */ 119 unsigned int no_cert_verify; 120 int use_timer; 121 122 /* Test harness message mutator callbacks */ 123 ossl_statem_mutate_handshake_cb mutate_handshake_cb; 124 ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb; 125 void *mutatearg; 126 unsigned int write_in_progress : 1; 127 }; 128 typedef struct ossl_statem_st OSSL_STATEM; 129 130 /***************************************************************************** 131 * * 132 * The following macros/functions represent the libssl internal API to the * 133 * state machine. Any libssl code may call these functions/macros * 134 * * 135 *****************************************************************************/ 136 137 typedef struct ssl_connection_st SSL_CONNECTION; 138 139 __owur int ossl_statem_accept(SSL *s); 140 __owur int ossl_statem_connect(SSL *s); 141 OSSL_HANDSHAKE_STATE ossl_statem_get_state(SSL_CONNECTION *s); 142 void ossl_statem_clear(SSL_CONNECTION *s); 143 void ossl_statem_set_renegotiate(SSL_CONNECTION *s); 144 void ossl_statem_send_fatal(SSL_CONNECTION *s, int al); 145 void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason, 146 const char *fmt, ...); 147 # define SSLfatal_alert(s, al) ossl_statem_send_fatal((s), (al)) 148 # define SSLfatal(s, al, r) SSLfatal_data((s), (al), (r), NULL) 149 # define SSLfatal_data \ 150 (ERR_new(), \ 151 ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC), \ 152 ossl_statem_fatal) 153 154 int ossl_statem_in_error(const SSL_CONNECTION *s); 155 void ossl_statem_set_in_init(SSL_CONNECTION *s, int init); 156 int ossl_statem_get_in_handshake(SSL_CONNECTION *s); 157 void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand); 158 __owur int ossl_statem_skip_early_data(SSL_CONNECTION *s); 159 int ossl_statem_check_finish_init(SSL_CONNECTION *s, int send); 160 void ossl_statem_set_hello_verify_done(SSL_CONNECTION *s); 161 __owur int ossl_statem_app_data_allowed(SSL_CONNECTION *s); 162 __owur int ossl_statem_export_allowed(SSL_CONNECTION *s); 163 __owur int ossl_statem_export_early_allowed(SSL_CONNECTION *s); 164 165 /* Flush the write BIO */ 166 int statem_flush(SSL_CONNECTION *s); 167 168 int ossl_statem_set_mutator(SSL *s, 169 ossl_statem_mutate_handshake_cb mutate_handshake_cb, 170 ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb, 171 void *mutatearg); 172 173 #endif 174