/src/openssl32/ssl/quic/quic_impl.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2022-2024 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  |  | #include <openssl/macros.h>  | 
11  |  | #include <openssl/objects.h>  | 
12  |  | #include <openssl/sslerr.h>  | 
13  |  | #include <crypto/rand.h>  | 
14  |  | #include "quic_local.h"  | 
15  |  | #include "internal/quic_tls.h"  | 
16  |  | #include "internal/quic_rx_depack.h"  | 
17  |  | #include "internal/quic_error.h"  | 
18  |  | #include "internal/time.h"  | 
19  |  |  | 
20  |  | typedef struct qctx_st QCTX;  | 
21  |  |  | 
22  |  | static void aon_write_finish(QUIC_XSO *xso);  | 
23  |  | static int create_channel(QUIC_CONNECTION *qc);  | 
24  |  | static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);  | 
25  |  | static int qc_try_create_default_xso_for_write(QCTX *ctx);  | 
26  |  | static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek);  | 
27  |  | static void quic_lock(QUIC_CONNECTION *qc);  | 
28  |  | static void quic_unlock(QUIC_CONNECTION *qc);  | 
29  |  | static void quic_lock_for_io(QCTX *ctx);  | 
30  |  | static int quic_do_handshake(QCTX *ctx);  | 
31  |  | static void qc_update_reject_policy(QUIC_CONNECTION *qc);  | 
32  |  | static void qc_touch_default_xso(QUIC_CONNECTION *qc);  | 
33  |  | static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);  | 
34  |  | static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,  | 
35  |  |                                         int touch, QUIC_XSO **old_xso);  | 
36  |  | static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock);  | 
37  |  | static int quic_validate_for_write(QUIC_XSO *xso, int *err);  | 
38  |  | static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active);  | 
39  |  | static int qc_blocking_mode(const QUIC_CONNECTION *qc);  | 
40  |  | static int xso_blocking_mode(const QUIC_XSO *xso);  | 
41  |  |  | 
42  |  | /*  | 
43  |  |  * QUIC Front-End I/O API: Common Utilities  | 
44  |  |  * ========================================  | 
45  |  |  */  | 
46  |  |  | 
47  |  | /*  | 
48  |  |  * Block until a predicate is met.  | 
49  |  |  *  | 
50  |  |  * Precondition: Must have a channel.  | 
51  |  |  * Precondition: Must hold channel lock (unchecked).  | 
52  |  |  */  | 
53  |  | QUIC_NEEDS_LOCK  | 
54  |  | static int block_until_pred(QUIC_CONNECTION *qc,  | 
55  |  |                             int (*pred)(void *arg), void *pred_arg,  | 
56  |  |                             uint32_t flags)  | 
57  | 0  | { | 
58  | 0  |     QUIC_REACTOR *rtor;  | 
59  |  | 
  | 
60  | 0  |     assert(qc->ch != NULL);  | 
61  |  |  | 
62  |  |     /*  | 
63  |  |      * Any attempt to block auto-disables tick inhibition as otherwise we will  | 
64  |  |      * hang around forever.  | 
65  |  |      */  | 
66  | 0  |     ossl_quic_channel_set_inhibit_tick(qc->ch, 0);  | 
67  |  | 
  | 
68  | 0  |     rtor = ossl_quic_channel_get_reactor(qc->ch);  | 
69  | 0  |     return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags,  | 
70  | 0  |                                               qc->mutex);  | 
71  | 0  | }  | 
72  |  |  | 
73  |  | static OSSL_TIME get_time(QUIC_CONNECTION *qc)  | 
74  | 68.5M  | { | 
75  | 68.5M  |     if (qc->override_now_cb != NULL)  | 
76  | 68.5M  |         return qc->override_now_cb(qc->override_now_cb_arg);  | 
77  | 11.1k  |     else  | 
78  | 11.1k  |         return ossl_time_now();  | 
79  | 68.5M  | }  | 
80  |  |  | 
81  |  | static OSSL_TIME get_time_cb(void *arg)  | 
82  | 48.3M  | { | 
83  | 48.3M  |     QUIC_CONNECTION *qc = arg;  | 
84  |  |  | 
85  | 48.3M  |     return get_time(qc);  | 
86  | 48.3M  | }  | 
87  |  |  | 
88  |  | /*  | 
89  |  |  * QCTX is a utility structure which provides information we commonly wish to  | 
90  |  |  * unwrap upon an API call being dispatched to us, namely:  | 
91  |  |  *  | 
92  |  |  *   - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO  | 
93  |  |  *     was passed);  | 
94  |  |  *   - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if  | 
95  |  |  *     a QCSO with a default stream was passed);  | 
96  |  |  *   - whether a QSSO was passed (xso == NULL must not be used to determine this  | 
97  |  |  *     because it may be non-NULL when a QCSO is passed if that QCSO has a  | 
98  |  |  *     default stream);  | 
99  |  |  *   - whether we are in "I/O context", meaning that non-normal errors can  | 
100  |  |  *     be reported via SSL_get_error() as well as via ERR. Functions such as  | 
101  |  |  *     SSL_read(), SSL_write() and SSL_do_handshake() are "I/O context"  | 
102  |  |  *     functions which are allowed to change the value returned by  | 
103  |  |  *     SSL_get_error. However, other functions (including functions which call  | 
104  |  |  *     SSL_do_handshake() implicitly) are not allowed to change the return value  | 
105  |  |  *     of SSL_get_error.  | 
106  |  |  */  | 
107  |  | struct qctx_st { | 
108  |  |     QUIC_CONNECTION *qc;  | 
109  |  |     QUIC_XSO        *xso;  | 
110  |  |     int             is_stream, in_io;  | 
111  |  | };  | 
112  |  |  | 
113  |  | QUIC_NEEDS_LOCK  | 
114  |  | static void quic_set_last_error(QCTX *ctx, int last_error)  | 
115  | 58.1M  | { | 
116  | 58.1M  |     if (!ctx->in_io)  | 
117  | 2.80k  |         return;  | 
118  |  |  | 
119  | 58.1M  |     if (ctx->is_stream && ctx->xso != NULL)  | 
120  | 4.03M  |         ctx->xso->last_error = last_error;  | 
121  | 54.1M  |     else if (!ctx->is_stream && ctx->qc != NULL)  | 
122  | 54.1M  |         ctx->qc->last_error = last_error;  | 
123  | 58.1M  | }  | 
124  |  |  | 
125  |  | /*  | 
126  |  |  * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()  | 
127  |  |  * rather than via ERR. Note that normal errors must always be raised while  | 
128  |  |  * holding a lock.  | 
129  |  |  */  | 
130  |  | QUIC_NEEDS_LOCK  | 
131  |  | static int quic_raise_normal_error(QCTX *ctx,  | 
132  |  |                                    int err)  | 
133  | 29.0M  | { | 
134  | 29.0M  |     assert(ctx->in_io);  | 
135  | 29.0M  |     quic_set_last_error(ctx, err);  | 
136  |  |  | 
137  | 29.0M  |     return 0;  | 
138  | 29.0M  | }  | 
139  |  |  | 
140  |  | /*  | 
141  |  |  * Raise a 'non-normal' error, meaning any error that is not reported via  | 
142  |  |  * SSL_get_error() and must be reported via ERR.  | 
143  |  |  *  | 
144  |  |  * qc should be provided if available. In exceptional circumstances when qc is  | 
145  |  |  * not known NULL may be passed. This should generally only happen when an  | 
146  |  |  * expect_...() function defined below fails, which generally indicates a  | 
147  |  |  * dispatch error or caller error.  | 
148  |  |  *  | 
149  |  |  * ctx should be NULL if the connection lock is not held.  | 
150  |  |  */  | 
151  |  | static int quic_raise_non_normal_error(QCTX *ctx,  | 
152  |  |                                        const char *file,  | 
153  |  |                                        int line,  | 
154  |  |                                        const char *func,  | 
155  |  |                                        int reason,  | 
156  |  |                                        const char *fmt,  | 
157  |  |                                        ...)  | 
158  | 20.8k  | { | 
159  | 20.8k  |     va_list args;  | 
160  |  |  | 
161  | 20.8k  |     if (ctx != NULL) { | 
162  | 20.8k  |         quic_set_last_error(ctx, SSL_ERROR_SSL);  | 
163  |  |  | 
164  | 20.8k  |         if (reason == SSL_R_PROTOCOL_IS_SHUTDOWN && ctx->qc != NULL)  | 
165  | 19.3k  |             ossl_quic_channel_restore_err_state(ctx->qc->ch);  | 
166  | 20.8k  |     }  | 
167  |  |  | 
168  | 20.8k  |     ERR_new();  | 
169  | 20.8k  |     ERR_set_debug(file, line, func);  | 
170  |  |  | 
171  | 20.8k  |     va_start(args, fmt);  | 
172  | 20.8k  |     ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);  | 
173  | 20.8k  |     va_end(args);  | 
174  |  |  | 
175  | 20.8k  |     return 0;  | 
176  | 20.8k  | }  | 
177  |  |  | 
178  |  | #define QUIC_RAISE_NORMAL_ERROR(ctx, err)                       \  | 
179  | 17.7M  |     quic_raise_normal_error((ctx), (err))  | 
180  |  |  | 
181  |  | #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg)           \  | 
182  | 11.8k  |     quic_raise_non_normal_error((ctx),                          \  | 
183  | 11.8k  |                                 OPENSSL_FILE, OPENSSL_LINE,     \  | 
184  | 11.8k  |                                 OPENSSL_FUNC,                   \  | 
185  | 11.8k  |                                 (reason),                       \  | 
186  | 11.8k  |                                 (msg))  | 
187  |  |  | 
188  |  | /*  | 
189  |  |  * Given a QCSO or QSSO, initialises a QCTX, determining the contextually  | 
190  |  |  * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer.  | 
191  |  |  *  | 
192  |  |  * After this returns 1, all fields of the passed QCTX are initialised.  | 
193  |  |  * Returns 0 on failure. This function is intended to be used to provide API  | 
194  |  |  * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure.  | 
195  |  |  */  | 
196  |  | static int expect_quic(const SSL *s, QCTX *ctx)  | 
197  | 55.9M  | { | 
198  | 55.9M  |     QUIC_CONNECTION *qc;  | 
199  | 55.9M  |     QUIC_XSO *xso;  | 
200  |  |  | 
201  | 55.9M  |     ctx->qc         = NULL;  | 
202  | 55.9M  |     ctx->xso        = NULL;  | 
203  | 55.9M  |     ctx->is_stream  = 0;  | 
204  |  |  | 
205  | 55.9M  |     if (s == NULL)  | 
206  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);  | 
207  |  |  | 
208  | 55.9M  |     switch (s->type) { | 
209  | 53.0M  |     case SSL_TYPE_QUIC_CONNECTION:  | 
210  | 53.0M  |         qc              = (QUIC_CONNECTION *)s;  | 
211  | 53.0M  |         ctx->qc         = qc;  | 
212  | 53.0M  |         ctx->xso        = qc->default_xso;  | 
213  | 53.0M  |         ctx->is_stream  = 0;  | 
214  | 53.0M  |         ctx->in_io      = 0;  | 
215  | 53.0M  |         return 1;  | 
216  |  |  | 
217  | 2.92M  |     case SSL_TYPE_QUIC_XSO:  | 
218  | 2.92M  |         xso             = (QUIC_XSO *)s;  | 
219  | 2.92M  |         ctx->qc         = xso->conn;  | 
220  | 2.92M  |         ctx->xso        = xso;  | 
221  | 2.92M  |         ctx->is_stream  = 1;  | 
222  | 2.92M  |         ctx->in_io      = 0;  | 
223  | 2.92M  |         return 1;  | 
224  |  |  | 
225  | 0  |     default:  | 
226  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);  | 
227  | 55.9M  |     }  | 
228  | 55.9M  | }  | 
229  |  |  | 
230  |  | /*  | 
231  |  |  * Like expect_quic(), but requires a QUIC_XSO be contextually available. In  | 
232  |  |  * other words, requires that the passed QSO be a QSSO or a QCSO with a default  | 
233  |  |  * stream.  | 
234  |  |  *  | 
235  |  |  * remote_init determines if we expect the default XSO to be remotely created or  | 
236  |  |  * not. If it is -1, do not instantiate a default XSO if one does not yet exist.  | 
237  |  |  *  | 
238  |  |  * Channel mutex is acquired and retained on success.  | 
239  |  |  */  | 
240  |  | QUIC_ACQUIRES_LOCK  | 
241  |  | static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,  | 
242  |  |                                                     int in_io, QCTX *ctx)  | 
243  | 3.36k  | { | 
244  | 3.36k  |     if (!expect_quic(s, ctx))  | 
245  | 0  |         return 0;  | 
246  |  |  | 
247  | 3.36k  |     if (in_io)  | 
248  | 3.36k  |         quic_lock_for_io(ctx);  | 
249  | 0  |     else  | 
250  | 0  |         quic_lock(ctx->qc);  | 
251  |  |  | 
252  | 3.36k  |     if (ctx->xso == NULL && remote_init >= 0) { | 
253  | 0  |         if (!quic_mutation_allowed(ctx->qc, /*req_active=*/0)) { | 
254  | 0  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
255  | 0  |             goto err;  | 
256  | 0  |         }  | 
257  |  |  | 
258  |  |         /* If we haven't finished the handshake, try to advance it. */  | 
259  | 0  |         if (quic_do_handshake(ctx) < 1)  | 
260  |  |             /* ossl_quic_do_handshake raised error here */  | 
261  | 0  |             goto err;  | 
262  |  |  | 
263  | 0  |         if (remote_init == 0) { | 
264  | 0  |             if (!qc_try_create_default_xso_for_write(ctx))  | 
265  | 0  |                 goto err;  | 
266  | 0  |         } else { | 
267  | 0  |             if (!qc_wait_for_default_xso_for_read(ctx, /*peek=*/0))  | 
268  | 0  |                 goto err;  | 
269  | 0  |         }  | 
270  |  |  | 
271  | 0  |         ctx->xso = ctx->qc->default_xso;  | 
272  | 0  |     }  | 
273  |  |  | 
274  | 3.36k  |     if (ctx->xso == NULL) { | 
275  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);  | 
276  | 0  |         goto err;  | 
277  | 0  |     }  | 
278  |  |  | 
279  | 3.36k  |     return 1; /* coverity[missing_unlock]: lock held */  | 
280  |  |  | 
281  | 0  | err:  | 
282  | 0  |     quic_unlock(ctx->qc);  | 
283  | 0  |     return 0;  | 
284  | 3.36k  | }  | 
285  |  |  | 
286  |  | /*  | 
287  |  |  * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still  | 
288  |  |  * be non-NULL if the QCSO has a default stream.  | 
289  |  |  */  | 
290  |  | static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)  | 
291  | 16.1k  | { | 
292  | 16.1k  |     if (!expect_quic(s, ctx))  | 
293  | 0  |         return 0;  | 
294  |  |  | 
295  | 16.1k  |     if (ctx->is_stream)  | 
296  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_CONN_USE_ONLY, NULL);  | 
297  |  |  | 
298  | 16.1k  |     return 1;  | 
299  | 16.1k  | }  | 
300  |  |  | 
301  |  | /*  | 
302  |  |  * Ensures that the channel mutex is held for a method which touches channel  | 
303  |  |  * state.  | 
304  |  |  *  | 
305  |  |  * Precondition: Channel mutex is not held (unchecked)  | 
306  |  |  */  | 
307  |  | static void quic_lock(QUIC_CONNECTION *qc)  | 
308  | 55.8M  | { | 
309  | 55.8M  | #if defined(OPENSSL_THREADS)  | 
310  | 55.8M  |     ossl_crypto_mutex_lock(qc->mutex);  | 
311  | 55.8M  | #endif  | 
312  | 55.8M  | }  | 
313  |  |  | 
314  |  | static void quic_lock_for_io(QCTX *ctx)  | 
315  | 17.7M  | { | 
316  | 17.7M  |     quic_lock(ctx->qc);  | 
317  | 17.7M  |     ctx->in_io = 1;  | 
318  |  |  | 
319  |  |     /*  | 
320  |  |      * We are entering an I/O function so we must update the values returned by  | 
321  |  |      * SSL_get_error and SSL_want. Set no error. This will be overridden later  | 
322  |  |      * if a call to QUIC_RAISE_NORMAL_ERROR or QUIC_RAISE_NON_NORMAL_ERROR  | 
323  |  |      * occurs during the API call.  | 
324  |  |      */  | 
325  | 17.7M  |     quic_set_last_error(ctx, SSL_ERROR_NONE);  | 
326  | 17.7M  | }  | 
327  |  |  | 
328  |  | /* Precondition: Channel mutex is held (unchecked) */  | 
329  |  | QUIC_NEEDS_LOCK  | 
330  |  | static void quic_unlock(QUIC_CONNECTION *qc)  | 
331  | 55.8M  | { | 
332  | 55.8M  | #if defined(OPENSSL_THREADS)  | 
333  | 55.8M  |     ossl_crypto_mutex_unlock(qc->mutex);  | 
334  | 55.8M  | #endif  | 
335  | 55.8M  | }  | 
336  |  |  | 
337  |  | /*  | 
338  |  |  * This predicate is the criterion which should determine API call rejection for  | 
339  |  |  * *most* mutating API calls, particularly stream-related operations for send  | 
340  |  |  * parts.  | 
341  |  |  *  | 
342  |  |  * A call is rejected (this function returns 0) if shutdown is in progress  | 
343  |  |  * (stream flushing), or we are in a TERMINATING or TERMINATED state. If  | 
344  |  |  * req_active=1, the connection must be active (i.e., the IDLE state is also  | 
345  |  |  * rejected).  | 
346  |  |  */  | 
347  |  | static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)  | 
348  | 29.0M  | { | 
349  | 29.0M  |     if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))  | 
350  | 3.57k  |         return 0;  | 
351  |  |  | 
352  | 28.9M  |     if (req_active && !ossl_quic_channel_is_active(qc->ch))  | 
353  | 0  |         return 0;  | 
354  |  |  | 
355  | 28.9M  |     return 1;  | 
356  | 28.9M  | }  | 
357  |  |  | 
358  |  | /*  | 
359  |  |  * QUIC Front-End I/O API: Initialization  | 
360  |  |  * ======================================  | 
361  |  |  *  | 
362  |  |  *         SSL_new                  => ossl_quic_new  | 
363  |  |  *                                     ossl_quic_init  | 
364  |  |  *         SSL_reset                => ossl_quic_reset  | 
365  |  |  *         SSL_clear                => ossl_quic_clear  | 
366  |  |  *                                     ossl_quic_deinit  | 
367  |  |  *         SSL_free                 => ossl_quic_free  | 
368  |  |  *  | 
369  |  |  *         SSL_set_options          => ossl_quic_set_options  | 
370  |  |  *         SSL_get_options          => ossl_quic_get_options  | 
371  |  |  *         SSL_clear_options        => ossl_quic_clear_options  | 
372  |  |  *  | 
373  |  |  */  | 
374  |  |  | 
375  |  | /* SSL_new */  | 
376  |  | SSL *ossl_quic_new(SSL_CTX *ctx)  | 
377  | 11.1k  | { | 
378  | 11.1k  |     QUIC_CONNECTION *qc = NULL;  | 
379  | 11.1k  |     SSL *ssl_base = NULL;  | 
380  | 11.1k  |     SSL_CONNECTION *sc = NULL;  | 
381  |  |  | 
382  | 11.1k  |     qc = OPENSSL_zalloc(sizeof(*qc));  | 
383  | 11.1k  |     if (qc == NULL) { | 
384  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);  | 
385  | 0  |         return NULL;  | 
386  | 0  |     }  | 
387  | 11.1k  | #if defined(OPENSSL_THREADS)  | 
388  | 11.1k  |     if ((qc->mutex = ossl_crypto_mutex_new()) == NULL) { | 
389  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);  | 
390  | 0  |         goto err;  | 
391  | 0  |     }  | 
392  | 11.1k  | #endif  | 
393  |  |  | 
394  |  |     /* Initialise the QUIC_CONNECTION's stub header. */  | 
395  | 11.1k  |     ssl_base = &qc->ssl;  | 
396  | 11.1k  |     if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) { | 
397  | 0  |         ssl_base = NULL;  | 
398  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);  | 
399  | 0  |         goto err;  | 
400  | 0  |     }  | 
401  |  |  | 
402  | 11.1k  |     qc->tls = ossl_ssl_connection_new_int(ctx, ssl_base, TLS_method());  | 
403  | 11.1k  |     if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) { | 
404  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);  | 
405  | 0  |         goto err;  | 
406  | 0  |     }  | 
407  |  |  | 
408  |  |     /* override the user_ssl of the inner connection */  | 
409  | 11.1k  |     sc->s3.flags |= TLS1_FLAGS_QUIC;  | 
410  |  |  | 
411  |  |     /* Restrict options derived from the SSL_CTX. */  | 
412  | 11.1k  |     sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;  | 
413  | 11.1k  |     sc->pha_enabled = 0;  | 
414  |  |  | 
415  | 11.1k  | #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)  | 
416  | 11.1k  |     qc->is_thread_assisted  | 
417  | 11.1k  |         = (ssl_base->method == OSSL_QUIC_client_thread_method());  | 
418  | 11.1k  | #endif  | 
419  |  |  | 
420  | 11.1k  |     qc->as_server       = 0; /* TODO(QUIC SERVER): add server support */  | 
421  | 11.1k  |     qc->as_server_state = qc->as_server;  | 
422  |  |  | 
423  | 11.1k  |     qc->default_stream_mode     = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;  | 
424  | 11.1k  |     qc->default_ssl_mode        = qc->ssl.ctx->mode;  | 
425  | 11.1k  |     qc->default_ssl_options     = qc->ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;  | 
426  | 11.1k  |     qc->desires_blocking        = 1;  | 
427  | 11.1k  |     qc->blocking                = 0;  | 
428  | 11.1k  |     qc->incoming_stream_policy  = SSL_INCOMING_STREAM_POLICY_AUTO;  | 
429  | 11.1k  |     qc->last_error              = SSL_ERROR_NONE;  | 
430  |  |  | 
431  | 11.1k  |     if (!create_channel(qc))  | 
432  | 0  |         goto err;  | 
433  |  |  | 
434  | 11.1k  |     ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, ssl_base);  | 
435  | 11.1k  |     ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);  | 
436  |  |  | 
437  | 11.1k  |     qc_update_reject_policy(qc);  | 
438  |  |  | 
439  |  |     /*  | 
440  |  |      * We do not create the default XSO yet. The reason for this is that the  | 
441  |  |      * stream ID of the default XSO will depend on whether the stream is client  | 
442  |  |      * or server-initiated, which depends on who transmits first. Since we do  | 
443  |  |      * not know whether the application will be using a client-transmits-first  | 
444  |  |      * or server-transmits-first protocol, we defer default XSO creation until  | 
445  |  |      * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,  | 
446  |  |      * we take that as a cue that the client is expecting a server-initiated  | 
447  |  |      * stream, and vice versa if SSL_write() is called first.  | 
448  |  |      */  | 
449  | 11.1k  |     return ssl_base;  | 
450  |  |  | 
451  | 0  | err:  | 
452  | 0  |     if (ssl_base == NULL) { | 
453  | 0  | #if defined(OPENSSL_THREADS)  | 
454  | 0  |         ossl_crypto_mutex_free(&qc->mutex);  | 
455  | 0  | #endif  | 
456  | 0  |         OPENSSL_free(qc);  | 
457  | 0  |     } else { | 
458  | 0  |         SSL_free(ssl_base);  | 
459  | 0  |     }  | 
460  | 0  |     return NULL;  | 
461  | 11.1k  | }  | 
462  |  |  | 
463  |  | /* SSL_free */  | 
464  |  | QUIC_TAKES_LOCK  | 
465  |  | void ossl_quic_free(SSL *s)  | 
466  | 14.2k  | { | 
467  | 14.2k  |     QCTX ctx;  | 
468  | 14.2k  |     int is_default;  | 
469  |  |  | 
470  |  |     /* We should never be called on anything but a QSO. */  | 
471  | 14.2k  |     if (!expect_quic(s, &ctx))  | 
472  | 0  |         return;  | 
473  |  |  | 
474  | 14.2k  |     quic_lock(ctx.qc);  | 
475  |  |  | 
476  | 14.2k  |     if (ctx.is_stream) { | 
477  |  |         /*  | 
478  |  |          * When a QSSO is freed, the XSO is freed immediately, because the XSO  | 
479  |  |          * itself only contains API personality layer data. However the  | 
480  |  |          * underlying QUIC_STREAM is not freed immediately but is instead marked  | 
481  |  |          * as deleted for later collection.  | 
482  |  |          */  | 
483  |  |  | 
484  | 3.08k  |         assert(ctx.qc->num_xso > 0);  | 
485  | 3.08k  |         --ctx.qc->num_xso;  | 
486  |  |  | 
487  |  |         /* If a stream's send part has not been finished, auto-reset it. */  | 
488  | 3.08k  |         if ((   ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY  | 
489  | 3.08k  |              || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)  | 
490  | 3.08k  |             && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))  | 
491  | 2.79k  |             ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),  | 
492  | 2.79k  |                                                         ctx.xso->stream, 0);  | 
493  |  |  | 
494  |  |         /* Do STOP_SENDING for the receive part, if applicable. */  | 
495  | 3.08k  |         if (   ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV  | 
496  | 3.08k  |             || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)  | 
497  | 2.85k  |             ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),  | 
498  | 2.85k  |                                                         ctx.xso->stream, 0);  | 
499  |  |  | 
500  |  |         /* Update stream state. */  | 
501  | 3.08k  |         ctx.xso->stream->deleted = 1;  | 
502  | 3.08k  |         ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),  | 
503  | 3.08k  |                                           ctx.xso->stream);  | 
504  |  |  | 
505  | 3.08k  |         is_default = (ctx.xso == ctx.qc->default_xso);  | 
506  | 3.08k  |         quic_unlock(ctx.qc);  | 
507  |  |  | 
508  |  |         /*  | 
509  |  |          * Unref the connection in most cases; the XSO has a ref to the QC and  | 
510  |  |          * not vice versa. But for a default XSO, to avoid circular references,  | 
511  |  |          * the QC refs the XSO but the XSO does not ref the QC. If we are the  | 
512  |  |          * default XSO, we only get here when the QC is being torn down anyway,  | 
513  |  |          * so don't call SSL_free(qc) as we are already in it.  | 
514  |  |          */  | 
515  | 3.08k  |         if (!is_default)  | 
516  | 1.55k  |             SSL_free(&ctx.qc->ssl);  | 
517  |  |  | 
518  |  |         /* Note: SSL_free calls OPENSSL_free(xso) for us */  | 
519  | 3.08k  |         return;  | 
520  | 3.08k  |     }  | 
521  |  |  | 
522  |  |     /*  | 
523  |  |      * Free the default XSO, if any. The QUIC_STREAM is not deleted at this  | 
524  |  |      * stage, but is freed during the channel free when the whole QSM is freed.  | 
525  |  |      */  | 
526  | 11.1k  |     if (ctx.qc->default_xso != NULL) { | 
527  | 1.53k  |         QUIC_XSO *xso = ctx.qc->default_xso;  | 
528  |  |  | 
529  | 1.53k  |         quic_unlock(ctx.qc);  | 
530  | 1.53k  |         SSL_free(&xso->ssl);  | 
531  | 1.53k  |         quic_lock(ctx.qc);  | 
532  | 1.53k  |         ctx.qc->default_xso = NULL;  | 
533  | 1.53k  |     }  | 
534  |  |  | 
535  |  |     /* Ensure we have no remaining XSOs. */  | 
536  | 11.1k  |     assert(ctx.qc->num_xso == 0);  | 
537  |  |  | 
538  | 11.1k  | #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)  | 
539  | 11.1k  |     if (ctx.qc->is_thread_assisted && ctx.qc->started) { | 
540  | 0  |         ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);  | 
541  | 0  |         ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);  | 
542  | 0  |     }  | 
543  | 11.1k  | #endif  | 
544  |  |  | 
545  | 11.1k  |     SSL_free(ctx.qc->tls);  | 
546  |  |  | 
547  | 11.1k  |     ossl_quic_channel_free(ctx.qc->ch);  | 
548  |  |  | 
549  | 11.1k  |     BIO_free_all(ctx.qc->net_rbio);  | 
550  | 11.1k  |     BIO_free_all(ctx.qc->net_wbio);  | 
551  |  |  | 
552  | 11.1k  |     quic_unlock(ctx.qc); /* tsan doesn't like freeing locked mutexes */  | 
553  | 11.1k  | #if defined(OPENSSL_THREADS)  | 
554  | 11.1k  |     ossl_crypto_mutex_free(&ctx.qc->mutex);  | 
555  | 11.1k  | #endif  | 
556  |  |  | 
557  |  |     /*  | 
558  |  |      * Note: SSL_free (that called this function) calls OPENSSL_free(ctx.qc) for  | 
559  |  |      * us  | 
560  |  |      */  | 
561  | 11.1k  | }  | 
562  |  |  | 
563  |  | /* SSL method init */  | 
564  |  | int ossl_quic_init(SSL *s)  | 
565  | 0  | { | 
566  |  |     /* Same op as SSL_clear, forward the call. */  | 
567  | 0  |     return ossl_quic_clear(s);  | 
568  | 0  | }  | 
569  |  |  | 
570  |  | /* SSL method deinit */  | 
571  |  | void ossl_quic_deinit(SSL *s)  | 
572  | 0  | { | 
573  |  |     /* No-op. */  | 
574  | 0  | }  | 
575  |  |  | 
576  |  | /* SSL_clear (ssl_reset method) */  | 
577  |  | int ossl_quic_reset(SSL *s)  | 
578  | 0  | { | 
579  | 0  |     QCTX ctx;  | 
580  |  | 
  | 
581  | 0  |     if (!expect_quic(s, &ctx))  | 
582  | 0  |         return 0;  | 
583  |  |  | 
584  | 0  |     ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);  | 
585  | 0  |     return 0;  | 
586  | 0  | }  | 
587  |  |  | 
588  |  | /* ssl_clear method (unused) */  | 
589  |  | int ossl_quic_clear(SSL *s)  | 
590  | 0  | { | 
591  | 0  |     QCTX ctx;  | 
592  |  | 
  | 
593  | 0  |     if (!expect_quic(s, &ctx))  | 
594  | 0  |         return 0;  | 
595  |  |  | 
596  | 0  |     ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);  | 
597  | 0  |     return 0;  | 
598  | 0  | }  | 
599  |  |  | 
600  |  | int ossl_quic_conn_set_override_now_cb(SSL *s,  | 
601  |  |                                        OSSL_TIME (*now_cb)(void *arg),  | 
602  |  |                                        void *now_cb_arg)  | 
603  | 11.1k  | { | 
604  | 11.1k  |     QCTX ctx;  | 
605  |  |  | 
606  | 11.1k  |     if (!expect_quic(s, &ctx))  | 
607  | 0  |         return 0;  | 
608  |  |  | 
609  | 11.1k  |     quic_lock(ctx.qc);  | 
610  |  |  | 
611  | 11.1k  |     ctx.qc->override_now_cb     = now_cb;  | 
612  | 11.1k  |     ctx.qc->override_now_cb_arg = now_cb_arg;  | 
613  |  |  | 
614  | 11.1k  |     quic_unlock(ctx.qc);  | 
615  | 11.1k  |     return 1;  | 
616  | 11.1k  | }  | 
617  |  |  | 
618  |  | void ossl_quic_conn_force_assist_thread_wake(SSL *s)  | 
619  | 0  | { | 
620  | 0  |     QCTX ctx;  | 
621  |  | 
  | 
622  | 0  |     if (!expect_quic(s, &ctx))  | 
623  | 0  |         return;  | 
624  |  |  | 
625  | 0  | #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)  | 
626  | 0  |     if (ctx.qc->is_thread_assisted && ctx.qc->started)  | 
627  | 0  |         ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);  | 
628  | 0  | #endif  | 
629  | 0  | }  | 
630  |  |  | 
631  |  | QUIC_NEEDS_LOCK  | 
632  |  | static void qc_touch_default_xso(QUIC_CONNECTION *qc)  | 
633  | 5.62k  | { | 
634  | 5.62k  |     qc->default_xso_created = 1;  | 
635  | 5.62k  |     qc_update_reject_policy(qc);  | 
636  | 5.62k  | }  | 
637  |  |  | 
638  |  | /*  | 
639  |  |  * Changes default XSO. Allows caller to keep reference to the old default XSO  | 
640  |  |  * (if any). Reference to new XSO is transferred from caller.  | 
641  |  |  */  | 
642  |  | QUIC_NEEDS_LOCK  | 
643  |  | static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,  | 
644  |  |                                         int touch,  | 
645  |  |                                         QUIC_XSO **old_xso)  | 
646  | 3.03k  | { | 
647  | 3.03k  |     int refs;  | 
648  |  |  | 
649  | 3.03k  |     *old_xso = NULL;  | 
650  |  |  | 
651  | 3.03k  |     if (qc->default_xso != xso) { | 
652  | 3.03k  |         *old_xso = qc->default_xso; /* transfer old XSO ref to caller */  | 
653  |  |  | 
654  | 3.03k  |         qc->default_xso = xso;  | 
655  |  |  | 
656  | 3.03k  |         if (xso == NULL) { | 
657  |  |             /*  | 
658  |  |              * Changing to not having a default XSO. XSO becomes standalone and  | 
659  |  |              * now has a ref to the QC.  | 
660  |  |              */  | 
661  | 0  |             if (!ossl_assert(SSL_up_ref(&qc->ssl)))  | 
662  | 0  |                 return;  | 
663  | 3.03k  |         } else { | 
664  |  |             /*  | 
665  |  |              * Changing from not having a default XSO to having one. The new XSO  | 
666  |  |              * will have had a reference to the QC we need to drop to avoid a  | 
667  |  |              * circular reference.  | 
668  |  |              *  | 
669  |  |              * Currently we never change directly from one default XSO to  | 
670  |  |              * another, though this function would also still be correct if this  | 
671  |  |              * weren't the case.  | 
672  |  |              */  | 
673  | 3.03k  |             assert(*old_xso == NULL);  | 
674  |  |  | 
675  | 3.03k  |             CRYPTO_DOWN_REF(&qc->ssl.references, &refs);  | 
676  | 3.03k  |             assert(refs > 0);  | 
677  | 3.03k  |         }  | 
678  | 3.03k  |     }  | 
679  |  |  | 
680  | 3.03k  |     if (touch)  | 
681  | 0  |         qc_touch_default_xso(qc);  | 
682  | 3.03k  | }  | 
683  |  |  | 
684  |  | /*  | 
685  |  |  * Changes default XSO, releasing the reference to any previous default XSO.  | 
686  |  |  * Reference to new XSO is transferred from caller.  | 
687  |  |  */  | 
688  |  | QUIC_NEEDS_LOCK  | 
689  |  | static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)  | 
690  | 3.03k  | { | 
691  | 3.03k  |     QUIC_XSO *old_xso = NULL;  | 
692  |  |  | 
693  | 3.03k  |     qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);  | 
694  |  |  | 
695  | 3.03k  |     if (old_xso != NULL)  | 
696  | 0  |         SSL_free(&old_xso->ssl);  | 
697  | 3.03k  | }  | 
698  |  |  | 
699  |  | QUIC_NEEDS_LOCK  | 
700  |  | static void xso_update_options(QUIC_XSO *xso)  | 
701  | 5.62k  | { | 
702  | 5.62k  |     int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);  | 
703  |  |  | 
704  | 5.62k  |     if (xso->stream->rstream != NULL)  | 
705  | 5.54k  |         ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);  | 
706  |  |  | 
707  | 5.62k  |     if (xso->stream->sstream != NULL)  | 
708  | 5.07k  |         ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);  | 
709  | 5.62k  | }  | 
710  |  |  | 
711  |  | /*  | 
712  |  |  * SSL_set_options  | 
713  |  |  * ---------------  | 
714  |  |  *  | 
715  |  |  * Setting options on a QCSO  | 
716  |  |  *   - configures the handshake-layer options;  | 
717  |  |  *   - configures the default data-plane options for new streams;  | 
718  |  |  *   - configures the data-plane options on the default XSO, if there is one.  | 
719  |  |  *  | 
720  |  |  * Setting options on a QSSO  | 
721  |  |  *   - configures data-plane options for that stream only.  | 
722  |  |  */  | 
723  |  | QUIC_TAKES_LOCK  | 
724  |  | static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)  | 
725  | 0  | { | 
726  | 0  |     QCTX ctx;  | 
727  | 0  |     uint64_t hs_mask_value, hs_or_value, ret;  | 
728  |  | 
  | 
729  | 0  |     if (!expect_quic(ssl, &ctx))  | 
730  | 0  |         return 0;  | 
731  |  |  | 
732  | 0  |     quic_lock(ctx.qc);  | 
733  |  | 
  | 
734  | 0  |     if (!ctx.is_stream) { | 
735  |  |         /*  | 
736  |  |          * If we were called on the connection, we apply any handshake option  | 
737  |  |          * changes.  | 
738  |  |          */  | 
739  | 0  |         hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);  | 
740  | 0  |         hs_or_value   = (or_value   & OSSL_QUIC_PERMITTED_OPTIONS_CONN);  | 
741  |  | 
  | 
742  | 0  |         SSL_clear_options(ctx.qc->tls, hs_mask_value);  | 
743  | 0  |         SSL_set_options(ctx.qc->tls, hs_or_value);  | 
744  |  |  | 
745  |  |         /* Update defaults for new streams. */  | 
746  | 0  |         ctx.qc->default_ssl_options  | 
747  | 0  |             = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)  | 
748  | 0  |               & OSSL_QUIC_PERMITTED_OPTIONS;  | 
749  | 0  |     }  | 
750  |  | 
  | 
