cd65610cd88d8aa1bdd8dfcddd0e7195242dfcd0
[openssl.git] / ssl / quic / quic_local.h
1 /*
2  * Copyright 2022-2023 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
10 #ifndef OSSL_QUIC_LOCAL_H
11 # define OSSL_QUIC_LOCAL_H
12
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"
26
27 # ifndef OPENSSL_NO_QUIC
28
29 /*
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.
33  */
34 struct quic_xso_st {
35     /* SSL object common header. */
36     struct ssl_st                   ssl;
37
38     /* The connection this stream is associated with. Always non-NULL. */
39     QUIC_CONNECTION                 *conn;
40
41     /* The stream object. Always non-NULL for as long as the XSO exists. */
42     QUIC_STREAM                     *stream;
43
44     /*
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.
48      */
49     unsigned int                    desires_blocking        : 1;
50
51     /*
52      * Has SSL_set_blocking_mode been called on this stream? If not set, we
53      * inherit from the QUIC_CONNECTION blocking state.
54      */
55     unsigned int                    desires_blocking_set    : 1;
56
57     /* The application has retired a FIN (i.e. SSL_ERROR_ZERO_RETURN). */
58     unsigned int                    retired_fin             : 1;
59
60     /*
61      * The application has requested a reset. Not set for reflexive
62      * STREAM_RESETs caused by peer STOP_SENDING.
63      */
64     unsigned int                    requested_reset         : 1;
65
66     /*
67      * This state tracks SSL_write all-or-nothing (AON) write semantics
68      * emulation.
69      *
70      * Example chronology:
71      *
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
82      *
83      */
84     /* Is an AON write in progress? */
85     unsigned int                    aon_write_in_progress   : 1;
86     /*
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.
90      *
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.
94      */
95     const unsigned char             *aon_buf_base;
96     /* The total length of the AON buffer being sent, in bytes. */
97     size_t                          aon_buf_len;
98     /*
99      * The position in the AON buffer up to which we have successfully sent data
100      * so far.
101      */
102     size_t                          aon_buf_pos;
103
104     /* SSL_set_mode */
105     uint32_t                        ssl_mode;
106
107     /* SSL_set_options */
108     uint64_t                        ssl_options;
109
110     /*
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.
114      */
115     int                             last_error;
116 };
117
118 struct quic_conn_st {
119     /*
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.
123      *
124      * Note: This must come first in the QUIC_CONNECTION structure.
125      */
126     struct ssl_st                   ssl;
127
128     SSL                             *tls;
129
130     /* The QUIC engine representing the QUIC event domain. */
131     QUIC_ENGINE                     *engine;
132
133     /* The QUIC port representing the QUIC listener and socket. */
134     QUIC_PORT                       *port;
135
136     /*
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
142      * that.
143      *
144      * The channel remains available after connection termination until the SSL
145      * object is freed, thus (ch != NULL) iff (started == 1).
146      */
147     QUIC_CHANNEL                    *ch;
148
149     /*
150      * The mutex used to synchronise access to the QUIC_CHANNEL. We own this but
151      * provide it to the channel.
152      */
153     CRYPTO_MUTEX                    *mutex;
154
155     /*
156      * If we have a default stream attached, this is the internal XSO
157      * object. If there is no default stream, this is NULL.
158      */
159     QUIC_XSO                        *default_xso;
160
161     /* The network read and write BIOs. */
162     BIO                             *net_rbio, *net_wbio;
163
164     /* Initial peer L4 address. */
165     BIO_ADDR                        init_peer_addr;
166
167 #  ifndef OPENSSL_NO_QUIC_THREAD_ASSIST
168     /* Manages thread for QUIC thread assisted mode. */
169     QUIC_THREAD_ASSIST              thread_assist;
170 #  endif
171
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;
175
176     /* Number of XSOs allocated. Includes the default XSO, if any. */
177     size_t                          num_xso;
178
179     /* Have we started? */
180     unsigned int                    started                 : 1;
181
182     /*
183      * This is 1 if we were instantiated using a QUIC server method
184      * (for future use).
185      */
186     unsigned int                    as_server               : 1;
187
188     /*
189      * Has the application called SSL_set_accept_state? We require this to be
190      * congruent with the value of as_server.
191      */
192     unsigned int                    as_server_state         : 1;
193
194     /* Are we using thread assisted mode? Never changes after init. */
195     unsigned int                    is_thread_assisted      : 1;
196
197     /* Do connection-level operations (e.g. handshakes) run in blocking mode? */
198     unsigned int                    blocking                : 1;
199
200     /* Does the application want blocking mode? */
201     unsigned int                    desires_blocking        : 1;
202
203     /* Have we created a default XSO yet? */
204     unsigned int                    default_xso_created     : 1;
205
206     /*
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.
210      */
211     unsigned int                    shutting_down           : 1;
212
213     /* Have we probed the BIOs for addressing support? */
214     unsigned int                    addressing_probe_done   : 1;
215
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;
219
220     /* Default stream type. Defaults to SSL_DEFAULT_STREAM_MODE_AUTO_BIDI. */
221     uint32_t                        default_stream_mode;
222
223     /* SSL_set_mode. This is not used directly but inherited by new XSOs. */
224     uint32_t                        default_ssl_mode;
225
226     /* SSL_set_options. This is not used directly but inherited by new XSOs. */
227     uint64_t                        default_ssl_options;
228
229     /* SSL_set_incoming_stream_policy. */
230     int                             incoming_stream_policy;
231     uint64_t                        incoming_stream_aec;
232
233     /*
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.
237      */
238     int                             last_error;
239 };
240
241 /* Internal calls to the QUIC CSM which come from various places. */
242 int ossl_quic_conn_on_handshake_confirmed(QUIC_CONNECTION *qc);
243
244 /*
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.
248  */
249 void ossl_quic_conn_raise_protocol_error(QUIC_CONNECTION *qc,
250                                          uint64_t error_code,
251                                          uint64_t frame_type,
252                                          const char *reason);
253
254 void ossl_quic_conn_on_remote_conn_close(QUIC_CONNECTION *qc,
255                                          OSSL_QUIC_FRAME_CONN_CLOSE *f);
256
257 int ossl_quic_trace(int write_p, int version, int content_type,
258                     const void *buf, size_t msglen, SSL *ssl, void *arg);
259
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)
265
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)            \
270          : NULL))
271
272 #  define QUIC_XSO_FROM_SSL_int(ssl, c)                             \
273     ((ssl) == NULL                                                  \
274      ? NULL                                                         \
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  \
279            : NULL))))
280
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 \
285          : NULL))
286
287 #  define IS_QUIC(ssl) ((ssl) != NULL                                   \
288                         && ((ssl)->type == SSL_TYPE_QUIC_CONNECTION     \
289                             || (ssl)->type == SSL_TYPE_QUIC_XSO))
290 # else
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
297 # endif
298
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)
311
312 # define IMPLEMENT_quic_meth_func(version, func_name, q_accept, \
313                                  q_connect, enc_data) \
314 const SSL_METHOD *func_name(void)  \
315         { \
316         static const SSL_METHOD func_name##_data= { \
317                 version, \
318                 0, \
319                 0, \
320                 ossl_quic_new, \
321                 ossl_quic_free, \
322                 ossl_quic_reset, \
323                 ossl_quic_init, \
324                 NULL /* clear */, \
325                 ossl_quic_deinit, \
326                 q_accept, \
327                 q_connect, \
328                 ossl_quic_read, \
329                 ossl_quic_peek, \
330                 ossl_quic_write, \
331                 NULL /* shutdown */, \
332                 NULL /* renegotiate */, \
333                 ossl_quic_renegotiate_check, \
334                 NULL /* read_bytes */, \
335                 NULL /* write_bytes */, \
336                 NULL /* dispatch_alert */, \
337                 ossl_quic_ctrl, \
338                 ossl_quic_ctx_ctrl, \
339                 ossl_quic_get_cipher_by_char, \
340                 NULL /* put_cipher_by_char */, \
341                 ossl_quic_pending, \
342                 ossl_quic_num_ciphers, \
343                 ossl_quic_get_cipher, \
344                 tls1_default_timeout, \
345                 &enc_data, \
346                 ssl_undefined_void_function, \
347                 ossl_quic_callback_ctrl, \
348                 ossl_quic_ctx_callback_ctrl, \
349         }; \
350         return &func_name##_data; \
351         }
352
353 #endif