2 * Copyright 2022 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 (QCSO) 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. */
44 /* Is this stream in blocking mode? */
45 unsigned int blocking : 1;
48 * This state tracks SSL_write all-or-nothing (AON) write semantics
53 * t=0: aon_write_in_progress=0
54 * t=1: SSL_write(ssl, b1, l1) called;
55 * too big to enqueue into sstream at once, SSL_ERROR_WANT_WRITE;
56 * aon_write_in_progress=1; aon_buf_base=b1; aon_buf_len=l1;
57 * aon_buf_pos < l1 (depends on how much room was in sstream);
58 * t=2: SSL_write(ssl, b2, l2);
59 * b2 must equal b1 (validated unless ACCEPT_MOVING_WRITE_BUFFER)
60 * l2 must equal l1 (always validated)
61 * append into sstream from [b2 + aon_buf_pos, b2 + aon_buf_len)
62 * if done, aon_write_in_progess=0
65 /* Is an AON write in progress? */
66 unsigned int aon_write_in_progress : 1;
68 * The base buffer pointer the caller passed us for the initial AON write
69 * call. We use this for validation purposes unless
70 * ACCEPT_MOVING_WRITE_BUFFER is enabled.
72 * NOTE: We never dereference this, as the caller might pass a different
73 * (but identical) buffer if using ACCEPT_MOVING_WRITE_BUFFER. It is for
74 * validation by pointer comparison only.
76 const unsigned char *aon_buf_base;
77 /* The total length of the AON buffer being sent, in bytes. */
80 * The position in the AON buffer up to which we have successfully sent data
91 * ssl_st is a common header for ordinary SSL objects, QUIC connection
92 * objects and QUIC stream objects, allowing objects of these different
93 * types to be disambiguated at runtime and providing some common fields.
95 * Note: This must come first in the QUIC_CONNECTION structure.
102 * The QUIC channel providing the core QUIC connection implementation. Note
103 * that this is not instantiated until we actually start trying to do the
104 * handshake. This is to allow us to gather information like whether we are
105 * going to be in client or server mode before committing to instantiating
106 * the channel, since we want to determine the channel arguments based on
109 * The channel remains available after connection termination until the SSL
110 * object is freed, thus (ch != NULL) iff (started == 1).
115 * The mutex used to synchronise access to the QUIC_CHANNEL. We own this but
116 * provide it to the channel.
121 * If we have a default stream attached, this is the internal XSO
122 * object. If there is no default stream, this is NULL.
124 QUIC_XSO *default_xso;
126 /* The network read and write BIOs. */
127 BIO *net_rbio, *net_wbio;
129 /* Initial peer L4 address. */
130 BIO_ADDR init_peer_addr;
132 # ifndef OPENSSL_NO_QUIC_THREAD_ASSIST
133 /* Manages thread for QUIC thread assisted mode. */
134 QUIC_THREAD_ASSIST thread_assist;
137 /* If non-NULL, used instead of ossl_time_now(). Used for testing. */
138 OSSL_TIME (*override_now_cb)(void *arg);
139 void *override_now_cb_arg;
141 /* Number of XSOs allocated. Includes the default XSO, if any. */
144 /* Have we started? */
145 unsigned int started : 1;
147 /* Can the read and write network BIOs support blocking? */
148 unsigned int can_poll_net_rbio : 1;
149 unsigned int can_poll_net_wbio : 1;
152 * This is 1 if we were instantiated using a QUIC server method
155 unsigned int as_server : 1;
158 * Has the application called SSL_set_accept_state? We require this to be
159 * congruent with the value of as_server.
161 unsigned int as_server_state : 1;
163 /* Are we using thread assisted mode? Never changes after init. */
164 unsigned int is_thread_assisted : 1;
166 /* Do connection-level operations (e.g. handshakes) run in blocking mode? */
167 unsigned int blocking : 1;
169 /* Do newly created streams start in blocking mode? Inherited by new XSOs. */
170 unsigned int default_blocking : 1;
172 /* Have we created a default XSO yet? */
173 unsigned int default_xso_created : 1;
175 /* Default stream type. Defaults to SSL_DEFAULT_STREAM_MODE_AUTO_BIDI. */
176 uint32_t default_stream_mode;
178 /* SSL_set_mode. This is not used directly but inherited by new XSOs. */
179 uint32_t default_ssl_mode;
182 * Last 'normal' error during an app-level I/O operation, used by
183 * SSL_get_error(); used to track data-path errors like SSL_ERROR_WANT_READ
184 * and SSL_ERROR_WANT_WRITE.
189 /* Internal calls to the QUIC CSM which come from various places. */
190 int ossl_quic_conn_on_handshake_confirmed(QUIC_CONNECTION *qc);
193 * To be called when a protocol violation occurs. The connection is torn down
194 * with the given error code, which should be a QUIC_ERR_* value. Reason string
195 * is optional and copied if provided. frame_type should be 0 if not applicable.
197 void ossl_quic_conn_raise_protocol_error(QUIC_CONNECTION *qc,
202 void ossl_quic_conn_on_remote_conn_close(QUIC_CONNECTION *qc,
203 OSSL_QUIC_FRAME_CONN_CLOSE *f);
205 # define OSSL_QUIC_ANY_VERSION 0xFFFFF
207 # define QUIC_CONNECTION_FROM_SSL_int(ssl, c) \
208 ((ssl) == NULL ? NULL \
209 : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
210 ? (c QUIC_CONNECTION *)(ssl) \
213 # define QUIC_XSO_FROM_SSL_int(ssl, c) \
216 : (((ssl)->type == SSL_TYPE_QUIC_XSO \
217 ? (c QUIC_XSO *)(ssl) \
218 : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
219 ? (c QUIC_XSO *)((QUIC_CONNECTION *)(ssl))->default_xso \
222 # define SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, c) \
223 ((ssl) == NULL ? NULL \
224 : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
225 ? (c SSL_CONNECTION *)((c QUIC_CONNECTION *)(ssl))->tls \
228 # define IS_QUIC(ssl) ((ssl) != NULL \
229 && ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
230 || (ssl)->type == SSL_TYPE_QUIC_XSO))
232 # define QUIC_CONNECTION_FROM_SSL_int(ssl, c) NULL
233 # define QUIC_XSO_FROM_SSL_int(ssl, c) NULL
234 # define SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, c) NULL
235 # define IS_QUIC(ssl) 0
238 # define QUIC_CONNECTION_FROM_SSL(ssl) \
239 QUIC_CONNECTION_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
240 # define QUIC_CONNECTION_FROM_CONST_SSL(ssl) \
241 QUIC_CONNECTION_FROM_SSL_int(ssl, const)
242 # define QUIC_XSO_FROM_SSL(ssl) \
243 QUIC_XSO_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
244 # define QUIC_XSO_FROM_CONST_SSL(ssl) \
245 QUIC_XSO_FROM_SSL_int(ssl, const)
246 # define SSL_CONNECTION_FROM_QUIC_SSL(ssl) \
247 SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
248 # define SSL_CONNECTION_FROM_CONST_QUIC_SSL(ssl) \
249 SSL_CONNECTION_FROM_CONST_QUIC_SSL_int(ssl, const)
251 # define IMPLEMENT_quic_meth_func(version, func_name, q_accept, \
252 q_connect, enc_data) \
253 const SSL_METHOD *func_name(void) \
255 static const SSL_METHOD func_name##_data= { \
270 NULL /* shutdown */, \
271 NULL /* renegotiate */, \
272 ossl_quic_renegotiate_check, \
273 NULL /* read_bytes */, \
274 NULL /* write_bytes */, \
275 NULL /* dispatch_alert */, \
277 ossl_quic_ctx_ctrl, \
278 NULL /* get_cipher_by_char */, \
279 NULL /* put_cipher_by_char */, \
281 ossl_quic_num_ciphers, \
282 ossl_quic_get_cipher, \
283 tls1_default_timeout, \
285 ssl_undefined_void_function, \
286 ossl_quic_callback_ctrl, \
287 ossl_quic_ctx_callback_ctrl, \
289 return &func_name##_data; \