751  | 0  |     if (ctx.xso != NULL) { | 
752  | 0  |         ctx.xso->ssl_options  | 
753  | 0  |             = ((ctx.xso->ssl_options & ~mask_value) | or_value)  | 
754  | 0  |             & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;  | 
755  |  | 
  | 
756  | 0  |         xso_update_options(ctx.xso);  | 
757  | 0  |     }  | 
758  |  | 
  | 
759  | 0  |     ret = ctx.is_stream ? ctx.xso->ssl_options : ctx.qc->default_ssl_options;  | 
760  |  | 
  | 
761  | 0  |     quic_unlock(ctx.qc);  | 
762  | 0  |     return ret;  | 
763  | 0  | }  | 
764  |  |  | 
765  |  | uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)  | 
766  | 0  | { | 
767  | 0  |     return quic_mask_or_options(ssl, 0, options);  | 
768  | 0  | }  | 
769  |  |  | 
770  |  | /* SSL_clear_options */  | 
771  |  | uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)  | 
772  | 0  | { | 
773  | 0  |     return quic_mask_or_options(ssl, options, 0);  | 
774  | 0  | }  | 
775  |  |  | 
776  |  | /* SSL_get_options */  | 
777  |  | uint64_t ossl_quic_get_options(const SSL *ssl)  | 
778  | 0  | { | 
779  | 0  |     return quic_mask_or_options((SSL *)ssl, 0, 0);  | 
780  | 0  | }  | 
781  |  |  | 
782  |  | /*  | 
783  |  |  * QUIC Front-End I/O API: Network BIO Configuration  | 
784  |  |  * =================================================  | 
785  |  |  *  | 
786  |  |  * Handling the different BIOs is difficult:  | 
787  |  |  *  | 
788  |  |  *   - It is more or less a requirement that we use non-blocking network I/O;  | 
789  |  |  *     we need to be able to have timeouts on recv() calls, and make best effort  | 
790  |  |  *     (non blocking) send() and recv() calls.  | 
791  |  |  *  | 
792  |  |  *     The only sensible way to do this is to configure the socket into  | 
793  |  |  *     non-blocking mode. We could try to do select() before calling send() or  | 
794  |  |  *     recv() to get a guarantee that the call will not block, but this will  | 
795  |  |  *     probably run into issues with buggy OSes which generate spurious socket  | 
796  |  |  *     readiness events. In any case, relying on this to work reliably does not  | 
797  |  |  *     seem sane.  | 
798  |  |  *  | 
799  |  |  *     Timeouts could be handled via setsockopt() socket timeout options, but  | 
800  |  |  *     this depends on OS support and adds another syscall to every network I/O  | 
801  |  |  *     operation. It also has obvious thread safety concerns if we want to move  | 
802  |  |  *     to concurrent use of a single socket at some later date.  | 
803  |  |  *  | 
804  |  |  *     Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to  | 
805  |  |  *     be made non-blocking. However some OSes (e.g. Windows) do not support  | 
806  |  |  *     this, so we cannot rely on this.  | 
807  |  |  *  | 
808  |  |  *     As such, we need to configure any FD in non-blocking mode. This may  | 
809  |  |  *     confound users who pass a blocking socket to libssl. However, in practice  | 
810  |  |  *     it would be extremely strange for a user of QUIC to pass an FD to us,  | 
811  |  |  *     then also try and send receive traffic on the same socket(!). Thus the  | 
812  |  |  *     impact of this should be limited, and can be documented.  | 
813  |  |  *  | 
814  |  |  *   - We support both blocking and non-blocking operation in terms of the API  | 
815  |  |  *     presented to the user. One prospect is to set the blocking mode based on  | 
816  |  |  *     whether the socket passed to us was already in blocking mode. However,  | 
817  |  |  *     Windows has no API for determining if a socket is in blocking mode (!),  | 
818  |  |  *     therefore this cannot be done portably. Currently therefore we expose an  | 
819  |  |  *     explicit API call to set this, and default to blocking mode.  | 
820  |  |  *  | 
821  |  |  *   - We need to determine our initial destination UDP address. The "natural"  | 
822  |  |  *     way for a user to do this is to set the peer variable on a BIO_dgram.  | 
823  |  |  *     However, this has problems because BIO_dgram's peer variable is used for  | 
824  |  |  *     both transmission and reception. This means it can be constantly being  | 
825  |  |  *     changed to a malicious value (e.g. if some random unrelated entity on the  | 
826  |  |  *     network starts sending traffic to us) on every read call. This is not a  | 
827  |  |  *     direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg  | 
828  |  |  *     calls only, which do not use this variable. However, we do need to let  | 
829  |  |  *     the user specify the peer in a 'normal' manner. The compromise here is  | 
830  |  |  *     that we grab the current peer value set at the time the write BIO is set  | 
831  |  |  *     and do not read the value again.  | 
832  |  |  *  | 
833  |  |  *   - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.  | 
834  |  |  *     Currently we do this by only supporting non-blocking mode.  | 
835  |  |  *  | 
836  |  |  */  | 
837  |  |  | 
838  |  | /*  | 
839  |  |  * Determines what initial destination UDP address we should use, if possible.  | 
840  |  |  * If this fails the client must set the destination address manually, or use a  | 
841  |  |  * BIO which does not need a destination address.  | 
842  |  |  */  | 
843  |  | static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)  | 
844  | 0  | { | 
845  | 0  |     if (BIO_dgram_detect_peer_addr(net_wbio, peer) <= 0)  | 
846  | 0  |         return 0;  | 
847  |  |  | 
848  | 0  |     return 1;  | 
849  | 0  | }  | 
850  |  |  | 
851  |  | static int qc_can_support_blocking_cached(QUIC_CONNECTION *qc)  | 
852  | 13.9M  | { | 
853  | 13.9M  |     QUIC_REACTOR *rtor = ossl_quic_channel_get_reactor(qc->ch);  | 
854  |  |  | 
855  | 13.9M  |     return ossl_quic_reactor_can_poll_r(rtor)  | 
856  | 13.9M  |         && ossl_quic_reactor_can_poll_w(rtor);  | 
857  | 13.9M  | }  | 
858  |  |  | 
859  |  | static void qc_update_can_support_blocking(QUIC_CONNECTION *qc)  | 
860  | 13.9M  | { | 
861  | 13.9M  |     ossl_quic_channel_update_poll_descriptors(qc->ch); /* best effort */  | 
862  | 13.9M  | }  | 
863  |  |  | 
864  |  | static void qc_update_blocking_mode(QUIC_CONNECTION *qc)  | 
865  | 13.9M  | { | 
866  | 13.9M  |     qc->blocking = qc->desires_blocking && qc_can_support_blocking_cached(qc);  | 
867  | 13.9M  | }  | 
868  |  |  | 
869  |  | void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)  | 
870  | 11.1k  | { | 
871  | 11.1k  |     QCTX ctx;  | 
872  |  |  | 
873  | 11.1k  |     if (!expect_quic(s, &ctx))  | 
874  | 0  |         return;  | 
875  |  |  | 
876  | 11.1k  |     if (ctx.qc->net_rbio == net_rbio)  | 
877  | 0  |         return;  | 
878  |  |  | 
879  | 11.1k  |     if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio))  | 
880  | 0  |         return;  | 
881  |  |  | 
882  | 11.1k  |     BIO_free_all(ctx.qc->net_rbio);  | 
883  | 11.1k  |     ctx.qc->net_rbio = net_rbio;  | 
884  |  |  | 
885  | 11.1k  |     if (net_rbio != NULL)  | 
886  | 11.1k  |         BIO_set_nbio(net_rbio, 1); /* best effort autoconfig */  | 
887  |  |  | 
888  |  |     /*  | 
889  |  |      * Determine if the current pair of read/write BIOs now set allows blocking  | 
890  |  |      * mode to be supported.  | 
891  |  |      */  | 
892  | 11.1k  |     qc_update_can_support_blocking(ctx.qc);  | 
893  | 11.1k  |     qc_update_blocking_mode(ctx.qc);  | 
894  | 11.1k  | }  | 
895  |  |  | 
896  |  | void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)  | 
897  | 11.1k  | { | 
898  | 11.1k  |     QCTX ctx;  | 
899  |  |  | 
900  | 11.1k  |     if (!expect_quic(s, &ctx))  | 
901  | 0  |         return;  | 
902  |  |  | 
903  | 11.1k  |     if (ctx.qc->net_wbio == net_wbio)  | 
904  | 0  |         return;  | 
905  |  |  | 
906  | 11.1k  |     if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio))  | 
907  | 0  |         return;  | 
908  |  |  | 
909  | 11.1k  |     BIO_free_all(ctx.qc->net_wbio);  | 
910  | 11.1k  |     ctx.qc->net_wbio = net_wbio;  | 
911  |  |  | 
912  | 11.1k  |     if (net_wbio != NULL)  | 
913  | 11.1k  |         BIO_set_nbio(net_wbio, 1); /* best effort autoconfig */  | 
914  |  |  | 
915  |  |     /*  | 
916  |  |      * Determine if the current pair of read/write BIOs now set allows blocking  | 
917  |  |      * mode to be supported.  | 
918  |  |      */  | 
919  | 11.1k  |     qc_update_can_support_blocking(ctx.qc);  | 
920  | 11.1k  |     qc_update_blocking_mode(ctx.qc);  | 
921  | 11.1k  | }  | 
922  |  |  | 
923  |  | BIO *ossl_quic_conn_get_net_rbio(const SSL *s)  | 
924  | 22.3k  | { | 
925  | 22.3k  |     QCTX ctx;  | 
926  |  |  | 
927  | 22.3k  |     if (!expect_quic(s, &ctx))  | 
928  | 0  |         return NULL;  | 
929  |  |  | 
930  | 22.3k  |     return ctx.qc->net_rbio;  | 
931  | 22.3k  | }  | 
932  |  |  | 
933  |  | BIO *ossl_quic_conn_get_net_wbio(const SSL *s)  | 
934  | 11.1k  | { | 
935  | 11.1k  |     QCTX ctx;  | 
936  |  |  | 
937  | 11.1k  |     if (!expect_quic(s, &ctx))  | 
938  | 0  |         return NULL;  | 
939  |  |  | 
940  | 11.1k  |     return ctx.qc->net_wbio;  | 
941  | 11.1k  | }  | 
942  |  |  | 
943  |  | int ossl_quic_conn_get_blocking_mode(const SSL *s)  | 
944  | 0  | { | 
945  | 0  |     QCTX ctx;  | 
946  |  | 
  | 
947  | 0  |     if (!expect_quic(s, &ctx))  | 
948  | 0  |         return 0;  | 
949  |  |  | 
950  | 0  |     if (ctx.is_stream)  | 
951  | 0  |         return xso_blocking_mode(ctx.xso);  | 
952  |  |  | 
953  | 0  |     return qc_blocking_mode(ctx.qc);  | 
954  | 0  | }  | 
955  |  |  | 
956  |  | QUIC_TAKES_LOCK  | 
957  |  | int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)  | 
958  | 0  | { | 
959  | 0  |     int ret = 0;  | 
960  | 0  |     QCTX ctx;  | 
961  |  | 
  | 
962  | 0  |     if (!expect_quic(s, &ctx))  | 
963  | 0  |         return 0;  | 
964  |  |  | 
965  | 0  |     quic_lock(ctx.qc);  | 
966  |  |  | 
967  |  |     /* Sanity check - can we support the request given the current network BIO? */  | 
968  | 0  |     if (blocking) { | 
969  |  |         /*  | 
970  |  |          * If called directly on a QCSO, update our information on network BIO  | 
971  |  |          * capabilities.  | 
972  |  |          */  | 
973  | 0  |         if (!ctx.is_stream)  | 
974  | 0  |             qc_update_can_support_blocking(ctx.qc);  | 
975  |  |  | 
976  |  |         /* Cannot enable blocking mode if we do not have pollable FDs. */  | 
977  | 0  |         if (!qc_can_support_blocking_cached(ctx.qc)) { | 
978  | 0  |             ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);  | 
979  | 0  |             goto out;  | 
980  | 0  |         }  | 
981  | 0  |     }  | 
982  |  |  | 
983  | 0  |     if (!ctx.is_stream)  | 
984  |  |         /*  | 
985  |  |          * If called directly on a QCSO, update default and connection-level  | 
986  |  |          * blocking modes.  | 
987  |  |          */  | 
988  | 0  |         ctx.qc->desires_blocking = (blocking != 0);  | 
989  |  | 
  | 
