2 * Copyright 2022-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
10 #ifndef OSSL_QUIC_LOCAL_H
11 # define OSSL_QUIC_LOCAL_H
13 # include <openssl/ssl.h>
14 # include "internal/quic_ssl.h" /* QUIC_CONNECTION */
15 # include "internal/quic_txp.h"
16 # include "internal/quic_statm.h"
17 # include "internal/quic_demux.h"
18 # include "internal/quic_record_rx.h"
19 # include "internal/quic_tls.h"
20 # include "internal/quic_fc.h"
21 # include "internal/quic_stream.h"
22 # include "internal/quic_channel.h"
23 # include "internal/quic_reactor.h"
24 # include "internal/quic_thread_assist.h"
25 # include "../ssl_local.h"
27 # ifndef OPENSSL_NO_QUIC
30 * QUIC stream SSL object (QSSO) type. This implements the API personality layer
31 * for QSSO objects, wrapping the QUIC-native QUIC_STREAM object and tracking
32 * state required by the libssl API personality.
35 /* SSL object common header. */
38 /* The connection this stream is associated with. Always non-NULL. */
39 QUIC_CONNECTION *conn;
41 /* The stream object. Always non-NULL for as long as the XSO exists. */
45 * Has this stream been logically configured into blocking mode? Only
46 * meaningful if desires_blocking_set is 1. Ignored if blocking is not
47 * currently possible given QUIC_CONNECTION configuration.
49 unsigned int desires_blocking : 1;
52 * Has SSL_set_blocking_mode been called on this stream? If not set, we
53 * inherit from the QUIC_CONNECTION blocking state.
55 unsigned int desires_blocking_set : 1;
57 /* The application has retired a FIN (i.e. SSL_ERROR_ZERO_RETURN). */
58 unsigned int retired_fin : 1;
61 * The application has requested a reset. Not set for reflexive
62 * STREAM_RESETs caused by peer STOP_SENDING.
64 unsigned int requested_reset : 1;
67 * This state tracks SSL_write all-or-nothing (AON) write semantics
72 * t=0: aon_write_in_progress=0
73 * t=1: SSL_write(ssl, b1, l1) called;
74 * too big to enqueue into sstream at once, SSL_ERROR_WANT_WRITE;
75 * aon_write_in_progress=1; aon_buf_base=b1; aon_buf_len=l1;
76 * aon_buf_pos < l1 (depends on how much room was in sstream);
77 * t=2: SSL_write(ssl, b2, l2);
78 * b2 must equal b1 (validated unless ACCEPT_MOVING_WRITE_BUFFER)
79 * l2 must equal l1 (always validated)
80 * append into sstream from [b2 + aon_buf_pos, b2 + aon_buf_len)
81 * if done, aon_write_in_progress=0
84 /* Is an AON write in progress? */
85 unsigned int aon_write_in_progress : 1;
87 * The base buffer pointer the caller passed us for the initial AON write
88 * call. We use this for validation purposes unless
89 * ACCEPT_MOVING_WRITE_BUFFER is enabled.
91 * NOTE: We never dereference this, as the caller might pass a different
92 * (but identical) buffer if using ACCEPT_MOVING_WRITE_BUFFER. It is for
93 * validation by pointer comparison only.
95 const unsigned char *aon_buf_base;
96 /* The total length of the AON buffer being sent, in bytes. */
99 * The position in the AON buffer up to which we have successfully sent data
107 /* SSL_set_options */
108 uint64_t ssl_options;
111 * Last 'normal' error during an app-level I/O operation, used by
112 * SSL_get_error(); used to track data-path errors like SSL_ERROR_WANT_READ
113 * and SSL_ERROR_WANT_WRITE.
118 struct quic_conn_st {
120 * ssl_st is a common header for ordinary SSL objects, QUIC connection
121 * objects and QUIC stream objects, allowing objects of these different
122 * types to be disambiguated at runtime and providing some common fields.
124 * Note: This must come first in the QUIC_CONNECTION structure.
130 /* The QUIC engine representing the QUIC event domain. */
133 /* The QUIC port representing the QUIC listener and socket. */
137 * The QUIC channel providing the core QUIC connection implementation. Note
138 * that this is not instantiated until we actually start trying to do the
139 * handshake. This is to allow us to gather information like whether we are
140 * going to be in client or server mode before committing to instantiating
141 * the channel, since we want to determine the channel arguments based on
144 * The channel remains available after connection termination until the SSL
145 * object is freed, thus (ch != NULL) iff (started == 1).
150 * The mutex used to synchronise access to the QUIC_CHANNEL. We own this but
151 * provide it to the channel.
156 * If we have a default stream attached, this is the internal XSO
157 * object. If there is no default stream, this is NULL.
159 QUIC_XSO *default_xso;
161 /* The network read and write BIOs. */
162 BIO *net_rbio, *net_wbio;
164 /* Initial peer L4 address. */
165 BIO_ADDR init_peer_addr;
167 # ifndef OPENSSL_NO_QUIC_THREAD_ASSIST
168 /* Manages thread for QUIC thread assisted mode. */
169 QUIC_THREAD_ASSIST thread_assist;
172 /* If non-NULL, used instead of ossl_time_now(). Used for testing. */
173 OSSL_TIME (*override_now_cb)(void *arg);
174 void *override_now_cb_arg;
176 /* Number of XSOs allocated. Includes the default XSO, if any. */
179 /* Have we started? */
180 unsigned int started : 1;
183 * This is 1 if we were instantiated using a QUIC server method
186 unsigned int as_server : 1;
189 * Has the application called SSL_set_accept_state? We require this to be
190 * congruent with the value of as_server.
192 unsigned int as_server_state : 1;
194 /* Are we using thread assisted mode? Never changes after init. */
195 unsigned int is_thread_assisted : 1;
197 /* Do connection-level operations (e.g. handshakes) run in blocking mode? */
198 unsigned int blocking : 1;
200 /* Does the application want blocking mode? */
201 unsigned int desires_blocking : 1;
203 /* Have we created a default XSO yet? */
204 unsigned int default_xso_created : 1;
207 * Pre-TERMINATING shutdown phase in which we are flushing streams.
208 * Monotonically transitions to 1.
209 * New streams cannot be created in this state.
211 unsigned int shutting_down : 1;
213 /* Have we probed the BIOs for addressing support? */
214 unsigned int addressing_probe_done : 1;
216 /* Are we using addressed mode (BIO_sendmmsg with non-NULL peer)? */
217 unsigned int addressed_mode_w : 1;
218 unsigned int addressed_mode_r : 1;
220 /* Default stream type. Defaults to SSL_DEFAULT_STREAM_MODE_AUTO_BIDI. */
221 uint32_t default_stream_mode;
223 /* SSL_set_mode. This is not used directly but inherited by new XSOs. */
224 uint32_t default_ssl_mode;
226 /* SSL_set_options. This is not used directly but inherited by new XSOs. */
227 uint64_t default_ssl_options;
229 /* SSL_set_incoming_stream_policy. */
230 int incoming_stream_policy;
231 uint64_t incoming_stream_aec;
234 * Last 'normal' error during an app-level I/O operation, used by
235 * SSL_get_error(); used to track data-path errors like SSL_ERROR_WANT_READ
236 * and SSL_ERROR_WANT_WRITE.
241 /* Internal calls to the QUIC CSM which come from various places. */
242 int ossl_quic_conn_on_handshake_confirmed(QUIC_CONNECTION *qc);
245 * To be called when a protocol violation occurs. The connection is torn down
246 * with the given error code, which should be a QUIC_ERR_* value. Reason string
247 * is optional and copied if provided. frame_type should be 0 if not applicable.
249 void ossl_quic_conn_raise_protocol_error(QUIC_CONNECTION *qc,
254 void ossl_quic_conn_on_remote_conn_close(QUIC_CONNECTION *qc,
255 OSSL_QUIC_FRAME_CONN_CLOSE *f);
257 int ossl_quic_trace(int write_p, int version, int content_type,
258 const void *buf, size_t msglen, SSL *ssl, void *arg);
260 # define OSSL_QUIC_ANY_VERSION 0xFFFFF
261 # define IS_QUIC_METHOD(m) \
262 ((m) == OSSL_QUIC_client_method() || \
263 (m) == OSSL_QUIC_client_thread_method())
264 # define IS_QUIC_CTX(ctx) IS_QUIC_METHOD((ctx)->method)
266 # define QUIC_CONNECTION_FROM_SSL_int(ssl, c) \
267 ((ssl) == NULL ? NULL \
268 : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
269 ? (c QUIC_CONNECTION *)(ssl) \
272 # define QUIC_XSO_FROM_SSL_int(ssl, c) \
275 : (((ssl)->type == SSL_TYPE_QUIC_XSO \
276 ? (c QUIC_XSO *)(ssl) \
277 : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
278 ? (c QUIC_XSO *)((QUIC_CONNECTION *)(ssl))->default_xso \
281 # define SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, c) \
282 ((ssl) == NULL ? NULL \
283 : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
284 ? (c SSL_CONNECTION *)((c QUIC_CONNECTION *)(ssl))->tls \
287 # define IS_QUIC(ssl) ((ssl) != NULL \
288 && ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
289 || (ssl)->type == SSL_TYPE_QUIC_XSO))
291 # define QUIC_CONNECTION_FROM_SSL_int(ssl, c) NULL
292 # define QUIC_XSO_FROM_SSL_int(ssl, c) NULL
293 # define SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, c) NULL
294 # define IS_QUIC(ssl) 0
295 # define IS_QUIC_CTX(ctx) 0
296 # define IS_QUIC_METHOD(m) 0
299 # define QUIC_CONNECTION_FROM_SSL(ssl) \
300 QUIC_CONNECTION_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
301 # define QUIC_CONNECTION_FROM_CONST_SSL(ssl) \
302 QUIC_CONNECTION_FROM_SSL_int(ssl, const)
303 # define QUIC_XSO_FROM_SSL(ssl) \
304 QUIC_XSO_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
305 # define QUIC_XSO_FROM_CONST_SSL(ssl) \
306 QUIC_XSO_FROM_SSL_int(ssl, const)
307 # define SSL_CONNECTION_FROM_QUIC_SSL(ssl) \
308 SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
309 # define SSL_CONNECTION_FROM_CONST_QUIC_SSL(ssl) \
310 SSL_CONNECTION_FROM_CONST_QUIC_SSL_int(ssl, const)
312 # define IMPLEMENT_quic_meth_func(version, func_name, q_accept, \
313 q_connect, enc_data) \
314 const SSL_METHOD *func_name(void) \
316 static const SSL_METHOD func_name##_data= { \
331 NULL /* shutdown */, \
332 NULL /* renegotiate */, \
333 ossl_quic_renegotiate_check, \
334 NULL /* read_bytes */, \
335 NULL /* write_bytes */, \
336 NULL /* dispatch_alert */, \
338 ossl_quic_ctx_ctrl, \
339 ossl_quic_get_cipher_by_char, \
340 NULL /* put_cipher_by_char */, \
342 ossl_quic_num_ciphers, \
343 ossl_quic_get_cipher, \
344 tls1_default_timeout, \
346 ssl_undefined_void_function, \
347 ossl_quic_callback_ctrl, \
348 ossl_quic_ctx_callback_ctrl, \
350 return &func_name##_data; \