eb6d3e4e6f6d3eef96f7060e77845bc1980eee76
[openssl.git] / ssl / quic / quic_local.h
1 /*
2  * Copyright 2022 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     /* Is this stream in blocking mode? */
45     unsigned int                    blocking                : 1;
46
47     /*
48      * This state tracks SSL_write all-or-nothing (AON) write semantics
49      * emulation.
50      *
51      * Example chronology:
52      *
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
63      *
64      */
65     /* Is an AON write in progress? */
66     unsigned int                    aon_write_in_progress   : 1;
67     /*
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.
71      *
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.
75      */
76     const unsigned char             *aon_buf_base;
77     /* The total length of the AON buffer being sent, in bytes. */
78     size_t                          aon_buf_len;
79     /*
80      * The position in the AON buffer up to which we have successfully sent data
81      * so far.
82      */
83     size_t                          aon_buf_pos;
84
85     /* SSL_set_mode */
86     uint32_t                        ssl_mode;
87
88     /* SSL_set_options */
89     uint64_t                        ssl_options;
90
91     /*
92      * Last 'normal' error during an app-level I/O operation, used by
93      * SSL_get_error(); used to track data-path errors like SSL_ERROR_WANT_READ
94      * and SSL_ERROR_WANT_WRITE.
95      */
96     int                             last_error;
97 };
98
99 struct quic_conn_st {
100     /*
101      * ssl_st is a common header for ordinary SSL objects, QUIC connection
102      * objects and QUIC stream objects, allowing objects of these different
103      * types to be disambiguated at runtime and providing some common fields.
104      *
105      * Note: This must come first in the QUIC_CONNECTION structure.
106      */
107     struct ssl_st                   ssl;
108
109     SSL                             *tls;
110
111     /*
112      * The QUIC channel providing the core QUIC connection implementation. Note
113      * that this is not instantiated until we actually start trying to do the
114      * handshake. This is to allow us to gather information like whether we are
115      * going to be in client or server mode before committing to instantiating
116      * the channel, since we want to determine the channel arguments based on
117      * that.
118      *
119      * The channel remains available after connection termination until the SSL
120      * object is freed, thus (ch != NULL) iff (started == 1).
121      */
122     QUIC_CHANNEL                    *ch;
123
124     /*
125      * The mutex used to synchronise access to the QUIC_CHANNEL. We own this but
126      * provide it to the channel.
127      */
128     CRYPTO_MUTEX                    *mutex;
129
130     /*
131      * If we have a default stream attached, this is the internal XSO
132      * object. If there is no default stream, this is NULL.
133      */
134     QUIC_XSO                        *default_xso;
135
136     /* The network read and write BIOs. */
137     BIO                             *net_rbio, *net_wbio;
138
139     /* Initial peer L4 address. */
140     BIO_ADDR                        init_peer_addr;
141
142 #  ifndef OPENSSL_NO_QUIC_THREAD_ASSIST
143     /* Manages thread for QUIC thread assisted mode. */
144     QUIC_THREAD_ASSIST              thread_assist;
145 #  endif
146
147     /* If non-NULL, used instead of ossl_time_now(). Used for testing. */
148     OSSL_TIME                       (*override_now_cb)(void *arg);
149     void                            *override_now_cb_arg;
150
151     /* Number of XSOs allocated. Includes the default XSO, if any. */
152     size_t                          num_xso;
153
154     /* Have we started? */
155     unsigned int                    started                 : 1;
156
157     /* Can the read and write network BIOs support blocking? */
158     unsigned int                    can_poll_net_rbio       : 1;
159     unsigned int                    can_poll_net_wbio       : 1;
160
161     /*
162      * This is 1 if we were instantiated using a QUIC server method
163      * (for future use).
164      */
165     unsigned int                    as_server               : 1;
166
167     /*
168      * Has the application called SSL_set_accept_state? We require this to be
169      * congruent with the value of as_server.
170      */
171     unsigned int                    as_server_state         : 1;
172
173     /* Are we using thread assisted mode? Never changes after init. */
174     unsigned int                    is_thread_assisted      : 1;
175
176     /* Do connection-level operations (e.g. handshakes) run in blocking mode? */
177     unsigned int                    blocking                : 1;
178
179     /* Do newly created streams start in blocking mode? Inherited by new XSOs. */
180     unsigned int                    default_blocking        : 1;
181
182     /* Have we created a default XSO yet? */
183     unsigned int                    default_xso_created     : 1;
184
185     /*
186      * Pre-TERMINATING shutdown phase in which we are flushing streams.
187      * Monotonically transitions to 1.
188      * New streams cannot be created in this state.
189      */
190     unsigned int                    shutting_down           : 1;
191
192     /* Default stream type. Defaults to SSL_DEFAULT_STREAM_MODE_AUTO_BIDI. */
193     uint32_t                        default_stream_mode;
194
195     /* SSL_set_mode. This is not used directly but inherited by new XSOs. */
196     uint32_t                        default_ssl_mode;
197
198     /* SSL_set_options. This is not used directly but inherited by new XSOs. */
199     uint64_t                        default_ssl_options;
200
201     /* SSL_set_incoming_stream_policy. */
202     int                             incoming_stream_policy;
203     uint64_t                        incoming_stream_aec;
204
205     /*
206      * Last 'normal' error during an app-level I/O operation, used by
207      * SSL_get_error(); used to track data-path errors like SSL_ERROR_WANT_READ
208      * and SSL_ERROR_WANT_WRITE.
209      */
210     int                             last_error;
211 };
212
213 /* Internal calls to the QUIC CSM which come from various places. */
214 int ossl_quic_conn_on_handshake_confirmed(QUIC_CONNECTION *qc);
215
216 /*
217  * To be called when a protocol violation occurs. The connection is torn down
218  * with the given error code, which should be a QUIC_ERR_* value. Reason string
219  * is optional and copied if provided. frame_type should be 0 if not applicable.
220  */
221 void ossl_quic_conn_raise_protocol_error(QUIC_CONNECTION *qc,
222                                          uint64_t error_code,
223                                          uint64_t frame_type,
224                                          const char *reason);
225
226 void ossl_quic_conn_on_remote_conn_close(QUIC_CONNECTION *qc,
227                                          OSSL_QUIC_FRAME_CONN_CLOSE *f);
228
229 int ossl_quic_trace(int write_p, int version, int content_type,
230                     const void *buf, size_t msglen, SSL *ssl, void *arg);
231
232 #  define OSSL_QUIC_ANY_VERSION 0xFFFFF
233 #  define IS_QUIC_METHOD(m) \
234     ((m) == OSSL_QUIC_client_method() || \
235      (m) == OSSL_QUIC_client_thread_method())
236 #  define IS_QUIC_CTX(ctx)          IS_QUIC_METHOD((ctx)->method)
237
238 #  define QUIC_CONNECTION_FROM_SSL_int(ssl, c)   \
239      ((ssl) == NULL ? NULL                       \
240       : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
241          ? (c QUIC_CONNECTION *)(ssl)            \
242          : NULL))
243
244 #  define QUIC_XSO_FROM_SSL_int(ssl, c)                             \
245     ((ssl) == NULL                                                  \
246      ? NULL                                                         \
247      : (((ssl)->type == SSL_TYPE_QUIC_XSO                           \
248         ? (c QUIC_XSO *)(ssl)                                       \
249         : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION                  \
250            ? (c QUIC_XSO *)((QUIC_CONNECTION *)(ssl))->default_xso  \
251            : NULL))))
252
253 #  define SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, c)               \
254      ((ssl) == NULL ? NULL                                       \
255       : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION                 \
256          ? (c SSL_CONNECTION *)((c QUIC_CONNECTION *)(ssl))->tls \
257          : NULL))
258
259 #  define IS_QUIC(ssl) ((ssl) != NULL                                   \
260                         && ((ssl)->type == SSL_TYPE_QUIC_CONNECTION     \
261                             || (ssl)->type == SSL_TYPE_QUIC_XSO))
262 # else
263 #  define QUIC_CONNECTION_FROM_SSL_int(ssl, c) NULL
264 #  define QUIC_XSO_FROM_SSL_int(ssl, c) NULL
265 #  define SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, c) NULL
266 #  define IS_QUIC(ssl) 0
267 #  define IS_QUIC_CTX(ctx) 0
268 #  define IS_QUIC_METHOD(m) 0
269 # endif
270
271 # define QUIC_CONNECTION_FROM_SSL(ssl) \
272     QUIC_CONNECTION_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
273 # define QUIC_CONNECTION_FROM_CONST_SSL(ssl) \
274     QUIC_CONNECTION_FROM_SSL_int(ssl, const)
275 # define QUIC_XSO_FROM_SSL(ssl) \
276     QUIC_XSO_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
277 # define QUIC_XSO_FROM_CONST_SSL(ssl) \
278     QUIC_XSO_FROM_SSL_int(ssl, const)
279 # define SSL_CONNECTION_FROM_QUIC_SSL(ssl) \
280     SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
281 # define SSL_CONNECTION_FROM_CONST_QUIC_SSL(ssl) \
282     SSL_CONNECTION_FROM_CONST_QUIC_SSL_int(ssl, const)
283
284 # define IMPLEMENT_quic_meth_func(version, func_name, q_accept, \
285                                  q_connect, enc_data) \
286 const SSL_METHOD *func_name(void)  \
287         { \
288         static const SSL_METHOD func_name##_data= { \
289                 version, \
290                 0, \
291                 0, \
292                 ossl_quic_new, \
293                 ossl_quic_free, \
294                 ossl_quic_reset, \
295                 ossl_quic_init, \
296                 ossl_quic_clear, \
297                 ossl_quic_deinit, \
298                 q_accept, \
299                 q_connect, \
300                 ossl_quic_read, \
301                 ossl_quic_peek, \
302                 ossl_quic_write, \
303                 NULL /* shutdown */, \
304                 NULL /* renegotiate */, \
305                 ossl_quic_renegotiate_check, \
306                 NULL /* read_bytes */, \
307                 NULL /* write_bytes */, \
308                 NULL /* dispatch_alert */, \
309                 ossl_quic_ctrl, \
310                 ossl_quic_ctx_ctrl, \
311                 NULL /* get_cipher_by_char */, \
312                 NULL /* put_cipher_by_char */, \
313                 ossl_quic_pending, \
314                 ossl_quic_num_ciphers, \
315                 ossl_quic_get_cipher, \
316                 tls1_default_timeout, \
317                 &enc_data, \
318                 ssl_undefined_void_function, \
319                 ossl_quic_callback_ctrl, \
320                 ossl_quic_ctx_callback_ctrl, \
321         }; \
322         return &func_name##_data; \
323         }
324
325 #endif