990  | 0  |     if (ctx.xso != NULL) { | 
991  |  |         /*  | 
992  |  |          * If called on a QSSO or a QCSO with a default XSO, update the blocking  | 
993  |  |          * mode.  | 
994  |  |          */  | 
995  | 0  |         ctx.xso->desires_blocking       = (blocking != 0);  | 
996  | 0  |         ctx.xso->desires_blocking_set   = 1;  | 
997  | 0  |     }  | 
998  |  | 
  | 
999  | 0  |     ret = 1;  | 
1000  | 0  | out:  | 
1001  | 0  |     qc_update_blocking_mode(ctx.qc);  | 
1002  | 0  |     quic_unlock(ctx.qc);  | 
1003  | 0  |     return ret;  | 
1004  | 0  | }  | 
1005  |  |  | 
1006  |  | int ossl_quic_conn_set_initial_peer_addr(SSL *s,  | 
1007  |  |                                          const BIO_ADDR *peer_addr)  | 
1008  | 22.0k  | { | 
1009  | 22.0k  |     QCTX ctx;  | 
1010  |  |  | 
1011  | 22.0k  |     if (!expect_quic(s, &ctx))  | 
1012  | 0  |         return 0;  | 
1013  |  |  | 
1014  | 22.0k  |     if (ctx.qc->started)  | 
1015  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,  | 
1016  | 22.0k  |                                        NULL);  | 
1017  |  |  | 
1018  | 22.0k  |     if (peer_addr == NULL) { | 
1019  | 0  |         BIO_ADDR_clear(&ctx.qc->init_peer_addr);  | 
1020  | 0  |         return 1;  | 
1021  | 0  |     }  | 
1022  |  |  | 
1023  | 22.0k  |     ctx.qc->init_peer_addr = *peer_addr;  | 
1024  | 22.0k  |     return 1;  | 
1025  | 22.0k  | }  | 
1026  |  |  | 
1027  |  | /*  | 
1028  |  |  * QUIC Front-End I/O API: Asynchronous I/O Management  | 
1029  |  |  * ===================================================  | 
1030  |  |  *  | 
1031  |  |  *   (BIO/)SSL_handle_events        => ossl_quic_handle_events  | 
1032  |  |  *   (BIO/)SSL_get_event_timeout    => ossl_quic_get_event_timeout  | 
1033  |  |  *   (BIO/)SSL_get_poll_fd          => ossl_quic_get_poll_fd  | 
1034  |  |  *  | 
1035  |  |  */  | 
1036  |  |  | 
1037  |  | /* Returns 1 if the connection is being used in blocking mode. */  | 
1038  |  | static int qc_blocking_mode(const QUIC_CONNECTION *qc)  | 
1039  | 28.6M  | { | 
1040  | 28.6M  |     return qc->blocking;  | 
1041  | 28.6M  | }  | 
1042  |  |  | 
1043  |  | static int xso_blocking_mode(const QUIC_XSO *xso)  | 
1044  | 3.01M  | { | 
1045  | 3.01M  |     if (xso->desires_blocking_set)  | 
1046  | 0  |         return xso->desires_blocking && qc_can_support_blocking_cached(xso->conn);  | 
1047  | 3.01M  |     else  | 
1048  |  |         /* Only ever set if we can support blocking. */  | 
1049  | 3.01M  |         return xso->conn->blocking;  | 
1050  | 3.01M  | }  | 
1051  |  |  | 
1052  |  | /* SSL_handle_events; performs QUIC I/O and timeout processing. */  | 
1053  |  | QUIC_TAKES_LOCK  | 
1054  |  | int ossl_quic_handle_events(SSL *s)  | 
1055  | 0  | { | 
1056  | 0  |     QCTX ctx;  | 
1057  |  | 
  | 
1058  | 0  |     if (!expect_quic(s, &ctx))  | 
1059  | 0  |         return 0;  | 
1060  |  |  | 
1061  | 0  |     quic_lock(ctx.qc);  | 
1062  | 0  |     if (ctx.qc->started)  | 
1063  | 0  |         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);  | 
1064  | 0  |     quic_unlock(ctx.qc);  | 
1065  | 0  |     return 1;  | 
1066  | 0  | }  | 
1067  |  |  | 
1068  |  | /*  | 
1069  |  |  * SSL_get_event_timeout. Get the time in milliseconds until the SSL object  | 
1070  |  |  * should next have events handled by the application by calling  | 
1071  |  |  * SSL_handle_events(). tv is set to 0 if the object should have events handled  | 
1072  |  |  * immediately. If no timeout is currently active, *is_infinite is set to 1 and  | 
1073  |  |  * the value of *tv is undefined.  | 
1074  |  |  */  | 
1075  |  | QUIC_TAKES_LOCK  | 
1076  |  | int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)  | 
1077  | 20.3M  | { | 
1078  | 20.3M  |     QCTX ctx;  | 
1079  | 20.3M  |     OSSL_TIME deadline = ossl_time_infinite();  | 
1080  |  |  | 
1081  | 20.3M  |     if (!expect_quic(s, &ctx))  | 
1082  | 0  |         return 0;  | 
1083  |  |  | 
1084  | 20.3M  |     quic_lock(ctx.qc);  | 
1085  |  |  | 
1086  | 20.3M  |     if (ctx.qc->started)  | 
1087  | 20.3M  |         deadline  | 
1088  | 20.3M  |             = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch));  | 
1089  |  |  | 
1090  | 20.3M  |     if (ossl_time_is_infinite(deadline)) { | 
1091  | 261k  |         *is_infinite = 1;  | 
1092  |  |  | 
1093  |  |         /*  | 
1094  |  |          * Robustness against faulty applications that don't check *is_infinite;  | 
1095  |  |          * harmless long timeout.  | 
1096  |  |          */  | 
1097  | 261k  |         tv->tv_sec  = 1000000;  | 
1098  | 261k  |         tv->tv_usec = 0;  | 
1099  |  |  | 
1100  | 261k  |         quic_unlock(ctx.qc);  | 
1101  | 261k  |         return 1;  | 
1102  | 261k  |     }  | 
1103  |  |  | 
1104  | 20.1M  |     *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, get_time(ctx.qc)));  | 
1105  | 20.1M  |     *is_infinite = 0;  | 
1106  | 20.1M  |     quic_unlock(ctx.qc);  | 
1107  | 20.1M  |     return 1;  | 
1108  | 20.3M  | }  | 
1109  |  |  | 
1110  |  | /* SSL_get_rpoll_descriptor */  | 
1111  |  | int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)  | 
1112  | 0  | { | 
1113  | 0  |     QCTX ctx;  | 
1114  |  | 
  | 
1115  | 0  |     if (!expect_quic(s, &ctx))  | 
1116  | 0  |         return 0;  | 
1117  |  |  | 
1118  | 0  |     if (desc == NULL || ctx.qc->net_rbio == NULL)  | 
1119  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,  | 
1120  | 0  |                                        NULL);  | 
1121  |  |  | 
1122  | 0  |     return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc);  | 
1123  | 0  | }  | 
1124  |  |  | 
1125  |  | /* SSL_get_wpoll_descriptor */  | 
1126  |  | int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)  | 
1127  | 0  | { | 
1128  | 0  |     QCTX ctx;  | 
1129  |  | 
  | 
1130  | 0  |     if (!expect_quic(s, &ctx))  | 
1131  | 0  |         return 0;  | 
1132  |  |  | 
1133  | 0  |     if (desc == NULL || ctx.qc->net_wbio == NULL)  | 
1134  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,  | 
1135  | 0  |                                        NULL);  | 
1136  |  |  | 
1137  | 0  |     return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc);  | 
1138  | 0  | }  | 
1139  |  |  | 
1140  |  | /* SSL_net_read_desired */  | 
1141  |  | QUIC_TAKES_LOCK  | 
1142  |  | int ossl_quic_get_net_read_desired(SSL *s)  | 
1143  | 0  | { | 
1144  | 0  |     QCTX ctx;  | 
1145  | 0  |     int ret;  | 
1146  |  | 
  | 
1147  | 0  |     if (!expect_quic(s, &ctx))  | 
1148  | 0  |         return 0;  | 
1149  |  |  | 
1150  | 0  |     quic_lock(ctx.qc);  | 
1151  | 0  |     ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));  | 
1152  | 0  |     quic_unlock(ctx.qc);  | 
1153  | 0  |     return ret;  | 
1154  | 0  | }  | 
1155  |  |  | 
1156  |  | /* SSL_net_write_desired */  | 
1157  |  | QUIC_TAKES_LOCK  | 
1158  |  | int ossl_quic_get_net_write_desired(SSL *s)  | 
1159  | 0  | { | 
1160  | 0  |     int ret;  | 
1161  | 0  |     QCTX ctx;  | 
1162  |  | 
  | 
1163  | 0  |     if (!expect_quic(s, &ctx))  | 
1164  | 0  |         return 0;  | 
1165  |  |  | 
1166  | 0  |     quic_lock(ctx.qc);  | 
1167  | 0  |     ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch));  | 
1168  | 0  |     quic_unlock(ctx.qc);  | 
1169  | 0  |     return ret;  | 
1170  | 0  | }  | 
1171  |  |  | 
1172  |  | /*  | 
1173  |  |  * QUIC Front-End I/O API: Connection Lifecycle Operations  | 
1174  |  |  * =======================================================  | 
1175  |  |  *  | 
1176  |  |  *         SSL_do_handshake         => ossl_quic_do_handshake  | 
1177  |  |  *         SSL_set_connect_state    => ossl_quic_set_connect_state  | 
1178  |  |  *         SSL_set_accept_state     => ossl_quic_set_accept_state  | 
1179  |  |  *         SSL_shutdown             => ossl_quic_shutdown  | 
1180  |  |  *         SSL_ctrl                 => ossl_quic_ctrl  | 
1181  |  |  *   (BIO/)SSL_connect              => ossl_quic_connect  | 
1182  |  |  *   (BIO/)SSL_accept               => ossl_quic_accept  | 
1183  |  |  *  | 
1184  |  |  */  | 
1185  |  |  | 
1186  |  | QUIC_NEEDS_LOCK  | 
1187  |  | static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)  | 
1188  | 0  | { | 
1189  | 0  |     QUIC_STREAM_MAP *qsm;  | 
1190  |  | 
  | 
1191  | 0  |     if (qc->shutting_down)  | 
1192  | 0  |         return;  | 
1193  |  |  | 
1194  | 0  |     qsm = ossl_quic_channel_get_qsm(qc->ch);  | 
1195  |  | 
  | 
1196  | 0  |     ossl_quic_stream_map_begin_shutdown_flush(qsm);  | 
1197  | 0  |     qc->shutting_down = 1;  | 
1198  | 0  | }  | 
1199  |  |  | 
1200  |  | /* Returns 1 if all shutdown-flush streams have been done with. */  | 
1201  |  | QUIC_NEEDS_LOCK  | 
1202  |  | static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)  | 
1203  | 0  | { | 
1204  | 0  |     QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);  | 
1205  |  | 
  | 
1206  | 0  |     return qc->shutting_down  | 
1207  | 0  |         && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);  | 
1208  | 0  | }  | 
1209  |  |  | 
1210  |  | /* SSL_shutdown */  | 
1211  |  | static int quic_shutdown_wait(void *arg)  | 
1212  | 0  | { | 
1213  | 0  |     QUIC_CONNECTION *qc = arg;  | 
1214  |  | 
  | 
1215  | 0  |     return ossl_quic_channel_is_terminated(qc->ch);  | 
1216  | 0  | }  | 
1217  |  |  | 
1218  |  | /* Returns 1 if shutdown flush process has finished or is inapplicable. */  | 
1219  |  | static int quic_shutdown_flush_wait(void *arg)  | 
1220  | 0  | { | 
1221  | 0  |     QUIC_CONNECTION *qc = arg;  | 
1222  |  | 
  | 
1223  | 0  |     return ossl_quic_channel_is_term_any(qc->ch)  | 
1224  | 0  |         || qc_shutdown_flush_finished(qc);  | 
1225  | 0  | }  | 
1226  |  |  | 
1227  |  | static int quic_shutdown_peer_wait(void *arg)  | 
1228  | 0  | { | 
1229  | 0  |     QUIC_CONNECTION *qc = arg;  | 
1230  | 0  |     return ossl_quic_channel_is_term_any(qc->ch);  | 
1231  | 0  | }  | 
1232  |  |  | 
1233  |  | QUIC_TAKES_LOCK  | 
1234  |  | int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,  | 
1235  |  |                             const SSL_SHUTDOWN_EX_ARGS *args,  | 
1236  |  |                             size_t args_len)  | 
1237  | 0  | { | 
1238  | 0  |     int ret;  | 
1239  | 0  |     QCTX ctx;  | 
1240  | 0  |     int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);  | 
1241  | 0  |     int no_block = ((flags & SSL_SHUTDOWN_FLAG_NO_BLOCK) != 0);  | 
1242  | 0  |     int wait_peer = ((flags & SSL_SHUTDOWN_FLAG_WAIT_PEER) != 0);  | 
1243  |  | 
  | 
1244  | 0  |     if (!expect_quic(s, &ctx))  | 
1245  | 0  |         return -1;  | 
1246  |  |  | 
1247  | 0  |     if (ctx.is_stream) { | 
1248  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_CONN_USE_ONLY, NULL);  | 
1249  | 0  |         return -1;  | 
1250  | 0  |     }  | 
1251  |  |  | 
1252  | 0  |     quic_lock(ctx.qc);  | 
1253  |  | 
  | 
1254  | 0  |     if (ossl_quic_channel_is_terminated(ctx.qc->ch)) { | 
1255  | 0  |         quic_unlock(ctx.qc);  | 
1256  | 0  |         return 1;  | 
1257  | 0  |     }  | 
1258  |  |  | 
1259  |  |     /* Phase 1: Stream Flushing */  | 
1260  | 0  |     if (!wait_peer && stream_flush) { | 
1261  | 0  |         qc_shutdown_flush_init(ctx.qc);  | 
1262  |  | 
  | 
1263  | 0  |         if (!qc_shutdown_flush_finished(ctx.qc)) { | 
1264  | 0  |             if (!no_block && qc_blocking_mode(ctx.qc)) { | 
1265  | 0  |                 ret = block_until_pred(ctx.qc, quic_shutdown_flush_wait, ctx.qc, 0);  | 
1266  | 0  |                 if (ret < 1) { | 
1267  | 0  |                     ret = 0;  | 
1268  | 0  |                     goto err;  | 
1269  | 0  |                 }  | 
1270  | 0  |             } else { | 
1271  | 0  |                 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);  | 
1272  | 0  |             }  | 
1273  | 0  |         }  | 
1274  |  |  | 
1275  | 0  |         if (!qc_shutdown_flush_finished(ctx.qc)) { | 
1276  | 0  |             quic_unlock(ctx.qc);  | 
1277  | 0  |             return 0; /* ongoing */  | 
1278  | 0  |         }  | 
1279  | 0  |     }  | 
1280  |  |  | 
1281  |  |     /* Phase 2: Connection Closure */  | 
1282  | 0  |     if (wait_peer && !ossl_quic_channel_is_term_any(ctx.qc->ch)) { | 
1283  | 0  |         if (!no_block && qc_blocking_mode(ctx.qc)) { | 
1284  | 0  |             ret = block_until_pred(ctx.qc, quic_shutdown_peer_wait, ctx.qc, 0);  | 
1285  | 0  |             if (ret < 1) { | 
1286  | 0  |                 ret = 0;  | 
1287  | 0  |                 goto err;  | 
1288  | 0  |             }  | 
1289  | 0  |         } else { | 
1290  | 0  |             ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);  | 
1291  | 0  |         }  | 
1292  |  |  | 
1293  | 0  |         if (!ossl_quic_channel_is_term_any(ctx.qc->ch)) { | 
1294  | 0  |             ret = 0; /* peer hasn't closed yet - still not done */  | 
1295  | 0  |             goto err;  | 
1296  | 0  |         }  | 
1297  |  |  | 
1298  |  |         /*  | 
1299  |  |          * We are at least terminating - go through the normal process of  | 
1300  |  |          * waiting until we are in the TERMINATED state.  | 
1301  |  |          */  | 
1302  | 0  |     }  | 
1303  |  |  | 
1304  |  |     /* Block mutation ops regardless of if we did stream flush. */  | 
1305  | 0  |     ctx.qc->shutting_down = 1;  | 
1306  |  |  | 
1307  |  |     /*  | 
1308  |  |      * This call is a no-op if we are already terminating, so it doesn't  | 
1309  |  |      * affect the wait_peer case.  | 
1310  |  |      */  | 
1311  | 0  |     ossl_quic_channel_local_close(ctx.qc->ch,  | 
1312  | 0  |                                   args != NULL ? args->quic_error_code : 0,  | 
1313  | 0  |                                   args != NULL ? args->quic_reason : NULL);  | 
1314  |  | 
  | 
1315  | 0  |     SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);  | 
1316  |  | 
  | 
