2 * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
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
9 #ifndef OSSL_INTERNAL_STATEM_H
10 # define OSSL_INTERNAL_STATEM_H
12 /*****************************************************************************
14 * These enums should be considered PRIVATE to the state machine. No *
15 * non-state machine code should need to use these *
17 *****************************************************************************/
19 * Valid return codes used for functions performing work prior to or after
20 * sending or receiving a message
23 /* Something went wrong */
25 /* We're done working and there shouldn't be anything else to do after */
27 /* We're done working move onto the next thing */
28 WORK_FINISHED_CONTINUE,
29 /* We're working on phase A */
31 /* We're working on phase B */
33 /* We're working on phase C */
37 /* Write transition return codes */
39 /* Something went wrong */
41 /* A transition was successfully completed and we should continue */
43 /* There is no more write work to be done */
47 /* Message flow states */
49 /* No handshake in progress */
51 /* A permanent error with this connection */
53 /* We are reading messages */
55 /* We are writing messages */
57 /* Handshake has finished */
65 READ_STATE_POST_PROCESS
70 WRITE_STATE_TRANSITION,
82 typedef int (*ossl_statem_mutate_handshake_cb)(const unsigned char *msgin,
84 unsigned char **msgout,
88 typedef void (*ossl_statem_finish_mutate_handshake_cb)(void *arg);
90 /*****************************************************************************
92 * This structure should be considered "opaque" to anything outside of the *
93 * state machine. No non-state machine code should be accessing the members *
94 * of this structure. *
96 *****************************************************************************/
98 struct ossl_statem_st {
100 WRITE_STATE write_state;
101 WORK_STATE write_state_work;
102 READ_STATE read_state;
103 WORK_STATE read_state_work;
104 OSSL_HANDSHAKE_STATE hand_state;
105 /* The handshake state requested by an API call (e.g. HelloRequest) */
106 OSSL_HANDSHAKE_STATE request_state;
108 int read_state_first_init;
109 /* true when we are actually in SSL_accept() or SSL_connect() */
112 * True when are processing a "real" handshake that needs cleaning up (not
113 * just a HelloRequest or similar).
116 /* Should we skip the CertificateVerify message? */
117 unsigned int no_cert_verify;
120 /* Test harness message mutator callbacks */
121 ossl_statem_mutate_handshake_cb mutate_handshake_cb;
122 ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb;
124 unsigned int write_in_progress : 1;
126 typedef struct ossl_statem_st OSSL_STATEM;
128 /*****************************************************************************
130 * The following macros/functions represent the libssl internal API to the *
131 * state machine. Any libssl code may call these functions/macros *
133 *****************************************************************************/
135 typedef struct ssl_connection_st SSL_CONNECTION;
137 __owur int ossl_statem_accept(SSL *s);
138 __owur int ossl_statem_connect(SSL *s);
139 OSSL_HANDSHAKE_STATE ossl_statem_get_state(SSL_CONNECTION *s);
140 void ossl_statem_clear(SSL_CONNECTION *s);
141 void ossl_statem_set_renegotiate(SSL_CONNECTION *s);
142 void ossl_statem_send_fatal(SSL_CONNECTION *s, int al);
143 void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason,
144 const char *fmt, ...);
145 # define SSLfatal_alert(s, al) ossl_statem_send_fatal((s), (al))
146 # define SSLfatal(s, al, r) SSLfatal_data((s), (al), (r), NULL)
147 # define SSLfatal_data \
149 ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC), \
152 int ossl_statem_in_error(const SSL_CONNECTION *s);
153 void ossl_statem_set_in_init(SSL_CONNECTION *s, int init);
154 int ossl_statem_get_in_handshake(SSL_CONNECTION *s);
155 void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand);
156 __owur int ossl_statem_skip_early_data(SSL_CONNECTION *s);
157 void ossl_statem_check_finish_init(SSL_CONNECTION *s, int send);
158 void ossl_statem_set_hello_verify_done(SSL_CONNECTION *s);
159 __owur int ossl_statem_app_data_allowed(SSL_CONNECTION *s);
160 __owur int ossl_statem_export_allowed(SSL_CONNECTION *s);
161 __owur int ossl_statem_export_early_allowed(SSL_CONNECTION *s);
163 /* Flush the write BIO */
164 int statem_flush(SSL_CONNECTION *s);
166 int ossl_statem_set_mutator(SSL *s,
167 ossl_statem_mutate_handshake_cb mutate_handshake_cb,
168 ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb,