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