1317  | 0  |     if (ossl_quic_channel_is_terminated(ctx.qc->ch)) { | 
1318  | 0  |         quic_unlock(ctx.qc);  | 
1319  | 0  |         return 1;  | 
1320  | 0  |     }  | 
1321  |  |  | 
1322  |  |     /* Phase 3: Terminating Wait Time */  | 
1323  | 0  |     if (!no_block && qc_blocking_mode(ctx.qc)  | 
1324  | 0  |         && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) { | 
1325  | 0  |         ret = block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0);  | 
1326  | 0  |         if (ret < 1) { | 
1327  | 0  |             ret = 0;  | 
1328  | 0  |             goto err;  | 
1329  | 0  |         }  | 
1330  | 0  |     } else { | 
1331  | 0  |         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);  | 
1332  | 0  |     }  | 
1333  |  |  | 
1334  | 0  |     ret = ossl_quic_channel_is_terminated(ctx.qc->ch);  | 
1335  | 0  | err:  | 
1336  | 0  |     quic_unlock(ctx.qc);  | 
1337  | 0  |     return ret;  | 
1338  | 0  | }  | 
1339  |  |  | 
1340  |  | /* SSL_ctrl */  | 
1341  |  | long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)  | 
1342  | 11.1k  | { | 
1343  | 11.1k  |     QCTX ctx;  | 
1344  |  |  | 
1345  | 11.1k  |     if (!expect_quic(s, &ctx))  | 
1346  | 0  |         return 0;  | 
1347  |  |  | 
1348  | 11.1k  |     switch (cmd) { | 
1349  | 0  |     case SSL_CTRL_MODE:  | 
1350  |  |         /* If called on a QCSO, update the default mode. */  | 
1351  | 0  |         if (!ctx.is_stream)  | 
1352  | 0  |             ctx.qc->default_ssl_mode |= (uint32_t)larg;  | 
1353  |  |  | 
1354  |  |         /*  | 
1355  |  |          * If we were called on a QSSO or have a default stream, we also update  | 
1356  |  |          * that.  | 
1357  |  |          */  | 
1358  | 0  |         if (ctx.xso != NULL) { | 
1359  |  |             /* Cannot enable EPW while AON write in progress. */  | 
1360  | 0  |             if (ctx.xso->aon_write_in_progress)  | 
1361  | 0  |                 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;  | 
1362  |  | 
  | 
1363  | 0  |             ctx.xso->ssl_mode |= (uint32_t)larg;  | 
1364  | 0  |             return ctx.xso->ssl_mode;  | 
1365  | 0  |         }  | 
1366  |  |  | 
1367  | 0  |         return ctx.qc->default_ssl_mode;  | 
1368  | 0  |     case SSL_CTRL_CLEAR_MODE:  | 
1369  | 0  |         if (!ctx.is_stream)  | 
1370  | 0  |             ctx.qc->default_ssl_mode &= ~(uint32_t)larg;  | 
1371  |  | 
  | 
1372  | 0  |         if (ctx.xso != NULL) { | 
1373  | 0  |             ctx.xso->ssl_mode &= ~(uint32_t)larg;  | 
1374  | 0  |             return ctx.xso->ssl_mode;  | 
1375  | 0  |         }  | 
1376  |  |  | 
1377  | 0  |         return ctx.qc->default_ssl_mode;  | 
1378  |  |  | 
1379  | 0  |     case SSL_CTRL_SET_MSG_CALLBACK_ARG:  | 
1380  | 0  |         ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);  | 
1381  |  |         /* This ctrl also needs to be passed to the internal SSL object */  | 
1382  | 0  |         return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);  | 
1383  |  |  | 
1384  | 0  |     case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */  | 
1385  | 0  |         { | 
1386  | 0  |             int is_infinite;  | 
1387  |  | 
  | 
1388  | 0  |             if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))  | 
1389  | 0  |                 return 0;  | 
1390  |  |  | 
1391  | 0  |             return !is_infinite;  | 
1392  | 0  |         }  | 
1393  | 0  |     case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */  | 
1394  |  |         /* For legacy compatibility with DTLS calls. */  | 
1395  | 0  |         return ossl_quic_handle_events(s) == 1 ? 1 : -1;  | 
1396  |  |  | 
1397  |  |         /* Mask ctrls we shouldn't support for QUIC. */  | 
1398  | 0  |     case SSL_CTRL_GET_READ_AHEAD:  | 
1399  | 0  |     case SSL_CTRL_SET_READ_AHEAD:  | 
1400  | 0  |     case SSL_CTRL_SET_MAX_SEND_FRAGMENT:  | 
1401  | 0  |     case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:  | 
1402  | 0  |     case SSL_CTRL_SET_MAX_PIPELINES:  | 
1403  | 0  |         return 0;  | 
1404  |  |  | 
1405  | 11.1k  |     default:  | 
1406  |  |         /*  | 
1407  |  |          * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl  | 
1408  |  |          * implementation. Either SSL_ctrl will handle it itself by direct  | 
1409  |  |          * access into handshake layer state, or failing that, it will be passed  | 
1410  |  |          * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not  | 
1411  |  |          * supported by anything, the handshake layer's ctrl method will finally  | 
1412  |  |          * return 0.  | 
1413  |  |          */  | 
1414  | 11.1k  |         return ossl_ctrl_internal(&ctx.qc->ssl, cmd, larg, parg, /*no_quic=*/1);  | 
1415  | 11.1k  |     }  | 
1416  | 11.1k  | }  | 
1417  |  |  | 
1418  |  | /* SSL_set_connect_state */  | 
1419  |  | void ossl_quic_set_connect_state(SSL *s)  | 
1420  | 11.1k  | { | 
1421  | 11.1k  |     QCTX ctx;  | 
1422  |  |  | 
1423  | 11.1k  |     if (!expect_quic(s, &ctx))  | 
1424  | 0  |         return;  | 
1425  |  |  | 
1426  |  |     /* Cannot be changed after handshake started */  | 
1427  | 11.1k  |     if (ctx.qc->started || ctx.is_stream)  | 
1428  | 0  |         return;  | 
1429  |  |  | 
1430  | 11.1k  |     ctx.qc->as_server_state = 0;  | 
1431  | 11.1k  | }  | 
1432  |  |  | 
1433  |  | /* SSL_set_accept_state */  | 
1434  |  | void ossl_quic_set_accept_state(SSL *s)  | 
1435  |  | { | 
1436  |  |     QCTX ctx;  | 
1437  |  |  | 
1438  |  |     if (!expect_quic(s, &ctx))  | 
1439  |  |         return;  | 
1440  |  |  | 
1441  |  |     /* Cannot be changed after handshake started */  | 
1442  |  |     if (ctx.qc->started || ctx.is_stream)  | 
1443  |  |         return;  | 
1444  |  |  | 
1445  |  |     ctx.qc->as_server_state = 1;  | 
1446  |  | }  | 
1447  |  |  | 
1448  |  | /* SSL_do_handshake */  | 
1449  |  | struct quic_handshake_wait_args { | 
1450  |  |     QUIC_CONNECTION     *qc;  | 
1451  |  | };  | 
1452  |  |  | 
1453  |  | static int tls_wants_non_io_retry(QUIC_CONNECTION *qc)  | 
1454  | 22.3M  | { | 
1455  | 22.3M  |     int want = SSL_want(qc->tls);  | 
1456  |  |  | 
1457  | 22.3M  |     if (want == SSL_X509_LOOKUP  | 
1458  | 22.3M  |             || want == SSL_CLIENT_HELLO_CB  | 
1459  | 22.3M  |             || want == SSL_RETRY_VERIFY)  | 
1460  | 0  |         return 1;  | 
1461  |  |  | 
1462  | 22.3M  |     return 0;  | 
1463  | 22.3M  | }  | 
1464  |  |  | 
1465  |  | static int quic_handshake_wait(void *arg)  | 
1466  | 0  | { | 
1467  | 0  |     struct quic_handshake_wait_args *args = arg;  | 
1468  |  | 
  | 
1469  | 0  |     if (!quic_mutation_allowed(args->qc, /*req_active=*/1))  | 
1470  | 0  |         return -1;  | 
1471  |  |  | 
1472  | 0  |     if (ossl_quic_channel_is_handshake_complete(args->qc->ch))  | 
1473  | 0  |         return 1;  | 
1474  |  |  | 
1475  | 0  |     if (tls_wants_non_io_retry(args->qc))  | 
1476  | 0  |         return 1;  | 
1477  |  |  | 
1478  | 0  |     return 0;  | 
1479  | 0  | }  | 
1480  |  |  | 
1481  |  | static int configure_channel(QUIC_CONNECTION *qc)  | 
1482  | 11.1k  | { | 
1483  | 11.1k  |     assert(qc->ch != NULL);  | 
1484  |  |  | 
1485  | 11.1k  |     if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)  | 
1486  | 11.1k  |         || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)  | 
1487  | 11.1k  |         || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))  | 
1488  | 0  |         return 0;  | 
1489  |  |  | 
1490  | 11.1k  |     return 1;  | 
1491  | 11.1k  | }  | 
1492  |  |  | 
1493  |  | QUIC_NEEDS_LOCK  | 
1494  |  | static int create_channel(QUIC_CONNECTION *qc)  | 
1495  | 11.1k  | { | 
1496  | 11.1k  |     QUIC_CHANNEL_ARGS args = {0}; | 
1497  |  |  | 
1498  | 11.1k  |     args.libctx     = qc->ssl.ctx->libctx;  | 
1499  | 11.1k  |     args.propq      = qc->ssl.ctx->propq;  | 
1500  | 11.1k  |     args.is_server  = qc->as_server;  | 
1501  | 11.1k  |     args.tls        = qc->tls;  | 
1502  | 11.1k  |     args.mutex      = qc->mutex;  | 
1503  | 11.1k  |     args.now_cb     = get_time_cb;  | 
1504  | 11.1k  |     args.now_cb_arg = qc;  | 
1505  |  |  | 
1506  | 11.1k  |     qc->ch = ossl_quic_channel_new(&args);  | 
1507  | 11.1k  |     if (qc->ch == NULL) { | 
1508  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);  | 
1509  | 0  |         return 0;  | 
1510  | 0  |     }  | 
1511  |  |  | 
1512  | 11.1k  |     return 1;  | 
1513  | 11.1k  | }  | 
1514  |  |  | 
1515  |  | /*  | 
1516  |  |  * Configures a channel with the information we have accumulated via calls made  | 
1517  |  |  * to us from the application prior to starting a handshake attempt.  | 
1518  |  |  */  | 
1519  |  | QUIC_NEEDS_LOCK  | 
1520  |  | static int ensure_channel_started(QCTX *ctx)  | 
1521  | 22.4M  | { | 
1522  | 22.4M  |     QUIC_CONNECTION *qc = ctx->qc;  | 
1523  |  |  | 
1524  | 22.4M  |     if (!qc->started) { | 
1525  | 22.0k  |         if (!configure_channel(qc)) { | 
1526  | 0  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,  | 
1527  | 0  |                                         "failed to configure channel");  | 
1528  | 0  |             return 0;  | 
1529  | 0  |         }  | 
1530  |  |  | 
1531  | 22.0k  |         if (!ossl_quic_channel_start(qc->ch)) { | 
1532  | 0  |             ossl_quic_channel_restore_err_state(qc->ch);  | 
1533  | 0  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,  | 
1534  | 0  |                                         "failed to start channel");  | 
1535  | 0  |             return 0;  | 
1536  | 0  |         }  | 
1537  |  |  | 
1538  | 22.0k  | #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)  | 
1539  | 22.0k  |         if (qc->is_thread_assisted)  | 
1540  | 0  |             if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch,  | 
1541  | 0  |                                                     qc->override_now_cb,  | 
1542  | 0  |                                                     qc->override_now_cb_arg)) { | 
1543  | 0  |                 QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,  | 
1544  | 0  |                                             "failed to start assist thread");  | 
1545  | 0  |                 return 0;  | 
1546  | 0  |             }  | 
1547  | 22.0k  | #endif  | 
1548  | 22.0k  |     }  | 
1549  |  |  | 
1550  | 22.4M  |     qc->started = 1;  | 
1551  | 22.4M  |     return 1;  | 
1552  | 22.4M  | }  | 
1553  |  |  | 
1554  |  | QUIC_NEEDS_LOCK  | 
1555  |  | static int quic_do_handshake(QCTX *ctx)  | 
1556  | 17.7M  | { | 
1557  | 17.7M  |     int ret;  | 
1558  | 17.7M  |     QUIC_CONNECTION *qc = ctx->qc;  | 
1559  |  |  | 
1560  | 17.7M  |     if (ossl_quic_channel_is_handshake_complete(qc->ch))  | 
1561  |  |         /* Handshake already completed. */  | 
1562  | 3.78M  |         return 1;  | 
1563  |  |  | 
1564  | 13.9M  |     if (!quic_mutation_allowed(qc, /*req_active=*/0))  | 
1565  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
1566  |  |  | 
1567  | 13.9M  |     if (qc->as_server != qc->as_server_state) { | 
1568  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);  | 
1569  | 0  |         return -1; /* Non-protocol error */  | 
1570  | 0  |     }  | 
1571  |  |  | 
1572  | 13.9M  |     if (qc->net_rbio == NULL || qc->net_wbio == NULL) { | 
1573  |  |         /* Need read and write BIOs. */  | 
1574  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);  | 
1575  | 0  |         return -1; /* Non-protocol error */  | 
1576  | 0  |     }  | 
1577  |  |  | 
1578  |  |     /*  | 
1579  |  |      * We need to determine our addressing mode. There are basically two  | 
1580  |  |      * ways we can use L4 addresses:  | 
1581  |  |      *  | 
1582  |  |      *   - Addressed mode, in which our BIO_sendmmsg calls have destination  | 
1583  |  |      *     addresses attached to them which we expect the underlying network BIO  | 
1584  |  |      *     to handle;  | 
1585  |  |      *  | 
1586  |  |      *   - Unaddressed mode, in which the BIO provided to us on the  | 
1587  |  |      *     network side neither provides us with L4 addresses nor is capable of  | 
1588  |  |      *     honouring ones we provide. We don't know where the QUIC traffic we  | 
1589  |  |      *     send ends up exactly and trust the application to know what it is  | 
1590  |  |      *     doing.  | 
1591  |  |      *  | 
1592  |  |      * Addressed mode is preferred because it enables support for connection  | 
1593  |  |      * migration, multipath, etc. in the future. Addressed mode is automatically  | 
1594  |  |      * enabled if we are using e.g. BIO_s_datagram, with or without  | 
1595  |  |      * BIO_s_connect.  | 
1596  |  |      *  | 
1597  |  |      * If we are passed a BIO_s_dgram_pair (or some custom BIO) we may have to  | 
1598  |  |      * use unaddressed mode unless that BIO supports capability flags indicating  | 
1599  |  |      * it can provide and honour L4 addresses.  | 
1600  |  |      *  | 
1601  |  |      * Our strategy for determining address mode is simple: we probe the  | 
1602  |  |      * underlying network BIOs for their capabilities. If the network BIOs  | 
1603  |  |      * support what we need, we use addressed mode. Otherwise, we use  | 
1604  |  |      * unaddressed mode.  | 
1605  |  |      *  | 
1606  |  |      * If addressed mode is chosen, we require an initial peer address to be  | 
1607  |  |      * set. If this is not set, we fail. If unaddressed mode is used, we do not  | 
1608  |  |      * require this, as such an address is superfluous, though it can be set if  | 
1609  |  |      * desired.  | 
1610  |  |      */  | 
1611  | 13.9M  |     if (!qc->started && !qc->addressing_probe_done) { | 
1612  | 11.1k  |         long rcaps = BIO_dgram_get_effective_caps(qc->net_rbio);  | 
1613  | 11.1k  |         long wcaps = BIO_dgram_get_effective_caps(qc->net_wbio);  | 
1614  |  |  | 
1615  | 11.1k  |         qc->addressed_mode_r = ((rcaps & BIO_DGRAM_CAP_PROVIDES_SRC_ADDR) != 0);  | 
1616  | 11.1k  |         qc->addressed_mode_w = ((wcaps & BIO_DGRAM_CAP_HANDLES_DST_ADDR) != 0);  | 
1617  | 11.1k  |         qc->addressing_probe_done = 1;  | 
1618  | 11.1k  |     }  | 
1619  |  |  | 
1620  | 13.9M  |     if (!qc->started && qc->addressed_mode_w  | 
1621  | 13.9M  |         && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) { | 
1622  |  |         /*  | 
1623  |  |          * We are trying to connect and are using addressed mode, which means we  | 
1624  |  |          * need an initial peer address; if we do not have a peer address yet,  | 
1625  |  |          * we should try to autodetect one.  | 
1626  |  |          *  | 
1627  |  |          * We do this as late as possible because some BIOs (e.g. BIO_s_connect)  | 
1628  |  |          * may not be able to provide us with a peer address until they have  | 
1629  |  |          * finished their own processing. They may not be able to perform this  | 
1630  |  |          * processing until an application has finished configuring that BIO  | 
1631  |  |          * (e.g. with setter calls), which might happen after SSL_set_bio is  | 
1632  |  |          * called.  | 
1633  |  |          */  | 
1634  | 0  |         if (!csm_analyse_init_peer_addr(qc->net_wbio, &qc->init_peer_addr))  | 
1635  |  |             /* best effort */  | 
1636  | 0  |             BIO_ADDR_clear(&qc->init_peer_addr);  | 
1637  | 0  |         else  | 
1638  | 0  |             ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);  | 
1639  | 0  |     }  | 
1640  |  |  | 
1641  | 13.9M  |     if (!qc->started  | 
1642  | 13.9M  |         && qc->addressed_mode_w  | 
1643  | 13.9M  |         && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) { | 
1644  |  |         /*  | 
1645  |  |          * If we still don't have a peer address in addressed mode, we can't do  | 
1646  |  |          * anything.  | 
1647  |  |          */  | 
1648  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);  | 
1649  | 0  |         return -1; /* Non-protocol error */  | 
1650  | 0  |     }  | 
1651  |  |  | 
1652  |  |     /*  | 
1653  |  |      * Start connection process. Note we may come here multiple times in  | 
1654  |  |      * non-blocking mode, which is fine.  | 
1655  |  |      */  | 
1656  | 13.9M  |     if (!ensure_channel_started(ctx)) /* raises on failure */  | 
1657  | 0  |         return -1; /* Non-protocol error */  | 
1658  |  |  | 
1659  | 13.9M  |     if (ossl_quic_channel_is_handshake_complete(qc->ch))  | 
1660  |  |         /* The handshake is now done. */  | 
1661  | 0  |         return 1;  | 
1662  |  |  | 
1663  | 13.9M  |     if (!qc_blocking_mode(qc)) { | 
1664  |  |         /* Try to advance the reactor. */  | 
1665  | 13.9M  |         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);  | 
1666  |  |  | 
1667  | 13.9M  |         if (ossl_quic_channel_is_handshake_complete(qc->ch))  | 
1668  |  |             /* The handshake is now done. */  | 
1669  | 2.97k  |             return 1;  | 
1670  |  |  | 
1671  | 13.9M  |         if (ossl_quic_channel_is_term_any(qc->ch)) { | 
1672  | 7.45k  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
1673  | 7.45k  |             return 0;  | 
1674  | 13.9M  |         } else if (qc->desires_blocking) { | 
1675  |  |             /*  | 
1676  |  |              * As a special case when doing a handshake when blocking mode is  | 
1677  |  |              * desired yet not available, see if the network BIOs have become  | 
1678  |  |              * poll descriptor-enabled. This supports BIOs such as BIO_s_connect  | 
1679  |  |              * which do late creation of socket FDs and therefore cannot expose  | 
1680  |  |              * a poll descriptor until after a network BIO is set on the QCSO.  | 
1681  |  |              */  | 
1682  | 13.9M  |             assert(!qc->blocking);  | 
1683  | 13.9M  |             qc_update_can_support_blocking(qc);  | 
1684  | 13.9M  |             qc_update_blocking_mode(qc);  | 
1685  | 13.9M  |         }  | 
1686  | 13.9M  |     }  | 
1687  |  |  | 
1688  |  |     /*  | 
1689  |  |      * We are either in blocking mode or just entered it due to the code above.  | 
1690  |  |      */  | 
1691  | 13.9M  |     if (qc_blocking_mode(qc)) { | 
1692  |  |         /* In blocking mode, wait for the handshake to complete. */  | 
1693  | 0  |         struct quic_handshake_wait_args args;  | 
1694  |  | 
  | 
1695  | 0  |         args.qc     = qc;  | 
1696  |  | 
  | 
1697  | 0  |         ret = block_until_pred(qc, quic_handshake_wait, &args, 0);  | 
1698  | 0  |         if (!quic_mutation_allowed(qc, /*req_active=*/1)) { | 
1699  | 0  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
1700  | 0  |             return 0; /* Shutdown before completion */  | 
1701  | 0  |         } else if (ret <= 0) { | 
1702  | 0  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
1703  | 0  |             return -1; /* Non-protocol error */  | 
1704  | 0  |         }  | 
1705  |  |  | 
1706  | 0  |         if (tls_wants_non_io_retry(qc)) { | 
1707  | 0  |             QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));  | 
1708  | 0  |             return -1;  | 
1709  | 0  |         }  | 
1710  |  |  | 
1711  | 0  |         assert(ossl_quic_channel_is_handshake_complete(qc->ch));  | 
1712  | 0  |         return 1;  | 
1713  | 0  |     }  | 
1714  |  |  | 
1715  | 13.9M  |     if (tls_wants_non_io_retry(qc)) { | 
1716  | 0  |         QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));  | 
1717  | 0  |         return -1;  | 
1718  | 0  |     }  | 
1719  |  |  | 
1720  |  |     /*  | 
1721  |  |      * Otherwise, indicate that the handshake isn't done yet.  | 
1722  |  |      * We can only get here in non-blocking mode.  | 
1723  |  |      */  | 
1724  | 13.9M  |     QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);  | 
1725  | 13.9M  |     return -1; /* Non-protocol error */  | 
1726  | 13.9M  | }  | 
1727  |  |  | 
1728  |  | QUIC_TAKES_LOCK  | 
1729  |  | int ossl_quic_do_handshake(SSL *s)  | 
1730  | 22.4M  | { | 
1731  | 22.4M  |     int ret;  | 
1732  | 22.4M  |     QCTX ctx;  | 
1733  |  |  | 
1734  | 22.4M  |     if (!expect_quic(s, &ctx))  | 
1735  | 0  |         return 0;  | 
1736  |  |  | 
1737  | 22.4M  |     quic_lock_for_io(&ctx);  | 
1738  |  |  | 
1739  | 22.4M  |     ret = quic_do_handshake(&ctx);  | 
1740  | 22.4M  |     quic_unlock(ctx.qc);  | 
1741  | 22.4M  |     return ret;  | 
1742  | 22.4M  | }  | 
1743  |  |  | 
1744  |  | /* SSL_connect */  | 
1745  |  | int ossl_quic_connect(SSL *s)  | 
1746  | 0  | { | 
1747  |  |     /* Ensure we are in connect state (no-op if non-idle). */  | 
1748  | 0  |     ossl_quic_set_connect_state(s);  | 
1749  |  |  | 
1750  |  |     /* Begin or continue the handshake */  | 
1751  | 0  |     return ossl_quic_do_handshake(s);  | 
1752  | 0  | }  | 
1753  |  |  | 
1754  |  | /* SSL_accept */  | 
1755  |  | int ossl_quic_accept(SSL *s)  | 
1756  | 0  | { | 
1757  |  |     /* Ensure we are in accept state (no-op if non-idle). */  | 
1758  | 0  |     ossl_quic_set_accept_state(s);  | 
1759  |  |  | 
1760  |  |     /* Begin or continue the handshake */  | 
1761  | 0  |     return ossl_quic_do_handshake(s);  | 
1762  | 0  | }  | 
1763  |  |  | 
1764  |  | /*  | 
1765  |  |  * QUIC Front-End I/O API: Stream Lifecycle Operations  | 
1766  |  |  * ===================================================  | 
1767  |  |  *  | 
1768  |  |  *         SSL_stream_new       => ossl_quic_conn_stream_new  | 
1769  |  |  *  | 
1770  |  |  */  | 
1771  |  |  | 
1772  |  | /*  | 
1773  |  |  * Try to create the default XSO if it doesn't already exist. Returns 1 if the  | 
1774  |  |  * default XSO was created. Returns 0 if it was not (e.g. because it already  | 
1775  |  |  * exists). Note that this is NOT an error condition.  | 
1776  |  |  */  | 
1777  |  | QUIC_NEEDS_LOCK  | 
1778  |  | static int qc_try_create_default_xso_for_write(QCTX *ctx)  | 
1779  | 0  | { | 
1780  | 0  |     uint64_t flags = 0;  | 
1781  | 0  |     QUIC_CONNECTION *qc = ctx->qc;  | 
1782  |  | 
  | 
1783  | 0  |     if (qc->default_xso_created  | 
1784  | 0  |         || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)  | 
1785  |  |         /*  | 
1786  |  |          * We only do this once. If the user detaches a previously created  | 
1787  |  |          * default XSO we don't auto-create another one.  | 
1788  |  |          */  | 
1789  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);  | 
1790  |  |  | 
1791  |  |     /* Create a locally-initiated stream. */  | 
1792  | 0  |     if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)  | 
1793  | 0  |         flags |= SSL_STREAM_FLAG_UNI;  | 
1794  |  | 
  | 
1795  | 0  |     qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,  | 
1796  | 0  |                                                             /*needs_lock=*/0),  | 
1797  | 0  |                        /*touch=*/0);  | 
1798  | 0  |     if (qc->default_xso == NULL)  | 
1799  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
1800  |  |  | 
1801  | 0  |     qc_touch_default_xso(qc);  | 
1802  | 0  |     return 1;  | 
1803  | 0  | }  | 
1804  |  |  | 
1805  |  | struct quic_wait_for_stream_args { | 
1806  |  |     QUIC_CONNECTION *qc;  | 
1807  |  |     QUIC_STREAM     *qs;  | 
1808  |  |     QCTX            *ctx;  | 
1809  |  |     uint64_t        expect_id;  | 
1810  |  | };  | 
1811  |  |  | 
1812  |  | QUIC_NEEDS_LOCK  | 
1813  |  | static int quic_wait_for_stream(void *arg)  | 
1814  | 0  | { | 
1815  | 0  |     struct quic_wait_for_stream_args *args = arg;  | 
1816  |  | 
  | 
1817  | 0  |     if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) { | 
1818  |  |         /* If connection is torn down due to an error while blocking, stop. */  | 
1819  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
1820  | 0  |         return -1;  | 
1821  | 0  |     }  | 
1822  |  |  | 
1823  | 0  |     args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),  | 
1824  | 0  |                                               args->expect_id | QUIC_STREAM_DIR_BIDI);  | 
1825  | 0  |     if (args->qs == NULL)  | 
1826  | 0  |         args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),  | 
1827  | 0  |                                                   args->expect_id | QUIC_STREAM_DIR_UNI);  | 
1828  |  | 
  | 
1829  | 0  |     if (args->qs != NULL)  | 
1830  | 0  |         return 1; /* stream now exists */  | 
1831  |  |  | 
1832  | 0  |     return 0; /* did not get a stream, keep trying */  | 
1833  | 0  | }  | 
1834  |  |  | 
1835  |  | QUIC_NEEDS_LOCK  | 
1836  |  | static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek)  | 
1837  | 774k  | { | 
1838  |  |     /* Called on a QCSO and we don't currently have a default stream. */  | 
1839  | 774k  |     uint64_t expect_id;  | 
1840  | 774k  |     QUIC_CONNECTION *qc = ctx->qc;  | 
1841  | 774k  |     QUIC_STREAM *qs;  | 
1842  | 774k  |     int res;  | 
1843  | 774k  |     struct quic_wait_for_stream_args wargs;  | 
1844  | 774k  |     OSSL_RTT_INFO rtt_info;  | 
1845  |  |  | 
1846  |  |     /*  | 
1847  |  |      * If default stream functionality is disabled or we already detached  | 
1848  |  |      * one, don't make another default stream and just fail.  | 
1849  |  |      */  | 
1850  | 774k  |     if (qc->default_xso_created  | 
1851  | 774k  |         || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)  | 
1852  | 6  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);  | 
1853  |  |  | 
1854  |  |     /*  | 
1855  |  |      * The peer may have opened a stream since we last ticked. So tick and  | 
1856  |  |      * see if the stream with ordinal 0 (remote, bidi/uni based on stream  | 
1857  |  |      * mode) exists yet. QUIC stream IDs must be allocated in order, so the  | 
1858  |  |      * first stream created by a peer must have an ordinal of 0.  | 
1859  |  |      */  | 
1860  | 774k  |     expect_id = qc->as_server  | 
1861  | 774k  |         ? QUIC_STREAM_INITIATOR_CLIENT  | 
1862  | 774k  |         : QUIC_STREAM_INITIATOR_SERVER;  | 
1863  |  |  | 
1864  | 774k  |     qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),  | 
1865  | 774k  |                                         expect_id | QUIC_STREAM_DIR_BIDI);  | 
1866  | 774k  |     if (qs == NULL)  | 
1867  | 774k  |         qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),  | 
1868  | 774k  |                                             expect_id | QUIC_STREAM_DIR_UNI);  | 
1869  |  |  | 
1870  | 774k  |     if (qs == NULL) { | 
1871  | 774k  |         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0);  | 
1872  |  |  | 
1873  | 774k  |         qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),  | 
1874  | 774k  |                                             expect_id);  | 
1875  | 774k  |     }  | 
1876  |  |  | 
1877  | 774k  |     if (qs == NULL) { | 
1878  | 773k  |         if (peek)  | 
1879  | 0  |             return 0;  | 
1880  |  |  | 
1881  | 773k  |         if (!qc_blocking_mode(qc))  | 
1882  |  |             /* Non-blocking mode, so just bail immediately. */  | 
1883  | 773k  |             return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);  | 
1884  |  |  | 
1885  |  |         /* Block until we have a stream. */  | 
1886  | 0  |         wargs.qc        = qc;  | 
1887  | 0  |         wargs.qs        = NULL;  | 
1888  | 0  |         wargs.ctx       = ctx;  | 
1889  | 0  |         wargs.expect_id = expect_id;  | 
1890  |  | 
  | 
1891  | 0  |         res = block_until_pred(qc, quic_wait_for_stream, &wargs, 0);  | 
1892  | 0  |         if (res == 0)  | 
1893  | 0  |             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
1894  | 0  |         else if (res < 0 || wargs.qs == NULL)  | 
1895  |  |             /* quic_wait_for_stream raised error here */  | 
1896  | 0  |             return 0;  | 
1897  |  |  | 
1898  | 0  |         qs = wargs.qs;  | 
1899  | 0  |     }  | 
1900  |  |  | 
1901  |  |     /*  | 
1902  |  |      * We now have qs != NULL. Remove it from the incoming stream queue so that  | 
1903  |  |      * it isn't also returned by any future SSL_accept_stream calls.  | 
1904  |  |      */  | 
1905  | 1.53k  |     ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);  | 
1906  | 1.53k  |     ossl_quic_stream_map_remove_from_accept_queue(ossl_quic_channel_get_qsm(qc->ch),  | 
1907  | 1.53k  |                                                   qs, rtt_info.smoothed_rtt);  | 
1908  |  |  | 
1909  |  |     /*  | 
1910  |  |      * Now make qs the default stream, creating the necessary XSO.  | 
1911  |  |      */  | 
1912  | 1.53k  |     qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);  | 
1913  | 1.53k  |     if (qc->default_xso == NULL)  | 
1914  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
1915  |  |  | 
1916  | 1.53k  |     qc_touch_default_xso(qc); /* inhibits default XSO */  | 
1917  | 1.53k  |     return 1;  | 
1918  | 1.53k  | }  | 
1919  |  |  | 
1920  |  | QUIC_NEEDS_LOCK  | 
1921  |  | static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)  | 
1922  | 5.62k  | { | 
1923  | 5.62k  |     QUIC_XSO *xso = NULL;  | 
1924  |  |  | 
1925  | 5.62k  |     if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) { | 
1926  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);  | 
1927  | 0  |         goto err;  | 
1928  | 0  |     }  | 
1929  |  |  | 
1930  | 5.62k  |     if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO)) { | 
1931  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);  | 
1932  | 0  |         goto err;  | 
1933  | 0  |     }  | 
1934  |  |  | 
1935  |  |     /* XSO refs QC */  | 
1936  | 5.62k  |     if (!SSL_up_ref(&qc->ssl)) { | 
1937  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SSL_LIB, NULL);  | 
1938  | 0  |         goto err;  | 
1939  | 0  |     }  | 
1940  |  |  | 
1941  | 5.62k  |     xso->conn       = qc;  | 
1942  | 5.62k  |     xso->ssl_mode   = qc->default_ssl_mode;  | 
1943  | 5.62k  |     xso->ssl_options  | 
1944  | 5.62k  |         = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;  | 
1945  | 5.62k  |     xso->last_error = SSL_ERROR_NONE;  | 
1946  |  |  | 
1947  | 5.62k  |     xso->stream     = qs;  | 
1948  |  |  | 
1949  | 5.62k  |     ++qc->num_xso;  | 
1950  | 5.62k  |     xso_update_options(xso);  | 
1951  | 5.62k  |     return xso;  | 
1952  |  |  | 
1953  | 0  | err:  | 
1954  | 0  |     OPENSSL_free(xso);  | 
1955  | 0  |     return NULL;  | 
1956  | 5.62k  | }  | 
1957  |  |  | 
1958  |  | struct quic_new_stream_wait_args { | 
1959  |  |     QUIC_CONNECTION *qc;  | 
1960  |  |     int is_uni;  | 
1961  |  | };  | 
1962  |  |  | 
1963  |  | static int quic_new_stream_wait(void *arg)  | 
1964  | 0  | { | 
1965  | 0  |     struct quic_new_stream_wait_args *args = arg;  | 
1966  | 0  |     QUIC_CONNECTION *qc = args->qc;  | 
1967  |  | 
  | 
1968  | 0  |     if (!quic_mutation_allowed(qc, /*req_active=*/1))  | 
1969  | 0  |         return -1;  | 
1970  |  |  | 
1971  | 0  |     if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))  | 
1972  | 0  |         return 1;  | 
1973  |  |  | 
1974  | 0  |     return 0;  | 
1975  | 0  | }  | 
1976  |  |  | 
1977  |  | /* locking depends on need_lock */  | 
1978  |  | static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)  | 
1979  | 5.20k  | { | 
1980  | 5.20k  |     int ret;  | 
1981  | 5.20k  |     QUIC_CONNECTION *qc = ctx->qc;  | 
1982  | 5.20k  |     QUIC_XSO *xso = NULL;  | 
1983  | 5.20k  |     QUIC_STREAM *qs = NULL;  | 
1984  | 5.20k  |     int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);  | 
1985  | 5.20k  |     int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);  | 
1986  | 5.20k  |     int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);  | 
1987  |  |  | 
1988  | 5.20k  |     if (need_lock)  | 
1989  | 5.20k  |         quic_lock(qc);  | 
1990  |  |  | 
1991  | 5.20k  |     if (!quic_mutation_allowed(qc, /*req_active=*/0)) { | 
1992  | 1.39k  |         QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
1993  | 1.39k  |         goto err;  | 
1994  | 1.39k  |     }  | 
1995  |  |  | 
1996  | 3.80k  |     if (!advance  | 
1997  | 3.80k  |         && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) { | 
1998  | 1.40k  |         struct quic_new_stream_wait_args args;  | 
1999  |  |  | 
2000  |  |         /*  | 
2001  |  |          * Stream count flow control currently doesn't permit this stream to be  | 
2002  |  |          * opened.  | 
2003  |  |          */  | 
2004  | 1.40k  |         if (no_blocking || !qc_blocking_mode(qc)) { | 
2005  | 1.40k  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);  | 
2006  | 1.40k  |             goto err;  | 
2007  | 1.40k  |         }  | 
2008  |  |  | 
2009  | 0  |         args.qc     = qc;  | 
2010  | 0  |         args.is_uni = is_uni;  | 
2011  |  |  | 
2012  |  |         /* Blocking mode - wait until we can get a stream. */  | 
2013  | 0  |         ret = block_until_pred(ctx->qc, quic_new_stream_wait, &args, 0);  | 
2014  | 0  |         if (!quic_mutation_allowed(qc, /*req_active=*/1)) { | 
2015  | 0  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
2016  | 0  |             goto err; /* Shutdown before completion */  | 
2017  | 0  |         } else if (ret <= 0) { | 
2018  | 0  |             QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2019  | 0  |             goto err; /* Non-protocol error */  | 
2020  | 0  |         }  | 
2021  | 0  |     }  | 
2022  |  |  | 
2023  | 2.39k  |     qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);  | 
2024  | 2.39k  |     if (qs == NULL) { | 
2025  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2026  | 0  |         goto err;  | 
2027  | 0  |     }  | 
2028  |  |  | 
2029  | 2.39k  |     xso = create_xso_from_stream(qc, qs);  | 
2030  | 2.39k  |     if (xso == NULL)  | 
2031  | 0  |         goto err;  | 
2032  |  |  | 
2033  | 2.39k  |     qc_touch_default_xso(qc); /* inhibits default XSO */  | 
2034  | 2.39k  |     if (need_lock)  | 
2035  | 2.39k  |         quic_unlock(qc);  | 
2036  |  |  | 
2037  | 2.39k  |     return &xso->ssl;  | 
2038  |  |  | 
2039  | 2.80k  | err:  | 
2040  | 2.80k  |     OPENSSL_free(xso);  | 
2041  | 2.80k  |     ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);  | 
2042  | 2.80k  |     if (need_lock)  | 
2043  | 2.80k  |         quic_unlock(qc);  | 
2044  |  |  | 
2045  | 2.80k  |     return NULL;  | 
2046  |  |  | 
2047  | 2.39k  | }  | 
2048  |  |  | 
2049  |  | QUIC_TAKES_LOCK  | 
2050  |  | SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)  | 
2051  | 5.20k  | { | 
2052  | 5.20k  |     QCTX ctx;  | 
2053  |  |  | 
2054  | 5.20k  |     if (!expect_quic_conn_only(s, &ctx))  | 
2055  | 0  |         return NULL;  | 
2056  |  |  | 
2057  | 5.20k  |     return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);  | 
2058  | 5.20k  | }  | 
2059  |  |  | 
2060  |  | /*  | 
2061  |  |  * QUIC Front-End I/O API: Steady-State Operations  | 
2062  |  |  * ===============================================  | 
2063  |  |  *  | 
2064  |  |  * Here we dispatch calls to the steady-state front-end I/O API functions; that  | 
2065  |  |  * is, the functions used during the established phase of a QUIC connection  | 
2066  |  |  * (e.g. SSL_read, SSL_write).  | 
2067  |  |  *  | 
2068  |  |  * Each function must handle both blocking and non-blocking modes. As discussed  | 
2069  |  |  * above, all QUIC I/O is implemented using non-blocking mode internally.  | 
2070  |  |  *  | 
2071  |  |  *         SSL_get_error        => partially implemented by ossl_quic_get_error  | 
2072  |  |  *         SSL_want             => ossl_quic_want  | 
2073  |  |  *   (BIO/)SSL_read             => ossl_quic_read  | 
2074  |  |  *   (BIO/)SSL_write            => ossl_quic_write  | 
2075  |  |  *         SSL_pending          => ossl_quic_pending  | 
2076  |  |  *         SSL_stream_conclude  => ossl_quic_conn_stream_conclude  | 
2077  |  |  *         SSL_key_update       => ossl_quic_key_update  | 
2078  |  |  */  | 
2079  |  |  | 
2080  |  | /* SSL_get_error */  | 
2081  |  | int ossl_quic_get_error(const SSL *s, int i)  | 
2082  | 29.0M  | { | 
2083  | 29.0M  |     QCTX ctx;  | 
2084  | 29.0M  |     int net_error, last_error;  | 
2085  |  |  | 
2086  | 29.0M  |     if (!expect_quic(s, &ctx))  | 
2087  | 79  |         return 0;  | 
2088  |  |  | 
2089  | 29.0M  |     quic_lock(ctx.qc);  | 
2090  | 29.0M  |     net_error = ossl_quic_channel_net_error(ctx.qc->ch);  | 
2091  | 29.0M  |     last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;  | 
2092  | 29.0M  |     quic_unlock(ctx.qc);  | 
2093  |  |  | 
2094  | 29.0M  |     if (net_error)  | 
2095  | 0  |         return SSL_ERROR_SYSCALL;  | 
2096  |  |  | 
2097  | 29.0M  |     return last_error;  | 
2098  | 29.0M  | }  | 
2099  |  |  | 
2100  |  | /* Converts a code returned by SSL_get_error to a code returned by SSL_want. */  | 
2101  |  | static int error_to_want(int error)  | 
2102  | 0  | { | 
2103  | 0  |     switch (error) { | 
2104  | 0  |     case SSL_ERROR_WANT_CONNECT: /* never used - UDP is connectionless */  | 
2105  | 0  |     case SSL_ERROR_WANT_ACCEPT:  /* never used - UDP is connectionless */  | 
2106  | 0  |     case SSL_ERROR_ZERO_RETURN:  | 
2107  | 0  |     default:  | 
2108  | 0  |         return SSL_NOTHING;  | 
2109  |  |  | 
2110  | 0  |     case SSL_ERROR_WANT_READ:  | 
2111  | 0  |         return SSL_READING;  | 
2112  |  |  | 
2113  | 0  |     case SSL_ERROR_WANT_WRITE:  | 
2114  | 0  |         return SSL_WRITING;  | 
2115  |  |  | 
2116  | 0  |     case SSL_ERROR_WANT_RETRY_VERIFY:  | 
2117  | 0  |         return SSL_RETRY_VERIFY;  | 
2118  |  |  | 
2119  | 0  |     case SSL_ERROR_WANT_CLIENT_HELLO_CB:  | 
2120  | 0  |         return SSL_CLIENT_HELLO_CB;  | 
2121  |  |  | 
2122  | 0  |     case SSL_ERROR_WANT_X509_LOOKUP:  | 
2123  | 0  |         return SSL_X509_LOOKUP;  | 
2124  | 0  |     }  | 
2125  | 0  | }  | 
2126  |  |  | 
2127  |  | /* SSL_want */  | 
2128  |  | int ossl_quic_want(const SSL *s)  | 
2129  | 0  | { | 
2130  | 0  |     QCTX ctx;  | 
2131  | 0  |     int w;  | 
2132  |  | 
  | 
2133  | 0  |     if (!expect_quic(s, &ctx))  | 
2134  | 0  |         return SSL_NOTHING;  | 
2135  |  |  | 
2136  | 0  |     quic_lock(ctx.qc);  | 
2137  |  | 
  | 
2138  | 0  |     w = error_to_want(ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error);  | 
2139  |  | 
  | 
2140  | 0  |     quic_unlock(ctx.qc);  | 
2141  | 0  |     return w;  | 
2142  | 0  | }  | 
2143  |  |  | 
2144  |  | /*  | 
2145  |  |  * SSL_write  | 
2146  |  |  * ---------  | 
2147  |  |  *  | 
2148  |  |  * The set of functions below provide the implementation of the public SSL_write  | 
2149  |  |  * function. We must handle:  | 
2150  |  |  *  | 
2151  |  |  *   - both blocking and non-blocking operation at the application level,  | 
2152  |  |  *     depending on how we are configured;  | 
2153  |  |  *  | 
2154  |  |  *   - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;  | 
2155  |  |  *  | 
2156  |  |  *   - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.  | 
2157  |  |  *  | 
2158  |  |  */  | 
2159  |  | QUIC_NEEDS_LOCK  | 
2160  |  | static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick)  | 
2161  | 3.29k  | { | 
2162  |  |     /*  | 
2163  |  |      * We have appended at least one byte to the stream.  | 
2164  |  |      * Potentially mark stream as active, depending on FC.  | 
2165  |  |      */  | 
2166  | 3.29k  |     if (did_append)  | 
2167  | 1.22k  |         ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),  | 
2168  | 1.22k  |                                           xso->stream);  | 
2169  |  |  | 
2170  |  |     /*  | 
2171  |  |      * Try and send.  | 
2172  |  |      *  | 
2173  |  |      * TODO(QUIC FUTURE): It is probably inefficient to try and do this  | 
2174  |  |      * immediately, plus we should eventually consider Nagle's algorithm.  | 
2175  |  |      */  | 
2176  | 3.29k  |     if (do_tick)  | 
2177  | 3.29k  |         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);  | 
2178  | 3.29k  | }  | 
2179  |  |  | 
2180  |  | struct quic_write_again_args { | 
2181  |  |     QUIC_XSO            *xso;  | 
2182  |  |     const unsigned char *buf;  | 
2183  |  |     size_t              len;  | 
2184  |  |     size_t              total_written;  | 
2185  |  |     int                 err;  | 
2186  |  | };  | 
2187  |  |  | 
2188  |  | /*  | 
2189  |  |  * Absolute maximum write buffer size, enforced to prevent a rogue peer from  | 
2190  |  |  * deliberately inducing DoS. This has been chosen based on the optimal buffer  | 
2191  |  |  * size for an RTT of 500ms and a bandwidth of 100 Mb/s.  | 
2192  |  |  */  | 
2193  | 0  | #define MAX_WRITE_BUF_SIZE      (6 * 1024 * 1024)  | 
2194  |  |  | 
2195  |  | /*  | 
2196  |  |  * Ensure spare buffer space available (up until a limit, at least).  | 
2197  |  |  */  | 
2198  |  | QUIC_NEEDS_LOCK  | 
2199  |  | static int sstream_ensure_spare(QUIC_SSTREAM *sstream, uint64_t spare)  | 
2200  | 6.08k  | { | 
2201  | 6.08k  |     size_t cur_sz = ossl_quic_sstream_get_buffer_size(sstream);  | 
2202  | 6.08k  |     size_t avail = ossl_quic_sstream_get_buffer_avail(sstream);  | 
2203  | 6.08k  |     size_t spare_ = (spare > SIZE_MAX) ? SIZE_MAX : (size_t)spare;  | 
2204  | 6.08k  |     size_t new_sz, growth;  | 
2205  |  |  | 
2206  | 6.08k  |     if (spare_ <= avail || cur_sz == MAX_WRITE_BUF_SIZE)  | 
2207  | 6.08k  |         return 1;  | 
2208  |  |  | 
2209  | 0  |     growth = spare_ - avail;  | 
2210  | 0  |     if (cur_sz + growth > MAX_WRITE_BUF_SIZE)  | 
2211  | 0  |         new_sz = MAX_WRITE_BUF_SIZE;  | 
2212  | 0  |     else  | 
2213  | 0  |         new_sz = cur_sz + growth;  | 
2214  |  | 
  | 
2215  | 0  |     return ossl_quic_sstream_set_buffer_size(sstream, new_sz);  | 
2216  | 6.08k  | }  | 
2217  |  |  | 
2218  |  | /*  | 
2219  |  |  * Append to a QUIC_STREAM's QUIC_SSTREAM, ensuring buffer space is expanded  | 
2220  |  |  * as needed according to flow control.  | 
2221  |  |  */  | 
2222  |  | QUIC_NEEDS_LOCK  | 
2223  |  | static int xso_sstream_append(QUIC_XSO *xso, const unsigned char *buf,  | 
2224  |  |                               size_t len, size_t *actual_written)  | 
2225  | 6.08k  | { | 
2226  | 6.08k  |     QUIC_SSTREAM *sstream = xso->stream->sstream;  | 
2227  | 6.08k  |     uint64_t cur = ossl_quic_sstream_get_cur_size(sstream);  | 
2228  | 6.08k  |     uint64_t cwm = ossl_quic_txfc_get_cwm(&xso->stream->txfc);  | 
2229  | 6.08k  |     uint64_t permitted = (cwm >= cur ? cwm - cur : 0);  | 
2230  |  |  | 
2231  | 6.08k  |     if (len > permitted)  | 
2232  | 4.40k  |         len = (size_t)permitted;  | 
2233  |  |  | 
2234  | 6.08k  |     if (!sstream_ensure_spare(sstream, len))  | 
2235  | 0  |         return 0;  | 
2236  |  |  | 
2237  | 6.08k  |     return ossl_quic_sstream_append(sstream, buf, len, actual_written);  | 
2238  | 6.08k  | }  | 
2239  |  |  | 
2240  |  | QUIC_NEEDS_LOCK  | 
2241  |  | static int quic_write_again(void *arg)  | 
2242  | 0  | { | 
2243  | 0  |     struct quic_write_again_args *args = arg;  | 
2244  | 0  |     size_t actual_written = 0;  | 
2245  |  | 
  | 
2246  | 0  |     if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))  | 
2247  |  |         /* If connection is torn down due to an error while blocking, stop. */  | 
2248  | 0  |         return -2;  | 
2249  |  |  | 
2250  | 0  |     if (!quic_validate_for_write(args->xso, &args->err))  | 
2251  |  |         /*  | 
2252  |  |          * Stream may have become invalid for write due to connection events  | 
2253  |  |          * while we blocked.  | 
2254  |  |          */  | 
2255  | 0  |         return -2;  | 
2256  |  |  | 
2257  | 0  |     args->err = ERR_R_INTERNAL_ERROR;  | 
2258  | 0  |     if (!xso_sstream_append(args->xso, args->buf, args->len, &actual_written))  | 
2259  | 0  |         return -2;  | 
2260  |  |  | 
2261  | 0  |     quic_post_write(args->xso, actual_written > 0, 0);  | 
2262  |  | 
  | 
2263  | 0  |     args->buf           += actual_written;  | 
2264  | 0  |     args->len           -= actual_written;  | 
2265  | 0  |     args->total_written += actual_written;  | 
2266  |  | 
  | 
2267  | 0  |     if (args->len == 0)  | 
2268  |  |         /* Written everything, done. */  | 
2269  | 0  |         return 1;  | 
2270  |  |  | 
2271  |  |     /* Not written everything yet, keep trying. */  | 
2272  | 0  |     return 0;  | 
2273  | 0  | }  | 
2274  |  |  | 
2275  |  | QUIC_NEEDS_LOCK  | 
2276  |  | static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,  | 
2277  |  |                                size_t *written)  | 
2278  | 0  | { | 
2279  | 0  |     int res;  | 
2280  | 0  |     QUIC_XSO *xso = ctx->xso;  | 
2281  | 0  |     struct quic_write_again_args args;  | 
2282  | 0  |     size_t actual_written = 0;  | 
2283  |  |  | 
2284  |  |     /* First make a best effort to append as much of the data as possible. */  | 
2285  | 0  |     if (!xso_sstream_append(xso, buf, len, &actual_written)) { | 
2286  |  |         /* Stream already finished or allocation error. */  | 
2287  | 0  |         *written = 0;  | 
2288  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2289  | 0  |     }  | 
2290  |  |  | 
2291  | 0  |     quic_post_write(xso, actual_written > 0, 1);  | 
2292  |  |  | 
2293  |  |     /*  | 
2294  |  |      * Record however much data we wrote  | 
2295  |  |      */  | 
2296  | 0  |     *written = actual_written;  | 
2297  |  | 
  | 
2298  | 0  |     if (actual_written == len) { | 
2299  |  |         /* Managed to append everything on the first try. */  | 
2300  | 0  |         return 1;  | 
2301  | 0  |     }  | 
2302  |  |  | 
2303  |  |     /*  | 
2304  |  |      * We did not manage to append all of the data immediately, so the stream  | 
2305  |  |      * buffer has probably filled up. This means we need to block until some of  | 
2306  |  |      * it is freed up.  | 
2307  |  |      */  | 
2308  | 0  |     args.xso            = xso;  | 
2309  | 0  |     args.buf            = (const unsigned char *)buf + actual_written;  | 
2310  | 0  |     args.len            = len - actual_written;  | 
2311  | 0  |     args.total_written  = 0;  | 
2312  | 0  |     args.err            = ERR_R_INTERNAL_ERROR;  | 
2313  |  | 
  | 
2314  | 0  |     res = block_until_pred(xso->conn, quic_write_again, &args, 0);  | 
2315  | 0  |     if (res <= 0) { | 
2316  | 0  |         if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))  | 
2317  | 0  |             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
2318  | 0  |         else  | 
2319  | 0  |             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);  | 
2320  | 0  |     }  | 
2321  |  |  | 
2322  |  |     /*  | 
2323  |  |      * When waiting on extra buffer space to be available, args.total_written  | 
2324  |  |      * holds the amount of remaining data we requested to write, which will be  | 
2325  |  |      * something less than the len parameter passed in, however much we wrote  | 
2326  |  |      * here, add it to the value that we wrote when we initially called  | 
2327  |  |      * xso_sstream_append  | 
2328  |  |      */  | 
2329  | 0  |     *written += args.total_written;  | 
2330  | 0  |     return 1;  | 
2331  | 0  | }  | 
2332  |  |  | 
2333  |  | /*  | 
2334  |  |  * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)  | 
2335  |  |  * write semantics.  | 
2336  |  |  */  | 
2337  |  | static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,  | 
2338  |  |                             size_t buf_len, size_t already_sent)  | 
2339  | 222  | { | 
2340  | 222  |     assert(!xso->aon_write_in_progress);  | 
2341  |  |  | 
2342  | 222  |     xso->aon_write_in_progress = 1;  | 
2343  | 222  |     xso->aon_buf_base          = buf;  | 
2344  | 222  |     xso->aon_buf_pos           = already_sent;  | 
2345  | 222  |     xso->aon_buf_len           = buf_len;  | 
2346  | 222  | }  | 
2347  |  |  | 
2348  |  | static void aon_write_finish(QUIC_XSO *xso)  | 
2349  | 75  | { | 
2350  | 75  |     xso->aon_write_in_progress   = 0;  | 
2351  | 75  |     xso->aon_buf_base            = NULL;  | 
2352  | 75  |     xso->aon_buf_pos             = 0;  | 
2353  | 75  |     xso->aon_buf_len             = 0;  | 
2354  | 75  | }  | 
2355  |  |  | 
2356  |  | QUIC_NEEDS_LOCK  | 
2357  |  | static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,  | 
2358  |  |                                       size_t len, size_t *written)  | 
2359  | 3.29k  | { | 
2360  | 3.29k  |     QUIC_XSO *xso = ctx->xso;  | 
2361  | 3.29k  |     const void *actual_buf;  | 
2362  | 3.29k  |     size_t actual_len, actual_written = 0;  | 
2363  | 3.29k  |     int accept_moving_buffer  | 
2364  | 3.29k  |         = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);  | 
2365  |  |  | 
2366  | 3.29k  |     if (xso->aon_write_in_progress) { | 
2367  |  |         /*  | 
2368  |  |          * We are in the middle of an AON write (i.e., a previous write did not  | 
2369  |  |          * manage to append all data to the SSTREAM and we have Enable Partial  | 
2370  |  |          * Write (EPW) mode disabled.)  | 
2371  |  |          */  | 
2372  | 1.43k  |         if ((!accept_moving_buffer && xso->aon_buf_base != buf)  | 
2373  | 1.43k  |             || len != xso->aon_buf_len)  | 
2374  |  |             /*  | 
2375  |  |              * Pointer must not have changed if we are not in accept moving  | 
2376  |  |              * buffer mode. Length must never change.  | 
2377  |  |              */  | 
2378  | 0  |             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);  | 
2379  |  |  | 
2380  | 1.43k  |         actual_buf = (unsigned char *)buf + xso->aon_buf_pos;  | 
2381  | 1.43k  |         actual_len = len - xso->aon_buf_pos;  | 
2382  | 1.43k  |         assert(actual_len > 0);  | 
2383  | 1.85k  |     } else { | 
2384  | 1.85k  |         actual_buf = buf;  | 
2385  | 1.85k  |         actual_len = len;  | 
2386  | 1.85k  |     }  | 
2387  |  |  | 
2388  |  |     /* First make a best effort to append as much of the data as possible. */  | 
2389  | 3.29k  |     if (!xso_sstream_append(xso, actual_buf, actual_len, &actual_written)) { | 
2390  |  |         /* Stream already finished or allocation error. */  | 
2391  | 0  |         *written = 0;  | 
2392  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2393  | 0  |     }  | 
2394  |  |  | 
2395  | 3.29k  |     quic_post_write(xso, actual_written > 0, 1);  | 
2396  |  |  | 
2397  | 3.29k  |     if (actual_written == actual_len) { | 
2398  |  |         /* We have sent everything. */  | 
2399  | 1.07k  |         if (xso->aon_write_in_progress) { | 
2400  |  |             /*  | 
2401  |  |              * We have sent everything, and we were in the middle of an AON  | 
2402  |  |              * write. The output write length is the total length of the AON  | 
2403  |  |              * buffer, not however many bytes we managed to write to the stream  | 
2404  |  |              * in this call.  | 
2405  |  |              */  | 
2406  | 73  |             *written = xso->aon_buf_len;  | 
2407  | 73  |             aon_write_finish(xso);  | 
2408  | 999  |         } else { | 
2409  | 999  |             *written = actual_written;  | 
2410  | 999  |         }  | 
2411  |  |  | 
2412  | 1.07k  |         return 1;  | 
2413  | 1.07k  |     }  | 
2414  |  |  | 
2415  | 2.21k  |     if (xso->aon_write_in_progress) { | 
2416  |  |         /*  | 
2417  |  |          * AON write is in progress but we have not written everything yet. We  | 
2418  |  |          * may have managed to send zero bytes, or some number of bytes less  | 
2419  |  |          * than the total remaining which need to be appended during this  | 
2420  |  |          * AON operation.  | 
2421  |  |          */  | 
2422  | 1.36k  |         xso->aon_buf_pos += actual_written;  | 
2423  | 1.36k  |         assert(xso->aon_buf_pos < xso->aon_buf_len);  | 
2424  | 1.36k  |         return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);  | 
2425  | 1.36k  |     }  | 
2426  |  |  | 
2427  |  |     /*  | 
2428  |  |      * Not in an existing AON operation but partial write is not enabled, so we  | 
2429  |  |      * need to begin a new AON operation. However we needn't bother if we didn't  | 
2430  |  |      * actually append anything.  | 
2431  |  |      */  | 
2432  | 855  |     if (actual_written > 0)  | 
2433  | 148  |         aon_write_begin(xso, buf, len, actual_written);  | 
2434  |  |  | 
2435  |  |     /*  | 
2436  |  |      * AON - We do not publicly admit to having appended anything until AON  | 
2437  |  |      * completes.  | 
2438  |  |      */  | 
2439  | 855  |     *written = 0;  | 
2440  | 855  |     return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);  | 
2441  | 2.21k  | }  | 
2442  |  |  | 
2443  |  | QUIC_NEEDS_LOCK  | 
2444  |  | static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,  | 
2445  |  |                                       size_t *written)  | 
2446  | 0  | { | 
2447  | 0  |     QUIC_XSO *xso = ctx->xso;  | 
2448  |  |  | 
2449  |  |     /* Simple best effort operation. */  | 
2450  | 0  |     if (!xso_sstream_append(xso, buf, len, written)) { | 
2451  |  |         /* Stream already finished or allocation error. */  | 
2452  | 0  |         *written = 0;  | 
2453  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2454  | 0  |     }  | 
2455  |  |  | 
2456  | 0  |     quic_post_write(xso, *written > 0, 1);  | 
2457  |  | 
  | 
2458  | 0  |     if (*written == 0)  | 
2459  |  |         /* SSL_write_ex returns 0 if it didn't read anything .*/  | 
2460  | 0  |         return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);  | 
2461  |  |  | 
2462  | 0  |     return 1;  | 
2463  | 0  | }  | 
2464  |  |  | 
2465  |  | QUIC_NEEDS_LOCK  | 
2466  |  | static int quic_validate_for_write(QUIC_XSO *xso, int *err)  | 
2467  | 6.09k  | { | 
2468  | 6.09k  |     QUIC_STREAM_MAP *qsm;  | 
2469  |  |  | 
2470  | 6.09k  |     if (xso == NULL || xso->stream == NULL) { | 
2471  | 0  |         *err = ERR_R_INTERNAL_ERROR;  | 
2472  | 0  |         return 0;  | 
2473  | 0  |     }  | 
2474  |  |  | 
2475  | 6.09k  |     switch (xso->stream->send_state) { | 
2476  | 0  |     default:  | 
2477  | 4  |     case QUIC_SSTREAM_STATE_NONE:  | 
2478  | 4  |         *err = SSL_R_STREAM_RECV_ONLY;  | 
2479  | 4  |         return 0;  | 
2480  |  |  | 
2481  | 1.33k  |     case QUIC_SSTREAM_STATE_READY:  | 
2482  | 1.33k  |         qsm = ossl_quic_channel_get_qsm(xso->conn->ch);  | 
2483  |  |  | 
2484  | 1.33k  |         if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) { | 
2485  | 0  |             *err = ERR_R_INTERNAL_ERROR;  | 
2486  | 0  |             return 0;  | 
2487  | 0  |         }  | 
2488  |  |  | 
2489  |  |         /* FALLTHROUGH */  | 
2490  | 6.08k  |     case QUIC_SSTREAM_STATE_SEND:  | 
2491  | 6.08k  |     case QUIC_SSTREAM_STATE_DATA_SENT:  | 
2492  | 6.08k  |         if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) { | 
2493  | 0  |             *err = SSL_R_STREAM_FINISHED;  | 
2494  | 0  |             return 0;  | 
2495  | 0  |         }  | 
2496  | 6.08k  |         return 1;  | 
2497  |  |  | 
2498  | 0  |     case QUIC_SSTREAM_STATE_DATA_RECVD:  | 
2499  | 0  |         *err = SSL_R_STREAM_FINISHED;  | 
2500  | 0  |         return 0;  | 
2501  |  |  | 
2502  | 9  |     case QUIC_SSTREAM_STATE_RESET_SENT:  | 
2503  | 9  |     case QUIC_SSTREAM_STATE_RESET_RECVD:  | 
2504  | 9  |         *err = SSL_R_STREAM_RESET;  | 
2505  | 9  |         return 0;  | 
2506  | 6.09k  |     }  | 
2507  | 6.09k  | }  | 
2508  |  |  | 
2509  |  | QUIC_TAKES_LOCK  | 
2510  |  | int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)  | 
2511  | 3.36k  | { | 
2512  | 3.36k  |     int ret;  | 
2513  | 3.36k  |     QCTX ctx;  | 
2514  | 3.36k  |     int partial_write, err;  | 
2515  |  |  | 
2516  | 3.36k  |     *written = 0;  | 
2517  |  |  | 
2518  | 3.36k  |     if (len == 0) { | 
2519  |  |         /* Do not autocreate default XSO for zero-length writes. */  | 
2520  | 0  |         if (!expect_quic(s, &ctx))  | 
2521  | 0  |             return 0;  | 
2522  |  |  | 
2523  | 0  |         quic_lock_for_io(&ctx);  | 
2524  | 3.36k  |     } else { | 
2525  | 3.36k  |         if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/1, &ctx))  | 
2526  | 0  |             return 0;  | 
2527  | 3.36k  |     }  | 
2528  |  |  | 
2529  | 3.36k  |     partial_write = ((ctx.xso != NULL)  | 
2530  | 3.36k  |         ? ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0) : 0);  | 
2531  |  |  | 
2532  | 3.36k  |     if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) { | 
2533  | 73  |         ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
2534  | 73  |         goto out;  | 
2535  | 73  |     }  | 
2536  |  |  | 
2537  |  |     /*  | 
2538  |  |      * If we haven't finished the handshake, try to advance it.  | 
2539  |  |      * We don't accept writes until the handshake is completed.  | 
2540  |  |      */  | 
2541  | 3.29k  |     if (quic_do_handshake(&ctx) < 1) { | 
2542  | 0  |         ret = 0;  | 
2543  | 0  |         goto out;  | 
2544  | 0  |     }  | 
2545  |  |  | 
2546  |  |     /* Ensure correct stream state, stream send part not concluded, etc. */  | 
2547  | 3.29k  |     if (len > 0 && !quic_validate_for_write(ctx.xso, &err)) { | 
2548  | 5  |         ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);  | 
2549  | 5  |         goto out;  | 
2550  | 5  |     }  | 
2551  |  |  | 
2552  | 3.29k  |     if (len == 0) { | 
2553  | 0  |         ret = 1;  | 
2554  | 0  |         goto out;  | 
2555  | 0  |     }  | 
2556  |  |  | 
2557  | 3.29k  |     if (xso_blocking_mode(ctx.xso))  | 
2558  | 0  |         ret = quic_write_blocking(&ctx, buf, len, written);  | 
2559  | 3.29k  |     else if (partial_write)  | 
2560  | 0  |         ret = quic_write_nonblocking_epw(&ctx, buf, len, written);  | 
2561  | 3.29k  |     else  | 
2562  | 3.29k  |         ret = quic_write_nonblocking_aon(&ctx, buf, len, written);  | 
2563  |  |  | 
2564  | 3.36k  | out:  | 
2565  | 3.36k  |     quic_unlock(ctx.qc);  | 
2566  | 3.36k  |     return ret;  | 
2567  | 3.29k  | }  | 
2568  |  |  | 
2569  |  | /*  | 
2570  |  |  * SSL_read  | 
2571  |  |  * --------  | 
2572  |  |  */  | 
2573  |  | struct quic_read_again_args { | 
2574  |  |     QCTX            *ctx;  | 
2575  |  |     QUIC_STREAM     *stream;  | 
2576  |  |     void            *buf;  | 
2577  |  |     size_t          len;  | 
2578  |  |     size_t          *bytes_read;  | 
2579  |  |     int             peek;  | 
2580  |  | };  | 
2581  |  |  | 
2582  |  | QUIC_NEEDS_LOCK  | 
2583  |  | static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)  | 
2584  | 11.5M  | { | 
2585  | 11.5M  |     QUIC_STREAM_MAP *qsm;  | 
2586  |  |  | 
2587  | 11.5M  |     *eos = 0;  | 
2588  |  |  | 
2589  | 11.5M  |     if (xso == NULL || xso->stream == NULL) { | 
2590  | 0  |         *err = ERR_R_INTERNAL_ERROR;  | 
2591  | 0  |         return 0;  | 
2592  | 0  |     }  | 
2593  |  |  | 
2594  | 11.5M  |     switch (xso->stream->recv_state) { | 
2595  | 0  |     default:  | 
2596  | 0  |     case QUIC_RSTREAM_STATE_NONE:  | 
2597  | 0  |         *err = SSL_R_STREAM_SEND_ONLY;  | 
2598  | 0  |         return 0;  | 
2599  |  |  | 
2600  | 5.28M  |     case QUIC_RSTREAM_STATE_RECV:  | 
2601  | 11.5M  |     case QUIC_RSTREAM_STATE_SIZE_KNOWN:  | 
2602  | 11.5M  |     case QUIC_RSTREAM_STATE_DATA_RECVD:  | 
2603  | 11.5M  |         return 1;  | 
2604  |  |  | 
2605  | 107  |     case QUIC_RSTREAM_STATE_DATA_READ:  | 
2606  | 107  |         *eos = 1;  | 
2607  | 107  |         return 0;  | 
2608  |  |  | 
2609  | 86  |     case QUIC_RSTREAM_STATE_RESET_RECVD:  | 
2610  | 86  |         qsm = ossl_quic_channel_get_qsm(xso->conn->ch);  | 
2611  | 86  |         ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);  | 
2612  |  |  | 
2613  |  |         /* FALLTHROUGH */  | 
2614  | 86  |     case QUIC_RSTREAM_STATE_RESET_READ:  | 
2615  | 86  |         *err = SSL_R_STREAM_RESET;  | 
2616  | 86  |         return 0;  | 
2617  | 11.5M  |     }  | 
2618  | 11.5M  | }  | 
2619  |  |  | 
2620  |  | QUIC_NEEDS_LOCK  | 
2621  |  | static int quic_read_actual(QCTX *ctx,  | 
2622  |  |                             QUIC_STREAM *stream,  | 
2623  |  |                             void *buf, size_t buf_len,  | 
2624  |  |                             size_t *bytes_read,  | 
2625  |  |                             int peek)  | 
2626  | 11.5M  | { | 
2627  | 11.5M  |     int is_fin = 0, err, eos;  | 
2628  | 11.5M  |     QUIC_CONNECTION *qc = ctx->qc;  | 
2629  |  |  | 
2630  | 11.5M  |     if (!quic_validate_for_read(ctx->xso, &err, &eos)) { | 
2631  | 193  |         if (eos)  | 
2632  | 107  |             return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);  | 
2633  | 86  |         else  | 
2634  | 86  |             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);  | 
2635  | 193  |     }  | 
2636  |  |  | 
2637  | 11.5M  |     if (peek) { | 
2638  | 0  |         if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,  | 
2639  | 0  |                                     bytes_read, &is_fin))  | 
2640  | 0  |             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2641  |  | 
  | 
2642  | 11.5M  |     } else { | 
2643  | 11.5M  |         if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,  | 
2644  | 11.5M  |                                     bytes_read, &is_fin))  | 
2645  | 0  |             return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2646  | 11.5M  |     }  | 
2647  |  |  | 
2648  | 11.5M  |     if (!peek) { | 
2649  | 11.5M  |         if (*bytes_read > 0) { | 
2650  |  |             /*  | 
2651  |  |              * We have read at least one byte from the stream. Inform stream-level  | 
2652  |  |              * RXFC of the retirement of controlled bytes. Update the active stream  | 
2653  |  |              * status (the RXFC may now want to emit a frame granting more credit to  | 
2654  |  |              * the peer).  | 
2655  |  |              */  | 
2656  | 2.00k  |             OSSL_RTT_INFO rtt_info;  | 
2657  |  |  | 
2658  | 2.00k  |             ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);  | 
2659  |  |  | 
2660  | 2.00k  |             if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,  | 
2661  | 2.00k  |                                           rtt_info.smoothed_rtt))  | 
2662  | 0  |                 return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2663  | 2.00k  |         }  | 
2664  |  |  | 
2665  | 11.5M  |         if (is_fin && !peek) { | 
2666  | 302  |             QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);  | 
2667  |  |  | 
2668  | 302  |             ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);  | 
2669  | 302  |         }  | 
2670  |  |  | 
2671  | 11.5M  |         if (*bytes_read > 0)  | 
2672  | 2.00k  |             ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),  | 
2673  | 2.00k  |                                               stream);  | 
2674  | 11.5M  |     }  | 
2675  |  |  | 
2676  | 11.5M  |     if (*bytes_read == 0 && is_fin)  | 
2677  | 28  |         return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);  | 
2678  |  |  | 
2679  | 11.5M  |     return 1;  | 
2680  | 11.5M  | }  | 
2681  |  |  | 
2682  |  | QUIC_NEEDS_LOCK  | 
2683  |  | static int quic_read_again(void *arg)  | 
2684  | 0  | { | 
2685  | 0  |     struct quic_read_again_args *args = arg;  | 
2686  |  | 
  | 
2687  | 0  |     if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) { | 
2688  |  |         /* If connection is torn down due to an error while blocking, stop. */  | 
2689  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
2690  | 0  |         return -1;  | 
2691  | 0  |     }  | 
2692  |  |  | 
2693  | 0  |     if (!quic_read_actual(args->ctx, args->stream,  | 
2694  | 0  |                           args->buf, args->len, args->bytes_read,  | 
2695  | 0  |                           args->peek))  | 
2696  | 0  |         return -1;  | 
2697  |  |  | 
2698  | 0  |     if (*args->bytes_read > 0)  | 
2699  |  |         /* got at least one byte, the SSL_read op can finish now */  | 
2700  | 0  |         return 1;  | 
2701  |  |  | 
2702  | 0  |     return 0; /* did not read anything, keep trying */  | 
2703  | 0  | }  | 
2704  |  |  | 
2705  |  | QUIC_TAKES_LOCK  | 
2706  |  | static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)  | 
2707  | 3.78M  | { | 
2708  | 3.78M  |     int ret, res;  | 
2709  | 3.78M  |     QCTX ctx;  | 
2710  | 3.78M  |     struct quic_read_again_args args;  | 
2711  |  |  | 
2712  | 3.78M  |     *bytes_read = 0;  | 
2713  |  |  | 
2714  | 3.78M  |     if (!expect_quic(s, &ctx))  | 
2715  | 0  |         return 0;  | 
2716  |  |  | 
2717  | 3.78M  |     quic_lock_for_io(&ctx);  | 
2718  |  |  | 
2719  | 3.78M  |     if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) { | 
2720  | 1.38k  |         ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
2721  | 1.38k  |         goto out;  | 
2722  | 1.38k  |     }  | 
2723  |  |  | 
2724  |  |     /* If we haven't finished the handshake, try to advance it. */  | 
2725  | 3.78M  |     if (quic_do_handshake(&ctx) < 1) { | 
2726  | 0  |         ret = 0; /* ossl_quic_do_handshake raised error here */  | 
2727  | 0  |         goto out;  | 
2728  | 0  |     }  | 
2729  |  |  | 
2730  | 3.78M  |     if (ctx.xso == NULL) { | 
2731  |  |         /*  | 
2732  |  |          * Called on a QCSO and we don't currently have a default stream.  | 
2733  |  |          *  | 
2734  |  |          * Wait until we get a stream initiated by the peer (blocking mode) or  | 
2735  |  |          * fail if we don't have one yet (non-blocking mode).  | 
2736  |  |          */  | 
2737  | 774k  |         if (!qc_wait_for_default_xso_for_read(&ctx, /*peek=*/0)) { | 
2738  | 773k  |             ret = 0; /* error already raised here */  | 
2739  | 773k  |             goto out;  | 
2740  | 773k  |         }  | 
2741  |  |  | 
2742  | 1.53k  |         ctx.xso = ctx.qc->default_xso;  | 
2743  | 1.53k  |     }  | 
2744  |  |  | 
2745  | 3.01M  |     if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) { | 
2746  | 93  |         ret = 0; /* quic_read_actual raised error here */  | 
2747  | 93  |         goto out;  | 
2748  | 93  |     }  | 
2749  |  |  | 
2750  | 3.01M  |     if (*bytes_read > 0) { | 
2751  |  |         /*  | 
2752  |  |          * Even though we succeeded, tick the reactor here to ensure we are  | 
2753  |  |          * handling other aspects of the QUIC connection.  | 
2754  |  |          */  | 
2755  | 863  |         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);  | 
2756  | 863  |         ret = 1;  | 
2757  | 3.00M  |     } else if (xso_blocking_mode(ctx.xso)) { | 
2758  |  |         /*  | 
2759  |  |          * We were not able to read anything immediately, so our stream  | 
2760  |  |          * buffer is empty. This means we need to block until we get  | 
2761  |  |          * at least one byte.  | 
2762  |  |          */  | 
2763  | 0  |         args.ctx        = &ctx;  | 
2764  | 0  |         args.stream     = ctx.xso->stream;  | 
2765  | 0  |         args.buf        = buf;  | 
2766  | 0  |         args.len        = len;  | 
2767  | 0  |         args.bytes_read = bytes_read;  | 
2768  | 0  |         args.peek       = peek;  | 
2769  |  | 
  | 
2770  | 0  |         res = block_until_pred(ctx.qc, quic_read_again, &args, 0);  | 
2771  | 0  |         if (res == 0) { | 
2772  | 0  |             ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2773  | 0  |             goto out;  | 
2774  | 0  |         } else if (res < 0) { | 
2775  | 0  |             ret = 0; /* quic_read_again raised error here */  | 
2776  | 0  |             goto out;  | 
2777  | 0  |         }  | 
2778  |  |  | 
2779  | 0  |         ret = 1;  | 
2780  | 3.00M  |     } else { | 
2781  |  |         /*  | 
2782  |  |          * We did not get any bytes and are not in blocking mode.  | 
2783  |  |          * Tick to see if this delivers any more.  | 
2784  |  |          */  | 
2785  | 3.00M  |         ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0);  | 
2786  |  |  | 
2787  |  |         /* Try the read again. */  | 
2788  | 3.00M  |         if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) { | 
2789  | 13  |             ret = 0; /* quic_read_actual raised error here */  | 
2790  | 13  |             goto out;  | 
2791  | 13  |         }  | 
2792  |  |  | 
2793  | 3.00M  |         if (*bytes_read > 0)  | 
2794  | 356  |             ret = 1; /* Succeeded this time. */  | 
2795  | 3.00M  |         else  | 
2796  | 3.00M  |             ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);  | 
2797  | 3.00M  |     }  | 
2798  |  |  | 
2799  | 3.78M  | out:  | 
2800  | 3.78M  |     quic_unlock(ctx.qc);  | 
2801  | 3.78M  |     return ret;  | 
2802  | 3.01M  | }  | 
2803  |  |  | 
2804  |  | int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)  | 
2805  | 6.66M  | { | 
2806  | 6.66M  |     return quic_read(s, buf, len, bytes_read, 0);  | 
2807  | 6.66M  | }  | 
2808  |  |  | 
2809  |  | int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)  | 
2810  | 0  | { | 
2811  | 0  |     return quic_read(s, buf, len, bytes_read, 1);  | 
2812  | 0  | }  | 
2813  |  |  | 
2814  |  | /*  | 
2815  |  |  * SSL_pending  | 
2816  |  |  * -----------  | 
2817  |  |  */  | 
2818  |  |  | 
2819  |  | QUIC_TAKES_LOCK  | 
2820  |  | static size_t ossl_quic_pending_int(const SSL *s, int check_channel)  | 
2821  | 0  | { | 
2822  | 0  |     QCTX ctx;  | 
2823  | 0  |     size_t avail = 0;  | 
2824  |  | 
  | 
2825  | 0  |     if (!expect_quic(s, &ctx))  | 
2826  | 0  |         return 0;  | 
2827  |  |  | 
2828  | 0  |     quic_lock(ctx.qc);  | 
2829  |  | 
  | 
2830  | 0  |     if (!ctx.qc->started)  | 
2831  | 0  |         goto out;  | 
2832  |  |  | 
2833  | 0  |     if (ctx.xso == NULL) { | 
2834  |  |         /* No XSO yet, but there might be a default XSO eligible to be created. */  | 
2835  | 0  |         if (qc_wait_for_default_xso_for_read(&ctx, /*peek=*/1)) { | 
2836  | 0  |             ctx.xso = ctx.qc->default_xso;  | 
2837  | 0  |         } else { | 
2838  | 0  |             QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_NO_STREAM, NULL);  | 
2839  | 0  |             goto out;  | 
2840  | 0  |         }  | 
2841  | 0  |     }  | 
2842  |  |  | 
2843  | 0  |     if (ctx.xso->stream == NULL) { | 
2844  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
2845  | 0  |         goto out;  | 
2846  | 0  |     }  | 
2847  |  |  | 
2848  | 0  |     if (check_channel)  | 
2849  | 0  |         avail = ossl_quic_stream_recv_pending(ctx.xso->stream,  | 
2850  | 0  |                                               /*include_fin=*/1)  | 
2851  | 0  |              || ossl_quic_channel_has_pending(ctx.qc->ch)  | 
2852  | 0  |              || ossl_quic_channel_is_term_any(ctx.qc->ch);  | 
2853  | 0  |     else  | 
2854  | 0  |         avail = ossl_quic_stream_recv_pending(ctx.xso->stream,  | 
2855  | 0  |                                               /*include_fin=*/0);  | 
2856  |  | 
  | 
2857  | 0  | out:  | 
2858  | 0  |     quic_unlock(ctx.qc);  | 
2859  | 0  |     return avail;  | 
2860  | 0  | }  | 
2861  |  |  | 
2862  |  | size_t ossl_quic_pending(const SSL *s)  | 
2863  | 0  | { | 
2864  | 0  |     return ossl_quic_pending_int(s, /*check_channel=*/0);  | 
2865  | 0  | }  | 
2866  |  |  | 
2867  |  | int ossl_quic_has_pending(const SSL *s)  | 
2868  | 0  | { | 
2869  |  |     /* Do we have app-side pending data or pending URXEs or RXEs? */  | 
2870  | 0  |     return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;  | 
2871  | 0  | }  | 
2872  |  |  | 
2873  |  | /*  | 
2874  |  |  * SSL_stream_conclude  | 
2875  |  |  * -------------------  | 
2876  |  |  */  | 
2877  |  | QUIC_TAKES_LOCK  | 
2878  |  | int ossl_quic_conn_stream_conclude(SSL *s)  | 
2879  | 0  | { | 
2880  | 0  |     QCTX ctx;  | 
2881  | 0  |     QUIC_STREAM *qs;  | 
2882  | 0  |     int err;  | 
2883  |  | 
  | 
2884  | 0  |     if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/0, &ctx))  | 
2885  | 0  |         return 0;  | 
2886  |  |  | 
2887  | 0  |     qs = ctx.xso->stream;  | 
2888  |  | 
  | 
2889  | 0  |     if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) { | 
2890  | 0  |         quic_unlock(ctx.qc);  | 
2891  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
2892  | 0  |     }  | 
2893  |  |  | 
2894  | 0  |     if (!quic_validate_for_write(ctx.xso, &err)) { | 
2895  | 0  |         quic_unlock(ctx.qc);  | 
2896  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);  | 
2897  | 0  |     }  | 
2898  |  |  | 
2899  | 0  |     if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) { | 
2900  | 0  |         quic_unlock(ctx.qc);  | 
2901  | 0  |         return 1;  | 
2902  | 0  |     }  | 
2903  |  |  | 
2904  | 0  |     ossl_quic_sstream_fin(qs->sstream);  | 
2905  | 0  |     quic_post_write(ctx.xso, 1, 1);  | 
2906  | 0  |     quic_unlock(ctx.qc);  | 
2907  | 0  |     return 1;  | 
2908  | 0  | }  | 
2909  |  |  | 
2910  |  | /*  | 
2911  |  |  * SSL_inject_net_dgram  | 
2912  |  |  * --------------------  | 
2913  |  |  */  | 
2914  |  | QUIC_TAKES_LOCK  | 
2915  |  | int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,  | 
2916  |  |                          size_t buf_len,  | 
2917  |  |                          const BIO_ADDR *peer,  | 
2918  |  |                          const BIO_ADDR *local)  | 
2919  | 0  | { | 
2920  | 0  |     int ret;  | 
2921  | 0  |     QCTX ctx;  | 
2922  | 0  |     QUIC_DEMUX *demux;  | 
2923  |  | 
  | 
2924  | 0  |     if (!expect_quic(s, &ctx))  | 
2925  | 0  |         return 0;  | 
2926  |  |  | 
2927  | 0  |     quic_lock(ctx.qc);  | 
2928  |  | 
  | 
2929  | 0  |     demux = ossl_quic_channel_get0_demux(ctx.qc->ch);  | 
2930  | 0  |     ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);  | 
2931  |  | 
  | 
2932  | 0  |     quic_unlock(ctx.qc);  | 
2933  | 0  |     return ret;  | 
2934  | 0  | }  | 
2935  |  |  | 
2936  |  | /*  | 
2937  |  |  * SSL_get0_connection  | 
2938  |  |  * -------------------  | 
2939  |  |  */  | 
2940  |  | SSL *ossl_quic_get0_connection(SSL *s)  | 
2941  | 0  | { | 
2942  | 0  |     QCTX ctx;  | 
2943  |  | 
  | 
2944  | 0  |     if (!expect_quic(s, &ctx))  | 
2945  | 0  |         return NULL;  | 
2946  |  |  | 
2947  | 0  |     return &ctx.qc->ssl;  | 
2948  | 0  | }  | 
2949  |  |  | 
2950  |  | /*  | 
2951  |  |  * SSL_get_stream_type  | 
2952  |  |  * -------------------  | 
2953  |  |  */  | 
2954  |  | int ossl_quic_get_stream_type(SSL *s)  | 
2955  | 0  | { | 
2956  | 0  |     QCTX ctx;  | 
2957  |  | 
  | 
2958  | 0  |     if (!expect_quic(s, &ctx))  | 
2959  | 0  |         return SSL_STREAM_TYPE_BIDI;  | 
2960  |  |  | 
2961  | 0  |     if (ctx.xso == NULL) { | 
2962  |  |         /*  | 
2963  |  |          * If deferred XSO creation has yet to occur, proceed according to the  | 
2964  |  |          * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know  | 
2965  |  |          * what kind of stream will be created yet, so return BIDI on the basis  | 
2966  |  |          * that at this time, the client still has the option of calling  | 
2967  |  |          * SSL_read() or SSL_write() first.  | 
2968  |  |          */  | 
2969  | 0  |         if (ctx.qc->default_xso_created  | 
2970  | 0  |             || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)  | 
2971  | 0  |             return SSL_STREAM_TYPE_NONE;  | 
2972  | 0  |         else  | 
2973  | 0  |             return SSL_STREAM_TYPE_BIDI;  | 
2974  | 0  |     }  | 
2975  |  |  | 
2976  | 0  |     if (ossl_quic_stream_is_bidi(ctx.xso->stream))  | 
2977  | 0  |         return SSL_STREAM_TYPE_BIDI;  | 
2978  |  |  | 
2979  | 0  |     if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)  | 
2980  | 0  |         return SSL_STREAM_TYPE_READ;  | 
2981  | 0  |     else  | 
2982  | 0  |         return SSL_STREAM_TYPE_WRITE;  | 
2983  | 0  | }  | 
2984  |  |  | 
2985  |  | /*  | 
2986  |  |  * SSL_get_stream_id  | 
2987  |  |  * -----------------  | 
2988  |  |  */  | 
2989  |  | QUIC_TAKES_LOCK  | 
2990  |  | uint64_t ossl_quic_get_stream_id(SSL *s)  | 
2991  | 0  | { | 
2992  | 0  |     QCTX ctx;  | 
2993  | 0  |     uint64_t id;  | 
2994  |  | 
  | 
2995  | 0  |     if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))  | 
2996  | 0  |         return UINT64_MAX;  | 
2997  |  |  | 
2998  | 0  |     id = ctx.xso->stream->id;  | 
2999  | 0  |     quic_unlock(ctx.qc);  | 
3000  |  | 
  | 
3001  | 0  |     return id;  | 
3002  | 0  | }  | 
3003  |  |  | 
3004  |  | /*  | 
3005  |  |  * SSL_is_stream_local  | 
3006  |  |  * -------------------  | 
3007  |  |  */  | 
3008  |  | QUIC_TAKES_LOCK  | 
3009  |  | int ossl_quic_is_stream_local(SSL *s)  | 
3010  | 0  | { | 
3011  | 0  |     QCTX ctx;  | 
3012  | 0  |     int is_local;  | 
3013  |  | 
  | 
3014  | 0  |     if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))  | 
3015  | 0  |         return -1;  | 
3016  |  |  | 
3017  | 0  |     is_local = ossl_quic_stream_is_local_init(ctx.xso->stream);  | 
3018  | 0  |     quic_unlock(ctx.qc);  | 
3019  |  | 
  | 
3020  | 0  |     return is_local;  | 
3021  | 0  | }  | 
3022  |  |  | 
3023  |  | /*  | 
3024  |  |  * SSL_set_default_stream_mode  | 
3025  |  |  * ---------------------------  | 
3026  |  |  */  | 
3027  |  | QUIC_TAKES_LOCK  | 
3028  |  | int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)  | 
3029  | 0  | { | 
3030  | 0  |     QCTX ctx;  | 
3031  |  | 
  | 
3032  | 0  |     if (!expect_quic_conn_only(s, &ctx))  | 
3033  | 0  |         return 0;  | 
3034  |  |  | 
3035  | 0  |     quic_lock(ctx.qc);  | 
3036  |  | 
  | 
3037  | 0  |     if (ctx.qc->default_xso_created) { | 
3038  | 0  |         quic_unlock(ctx.qc);  | 
3039  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,  | 
3040  | 0  |                                        "too late to change default stream mode");  | 
3041  | 0  |     }  | 
3042  |  |  | 
3043  | 0  |     switch (mode) { | 
3044  | 0  |     case SSL_DEFAULT_STREAM_MODE_NONE:  | 
3045  | 0  |     case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:  | 
3046  | 0  |     case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:  | 
3047  | 0  |         ctx.qc->default_stream_mode = mode;  | 
3048  | 0  |         break;  | 
3049  | 0  |     default:  | 
3050  | 0  |         quic_unlock(ctx.qc);  | 
3051  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,  | 
3052  | 0  |                                        "bad default stream type");  | 
3053  | 0  |     }  | 
3054  |  |  | 
3055  | 0  |     quic_unlock(ctx.qc);  | 
3056  | 0  |     return 1;  | 
3057  | 0  | }  | 
3058  |  |  | 
3059  |  | /*  | 
3060  |  |  * SSL_detach_stream  | 
3061  |  |  * -----------------  | 
3062  |  |  */  | 
3063  |  | QUIC_TAKES_LOCK  | 
3064  |  | SSL *ossl_quic_detach_stream(SSL *s)  | 
3065  | 0  | { | 
3066  | 0  |     QCTX ctx;  | 
3067  | 0  |     QUIC_XSO *xso = NULL;  | 
3068  |  | 
  | 
3069  | 0  |     if (!expect_quic_conn_only(s, &ctx))  | 
3070  | 0  |         return NULL;  | 
3071  |  |  | 
3072  | 0  |     quic_lock(ctx.qc);  | 
3073  |  |  | 
3074  |  |     /* Calling this function inhibits default XSO autocreation. */  | 
3075  |  |     /* QC ref to any default XSO is transferred to us and to caller. */  | 
3076  | 0  |     qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);  | 
3077  |  | 
  | 
3078  | 0  |     quic_unlock(ctx.qc);  | 
3079  |  | 
  | 
3080  | 0  |     return xso != NULL ? &xso->ssl : NULL;  | 
3081  | 0  | }  | 
3082  |  |  | 
3083  |  | /*  | 
3084  |  |  * SSL_attach_stream  | 
3085  |  |  * -----------------  | 
3086  |  |  */  | 
3087  |  | QUIC_TAKES_LOCK  | 
3088  |  | int ossl_quic_attach_stream(SSL *conn, SSL *stream)  | 
3089  | 0  | { | 
3090  | 0  |     QCTX ctx;  | 
3091  | 0  |     QUIC_XSO *xso;  | 
3092  | 0  |     int nref;  | 
3093  |  | 
  | 
3094  | 0  |     if (!expect_quic_conn_only(conn, &ctx))  | 
3095  | 0  |         return 0;  | 
3096  |  |  | 
3097  | 0  |     if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)  | 
3098  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,  | 
3099  | 0  |                                        "stream to attach must be a valid QUIC stream");  | 
3100  |  |  | 
3101  | 0  |     xso = (QUIC_XSO *)stream;  | 
3102  |  | 
  | 
3103  | 0  |     quic_lock(ctx.qc);  | 
3104  |  | 
  | 
3105  | 0  |     if (ctx.qc->default_xso != NULL) { | 
3106  | 0  |         quic_unlock(ctx.qc);  | 
3107  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,  | 
3108  | 0  |                                        "connection already has a default stream");  | 
3109  | 0  |     }  | 
3110  |  |  | 
3111  |  |     /*  | 
3112  |  |      * It is a caller error for the XSO being attached as a default XSO to have  | 
3113  |  |      * more than one ref.  | 
3114  |  |      */  | 
3115  | 0  |     if (!CRYPTO_GET_REF(&xso->ssl.references, &nref)) { | 
3116  | 0  |         quic_unlock(ctx.qc);  | 
3117  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,  | 
3118  | 0  |                                        "ref");  | 
3119  | 0  |     }  | 
3120  |  |  | 
3121  | 0  |     if (nref != 1) { | 
3122  | 0  |         quic_unlock(ctx.qc);  | 
3123  | 0  |         return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,  | 
3124  | 0  |                                        "stream being attached must have "  | 
3125  | 0  |                                        "only 1 reference");  | 
3126  | 0  |     }  | 
3127  |  |  | 
3128  |  |     /* Caller's reference to the XSO is transferred to us. */  | 
3129  |  |     /* Calling this function inhibits default XSO autocreation. */  | 
3130  | 0  |     qc_set_default_xso(ctx.qc, xso, /*touch=*/1);  | 
3131  |  | 
  | 
3132  | 0  |     quic_unlock(ctx.qc);  | 
3133  | 0  |     return 1;  | 
3134  | 0  | }  | 
3135  |  |  | 
3136  |  | /*  | 
3137  |  |  * SSL_set_incoming_stream_policy  | 
3138  |  |  * ------------------------------  | 
3139  |  |  */  | 
3140  |  | QUIC_NEEDS_LOCK  | 
3141  |  | static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)  | 
3142  | 50.0k  | { | 
3143  | 50.0k  |     switch (qc->incoming_stream_policy) { | 
3144  | 22.0k  |         case SSL_INCOMING_STREAM_POLICY_AUTO:  | 
3145  | 22.0k  |             if ((qc->default_xso == NULL && !qc->default_xso_created)  | 
3146  | 22.0k  |                 || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)  | 
3147  | 22.0k  |                 return SSL_INCOMING_STREAM_POLICY_ACCEPT;  | 
3148  | 0  |             else  | 
3149  | 0  |                 return SSL_INCOMING_STREAM_POLICY_REJECT;  | 
3150  |  |  | 
3151  | 27.9k  |         default:  | 
3152  | 27.9k  |             return qc->incoming_stream_policy;  | 
3153  | 50.0k  |     }  | 
3154  | 50.0k  | }  | 
3155  |  |  | 
3156  |  | QUIC_NEEDS_LOCK  | 
3157  |  | static void qc_update_reject_policy(QUIC_CONNECTION *qc)  | 
3158  | 49.8k  | { | 
3159  | 49.8k  |     int policy = qc_get_effective_incoming_stream_policy(qc);  | 
3160  | 49.8k  |     int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);  | 
3161  |  |  | 
3162  | 49.8k  |     ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,  | 
3163  | 49.8k  |                                                       enable_reject,  | 
3164  | 49.8k  |                                                       qc->incoming_stream_aec);  | 
3165  | 49.8k  | }  | 
3166  |  |  | 
3167  |  | QUIC_TAKES_LOCK  | 
3168  |  | int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,  | 
3169  |  |                                          uint64_t aec)  | 
3170  | 22.0k  | { | 
3171  | 22.0k  |     int ret = 1;  | 
3172  | 22.0k  |     QCTX ctx;  | 
3173  |  |  | 
3174  | 22.0k  |     if (!expect_quic_conn_only(s, &ctx))  | 
3175  | 0  |         return 0;  | 
3176  |  |  | 
3177  | 22.0k  |     quic_lock(ctx.qc);  | 
3178  |  |  | 
3179  | 22.0k  |     switch (policy) { | 
3180  | 0  |     case SSL_INCOMING_STREAM_POLICY_AUTO:  | 
3181  | 22.0k  |     case SSL_INCOMING_STREAM_POLICY_ACCEPT:  | 
3182  | 22.0k  |     case SSL_INCOMING_STREAM_POLICY_REJECT:  | 
3183  | 22.0k  |         ctx.qc->incoming_stream_policy = policy;  | 
3184  | 22.0k  |         ctx.qc->incoming_stream_aec    = aec;  | 
3185  | 22.0k  |         break;  | 
3186  |  |  | 
3187  | 0  |     default:  | 
3188  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);  | 
3189  | 0  |         ret = 0;  | 
3190  | 0  |         break;  | 
3191  | 22.0k  |     }  | 
3192  |  |  | 
3193  | 22.0k  |     qc_update_reject_policy(ctx.qc);  | 
3194  | 22.0k  |     quic_unlock(ctx.qc);  | 
3195  | 22.0k  |     return ret;  | 
3196  | 22.0k  | }  | 
3197  |  |  | 
3198  |  | /*  | 
3199  |  |  * SSL_accept_stream  | 
3200  |  |  * -----------------  | 
3201  |  |  */  | 
3202  |  | struct wait_for_incoming_stream_args { | 
3203  |  |     QCTX            *ctx;  | 
3204  |  |     QUIC_STREAM     *qs;  | 
3205  |  | };  | 
3206  |  |  | 
3207  |  | QUIC_NEEDS_LOCK  | 
3208  |  | static int wait_for_incoming_stream(void *arg)  | 
3209  | 0  | { | 
3210  | 0  |     struct wait_for_incoming_stream_args *args = arg;  | 
3211  | 0  |     QUIC_CONNECTION *qc = args->ctx->qc;  | 
3212  | 0  |     QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);  | 
3213  |  | 
  | 
3214  | 0  |     if (!quic_mutation_allowed(qc, /*req_active=*/1)) { | 
3215  |  |         /* If connection is torn down due to an error while blocking, stop. */  | 
3216  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);  | 
3217  | 0  |         return -1;  | 
3218  | 0  |     }  | 
3219  |  |  | 
3220  | 0  |     args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);  | 
3221  | 0  |     if (args->qs != NULL)  | 
3222  | 0  |         return 1; /* got a stream */  | 
3223  |  |  | 
3224  | 0  |     return 0; /* did not get a stream, keep trying */  | 
3225  | 0  | }  | 
3226  |  |  | 
3227  |  | QUIC_TAKES_LOCK  | 
3228  |  | SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)  | 
3229  | 196  | { | 
3230  | 196  |     QCTX ctx;  | 
3231  | 196  |     int ret;  | 
3232  | 196  |     SSL *new_s = NULL;  | 
3233  | 196  |     QUIC_STREAM_MAP *qsm;  | 
3234  | 196  |     QUIC_STREAM *qs;  | 
3235  | 196  |     QUIC_XSO *xso;  | 
3236  | 196  |     OSSL_RTT_INFO rtt_info;  | 
3237  |  |  | 
3238  | 196  |     if (!expect_quic_conn_only(s, &ctx))  | 
3239  | 0  |         return NULL;  | 
3240  |  |  | 
3241  | 196  |     quic_lock(ctx.qc);  | 
3242  |  |  | 
3243  | 196  |     if (qc_get_effective_incoming_stream_policy(ctx.qc)  | 
3244  | 196  |         == SSL_INCOMING_STREAM_POLICY_REJECT) { | 
3245  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);  | 
3246  | 0  |         goto out;  | 
3247  | 0  |     }  | 
3248  |  |  | 
3249  | 196  |     qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);  | 
3250  |  |  | 
3251  | 196  |     qs = ossl_quic_stream_map_peek_accept_queue(qsm);  | 
3252  | 196  |     if (qs == NULL) { | 
3253  | 0  |         if (qc_blocking_mode(ctx.qc)  | 
3254  | 0  |             && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) { | 
3255  | 0  |             struct wait_for_incoming_stream_args args;  | 
3256  |  | 
  | 
3257  | 0  |             args.ctx = &ctx;  | 
3258  | 0  |             args.qs = NULL;  | 
3259  |  | 
  | 
3260  | 0  |             ret = block_until_pred(ctx.qc, wait_for_incoming_stream, &args, 0);  | 
3261  | 0  |             if (ret == 0) { | 
3262  | 0  |                 QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
3263  | 0  |                 goto out;  | 
3264  | 0  |             } else if (ret < 0 || args.qs == NULL) { | 
3265  | 0  |                 goto out;  | 
3266  | 0  |             }  | 
3267  |  |  | 
3268  | 0  |             qs = args.qs;  | 
3269  | 0  |         } else { | 
3270  | 0  |             goto out;  | 
3271  | 0  |         }  | 
3272  | 0  |     }  | 
3273  |  |  | 
3274  | 196  |     xso = create_xso_from_stream(ctx.qc, qs);  | 
3275  | 196  |     if (xso == NULL)  | 
3276  | 0  |         goto out;  | 
3277  |  |  | 
3278  | 196  |     ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);  | 
3279  | 196  |     ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,  | 
3280  | 196  |                                                   rtt_info.smoothed_rtt);  | 
3281  | 196  |     new_s = &xso->ssl;  | 
3282  |  |  | 
3283  |  |     /* Calling this function inhibits default XSO autocreation. */  | 
3284  | 196  |     qc_touch_default_xso(ctx.qc); /* inhibits default XSO */  | 
3285  |  |  | 
3286  | 196  | out:  | 
3287  | 196  |     quic_unlock(ctx.qc);  | 
3288  | 196  |     return new_s;  | 
3289  | 196  | }  | 
3290  |  |  | 
3291  |  | /*  | 
3292  |  |  * SSL_get_accept_stream_queue_len  | 
3293  |  |  * -------------------------------  | 
3294  |  |  */  | 
3295  |  | QUIC_TAKES_LOCK  | 
3296  |  | size_t ossl_quic_get_accept_stream_queue_len(SSL *s)  | 
3297  | 4.41k  | { | 
3298  | 4.41k  |     QCTX ctx;  | 
3299  | 4.41k  |     size_t v;  | 
3300  |  |  | 
3301  | 4.41k  |     if (!expect_quic_conn_only(s, &ctx))  | 
3302  | 0  |         return 0;  | 
3303  |  |  | 
3304  | 4.41k  |     quic_lock(ctx.qc);  | 
3305  |  |  | 
3306  | 4.41k  |     v = ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));  | 
3307  |  |  | 
3308  | 4.41k  |     quic_unlock(ctx.qc);  | 
3309  | 4.41k  |     return v;  | 
3310  | 4.41k  | }  | 
3311  |  |  | 
3312  |  | /*  | 
3313  |  |  * SSL_stream_reset  | 
3314  |  |  * ----------------  | 
3315  |  |  */  | 
3316  |  | int ossl_quic_stream_reset(SSL *ssl,  | 
3317  |  |                            const SSL_STREAM_RESET_ARGS *args,  | 
3318  |  |                            size_t args_len)  | 
3319  | 0  | { | 
3320  | 0  |     QCTX ctx;  | 
3321  | 0  |     QUIC_STREAM_MAP *qsm;  | 
3322  | 0  |     QUIC_STREAM *qs;  | 
3323  | 0  |     uint64_t error_code;  | 
3324  | 0  |     int ok, err;  | 
3325  |  | 
  | 
3326  | 0  |     if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, /*io=*/0, &ctx))  | 
3327  | 0  |         return 0;  | 
3328  |  |  | 
3329  | 0  |     qsm         = ossl_quic_channel_get_qsm(ctx.qc->ch);  | 
3330  | 0  |     qs          = ctx.xso->stream;  | 
3331  | 0  |     error_code  = (args != NULL ? args->quic_error_code : 0);  | 
3332  |  | 
  | 
3333  | 0  |     if (!quic_validate_for_write(ctx.xso, &err)) { | 
3334  | 0  |         ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);  | 
3335  | 0  |         goto err;  | 
3336  | 0  |     }  | 
3337  |  |  | 
3338  | 0  |     ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);  | 
3339  |  | 
  | 
3340  | 0  | err:  | 
3341  | 0  |     quic_unlock(ctx.qc);  | 
3342  | 0  |     return ok;  | 
3343  | 0  | }  | 
3344  |  |  | 
3345  |  | /*  | 
3346  |  |  * SSL_get_stream_read_state  | 
3347  |  |  * -------------------------  | 
3348  |  |  */  | 
3349  |  | static void quic_classify_stream(QUIC_CONNECTION *qc,  | 
3350  |  |                                  QUIC_STREAM *qs,  | 
3351  |  |                                  int is_write,  | 
3352  |  |                                  int *state,  | 
3353  |  |                                  uint64_t *app_error_code)  | 
3354  | 0  | { | 
3355  | 0  |     int local_init;  | 
3356  | 0  |     uint64_t final_size;  | 
3357  |  | 
  | 
3358  | 0  |     local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);  | 
3359  |  | 
  | 
3360  | 0  |     if (app_error_code != NULL)  | 
3361  | 0  |         *app_error_code = UINT64_MAX;  | 
3362  | 0  |     else  | 
3363  | 0  |         app_error_code = &final_size; /* throw away value */  | 
3364  |  | 
  | 
3365  | 0  |     if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) { | 
3366  |  |         /*  | 
3367  |  |          * Unidirectional stream and this direction of transmission doesn't  | 
3368  |  |          * exist.  | 
3369  |  |          */  | 
3370  | 0  |         *state = SSL_STREAM_STATE_WRONG_DIR;  | 
3371  | 0  |     } else if (ossl_quic_channel_is_term_any(qc->ch)) { | 
3372  |  |         /* Connection already closed. */  | 
3373  | 0  |         *state = SSL_STREAM_STATE_CONN_CLOSED;  | 
3374  | 0  |     } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) { | 
3375  |  |         /* Application has read a FIN. */  | 
3376  | 0  |         *state = SSL_STREAM_STATE_FINISHED;  | 
3377  | 0  |     } else if ((!is_write && qs->stop_sending)  | 
3378  | 0  |                || (is_write && ossl_quic_stream_send_is_reset(qs))) { | 
3379  |  |         /*  | 
3380  |  |          * Stream has been reset locally. FIN takes precedence over this for the  | 
3381  |  |          * read case as the application need not care if the stream is reset  | 
3382  |  |          * after a FIN has been successfully processed.  | 
3383  |  |          */  | 
3384  | 0  |         *state          = SSL_STREAM_STATE_RESET_LOCAL;  | 
3385  | 0  |         *app_error_code = !is_write  | 
3386  | 0  |             ? qs->stop_sending_aec  | 
3387  | 0  |             : qs->reset_stream_aec;  | 
3388  | 0  |     } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))  | 
3389  | 0  |                || (is_write && qs->peer_stop_sending)) { | 
3390  |  |         /*  | 
3391  |  |          * Stream has been reset remotely. */  | 
3392  | 0  |         *state          = SSL_STREAM_STATE_RESET_REMOTE;  | 
3393  | 0  |         *app_error_code = !is_write  | 
3394  | 0  |             ? qs->peer_reset_stream_aec  | 
3395  | 0  |             : qs->peer_stop_sending_aec;  | 
3396  | 0  |     } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,  | 
3397  | 0  |                                                             &final_size)) { | 
3398  |  |         /*  | 
3399  |  |          * Stream has been finished. Stream reset takes precedence over this for  | 
3400  |  |          * the write case as peer may not have received all data.  | 
3401  |  |          */  | 
3402  | 0  |         *state = SSL_STREAM_STATE_FINISHED;  | 
3403  | 0  |     } else { | 
3404  |  |         /* Stream still healthy. */  | 
3405  | 0  |         *state = SSL_STREAM_STATE_OK;  | 
3406  | 0  |     }  | 
3407  | 0  | }  | 
3408  |  |  | 
3409  |  | static int quic_get_stream_state(SSL *ssl, int is_write)  | 
3410  | 0  | { | 
3411  | 0  |     QCTX ctx;  | 
3412  | 0  |     int state;  | 
3413  |  | 
  | 
3414  | 0  |     if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))  | 
3415  | 0  |         return SSL_STREAM_STATE_NONE;  | 
3416  |  |  | 
3417  | 0  |     quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);  | 
3418  | 0  |     quic_unlock(ctx.qc);  | 
3419  | 0  |     return state;  | 
3420  | 0  | }  | 
3421  |  |  | 
3422  |  | int ossl_quic_get_stream_read_state(SSL *ssl)  | 
3423  | 0  | { | 
3424  | 0  |     return quic_get_stream_state(ssl, /*is_write=*/0);  | 
3425  | 0  | }  | 
3426  |  |  | 
3427  |  | /*  | 
3428  |  |  * SSL_get_stream_write_state  | 
3429  |  |  * --------------------------  | 
3430  |  |  */  | 
3431  |  | int ossl_quic_get_stream_write_state(SSL *ssl)  | 
3432  | 0  | { | 
3433  | 0  |     return quic_get_stream_state(ssl, /*is_write=*/1);  | 
3434  | 0  | }  | 
3435  |  |  | 
3436  |  | /*  | 
3437  |  |  * SSL_get_stream_read_error_code  | 
3438  |  |  * ------------------------------  | 
3439  |  |  */  | 
3440  |  | static int quic_get_stream_error_code(SSL *ssl, int is_write,  | 
3441  |  |                                       uint64_t *app_error_code)  | 
3442  | 0  | { | 
3443  | 0  |     QCTX ctx;  | 
3444  | 0  |     int state;  | 
3445  |  | 
  | 
3446  | 0  |     if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))  | 
3447  | 0  |         return -1;  | 
3448  |  |  | 
3449  | 0  |     quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,  | 
3450  | 0  |                          &state, app_error_code);  | 
3451  |  | 
  | 
3452  | 0  |     quic_unlock(ctx.qc);  | 
3453  | 0  |     switch (state) { | 
3454  | 0  |         case SSL_STREAM_STATE_FINISHED:  | 
3455  | 0  |              return 0;  | 
3456  | 0  |         case SSL_STREAM_STATE_RESET_LOCAL:  | 
3457  | 0  |         case SSL_STREAM_STATE_RESET_REMOTE:  | 
3458  | 0  |              return 1;  | 
3459  | 0  |         default:  | 
3460  | 0  |              return -1;  | 
3461  | 0  |     }  | 
3462  | 0  | }  | 
3463  |  |  | 
3464  |  | int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)  | 
3465  | 0  | { | 
3466  | 0  |     return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);  | 
3467  | 0  | }  | 
3468  |  |  | 
3469  |  | /*  | 
3470  |  |  * SSL_get_stream_write_error_code  | 
3471  |  |  * -------------------------------  | 
3472  |  |  */  | 
3473  |  | int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)  | 
3474  | 0  | { | 
3475  | 0  |     return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);  | 
3476  | 0  | }  | 
3477  |  |  | 
3478  |  | /*  | 
3479  |  |  * Write buffer size mutation  | 
3480  |  |  * --------------------------  | 
3481  |  |  */  | 
3482  |  | int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)  | 
3483  | 0  | { | 
3484  | 0  |     int ret = 0;  | 
3485  | 0  |     QCTX ctx;  | 
3486  |  | 
  | 
3487  | 0  |     if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))  | 
3488  | 0  |         return 0;  | 
3489  |  |  | 
3490  | 0  |     if (!ossl_quic_stream_has_send(ctx.xso->stream)) { | 
3491  |  |         /* Called on a unidirectional receive-only stream - error. */  | 
3492  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);  | 
3493  | 0  |         goto out;  | 
3494  | 0  |     }  | 
3495  |  |  | 
3496  | 0  |     if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) { | 
3497  |  |         /*  | 
3498  |  |          * If the stream has a send part but we have disposed of it because we  | 
3499  |  |          * no longer need it, this is a no-op.  | 
3500  |  |          */  | 
3501  | 0  |         ret = 1;  | 
3502  | 0  |         goto out;  | 
3503  | 0  |     }  | 
3504  |  |  | 
3505  | 0  |     if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) { | 
3506  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);  | 
3507  | 0  |         goto out;  | 
3508  | 0  |     }  | 
3509  |  |  | 
3510  | 0  |     ret = 1;  | 
3511  |  | 
  | 
3512  | 0  | out:  | 
3513  | 0  |     quic_unlock(ctx.qc);  | 
3514  | 0  |     return ret;  | 
3515  | 0  | }  | 
3516  |  |  | 
3517  |  | /*  | 
3518  |  |  * SSL_get_conn_close_info  | 
3519  |  |  * -----------------------  | 
3520  |  |  */  | 
3521  |  | int ossl_quic_get_conn_close_info(SSL *ssl,  | 
3522  |  |                                   SSL_CONN_CLOSE_INFO *info,  | 
3523  |  |                                   size_t info_len)  | 
3524  | 0  | { | 
3525  | 0  |     QCTX ctx;  | 
3526  | 0  |     const QUIC_TERMINATE_CAUSE *tc;  | 
3527  |  | 
  | 
3528  | 0  |     if (!expect_quic_conn_only(ssl, &ctx))  | 
3529  | 0  |         return -1;  | 
3530  |  |  | 
3531  | 0  |     tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);  | 
3532  | 0  |     if (tc == NULL)  | 
3533  | 0  |         return 0;  | 
3534  |  |  | 
3535  | 0  |     info->error_code    = tc->error_code;  | 
3536  | 0  |     info->frame_type    = tc->frame_type;  | 
3537  | 0  |     info->reason        = tc->reason;  | 
3538  | 0  |     info->reason_len    = tc->reason_len;  | 
3539  | 0  |     info->flags         = 0;  | 
3540  | 0  |     if (!tc->remote)  | 
3541  | 0  |         info->flags |= SSL_CONN_CLOSE_FLAG_LOCAL;  | 
3542  | 0  |     if (!tc->app)  | 
3543  | 0  |         info->flags |= SSL_CONN_CLOSE_FLAG_TRANSPORT;  | 
3544  | 0  |     return 1;  | 
3545  | 0  | }  | 
3546  |  |  | 
3547  |  | /*  | 
3548  |  |  * SSL_key_update  | 
3549  |  |  * --------------  | 
3550  |  |  */  | 
3551  |  | int ossl_quic_key_update(SSL *ssl, int update_type)  | 
3552  | 0  | { | 
3553  | 0  |     QCTX ctx;  | 
3554  |  | 
  | 
3555  | 0  |     if (!expect_quic_conn_only(ssl, &ctx))  | 
3556  | 0  |         return 0;  | 
3557  |  |  | 
3558  | 0  |     switch (update_type) { | 
3559  | 0  |     case SSL_KEY_UPDATE_NOT_REQUESTED:  | 
3560  |  |         /*  | 
3561  |  |          * QUIC signals peer key update implicily by triggering a local  | 
3562  |  |          * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.  | 
3563  |  |          */  | 
3564  | 0  |     case SSL_KEY_UPDATE_REQUESTED:  | 
3565  | 0  |         break;  | 
3566  |  |  | 
3567  | 0  |     default:  | 
3568  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);  | 
3569  | 0  |         return 0;  | 
3570  | 0  |     }  | 
3571  |  |  | 
3572  | 0  |     quic_lock(ctx.qc);  | 
3573  |  |  | 
3574  |  |     /* Attempt to perform a TXKU. */  | 
3575  | 0  |     if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) { | 
3576  | 0  |         QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);  | 
3577  | 0  |         quic_unlock(ctx.qc);  | 
3578  | 0  |         return 0;  | 
3579  | 0  |     }  | 
3580  |  |  | 
3581  | 0  |     quic_unlock(ctx.qc);  | 
3582  | 0  |     return 1;  | 
3583  | 0  | }  | 
3584  |  |  | 
3585  |  | /*  | 
3586  |  |  * SSL_get_key_update_type  | 
3587  |  |  * -----------------------  | 
3588  |  |  */  | 
3589  |  | int ossl_quic_get_key_update_type(const SSL *s)  | 
3590  | 0  | { | 
3591  |  |     /*  | 
3592  |  |      * We always handle key updates immediately so a key update is never  | 
3593  |  |      * pending.  | 
3594  |  |      */  | 
3595  | 0  |     return SSL_KEY_UPDATE_NONE;  | 
3596  | 0  | }  | 
3597  |  |  | 
3598  |  | /*  | 
3599  |  |  * QUIC Front-End I/O API: SSL_CTX Management  | 
3600  |  |  * ==========================================  | 
3601  |  |  */  | 
3602  |  |  | 
3603  |  | long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)  | 
3604  | 10.9k  | { | 
3605  | 10.9k  |     switch (cmd) { | 
3606  | 10.9k  |     default:  | 
3607  | 10.9k  |         return ssl3_ctx_ctrl(ctx, cmd, larg, parg);  | 
3608  | 10.9k  |     }  | 
3609  | 10.9k  | }  | 
3610  |  |  | 
3611  |  | long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))  | 
3612  | 0  | { | 
3613  | 0  |     QCTX ctx;  | 
3614  |  | 
  | 
3615  | 0  |     if (!expect_quic_conn_only(s, &ctx))  | 
3616  | 0  |         return 0;  | 
3617  |  |  | 
3618  | 0  |     switch (cmd) { | 
3619  | 0  |     case SSL_CTRL_SET_MSG_CALLBACK:  | 
3620  | 0  |         ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,  | 
3621  | 0  |                                            &ctx.qc->ssl);  | 
3622  |  |         /* This callback also needs to be set on the internal SSL object */  | 
3623  | 0  |         return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;  | 
3624  |  | 
  | 
3625  | 0  |     default:  | 
3626  |  |         /* Probably a TLS related ctrl. Defer to our internal SSL object */  | 
3627  | 0  |         return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);  | 
3628  | 0  |     }  | 
3629  | 0  | }  | 
3630  |  |  | 
3631  |  | long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))  | 
3632  | 0  | { | 
3633  | 0  |     return ssl3_ctx_callback_ctrl(ctx, cmd, fp);  | 
3634  | 0  | }  | 
3635  |  |  | 
3636  |  | int ossl_quic_renegotiate_check(SSL *ssl, int initok)  | 
3637  | 0  | { | 
3638  |  |     /* We never do renegotiation. */  | 
3639  | 0  |     return 0;  | 
3640  | 0  | }  | 
3641  |  |  | 
3642  |  | const SSL_CIPHER *ossl_quic_get_cipher_by_char(const unsigned char *p)  | 
3643  | 0  | { | 
3644  | 0  |     const SSL_CIPHER *ciph = ssl3_get_cipher_by_char(p);  | 
3645  |  | 
  | 
3646  | 0  |     if ((ciph->algorithm2 & SSL_QUIC) == 0)  | 
3647  | 0  |         return NULL;  | 
3648  |  |  | 
3649  | 0  |     return ciph;  | 
3650  | 0  | }  | 
3651  |  |  | 
3652  |  | /*  | 
3653  |  |  * These functions define the TLSv1.2 (and below) ciphers that are supported by  | 
3654  |  |  * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.  | 
3655  |  |  */  | 
3656  |  |  | 
3657  |  | int ossl_quic_num_ciphers(void)  | 
3658  | 22.1k  | { | 
3659  | 22.1k  |     return 0;  | 
3660  | 22.1k  | }  | 
3661  |  |  | 
3662  |  | const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)  | 
3663  | 0  | { | 
3664  | 0  |     return NULL;  | 
3665  | 0  | }  | 
3666  |  |  | 
3667  |  | /*  | 
3668  |  |  * SSL_get_shutdown()  | 
3669  |  |  * ------------------  | 
3670  |  |  */  | 
3671  |  | int ossl_quic_get_shutdown(const SSL *s)  | 
3672  | 0  | { | 
3673  | 0  |     QCTX ctx;  | 
3674  | 0  |     int shut = 0;  | 
3675  |  | 
  | 
3676  | 0  |     if (!expect_quic_conn_only(s, &ctx))  | 
3677  | 0  |         return 0;  | 
3678  |  |  | 
3679  | 0  |     if (ossl_quic_channel_is_term_any(ctx.qc->ch)) { | 
3680  | 0  |         shut |= SSL_SENT_SHUTDOWN;  | 
3681  | 0  |         if (!ossl_quic_channel_is_closing(ctx.qc->ch))  | 
3682  | 0  |             shut |= SSL_RECEIVED_SHUTDOWN;  | 
3683  | 0  |     }  | 
3684  |  | 
  | 
3685  | 0  |     return shut;  | 
3686  | 0  | }  | 
3687  |  |  | 
3688  |  | /*  | 
3689  |  |  * Internal Testing APIs  | 
3690  |  |  * =====================  | 
3691  |  |  */  | 
3692  |  |  | 
3693  |  | QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)  | 
3694  | 0  | { | 
3695  | 0  |     QCTX ctx;  | 
3696  |  | 
  | 
3697  | 0  |     if (!expect_quic_conn_only(s, &ctx))  | 
3698  | 0  |         return NULL;  | 
3699  |  |  | 
3700  | 0  |     return ctx.qc->ch;  | 
3701  | 0  | }  |