1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery *
4*e7be843bSPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e7be843bSPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*e7be843bSPierre Pronchery * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery */
9*e7be843bSPierre Pronchery
10*e7be843bSPierre Pronchery #include <openssl/macros.h>
11*e7be843bSPierre Pronchery #include <openssl/objects.h>
12*e7be843bSPierre Pronchery #include <openssl/sslerr.h>
13*e7be843bSPierre Pronchery #include <crypto/rand.h>
14*e7be843bSPierre Pronchery #include "quic_local.h"
15*e7be843bSPierre Pronchery #include "internal/hashfunc.h"
16*e7be843bSPierre Pronchery #include "internal/ssl_unwrap.h"
17*e7be843bSPierre Pronchery #include "internal/quic_tls.h"
18*e7be843bSPierre Pronchery #include "internal/quic_rx_depack.h"
19*e7be843bSPierre Pronchery #include "internal/quic_error.h"
20*e7be843bSPierre Pronchery #include "internal/quic_engine.h"
21*e7be843bSPierre Pronchery #include "internal/quic_port.h"
22*e7be843bSPierre Pronchery #include "internal/quic_reactor_wait_ctx.h"
23*e7be843bSPierre Pronchery #include "internal/time.h"
24*e7be843bSPierre Pronchery
25*e7be843bSPierre Pronchery typedef struct qctx_st QCTX;
26*e7be843bSPierre Pronchery
27*e7be843bSPierre Pronchery static void qc_cleanup(QUIC_CONNECTION *qc, int have_lock);
28*e7be843bSPierre Pronchery static void aon_write_finish(QUIC_XSO *xso);
29*e7be843bSPierre Pronchery static int create_channel(QUIC_CONNECTION *qc, SSL_CTX *ctx);
30*e7be843bSPierre Pronchery static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs);
31*e7be843bSPierre Pronchery static QUIC_CONNECTION *create_qc_from_incoming_conn(QUIC_LISTENER *ql, QUIC_CHANNEL *ch);
32*e7be843bSPierre Pronchery static int qc_try_create_default_xso_for_write(QCTX *ctx);
33*e7be843bSPierre Pronchery static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek);
34*e7be843bSPierre Pronchery static void qctx_lock(QCTX *qctx);
35*e7be843bSPierre Pronchery static void qctx_unlock(QCTX *qctx);
36*e7be843bSPierre Pronchery static void qctx_lock_for_io(QCTX *ctx);
37*e7be843bSPierre Pronchery static int quic_do_handshake(QCTX *ctx);
38*e7be843bSPierre Pronchery static void qc_update_reject_policy(QUIC_CONNECTION *qc);
39*e7be843bSPierre Pronchery static void qc_touch_default_xso(QUIC_CONNECTION *qc);
40*e7be843bSPierre Pronchery static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch);
41*e7be843bSPierre Pronchery static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
42*e7be843bSPierre Pronchery int touch, QUIC_XSO **old_xso);
43*e7be843bSPierre Pronchery static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock);
44*e7be843bSPierre Pronchery static int quic_validate_for_write(QUIC_XSO *xso, int *err);
45*e7be843bSPierre Pronchery static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active);
46*e7be843bSPierre Pronchery static void qctx_maybe_autotick(QCTX *ctx);
47*e7be843bSPierre Pronchery static int qctx_should_autotick(QCTX *ctx);
48*e7be843bSPierre Pronchery
49*e7be843bSPierre Pronchery /*
50*e7be843bSPierre Pronchery * QCTX is a utility structure which provides information we commonly wish to
51*e7be843bSPierre Pronchery * unwrap upon an API call being dispatched to us, namely:
52*e7be843bSPierre Pronchery *
53*e7be843bSPierre Pronchery * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO
54*e7be843bSPierre Pronchery * was passed);
55*e7be843bSPierre Pronchery * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if
56*e7be843bSPierre Pronchery * a QCSO with a default stream was passed);
57*e7be843bSPierre Pronchery * - whether a QSSO was passed (xso == NULL must not be used to determine this
58*e7be843bSPierre Pronchery * because it may be non-NULL when a QCSO is passed if that QCSO has a
59*e7be843bSPierre Pronchery * default stream);
60*e7be843bSPierre Pronchery * - a pointer to a QUIC_LISTENER object, if one is relevant;
61*e7be843bSPierre Pronchery * - whether we are in "I/O context", meaning that non-normal errors can
62*e7be843bSPierre Pronchery * be reported via SSL_get_error() as well as via ERR. Functions such as
63*e7be843bSPierre Pronchery * SSL_read(), SSL_write() and SSL_do_handshake() are "I/O context"
64*e7be843bSPierre Pronchery * functions which are allowed to change the value returned by
65*e7be843bSPierre Pronchery * SSL_get_error. However, other functions (including functions which call
66*e7be843bSPierre Pronchery * SSL_do_handshake() implicitly) are not allowed to change the return value
67*e7be843bSPierre Pronchery * of SSL_get_error.
68*e7be843bSPierre Pronchery */
69*e7be843bSPierre Pronchery struct qctx_st {
70*e7be843bSPierre Pronchery QUIC_OBJ *obj;
71*e7be843bSPierre Pronchery QUIC_DOMAIN *qd;
72*e7be843bSPierre Pronchery QUIC_LISTENER *ql;
73*e7be843bSPierre Pronchery QUIC_CONNECTION *qc;
74*e7be843bSPierre Pronchery QUIC_XSO *xso;
75*e7be843bSPierre Pronchery int is_stream, is_listener, is_domain, in_io;
76*e7be843bSPierre Pronchery };
77*e7be843bSPierre Pronchery
78*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_set_last_error(QCTX * ctx,int last_error)79*e7be843bSPierre Pronchery static void quic_set_last_error(QCTX *ctx, int last_error)
80*e7be843bSPierre Pronchery {
81*e7be843bSPierre Pronchery if (!ctx->in_io)
82*e7be843bSPierre Pronchery return;
83*e7be843bSPierre Pronchery
84*e7be843bSPierre Pronchery if (ctx->is_stream && ctx->xso != NULL)
85*e7be843bSPierre Pronchery ctx->xso->last_error = last_error;
86*e7be843bSPierre Pronchery else if (!ctx->is_stream && ctx->qc != NULL)
87*e7be843bSPierre Pronchery ctx->qc->last_error = last_error;
88*e7be843bSPierre Pronchery }
89*e7be843bSPierre Pronchery
90*e7be843bSPierre Pronchery /*
91*e7be843bSPierre Pronchery * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
92*e7be843bSPierre Pronchery * rather than via ERR. Note that normal errors must always be raised while
93*e7be843bSPierre Pronchery * holding a lock.
94*e7be843bSPierre Pronchery */
95*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_raise_normal_error(QCTX * ctx,int err)96*e7be843bSPierre Pronchery static int quic_raise_normal_error(QCTX *ctx,
97*e7be843bSPierre Pronchery int err)
98*e7be843bSPierre Pronchery {
99*e7be843bSPierre Pronchery assert(ctx->in_io);
100*e7be843bSPierre Pronchery quic_set_last_error(ctx, err);
101*e7be843bSPierre Pronchery
102*e7be843bSPierre Pronchery return 0;
103*e7be843bSPierre Pronchery }
104*e7be843bSPierre Pronchery
105*e7be843bSPierre Pronchery /*
106*e7be843bSPierre Pronchery * Raise a 'non-normal' error, meaning any error that is not reported via
107*e7be843bSPierre Pronchery * SSL_get_error() and must be reported via ERR.
108*e7be843bSPierre Pronchery *
109*e7be843bSPierre Pronchery * qc should be provided if available. In exceptional circumstances when qc is
110*e7be843bSPierre Pronchery * not known NULL may be passed. This should generally only happen when an
111*e7be843bSPierre Pronchery * expect_...() function defined below fails, which generally indicates a
112*e7be843bSPierre Pronchery * dispatch error or caller error.
113*e7be843bSPierre Pronchery *
114*e7be843bSPierre Pronchery * ctx should be NULL if the connection lock is not held.
115*e7be843bSPierre Pronchery */
quic_raise_non_normal_error(QCTX * ctx,const char * file,int line,const char * func,int reason,const char * fmt,...)116*e7be843bSPierre Pronchery static int quic_raise_non_normal_error(QCTX *ctx,
117*e7be843bSPierre Pronchery const char *file,
118*e7be843bSPierre Pronchery int line,
119*e7be843bSPierre Pronchery const char *func,
120*e7be843bSPierre Pronchery int reason,
121*e7be843bSPierre Pronchery const char *fmt,
122*e7be843bSPierre Pronchery ...)
123*e7be843bSPierre Pronchery {
124*e7be843bSPierre Pronchery va_list args;
125*e7be843bSPierre Pronchery
126*e7be843bSPierre Pronchery if (ctx != NULL) {
127*e7be843bSPierre Pronchery quic_set_last_error(ctx, SSL_ERROR_SSL);
128*e7be843bSPierre Pronchery
129*e7be843bSPierre Pronchery if (reason == SSL_R_PROTOCOL_IS_SHUTDOWN && ctx->qc != NULL)
130*e7be843bSPierre Pronchery ossl_quic_channel_restore_err_state(ctx->qc->ch);
131*e7be843bSPierre Pronchery }
132*e7be843bSPierre Pronchery
133*e7be843bSPierre Pronchery ERR_new();
134*e7be843bSPierre Pronchery ERR_set_debug(file, line, func);
135*e7be843bSPierre Pronchery
136*e7be843bSPierre Pronchery va_start(args, fmt);
137*e7be843bSPierre Pronchery ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
138*e7be843bSPierre Pronchery va_end(args);
139*e7be843bSPierre Pronchery
140*e7be843bSPierre Pronchery return 0;
141*e7be843bSPierre Pronchery }
142*e7be843bSPierre Pronchery
143*e7be843bSPierre Pronchery #define QUIC_RAISE_NORMAL_ERROR(ctx, err) \
144*e7be843bSPierre Pronchery quic_raise_normal_error((ctx), (err))
145*e7be843bSPierre Pronchery
146*e7be843bSPierre Pronchery #define QUIC_RAISE_NON_NORMAL_ERROR(ctx, reason, msg) \
147*e7be843bSPierre Pronchery quic_raise_non_normal_error((ctx), \
148*e7be843bSPierre Pronchery OPENSSL_FILE, OPENSSL_LINE, \
149*e7be843bSPierre Pronchery OPENSSL_FUNC, \
150*e7be843bSPierre Pronchery (reason), \
151*e7be843bSPierre Pronchery (msg))
152*e7be843bSPierre Pronchery /*
153*e7be843bSPierre Pronchery * Flags for expect_quic_as:
154*e7be843bSPierre Pronchery *
155*e7be843bSPierre Pronchery * QCTX_C
156*e7be843bSPierre Pronchery * The input SSL object may be a QCSO.
157*e7be843bSPierre Pronchery *
158*e7be843bSPierre Pronchery * QCTX_S
159*e7be843bSPierre Pronchery * The input SSL object may be a QSSO or a QCSO with a default stream
160*e7be843bSPierre Pronchery * attached.
161*e7be843bSPierre Pronchery *
162*e7be843bSPierre Pronchery * (Note this means there is no current way to require an SSL object with a
163*e7be843bSPierre Pronchery * QUIC stream which is not a QCSO; a QCSO with a default stream attached
164*e7be843bSPierre Pronchery * is always considered to satisfy QCTX_S.)
165*e7be843bSPierre Pronchery *
166*e7be843bSPierre Pronchery * QCTX_AUTO_S
167*e7be843bSPierre Pronchery * The input SSL object may be a QSSO or a QCSO with a default stream
168*e7be843bSPierre Pronchery * attached. If no default stream is currently attached to a QCSO,
169*e7be843bSPierre Pronchery * one may be auto-created if possible.
170*e7be843bSPierre Pronchery *
171*e7be843bSPierre Pronchery * If QCTX_REMOTE_INIT is set, an auto-created default XSO is
172*e7be843bSPierre Pronchery * initiated by the remote party (i.e., local party reads first).
173*e7be843bSPierre Pronchery *
174*e7be843bSPierre Pronchery * If it is not set, an auto-created default XSO is
175*e7be843bSPierre Pronchery * initiated by the local party (i.e., local party writes first).
176*e7be843bSPierre Pronchery *
177*e7be843bSPierre Pronchery * QCTX_L
178*e7be843bSPierre Pronchery * The input SSL object may be a QLSO.
179*e7be843bSPierre Pronchery *
180*e7be843bSPierre Pronchery * QCTX_LOCK
181*e7be843bSPierre Pronchery * If and only if the function returns successfully, the ctx
182*e7be843bSPierre Pronchery * is guaranteed to be locked.
183*e7be843bSPierre Pronchery *
184*e7be843bSPierre Pronchery * QCTX_IO
185*e7be843bSPierre Pronchery * Begin an I/O context. If not set, begins a non-I/O context.
186*e7be843bSPierre Pronchery * This determines whether SSL_get_error() is updated; the value it returns
187*e7be843bSPierre Pronchery * is modified only by an I/O call.
188*e7be843bSPierre Pronchery *
189*e7be843bSPierre Pronchery * QCTX_NO_ERROR
190*e7be843bSPierre Pronchery * Don't raise an error if the object type is wrong. Should not be used in
191*e7be843bSPierre Pronchery * conjunction with any flags that may raise errors not related to a wrong
192*e7be843bSPierre Pronchery * object type.
193*e7be843bSPierre Pronchery */
194*e7be843bSPierre Pronchery #define QCTX_C (1U << 0)
195*e7be843bSPierre Pronchery #define QCTX_S (1U << 1)
196*e7be843bSPierre Pronchery #define QCTX_L (1U << 2)
197*e7be843bSPierre Pronchery #define QCTX_AUTO_S (1U << 3)
198*e7be843bSPierre Pronchery #define QCTX_REMOTE_INIT (1U << 4)
199*e7be843bSPierre Pronchery #define QCTX_LOCK (1U << 5)
200*e7be843bSPierre Pronchery #define QCTX_IO (1U << 6)
201*e7be843bSPierre Pronchery #define QCTX_D (1U << 7)
202*e7be843bSPierre Pronchery #define QCTX_NO_ERROR (1U << 8)
203*e7be843bSPierre Pronchery
204*e7be843bSPierre Pronchery /*
205*e7be843bSPierre Pronchery * Called when expect_quic failed. Used to diagnose why such a call failed and
206*e7be843bSPierre Pronchery * raise a reasonable error code based on the configured preconditions in flags.
207*e7be843bSPierre Pronchery */
wrong_type(const SSL * s,uint32_t flags)208*e7be843bSPierre Pronchery static int wrong_type(const SSL *s, uint32_t flags)
209*e7be843bSPierre Pronchery {
210*e7be843bSPierre Pronchery const uint32_t mask = QCTX_C | QCTX_S | QCTX_L | QCTX_D;
211*e7be843bSPierre Pronchery int code = ERR_R_UNSUPPORTED;
212*e7be843bSPierre Pronchery
213*e7be843bSPierre Pronchery if ((flags & QCTX_NO_ERROR) != 0)
214*e7be843bSPierre Pronchery return 1;
215*e7be843bSPierre Pronchery else if ((flags & mask) == QCTX_D)
216*e7be843bSPierre Pronchery code = SSL_R_DOMAIN_USE_ONLY;
217*e7be843bSPierre Pronchery else if ((flags & mask) == QCTX_L)
218*e7be843bSPierre Pronchery code = SSL_R_LISTENER_USE_ONLY;
219*e7be843bSPierre Pronchery else if ((flags & mask) == QCTX_C)
220*e7be843bSPierre Pronchery code = SSL_R_CONN_USE_ONLY;
221*e7be843bSPierre Pronchery else if ((flags & mask) == QCTX_S
222*e7be843bSPierre Pronchery || (flags & mask) == (QCTX_C | QCTX_S))
223*e7be843bSPierre Pronchery code = SSL_R_NO_STREAM;
224*e7be843bSPierre Pronchery
225*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(NULL, code, NULL);
226*e7be843bSPierre Pronchery }
227*e7be843bSPierre Pronchery
228*e7be843bSPierre Pronchery /*
229*e7be843bSPierre Pronchery * Given a QDSO, QCSO, QSSO or QLSO, initialises a QCTX, determining the
230*e7be843bSPierre Pronchery * contextually applicable QUIC_LISTENER, QUIC_CONNECTION and QUIC_XSO
231*e7be843bSPierre Pronchery * pointers.
232*e7be843bSPierre Pronchery *
233*e7be843bSPierre Pronchery * After this returns 1, all fields of the passed QCTX are initialised.
234*e7be843bSPierre Pronchery * Returns 0 on failure. This function is intended to be used to provide API
235*e7be843bSPierre Pronchery * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure
236*e7be843bSPierre Pronchery * unless the QCTX_NO_ERROR flag is set.
237*e7be843bSPierre Pronchery *
238*e7be843bSPierre Pronchery * The flags argument controls the preconditions and postconditions of this
239*e7be843bSPierre Pronchery * function. See above for the different flags.
240*e7be843bSPierre Pronchery *
241*e7be843bSPierre Pronchery * The fields of a QCTX are initialised as follows depending on the identity of
242*e7be843bSPierre Pronchery * the SSL object, and assuming the preconditions demanded by the flags field as
243*e7be843bSPierre Pronchery * described above are met:
244*e7be843bSPierre Pronchery *
245*e7be843bSPierre Pronchery * QDSO QLSO QCSO QSSO
246*e7be843bSPierre Pronchery * qd non-NULL maybe maybe maybe
247*e7be843bSPierre Pronchery * ql NULL non-NULL maybe maybe
248*e7be843bSPierre Pronchery * qc NULL NULL non-NULL non-NULL
249*e7be843bSPierre Pronchery * xso NULL NULL maybe non-NULL
250*e7be843bSPierre Pronchery * is_stream 0 0 0 1
251*e7be843bSPierre Pronchery * is_listener 0 1 0 0
252*e7be843bSPierre Pronchery * is_domain 1 0 0 0
253*e7be843bSPierre Pronchery *
254*e7be843bSPierre Pronchery */
expect_quic_as(const SSL * s,QCTX * ctx,uint32_t flags)255*e7be843bSPierre Pronchery static int expect_quic_as(const SSL *s, QCTX *ctx, uint32_t flags)
256*e7be843bSPierre Pronchery {
257*e7be843bSPierre Pronchery int ok = 0, locked = 0, lock_requested = ((flags & QCTX_LOCK) != 0);
258*e7be843bSPierre Pronchery QUIC_DOMAIN *qd;
259*e7be843bSPierre Pronchery QUIC_LISTENER *ql;
260*e7be843bSPierre Pronchery QUIC_CONNECTION *qc;
261*e7be843bSPierre Pronchery QUIC_XSO *xso;
262*e7be843bSPierre Pronchery
263*e7be843bSPierre Pronchery if ((flags & QCTX_AUTO_S) != 0)
264*e7be843bSPierre Pronchery flags |= QCTX_S;
265*e7be843bSPierre Pronchery
266*e7be843bSPierre Pronchery ctx->obj = NULL;
267*e7be843bSPierre Pronchery ctx->qd = NULL;
268*e7be843bSPierre Pronchery ctx->ql = NULL;
269*e7be843bSPierre Pronchery ctx->qc = NULL;
270*e7be843bSPierre Pronchery ctx->xso = NULL;
271*e7be843bSPierre Pronchery ctx->is_stream = 0;
272*e7be843bSPierre Pronchery ctx->is_listener = 0;
273*e7be843bSPierre Pronchery ctx->is_domain = 0;
274*e7be843bSPierre Pronchery ctx->in_io = ((flags & QCTX_IO) != 0);
275*e7be843bSPierre Pronchery
276*e7be843bSPierre Pronchery if (s == NULL) {
277*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_PASSED_NULL_PARAMETER, NULL);
278*e7be843bSPierre Pronchery goto err;
279*e7be843bSPierre Pronchery }
280*e7be843bSPierre Pronchery
281*e7be843bSPierre Pronchery switch (s->type) {
282*e7be843bSPierre Pronchery case SSL_TYPE_QUIC_DOMAIN:
283*e7be843bSPierre Pronchery if ((flags & QCTX_D) == 0) {
284*e7be843bSPierre Pronchery wrong_type(s, flags);
285*e7be843bSPierre Pronchery goto err;
286*e7be843bSPierre Pronchery }
287*e7be843bSPierre Pronchery
288*e7be843bSPierre Pronchery qd = (QUIC_DOMAIN *)s;
289*e7be843bSPierre Pronchery ctx->obj = &qd->obj;
290*e7be843bSPierre Pronchery ctx->qd = qd;
291*e7be843bSPierre Pronchery ctx->is_domain = 1;
292*e7be843bSPierre Pronchery break;
293*e7be843bSPierre Pronchery
294*e7be843bSPierre Pronchery case SSL_TYPE_QUIC_LISTENER:
295*e7be843bSPierre Pronchery if ((flags & QCTX_L) == 0) {
296*e7be843bSPierre Pronchery wrong_type(s, flags);
297*e7be843bSPierre Pronchery goto err;
298*e7be843bSPierre Pronchery }
299*e7be843bSPierre Pronchery
300*e7be843bSPierre Pronchery ql = (QUIC_LISTENER *)s;
301*e7be843bSPierre Pronchery ctx->obj = &ql->obj;
302*e7be843bSPierre Pronchery ctx->qd = ql->domain;
303*e7be843bSPierre Pronchery ctx->ql = ql;
304*e7be843bSPierre Pronchery ctx->is_listener = 1;
305*e7be843bSPierre Pronchery break;
306*e7be843bSPierre Pronchery
307*e7be843bSPierre Pronchery case SSL_TYPE_QUIC_CONNECTION:
308*e7be843bSPierre Pronchery qc = (QUIC_CONNECTION *)s;
309*e7be843bSPierre Pronchery ctx->obj = &qc->obj;
310*e7be843bSPierre Pronchery ctx->qd = qc->domain;
311*e7be843bSPierre Pronchery ctx->ql = qc->listener; /* never changes, so can be read without lock */
312*e7be843bSPierre Pronchery ctx->qc = qc;
313*e7be843bSPierre Pronchery
314*e7be843bSPierre Pronchery if ((flags & QCTX_AUTO_S) != 0) {
315*e7be843bSPierre Pronchery if ((flags & QCTX_IO) != 0)
316*e7be843bSPierre Pronchery qctx_lock_for_io(ctx);
317*e7be843bSPierre Pronchery else
318*e7be843bSPierre Pronchery qctx_lock(ctx);
319*e7be843bSPierre Pronchery
320*e7be843bSPierre Pronchery locked = 1;
321*e7be843bSPierre Pronchery }
322*e7be843bSPierre Pronchery
323*e7be843bSPierre Pronchery if ((flags & QCTX_AUTO_S) != 0 && qc->default_xso == NULL) {
324*e7be843bSPierre Pronchery if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
325*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
326*e7be843bSPierre Pronchery goto err;
327*e7be843bSPierre Pronchery }
328*e7be843bSPierre Pronchery
329*e7be843bSPierre Pronchery /* If we haven't finished the handshake, try to advance it. */
330*e7be843bSPierre Pronchery if (quic_do_handshake(ctx) < 1)
331*e7be843bSPierre Pronchery /* ossl_quic_do_handshake raised error here */
332*e7be843bSPierre Pronchery goto err;
333*e7be843bSPierre Pronchery
334*e7be843bSPierre Pronchery if ((flags & QCTX_REMOTE_INIT) != 0) {
335*e7be843bSPierre Pronchery if (!qc_wait_for_default_xso_for_read(ctx, /*peek=*/0))
336*e7be843bSPierre Pronchery goto err;
337*e7be843bSPierre Pronchery } else {
338*e7be843bSPierre Pronchery if (!qc_try_create_default_xso_for_write(ctx))
339*e7be843bSPierre Pronchery goto err;
340*e7be843bSPierre Pronchery }
341*e7be843bSPierre Pronchery }
342*e7be843bSPierre Pronchery
343*e7be843bSPierre Pronchery if ((flags & QCTX_C) == 0
344*e7be843bSPierre Pronchery && (qc->default_xso == NULL || (flags & QCTX_S) == 0)) {
345*e7be843bSPierre Pronchery wrong_type(s, flags);
346*e7be843bSPierre Pronchery goto err;
347*e7be843bSPierre Pronchery }
348*e7be843bSPierre Pronchery
349*e7be843bSPierre Pronchery ctx->xso = qc->default_xso;
350*e7be843bSPierre Pronchery break;
351*e7be843bSPierre Pronchery
352*e7be843bSPierre Pronchery case SSL_TYPE_QUIC_XSO:
353*e7be843bSPierre Pronchery if ((flags & QCTX_S) == 0) {
354*e7be843bSPierre Pronchery wrong_type(s, flags);
355*e7be843bSPierre Pronchery goto err;
356*e7be843bSPierre Pronchery }
357*e7be843bSPierre Pronchery
358*e7be843bSPierre Pronchery xso = (QUIC_XSO *)s;
359*e7be843bSPierre Pronchery ctx->obj = &xso->obj;
360*e7be843bSPierre Pronchery ctx->qd = xso->conn->domain;
361*e7be843bSPierre Pronchery ctx->ql = xso->conn->listener;
362*e7be843bSPierre Pronchery ctx->qc = xso->conn;
363*e7be843bSPierre Pronchery ctx->xso = xso;
364*e7be843bSPierre Pronchery ctx->is_stream = 1;
365*e7be843bSPierre Pronchery break;
366*e7be843bSPierre Pronchery
367*e7be843bSPierre Pronchery default:
368*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
369*e7be843bSPierre Pronchery goto err;
370*e7be843bSPierre Pronchery }
371*e7be843bSPierre Pronchery
372*e7be843bSPierre Pronchery if (lock_requested && !locked) {
373*e7be843bSPierre Pronchery if ((flags & QCTX_IO) != 0)
374*e7be843bSPierre Pronchery qctx_lock_for_io(ctx);
375*e7be843bSPierre Pronchery else
376*e7be843bSPierre Pronchery qctx_lock(ctx);
377*e7be843bSPierre Pronchery
378*e7be843bSPierre Pronchery locked = 1;
379*e7be843bSPierre Pronchery }
380*e7be843bSPierre Pronchery
381*e7be843bSPierre Pronchery ok = 1;
382*e7be843bSPierre Pronchery err:
383*e7be843bSPierre Pronchery if (locked && (!ok || !lock_requested))
384*e7be843bSPierre Pronchery qctx_unlock(ctx);
385*e7be843bSPierre Pronchery
386*e7be843bSPierre Pronchery return ok;
387*e7be843bSPierre Pronchery }
388*e7be843bSPierre Pronchery
is_quic_c(const SSL * s,QCTX * ctx,int raiseerrs)389*e7be843bSPierre Pronchery static int is_quic_c(const SSL *s, QCTX *ctx, int raiseerrs)
390*e7be843bSPierre Pronchery {
391*e7be843bSPierre Pronchery uint32_t flags = QCTX_C;
392*e7be843bSPierre Pronchery
393*e7be843bSPierre Pronchery if (!raiseerrs)
394*e7be843bSPierre Pronchery flags |= QCTX_NO_ERROR;
395*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, flags);
396*e7be843bSPierre Pronchery }
397*e7be843bSPierre Pronchery
398*e7be843bSPierre Pronchery /* Same as expect_quic_cs except that errors are not raised if raiseerrs == 0 */
is_quic_cs(const SSL * s,QCTX * ctx,int raiseerrs)399*e7be843bSPierre Pronchery static int is_quic_cs(const SSL *s, QCTX *ctx, int raiseerrs)
400*e7be843bSPierre Pronchery {
401*e7be843bSPierre Pronchery uint32_t flags = QCTX_C | QCTX_S;
402*e7be843bSPierre Pronchery
403*e7be843bSPierre Pronchery if (!raiseerrs)
404*e7be843bSPierre Pronchery flags |= QCTX_NO_ERROR;
405*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, flags);
406*e7be843bSPierre Pronchery }
407*e7be843bSPierre Pronchery
expect_quic_cs(const SSL * s,QCTX * ctx)408*e7be843bSPierre Pronchery static int expect_quic_cs(const SSL *s, QCTX *ctx)
409*e7be843bSPierre Pronchery {
410*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, QCTX_C | QCTX_S);
411*e7be843bSPierre Pronchery }
412*e7be843bSPierre Pronchery
expect_quic_csl(const SSL * s,QCTX * ctx)413*e7be843bSPierre Pronchery static int expect_quic_csl(const SSL *s, QCTX *ctx)
414*e7be843bSPierre Pronchery {
415*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, QCTX_C | QCTX_S | QCTX_L);
416*e7be843bSPierre Pronchery }
417*e7be843bSPierre Pronchery
expect_quic_csld(const SSL * s,QCTX * ctx)418*e7be843bSPierre Pronchery static int expect_quic_csld(const SSL *s, QCTX *ctx)
419*e7be843bSPierre Pronchery {
420*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, QCTX_C | QCTX_S | QCTX_L | QCTX_D);
421*e7be843bSPierre Pronchery }
422*e7be843bSPierre Pronchery
423*e7be843bSPierre Pronchery #define expect_quic_any expect_quic_csld
424*e7be843bSPierre Pronchery
expect_quic_listener(const SSL * s,QCTX * ctx)425*e7be843bSPierre Pronchery static int expect_quic_listener(const SSL *s, QCTX *ctx)
426*e7be843bSPierre Pronchery {
427*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, QCTX_L);
428*e7be843bSPierre Pronchery }
429*e7be843bSPierre Pronchery
expect_quic_domain(const SSL * s,QCTX * ctx)430*e7be843bSPierre Pronchery static int expect_quic_domain(const SSL *s, QCTX *ctx)
431*e7be843bSPierre Pronchery {
432*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, QCTX_D);
433*e7be843bSPierre Pronchery }
434*e7be843bSPierre Pronchery
435*e7be843bSPierre Pronchery /*
436*e7be843bSPierre Pronchery * Like expect_quic_cs(), but requires a QUIC_XSO be contextually available. In
437*e7be843bSPierre Pronchery * other words, requires that the passed QSO be a QSSO or a QCSO with a default
438*e7be843bSPierre Pronchery * stream.
439*e7be843bSPierre Pronchery *
440*e7be843bSPierre Pronchery * remote_init determines if we expect the default XSO to be remotely created or
441*e7be843bSPierre Pronchery * not. If it is -1, do not instantiate a default XSO if one does not yet exist.
442*e7be843bSPierre Pronchery *
443*e7be843bSPierre Pronchery * Channel mutex is acquired and retained on success.
444*e7be843bSPierre Pronchery */
445*e7be843bSPierre Pronchery QUIC_ACQUIRES_LOCK
expect_quic_with_stream_lock(const SSL * s,int remote_init,int in_io,QCTX * ctx)446*e7be843bSPierre Pronchery static int ossl_unused expect_quic_with_stream_lock(const SSL *s, int remote_init,
447*e7be843bSPierre Pronchery int in_io, QCTX *ctx)
448*e7be843bSPierre Pronchery {
449*e7be843bSPierre Pronchery uint32_t flags = QCTX_S | QCTX_LOCK;
450*e7be843bSPierre Pronchery
451*e7be843bSPierre Pronchery if (remote_init >= 0)
452*e7be843bSPierre Pronchery flags |= QCTX_AUTO_S;
453*e7be843bSPierre Pronchery
454*e7be843bSPierre Pronchery if (remote_init > 0)
455*e7be843bSPierre Pronchery flags |= QCTX_REMOTE_INIT;
456*e7be843bSPierre Pronchery
457*e7be843bSPierre Pronchery if (in_io)
458*e7be843bSPierre Pronchery flags |= QCTX_IO;
459*e7be843bSPierre Pronchery
460*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, flags);
461*e7be843bSPierre Pronchery }
462*e7be843bSPierre Pronchery
463*e7be843bSPierre Pronchery /*
464*e7be843bSPierre Pronchery * Like expect_quic_cs(), but fails if called on a QUIC_XSO. ctx->xso may still
465*e7be843bSPierre Pronchery * be non-NULL if the QCSO has a default stream.
466*e7be843bSPierre Pronchery */
expect_quic_conn_only(const SSL * s,QCTX * ctx)467*e7be843bSPierre Pronchery static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx)
468*e7be843bSPierre Pronchery {
469*e7be843bSPierre Pronchery return expect_quic_as(s, ctx, QCTX_C);
470*e7be843bSPierre Pronchery }
471*e7be843bSPierre Pronchery
472*e7be843bSPierre Pronchery /*
473*e7be843bSPierre Pronchery * Ensures that the domain mutex is held for a method which touches channel
474*e7be843bSPierre Pronchery * state.
475*e7be843bSPierre Pronchery *
476*e7be843bSPierre Pronchery * Precondition: Domain mutex is not held (unchecked)
477*e7be843bSPierre Pronchery */
qctx_lock(QCTX * ctx)478*e7be843bSPierre Pronchery static void qctx_lock(QCTX *ctx)
479*e7be843bSPierre Pronchery {
480*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
481*e7be843bSPierre Pronchery assert(ctx->obj != NULL);
482*e7be843bSPierre Pronchery ossl_crypto_mutex_lock(ossl_quic_obj_get0_mutex(ctx->obj));
483*e7be843bSPierre Pronchery #endif
484*e7be843bSPierre Pronchery }
485*e7be843bSPierre Pronchery
486*e7be843bSPierre Pronchery /* Precondition: Channel mutex is held (unchecked) */
487*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qctx_unlock(QCTX * ctx)488*e7be843bSPierre Pronchery static void qctx_unlock(QCTX *ctx)
489*e7be843bSPierre Pronchery {
490*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
491*e7be843bSPierre Pronchery assert(ctx->obj != NULL);
492*e7be843bSPierre Pronchery ossl_crypto_mutex_unlock(ossl_quic_obj_get0_mutex(ctx->obj));
493*e7be843bSPierre Pronchery #endif
494*e7be843bSPierre Pronchery }
495*e7be843bSPierre Pronchery
qctx_lock_for_io(QCTX * ctx)496*e7be843bSPierre Pronchery static void qctx_lock_for_io(QCTX *ctx)
497*e7be843bSPierre Pronchery {
498*e7be843bSPierre Pronchery qctx_lock(ctx);
499*e7be843bSPierre Pronchery ctx->in_io = 1;
500*e7be843bSPierre Pronchery
501*e7be843bSPierre Pronchery /*
502*e7be843bSPierre Pronchery * We are entering an I/O function so we must update the values returned by
503*e7be843bSPierre Pronchery * SSL_get_error and SSL_want. Set no error. This will be overridden later
504*e7be843bSPierre Pronchery * if a call to QUIC_RAISE_NORMAL_ERROR or QUIC_RAISE_NON_NORMAL_ERROR
505*e7be843bSPierre Pronchery * occurs during the API call.
506*e7be843bSPierre Pronchery */
507*e7be843bSPierre Pronchery quic_set_last_error(ctx, SSL_ERROR_NONE);
508*e7be843bSPierre Pronchery }
509*e7be843bSPierre Pronchery
510*e7be843bSPierre Pronchery /*
511*e7be843bSPierre Pronchery * This predicate is the criterion which should determine API call rejection for
512*e7be843bSPierre Pronchery * *most* mutating API calls, particularly stream-related operations for send
513*e7be843bSPierre Pronchery * parts.
514*e7be843bSPierre Pronchery *
515*e7be843bSPierre Pronchery * A call is rejected (this function returns 0) if shutdown is in progress
516*e7be843bSPierre Pronchery * (stream flushing), or we are in a TERMINATING or TERMINATED state. If
517*e7be843bSPierre Pronchery * req_active=1, the connection must be active (i.e., the IDLE state is also
518*e7be843bSPierre Pronchery * rejected).
519*e7be843bSPierre Pronchery */
quic_mutation_allowed(QUIC_CONNECTION * qc,int req_active)520*e7be843bSPierre Pronchery static int quic_mutation_allowed(QUIC_CONNECTION *qc, int req_active)
521*e7be843bSPierre Pronchery {
522*e7be843bSPierre Pronchery if (qc->shutting_down || ossl_quic_channel_is_term_any(qc->ch))
523*e7be843bSPierre Pronchery return 0;
524*e7be843bSPierre Pronchery
525*e7be843bSPierre Pronchery if (req_active && !ossl_quic_channel_is_active(qc->ch))
526*e7be843bSPierre Pronchery return 0;
527*e7be843bSPierre Pronchery
528*e7be843bSPierre Pronchery return 1;
529*e7be843bSPierre Pronchery }
530*e7be843bSPierre Pronchery
qctx_is_top_level(QCTX * ctx)531*e7be843bSPierre Pronchery static int qctx_is_top_level(QCTX *ctx)
532*e7be843bSPierre Pronchery {
533*e7be843bSPierre Pronchery return ctx->obj->parent_obj == NULL;
534*e7be843bSPierre Pronchery }
535*e7be843bSPierre Pronchery
qctx_blocking(QCTX * ctx)536*e7be843bSPierre Pronchery static int qctx_blocking(QCTX *ctx)
537*e7be843bSPierre Pronchery {
538*e7be843bSPierre Pronchery return ossl_quic_obj_blocking(ctx->obj);
539*e7be843bSPierre Pronchery }
540*e7be843bSPierre Pronchery
541*e7be843bSPierre Pronchery /*
542*e7be843bSPierre Pronchery * Block until a predicate is met.
543*e7be843bSPierre Pronchery *
544*e7be843bSPierre Pronchery * Precondition: Must have a channel.
545*e7be843bSPierre Pronchery * Precondition: Must hold channel lock (unchecked).
546*e7be843bSPierre Pronchery */
547*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
block_until_pred(QCTX * ctx,int (* pred)(void * arg),void * pred_arg,uint32_t flags)548*e7be843bSPierre Pronchery static int block_until_pred(QCTX *ctx,
549*e7be843bSPierre Pronchery int (*pred)(void *arg), void *pred_arg,
550*e7be843bSPierre Pronchery uint32_t flags)
551*e7be843bSPierre Pronchery {
552*e7be843bSPierre Pronchery QUIC_ENGINE *qeng;
553*e7be843bSPierre Pronchery QUIC_REACTOR *rtor;
554*e7be843bSPierre Pronchery
555*e7be843bSPierre Pronchery qeng = ossl_quic_obj_get0_engine(ctx->obj);
556*e7be843bSPierre Pronchery assert(qeng != NULL);
557*e7be843bSPierre Pronchery
558*e7be843bSPierre Pronchery /*
559*e7be843bSPierre Pronchery * Any attempt to block auto-disables tick inhibition as otherwise we will
560*e7be843bSPierre Pronchery * hang around forever.
561*e7be843bSPierre Pronchery */
562*e7be843bSPierre Pronchery ossl_quic_engine_set_inhibit_tick(qeng, 0);
563*e7be843bSPierre Pronchery
564*e7be843bSPierre Pronchery rtor = ossl_quic_engine_get0_reactor(qeng);
565*e7be843bSPierre Pronchery return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags);
566*e7be843bSPierre Pronchery }
567*e7be843bSPierre Pronchery
568*e7be843bSPierre Pronchery /*
569*e7be843bSPierre Pronchery * QUIC Front-End I/O API: Initialization
570*e7be843bSPierre Pronchery * ======================================
571*e7be843bSPierre Pronchery *
572*e7be843bSPierre Pronchery * SSL_new => ossl_quic_new
573*e7be843bSPierre Pronchery * ossl_quic_init
574*e7be843bSPierre Pronchery * SSL_reset => ossl_quic_reset
575*e7be843bSPierre Pronchery * SSL_clear => ossl_quic_clear
576*e7be843bSPierre Pronchery * ossl_quic_deinit
577*e7be843bSPierre Pronchery * SSL_free => ossl_quic_free
578*e7be843bSPierre Pronchery *
579*e7be843bSPierre Pronchery * SSL_set_options => ossl_quic_set_options
580*e7be843bSPierre Pronchery * SSL_get_options => ossl_quic_get_options
581*e7be843bSPierre Pronchery * SSL_clear_options => ossl_quic_clear_options
582*e7be843bSPierre Pronchery *
583*e7be843bSPierre Pronchery */
584*e7be843bSPierre Pronchery
585*e7be843bSPierre Pronchery /* SSL_new */
ossl_quic_new(SSL_CTX * ctx)586*e7be843bSPierre Pronchery SSL *ossl_quic_new(SSL_CTX *ctx)
587*e7be843bSPierre Pronchery {
588*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = NULL;
589*e7be843bSPierre Pronchery SSL_CONNECTION *sc = NULL;
590*e7be843bSPierre Pronchery
591*e7be843bSPierre Pronchery /*
592*e7be843bSPierre Pronchery * QUIC_server_method should not be used with SSL_new.
593*e7be843bSPierre Pronchery * It should only be used with SSL_new_listener.
594*e7be843bSPierre Pronchery */
595*e7be843bSPierre Pronchery if (ctx->method == OSSL_QUIC_server_method()) {
596*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
597*e7be843bSPierre Pronchery return NULL;
598*e7be843bSPierre Pronchery }
599*e7be843bSPierre Pronchery
600*e7be843bSPierre Pronchery qc = OPENSSL_zalloc(sizeof(*qc));
601*e7be843bSPierre Pronchery if (qc == NULL) {
602*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
603*e7be843bSPierre Pronchery return NULL;
604*e7be843bSPierre Pronchery }
605*e7be843bSPierre Pronchery
606*e7be843bSPierre Pronchery /* Create the QUIC domain mutex. */
607*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
608*e7be843bSPierre Pronchery if ((qc->mutex = ossl_crypto_mutex_new()) == NULL) {
609*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
610*e7be843bSPierre Pronchery goto err;
611*e7be843bSPierre Pronchery }
612*e7be843bSPierre Pronchery #endif
613*e7be843bSPierre Pronchery
614*e7be843bSPierre Pronchery /* Create the handshake layer. */
615*e7be843bSPierre Pronchery qc->tls = ossl_ssl_connection_new_int(ctx, &qc->obj.ssl, TLS_method());
616*e7be843bSPierre Pronchery if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
617*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
618*e7be843bSPierre Pronchery goto err;
619*e7be843bSPierre Pronchery }
620*e7be843bSPierre Pronchery
621*e7be843bSPierre Pronchery /* override the user_ssl of the inner connection */
622*e7be843bSPierre Pronchery sc->s3.flags |= TLS1_FLAGS_QUIC | TLS1_FLAGS_QUIC_INTERNAL;
623*e7be843bSPierre Pronchery
624*e7be843bSPierre Pronchery /* Restrict options derived from the SSL_CTX. */
625*e7be843bSPierre Pronchery sc->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
626*e7be843bSPierre Pronchery sc->pha_enabled = 0;
627*e7be843bSPierre Pronchery
628*e7be843bSPierre Pronchery /* Determine mode of operation. */
629*e7be843bSPierre Pronchery #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
630*e7be843bSPierre Pronchery qc->is_thread_assisted
631*e7be843bSPierre Pronchery = ((ctx->domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0);
632*e7be843bSPierre Pronchery #endif
633*e7be843bSPierre Pronchery
634*e7be843bSPierre Pronchery qc->as_server = 0;
635*e7be843bSPierre Pronchery qc->as_server_state = qc->as_server;
636*e7be843bSPierre Pronchery
637*e7be843bSPierre Pronchery if (!create_channel(qc, ctx))
638*e7be843bSPierre Pronchery goto err;
639*e7be843bSPierre Pronchery
640*e7be843bSPierre Pronchery ossl_quic_channel_set_msg_callback(qc->ch, ctx->msg_callback, &qc->obj.ssl);
641*e7be843bSPierre Pronchery ossl_quic_channel_set_msg_callback_arg(qc->ch, ctx->msg_callback_arg);
642*e7be843bSPierre Pronchery
643*e7be843bSPierre Pronchery /* Initialise the QUIC_CONNECTION's QUIC_OBJ base. */
644*e7be843bSPierre Pronchery if (!ossl_quic_obj_init(&qc->obj, ctx, SSL_TYPE_QUIC_CONNECTION, NULL,
645*e7be843bSPierre Pronchery qc->engine, qc->port)) {
646*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
647*e7be843bSPierre Pronchery goto err;
648*e7be843bSPierre Pronchery }
649*e7be843bSPierre Pronchery
650*e7be843bSPierre Pronchery /* Initialise libssl APL-related state. */
651*e7be843bSPierre Pronchery qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
652*e7be843bSPierre Pronchery qc->default_ssl_mode = qc->obj.ssl.ctx->mode;
653*e7be843bSPierre Pronchery qc->default_ssl_options = qc->obj.ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
654*e7be843bSPierre Pronchery qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
655*e7be843bSPierre Pronchery qc->last_error = SSL_ERROR_NONE;
656*e7be843bSPierre Pronchery
657*e7be843bSPierre Pronchery qc_update_reject_policy(qc);
658*e7be843bSPierre Pronchery
659*e7be843bSPierre Pronchery /*
660*e7be843bSPierre Pronchery * We do not create the default XSO yet. The reason for this is that the
661*e7be843bSPierre Pronchery * stream ID of the default XSO will depend on whether the stream is client
662*e7be843bSPierre Pronchery * or server-initiated, which depends on who transmits first. Since we do
663*e7be843bSPierre Pronchery * not know whether the application will be using a client-transmits-first
664*e7be843bSPierre Pronchery * or server-transmits-first protocol, we defer default XSO creation until
665*e7be843bSPierre Pronchery * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first,
666*e7be843bSPierre Pronchery * we take that as a cue that the client is expecting a server-initiated
667*e7be843bSPierre Pronchery * stream, and vice versa if SSL_write() is called first.
668*e7be843bSPierre Pronchery */
669*e7be843bSPierre Pronchery return &qc->obj.ssl;
670*e7be843bSPierre Pronchery
671*e7be843bSPierre Pronchery err:
672*e7be843bSPierre Pronchery if (qc != NULL) {
673*e7be843bSPierre Pronchery qc_cleanup(qc, /*have_lock=*/0);
674*e7be843bSPierre Pronchery OPENSSL_free(qc);
675*e7be843bSPierre Pronchery }
676*e7be843bSPierre Pronchery return NULL;
677*e7be843bSPierre Pronchery }
678*e7be843bSPierre Pronchery
679*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_unref_port_bios(QUIC_PORT * port)680*e7be843bSPierre Pronchery static void quic_unref_port_bios(QUIC_PORT *port)
681*e7be843bSPierre Pronchery {
682*e7be843bSPierre Pronchery BIO *b;
683*e7be843bSPierre Pronchery
684*e7be843bSPierre Pronchery b = ossl_quic_port_get_net_rbio(port);
685*e7be843bSPierre Pronchery BIO_free_all(b);
686*e7be843bSPierre Pronchery
687*e7be843bSPierre Pronchery b = ossl_quic_port_get_net_wbio(port);
688*e7be843bSPierre Pronchery BIO_free_all(b);
689*e7be843bSPierre Pronchery }
690*e7be843bSPierre Pronchery
691*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_cleanup(QUIC_CONNECTION * qc,int have_lock)692*e7be843bSPierre Pronchery static void qc_cleanup(QUIC_CONNECTION *qc, int have_lock)
693*e7be843bSPierre Pronchery {
694*e7be843bSPierre Pronchery SSL_free(qc->tls);
695*e7be843bSPierre Pronchery qc->tls = NULL;
696*e7be843bSPierre Pronchery
697*e7be843bSPierre Pronchery ossl_quic_channel_free(qc->ch);
698*e7be843bSPierre Pronchery qc->ch = NULL;
699*e7be843bSPierre Pronchery
700*e7be843bSPierre Pronchery if (qc->port != NULL && qc->listener == NULL && qc->pending == 0) { /* TODO */
701*e7be843bSPierre Pronchery quic_unref_port_bios(qc->port);
702*e7be843bSPierre Pronchery ossl_quic_port_free(qc->port);
703*e7be843bSPierre Pronchery qc->port = NULL;
704*e7be843bSPierre Pronchery
705*e7be843bSPierre Pronchery ossl_quic_engine_free(qc->engine);
706*e7be843bSPierre Pronchery qc->engine = NULL;
707*e7be843bSPierre Pronchery }
708*e7be843bSPierre Pronchery
709*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
710*e7be843bSPierre Pronchery if (have_lock)
711*e7be843bSPierre Pronchery /* tsan doesn't like freeing locked mutexes */
712*e7be843bSPierre Pronchery ossl_crypto_mutex_unlock(qc->mutex);
713*e7be843bSPierre Pronchery
714*e7be843bSPierre Pronchery if (qc->listener == NULL && qc->pending == 0)
715*e7be843bSPierre Pronchery ossl_crypto_mutex_free(&qc->mutex);
716*e7be843bSPierre Pronchery #endif
717*e7be843bSPierre Pronchery }
718*e7be843bSPierre Pronchery
719*e7be843bSPierre Pronchery /* SSL_free */
720*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
quic_free_listener(QCTX * ctx)721*e7be843bSPierre Pronchery static void quic_free_listener(QCTX *ctx)
722*e7be843bSPierre Pronchery {
723*e7be843bSPierre Pronchery quic_unref_port_bios(ctx->ql->port);
724*e7be843bSPierre Pronchery ossl_quic_port_drop_incoming(ctx->ql->port);
725*e7be843bSPierre Pronchery ossl_quic_port_free(ctx->ql->port);
726*e7be843bSPierre Pronchery
727*e7be843bSPierre Pronchery if (ctx->ql->domain == NULL) {
728*e7be843bSPierre Pronchery ossl_quic_engine_free(ctx->ql->engine);
729*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
730*e7be843bSPierre Pronchery ossl_crypto_mutex_free(&ctx->ql->mutex);
731*e7be843bSPierre Pronchery #endif
732*e7be843bSPierre Pronchery } else {
733*e7be843bSPierre Pronchery SSL_free(&ctx->ql->domain->obj.ssl);
734*e7be843bSPierre Pronchery }
735*e7be843bSPierre Pronchery }
736*e7be843bSPierre Pronchery
737*e7be843bSPierre Pronchery /* SSL_free */
738*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
quic_free_domain(QCTX * ctx)739*e7be843bSPierre Pronchery static void quic_free_domain(QCTX *ctx)
740*e7be843bSPierre Pronchery {
741*e7be843bSPierre Pronchery ossl_quic_engine_free(ctx->qd->engine);
742*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
743*e7be843bSPierre Pronchery ossl_crypto_mutex_free(&ctx->qd->mutex);
744*e7be843bSPierre Pronchery #endif
745*e7be843bSPierre Pronchery }
746*e7be843bSPierre Pronchery
747*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_free(SSL * s)748*e7be843bSPierre Pronchery void ossl_quic_free(SSL *s)
749*e7be843bSPierre Pronchery {
750*e7be843bSPierre Pronchery QCTX ctx;
751*e7be843bSPierre Pronchery int is_default;
752*e7be843bSPierre Pronchery
753*e7be843bSPierre Pronchery /* We should never be called on anything but a QSO. */
754*e7be843bSPierre Pronchery if (!expect_quic_any(s, &ctx))
755*e7be843bSPierre Pronchery return;
756*e7be843bSPierre Pronchery
757*e7be843bSPierre Pronchery if (ctx.is_domain) {
758*e7be843bSPierre Pronchery quic_free_domain(&ctx);
759*e7be843bSPierre Pronchery return;
760*e7be843bSPierre Pronchery }
761*e7be843bSPierre Pronchery
762*e7be843bSPierre Pronchery if (ctx.is_listener) {
763*e7be843bSPierre Pronchery quic_free_listener(&ctx);
764*e7be843bSPierre Pronchery return;
765*e7be843bSPierre Pronchery }
766*e7be843bSPierre Pronchery
767*e7be843bSPierre Pronchery qctx_lock(&ctx);
768*e7be843bSPierre Pronchery
769*e7be843bSPierre Pronchery if (ctx.is_stream) {
770*e7be843bSPierre Pronchery /*
771*e7be843bSPierre Pronchery * When a QSSO is freed, the XSO is freed immediately, because the XSO
772*e7be843bSPierre Pronchery * itself only contains API personality layer data. However the
773*e7be843bSPierre Pronchery * underlying QUIC_STREAM is not freed immediately but is instead marked
774*e7be843bSPierre Pronchery * as deleted for later collection.
775*e7be843bSPierre Pronchery */
776*e7be843bSPierre Pronchery
777*e7be843bSPierre Pronchery assert(ctx.qc->num_xso > 0);
778*e7be843bSPierre Pronchery --ctx.qc->num_xso;
779*e7be843bSPierre Pronchery
780*e7be843bSPierre Pronchery /* If a stream's send part has not been finished, auto-reset it. */
781*e7be843bSPierre Pronchery if (( ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_READY
782*e7be843bSPierre Pronchery || ctx.xso->stream->send_state == QUIC_SSTREAM_STATE_SEND)
783*e7be843bSPierre Pronchery && !ossl_quic_sstream_get_final_size(ctx.xso->stream->sstream, NULL))
784*e7be843bSPierre Pronchery ossl_quic_stream_map_reset_stream_send_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
785*e7be843bSPierre Pronchery ctx.xso->stream, 0);
786*e7be843bSPierre Pronchery
787*e7be843bSPierre Pronchery /* Do STOP_SENDING for the receive part, if applicable. */
788*e7be843bSPierre Pronchery if ( ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_RECV
789*e7be843bSPierre Pronchery || ctx.xso->stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN)
790*e7be843bSPierre Pronchery ossl_quic_stream_map_stop_sending_recv_part(ossl_quic_channel_get_qsm(ctx.qc->ch),
791*e7be843bSPierre Pronchery ctx.xso->stream, 0);
792*e7be843bSPierre Pronchery
793*e7be843bSPierre Pronchery /* Update stream state. */
794*e7be843bSPierre Pronchery ctx.xso->stream->deleted = 1;
795*e7be843bSPierre Pronchery ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.qc->ch),
796*e7be843bSPierre Pronchery ctx.xso->stream);
797*e7be843bSPierre Pronchery
798*e7be843bSPierre Pronchery is_default = (ctx.xso == ctx.qc->default_xso);
799*e7be843bSPierre Pronchery qctx_unlock(&ctx);
800*e7be843bSPierre Pronchery
801*e7be843bSPierre Pronchery /*
802*e7be843bSPierre Pronchery * Unref the connection in most cases; the XSO has a ref to the QC and
803*e7be843bSPierre Pronchery * not vice versa. But for a default XSO, to avoid circular references,
804*e7be843bSPierre Pronchery * the QC refs the XSO but the XSO does not ref the QC. If we are the
805*e7be843bSPierre Pronchery * default XSO, we only get here when the QC is being torn down anyway,
806*e7be843bSPierre Pronchery * so don't call SSL_free(qc) as we are already in it.
807*e7be843bSPierre Pronchery */
808*e7be843bSPierre Pronchery if (!is_default)
809*e7be843bSPierre Pronchery SSL_free(&ctx.qc->obj.ssl);
810*e7be843bSPierre Pronchery
811*e7be843bSPierre Pronchery /* Note: SSL_free calls OPENSSL_free(xso) for us */
812*e7be843bSPierre Pronchery return;
813*e7be843bSPierre Pronchery }
814*e7be843bSPierre Pronchery
815*e7be843bSPierre Pronchery /*
816*e7be843bSPierre Pronchery * Free the default XSO, if any. The QUIC_STREAM is not deleted at this
817*e7be843bSPierre Pronchery * stage, but is freed during the channel free when the whole QSM is freed.
818*e7be843bSPierre Pronchery */
819*e7be843bSPierre Pronchery if (ctx.qc->default_xso != NULL) {
820*e7be843bSPierre Pronchery QUIC_XSO *xso = ctx.qc->default_xso;
821*e7be843bSPierre Pronchery
822*e7be843bSPierre Pronchery qctx_unlock(&ctx);
823*e7be843bSPierre Pronchery SSL_free(&xso->obj.ssl);
824*e7be843bSPierre Pronchery qctx_lock(&ctx);
825*e7be843bSPierre Pronchery ctx.qc->default_xso = NULL;
826*e7be843bSPierre Pronchery }
827*e7be843bSPierre Pronchery
828*e7be843bSPierre Pronchery /* Ensure we have no remaining XSOs. */
829*e7be843bSPierre Pronchery assert(ctx.qc->num_xso == 0);
830*e7be843bSPierre Pronchery
831*e7be843bSPierre Pronchery #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
832*e7be843bSPierre Pronchery if (ctx.qc->is_thread_assisted && ctx.qc->started) {
833*e7be843bSPierre Pronchery ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist);
834*e7be843bSPierre Pronchery ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist);
835*e7be843bSPierre Pronchery }
836*e7be843bSPierre Pronchery #endif
837*e7be843bSPierre Pronchery
838*e7be843bSPierre Pronchery /*
839*e7be843bSPierre Pronchery * Note: SSL_free (that called this function) calls OPENSSL_free(ctx.qc) for
840*e7be843bSPierre Pronchery * us
841*e7be843bSPierre Pronchery */
842*e7be843bSPierre Pronchery qc_cleanup(ctx.qc, /*have_lock=*/1);
843*e7be843bSPierre Pronchery /* Note: SSL_free calls OPENSSL_free(qc) for us */
844*e7be843bSPierre Pronchery
845*e7be843bSPierre Pronchery if (ctx.qc->listener != NULL)
846*e7be843bSPierre Pronchery SSL_free(&ctx.qc->listener->obj.ssl);
847*e7be843bSPierre Pronchery if (ctx.qc->domain != NULL)
848*e7be843bSPierre Pronchery SSL_free(&ctx.qc->domain->obj.ssl);
849*e7be843bSPierre Pronchery }
850*e7be843bSPierre Pronchery
851*e7be843bSPierre Pronchery /* SSL method init */
ossl_quic_init(SSL * s)852*e7be843bSPierre Pronchery int ossl_quic_init(SSL *s)
853*e7be843bSPierre Pronchery {
854*e7be843bSPierre Pronchery /* Same op as SSL_clear, forward the call. */
855*e7be843bSPierre Pronchery return ossl_quic_clear(s);
856*e7be843bSPierre Pronchery }
857*e7be843bSPierre Pronchery
858*e7be843bSPierre Pronchery /* SSL method deinit */
ossl_quic_deinit(SSL * s)859*e7be843bSPierre Pronchery void ossl_quic_deinit(SSL *s)
860*e7be843bSPierre Pronchery {
861*e7be843bSPierre Pronchery /* No-op. */
862*e7be843bSPierre Pronchery }
863*e7be843bSPierre Pronchery
864*e7be843bSPierre Pronchery /* SSL_clear (ssl_reset method) */
ossl_quic_reset(SSL * s)865*e7be843bSPierre Pronchery int ossl_quic_reset(SSL *s)
866*e7be843bSPierre Pronchery {
867*e7be843bSPierre Pronchery QCTX ctx;
868*e7be843bSPierre Pronchery
869*e7be843bSPierre Pronchery if (!expect_quic_any(s, &ctx))
870*e7be843bSPierre Pronchery return 0;
871*e7be843bSPierre Pronchery
872*e7be843bSPierre Pronchery ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
873*e7be843bSPierre Pronchery return 0;
874*e7be843bSPierre Pronchery }
875*e7be843bSPierre Pronchery
876*e7be843bSPierre Pronchery /* ssl_clear method (unused) */
ossl_quic_clear(SSL * s)877*e7be843bSPierre Pronchery int ossl_quic_clear(SSL *s)
878*e7be843bSPierre Pronchery {
879*e7be843bSPierre Pronchery QCTX ctx;
880*e7be843bSPierre Pronchery
881*e7be843bSPierre Pronchery if (!expect_quic_any(s, &ctx))
882*e7be843bSPierre Pronchery return 0;
883*e7be843bSPierre Pronchery
884*e7be843bSPierre Pronchery ERR_raise(ERR_LIB_SSL, ERR_R_UNSUPPORTED);
885*e7be843bSPierre Pronchery return 0;
886*e7be843bSPierre Pronchery }
887*e7be843bSPierre Pronchery
ossl_quic_set_override_now_cb(SSL * s,OSSL_TIME (* now_cb)(void * arg),void * now_cb_arg)888*e7be843bSPierre Pronchery int ossl_quic_set_override_now_cb(SSL *s,
889*e7be843bSPierre Pronchery OSSL_TIME (*now_cb)(void *arg),
890*e7be843bSPierre Pronchery void *now_cb_arg)
891*e7be843bSPierre Pronchery {
892*e7be843bSPierre Pronchery QCTX ctx;
893*e7be843bSPierre Pronchery
894*e7be843bSPierre Pronchery if (!expect_quic_any(s, &ctx))
895*e7be843bSPierre Pronchery return 0;
896*e7be843bSPierre Pronchery
897*e7be843bSPierre Pronchery qctx_lock(&ctx);
898*e7be843bSPierre Pronchery
899*e7be843bSPierre Pronchery ossl_quic_engine_set_time_cb(ctx.obj->engine, now_cb, now_cb_arg);
900*e7be843bSPierre Pronchery
901*e7be843bSPierre Pronchery qctx_unlock(&ctx);
902*e7be843bSPierre Pronchery return 1;
903*e7be843bSPierre Pronchery }
904*e7be843bSPierre Pronchery
ossl_quic_conn_force_assist_thread_wake(SSL * s)905*e7be843bSPierre Pronchery void ossl_quic_conn_force_assist_thread_wake(SSL *s)
906*e7be843bSPierre Pronchery {
907*e7be843bSPierre Pronchery QCTX ctx;
908*e7be843bSPierre Pronchery
909*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
910*e7be843bSPierre Pronchery return;
911*e7be843bSPierre Pronchery
912*e7be843bSPierre Pronchery #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
913*e7be843bSPierre Pronchery if (ctx.qc->is_thread_assisted && ctx.qc->started)
914*e7be843bSPierre Pronchery ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist);
915*e7be843bSPierre Pronchery #endif
916*e7be843bSPierre Pronchery }
917*e7be843bSPierre Pronchery
918*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_touch_default_xso(QUIC_CONNECTION * qc)919*e7be843bSPierre Pronchery static void qc_touch_default_xso(QUIC_CONNECTION *qc)
920*e7be843bSPierre Pronchery {
921*e7be843bSPierre Pronchery qc->default_xso_created = 1;
922*e7be843bSPierre Pronchery qc_update_reject_policy(qc);
923*e7be843bSPierre Pronchery }
924*e7be843bSPierre Pronchery
925*e7be843bSPierre Pronchery /*
926*e7be843bSPierre Pronchery * Changes default XSO. Allows caller to keep reference to the old default XSO
927*e7be843bSPierre Pronchery * (if any). Reference to new XSO is transferred from caller.
928*e7be843bSPierre Pronchery */
929*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_set_default_xso_keep_ref(QUIC_CONNECTION * qc,QUIC_XSO * xso,int touch,QUIC_XSO ** old_xso)930*e7be843bSPierre Pronchery static void qc_set_default_xso_keep_ref(QUIC_CONNECTION *qc, QUIC_XSO *xso,
931*e7be843bSPierre Pronchery int touch,
932*e7be843bSPierre Pronchery QUIC_XSO **old_xso)
933*e7be843bSPierre Pronchery {
934*e7be843bSPierre Pronchery int refs;
935*e7be843bSPierre Pronchery
936*e7be843bSPierre Pronchery *old_xso = NULL;
937*e7be843bSPierre Pronchery
938*e7be843bSPierre Pronchery if (qc->default_xso != xso) {
939*e7be843bSPierre Pronchery *old_xso = qc->default_xso; /* transfer old XSO ref to caller */
940*e7be843bSPierre Pronchery
941*e7be843bSPierre Pronchery qc->default_xso = xso;
942*e7be843bSPierre Pronchery
943*e7be843bSPierre Pronchery if (xso == NULL) {
944*e7be843bSPierre Pronchery /*
945*e7be843bSPierre Pronchery * Changing to not having a default XSO. XSO becomes standalone and
946*e7be843bSPierre Pronchery * now has a ref to the QC.
947*e7be843bSPierre Pronchery */
948*e7be843bSPierre Pronchery if (!ossl_assert(SSL_up_ref(&qc->obj.ssl)))
949*e7be843bSPierre Pronchery return;
950*e7be843bSPierre Pronchery } else {
951*e7be843bSPierre Pronchery /*
952*e7be843bSPierre Pronchery * Changing from not having a default XSO to having one. The new XSO
953*e7be843bSPierre Pronchery * will have had a reference to the QC we need to drop to avoid a
954*e7be843bSPierre Pronchery * circular reference.
955*e7be843bSPierre Pronchery *
956*e7be843bSPierre Pronchery * Currently we never change directly from one default XSO to
957*e7be843bSPierre Pronchery * another, though this function would also still be correct if this
958*e7be843bSPierre Pronchery * weren't the case.
959*e7be843bSPierre Pronchery */
960*e7be843bSPierre Pronchery assert(*old_xso == NULL);
961*e7be843bSPierre Pronchery
962*e7be843bSPierre Pronchery CRYPTO_DOWN_REF(&qc->obj.ssl.references, &refs);
963*e7be843bSPierre Pronchery assert(refs > 0);
964*e7be843bSPierre Pronchery }
965*e7be843bSPierre Pronchery }
966*e7be843bSPierre Pronchery
967*e7be843bSPierre Pronchery if (touch)
968*e7be843bSPierre Pronchery qc_touch_default_xso(qc);
969*e7be843bSPierre Pronchery }
970*e7be843bSPierre Pronchery
971*e7be843bSPierre Pronchery /*
972*e7be843bSPierre Pronchery * Changes default XSO, releasing the reference to any previous default XSO.
973*e7be843bSPierre Pronchery * Reference to new XSO is transferred from caller.
974*e7be843bSPierre Pronchery */
975*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_set_default_xso(QUIC_CONNECTION * qc,QUIC_XSO * xso,int touch)976*e7be843bSPierre Pronchery static void qc_set_default_xso(QUIC_CONNECTION *qc, QUIC_XSO *xso, int touch)
977*e7be843bSPierre Pronchery {
978*e7be843bSPierre Pronchery QUIC_XSO *old_xso = NULL;
979*e7be843bSPierre Pronchery
980*e7be843bSPierre Pronchery qc_set_default_xso_keep_ref(qc, xso, touch, &old_xso);
981*e7be843bSPierre Pronchery
982*e7be843bSPierre Pronchery if (old_xso != NULL)
983*e7be843bSPierre Pronchery SSL_free(&old_xso->obj.ssl);
984*e7be843bSPierre Pronchery }
985*e7be843bSPierre Pronchery
986*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
xso_update_options(QUIC_XSO * xso)987*e7be843bSPierre Pronchery static void xso_update_options(QUIC_XSO *xso)
988*e7be843bSPierre Pronchery {
989*e7be843bSPierre Pronchery int cleanse = ((xso->ssl_options & SSL_OP_CLEANSE_PLAINTEXT) != 0);
990*e7be843bSPierre Pronchery
991*e7be843bSPierre Pronchery if (xso->stream->rstream != NULL)
992*e7be843bSPierre Pronchery ossl_quic_rstream_set_cleanse(xso->stream->rstream, cleanse);
993*e7be843bSPierre Pronchery
994*e7be843bSPierre Pronchery if (xso->stream->sstream != NULL)
995*e7be843bSPierre Pronchery ossl_quic_sstream_set_cleanse(xso->stream->sstream, cleanse);
996*e7be843bSPierre Pronchery }
997*e7be843bSPierre Pronchery
998*e7be843bSPierre Pronchery /*
999*e7be843bSPierre Pronchery * SSL_set_options
1000*e7be843bSPierre Pronchery * ---------------
1001*e7be843bSPierre Pronchery *
1002*e7be843bSPierre Pronchery * Setting options on a QCSO
1003*e7be843bSPierre Pronchery * - configures the handshake-layer options;
1004*e7be843bSPierre Pronchery * - configures the default data-plane options for new streams;
1005*e7be843bSPierre Pronchery * - configures the data-plane options on the default XSO, if there is one.
1006*e7be843bSPierre Pronchery *
1007*e7be843bSPierre Pronchery * Setting options on a QSSO
1008*e7be843bSPierre Pronchery * - configures data-plane options for that stream only.
1009*e7be843bSPierre Pronchery */
1010*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
quic_mask_or_options(SSL * ssl,uint64_t mask_value,uint64_t or_value)1011*e7be843bSPierre Pronchery static uint64_t quic_mask_or_options(SSL *ssl, uint64_t mask_value, uint64_t or_value)
1012*e7be843bSPierre Pronchery {
1013*e7be843bSPierre Pronchery QCTX ctx;
1014*e7be843bSPierre Pronchery uint64_t hs_mask_value, hs_or_value, ret;
1015*e7be843bSPierre Pronchery
1016*e7be843bSPierre Pronchery if (!expect_quic_cs(ssl, &ctx))
1017*e7be843bSPierre Pronchery return 0;
1018*e7be843bSPierre Pronchery
1019*e7be843bSPierre Pronchery qctx_lock(&ctx);
1020*e7be843bSPierre Pronchery
1021*e7be843bSPierre Pronchery if (!ctx.is_stream) {
1022*e7be843bSPierre Pronchery /*
1023*e7be843bSPierre Pronchery * If we were called on the connection, we apply any handshake option
1024*e7be843bSPierre Pronchery * changes.
1025*e7be843bSPierre Pronchery */
1026*e7be843bSPierre Pronchery hs_mask_value = (mask_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
1027*e7be843bSPierre Pronchery hs_or_value = (or_value & OSSL_QUIC_PERMITTED_OPTIONS_CONN);
1028*e7be843bSPierre Pronchery
1029*e7be843bSPierre Pronchery SSL_clear_options(ctx.qc->tls, hs_mask_value);
1030*e7be843bSPierre Pronchery SSL_set_options(ctx.qc->tls, hs_or_value);
1031*e7be843bSPierre Pronchery
1032*e7be843bSPierre Pronchery /* Update defaults for new streams. */
1033*e7be843bSPierre Pronchery ctx.qc->default_ssl_options
1034*e7be843bSPierre Pronchery = ((ctx.qc->default_ssl_options & ~mask_value) | or_value)
1035*e7be843bSPierre Pronchery & OSSL_QUIC_PERMITTED_OPTIONS;
1036*e7be843bSPierre Pronchery }
1037*e7be843bSPierre Pronchery
1038*e7be843bSPierre Pronchery ret = ctx.qc->default_ssl_options;
1039*e7be843bSPierre Pronchery if (ctx.xso != NULL) {
1040*e7be843bSPierre Pronchery ctx.xso->ssl_options
1041*e7be843bSPierre Pronchery = ((ctx.xso->ssl_options & ~mask_value) | or_value)
1042*e7be843bSPierre Pronchery & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
1043*e7be843bSPierre Pronchery
1044*e7be843bSPierre Pronchery xso_update_options(ctx.xso);
1045*e7be843bSPierre Pronchery
1046*e7be843bSPierre Pronchery if (ctx.is_stream)
1047*e7be843bSPierre Pronchery ret = ctx.xso->ssl_options;
1048*e7be843bSPierre Pronchery }
1049*e7be843bSPierre Pronchery
1050*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1051*e7be843bSPierre Pronchery return ret;
1052*e7be843bSPierre Pronchery }
1053*e7be843bSPierre Pronchery
ossl_quic_set_options(SSL * ssl,uint64_t options)1054*e7be843bSPierre Pronchery uint64_t ossl_quic_set_options(SSL *ssl, uint64_t options)
1055*e7be843bSPierre Pronchery {
1056*e7be843bSPierre Pronchery return quic_mask_or_options(ssl, 0, options);
1057*e7be843bSPierre Pronchery }
1058*e7be843bSPierre Pronchery
1059*e7be843bSPierre Pronchery /* SSL_clear_options */
ossl_quic_clear_options(SSL * ssl,uint64_t options)1060*e7be843bSPierre Pronchery uint64_t ossl_quic_clear_options(SSL *ssl, uint64_t options)
1061*e7be843bSPierre Pronchery {
1062*e7be843bSPierre Pronchery return quic_mask_or_options(ssl, options, 0);
1063*e7be843bSPierre Pronchery }
1064*e7be843bSPierre Pronchery
1065*e7be843bSPierre Pronchery /* SSL_get_options */
ossl_quic_get_options(const SSL * ssl)1066*e7be843bSPierre Pronchery uint64_t ossl_quic_get_options(const SSL *ssl)
1067*e7be843bSPierre Pronchery {
1068*e7be843bSPierre Pronchery return quic_mask_or_options((SSL *)ssl, 0, 0);
1069*e7be843bSPierre Pronchery }
1070*e7be843bSPierre Pronchery
1071*e7be843bSPierre Pronchery /*
1072*e7be843bSPierre Pronchery * QUIC Front-End I/O API: Network BIO Configuration
1073*e7be843bSPierre Pronchery * =================================================
1074*e7be843bSPierre Pronchery *
1075*e7be843bSPierre Pronchery * Handling the different BIOs is difficult:
1076*e7be843bSPierre Pronchery *
1077*e7be843bSPierre Pronchery * - It is more or less a requirement that we use non-blocking network I/O;
1078*e7be843bSPierre Pronchery * we need to be able to have timeouts on recv() calls, and make best effort
1079*e7be843bSPierre Pronchery * (non blocking) send() and recv() calls.
1080*e7be843bSPierre Pronchery *
1081*e7be843bSPierre Pronchery * The only sensible way to do this is to configure the socket into
1082*e7be843bSPierre Pronchery * non-blocking mode. We could try to do select() before calling send() or
1083*e7be843bSPierre Pronchery * recv() to get a guarantee that the call will not block, but this will
1084*e7be843bSPierre Pronchery * probably run into issues with buggy OSes which generate spurious socket
1085*e7be843bSPierre Pronchery * readiness events. In any case, relying on this to work reliably does not
1086*e7be843bSPierre Pronchery * seem sane.
1087*e7be843bSPierre Pronchery *
1088*e7be843bSPierre Pronchery * Timeouts could be handled via setsockopt() socket timeout options, but
1089*e7be843bSPierre Pronchery * this depends on OS support and adds another syscall to every network I/O
1090*e7be843bSPierre Pronchery * operation. It also has obvious thread safety concerns if we want to move
1091*e7be843bSPierre Pronchery * to concurrent use of a single socket at some later date.
1092*e7be843bSPierre Pronchery *
1093*e7be843bSPierre Pronchery * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
1094*e7be843bSPierre Pronchery * be made non-blocking. However some OSes (e.g. Windows) do not support
1095*e7be843bSPierre Pronchery * this, so we cannot rely on this.
1096*e7be843bSPierre Pronchery *
1097*e7be843bSPierre Pronchery * As such, we need to configure any FD in non-blocking mode. This may
1098*e7be843bSPierre Pronchery * confound users who pass a blocking socket to libssl. However, in practice
1099*e7be843bSPierre Pronchery * it would be extremely strange for a user of QUIC to pass an FD to us,
1100*e7be843bSPierre Pronchery * then also try and send receive traffic on the same socket(!). Thus the
1101*e7be843bSPierre Pronchery * impact of this should be limited, and can be documented.
1102*e7be843bSPierre Pronchery *
1103*e7be843bSPierre Pronchery * - We support both blocking and non-blocking operation in terms of the API
1104*e7be843bSPierre Pronchery * presented to the user. One prospect is to set the blocking mode based on
1105*e7be843bSPierre Pronchery * whether the socket passed to us was already in blocking mode. However,
1106*e7be843bSPierre Pronchery * Windows has no API for determining if a socket is in blocking mode (!),
1107*e7be843bSPierre Pronchery * therefore this cannot be done portably. Currently therefore we expose an
1108*e7be843bSPierre Pronchery * explicit API call to set this, and default to blocking mode.
1109*e7be843bSPierre Pronchery *
1110*e7be843bSPierre Pronchery * - We need to determine our initial destination UDP address. The "natural"
1111*e7be843bSPierre Pronchery * way for a user to do this is to set the peer variable on a BIO_dgram.
1112*e7be843bSPierre Pronchery * However, this has problems because BIO_dgram's peer variable is used for
1113*e7be843bSPierre Pronchery * both transmission and reception. This means it can be constantly being
1114*e7be843bSPierre Pronchery * changed to a malicious value (e.g. if some random unrelated entity on the
1115*e7be843bSPierre Pronchery * network starts sending traffic to us) on every read call. This is not a
1116*e7be843bSPierre Pronchery * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
1117*e7be843bSPierre Pronchery * calls only, which do not use this variable. However, we do need to let
1118*e7be843bSPierre Pronchery * the user specify the peer in a 'normal' manner. The compromise here is
1119*e7be843bSPierre Pronchery * that we grab the current peer value set at the time the write BIO is set
1120*e7be843bSPierre Pronchery * and do not read the value again.
1121*e7be843bSPierre Pronchery *
1122*e7be843bSPierre Pronchery * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
1123*e7be843bSPierre Pronchery * Currently we do this by only supporting non-blocking mode.
1124*e7be843bSPierre Pronchery *
1125*e7be843bSPierre Pronchery */
1126*e7be843bSPierre Pronchery
1127*e7be843bSPierre Pronchery /*
1128*e7be843bSPierre Pronchery * Determines what initial destination UDP address we should use, if possible.
1129*e7be843bSPierre Pronchery * If this fails the client must set the destination address manually, or use a
1130*e7be843bSPierre Pronchery * BIO which does not need a destination address.
1131*e7be843bSPierre Pronchery */
csm_analyse_init_peer_addr(BIO * net_wbio,BIO_ADDR * peer)1132*e7be843bSPierre Pronchery static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
1133*e7be843bSPierre Pronchery {
1134*e7be843bSPierre Pronchery if (BIO_dgram_detect_peer_addr(net_wbio, peer) <= 0)
1135*e7be843bSPierre Pronchery return 0;
1136*e7be843bSPierre Pronchery
1137*e7be843bSPierre Pronchery return 1;
1138*e7be843bSPierre Pronchery }
1139*e7be843bSPierre Pronchery
1140*e7be843bSPierre Pronchery static int
quic_set0_net_rbio(QUIC_OBJ * obj,BIO * net_rbio)1141*e7be843bSPierre Pronchery quic_set0_net_rbio(QUIC_OBJ *obj, BIO *net_rbio)
1142*e7be843bSPierre Pronchery {
1143*e7be843bSPierre Pronchery QUIC_PORT *port;
1144*e7be843bSPierre Pronchery BIO *old_rbio = NULL;
1145*e7be843bSPierre Pronchery
1146*e7be843bSPierre Pronchery port = ossl_quic_obj_get0_port(obj);
1147*e7be843bSPierre Pronchery old_rbio = ossl_quic_port_get_net_rbio(port);
1148*e7be843bSPierre Pronchery if (old_rbio == net_rbio)
1149*e7be843bSPierre Pronchery return 0;
1150*e7be843bSPierre Pronchery
1151*e7be843bSPierre Pronchery if (!ossl_quic_port_set_net_rbio(port, net_rbio))
1152*e7be843bSPierre Pronchery return 0;
1153*e7be843bSPierre Pronchery
1154*e7be843bSPierre Pronchery BIO_free_all(old_rbio);
1155*e7be843bSPierre Pronchery if (net_rbio != NULL)
1156*e7be843bSPierre Pronchery BIO_set_nbio(net_rbio, 1); /* best effort autoconfig */
1157*e7be843bSPierre Pronchery
1158*e7be843bSPierre Pronchery return 1;
1159*e7be843bSPierre Pronchery }
1160*e7be843bSPierre Pronchery
1161*e7be843bSPierre Pronchery static int
quic_set0_net_wbio(QUIC_OBJ * obj,BIO * net_wbio)1162*e7be843bSPierre Pronchery quic_set0_net_wbio(QUIC_OBJ *obj, BIO *net_wbio)
1163*e7be843bSPierre Pronchery {
1164*e7be843bSPierre Pronchery QUIC_PORT *port;
1165*e7be843bSPierre Pronchery BIO *old_wbio = NULL;
1166*e7be843bSPierre Pronchery
1167*e7be843bSPierre Pronchery port = ossl_quic_obj_get0_port(obj);
1168*e7be843bSPierre Pronchery old_wbio = ossl_quic_port_get_net_wbio(port);
1169*e7be843bSPierre Pronchery if (old_wbio == net_wbio)
1170*e7be843bSPierre Pronchery return 0;
1171*e7be843bSPierre Pronchery
1172*e7be843bSPierre Pronchery if (!ossl_quic_port_set_net_wbio(port, net_wbio))
1173*e7be843bSPierre Pronchery return 0;
1174*e7be843bSPierre Pronchery
1175*e7be843bSPierre Pronchery BIO_free_all(old_wbio);
1176*e7be843bSPierre Pronchery if (net_wbio != NULL)
1177*e7be843bSPierre Pronchery BIO_set_nbio(net_wbio, 1); /* best effort autoconfig */
1178*e7be843bSPierre Pronchery
1179*e7be843bSPierre Pronchery return 1;
1180*e7be843bSPierre Pronchery }
1181*e7be843bSPierre Pronchery
ossl_quic_conn_set0_net_rbio(SSL * s,BIO * net_rbio)1182*e7be843bSPierre Pronchery void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio)
1183*e7be843bSPierre Pronchery {
1184*e7be843bSPierre Pronchery QCTX ctx;
1185*e7be843bSPierre Pronchery
1186*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1187*e7be843bSPierre Pronchery return;
1188*e7be843bSPierre Pronchery
1189*e7be843bSPierre Pronchery /* Returns 0 if no change. */
1190*e7be843bSPierre Pronchery if (!quic_set0_net_rbio(ctx.obj, net_rbio))
1191*e7be843bSPierre Pronchery return;
1192*e7be843bSPierre Pronchery }
1193*e7be843bSPierre Pronchery
ossl_quic_conn_set0_net_wbio(SSL * s,BIO * net_wbio)1194*e7be843bSPierre Pronchery void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio)
1195*e7be843bSPierre Pronchery {
1196*e7be843bSPierre Pronchery QCTX ctx;
1197*e7be843bSPierre Pronchery
1198*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1199*e7be843bSPierre Pronchery return;
1200*e7be843bSPierre Pronchery
1201*e7be843bSPierre Pronchery /* Returns 0 if no change. */
1202*e7be843bSPierre Pronchery if (!quic_set0_net_wbio(ctx.obj, net_wbio))
1203*e7be843bSPierre Pronchery return;
1204*e7be843bSPierre Pronchery }
1205*e7be843bSPierre Pronchery
ossl_quic_conn_get_net_rbio(const SSL * s)1206*e7be843bSPierre Pronchery BIO *ossl_quic_conn_get_net_rbio(const SSL *s)
1207*e7be843bSPierre Pronchery {
1208*e7be843bSPierre Pronchery QCTX ctx;
1209*e7be843bSPierre Pronchery QUIC_PORT *port;
1210*e7be843bSPierre Pronchery
1211*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1212*e7be843bSPierre Pronchery return NULL;
1213*e7be843bSPierre Pronchery
1214*e7be843bSPierre Pronchery port = ossl_quic_obj_get0_port(ctx.obj);
1215*e7be843bSPierre Pronchery assert(port != NULL);
1216*e7be843bSPierre Pronchery return ossl_quic_port_get_net_rbio(port);
1217*e7be843bSPierre Pronchery }
1218*e7be843bSPierre Pronchery
ossl_quic_conn_get_net_wbio(const SSL * s)1219*e7be843bSPierre Pronchery BIO *ossl_quic_conn_get_net_wbio(const SSL *s)
1220*e7be843bSPierre Pronchery {
1221*e7be843bSPierre Pronchery QCTX ctx;
1222*e7be843bSPierre Pronchery QUIC_PORT *port;
1223*e7be843bSPierre Pronchery
1224*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1225*e7be843bSPierre Pronchery return NULL;
1226*e7be843bSPierre Pronchery
1227*e7be843bSPierre Pronchery port = ossl_quic_obj_get0_port(ctx.obj);
1228*e7be843bSPierre Pronchery assert(port != NULL);
1229*e7be843bSPierre Pronchery return ossl_quic_port_get_net_wbio(port);
1230*e7be843bSPierre Pronchery }
1231*e7be843bSPierre Pronchery
ossl_quic_conn_get_blocking_mode(const SSL * s)1232*e7be843bSPierre Pronchery int ossl_quic_conn_get_blocking_mode(const SSL *s)
1233*e7be843bSPierre Pronchery {
1234*e7be843bSPierre Pronchery QCTX ctx;
1235*e7be843bSPierre Pronchery
1236*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1237*e7be843bSPierre Pronchery return 0;
1238*e7be843bSPierre Pronchery
1239*e7be843bSPierre Pronchery return qctx_blocking(&ctx);
1240*e7be843bSPierre Pronchery }
1241*e7be843bSPierre Pronchery
1242*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_conn_set_blocking_mode(SSL * s,int blocking)1243*e7be843bSPierre Pronchery int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking)
1244*e7be843bSPierre Pronchery {
1245*e7be843bSPierre Pronchery int ret = 0;
1246*e7be843bSPierre Pronchery unsigned int mode;
1247*e7be843bSPierre Pronchery QCTX ctx;
1248*e7be843bSPierre Pronchery
1249*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1250*e7be843bSPierre Pronchery return 0;
1251*e7be843bSPierre Pronchery
1252*e7be843bSPierre Pronchery qctx_lock(&ctx);
1253*e7be843bSPierre Pronchery
1254*e7be843bSPierre Pronchery /* Sanity check - can we support the request given the current network BIO? */
1255*e7be843bSPierre Pronchery if (blocking) {
1256*e7be843bSPierre Pronchery /*
1257*e7be843bSPierre Pronchery * If called directly on a top-level object (QCSO or QLSO), update our
1258*e7be843bSPierre Pronchery * information on network BIO capabilities.
1259*e7be843bSPierre Pronchery */
1260*e7be843bSPierre Pronchery if (qctx_is_top_level(&ctx))
1261*e7be843bSPierre Pronchery ossl_quic_engine_update_poll_descriptors(ctx.obj->engine, /*force=*/1);
1262*e7be843bSPierre Pronchery
1263*e7be843bSPierre Pronchery /* Cannot enable blocking mode if we do not have pollable FDs. */
1264*e7be843bSPierre Pronchery if (!ossl_quic_obj_can_support_blocking(ctx.obj)) {
1265*e7be843bSPierre Pronchery ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1266*e7be843bSPierre Pronchery goto out;
1267*e7be843bSPierre Pronchery }
1268*e7be843bSPierre Pronchery }
1269*e7be843bSPierre Pronchery
1270*e7be843bSPierre Pronchery mode = (blocking != 0)
1271*e7be843bSPierre Pronchery ? QUIC_BLOCKING_MODE_BLOCKING
1272*e7be843bSPierre Pronchery : QUIC_BLOCKING_MODE_NONBLOCKING;
1273*e7be843bSPierre Pronchery
1274*e7be843bSPierre Pronchery ossl_quic_obj_set_blocking_mode(ctx.obj, mode);
1275*e7be843bSPierre Pronchery
1276*e7be843bSPierre Pronchery ret = 1;
1277*e7be843bSPierre Pronchery out:
1278*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1279*e7be843bSPierre Pronchery return ret;
1280*e7be843bSPierre Pronchery }
1281*e7be843bSPierre Pronchery
ossl_quic_conn_set_initial_peer_addr(SSL * s,const BIO_ADDR * peer_addr)1282*e7be843bSPierre Pronchery int ossl_quic_conn_set_initial_peer_addr(SSL *s,
1283*e7be843bSPierre Pronchery const BIO_ADDR *peer_addr)
1284*e7be843bSPierre Pronchery {
1285*e7be843bSPierre Pronchery QCTX ctx;
1286*e7be843bSPierre Pronchery
1287*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
1288*e7be843bSPierre Pronchery return 0;
1289*e7be843bSPierre Pronchery
1290*e7be843bSPierre Pronchery if (ctx.qc->started)
1291*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
1292*e7be843bSPierre Pronchery NULL);
1293*e7be843bSPierre Pronchery
1294*e7be843bSPierre Pronchery if (peer_addr == NULL) {
1295*e7be843bSPierre Pronchery BIO_ADDR_clear(&ctx.qc->init_peer_addr);
1296*e7be843bSPierre Pronchery return 1;
1297*e7be843bSPierre Pronchery }
1298*e7be843bSPierre Pronchery
1299*e7be843bSPierre Pronchery return BIO_ADDR_copy(&ctx.qc->init_peer_addr, peer_addr);
1300*e7be843bSPierre Pronchery }
1301*e7be843bSPierre Pronchery
1302*e7be843bSPierre Pronchery /*
1303*e7be843bSPierre Pronchery * QUIC Front-End I/O API: Asynchronous I/O Management
1304*e7be843bSPierre Pronchery * ===================================================
1305*e7be843bSPierre Pronchery *
1306*e7be843bSPierre Pronchery * (BIO/)SSL_handle_events => ossl_quic_handle_events
1307*e7be843bSPierre Pronchery * (BIO/)SSL_get_event_timeout => ossl_quic_get_event_timeout
1308*e7be843bSPierre Pronchery * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
1309*e7be843bSPierre Pronchery *
1310*e7be843bSPierre Pronchery */
1311*e7be843bSPierre Pronchery
1312*e7be843bSPierre Pronchery /* SSL_handle_events; performs QUIC I/O and timeout processing. */
1313*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_handle_events(SSL * s)1314*e7be843bSPierre Pronchery int ossl_quic_handle_events(SSL *s)
1315*e7be843bSPierre Pronchery {
1316*e7be843bSPierre Pronchery QCTX ctx;
1317*e7be843bSPierre Pronchery
1318*e7be843bSPierre Pronchery if (!expect_quic_any(s, &ctx))
1319*e7be843bSPierre Pronchery return 0;
1320*e7be843bSPierre Pronchery
1321*e7be843bSPierre Pronchery qctx_lock(&ctx);
1322*e7be843bSPierre Pronchery ossl_quic_reactor_tick(ossl_quic_obj_get0_reactor(ctx.obj), 0);
1323*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1324*e7be843bSPierre Pronchery return 1;
1325*e7be843bSPierre Pronchery }
1326*e7be843bSPierre Pronchery
1327*e7be843bSPierre Pronchery /*
1328*e7be843bSPierre Pronchery * SSL_get_event_timeout. Get the time in milliseconds until the SSL object
1329*e7be843bSPierre Pronchery * should next have events handled by the application by calling
1330*e7be843bSPierre Pronchery * SSL_handle_events(). tv is set to 0 if the object should have events handled
1331*e7be843bSPierre Pronchery * immediately. If no timeout is currently active, *is_infinite is set to 1 and
1332*e7be843bSPierre Pronchery * the value of *tv is undefined.
1333*e7be843bSPierre Pronchery */
1334*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_get_event_timeout(SSL * s,struct timeval * tv,int * is_infinite)1335*e7be843bSPierre Pronchery int ossl_quic_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
1336*e7be843bSPierre Pronchery {
1337*e7be843bSPierre Pronchery QCTX ctx;
1338*e7be843bSPierre Pronchery QUIC_REACTOR *reactor;
1339*e7be843bSPierre Pronchery OSSL_TIME deadline;
1340*e7be843bSPierre Pronchery OSSL_TIME basetime;
1341*e7be843bSPierre Pronchery
1342*e7be843bSPierre Pronchery if (!expect_quic_any(s, &ctx))
1343*e7be843bSPierre Pronchery return 0;
1344*e7be843bSPierre Pronchery
1345*e7be843bSPierre Pronchery qctx_lock(&ctx);
1346*e7be843bSPierre Pronchery
1347*e7be843bSPierre Pronchery reactor = ossl_quic_obj_get0_reactor(ctx.obj);
1348*e7be843bSPierre Pronchery deadline = ossl_quic_reactor_get_tick_deadline(reactor);
1349*e7be843bSPierre Pronchery
1350*e7be843bSPierre Pronchery if (ossl_time_is_infinite(deadline)) {
1351*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1352*e7be843bSPierre Pronchery *is_infinite = 1;
1353*e7be843bSPierre Pronchery
1354*e7be843bSPierre Pronchery /*
1355*e7be843bSPierre Pronchery * Robustness against faulty applications that don't check *is_infinite;
1356*e7be843bSPierre Pronchery * harmless long timeout.
1357*e7be843bSPierre Pronchery */
1358*e7be843bSPierre Pronchery tv->tv_sec = 1000000;
1359*e7be843bSPierre Pronchery tv->tv_usec = 0;
1360*e7be843bSPierre Pronchery return 1;
1361*e7be843bSPierre Pronchery }
1362*e7be843bSPierre Pronchery
1363*e7be843bSPierre Pronchery basetime = ossl_quic_engine_get_time(ctx.obj->engine);
1364*e7be843bSPierre Pronchery
1365*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1366*e7be843bSPierre Pronchery
1367*e7be843bSPierre Pronchery *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, basetime));
1368*e7be843bSPierre Pronchery *is_infinite = 0;
1369*e7be843bSPierre Pronchery
1370*e7be843bSPierre Pronchery return 1;
1371*e7be843bSPierre Pronchery }
1372*e7be843bSPierre Pronchery
1373*e7be843bSPierre Pronchery /* SSL_get_rpoll_descriptor */
ossl_quic_get_rpoll_descriptor(SSL * s,BIO_POLL_DESCRIPTOR * desc)1374*e7be843bSPierre Pronchery int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1375*e7be843bSPierre Pronchery {
1376*e7be843bSPierre Pronchery QCTX ctx;
1377*e7be843bSPierre Pronchery QUIC_PORT *port = NULL;
1378*e7be843bSPierre Pronchery BIO *net_rbio;
1379*e7be843bSPierre Pronchery
1380*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1381*e7be843bSPierre Pronchery return 0;
1382*e7be843bSPierre Pronchery
1383*e7be843bSPierre Pronchery port = ossl_quic_obj_get0_port(ctx.obj);
1384*e7be843bSPierre Pronchery net_rbio = ossl_quic_port_get_net_rbio(port);
1385*e7be843bSPierre Pronchery if (desc == NULL || net_rbio == NULL)
1386*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1387*e7be843bSPierre Pronchery NULL);
1388*e7be843bSPierre Pronchery
1389*e7be843bSPierre Pronchery return BIO_get_rpoll_descriptor(net_rbio, desc);
1390*e7be843bSPierre Pronchery }
1391*e7be843bSPierre Pronchery
1392*e7be843bSPierre Pronchery /* SSL_get_wpoll_descriptor */
ossl_quic_get_wpoll_descriptor(SSL * s,BIO_POLL_DESCRIPTOR * desc)1393*e7be843bSPierre Pronchery int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
1394*e7be843bSPierre Pronchery {
1395*e7be843bSPierre Pronchery QCTX ctx;
1396*e7be843bSPierre Pronchery QUIC_PORT *port = NULL;
1397*e7be843bSPierre Pronchery BIO *net_wbio;
1398*e7be843bSPierre Pronchery
1399*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1400*e7be843bSPierre Pronchery return 0;
1401*e7be843bSPierre Pronchery
1402*e7be843bSPierre Pronchery port = ossl_quic_obj_get0_port(ctx.obj);
1403*e7be843bSPierre Pronchery net_wbio = ossl_quic_port_get_net_wbio(port);
1404*e7be843bSPierre Pronchery if (desc == NULL || net_wbio == NULL)
1405*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
1406*e7be843bSPierre Pronchery NULL);
1407*e7be843bSPierre Pronchery
1408*e7be843bSPierre Pronchery return BIO_get_wpoll_descriptor(net_wbio, desc);
1409*e7be843bSPierre Pronchery }
1410*e7be843bSPierre Pronchery
1411*e7be843bSPierre Pronchery /* SSL_net_read_desired */
1412*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_get_net_read_desired(SSL * s)1413*e7be843bSPierre Pronchery int ossl_quic_get_net_read_desired(SSL *s)
1414*e7be843bSPierre Pronchery {
1415*e7be843bSPierre Pronchery QCTX ctx;
1416*e7be843bSPierre Pronchery int ret;
1417*e7be843bSPierre Pronchery
1418*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1419*e7be843bSPierre Pronchery return 0;
1420*e7be843bSPierre Pronchery
1421*e7be843bSPierre Pronchery qctx_lock(&ctx);
1422*e7be843bSPierre Pronchery ret = ossl_quic_reactor_net_read_desired(ossl_quic_obj_get0_reactor(ctx.obj));
1423*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1424*e7be843bSPierre Pronchery return ret;
1425*e7be843bSPierre Pronchery }
1426*e7be843bSPierre Pronchery
1427*e7be843bSPierre Pronchery /* SSL_net_write_desired */
1428*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_get_net_write_desired(SSL * s)1429*e7be843bSPierre Pronchery int ossl_quic_get_net_write_desired(SSL *s)
1430*e7be843bSPierre Pronchery {
1431*e7be843bSPierre Pronchery int ret;
1432*e7be843bSPierre Pronchery QCTX ctx;
1433*e7be843bSPierre Pronchery
1434*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1435*e7be843bSPierre Pronchery return 0;
1436*e7be843bSPierre Pronchery
1437*e7be843bSPierre Pronchery qctx_lock(&ctx);
1438*e7be843bSPierre Pronchery ret = ossl_quic_reactor_net_write_desired(ossl_quic_obj_get0_reactor(ctx.obj));
1439*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1440*e7be843bSPierre Pronchery return ret;
1441*e7be843bSPierre Pronchery }
1442*e7be843bSPierre Pronchery
1443*e7be843bSPierre Pronchery /*
1444*e7be843bSPierre Pronchery * QUIC Front-End I/O API: Connection Lifecycle Operations
1445*e7be843bSPierre Pronchery * =======================================================
1446*e7be843bSPierre Pronchery *
1447*e7be843bSPierre Pronchery * SSL_do_handshake => ossl_quic_do_handshake
1448*e7be843bSPierre Pronchery * SSL_set_connect_state => ossl_quic_set_connect_state
1449*e7be843bSPierre Pronchery * SSL_set_accept_state => ossl_quic_set_accept_state
1450*e7be843bSPierre Pronchery * SSL_shutdown => ossl_quic_shutdown
1451*e7be843bSPierre Pronchery * SSL_ctrl => ossl_quic_ctrl
1452*e7be843bSPierre Pronchery * (BIO/)SSL_connect => ossl_quic_connect
1453*e7be843bSPierre Pronchery * (BIO/)SSL_accept => ossl_quic_accept
1454*e7be843bSPierre Pronchery *
1455*e7be843bSPierre Pronchery */
1456*e7be843bSPierre Pronchery
1457*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_shutdown_flush_init(QUIC_CONNECTION * qc)1458*e7be843bSPierre Pronchery static void qc_shutdown_flush_init(QUIC_CONNECTION *qc)
1459*e7be843bSPierre Pronchery {
1460*e7be843bSPierre Pronchery QUIC_STREAM_MAP *qsm;
1461*e7be843bSPierre Pronchery
1462*e7be843bSPierre Pronchery if (qc->shutting_down)
1463*e7be843bSPierre Pronchery return;
1464*e7be843bSPierre Pronchery
1465*e7be843bSPierre Pronchery qsm = ossl_quic_channel_get_qsm(qc->ch);
1466*e7be843bSPierre Pronchery
1467*e7be843bSPierre Pronchery ossl_quic_stream_map_begin_shutdown_flush(qsm);
1468*e7be843bSPierre Pronchery qc->shutting_down = 1;
1469*e7be843bSPierre Pronchery }
1470*e7be843bSPierre Pronchery
1471*e7be843bSPierre Pronchery /* Returns 1 if all shutdown-flush streams have been done with. */
1472*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_shutdown_flush_finished(QUIC_CONNECTION * qc)1473*e7be843bSPierre Pronchery static int qc_shutdown_flush_finished(QUIC_CONNECTION *qc)
1474*e7be843bSPierre Pronchery {
1475*e7be843bSPierre Pronchery QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
1476*e7be843bSPierre Pronchery
1477*e7be843bSPierre Pronchery return qc->shutting_down
1478*e7be843bSPierre Pronchery && ossl_quic_stream_map_is_shutdown_flush_finished(qsm);
1479*e7be843bSPierre Pronchery }
1480*e7be843bSPierre Pronchery
1481*e7be843bSPierre Pronchery /* SSL_shutdown */
quic_shutdown_wait(void * arg)1482*e7be843bSPierre Pronchery static int quic_shutdown_wait(void *arg)
1483*e7be843bSPierre Pronchery {
1484*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = arg;
1485*e7be843bSPierre Pronchery
1486*e7be843bSPierre Pronchery return ossl_quic_channel_is_terminated(qc->ch);
1487*e7be843bSPierre Pronchery }
1488*e7be843bSPierre Pronchery
1489*e7be843bSPierre Pronchery /* Returns 1 if shutdown flush process has finished or is inapplicable. */
quic_shutdown_flush_wait(void * arg)1490*e7be843bSPierre Pronchery static int quic_shutdown_flush_wait(void *arg)
1491*e7be843bSPierre Pronchery {
1492*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = arg;
1493*e7be843bSPierre Pronchery
1494*e7be843bSPierre Pronchery return ossl_quic_channel_is_term_any(qc->ch)
1495*e7be843bSPierre Pronchery || qc_shutdown_flush_finished(qc);
1496*e7be843bSPierre Pronchery }
1497*e7be843bSPierre Pronchery
quic_shutdown_peer_wait(void * arg)1498*e7be843bSPierre Pronchery static int quic_shutdown_peer_wait(void *arg)
1499*e7be843bSPierre Pronchery {
1500*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = arg;
1501*e7be843bSPierre Pronchery return ossl_quic_channel_is_term_any(qc->ch);
1502*e7be843bSPierre Pronchery }
1503*e7be843bSPierre Pronchery
1504*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_conn_shutdown(SSL * s,uint64_t flags,const SSL_SHUTDOWN_EX_ARGS * args,size_t args_len)1505*e7be843bSPierre Pronchery int ossl_quic_conn_shutdown(SSL *s, uint64_t flags,
1506*e7be843bSPierre Pronchery const SSL_SHUTDOWN_EX_ARGS *args,
1507*e7be843bSPierre Pronchery size_t args_len)
1508*e7be843bSPierre Pronchery {
1509*e7be843bSPierre Pronchery int ret;
1510*e7be843bSPierre Pronchery QCTX ctx;
1511*e7be843bSPierre Pronchery int stream_flush = ((flags & SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH) == 0);
1512*e7be843bSPierre Pronchery int no_block = ((flags & SSL_SHUTDOWN_FLAG_NO_BLOCK) != 0);
1513*e7be843bSPierre Pronchery int wait_peer = ((flags & SSL_SHUTDOWN_FLAG_WAIT_PEER) != 0);
1514*e7be843bSPierre Pronchery
1515*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
1516*e7be843bSPierre Pronchery return -1;
1517*e7be843bSPierre Pronchery
1518*e7be843bSPierre Pronchery if (ctx.is_stream) {
1519*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_CONN_USE_ONLY, NULL);
1520*e7be843bSPierre Pronchery return -1;
1521*e7be843bSPierre Pronchery }
1522*e7be843bSPierre Pronchery
1523*e7be843bSPierre Pronchery qctx_lock(&ctx);
1524*e7be843bSPierre Pronchery
1525*e7be843bSPierre Pronchery if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1526*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1527*e7be843bSPierre Pronchery return 1;
1528*e7be843bSPierre Pronchery }
1529*e7be843bSPierre Pronchery
1530*e7be843bSPierre Pronchery /* Phase 1: Stream Flushing */
1531*e7be843bSPierre Pronchery if (!wait_peer && stream_flush) {
1532*e7be843bSPierre Pronchery qc_shutdown_flush_init(ctx.qc);
1533*e7be843bSPierre Pronchery
1534*e7be843bSPierre Pronchery if (!qc_shutdown_flush_finished(ctx.qc)) {
1535*e7be843bSPierre Pronchery if (!no_block && qctx_blocking(&ctx)) {
1536*e7be843bSPierre Pronchery ret = block_until_pred(&ctx, quic_shutdown_flush_wait, ctx.qc, 0);
1537*e7be843bSPierre Pronchery if (ret < 1) {
1538*e7be843bSPierre Pronchery ret = 0;
1539*e7be843bSPierre Pronchery goto err;
1540*e7be843bSPierre Pronchery }
1541*e7be843bSPierre Pronchery } else {
1542*e7be843bSPierre Pronchery qctx_maybe_autotick(&ctx);
1543*e7be843bSPierre Pronchery }
1544*e7be843bSPierre Pronchery }
1545*e7be843bSPierre Pronchery
1546*e7be843bSPierre Pronchery if (!qc_shutdown_flush_finished(ctx.qc)) {
1547*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1548*e7be843bSPierre Pronchery return 0; /* ongoing */
1549*e7be843bSPierre Pronchery }
1550*e7be843bSPierre Pronchery }
1551*e7be843bSPierre Pronchery
1552*e7be843bSPierre Pronchery /* Phase 2: Connection Closure */
1553*e7be843bSPierre Pronchery if (wait_peer && !ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1554*e7be843bSPierre Pronchery if (!no_block && qctx_blocking(&ctx)) {
1555*e7be843bSPierre Pronchery ret = block_until_pred(&ctx, quic_shutdown_peer_wait, ctx.qc, 0);
1556*e7be843bSPierre Pronchery if (ret < 1) {
1557*e7be843bSPierre Pronchery ret = 0;
1558*e7be843bSPierre Pronchery goto err;
1559*e7be843bSPierre Pronchery }
1560*e7be843bSPierre Pronchery } else {
1561*e7be843bSPierre Pronchery qctx_maybe_autotick(&ctx);
1562*e7be843bSPierre Pronchery }
1563*e7be843bSPierre Pronchery
1564*e7be843bSPierre Pronchery if (!ossl_quic_channel_is_term_any(ctx.qc->ch)) {
1565*e7be843bSPierre Pronchery ret = 0; /* peer hasn't closed yet - still not done */
1566*e7be843bSPierre Pronchery goto err;
1567*e7be843bSPierre Pronchery }
1568*e7be843bSPierre Pronchery
1569*e7be843bSPierre Pronchery /*
1570*e7be843bSPierre Pronchery * We are at least terminating - go through the normal process of
1571*e7be843bSPierre Pronchery * waiting until we are in the TERMINATED state.
1572*e7be843bSPierre Pronchery */
1573*e7be843bSPierre Pronchery }
1574*e7be843bSPierre Pronchery
1575*e7be843bSPierre Pronchery /* Block mutation ops regardless of if we did stream flush. */
1576*e7be843bSPierre Pronchery ctx.qc->shutting_down = 1;
1577*e7be843bSPierre Pronchery
1578*e7be843bSPierre Pronchery /*
1579*e7be843bSPierre Pronchery * This call is a no-op if we are already terminating, so it doesn't
1580*e7be843bSPierre Pronchery * affect the wait_peer case.
1581*e7be843bSPierre Pronchery */
1582*e7be843bSPierre Pronchery ossl_quic_channel_local_close(ctx.qc->ch,
1583*e7be843bSPierre Pronchery args != NULL ? args->quic_error_code : 0,
1584*e7be843bSPierre Pronchery args != NULL ? args->quic_reason : NULL);
1585*e7be843bSPierre Pronchery
1586*e7be843bSPierre Pronchery SSL_set_shutdown(ctx.qc->tls, SSL_SENT_SHUTDOWN);
1587*e7be843bSPierre Pronchery
1588*e7be843bSPierre Pronchery if (ossl_quic_channel_is_terminated(ctx.qc->ch)) {
1589*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1590*e7be843bSPierre Pronchery return 1;
1591*e7be843bSPierre Pronchery }
1592*e7be843bSPierre Pronchery
1593*e7be843bSPierre Pronchery /* Phase 3: Terminating Wait Time */
1594*e7be843bSPierre Pronchery if (!no_block && qctx_blocking(&ctx)
1595*e7be843bSPierre Pronchery && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) {
1596*e7be843bSPierre Pronchery ret = block_until_pred(&ctx, quic_shutdown_wait, ctx.qc, 0);
1597*e7be843bSPierre Pronchery if (ret < 1) {
1598*e7be843bSPierre Pronchery ret = 0;
1599*e7be843bSPierre Pronchery goto err;
1600*e7be843bSPierre Pronchery }
1601*e7be843bSPierre Pronchery } else {
1602*e7be843bSPierre Pronchery qctx_maybe_autotick(&ctx);
1603*e7be843bSPierre Pronchery }
1604*e7be843bSPierre Pronchery
1605*e7be843bSPierre Pronchery ret = ossl_quic_channel_is_terminated(ctx.qc->ch);
1606*e7be843bSPierre Pronchery err:
1607*e7be843bSPierre Pronchery qctx_unlock(&ctx);
1608*e7be843bSPierre Pronchery return ret;
1609*e7be843bSPierre Pronchery }
1610*e7be843bSPierre Pronchery
1611*e7be843bSPierre Pronchery /* SSL_ctrl */
ossl_quic_ctrl(SSL * s,int cmd,long larg,void * parg)1612*e7be843bSPierre Pronchery long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
1613*e7be843bSPierre Pronchery {
1614*e7be843bSPierre Pronchery QCTX ctx;
1615*e7be843bSPierre Pronchery
1616*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
1617*e7be843bSPierre Pronchery return 0;
1618*e7be843bSPierre Pronchery
1619*e7be843bSPierre Pronchery switch (cmd) {
1620*e7be843bSPierre Pronchery case SSL_CTRL_MODE:
1621*e7be843bSPierre Pronchery if (ctx.is_listener)
1622*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1623*e7be843bSPierre Pronchery
1624*e7be843bSPierre Pronchery /* If called on a QCSO, update the default mode. */
1625*e7be843bSPierre Pronchery if (!ctx.is_stream)
1626*e7be843bSPierre Pronchery ctx.qc->default_ssl_mode |= (uint32_t)larg;
1627*e7be843bSPierre Pronchery
1628*e7be843bSPierre Pronchery /*
1629*e7be843bSPierre Pronchery * If we were called on a QSSO or have a default stream, we also update
1630*e7be843bSPierre Pronchery * that.
1631*e7be843bSPierre Pronchery */
1632*e7be843bSPierre Pronchery if (ctx.xso != NULL) {
1633*e7be843bSPierre Pronchery /* Cannot enable EPW while AON write in progress. */
1634*e7be843bSPierre Pronchery if (ctx.xso->aon_write_in_progress)
1635*e7be843bSPierre Pronchery larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
1636*e7be843bSPierre Pronchery
1637*e7be843bSPierre Pronchery ctx.xso->ssl_mode |= (uint32_t)larg;
1638*e7be843bSPierre Pronchery return ctx.xso->ssl_mode;
1639*e7be843bSPierre Pronchery }
1640*e7be843bSPierre Pronchery
1641*e7be843bSPierre Pronchery return ctx.qc->default_ssl_mode;
1642*e7be843bSPierre Pronchery case SSL_CTRL_CLEAR_MODE:
1643*e7be843bSPierre Pronchery if (ctx.is_listener)
1644*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1645*e7be843bSPierre Pronchery
1646*e7be843bSPierre Pronchery if (!ctx.is_stream)
1647*e7be843bSPierre Pronchery ctx.qc->default_ssl_mode &= ~(uint32_t)larg;
1648*e7be843bSPierre Pronchery
1649*e7be843bSPierre Pronchery if (ctx.xso != NULL) {
1650*e7be843bSPierre Pronchery ctx.xso->ssl_mode &= ~(uint32_t)larg;
1651*e7be843bSPierre Pronchery return ctx.xso->ssl_mode;
1652*e7be843bSPierre Pronchery }
1653*e7be843bSPierre Pronchery
1654*e7be843bSPierre Pronchery return ctx.qc->default_ssl_mode;
1655*e7be843bSPierre Pronchery
1656*e7be843bSPierre Pronchery case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1657*e7be843bSPierre Pronchery if (ctx.is_listener)
1658*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1659*e7be843bSPierre Pronchery
1660*e7be843bSPierre Pronchery ossl_quic_channel_set_msg_callback_arg(ctx.qc->ch, parg);
1661*e7be843bSPierre Pronchery /* This ctrl also needs to be passed to the internal SSL object */
1662*e7be843bSPierre Pronchery return SSL_ctrl(ctx.qc->tls, cmd, larg, parg);
1663*e7be843bSPierre Pronchery
1664*e7be843bSPierre Pronchery case DTLS_CTRL_GET_TIMEOUT: /* DTLSv1_get_timeout */
1665*e7be843bSPierre Pronchery {
1666*e7be843bSPierre Pronchery int is_infinite;
1667*e7be843bSPierre Pronchery
1668*e7be843bSPierre Pronchery if (!ossl_quic_get_event_timeout(s, parg, &is_infinite))
1669*e7be843bSPierre Pronchery return 0;
1670*e7be843bSPierre Pronchery
1671*e7be843bSPierre Pronchery return !is_infinite;
1672*e7be843bSPierre Pronchery }
1673*e7be843bSPierre Pronchery case DTLS_CTRL_HANDLE_TIMEOUT: /* DTLSv1_handle_timeout */
1674*e7be843bSPierre Pronchery /* For legacy compatibility with DTLS calls. */
1675*e7be843bSPierre Pronchery return ossl_quic_handle_events(s) == 1 ? 1 : -1;
1676*e7be843bSPierre Pronchery
1677*e7be843bSPierre Pronchery /* Mask ctrls we shouldn't support for QUIC. */
1678*e7be843bSPierre Pronchery case SSL_CTRL_GET_READ_AHEAD:
1679*e7be843bSPierre Pronchery case SSL_CTRL_SET_READ_AHEAD:
1680*e7be843bSPierre Pronchery case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1681*e7be843bSPierre Pronchery case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
1682*e7be843bSPierre Pronchery case SSL_CTRL_SET_MAX_PIPELINES:
1683*e7be843bSPierre Pronchery return 0;
1684*e7be843bSPierre Pronchery
1685*e7be843bSPierre Pronchery default:
1686*e7be843bSPierre Pronchery /*
1687*e7be843bSPierre Pronchery * Probably a TLS related ctrl. Send back to the frontend SSL_ctrl
1688*e7be843bSPierre Pronchery * implementation. Either SSL_ctrl will handle it itself by direct
1689*e7be843bSPierre Pronchery * access into handshake layer state, or failing that, it will be passed
1690*e7be843bSPierre Pronchery * to the handshake layer via the SSL_METHOD vtable. If the ctrl is not
1691*e7be843bSPierre Pronchery * supported by anything, the handshake layer's ctrl method will finally
1692*e7be843bSPierre Pronchery * return 0.
1693*e7be843bSPierre Pronchery */
1694*e7be843bSPierre Pronchery if (ctx.is_listener)
1695*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
1696*e7be843bSPierre Pronchery
1697*e7be843bSPierre Pronchery return ossl_ctrl_internal(&ctx.qc->obj.ssl, cmd, larg, parg, /*no_quic=*/1);
1698*e7be843bSPierre Pronchery }
1699*e7be843bSPierre Pronchery }
1700*e7be843bSPierre Pronchery
1701*e7be843bSPierre Pronchery /* SSL_set_connect_state */
ossl_quic_set_connect_state(SSL * s,int raiseerrs)1702*e7be843bSPierre Pronchery int ossl_quic_set_connect_state(SSL *s, int raiseerrs)
1703*e7be843bSPierre Pronchery {
1704*e7be843bSPierre Pronchery QCTX ctx;
1705*e7be843bSPierre Pronchery
1706*e7be843bSPierre Pronchery if (!is_quic_c(s, &ctx, raiseerrs))
1707*e7be843bSPierre Pronchery return 0;
1708*e7be843bSPierre Pronchery
1709*e7be843bSPierre Pronchery if (ctx.qc->as_server_state == 0)
1710*e7be843bSPierre Pronchery return 1;
1711*e7be843bSPierre Pronchery
1712*e7be843bSPierre Pronchery /* Cannot be changed after handshake started */
1713*e7be843bSPierre Pronchery if (ctx.qc->started) {
1714*e7be843bSPierre Pronchery if (raiseerrs)
1715*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, SSL_R_INVALID_COMMAND, NULL);
1716*e7be843bSPierre Pronchery return 0;
1717*e7be843bSPierre Pronchery }
1718*e7be843bSPierre Pronchery
1719*e7be843bSPierre Pronchery ctx.qc->as_server_state = 0;
1720*e7be843bSPierre Pronchery return 1;
1721*e7be843bSPierre Pronchery }
1722*e7be843bSPierre Pronchery
1723*e7be843bSPierre Pronchery /* SSL_set_accept_state */
ossl_quic_set_accept_state(SSL * s,int raiseerrs)1724*e7be843bSPierre Pronchery int ossl_quic_set_accept_state(SSL *s, int raiseerrs)
1725*e7be843bSPierre Pronchery {
1726*e7be843bSPierre Pronchery QCTX ctx;
1727*e7be843bSPierre Pronchery
1728*e7be843bSPierre Pronchery if (!is_quic_c(s, &ctx, raiseerrs))
1729*e7be843bSPierre Pronchery return 0;
1730*e7be843bSPierre Pronchery
1731*e7be843bSPierre Pronchery if (ctx.qc->as_server_state == 1)
1732*e7be843bSPierre Pronchery return 1;
1733*e7be843bSPierre Pronchery
1734*e7be843bSPierre Pronchery /* Cannot be changed after handshake started */
1735*e7be843bSPierre Pronchery if (ctx.qc->started) {
1736*e7be843bSPierre Pronchery if (raiseerrs)
1737*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, SSL_R_INVALID_COMMAND, NULL);
1738*e7be843bSPierre Pronchery return 0;
1739*e7be843bSPierre Pronchery }
1740*e7be843bSPierre Pronchery
1741*e7be843bSPierre Pronchery ctx.qc->as_server_state = 1;
1742*e7be843bSPierre Pronchery return 1;
1743*e7be843bSPierre Pronchery }
1744*e7be843bSPierre Pronchery
1745*e7be843bSPierre Pronchery /* SSL_do_handshake */
1746*e7be843bSPierre Pronchery struct quic_handshake_wait_args {
1747*e7be843bSPierre Pronchery QUIC_CONNECTION *qc;
1748*e7be843bSPierre Pronchery };
1749*e7be843bSPierre Pronchery
tls_wants_non_io_retry(QUIC_CONNECTION * qc)1750*e7be843bSPierre Pronchery static int tls_wants_non_io_retry(QUIC_CONNECTION *qc)
1751*e7be843bSPierre Pronchery {
1752*e7be843bSPierre Pronchery int want = SSL_want(qc->tls);
1753*e7be843bSPierre Pronchery
1754*e7be843bSPierre Pronchery if (want == SSL_X509_LOOKUP
1755*e7be843bSPierre Pronchery || want == SSL_CLIENT_HELLO_CB
1756*e7be843bSPierre Pronchery || want == SSL_RETRY_VERIFY)
1757*e7be843bSPierre Pronchery return 1;
1758*e7be843bSPierre Pronchery
1759*e7be843bSPierre Pronchery return 0;
1760*e7be843bSPierre Pronchery }
1761*e7be843bSPierre Pronchery
quic_handshake_wait(void * arg)1762*e7be843bSPierre Pronchery static int quic_handshake_wait(void *arg)
1763*e7be843bSPierre Pronchery {
1764*e7be843bSPierre Pronchery struct quic_handshake_wait_args *args = arg;
1765*e7be843bSPierre Pronchery
1766*e7be843bSPierre Pronchery if (!quic_mutation_allowed(args->qc, /*req_active=*/1))
1767*e7be843bSPierre Pronchery return -1;
1768*e7be843bSPierre Pronchery
1769*e7be843bSPierre Pronchery if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
1770*e7be843bSPierre Pronchery return 1;
1771*e7be843bSPierre Pronchery
1772*e7be843bSPierre Pronchery if (tls_wants_non_io_retry(args->qc))
1773*e7be843bSPierre Pronchery return 1;
1774*e7be843bSPierre Pronchery
1775*e7be843bSPierre Pronchery return 0;
1776*e7be843bSPierre Pronchery }
1777*e7be843bSPierre Pronchery
configure_channel(QUIC_CONNECTION * qc)1778*e7be843bSPierre Pronchery static int configure_channel(QUIC_CONNECTION *qc)
1779*e7be843bSPierre Pronchery {
1780*e7be843bSPierre Pronchery assert(qc->ch != NULL);
1781*e7be843bSPierre Pronchery
1782*e7be843bSPierre Pronchery if (!ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
1783*e7be843bSPierre Pronchery return 0;
1784*e7be843bSPierre Pronchery
1785*e7be843bSPierre Pronchery return 1;
1786*e7be843bSPierre Pronchery }
1787*e7be843bSPierre Pronchery
need_notifier_for_domain_flags(uint64_t domain_flags)1788*e7be843bSPierre Pronchery static int need_notifier_for_domain_flags(uint64_t domain_flags)
1789*e7be843bSPierre Pronchery {
1790*e7be843bSPierre Pronchery return (domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0
1791*e7be843bSPierre Pronchery || ((domain_flags & SSL_DOMAIN_FLAG_MULTI_THREAD) != 0
1792*e7be843bSPierre Pronchery && (domain_flags & SSL_DOMAIN_FLAG_BLOCKING) != 0);
1793*e7be843bSPierre Pronchery }
1794*e7be843bSPierre Pronchery
1795*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
create_channel(QUIC_CONNECTION * qc,SSL_CTX * ctx)1796*e7be843bSPierre Pronchery static int create_channel(QUIC_CONNECTION *qc, SSL_CTX *ctx)
1797*e7be843bSPierre Pronchery {
1798*e7be843bSPierre Pronchery QUIC_ENGINE_ARGS engine_args = {0};
1799*e7be843bSPierre Pronchery QUIC_PORT_ARGS port_args = {0};
1800*e7be843bSPierre Pronchery
1801*e7be843bSPierre Pronchery engine_args.libctx = ctx->libctx;
1802*e7be843bSPierre Pronchery engine_args.propq = ctx->propq;
1803*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
1804*e7be843bSPierre Pronchery engine_args.mutex = qc->mutex;
1805*e7be843bSPierre Pronchery #endif
1806*e7be843bSPierre Pronchery
1807*e7be843bSPierre Pronchery if (need_notifier_for_domain_flags(ctx->domain_flags))
1808*e7be843bSPierre Pronchery engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
1809*e7be843bSPierre Pronchery
1810*e7be843bSPierre Pronchery qc->engine = ossl_quic_engine_new(&engine_args);
1811*e7be843bSPierre Pronchery if (qc->engine == NULL) {
1812*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1813*e7be843bSPierre Pronchery return 0;
1814*e7be843bSPierre Pronchery }
1815*e7be843bSPierre Pronchery
1816*e7be843bSPierre Pronchery port_args.channel_ctx = ctx;
1817*e7be843bSPierre Pronchery qc->port = ossl_quic_engine_create_port(qc->engine, &port_args);
1818*e7be843bSPierre Pronchery if (qc->port == NULL) {
1819*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1820*e7be843bSPierre Pronchery ossl_quic_engine_free(qc->engine);
1821*e7be843bSPierre Pronchery return 0;
1822*e7be843bSPierre Pronchery }
1823*e7be843bSPierre Pronchery
1824*e7be843bSPierre Pronchery qc->ch = ossl_quic_port_create_outgoing(qc->port, qc->tls);
1825*e7be843bSPierre Pronchery if (qc->ch == NULL) {
1826*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
1827*e7be843bSPierre Pronchery ossl_quic_port_free(qc->port);
1828*e7be843bSPierre Pronchery ossl_quic_engine_free(qc->engine);
1829*e7be843bSPierre Pronchery return 0;
1830*e7be843bSPierre Pronchery }
1831*e7be843bSPierre Pronchery
1832*e7be843bSPierre Pronchery return 1;
1833*e7be843bSPierre Pronchery }
1834*e7be843bSPierre Pronchery
1835*e7be843bSPierre Pronchery /*
1836*e7be843bSPierre Pronchery * Configures a channel with the information we have accumulated via calls made
1837*e7be843bSPierre Pronchery * to us from the application prior to starting a handshake attempt.
1838*e7be843bSPierre Pronchery */
1839*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
ensure_channel_started(QCTX * ctx)1840*e7be843bSPierre Pronchery static int ensure_channel_started(QCTX *ctx)
1841*e7be843bSPierre Pronchery {
1842*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = ctx->qc;
1843*e7be843bSPierre Pronchery
1844*e7be843bSPierre Pronchery if (!qc->started) {
1845*e7be843bSPierre Pronchery if (!configure_channel(qc)) {
1846*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1847*e7be843bSPierre Pronchery "failed to configure channel");
1848*e7be843bSPierre Pronchery return 0;
1849*e7be843bSPierre Pronchery }
1850*e7be843bSPierre Pronchery
1851*e7be843bSPierre Pronchery if (!ossl_quic_channel_start(qc->ch)) {
1852*e7be843bSPierre Pronchery ossl_quic_channel_restore_err_state(qc->ch);
1853*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1854*e7be843bSPierre Pronchery "failed to start channel");
1855*e7be843bSPierre Pronchery return 0;
1856*e7be843bSPierre Pronchery }
1857*e7be843bSPierre Pronchery
1858*e7be843bSPierre Pronchery #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
1859*e7be843bSPierre Pronchery if (qc->is_thread_assisted)
1860*e7be843bSPierre Pronchery if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch)) {
1861*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR,
1862*e7be843bSPierre Pronchery "failed to start assist thread");
1863*e7be843bSPierre Pronchery return 0;
1864*e7be843bSPierre Pronchery }
1865*e7be843bSPierre Pronchery #endif
1866*e7be843bSPierre Pronchery }
1867*e7be843bSPierre Pronchery
1868*e7be843bSPierre Pronchery qc->started = 1;
1869*e7be843bSPierre Pronchery return 1;
1870*e7be843bSPierre Pronchery }
1871*e7be843bSPierre Pronchery
1872*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_do_handshake(QCTX * ctx)1873*e7be843bSPierre Pronchery static int quic_do_handshake(QCTX *ctx)
1874*e7be843bSPierre Pronchery {
1875*e7be843bSPierre Pronchery int ret;
1876*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = ctx->qc;
1877*e7be843bSPierre Pronchery QUIC_PORT *port;
1878*e7be843bSPierre Pronchery BIO *net_rbio, *net_wbio;
1879*e7be843bSPierre Pronchery
1880*e7be843bSPierre Pronchery if (ossl_quic_channel_is_handshake_complete(qc->ch))
1881*e7be843bSPierre Pronchery /* Handshake already completed. */
1882*e7be843bSPierre Pronchery return 1;
1883*e7be843bSPierre Pronchery
1884*e7be843bSPierre Pronchery if (!quic_mutation_allowed(qc, /*req_active=*/0))
1885*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1886*e7be843bSPierre Pronchery
1887*e7be843bSPierre Pronchery if (qc->as_server != qc->as_server_state) {
1888*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
1889*e7be843bSPierre Pronchery return -1; /* Non-protocol error */
1890*e7be843bSPierre Pronchery }
1891*e7be843bSPierre Pronchery
1892*e7be843bSPierre Pronchery port = ossl_quic_obj_get0_port(ctx->obj);
1893*e7be843bSPierre Pronchery net_rbio = ossl_quic_port_get_net_rbio(port);
1894*e7be843bSPierre Pronchery net_wbio = ossl_quic_port_get_net_wbio(port);
1895*e7be843bSPierre Pronchery if (net_rbio == NULL || net_wbio == NULL) {
1896*e7be843bSPierre Pronchery /* Need read and write BIOs. */
1897*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BIO_NOT_SET, NULL);
1898*e7be843bSPierre Pronchery return -1; /* Non-protocol error */
1899*e7be843bSPierre Pronchery }
1900*e7be843bSPierre Pronchery
1901*e7be843bSPierre Pronchery if (!qc->started && ossl_quic_port_is_addressed_w(port)
1902*e7be843bSPierre Pronchery && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1903*e7be843bSPierre Pronchery /*
1904*e7be843bSPierre Pronchery * We are trying to connect and are using addressed mode, which means we
1905*e7be843bSPierre Pronchery * need an initial peer address; if we do not have a peer address yet,
1906*e7be843bSPierre Pronchery * we should try to autodetect one.
1907*e7be843bSPierre Pronchery *
1908*e7be843bSPierre Pronchery * We do this as late as possible because some BIOs (e.g. BIO_s_connect)
1909*e7be843bSPierre Pronchery * may not be able to provide us with a peer address until they have
1910*e7be843bSPierre Pronchery * finished their own processing. They may not be able to perform this
1911*e7be843bSPierre Pronchery * processing until an application has finished configuring that BIO
1912*e7be843bSPierre Pronchery * (e.g. with setter calls), which might happen after SSL_set_bio is
1913*e7be843bSPierre Pronchery * called.
1914*e7be843bSPierre Pronchery */
1915*e7be843bSPierre Pronchery if (!csm_analyse_init_peer_addr(net_wbio, &qc->init_peer_addr))
1916*e7be843bSPierre Pronchery /* best effort */
1917*e7be843bSPierre Pronchery BIO_ADDR_clear(&qc->init_peer_addr);
1918*e7be843bSPierre Pronchery else
1919*e7be843bSPierre Pronchery ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
1920*e7be843bSPierre Pronchery }
1921*e7be843bSPierre Pronchery
1922*e7be843bSPierre Pronchery if (!qc->started
1923*e7be843bSPierre Pronchery && ossl_quic_port_is_addressed_w(port)
1924*e7be843bSPierre Pronchery && BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
1925*e7be843bSPierre Pronchery /*
1926*e7be843bSPierre Pronchery * If we still don't have a peer address in addressed mode, we can't do
1927*e7be843bSPierre Pronchery * anything.
1928*e7be843bSPierre Pronchery */
1929*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
1930*e7be843bSPierre Pronchery return -1; /* Non-protocol error */
1931*e7be843bSPierre Pronchery }
1932*e7be843bSPierre Pronchery
1933*e7be843bSPierre Pronchery /*
1934*e7be843bSPierre Pronchery * Start connection process. Note we may come here multiple times in
1935*e7be843bSPierre Pronchery * non-blocking mode, which is fine.
1936*e7be843bSPierre Pronchery */
1937*e7be843bSPierre Pronchery if (!ensure_channel_started(ctx)) /* raises on failure */
1938*e7be843bSPierre Pronchery return -1; /* Non-protocol error */
1939*e7be843bSPierre Pronchery
1940*e7be843bSPierre Pronchery if (ossl_quic_channel_is_handshake_complete(qc->ch))
1941*e7be843bSPierre Pronchery /* The handshake is now done. */
1942*e7be843bSPierre Pronchery return 1;
1943*e7be843bSPierre Pronchery
1944*e7be843bSPierre Pronchery if (!qctx_blocking(ctx)) {
1945*e7be843bSPierre Pronchery /* Try to advance the reactor. */
1946*e7be843bSPierre Pronchery qctx_maybe_autotick(ctx);
1947*e7be843bSPierre Pronchery
1948*e7be843bSPierre Pronchery if (ossl_quic_channel_is_handshake_complete(qc->ch))
1949*e7be843bSPierre Pronchery /* The handshake is now done. */
1950*e7be843bSPierre Pronchery return 1;
1951*e7be843bSPierre Pronchery
1952*e7be843bSPierre Pronchery if (ossl_quic_channel_is_term_any(qc->ch)) {
1953*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1954*e7be843bSPierre Pronchery return 0;
1955*e7be843bSPierre Pronchery } else if (ossl_quic_obj_desires_blocking(&qc->obj)) {
1956*e7be843bSPierre Pronchery /*
1957*e7be843bSPierre Pronchery * As a special case when doing a handshake when blocking mode is
1958*e7be843bSPierre Pronchery * desired yet not available, see if the network BIOs have become
1959*e7be843bSPierre Pronchery * poll descriptor-enabled. This supports BIOs such as BIO_s_connect
1960*e7be843bSPierre Pronchery * which do late creation of socket FDs and therefore cannot expose
1961*e7be843bSPierre Pronchery * a poll descriptor until after a network BIO is set on the QCSO.
1962*e7be843bSPierre Pronchery */
1963*e7be843bSPierre Pronchery ossl_quic_engine_update_poll_descriptors(qc->obj.engine, /*force=*/1);
1964*e7be843bSPierre Pronchery }
1965*e7be843bSPierre Pronchery }
1966*e7be843bSPierre Pronchery
1967*e7be843bSPierre Pronchery /*
1968*e7be843bSPierre Pronchery * We are either in blocking mode or just entered it due to the code above.
1969*e7be843bSPierre Pronchery */
1970*e7be843bSPierre Pronchery if (qctx_blocking(ctx)) {
1971*e7be843bSPierre Pronchery /* In blocking mode, wait for the handshake to complete. */
1972*e7be843bSPierre Pronchery struct quic_handshake_wait_args args;
1973*e7be843bSPierre Pronchery
1974*e7be843bSPierre Pronchery args.qc = qc;
1975*e7be843bSPierre Pronchery
1976*e7be843bSPierre Pronchery ret = block_until_pred(ctx, quic_handshake_wait, &args, 0);
1977*e7be843bSPierre Pronchery if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
1978*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1979*e7be843bSPierre Pronchery return 0; /* Shutdown before completion */
1980*e7be843bSPierre Pronchery } else if (ret <= 0) {
1981*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
1982*e7be843bSPierre Pronchery return -1; /* Non-protocol error */
1983*e7be843bSPierre Pronchery }
1984*e7be843bSPierre Pronchery
1985*e7be843bSPierre Pronchery if (tls_wants_non_io_retry(qc)) {
1986*e7be843bSPierre Pronchery QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
1987*e7be843bSPierre Pronchery return -1;
1988*e7be843bSPierre Pronchery }
1989*e7be843bSPierre Pronchery
1990*e7be843bSPierre Pronchery assert(ossl_quic_channel_is_handshake_complete(qc->ch));
1991*e7be843bSPierre Pronchery return 1;
1992*e7be843bSPierre Pronchery }
1993*e7be843bSPierre Pronchery
1994*e7be843bSPierre Pronchery if (tls_wants_non_io_retry(qc)) {
1995*e7be843bSPierre Pronchery QUIC_RAISE_NORMAL_ERROR(ctx, SSL_get_error(qc->tls, 0));
1996*e7be843bSPierre Pronchery return -1;
1997*e7be843bSPierre Pronchery }
1998*e7be843bSPierre Pronchery
1999*e7be843bSPierre Pronchery /*
2000*e7be843bSPierre Pronchery * Otherwise, indicate that the handshake isn't done yet.
2001*e7be843bSPierre Pronchery * We can only get here in non-blocking mode.
2002*e7be843bSPierre Pronchery */
2003*e7be843bSPierre Pronchery QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
2004*e7be843bSPierre Pronchery return -1; /* Non-protocol error */
2005*e7be843bSPierre Pronchery }
2006*e7be843bSPierre Pronchery
2007*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_do_handshake(SSL * s)2008*e7be843bSPierre Pronchery int ossl_quic_do_handshake(SSL *s)
2009*e7be843bSPierre Pronchery {
2010*e7be843bSPierre Pronchery int ret;
2011*e7be843bSPierre Pronchery QCTX ctx;
2012*e7be843bSPierre Pronchery
2013*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
2014*e7be843bSPierre Pronchery return 0;
2015*e7be843bSPierre Pronchery
2016*e7be843bSPierre Pronchery qctx_lock_for_io(&ctx);
2017*e7be843bSPierre Pronchery
2018*e7be843bSPierre Pronchery ret = quic_do_handshake(&ctx);
2019*e7be843bSPierre Pronchery qctx_unlock(&ctx);
2020*e7be843bSPierre Pronchery return ret;
2021*e7be843bSPierre Pronchery }
2022*e7be843bSPierre Pronchery
2023*e7be843bSPierre Pronchery /* SSL_connect */
ossl_quic_connect(SSL * s)2024*e7be843bSPierre Pronchery int ossl_quic_connect(SSL *s)
2025*e7be843bSPierre Pronchery {
2026*e7be843bSPierre Pronchery /* Ensure we are in connect state (no-op if non-idle). */
2027*e7be843bSPierre Pronchery if (!ossl_quic_set_connect_state(s, 1))
2028*e7be843bSPierre Pronchery return -1;
2029*e7be843bSPierre Pronchery
2030*e7be843bSPierre Pronchery /* Begin or continue the handshake */
2031*e7be843bSPierre Pronchery return ossl_quic_do_handshake(s);
2032*e7be843bSPierre Pronchery }
2033*e7be843bSPierre Pronchery
2034*e7be843bSPierre Pronchery /* SSL_accept */
ossl_quic_accept(SSL * s)2035*e7be843bSPierre Pronchery int ossl_quic_accept(SSL *s)
2036*e7be843bSPierre Pronchery {
2037*e7be843bSPierre Pronchery /* Ensure we are in accept state (no-op if non-idle). */
2038*e7be843bSPierre Pronchery if (!ossl_quic_set_accept_state(s, 1))
2039*e7be843bSPierre Pronchery return -1;
2040*e7be843bSPierre Pronchery
2041*e7be843bSPierre Pronchery /* Begin or continue the handshake */
2042*e7be843bSPierre Pronchery return ossl_quic_do_handshake(s);
2043*e7be843bSPierre Pronchery }
2044*e7be843bSPierre Pronchery
2045*e7be843bSPierre Pronchery /*
2046*e7be843bSPierre Pronchery * QUIC Front-End I/O API: Stream Lifecycle Operations
2047*e7be843bSPierre Pronchery * ===================================================
2048*e7be843bSPierre Pronchery *
2049*e7be843bSPierre Pronchery * SSL_stream_new => ossl_quic_conn_stream_new
2050*e7be843bSPierre Pronchery *
2051*e7be843bSPierre Pronchery */
2052*e7be843bSPierre Pronchery
2053*e7be843bSPierre Pronchery /*
2054*e7be843bSPierre Pronchery * Try to create the default XSO if it doesn't already exist. Returns 1 if the
2055*e7be843bSPierre Pronchery * default XSO was created. Returns 0 if it was not (e.g. because it already
2056*e7be843bSPierre Pronchery * exists). Note that this is NOT an error condition.
2057*e7be843bSPierre Pronchery */
2058*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_try_create_default_xso_for_write(QCTX * ctx)2059*e7be843bSPierre Pronchery static int qc_try_create_default_xso_for_write(QCTX *ctx)
2060*e7be843bSPierre Pronchery {
2061*e7be843bSPierre Pronchery uint64_t flags = 0;
2062*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = ctx->qc;
2063*e7be843bSPierre Pronchery
2064*e7be843bSPierre Pronchery if (qc->default_xso_created
2065*e7be843bSPierre Pronchery || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2066*e7be843bSPierre Pronchery /*
2067*e7be843bSPierre Pronchery * We only do this once. If the user detaches a previously created
2068*e7be843bSPierre Pronchery * default XSO we don't auto-create another one.
2069*e7be843bSPierre Pronchery */
2070*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
2071*e7be843bSPierre Pronchery
2072*e7be843bSPierre Pronchery /* Create a locally-initiated stream. */
2073*e7be843bSPierre Pronchery if (qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_AUTO_UNI)
2074*e7be843bSPierre Pronchery flags |= SSL_STREAM_FLAG_UNI;
2075*e7be843bSPierre Pronchery
2076*e7be843bSPierre Pronchery qc_set_default_xso(qc, (QUIC_XSO *)quic_conn_stream_new(ctx, flags,
2077*e7be843bSPierre Pronchery /*needs_lock=*/0),
2078*e7be843bSPierre Pronchery /*touch=*/0);
2079*e7be843bSPierre Pronchery if (qc->default_xso == NULL)
2080*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2081*e7be843bSPierre Pronchery
2082*e7be843bSPierre Pronchery qc_touch_default_xso(qc);
2083*e7be843bSPierre Pronchery return 1;
2084*e7be843bSPierre Pronchery }
2085*e7be843bSPierre Pronchery
2086*e7be843bSPierre Pronchery struct quic_wait_for_stream_args {
2087*e7be843bSPierre Pronchery QUIC_CONNECTION *qc;
2088*e7be843bSPierre Pronchery QUIC_STREAM *qs;
2089*e7be843bSPierre Pronchery QCTX *ctx;
2090*e7be843bSPierre Pronchery uint64_t expect_id;
2091*e7be843bSPierre Pronchery };
2092*e7be843bSPierre Pronchery
2093*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_wait_for_stream(void * arg)2094*e7be843bSPierre Pronchery static int quic_wait_for_stream(void *arg)
2095*e7be843bSPierre Pronchery {
2096*e7be843bSPierre Pronchery struct quic_wait_for_stream_args *args = arg;
2097*e7be843bSPierre Pronchery
2098*e7be843bSPierre Pronchery if (!quic_mutation_allowed(args->qc, /*req_active=*/1)) {
2099*e7be843bSPierre Pronchery /* If connection is torn down due to an error while blocking, stop. */
2100*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2101*e7be843bSPierre Pronchery return -1;
2102*e7be843bSPierre Pronchery }
2103*e7be843bSPierre Pronchery
2104*e7be843bSPierre Pronchery args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
2105*e7be843bSPierre Pronchery args->expect_id | QUIC_STREAM_DIR_BIDI);
2106*e7be843bSPierre Pronchery if (args->qs == NULL)
2107*e7be843bSPierre Pronchery args->qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(args->qc->ch),
2108*e7be843bSPierre Pronchery args->expect_id | QUIC_STREAM_DIR_UNI);
2109*e7be843bSPierre Pronchery
2110*e7be843bSPierre Pronchery if (args->qs != NULL)
2111*e7be843bSPierre Pronchery return 1; /* stream now exists */
2112*e7be843bSPierre Pronchery
2113*e7be843bSPierre Pronchery return 0; /* did not get a stream, keep trying */
2114*e7be843bSPierre Pronchery }
2115*e7be843bSPierre Pronchery
2116*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_wait_for_default_xso_for_read(QCTX * ctx,int peek)2117*e7be843bSPierre Pronchery static int qc_wait_for_default_xso_for_read(QCTX *ctx, int peek)
2118*e7be843bSPierre Pronchery {
2119*e7be843bSPierre Pronchery /* Called on a QCSO and we don't currently have a default stream. */
2120*e7be843bSPierre Pronchery uint64_t expect_id;
2121*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = ctx->qc;
2122*e7be843bSPierre Pronchery QUIC_STREAM *qs;
2123*e7be843bSPierre Pronchery int res;
2124*e7be843bSPierre Pronchery struct quic_wait_for_stream_args wargs;
2125*e7be843bSPierre Pronchery OSSL_RTT_INFO rtt_info;
2126*e7be843bSPierre Pronchery
2127*e7be843bSPierre Pronchery /*
2128*e7be843bSPierre Pronchery * If default stream functionality is disabled or we already detached
2129*e7be843bSPierre Pronchery * one, don't make another default stream and just fail.
2130*e7be843bSPierre Pronchery */
2131*e7be843bSPierre Pronchery if (qc->default_xso_created
2132*e7be843bSPierre Pronchery || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
2133*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
2134*e7be843bSPierre Pronchery
2135*e7be843bSPierre Pronchery /*
2136*e7be843bSPierre Pronchery * The peer may have opened a stream since we last ticked. So tick and
2137*e7be843bSPierre Pronchery * see if the stream with ordinal 0 (remote, bidi/uni based on stream
2138*e7be843bSPierre Pronchery * mode) exists yet. QUIC stream IDs must be allocated in order, so the
2139*e7be843bSPierre Pronchery * first stream created by a peer must have an ordinal of 0.
2140*e7be843bSPierre Pronchery */
2141*e7be843bSPierre Pronchery expect_id = qc->as_server
2142*e7be843bSPierre Pronchery ? QUIC_STREAM_INITIATOR_CLIENT
2143*e7be843bSPierre Pronchery : QUIC_STREAM_INITIATOR_SERVER;
2144*e7be843bSPierre Pronchery
2145*e7be843bSPierre Pronchery qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2146*e7be843bSPierre Pronchery expect_id | QUIC_STREAM_DIR_BIDI);
2147*e7be843bSPierre Pronchery if (qs == NULL)
2148*e7be843bSPierre Pronchery qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2149*e7be843bSPierre Pronchery expect_id | QUIC_STREAM_DIR_UNI);
2150*e7be843bSPierre Pronchery
2151*e7be843bSPierre Pronchery if (qs == NULL) {
2152*e7be843bSPierre Pronchery qctx_maybe_autotick(ctx);
2153*e7be843bSPierre Pronchery
2154*e7be843bSPierre Pronchery qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(qc->ch),
2155*e7be843bSPierre Pronchery expect_id);
2156*e7be843bSPierre Pronchery }
2157*e7be843bSPierre Pronchery
2158*e7be843bSPierre Pronchery if (qs == NULL) {
2159*e7be843bSPierre Pronchery if (peek)
2160*e7be843bSPierre Pronchery return 0;
2161*e7be843bSPierre Pronchery
2162*e7be843bSPierre Pronchery if (ossl_quic_channel_is_term_any(qc->ch)) {
2163*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2164*e7be843bSPierre Pronchery } else if (!qctx_blocking(ctx)) {
2165*e7be843bSPierre Pronchery /* Non-blocking mode, so just bail immediately. */
2166*e7be843bSPierre Pronchery return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_READ);
2167*e7be843bSPierre Pronchery }
2168*e7be843bSPierre Pronchery
2169*e7be843bSPierre Pronchery /* Block until we have a stream. */
2170*e7be843bSPierre Pronchery wargs.qc = qc;
2171*e7be843bSPierre Pronchery wargs.qs = NULL;
2172*e7be843bSPierre Pronchery wargs.ctx = ctx;
2173*e7be843bSPierre Pronchery wargs.expect_id = expect_id;
2174*e7be843bSPierre Pronchery
2175*e7be843bSPierre Pronchery res = block_until_pred(ctx, quic_wait_for_stream, &wargs, 0);
2176*e7be843bSPierre Pronchery if (res == 0)
2177*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2178*e7be843bSPierre Pronchery else if (res < 0 || wargs.qs == NULL)
2179*e7be843bSPierre Pronchery /* quic_wait_for_stream raised error here */
2180*e7be843bSPierre Pronchery return 0;
2181*e7be843bSPierre Pronchery
2182*e7be843bSPierre Pronchery qs = wargs.qs;
2183*e7be843bSPierre Pronchery }
2184*e7be843bSPierre Pronchery
2185*e7be843bSPierre Pronchery /*
2186*e7be843bSPierre Pronchery * We now have qs != NULL. Remove it from the incoming stream queue so that
2187*e7be843bSPierre Pronchery * it isn't also returned by any future SSL_accept_stream calls.
2188*e7be843bSPierre Pronchery */
2189*e7be843bSPierre Pronchery ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2190*e7be843bSPierre Pronchery ossl_quic_stream_map_remove_from_accept_queue(ossl_quic_channel_get_qsm(qc->ch),
2191*e7be843bSPierre Pronchery qs, rtt_info.smoothed_rtt);
2192*e7be843bSPierre Pronchery
2193*e7be843bSPierre Pronchery /*
2194*e7be843bSPierre Pronchery * Now make qs the default stream, creating the necessary XSO.
2195*e7be843bSPierre Pronchery */
2196*e7be843bSPierre Pronchery qc_set_default_xso(qc, create_xso_from_stream(qc, qs), /*touch=*/0);
2197*e7be843bSPierre Pronchery if (qc->default_xso == NULL)
2198*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2199*e7be843bSPierre Pronchery
2200*e7be843bSPierre Pronchery qc_touch_default_xso(qc); /* inhibits default XSO */
2201*e7be843bSPierre Pronchery return 1;
2202*e7be843bSPierre Pronchery }
2203*e7be843bSPierre Pronchery
2204*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
create_xso_from_stream(QUIC_CONNECTION * qc,QUIC_STREAM * qs)2205*e7be843bSPierre Pronchery static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs)
2206*e7be843bSPierre Pronchery {
2207*e7be843bSPierre Pronchery QUIC_XSO *xso = NULL;
2208*e7be843bSPierre Pronchery
2209*e7be843bSPierre Pronchery if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) {
2210*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
2211*e7be843bSPierre Pronchery goto err;
2212*e7be843bSPierre Pronchery }
2213*e7be843bSPierre Pronchery
2214*e7be843bSPierre Pronchery if (!ossl_quic_obj_init(&xso->obj, qc->obj.ssl.ctx, SSL_TYPE_QUIC_XSO,
2215*e7be843bSPierre Pronchery &qc->obj.ssl, NULL, NULL)) {
2216*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
2217*e7be843bSPierre Pronchery goto err;
2218*e7be843bSPierre Pronchery }
2219*e7be843bSPierre Pronchery
2220*e7be843bSPierre Pronchery /* XSO refs QC */
2221*e7be843bSPierre Pronchery if (!SSL_up_ref(&qc->obj.ssl)) {
2222*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_SSL_LIB, NULL);
2223*e7be843bSPierre Pronchery goto err;
2224*e7be843bSPierre Pronchery }
2225*e7be843bSPierre Pronchery
2226*e7be843bSPierre Pronchery xso->conn = qc;
2227*e7be843bSPierre Pronchery xso->ssl_mode = qc->default_ssl_mode;
2228*e7be843bSPierre Pronchery xso->ssl_options
2229*e7be843bSPierre Pronchery = qc->default_ssl_options & OSSL_QUIC_PERMITTED_OPTIONS_STREAM;
2230*e7be843bSPierre Pronchery xso->last_error = SSL_ERROR_NONE;
2231*e7be843bSPierre Pronchery
2232*e7be843bSPierre Pronchery xso->stream = qs;
2233*e7be843bSPierre Pronchery
2234*e7be843bSPierre Pronchery ++qc->num_xso;
2235*e7be843bSPierre Pronchery xso_update_options(xso);
2236*e7be843bSPierre Pronchery return xso;
2237*e7be843bSPierre Pronchery
2238*e7be843bSPierre Pronchery err:
2239*e7be843bSPierre Pronchery OPENSSL_free(xso);
2240*e7be843bSPierre Pronchery return NULL;
2241*e7be843bSPierre Pronchery }
2242*e7be843bSPierre Pronchery
2243*e7be843bSPierre Pronchery struct quic_new_stream_wait_args {
2244*e7be843bSPierre Pronchery QUIC_CONNECTION *qc;
2245*e7be843bSPierre Pronchery int is_uni;
2246*e7be843bSPierre Pronchery };
2247*e7be843bSPierre Pronchery
quic_new_stream_wait(void * arg)2248*e7be843bSPierre Pronchery static int quic_new_stream_wait(void *arg)
2249*e7be843bSPierre Pronchery {
2250*e7be843bSPierre Pronchery struct quic_new_stream_wait_args *args = arg;
2251*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = args->qc;
2252*e7be843bSPierre Pronchery
2253*e7be843bSPierre Pronchery if (!quic_mutation_allowed(qc, /*req_active=*/1))
2254*e7be843bSPierre Pronchery return -1;
2255*e7be843bSPierre Pronchery
2256*e7be843bSPierre Pronchery if (ossl_quic_channel_is_new_local_stream_admissible(qc->ch, args->is_uni))
2257*e7be843bSPierre Pronchery return 1;
2258*e7be843bSPierre Pronchery
2259*e7be843bSPierre Pronchery return 0;
2260*e7be843bSPierre Pronchery }
2261*e7be843bSPierre Pronchery
2262*e7be843bSPierre Pronchery /* locking depends on need_lock */
quic_conn_stream_new(QCTX * ctx,uint64_t flags,int need_lock)2263*e7be843bSPierre Pronchery static SSL *quic_conn_stream_new(QCTX *ctx, uint64_t flags, int need_lock)
2264*e7be843bSPierre Pronchery {
2265*e7be843bSPierre Pronchery int ret;
2266*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = ctx->qc;
2267*e7be843bSPierre Pronchery QUIC_XSO *xso = NULL;
2268*e7be843bSPierre Pronchery QUIC_STREAM *qs = NULL;
2269*e7be843bSPierre Pronchery int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0);
2270*e7be843bSPierre Pronchery int no_blocking = ((flags & SSL_STREAM_FLAG_NO_BLOCK) != 0);
2271*e7be843bSPierre Pronchery int advance = ((flags & SSL_STREAM_FLAG_ADVANCE) != 0);
2272*e7be843bSPierre Pronchery
2273*e7be843bSPierre Pronchery if (need_lock)
2274*e7be843bSPierre Pronchery qctx_lock(ctx);
2275*e7be843bSPierre Pronchery
2276*e7be843bSPierre Pronchery if (!quic_mutation_allowed(qc, /*req_active=*/0)) {
2277*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2278*e7be843bSPierre Pronchery goto err;
2279*e7be843bSPierre Pronchery }
2280*e7be843bSPierre Pronchery
2281*e7be843bSPierre Pronchery if (!advance
2282*e7be843bSPierre Pronchery && !ossl_quic_channel_is_new_local_stream_admissible(qc->ch, is_uni)) {
2283*e7be843bSPierre Pronchery struct quic_new_stream_wait_args args;
2284*e7be843bSPierre Pronchery
2285*e7be843bSPierre Pronchery /*
2286*e7be843bSPierre Pronchery * Stream count flow control currently doesn't permit this stream to be
2287*e7be843bSPierre Pronchery * opened.
2288*e7be843bSPierre Pronchery */
2289*e7be843bSPierre Pronchery if (no_blocking || !qctx_blocking(ctx)) {
2290*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_COUNT_LIMITED, NULL);
2291*e7be843bSPierre Pronchery goto err;
2292*e7be843bSPierre Pronchery }
2293*e7be843bSPierre Pronchery
2294*e7be843bSPierre Pronchery args.qc = qc;
2295*e7be843bSPierre Pronchery args.is_uni = is_uni;
2296*e7be843bSPierre Pronchery
2297*e7be843bSPierre Pronchery /* Blocking mode - wait until we can get a stream. */
2298*e7be843bSPierre Pronchery ret = block_until_pred(ctx, quic_new_stream_wait, &args, 0);
2299*e7be843bSPierre Pronchery if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
2300*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2301*e7be843bSPierre Pronchery goto err; /* Shutdown before completion */
2302*e7be843bSPierre Pronchery } else if (ret <= 0) {
2303*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2304*e7be843bSPierre Pronchery goto err; /* Non-protocol error */
2305*e7be843bSPierre Pronchery }
2306*e7be843bSPierre Pronchery }
2307*e7be843bSPierre Pronchery
2308*e7be843bSPierre Pronchery qs = ossl_quic_channel_new_stream_local(qc->ch, is_uni);
2309*e7be843bSPierre Pronchery if (qs == NULL) {
2310*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2311*e7be843bSPierre Pronchery goto err;
2312*e7be843bSPierre Pronchery }
2313*e7be843bSPierre Pronchery
2314*e7be843bSPierre Pronchery xso = create_xso_from_stream(qc, qs);
2315*e7be843bSPierre Pronchery if (xso == NULL)
2316*e7be843bSPierre Pronchery goto err;
2317*e7be843bSPierre Pronchery
2318*e7be843bSPierre Pronchery qc_touch_default_xso(qc); /* inhibits default XSO */
2319*e7be843bSPierre Pronchery if (need_lock)
2320*e7be843bSPierre Pronchery qctx_unlock(ctx);
2321*e7be843bSPierre Pronchery
2322*e7be843bSPierre Pronchery return &xso->obj.ssl;
2323*e7be843bSPierre Pronchery
2324*e7be843bSPierre Pronchery err:
2325*e7be843bSPierre Pronchery OPENSSL_free(xso);
2326*e7be843bSPierre Pronchery ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs);
2327*e7be843bSPierre Pronchery if (need_lock)
2328*e7be843bSPierre Pronchery qctx_unlock(ctx);
2329*e7be843bSPierre Pronchery
2330*e7be843bSPierre Pronchery return NULL;
2331*e7be843bSPierre Pronchery
2332*e7be843bSPierre Pronchery }
2333*e7be843bSPierre Pronchery
2334*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_conn_stream_new(SSL * s,uint64_t flags)2335*e7be843bSPierre Pronchery SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags)
2336*e7be843bSPierre Pronchery {
2337*e7be843bSPierre Pronchery QCTX ctx;
2338*e7be843bSPierre Pronchery
2339*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
2340*e7be843bSPierre Pronchery return NULL;
2341*e7be843bSPierre Pronchery
2342*e7be843bSPierre Pronchery return quic_conn_stream_new(&ctx, flags, /*need_lock=*/1);
2343*e7be843bSPierre Pronchery }
2344*e7be843bSPierre Pronchery
2345*e7be843bSPierre Pronchery /*
2346*e7be843bSPierre Pronchery * QUIC Front-End I/O API: Steady-State Operations
2347*e7be843bSPierre Pronchery * ===============================================
2348*e7be843bSPierre Pronchery *
2349*e7be843bSPierre Pronchery * Here we dispatch calls to the steady-state front-end I/O API functions; that
2350*e7be843bSPierre Pronchery * is, the functions used during the established phase of a QUIC connection
2351*e7be843bSPierre Pronchery * (e.g. SSL_read, SSL_write).
2352*e7be843bSPierre Pronchery *
2353*e7be843bSPierre Pronchery * Each function must handle both blocking and non-blocking modes. As discussed
2354*e7be843bSPierre Pronchery * above, all QUIC I/O is implemented using non-blocking mode internally.
2355*e7be843bSPierre Pronchery *
2356*e7be843bSPierre Pronchery * SSL_get_error => partially implemented by ossl_quic_get_error
2357*e7be843bSPierre Pronchery * SSL_want => ossl_quic_want
2358*e7be843bSPierre Pronchery * (BIO/)SSL_read => ossl_quic_read
2359*e7be843bSPierre Pronchery * (BIO/)SSL_write => ossl_quic_write
2360*e7be843bSPierre Pronchery * SSL_pending => ossl_quic_pending
2361*e7be843bSPierre Pronchery * SSL_stream_conclude => ossl_quic_conn_stream_conclude
2362*e7be843bSPierre Pronchery * SSL_key_update => ossl_quic_key_update
2363*e7be843bSPierre Pronchery */
2364*e7be843bSPierre Pronchery
2365*e7be843bSPierre Pronchery /* SSL_get_error */
ossl_quic_get_error(const SSL * s,int i)2366*e7be843bSPierre Pronchery int ossl_quic_get_error(const SSL *s, int i)
2367*e7be843bSPierre Pronchery {
2368*e7be843bSPierre Pronchery QCTX ctx;
2369*e7be843bSPierre Pronchery int net_error, last_error;
2370*e7be843bSPierre Pronchery
2371*e7be843bSPierre Pronchery /* SSL_get_errors() should not raise new errors */
2372*e7be843bSPierre Pronchery if (!is_quic_cs(s, &ctx, 0 /* suppress errors */))
2373*e7be843bSPierre Pronchery return SSL_ERROR_SSL;
2374*e7be843bSPierre Pronchery
2375*e7be843bSPierre Pronchery qctx_lock(&ctx);
2376*e7be843bSPierre Pronchery net_error = ossl_quic_channel_net_error(ctx.qc->ch);
2377*e7be843bSPierre Pronchery last_error = ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error;
2378*e7be843bSPierre Pronchery qctx_unlock(&ctx);
2379*e7be843bSPierre Pronchery
2380*e7be843bSPierre Pronchery if (net_error)
2381*e7be843bSPierre Pronchery return SSL_ERROR_SYSCALL;
2382*e7be843bSPierre Pronchery
2383*e7be843bSPierre Pronchery return last_error;
2384*e7be843bSPierre Pronchery }
2385*e7be843bSPierre Pronchery
2386*e7be843bSPierre Pronchery /* Converts a code returned by SSL_get_error to a code returned by SSL_want. */
error_to_want(int error)2387*e7be843bSPierre Pronchery static int error_to_want(int error)
2388*e7be843bSPierre Pronchery {
2389*e7be843bSPierre Pronchery switch (error) {
2390*e7be843bSPierre Pronchery case SSL_ERROR_WANT_CONNECT: /* never used - UDP is connectionless */
2391*e7be843bSPierre Pronchery case SSL_ERROR_WANT_ACCEPT: /* never used - UDP is connectionless */
2392*e7be843bSPierre Pronchery case SSL_ERROR_ZERO_RETURN:
2393*e7be843bSPierre Pronchery default:
2394*e7be843bSPierre Pronchery return SSL_NOTHING;
2395*e7be843bSPierre Pronchery
2396*e7be843bSPierre Pronchery case SSL_ERROR_WANT_READ:
2397*e7be843bSPierre Pronchery return SSL_READING;
2398*e7be843bSPierre Pronchery
2399*e7be843bSPierre Pronchery case SSL_ERROR_WANT_WRITE:
2400*e7be843bSPierre Pronchery return SSL_WRITING;
2401*e7be843bSPierre Pronchery
2402*e7be843bSPierre Pronchery case SSL_ERROR_WANT_RETRY_VERIFY:
2403*e7be843bSPierre Pronchery return SSL_RETRY_VERIFY;
2404*e7be843bSPierre Pronchery
2405*e7be843bSPierre Pronchery case SSL_ERROR_WANT_CLIENT_HELLO_CB:
2406*e7be843bSPierre Pronchery return SSL_CLIENT_HELLO_CB;
2407*e7be843bSPierre Pronchery
2408*e7be843bSPierre Pronchery case SSL_ERROR_WANT_X509_LOOKUP:
2409*e7be843bSPierre Pronchery return SSL_X509_LOOKUP;
2410*e7be843bSPierre Pronchery }
2411*e7be843bSPierre Pronchery }
2412*e7be843bSPierre Pronchery
2413*e7be843bSPierre Pronchery /* SSL_want */
ossl_quic_want(const SSL * s)2414*e7be843bSPierre Pronchery int ossl_quic_want(const SSL *s)
2415*e7be843bSPierre Pronchery {
2416*e7be843bSPierre Pronchery QCTX ctx;
2417*e7be843bSPierre Pronchery int w;
2418*e7be843bSPierre Pronchery
2419*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
2420*e7be843bSPierre Pronchery return SSL_NOTHING;
2421*e7be843bSPierre Pronchery
2422*e7be843bSPierre Pronchery qctx_lock(&ctx);
2423*e7be843bSPierre Pronchery
2424*e7be843bSPierre Pronchery w = error_to_want(ctx.is_stream ? ctx.xso->last_error : ctx.qc->last_error);
2425*e7be843bSPierre Pronchery
2426*e7be843bSPierre Pronchery qctx_unlock(&ctx);
2427*e7be843bSPierre Pronchery return w;
2428*e7be843bSPierre Pronchery }
2429*e7be843bSPierre Pronchery
2430*e7be843bSPierre Pronchery /*
2431*e7be843bSPierre Pronchery * SSL_write
2432*e7be843bSPierre Pronchery * ---------
2433*e7be843bSPierre Pronchery *
2434*e7be843bSPierre Pronchery * The set of functions below provide the implementation of the public SSL_write
2435*e7be843bSPierre Pronchery * function. We must handle:
2436*e7be843bSPierre Pronchery *
2437*e7be843bSPierre Pronchery * - both blocking and non-blocking operation at the application level,
2438*e7be843bSPierre Pronchery * depending on how we are configured;
2439*e7be843bSPierre Pronchery *
2440*e7be843bSPierre Pronchery * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
2441*e7be843bSPierre Pronchery *
2442*e7be843bSPierre Pronchery * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
2443*e7be843bSPierre Pronchery *
2444*e7be843bSPierre Pronchery */
2445*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_post_write(QUIC_XSO * xso,int did_append,int did_append_all,uint64_t flags,int do_tick)2446*e7be843bSPierre Pronchery static void quic_post_write(QUIC_XSO *xso, int did_append,
2447*e7be843bSPierre Pronchery int did_append_all, uint64_t flags,
2448*e7be843bSPierre Pronchery int do_tick)
2449*e7be843bSPierre Pronchery {
2450*e7be843bSPierre Pronchery /*
2451*e7be843bSPierre Pronchery * We have appended at least one byte to the stream.
2452*e7be843bSPierre Pronchery * Potentially mark stream as active, depending on FC.
2453*e7be843bSPierre Pronchery */
2454*e7be843bSPierre Pronchery if (did_append)
2455*e7be843bSPierre Pronchery ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch),
2456*e7be843bSPierre Pronchery xso->stream);
2457*e7be843bSPierre Pronchery
2458*e7be843bSPierre Pronchery if (did_append_all && (flags & SSL_WRITE_FLAG_CONCLUDE) != 0)
2459*e7be843bSPierre Pronchery ossl_quic_sstream_fin(xso->stream->sstream);
2460*e7be843bSPierre Pronchery
2461*e7be843bSPierre Pronchery /*
2462*e7be843bSPierre Pronchery * Try and send.
2463*e7be843bSPierre Pronchery *
2464*e7be843bSPierre Pronchery * TODO(QUIC FUTURE): It is probably inefficient to try and do this
2465*e7be843bSPierre Pronchery * immediately, plus we should eventually consider Nagle's algorithm.
2466*e7be843bSPierre Pronchery */
2467*e7be843bSPierre Pronchery if (do_tick)
2468*e7be843bSPierre Pronchery ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0);
2469*e7be843bSPierre Pronchery }
2470*e7be843bSPierre Pronchery
2471*e7be843bSPierre Pronchery struct quic_write_again_args {
2472*e7be843bSPierre Pronchery QUIC_XSO *xso;
2473*e7be843bSPierre Pronchery const unsigned char *buf;
2474*e7be843bSPierre Pronchery size_t len;
2475*e7be843bSPierre Pronchery size_t total_written;
2476*e7be843bSPierre Pronchery int err;
2477*e7be843bSPierre Pronchery uint64_t flags;
2478*e7be843bSPierre Pronchery };
2479*e7be843bSPierre Pronchery
2480*e7be843bSPierre Pronchery /*
2481*e7be843bSPierre Pronchery * Absolute maximum write buffer size, enforced to prevent a rogue peer from
2482*e7be843bSPierre Pronchery * deliberately inducing DoS. This has been chosen based on the optimal buffer
2483*e7be843bSPierre Pronchery * size for an RTT of 500ms and a bandwidth of 100 Mb/s.
2484*e7be843bSPierre Pronchery */
2485*e7be843bSPierre Pronchery #define MAX_WRITE_BUF_SIZE (6 * 1024 * 1024)
2486*e7be843bSPierre Pronchery
2487*e7be843bSPierre Pronchery /*
2488*e7be843bSPierre Pronchery * Ensure spare buffer space available (up until a limit, at least).
2489*e7be843bSPierre Pronchery */
2490*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
sstream_ensure_spare(QUIC_SSTREAM * sstream,uint64_t spare)2491*e7be843bSPierre Pronchery static int sstream_ensure_spare(QUIC_SSTREAM *sstream, uint64_t spare)
2492*e7be843bSPierre Pronchery {
2493*e7be843bSPierre Pronchery size_t cur_sz = ossl_quic_sstream_get_buffer_size(sstream);
2494*e7be843bSPierre Pronchery size_t avail = ossl_quic_sstream_get_buffer_avail(sstream);
2495*e7be843bSPierre Pronchery size_t spare_ = (spare > SIZE_MAX) ? SIZE_MAX : (size_t)spare;
2496*e7be843bSPierre Pronchery size_t new_sz, growth;
2497*e7be843bSPierre Pronchery
2498*e7be843bSPierre Pronchery if (spare_ <= avail || cur_sz == MAX_WRITE_BUF_SIZE)
2499*e7be843bSPierre Pronchery return 1;
2500*e7be843bSPierre Pronchery
2501*e7be843bSPierre Pronchery growth = spare_ - avail;
2502*e7be843bSPierre Pronchery if (cur_sz + growth > MAX_WRITE_BUF_SIZE)
2503*e7be843bSPierre Pronchery new_sz = MAX_WRITE_BUF_SIZE;
2504*e7be843bSPierre Pronchery else
2505*e7be843bSPierre Pronchery new_sz = cur_sz + growth;
2506*e7be843bSPierre Pronchery
2507*e7be843bSPierre Pronchery return ossl_quic_sstream_set_buffer_size(sstream, new_sz);
2508*e7be843bSPierre Pronchery }
2509*e7be843bSPierre Pronchery
2510*e7be843bSPierre Pronchery /*
2511*e7be843bSPierre Pronchery * Append to a QUIC_STREAM's QUIC_SSTREAM, ensuring buffer space is expanded
2512*e7be843bSPierre Pronchery * as needed according to flow control.
2513*e7be843bSPierre Pronchery */
2514*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
xso_sstream_append(QUIC_XSO * xso,const unsigned char * buf,size_t len,size_t * actual_written)2515*e7be843bSPierre Pronchery static int xso_sstream_append(QUIC_XSO *xso, const unsigned char *buf,
2516*e7be843bSPierre Pronchery size_t len, size_t *actual_written)
2517*e7be843bSPierre Pronchery {
2518*e7be843bSPierre Pronchery QUIC_SSTREAM *sstream = xso->stream->sstream;
2519*e7be843bSPierre Pronchery uint64_t cur = ossl_quic_sstream_get_cur_size(sstream);
2520*e7be843bSPierre Pronchery uint64_t cwm = ossl_quic_txfc_get_cwm(&xso->stream->txfc);
2521*e7be843bSPierre Pronchery uint64_t permitted = (cwm >= cur ? cwm - cur : 0);
2522*e7be843bSPierre Pronchery
2523*e7be843bSPierre Pronchery if (len > permitted)
2524*e7be843bSPierre Pronchery len = (size_t)permitted;
2525*e7be843bSPierre Pronchery
2526*e7be843bSPierre Pronchery if (!sstream_ensure_spare(sstream, len))
2527*e7be843bSPierre Pronchery return 0;
2528*e7be843bSPierre Pronchery
2529*e7be843bSPierre Pronchery return ossl_quic_sstream_append(sstream, buf, len, actual_written);
2530*e7be843bSPierre Pronchery }
2531*e7be843bSPierre Pronchery
2532*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_write_again(void * arg)2533*e7be843bSPierre Pronchery static int quic_write_again(void *arg)
2534*e7be843bSPierre Pronchery {
2535*e7be843bSPierre Pronchery struct quic_write_again_args *args = arg;
2536*e7be843bSPierre Pronchery size_t actual_written = 0;
2537*e7be843bSPierre Pronchery
2538*e7be843bSPierre Pronchery if (!quic_mutation_allowed(args->xso->conn, /*req_active=*/1))
2539*e7be843bSPierre Pronchery /* If connection is torn down due to an error while blocking, stop. */
2540*e7be843bSPierre Pronchery return -2;
2541*e7be843bSPierre Pronchery
2542*e7be843bSPierre Pronchery if (!quic_validate_for_write(args->xso, &args->err))
2543*e7be843bSPierre Pronchery /*
2544*e7be843bSPierre Pronchery * Stream may have become invalid for write due to connection events
2545*e7be843bSPierre Pronchery * while we blocked.
2546*e7be843bSPierre Pronchery */
2547*e7be843bSPierre Pronchery return -2;
2548*e7be843bSPierre Pronchery
2549*e7be843bSPierre Pronchery args->err = ERR_R_INTERNAL_ERROR;
2550*e7be843bSPierre Pronchery if (!xso_sstream_append(args->xso, args->buf, args->len, &actual_written))
2551*e7be843bSPierre Pronchery return -2;
2552*e7be843bSPierre Pronchery
2553*e7be843bSPierre Pronchery quic_post_write(args->xso, actual_written > 0,
2554*e7be843bSPierre Pronchery args->len == actual_written, args->flags, 0);
2555*e7be843bSPierre Pronchery
2556*e7be843bSPierre Pronchery args->buf += actual_written;
2557*e7be843bSPierre Pronchery args->len -= actual_written;
2558*e7be843bSPierre Pronchery args->total_written += actual_written;
2559*e7be843bSPierre Pronchery
2560*e7be843bSPierre Pronchery if (args->len == 0)
2561*e7be843bSPierre Pronchery /* Written everything, done. */
2562*e7be843bSPierre Pronchery return 1;
2563*e7be843bSPierre Pronchery
2564*e7be843bSPierre Pronchery /* Not written everything yet, keep trying. */
2565*e7be843bSPierre Pronchery return 0;
2566*e7be843bSPierre Pronchery }
2567*e7be843bSPierre Pronchery
2568*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_write_blocking(QCTX * ctx,const void * buf,size_t len,uint64_t flags,size_t * written)2569*e7be843bSPierre Pronchery static int quic_write_blocking(QCTX *ctx, const void *buf, size_t len,
2570*e7be843bSPierre Pronchery uint64_t flags, size_t *written)
2571*e7be843bSPierre Pronchery {
2572*e7be843bSPierre Pronchery int res;
2573*e7be843bSPierre Pronchery QUIC_XSO *xso = ctx->xso;
2574*e7be843bSPierre Pronchery struct quic_write_again_args args;
2575*e7be843bSPierre Pronchery size_t actual_written = 0;
2576*e7be843bSPierre Pronchery
2577*e7be843bSPierre Pronchery /* First make a best effort to append as much of the data as possible. */
2578*e7be843bSPierre Pronchery if (!xso_sstream_append(xso, buf, len, &actual_written)) {
2579*e7be843bSPierre Pronchery /* Stream already finished or allocation error. */
2580*e7be843bSPierre Pronchery *written = 0;
2581*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2582*e7be843bSPierre Pronchery }
2583*e7be843bSPierre Pronchery
2584*e7be843bSPierre Pronchery quic_post_write(xso, actual_written > 0, actual_written == len, flags, 1);
2585*e7be843bSPierre Pronchery
2586*e7be843bSPierre Pronchery /*
2587*e7be843bSPierre Pronchery * Record however much data we wrote
2588*e7be843bSPierre Pronchery */
2589*e7be843bSPierre Pronchery *written = actual_written;
2590*e7be843bSPierre Pronchery
2591*e7be843bSPierre Pronchery if (actual_written == len) {
2592*e7be843bSPierre Pronchery /* Managed to append everything on the first try. */
2593*e7be843bSPierre Pronchery return 1;
2594*e7be843bSPierre Pronchery }
2595*e7be843bSPierre Pronchery
2596*e7be843bSPierre Pronchery /*
2597*e7be843bSPierre Pronchery * We did not manage to append all of the data immediately, so the stream
2598*e7be843bSPierre Pronchery * buffer has probably filled up. This means we need to block until some of
2599*e7be843bSPierre Pronchery * it is freed up.
2600*e7be843bSPierre Pronchery */
2601*e7be843bSPierre Pronchery args.xso = xso;
2602*e7be843bSPierre Pronchery args.buf = (const unsigned char *)buf + actual_written;
2603*e7be843bSPierre Pronchery args.len = len - actual_written;
2604*e7be843bSPierre Pronchery args.total_written = 0;
2605*e7be843bSPierre Pronchery args.err = ERR_R_INTERNAL_ERROR;
2606*e7be843bSPierre Pronchery args.flags = flags;
2607*e7be843bSPierre Pronchery
2608*e7be843bSPierre Pronchery res = block_until_pred(ctx, quic_write_again, &args, 0);
2609*e7be843bSPierre Pronchery if (res <= 0) {
2610*e7be843bSPierre Pronchery if (!quic_mutation_allowed(xso->conn, /*req_active=*/1))
2611*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2612*e7be843bSPierre Pronchery else
2613*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, args.err, NULL);
2614*e7be843bSPierre Pronchery }
2615*e7be843bSPierre Pronchery
2616*e7be843bSPierre Pronchery /*
2617*e7be843bSPierre Pronchery * When waiting on extra buffer space to be available, args.total_written
2618*e7be843bSPierre Pronchery * holds the amount of remaining data we requested to write, which will be
2619*e7be843bSPierre Pronchery * something less than the len parameter passed in, however much we wrote
2620*e7be843bSPierre Pronchery * here, add it to the value that we wrote when we initially called
2621*e7be843bSPierre Pronchery * xso_sstream_append
2622*e7be843bSPierre Pronchery */
2623*e7be843bSPierre Pronchery *written += args.total_written;
2624*e7be843bSPierre Pronchery return 1;
2625*e7be843bSPierre Pronchery }
2626*e7be843bSPierre Pronchery
2627*e7be843bSPierre Pronchery /*
2628*e7be843bSPierre Pronchery * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
2629*e7be843bSPierre Pronchery * write semantics.
2630*e7be843bSPierre Pronchery */
aon_write_begin(QUIC_XSO * xso,const unsigned char * buf,size_t buf_len,size_t already_sent)2631*e7be843bSPierre Pronchery static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf,
2632*e7be843bSPierre Pronchery size_t buf_len, size_t already_sent)
2633*e7be843bSPierre Pronchery {
2634*e7be843bSPierre Pronchery assert(!xso->aon_write_in_progress);
2635*e7be843bSPierre Pronchery
2636*e7be843bSPierre Pronchery xso->aon_write_in_progress = 1;
2637*e7be843bSPierre Pronchery xso->aon_buf_base = buf;
2638*e7be843bSPierre Pronchery xso->aon_buf_pos = already_sent;
2639*e7be843bSPierre Pronchery xso->aon_buf_len = buf_len;
2640*e7be843bSPierre Pronchery }
2641*e7be843bSPierre Pronchery
aon_write_finish(QUIC_XSO * xso)2642*e7be843bSPierre Pronchery static void aon_write_finish(QUIC_XSO *xso)
2643*e7be843bSPierre Pronchery {
2644*e7be843bSPierre Pronchery xso->aon_write_in_progress = 0;
2645*e7be843bSPierre Pronchery xso->aon_buf_base = NULL;
2646*e7be843bSPierre Pronchery xso->aon_buf_pos = 0;
2647*e7be843bSPierre Pronchery xso->aon_buf_len = 0;
2648*e7be843bSPierre Pronchery }
2649*e7be843bSPierre Pronchery
2650*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_write_nonblocking_aon(QCTX * ctx,const void * buf,size_t len,uint64_t flags,size_t * written)2651*e7be843bSPierre Pronchery static int quic_write_nonblocking_aon(QCTX *ctx, const void *buf,
2652*e7be843bSPierre Pronchery size_t len, uint64_t flags,
2653*e7be843bSPierre Pronchery size_t *written)
2654*e7be843bSPierre Pronchery {
2655*e7be843bSPierre Pronchery QUIC_XSO *xso = ctx->xso;
2656*e7be843bSPierre Pronchery const void *actual_buf;
2657*e7be843bSPierre Pronchery size_t actual_len, actual_written = 0;
2658*e7be843bSPierre Pronchery int accept_moving_buffer
2659*e7be843bSPierre Pronchery = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
2660*e7be843bSPierre Pronchery
2661*e7be843bSPierre Pronchery if (xso->aon_write_in_progress) {
2662*e7be843bSPierre Pronchery /*
2663*e7be843bSPierre Pronchery * We are in the middle of an AON write (i.e., a previous write did not
2664*e7be843bSPierre Pronchery * manage to append all data to the SSTREAM and we have Enable Partial
2665*e7be843bSPierre Pronchery * Write (EPW) mode disabled.)
2666*e7be843bSPierre Pronchery */
2667*e7be843bSPierre Pronchery if ((!accept_moving_buffer && xso->aon_buf_base != buf)
2668*e7be843bSPierre Pronchery || len != xso->aon_buf_len)
2669*e7be843bSPierre Pronchery /*
2670*e7be843bSPierre Pronchery * Pointer must not have changed if we are not in accept moving
2671*e7be843bSPierre Pronchery * buffer mode. Length must never change.
2672*e7be843bSPierre Pronchery */
2673*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_BAD_WRITE_RETRY, NULL);
2674*e7be843bSPierre Pronchery
2675*e7be843bSPierre Pronchery actual_buf = (unsigned char *)buf + xso->aon_buf_pos;
2676*e7be843bSPierre Pronchery actual_len = len - xso->aon_buf_pos;
2677*e7be843bSPierre Pronchery assert(actual_len > 0);
2678*e7be843bSPierre Pronchery } else {
2679*e7be843bSPierre Pronchery actual_buf = buf;
2680*e7be843bSPierre Pronchery actual_len = len;
2681*e7be843bSPierre Pronchery }
2682*e7be843bSPierre Pronchery
2683*e7be843bSPierre Pronchery /* First make a best effort to append as much of the data as possible. */
2684*e7be843bSPierre Pronchery if (!xso_sstream_append(xso, actual_buf, actual_len, &actual_written)) {
2685*e7be843bSPierre Pronchery /* Stream already finished or allocation error. */
2686*e7be843bSPierre Pronchery *written = 0;
2687*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2688*e7be843bSPierre Pronchery }
2689*e7be843bSPierre Pronchery
2690*e7be843bSPierre Pronchery quic_post_write(xso, actual_written > 0, actual_written == actual_len,
2691*e7be843bSPierre Pronchery flags, qctx_should_autotick(ctx));
2692*e7be843bSPierre Pronchery
2693*e7be843bSPierre Pronchery if (actual_written == actual_len) {
2694*e7be843bSPierre Pronchery /* We have sent everything. */
2695*e7be843bSPierre Pronchery if (xso->aon_write_in_progress) {
2696*e7be843bSPierre Pronchery /*
2697*e7be843bSPierre Pronchery * We have sent everything, and we were in the middle of an AON
2698*e7be843bSPierre Pronchery * write. The output write length is the total length of the AON
2699*e7be843bSPierre Pronchery * buffer, not however many bytes we managed to write to the stream
2700*e7be843bSPierre Pronchery * in this call.
2701*e7be843bSPierre Pronchery */
2702*e7be843bSPierre Pronchery *written = xso->aon_buf_len;
2703*e7be843bSPierre Pronchery aon_write_finish(xso);
2704*e7be843bSPierre Pronchery } else {
2705*e7be843bSPierre Pronchery *written = actual_written;
2706*e7be843bSPierre Pronchery }
2707*e7be843bSPierre Pronchery
2708*e7be843bSPierre Pronchery return 1;
2709*e7be843bSPierre Pronchery }
2710*e7be843bSPierre Pronchery
2711*e7be843bSPierre Pronchery if (xso->aon_write_in_progress) {
2712*e7be843bSPierre Pronchery /*
2713*e7be843bSPierre Pronchery * AON write is in progress but we have not written everything yet. We
2714*e7be843bSPierre Pronchery * may have managed to send zero bytes, or some number of bytes less
2715*e7be843bSPierre Pronchery * than the total remaining which need to be appended during this
2716*e7be843bSPierre Pronchery * AON operation.
2717*e7be843bSPierre Pronchery */
2718*e7be843bSPierre Pronchery xso->aon_buf_pos += actual_written;
2719*e7be843bSPierre Pronchery assert(xso->aon_buf_pos < xso->aon_buf_len);
2720*e7be843bSPierre Pronchery return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2721*e7be843bSPierre Pronchery }
2722*e7be843bSPierre Pronchery
2723*e7be843bSPierre Pronchery /*
2724*e7be843bSPierre Pronchery * Not in an existing AON operation but partial write is not enabled, so we
2725*e7be843bSPierre Pronchery * need to begin a new AON operation. However we needn't bother if we didn't
2726*e7be843bSPierre Pronchery * actually append anything.
2727*e7be843bSPierre Pronchery */
2728*e7be843bSPierre Pronchery if (actual_written > 0)
2729*e7be843bSPierre Pronchery aon_write_begin(xso, buf, len, actual_written);
2730*e7be843bSPierre Pronchery
2731*e7be843bSPierre Pronchery /*
2732*e7be843bSPierre Pronchery * AON - We do not publicly admit to having appended anything until AON
2733*e7be843bSPierre Pronchery * completes.
2734*e7be843bSPierre Pronchery */
2735*e7be843bSPierre Pronchery *written = 0;
2736*e7be843bSPierre Pronchery return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2737*e7be843bSPierre Pronchery }
2738*e7be843bSPierre Pronchery
2739*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_write_nonblocking_epw(QCTX * ctx,const void * buf,size_t len,uint64_t flags,size_t * written)2740*e7be843bSPierre Pronchery static int quic_write_nonblocking_epw(QCTX *ctx, const void *buf, size_t len,
2741*e7be843bSPierre Pronchery uint64_t flags, size_t *written)
2742*e7be843bSPierre Pronchery {
2743*e7be843bSPierre Pronchery QUIC_XSO *xso = ctx->xso;
2744*e7be843bSPierre Pronchery
2745*e7be843bSPierre Pronchery /* Simple best effort operation. */
2746*e7be843bSPierre Pronchery if (!xso_sstream_append(xso, buf, len, written)) {
2747*e7be843bSPierre Pronchery /* Stream already finished or allocation error. */
2748*e7be843bSPierre Pronchery *written = 0;
2749*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2750*e7be843bSPierre Pronchery }
2751*e7be843bSPierre Pronchery
2752*e7be843bSPierre Pronchery quic_post_write(xso, *written > 0, *written == len, flags,
2753*e7be843bSPierre Pronchery qctx_should_autotick(ctx));
2754*e7be843bSPierre Pronchery
2755*e7be843bSPierre Pronchery if (*written == 0)
2756*e7be843bSPierre Pronchery /* SSL_write_ex returns 0 if it didn't write anything. */
2757*e7be843bSPierre Pronchery return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_WANT_WRITE);
2758*e7be843bSPierre Pronchery
2759*e7be843bSPierre Pronchery return 1;
2760*e7be843bSPierre Pronchery }
2761*e7be843bSPierre Pronchery
2762*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_validate_for_write(QUIC_XSO * xso,int * err)2763*e7be843bSPierre Pronchery static int quic_validate_for_write(QUIC_XSO *xso, int *err)
2764*e7be843bSPierre Pronchery {
2765*e7be843bSPierre Pronchery QUIC_STREAM_MAP *qsm;
2766*e7be843bSPierre Pronchery
2767*e7be843bSPierre Pronchery if (xso == NULL || xso->stream == NULL) {
2768*e7be843bSPierre Pronchery *err = ERR_R_INTERNAL_ERROR;
2769*e7be843bSPierre Pronchery return 0;
2770*e7be843bSPierre Pronchery }
2771*e7be843bSPierre Pronchery
2772*e7be843bSPierre Pronchery switch (xso->stream->send_state) {
2773*e7be843bSPierre Pronchery default:
2774*e7be843bSPierre Pronchery case QUIC_SSTREAM_STATE_NONE:
2775*e7be843bSPierre Pronchery *err = SSL_R_STREAM_RECV_ONLY;
2776*e7be843bSPierre Pronchery return 0;
2777*e7be843bSPierre Pronchery
2778*e7be843bSPierre Pronchery case QUIC_SSTREAM_STATE_READY:
2779*e7be843bSPierre Pronchery qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2780*e7be843bSPierre Pronchery
2781*e7be843bSPierre Pronchery if (!ossl_quic_stream_map_ensure_send_part_id(qsm, xso->stream)) {
2782*e7be843bSPierre Pronchery *err = ERR_R_INTERNAL_ERROR;
2783*e7be843bSPierre Pronchery return 0;
2784*e7be843bSPierre Pronchery }
2785*e7be843bSPierre Pronchery
2786*e7be843bSPierre Pronchery /* FALLTHROUGH */
2787*e7be843bSPierre Pronchery case QUIC_SSTREAM_STATE_SEND:
2788*e7be843bSPierre Pronchery case QUIC_SSTREAM_STATE_DATA_SENT:
2789*e7be843bSPierre Pronchery if (ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)) {
2790*e7be843bSPierre Pronchery *err = SSL_R_STREAM_FINISHED;
2791*e7be843bSPierre Pronchery return 0;
2792*e7be843bSPierre Pronchery }
2793*e7be843bSPierre Pronchery return 1;
2794*e7be843bSPierre Pronchery
2795*e7be843bSPierre Pronchery case QUIC_SSTREAM_STATE_DATA_RECVD:
2796*e7be843bSPierre Pronchery *err = SSL_R_STREAM_FINISHED;
2797*e7be843bSPierre Pronchery return 0;
2798*e7be843bSPierre Pronchery
2799*e7be843bSPierre Pronchery case QUIC_SSTREAM_STATE_RESET_SENT:
2800*e7be843bSPierre Pronchery case QUIC_SSTREAM_STATE_RESET_RECVD:
2801*e7be843bSPierre Pronchery *err = SSL_R_STREAM_RESET;
2802*e7be843bSPierre Pronchery return 0;
2803*e7be843bSPierre Pronchery }
2804*e7be843bSPierre Pronchery }
2805*e7be843bSPierre Pronchery
2806*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_write_flags(SSL * s,const void * buf,size_t len,uint64_t flags,size_t * written)2807*e7be843bSPierre Pronchery int ossl_quic_write_flags(SSL *s, const void *buf, size_t len,
2808*e7be843bSPierre Pronchery uint64_t flags, size_t *written)
2809*e7be843bSPierre Pronchery {
2810*e7be843bSPierre Pronchery int ret;
2811*e7be843bSPierre Pronchery QCTX ctx;
2812*e7be843bSPierre Pronchery int partial_write, err;
2813*e7be843bSPierre Pronchery
2814*e7be843bSPierre Pronchery *written = 0;
2815*e7be843bSPierre Pronchery
2816*e7be843bSPierre Pronchery if (len == 0) {
2817*e7be843bSPierre Pronchery /* Do not autocreate default XSO for zero-length writes. */
2818*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
2819*e7be843bSPierre Pronchery return 0;
2820*e7be843bSPierre Pronchery
2821*e7be843bSPierre Pronchery qctx_lock_for_io(&ctx);
2822*e7be843bSPierre Pronchery } else {
2823*e7be843bSPierre Pronchery if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/1, &ctx))
2824*e7be843bSPierre Pronchery return 0;
2825*e7be843bSPierre Pronchery }
2826*e7be843bSPierre Pronchery
2827*e7be843bSPierre Pronchery partial_write = ((ctx.xso != NULL)
2828*e7be843bSPierre Pronchery ? ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0) : 0);
2829*e7be843bSPierre Pronchery
2830*e7be843bSPierre Pronchery if ((flags & ~SSL_WRITE_FLAG_CONCLUDE) != 0) {
2831*e7be843bSPierre Pronchery ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_UNSUPPORTED_WRITE_FLAG, NULL);
2832*e7be843bSPierre Pronchery goto out;
2833*e7be843bSPierre Pronchery }
2834*e7be843bSPierre Pronchery
2835*e7be843bSPierre Pronchery if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
2836*e7be843bSPierre Pronchery ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
2837*e7be843bSPierre Pronchery goto out;
2838*e7be843bSPierre Pronchery }
2839*e7be843bSPierre Pronchery
2840*e7be843bSPierre Pronchery /*
2841*e7be843bSPierre Pronchery * If we haven't finished the handshake, try to advance it.
2842*e7be843bSPierre Pronchery * We don't accept writes until the handshake is completed.
2843*e7be843bSPierre Pronchery */
2844*e7be843bSPierre Pronchery if (quic_do_handshake(&ctx) < 1) {
2845*e7be843bSPierre Pronchery ret = 0;
2846*e7be843bSPierre Pronchery goto out;
2847*e7be843bSPierre Pronchery }
2848*e7be843bSPierre Pronchery
2849*e7be843bSPierre Pronchery /* Ensure correct stream state, stream send part not concluded, etc. */
2850*e7be843bSPierre Pronchery if (len > 0 && !quic_validate_for_write(ctx.xso, &err)) {
2851*e7be843bSPierre Pronchery ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
2852*e7be843bSPierre Pronchery goto out;
2853*e7be843bSPierre Pronchery }
2854*e7be843bSPierre Pronchery
2855*e7be843bSPierre Pronchery if (len == 0) {
2856*e7be843bSPierre Pronchery if ((flags & SSL_WRITE_FLAG_CONCLUDE) != 0)
2857*e7be843bSPierre Pronchery quic_post_write(ctx.xso, 0, 1, flags,
2858*e7be843bSPierre Pronchery qctx_should_autotick(&ctx));
2859*e7be843bSPierre Pronchery
2860*e7be843bSPierre Pronchery ret = 1;
2861*e7be843bSPierre Pronchery goto out;
2862*e7be843bSPierre Pronchery }
2863*e7be843bSPierre Pronchery
2864*e7be843bSPierre Pronchery if (qctx_blocking(&ctx))
2865*e7be843bSPierre Pronchery ret = quic_write_blocking(&ctx, buf, len, flags, written);
2866*e7be843bSPierre Pronchery else if (partial_write)
2867*e7be843bSPierre Pronchery ret = quic_write_nonblocking_epw(&ctx, buf, len, flags, written);
2868*e7be843bSPierre Pronchery else
2869*e7be843bSPierre Pronchery ret = quic_write_nonblocking_aon(&ctx, buf, len, flags, written);
2870*e7be843bSPierre Pronchery
2871*e7be843bSPierre Pronchery out:
2872*e7be843bSPierre Pronchery qctx_unlock(&ctx);
2873*e7be843bSPierre Pronchery return ret;
2874*e7be843bSPierre Pronchery }
2875*e7be843bSPierre Pronchery
2876*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_write(SSL * s,const void * buf,size_t len,size_t * written)2877*e7be843bSPierre Pronchery int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
2878*e7be843bSPierre Pronchery {
2879*e7be843bSPierre Pronchery return ossl_quic_write_flags(s, buf, len, 0, written);
2880*e7be843bSPierre Pronchery }
2881*e7be843bSPierre Pronchery
2882*e7be843bSPierre Pronchery /*
2883*e7be843bSPierre Pronchery * SSL_read
2884*e7be843bSPierre Pronchery * --------
2885*e7be843bSPierre Pronchery */
2886*e7be843bSPierre Pronchery struct quic_read_again_args {
2887*e7be843bSPierre Pronchery QCTX *ctx;
2888*e7be843bSPierre Pronchery QUIC_STREAM *stream;
2889*e7be843bSPierre Pronchery void *buf;
2890*e7be843bSPierre Pronchery size_t len;
2891*e7be843bSPierre Pronchery size_t *bytes_read;
2892*e7be843bSPierre Pronchery int peek;
2893*e7be843bSPierre Pronchery };
2894*e7be843bSPierre Pronchery
2895*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_validate_for_read(QUIC_XSO * xso,int * err,int * eos)2896*e7be843bSPierre Pronchery static int quic_validate_for_read(QUIC_XSO *xso, int *err, int *eos)
2897*e7be843bSPierre Pronchery {
2898*e7be843bSPierre Pronchery QUIC_STREAM_MAP *qsm;
2899*e7be843bSPierre Pronchery
2900*e7be843bSPierre Pronchery *eos = 0;
2901*e7be843bSPierre Pronchery
2902*e7be843bSPierre Pronchery if (xso == NULL || xso->stream == NULL) {
2903*e7be843bSPierre Pronchery *err = ERR_R_INTERNAL_ERROR;
2904*e7be843bSPierre Pronchery return 0;
2905*e7be843bSPierre Pronchery }
2906*e7be843bSPierre Pronchery
2907*e7be843bSPierre Pronchery switch (xso->stream->recv_state) {
2908*e7be843bSPierre Pronchery default:
2909*e7be843bSPierre Pronchery case QUIC_RSTREAM_STATE_NONE:
2910*e7be843bSPierre Pronchery *err = SSL_R_STREAM_SEND_ONLY;
2911*e7be843bSPierre Pronchery return 0;
2912*e7be843bSPierre Pronchery
2913*e7be843bSPierre Pronchery case QUIC_RSTREAM_STATE_RECV:
2914*e7be843bSPierre Pronchery case QUIC_RSTREAM_STATE_SIZE_KNOWN:
2915*e7be843bSPierre Pronchery case QUIC_RSTREAM_STATE_DATA_RECVD:
2916*e7be843bSPierre Pronchery return 1;
2917*e7be843bSPierre Pronchery
2918*e7be843bSPierre Pronchery case QUIC_RSTREAM_STATE_DATA_READ:
2919*e7be843bSPierre Pronchery *eos = 1;
2920*e7be843bSPierre Pronchery return 0;
2921*e7be843bSPierre Pronchery
2922*e7be843bSPierre Pronchery case QUIC_RSTREAM_STATE_RESET_RECVD:
2923*e7be843bSPierre Pronchery qsm = ossl_quic_channel_get_qsm(xso->conn->ch);
2924*e7be843bSPierre Pronchery ossl_quic_stream_map_notify_app_read_reset_recv_part(qsm, xso->stream);
2925*e7be843bSPierre Pronchery
2926*e7be843bSPierre Pronchery /* FALLTHROUGH */
2927*e7be843bSPierre Pronchery case QUIC_RSTREAM_STATE_RESET_READ:
2928*e7be843bSPierre Pronchery *err = SSL_R_STREAM_RESET;
2929*e7be843bSPierre Pronchery return 0;
2930*e7be843bSPierre Pronchery }
2931*e7be843bSPierre Pronchery }
2932*e7be843bSPierre Pronchery
2933*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_read_actual(QCTX * ctx,QUIC_STREAM * stream,void * buf,size_t buf_len,size_t * bytes_read,int peek)2934*e7be843bSPierre Pronchery static int quic_read_actual(QCTX *ctx,
2935*e7be843bSPierre Pronchery QUIC_STREAM *stream,
2936*e7be843bSPierre Pronchery void *buf, size_t buf_len,
2937*e7be843bSPierre Pronchery size_t *bytes_read,
2938*e7be843bSPierre Pronchery int peek)
2939*e7be843bSPierre Pronchery {
2940*e7be843bSPierre Pronchery int is_fin = 0, err, eos;
2941*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = ctx->qc;
2942*e7be843bSPierre Pronchery
2943*e7be843bSPierre Pronchery if (!quic_validate_for_read(ctx->xso, &err, &eos)) {
2944*e7be843bSPierre Pronchery if (eos) {
2945*e7be843bSPierre Pronchery ctx->xso->retired_fin = 1;
2946*e7be843bSPierre Pronchery return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2947*e7be843bSPierre Pronchery } else {
2948*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, err, NULL);
2949*e7be843bSPierre Pronchery }
2950*e7be843bSPierre Pronchery }
2951*e7be843bSPierre Pronchery
2952*e7be843bSPierre Pronchery if (peek) {
2953*e7be843bSPierre Pronchery if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
2954*e7be843bSPierre Pronchery bytes_read, &is_fin))
2955*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2956*e7be843bSPierre Pronchery
2957*e7be843bSPierre Pronchery } else {
2958*e7be843bSPierre Pronchery if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
2959*e7be843bSPierre Pronchery bytes_read, &is_fin))
2960*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2961*e7be843bSPierre Pronchery }
2962*e7be843bSPierre Pronchery
2963*e7be843bSPierre Pronchery if (!peek) {
2964*e7be843bSPierre Pronchery if (*bytes_read > 0) {
2965*e7be843bSPierre Pronchery /*
2966*e7be843bSPierre Pronchery * We have read at least one byte from the stream. Inform stream-level
2967*e7be843bSPierre Pronchery * RXFC of the retirement of controlled bytes. Update the active stream
2968*e7be843bSPierre Pronchery * status (the RXFC may now want to emit a frame granting more credit to
2969*e7be843bSPierre Pronchery * the peer).
2970*e7be843bSPierre Pronchery */
2971*e7be843bSPierre Pronchery OSSL_RTT_INFO rtt_info;
2972*e7be843bSPierre Pronchery
2973*e7be843bSPierre Pronchery ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
2974*e7be843bSPierre Pronchery
2975*e7be843bSPierre Pronchery if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read,
2976*e7be843bSPierre Pronchery rtt_info.smoothed_rtt))
2977*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_INTERNAL_ERROR, NULL);
2978*e7be843bSPierre Pronchery }
2979*e7be843bSPierre Pronchery
2980*e7be843bSPierre Pronchery if (is_fin && !peek) {
2981*e7be843bSPierre Pronchery QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(ctx->qc->ch);
2982*e7be843bSPierre Pronchery
2983*e7be843bSPierre Pronchery ossl_quic_stream_map_notify_totally_read(qsm, ctx->xso->stream);
2984*e7be843bSPierre Pronchery }
2985*e7be843bSPierre Pronchery
2986*e7be843bSPierre Pronchery if (*bytes_read > 0)
2987*e7be843bSPierre Pronchery ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
2988*e7be843bSPierre Pronchery stream);
2989*e7be843bSPierre Pronchery }
2990*e7be843bSPierre Pronchery
2991*e7be843bSPierre Pronchery if (*bytes_read == 0 && is_fin) {
2992*e7be843bSPierre Pronchery ctx->xso->retired_fin = 1;
2993*e7be843bSPierre Pronchery return QUIC_RAISE_NORMAL_ERROR(ctx, SSL_ERROR_ZERO_RETURN);
2994*e7be843bSPierre Pronchery }
2995*e7be843bSPierre Pronchery
2996*e7be843bSPierre Pronchery return 1;
2997*e7be843bSPierre Pronchery }
2998*e7be843bSPierre Pronchery
2999*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
quic_read_again(void * arg)3000*e7be843bSPierre Pronchery static int quic_read_again(void *arg)
3001*e7be843bSPierre Pronchery {
3002*e7be843bSPierre Pronchery struct quic_read_again_args *args = arg;
3003*e7be843bSPierre Pronchery
3004*e7be843bSPierre Pronchery if (!quic_mutation_allowed(args->ctx->qc, /*req_active=*/1)) {
3005*e7be843bSPierre Pronchery /* If connection is torn down due to an error while blocking, stop. */
3006*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3007*e7be843bSPierre Pronchery return -1;
3008*e7be843bSPierre Pronchery }
3009*e7be843bSPierre Pronchery
3010*e7be843bSPierre Pronchery if (!quic_read_actual(args->ctx, args->stream,
3011*e7be843bSPierre Pronchery args->buf, args->len, args->bytes_read,
3012*e7be843bSPierre Pronchery args->peek))
3013*e7be843bSPierre Pronchery return -1;
3014*e7be843bSPierre Pronchery
3015*e7be843bSPierre Pronchery if (*args->bytes_read > 0)
3016*e7be843bSPierre Pronchery /* got at least one byte, the SSL_read op can finish now */
3017*e7be843bSPierre Pronchery return 1;
3018*e7be843bSPierre Pronchery
3019*e7be843bSPierre Pronchery return 0; /* did not read anything, keep trying */
3020*e7be843bSPierre Pronchery }
3021*e7be843bSPierre Pronchery
3022*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
quic_read(SSL * s,void * buf,size_t len,size_t * bytes_read,int peek)3023*e7be843bSPierre Pronchery static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
3024*e7be843bSPierre Pronchery {
3025*e7be843bSPierre Pronchery int ret, res;
3026*e7be843bSPierre Pronchery QCTX ctx;
3027*e7be843bSPierre Pronchery struct quic_read_again_args args;
3028*e7be843bSPierre Pronchery
3029*e7be843bSPierre Pronchery *bytes_read = 0;
3030*e7be843bSPierre Pronchery
3031*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
3032*e7be843bSPierre Pronchery return 0;
3033*e7be843bSPierre Pronchery
3034*e7be843bSPierre Pronchery qctx_lock_for_io(&ctx);
3035*e7be843bSPierre Pronchery
3036*e7be843bSPierre Pronchery /* If we haven't finished the handshake, try to advance it. */
3037*e7be843bSPierre Pronchery if (quic_do_handshake(&ctx) < 1) {
3038*e7be843bSPierre Pronchery ret = 0; /* ossl_quic_do_handshake raised error here */
3039*e7be843bSPierre Pronchery goto out;
3040*e7be843bSPierre Pronchery }
3041*e7be843bSPierre Pronchery
3042*e7be843bSPierre Pronchery if (ctx.xso == NULL) {
3043*e7be843bSPierre Pronchery /*
3044*e7be843bSPierre Pronchery * Called on a QCSO and we don't currently have a default stream.
3045*e7be843bSPierre Pronchery *
3046*e7be843bSPierre Pronchery * Wait until we get a stream initiated by the peer (blocking mode) or
3047*e7be843bSPierre Pronchery * fail if we don't have one yet (non-blocking mode).
3048*e7be843bSPierre Pronchery */
3049*e7be843bSPierre Pronchery if (!qc_wait_for_default_xso_for_read(&ctx, /*peek=*/0)) {
3050*e7be843bSPierre Pronchery ret = 0; /* error already raised here */
3051*e7be843bSPierre Pronchery goto out;
3052*e7be843bSPierre Pronchery }
3053*e7be843bSPierre Pronchery
3054*e7be843bSPierre Pronchery ctx.xso = ctx.qc->default_xso;
3055*e7be843bSPierre Pronchery }
3056*e7be843bSPierre Pronchery
3057*e7be843bSPierre Pronchery if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
3058*e7be843bSPierre Pronchery ret = 0; /* quic_read_actual raised error here */
3059*e7be843bSPierre Pronchery goto out;
3060*e7be843bSPierre Pronchery }
3061*e7be843bSPierre Pronchery
3062*e7be843bSPierre Pronchery if (*bytes_read > 0) {
3063*e7be843bSPierre Pronchery /*
3064*e7be843bSPierre Pronchery * Even though we succeeded, tick the reactor here to ensure we are
3065*e7be843bSPierre Pronchery * handling other aspects of the QUIC connection.
3066*e7be843bSPierre Pronchery */
3067*e7be843bSPierre Pronchery if (quic_mutation_allowed(ctx.qc, /*req_active=*/0))
3068*e7be843bSPierre Pronchery qctx_maybe_autotick(&ctx);
3069*e7be843bSPierre Pronchery
3070*e7be843bSPierre Pronchery ret = 1;
3071*e7be843bSPierre Pronchery } else if (!quic_mutation_allowed(ctx.qc, /*req_active=*/0)) {
3072*e7be843bSPierre Pronchery ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3073*e7be843bSPierre Pronchery goto out;
3074*e7be843bSPierre Pronchery } else if (qctx_blocking(&ctx)) {
3075*e7be843bSPierre Pronchery /*
3076*e7be843bSPierre Pronchery * We were not able to read anything immediately, so our stream
3077*e7be843bSPierre Pronchery * buffer is empty. This means we need to block until we get
3078*e7be843bSPierre Pronchery * at least one byte.
3079*e7be843bSPierre Pronchery */
3080*e7be843bSPierre Pronchery args.ctx = &ctx;
3081*e7be843bSPierre Pronchery args.stream = ctx.xso->stream;
3082*e7be843bSPierre Pronchery args.buf = buf;
3083*e7be843bSPierre Pronchery args.len = len;
3084*e7be843bSPierre Pronchery args.bytes_read = bytes_read;
3085*e7be843bSPierre Pronchery args.peek = peek;
3086*e7be843bSPierre Pronchery
3087*e7be843bSPierre Pronchery res = block_until_pred(&ctx, quic_read_again, &args, 0);
3088*e7be843bSPierre Pronchery if (res == 0) {
3089*e7be843bSPierre Pronchery ret = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3090*e7be843bSPierre Pronchery goto out;
3091*e7be843bSPierre Pronchery } else if (res < 0) {
3092*e7be843bSPierre Pronchery ret = 0; /* quic_read_again raised error here */
3093*e7be843bSPierre Pronchery goto out;
3094*e7be843bSPierre Pronchery }
3095*e7be843bSPierre Pronchery
3096*e7be843bSPierre Pronchery ret = 1;
3097*e7be843bSPierre Pronchery } else {
3098*e7be843bSPierre Pronchery /*
3099*e7be843bSPierre Pronchery * We did not get any bytes and are not in blocking mode.
3100*e7be843bSPierre Pronchery * Tick to see if this delivers any more.
3101*e7be843bSPierre Pronchery */
3102*e7be843bSPierre Pronchery qctx_maybe_autotick(&ctx);
3103*e7be843bSPierre Pronchery
3104*e7be843bSPierre Pronchery /* Try the read again. */
3105*e7be843bSPierre Pronchery if (!quic_read_actual(&ctx, ctx.xso->stream, buf, len, bytes_read, peek)) {
3106*e7be843bSPierre Pronchery ret = 0; /* quic_read_actual raised error here */
3107*e7be843bSPierre Pronchery goto out;
3108*e7be843bSPierre Pronchery }
3109*e7be843bSPierre Pronchery
3110*e7be843bSPierre Pronchery if (*bytes_read > 0)
3111*e7be843bSPierre Pronchery ret = 1; /* Succeeded this time. */
3112*e7be843bSPierre Pronchery else
3113*e7be843bSPierre Pronchery ret = QUIC_RAISE_NORMAL_ERROR(&ctx, SSL_ERROR_WANT_READ);
3114*e7be843bSPierre Pronchery }
3115*e7be843bSPierre Pronchery
3116*e7be843bSPierre Pronchery out:
3117*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3118*e7be843bSPierre Pronchery return ret;
3119*e7be843bSPierre Pronchery }
3120*e7be843bSPierre Pronchery
ossl_quic_read(SSL * s,void * buf,size_t len,size_t * bytes_read)3121*e7be843bSPierre Pronchery int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
3122*e7be843bSPierre Pronchery {
3123*e7be843bSPierre Pronchery return quic_read(s, buf, len, bytes_read, 0);
3124*e7be843bSPierre Pronchery }
3125*e7be843bSPierre Pronchery
ossl_quic_peek(SSL * s,void * buf,size_t len,size_t * bytes_read)3126*e7be843bSPierre Pronchery int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
3127*e7be843bSPierre Pronchery {
3128*e7be843bSPierre Pronchery return quic_read(s, buf, len, bytes_read, 1);
3129*e7be843bSPierre Pronchery }
3130*e7be843bSPierre Pronchery
3131*e7be843bSPierre Pronchery /*
3132*e7be843bSPierre Pronchery * SSL_pending
3133*e7be843bSPierre Pronchery * -----------
3134*e7be843bSPierre Pronchery */
3135*e7be843bSPierre Pronchery
3136*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_pending_int(const SSL * s,int check_channel)3137*e7be843bSPierre Pronchery static size_t ossl_quic_pending_int(const SSL *s, int check_channel)
3138*e7be843bSPierre Pronchery {
3139*e7be843bSPierre Pronchery QCTX ctx;
3140*e7be843bSPierre Pronchery size_t avail = 0;
3141*e7be843bSPierre Pronchery
3142*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
3143*e7be843bSPierre Pronchery return 0;
3144*e7be843bSPierre Pronchery
3145*e7be843bSPierre Pronchery qctx_lock(&ctx);
3146*e7be843bSPierre Pronchery
3147*e7be843bSPierre Pronchery if (!ctx.qc->started)
3148*e7be843bSPierre Pronchery goto out;
3149*e7be843bSPierre Pronchery
3150*e7be843bSPierre Pronchery if (ctx.xso == NULL) {
3151*e7be843bSPierre Pronchery /* No XSO yet, but there might be a default XSO eligible to be created. */
3152*e7be843bSPierre Pronchery if (qc_wait_for_default_xso_for_read(&ctx, /*peek=*/1)) {
3153*e7be843bSPierre Pronchery ctx.xso = ctx.qc->default_xso;
3154*e7be843bSPierre Pronchery } else {
3155*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_NO_STREAM, NULL);
3156*e7be843bSPierre Pronchery goto out;
3157*e7be843bSPierre Pronchery }
3158*e7be843bSPierre Pronchery }
3159*e7be843bSPierre Pronchery
3160*e7be843bSPierre Pronchery if (ctx.xso->stream == NULL) {
3161*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3162*e7be843bSPierre Pronchery goto out;
3163*e7be843bSPierre Pronchery }
3164*e7be843bSPierre Pronchery
3165*e7be843bSPierre Pronchery if (check_channel)
3166*e7be843bSPierre Pronchery avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
3167*e7be843bSPierre Pronchery /*include_fin=*/1)
3168*e7be843bSPierre Pronchery || ossl_quic_channel_has_pending(ctx.qc->ch)
3169*e7be843bSPierre Pronchery || ossl_quic_channel_is_term_any(ctx.qc->ch);
3170*e7be843bSPierre Pronchery else
3171*e7be843bSPierre Pronchery avail = ossl_quic_stream_recv_pending(ctx.xso->stream,
3172*e7be843bSPierre Pronchery /*include_fin=*/0);
3173*e7be843bSPierre Pronchery
3174*e7be843bSPierre Pronchery out:
3175*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3176*e7be843bSPierre Pronchery return avail;
3177*e7be843bSPierre Pronchery }
3178*e7be843bSPierre Pronchery
ossl_quic_pending(const SSL * s)3179*e7be843bSPierre Pronchery size_t ossl_quic_pending(const SSL *s)
3180*e7be843bSPierre Pronchery {
3181*e7be843bSPierre Pronchery return ossl_quic_pending_int(s, /*check_channel=*/0);
3182*e7be843bSPierre Pronchery }
3183*e7be843bSPierre Pronchery
ossl_quic_has_pending(const SSL * s)3184*e7be843bSPierre Pronchery int ossl_quic_has_pending(const SSL *s)
3185*e7be843bSPierre Pronchery {
3186*e7be843bSPierre Pronchery /* Do we have app-side pending data or pending URXEs or RXEs? */
3187*e7be843bSPierre Pronchery return ossl_quic_pending_int(s, /*check_channel=*/1) > 0;
3188*e7be843bSPierre Pronchery }
3189*e7be843bSPierre Pronchery
3190*e7be843bSPierre Pronchery /*
3191*e7be843bSPierre Pronchery * SSL_stream_conclude
3192*e7be843bSPierre Pronchery * -------------------
3193*e7be843bSPierre Pronchery */
3194*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_conn_stream_conclude(SSL * s)3195*e7be843bSPierre Pronchery int ossl_quic_conn_stream_conclude(SSL *s)
3196*e7be843bSPierre Pronchery {
3197*e7be843bSPierre Pronchery QCTX ctx;
3198*e7be843bSPierre Pronchery QUIC_STREAM *qs;
3199*e7be843bSPierre Pronchery int err;
3200*e7be843bSPierre Pronchery
3201*e7be843bSPierre Pronchery if (!expect_quic_with_stream_lock(s, /*remote_init=*/0, /*io=*/0, &ctx))
3202*e7be843bSPierre Pronchery return 0;
3203*e7be843bSPierre Pronchery
3204*e7be843bSPierre Pronchery qs = ctx.xso->stream;
3205*e7be843bSPierre Pronchery
3206*e7be843bSPierre Pronchery if (!quic_mutation_allowed(ctx.qc, /*req_active=*/1)) {
3207*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3208*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3209*e7be843bSPierre Pronchery }
3210*e7be843bSPierre Pronchery
3211*e7be843bSPierre Pronchery if (!quic_validate_for_write(ctx.xso, &err)) {
3212*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3213*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
3214*e7be843bSPierre Pronchery }
3215*e7be843bSPierre Pronchery
3216*e7be843bSPierre Pronchery if (ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
3217*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3218*e7be843bSPierre Pronchery return 1;
3219*e7be843bSPierre Pronchery }
3220*e7be843bSPierre Pronchery
3221*e7be843bSPierre Pronchery ossl_quic_sstream_fin(qs->sstream);
3222*e7be843bSPierre Pronchery quic_post_write(ctx.xso, 1, 0, 0, qctx_should_autotick(&ctx));
3223*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3224*e7be843bSPierre Pronchery return 1;
3225*e7be843bSPierre Pronchery }
3226*e7be843bSPierre Pronchery
3227*e7be843bSPierre Pronchery /*
3228*e7be843bSPierre Pronchery * SSL_inject_net_dgram
3229*e7be843bSPierre Pronchery * --------------------
3230*e7be843bSPierre Pronchery */
3231*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
SSL_inject_net_dgram(SSL * s,const unsigned char * buf,size_t buf_len,const BIO_ADDR * peer,const BIO_ADDR * local)3232*e7be843bSPierre Pronchery int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
3233*e7be843bSPierre Pronchery size_t buf_len,
3234*e7be843bSPierre Pronchery const BIO_ADDR *peer,
3235*e7be843bSPierre Pronchery const BIO_ADDR *local)
3236*e7be843bSPierre Pronchery {
3237*e7be843bSPierre Pronchery int ret = 0;
3238*e7be843bSPierre Pronchery QCTX ctx;
3239*e7be843bSPierre Pronchery QUIC_DEMUX *demux;
3240*e7be843bSPierre Pronchery QUIC_PORT *port;
3241*e7be843bSPierre Pronchery
3242*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
3243*e7be843bSPierre Pronchery return 0;
3244*e7be843bSPierre Pronchery
3245*e7be843bSPierre Pronchery qctx_lock(&ctx);
3246*e7be843bSPierre Pronchery
3247*e7be843bSPierre Pronchery port = ossl_quic_obj_get0_port(ctx.obj);
3248*e7be843bSPierre Pronchery if (port == NULL) {
3249*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_UNSUPPORTED, NULL);
3250*e7be843bSPierre Pronchery goto err;
3251*e7be843bSPierre Pronchery }
3252*e7be843bSPierre Pronchery
3253*e7be843bSPierre Pronchery demux = ossl_quic_port_get0_demux(port);
3254*e7be843bSPierre Pronchery ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
3255*e7be843bSPierre Pronchery
3256*e7be843bSPierre Pronchery err:
3257*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3258*e7be843bSPierre Pronchery return ret;
3259*e7be843bSPierre Pronchery }
3260*e7be843bSPierre Pronchery
3261*e7be843bSPierre Pronchery /*
3262*e7be843bSPierre Pronchery * SSL_get0_connection
3263*e7be843bSPierre Pronchery * -------------------
3264*e7be843bSPierre Pronchery */
ossl_quic_get0_connection(SSL * s)3265*e7be843bSPierre Pronchery SSL *ossl_quic_get0_connection(SSL *s)
3266*e7be843bSPierre Pronchery {
3267*e7be843bSPierre Pronchery QCTX ctx;
3268*e7be843bSPierre Pronchery
3269*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
3270*e7be843bSPierre Pronchery return NULL;
3271*e7be843bSPierre Pronchery
3272*e7be843bSPierre Pronchery return &ctx.qc->obj.ssl;
3273*e7be843bSPierre Pronchery }
3274*e7be843bSPierre Pronchery
3275*e7be843bSPierre Pronchery /*
3276*e7be843bSPierre Pronchery * SSL_get0_listener
3277*e7be843bSPierre Pronchery * -----------------
3278*e7be843bSPierre Pronchery */
ossl_quic_get0_listener(SSL * s)3279*e7be843bSPierre Pronchery SSL *ossl_quic_get0_listener(SSL *s)
3280*e7be843bSPierre Pronchery {
3281*e7be843bSPierre Pronchery QCTX ctx;
3282*e7be843bSPierre Pronchery
3283*e7be843bSPierre Pronchery if (!expect_quic_csl(s, &ctx))
3284*e7be843bSPierre Pronchery return NULL;
3285*e7be843bSPierre Pronchery
3286*e7be843bSPierre Pronchery return ctx.ql != NULL ? &ctx.ql->obj.ssl : NULL;
3287*e7be843bSPierre Pronchery }
3288*e7be843bSPierre Pronchery
3289*e7be843bSPierre Pronchery /*
3290*e7be843bSPierre Pronchery * SSL_get0_domain
3291*e7be843bSPierre Pronchery * ---------------
3292*e7be843bSPierre Pronchery */
ossl_quic_get0_domain(SSL * s)3293*e7be843bSPierre Pronchery SSL *ossl_quic_get0_domain(SSL *s)
3294*e7be843bSPierre Pronchery {
3295*e7be843bSPierre Pronchery QCTX ctx;
3296*e7be843bSPierre Pronchery
3297*e7be843bSPierre Pronchery if (!expect_quic_any(s, &ctx))
3298*e7be843bSPierre Pronchery return NULL;
3299*e7be843bSPierre Pronchery
3300*e7be843bSPierre Pronchery return ctx.qd != NULL ? &ctx.qd->obj.ssl : NULL;
3301*e7be843bSPierre Pronchery }
3302*e7be843bSPierre Pronchery
3303*e7be843bSPierre Pronchery /*
3304*e7be843bSPierre Pronchery * SSL_get_domain_flags
3305*e7be843bSPierre Pronchery * --------------------
3306*e7be843bSPierre Pronchery */
ossl_quic_get_domain_flags(const SSL * ssl,uint64_t * domain_flags)3307*e7be843bSPierre Pronchery int ossl_quic_get_domain_flags(const SSL *ssl, uint64_t *domain_flags)
3308*e7be843bSPierre Pronchery {
3309*e7be843bSPierre Pronchery QCTX ctx;
3310*e7be843bSPierre Pronchery
3311*e7be843bSPierre Pronchery if (!expect_quic_any(ssl, &ctx))
3312*e7be843bSPierre Pronchery return 0;
3313*e7be843bSPierre Pronchery
3314*e7be843bSPierre Pronchery if (domain_flags != NULL)
3315*e7be843bSPierre Pronchery *domain_flags = ctx.obj->domain_flags;
3316*e7be843bSPierre Pronchery
3317*e7be843bSPierre Pronchery return 1;
3318*e7be843bSPierre Pronchery }
3319*e7be843bSPierre Pronchery
3320*e7be843bSPierre Pronchery /*
3321*e7be843bSPierre Pronchery * SSL_get_stream_type
3322*e7be843bSPierre Pronchery * -------------------
3323*e7be843bSPierre Pronchery */
ossl_quic_get_stream_type(SSL * s)3324*e7be843bSPierre Pronchery int ossl_quic_get_stream_type(SSL *s)
3325*e7be843bSPierre Pronchery {
3326*e7be843bSPierre Pronchery QCTX ctx;
3327*e7be843bSPierre Pronchery
3328*e7be843bSPierre Pronchery if (!expect_quic_cs(s, &ctx))
3329*e7be843bSPierre Pronchery return SSL_STREAM_TYPE_BIDI;
3330*e7be843bSPierre Pronchery
3331*e7be843bSPierre Pronchery if (ctx.xso == NULL) {
3332*e7be843bSPierre Pronchery /*
3333*e7be843bSPierre Pronchery * If deferred XSO creation has yet to occur, proceed according to the
3334*e7be843bSPierre Pronchery * default stream mode. If AUTO_BIDI or AUTO_UNI is set, we cannot know
3335*e7be843bSPierre Pronchery * what kind of stream will be created yet, so return BIDI on the basis
3336*e7be843bSPierre Pronchery * that at this time, the client still has the option of calling
3337*e7be843bSPierre Pronchery * SSL_read() or SSL_write() first.
3338*e7be843bSPierre Pronchery */
3339*e7be843bSPierre Pronchery if (ctx.qc->default_xso_created
3340*e7be843bSPierre Pronchery || ctx.qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
3341*e7be843bSPierre Pronchery return SSL_STREAM_TYPE_NONE;
3342*e7be843bSPierre Pronchery else
3343*e7be843bSPierre Pronchery return SSL_STREAM_TYPE_BIDI;
3344*e7be843bSPierre Pronchery }
3345*e7be843bSPierre Pronchery
3346*e7be843bSPierre Pronchery if (ossl_quic_stream_is_bidi(ctx.xso->stream))
3347*e7be843bSPierre Pronchery return SSL_STREAM_TYPE_BIDI;
3348*e7be843bSPierre Pronchery
3349*e7be843bSPierre Pronchery if (ossl_quic_stream_is_server_init(ctx.xso->stream) != ctx.qc->as_server)
3350*e7be843bSPierre Pronchery return SSL_STREAM_TYPE_READ;
3351*e7be843bSPierre Pronchery else
3352*e7be843bSPierre Pronchery return SSL_STREAM_TYPE_WRITE;
3353*e7be843bSPierre Pronchery }
3354*e7be843bSPierre Pronchery
3355*e7be843bSPierre Pronchery /*
3356*e7be843bSPierre Pronchery * SSL_get_stream_id
3357*e7be843bSPierre Pronchery * -----------------
3358*e7be843bSPierre Pronchery */
3359*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_get_stream_id(SSL * s)3360*e7be843bSPierre Pronchery uint64_t ossl_quic_get_stream_id(SSL *s)
3361*e7be843bSPierre Pronchery {
3362*e7be843bSPierre Pronchery QCTX ctx;
3363*e7be843bSPierre Pronchery uint64_t id;
3364*e7be843bSPierre Pronchery
3365*e7be843bSPierre Pronchery if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
3366*e7be843bSPierre Pronchery return UINT64_MAX;
3367*e7be843bSPierre Pronchery
3368*e7be843bSPierre Pronchery id = ctx.xso->stream->id;
3369*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3370*e7be843bSPierre Pronchery
3371*e7be843bSPierre Pronchery return id;
3372*e7be843bSPierre Pronchery }
3373*e7be843bSPierre Pronchery
3374*e7be843bSPierre Pronchery /*
3375*e7be843bSPierre Pronchery * SSL_is_stream_local
3376*e7be843bSPierre Pronchery * -------------------
3377*e7be843bSPierre Pronchery */
3378*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_is_stream_local(SSL * s)3379*e7be843bSPierre Pronchery int ossl_quic_is_stream_local(SSL *s)
3380*e7be843bSPierre Pronchery {
3381*e7be843bSPierre Pronchery QCTX ctx;
3382*e7be843bSPierre Pronchery int is_local;
3383*e7be843bSPierre Pronchery
3384*e7be843bSPierre Pronchery if (!expect_quic_with_stream_lock(s, /*remote_init=*/-1, /*io=*/0, &ctx))
3385*e7be843bSPierre Pronchery return -1;
3386*e7be843bSPierre Pronchery
3387*e7be843bSPierre Pronchery is_local = ossl_quic_stream_is_local_init(ctx.xso->stream);
3388*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3389*e7be843bSPierre Pronchery
3390*e7be843bSPierre Pronchery return is_local;
3391*e7be843bSPierre Pronchery }
3392*e7be843bSPierre Pronchery
3393*e7be843bSPierre Pronchery /*
3394*e7be843bSPierre Pronchery * SSL_set_default_stream_mode
3395*e7be843bSPierre Pronchery * ---------------------------
3396*e7be843bSPierre Pronchery */
3397*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_set_default_stream_mode(SSL * s,uint32_t mode)3398*e7be843bSPierre Pronchery int ossl_quic_set_default_stream_mode(SSL *s, uint32_t mode)
3399*e7be843bSPierre Pronchery {
3400*e7be843bSPierre Pronchery QCTX ctx;
3401*e7be843bSPierre Pronchery
3402*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
3403*e7be843bSPierre Pronchery return 0;
3404*e7be843bSPierre Pronchery
3405*e7be843bSPierre Pronchery qctx_lock(&ctx);
3406*e7be843bSPierre Pronchery
3407*e7be843bSPierre Pronchery if (ctx.qc->default_xso_created) {
3408*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3409*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3410*e7be843bSPierre Pronchery "too late to change default stream mode");
3411*e7be843bSPierre Pronchery }
3412*e7be843bSPierre Pronchery
3413*e7be843bSPierre Pronchery switch (mode) {
3414*e7be843bSPierre Pronchery case SSL_DEFAULT_STREAM_MODE_NONE:
3415*e7be843bSPierre Pronchery case SSL_DEFAULT_STREAM_MODE_AUTO_BIDI:
3416*e7be843bSPierre Pronchery case SSL_DEFAULT_STREAM_MODE_AUTO_UNI:
3417*e7be843bSPierre Pronchery ctx.qc->default_stream_mode = mode;
3418*e7be843bSPierre Pronchery break;
3419*e7be843bSPierre Pronchery default:
3420*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3421*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3422*e7be843bSPierre Pronchery "bad default stream type");
3423*e7be843bSPierre Pronchery }
3424*e7be843bSPierre Pronchery
3425*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3426*e7be843bSPierre Pronchery return 1;
3427*e7be843bSPierre Pronchery }
3428*e7be843bSPierre Pronchery
3429*e7be843bSPierre Pronchery /*
3430*e7be843bSPierre Pronchery * SSL_detach_stream
3431*e7be843bSPierre Pronchery * -----------------
3432*e7be843bSPierre Pronchery */
3433*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_detach_stream(SSL * s)3434*e7be843bSPierre Pronchery SSL *ossl_quic_detach_stream(SSL *s)
3435*e7be843bSPierre Pronchery {
3436*e7be843bSPierre Pronchery QCTX ctx;
3437*e7be843bSPierre Pronchery QUIC_XSO *xso = NULL;
3438*e7be843bSPierre Pronchery
3439*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
3440*e7be843bSPierre Pronchery return NULL;
3441*e7be843bSPierre Pronchery
3442*e7be843bSPierre Pronchery qctx_lock(&ctx);
3443*e7be843bSPierre Pronchery
3444*e7be843bSPierre Pronchery /* Calling this function inhibits default XSO autocreation. */
3445*e7be843bSPierre Pronchery /* QC ref to any default XSO is transferred to us and to caller. */
3446*e7be843bSPierre Pronchery qc_set_default_xso_keep_ref(ctx.qc, NULL, /*touch=*/1, &xso);
3447*e7be843bSPierre Pronchery
3448*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3449*e7be843bSPierre Pronchery
3450*e7be843bSPierre Pronchery return xso != NULL ? &xso->obj.ssl : NULL;
3451*e7be843bSPierre Pronchery }
3452*e7be843bSPierre Pronchery
3453*e7be843bSPierre Pronchery /*
3454*e7be843bSPierre Pronchery * SSL_attach_stream
3455*e7be843bSPierre Pronchery * -----------------
3456*e7be843bSPierre Pronchery */
3457*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_attach_stream(SSL * conn,SSL * stream)3458*e7be843bSPierre Pronchery int ossl_quic_attach_stream(SSL *conn, SSL *stream)
3459*e7be843bSPierre Pronchery {
3460*e7be843bSPierre Pronchery QCTX ctx;
3461*e7be843bSPierre Pronchery QUIC_XSO *xso;
3462*e7be843bSPierre Pronchery int nref;
3463*e7be843bSPierre Pronchery
3464*e7be843bSPierre Pronchery if (!expect_quic_conn_only(conn, &ctx))
3465*e7be843bSPierre Pronchery return 0;
3466*e7be843bSPierre Pronchery
3467*e7be843bSPierre Pronchery if (stream == NULL || stream->type != SSL_TYPE_QUIC_XSO)
3468*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_NULL_PARAMETER,
3469*e7be843bSPierre Pronchery "stream to attach must be a valid QUIC stream");
3470*e7be843bSPierre Pronchery
3471*e7be843bSPierre Pronchery xso = (QUIC_XSO *)stream;
3472*e7be843bSPierre Pronchery
3473*e7be843bSPierre Pronchery qctx_lock(&ctx);
3474*e7be843bSPierre Pronchery
3475*e7be843bSPierre Pronchery if (ctx.qc->default_xso != NULL) {
3476*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3477*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
3478*e7be843bSPierre Pronchery "connection already has a default stream");
3479*e7be843bSPierre Pronchery }
3480*e7be843bSPierre Pronchery
3481*e7be843bSPierre Pronchery /*
3482*e7be843bSPierre Pronchery * It is a caller error for the XSO being attached as a default XSO to have
3483*e7be843bSPierre Pronchery * more than one ref.
3484*e7be843bSPierre Pronchery */
3485*e7be843bSPierre Pronchery if (!CRYPTO_GET_REF(&xso->obj.ssl.references, &nref)) {
3486*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3487*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR,
3488*e7be843bSPierre Pronchery "ref");
3489*e7be843bSPierre Pronchery }
3490*e7be843bSPierre Pronchery
3491*e7be843bSPierre Pronchery if (nref != 1) {
3492*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3493*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3494*e7be843bSPierre Pronchery "stream being attached must have "
3495*e7be843bSPierre Pronchery "only 1 reference");
3496*e7be843bSPierre Pronchery }
3497*e7be843bSPierre Pronchery
3498*e7be843bSPierre Pronchery /* Caller's reference to the XSO is transferred to us. */
3499*e7be843bSPierre Pronchery /* Calling this function inhibits default XSO autocreation. */
3500*e7be843bSPierre Pronchery qc_set_default_xso(ctx.qc, xso, /*touch=*/1);
3501*e7be843bSPierre Pronchery
3502*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3503*e7be843bSPierre Pronchery return 1;
3504*e7be843bSPierre Pronchery }
3505*e7be843bSPierre Pronchery
3506*e7be843bSPierre Pronchery /*
3507*e7be843bSPierre Pronchery * SSL_set_incoming_stream_policy
3508*e7be843bSPierre Pronchery * ------------------------------
3509*e7be843bSPierre Pronchery */
3510*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_get_effective_incoming_stream_policy(QUIC_CONNECTION * qc)3511*e7be843bSPierre Pronchery static int qc_get_effective_incoming_stream_policy(QUIC_CONNECTION *qc)
3512*e7be843bSPierre Pronchery {
3513*e7be843bSPierre Pronchery switch (qc->incoming_stream_policy) {
3514*e7be843bSPierre Pronchery case SSL_INCOMING_STREAM_POLICY_AUTO:
3515*e7be843bSPierre Pronchery if ((qc->default_xso == NULL && !qc->default_xso_created)
3516*e7be843bSPierre Pronchery || qc->default_stream_mode == SSL_DEFAULT_STREAM_MODE_NONE)
3517*e7be843bSPierre Pronchery return SSL_INCOMING_STREAM_POLICY_ACCEPT;
3518*e7be843bSPierre Pronchery else
3519*e7be843bSPierre Pronchery return SSL_INCOMING_STREAM_POLICY_REJECT;
3520*e7be843bSPierre Pronchery
3521*e7be843bSPierre Pronchery default:
3522*e7be843bSPierre Pronchery return qc->incoming_stream_policy;
3523*e7be843bSPierre Pronchery }
3524*e7be843bSPierre Pronchery }
3525*e7be843bSPierre Pronchery
3526*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qc_update_reject_policy(QUIC_CONNECTION * qc)3527*e7be843bSPierre Pronchery static void qc_update_reject_policy(QUIC_CONNECTION *qc)
3528*e7be843bSPierre Pronchery {
3529*e7be843bSPierre Pronchery int policy = qc_get_effective_incoming_stream_policy(qc);
3530*e7be843bSPierre Pronchery int enable_reject = (policy == SSL_INCOMING_STREAM_POLICY_REJECT);
3531*e7be843bSPierre Pronchery
3532*e7be843bSPierre Pronchery ossl_quic_channel_set_incoming_stream_auto_reject(qc->ch,
3533*e7be843bSPierre Pronchery enable_reject,
3534*e7be843bSPierre Pronchery qc->incoming_stream_aec);
3535*e7be843bSPierre Pronchery }
3536*e7be843bSPierre Pronchery
3537*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_set_incoming_stream_policy(SSL * s,int policy,uint64_t aec)3538*e7be843bSPierre Pronchery int ossl_quic_set_incoming_stream_policy(SSL *s, int policy,
3539*e7be843bSPierre Pronchery uint64_t aec)
3540*e7be843bSPierre Pronchery {
3541*e7be843bSPierre Pronchery int ret = 1;
3542*e7be843bSPierre Pronchery QCTX ctx;
3543*e7be843bSPierre Pronchery
3544*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
3545*e7be843bSPierre Pronchery return 0;
3546*e7be843bSPierre Pronchery
3547*e7be843bSPierre Pronchery qctx_lock(&ctx);
3548*e7be843bSPierre Pronchery
3549*e7be843bSPierre Pronchery switch (policy) {
3550*e7be843bSPierre Pronchery case SSL_INCOMING_STREAM_POLICY_AUTO:
3551*e7be843bSPierre Pronchery case SSL_INCOMING_STREAM_POLICY_ACCEPT:
3552*e7be843bSPierre Pronchery case SSL_INCOMING_STREAM_POLICY_REJECT:
3553*e7be843bSPierre Pronchery ctx.qc->incoming_stream_policy = policy;
3554*e7be843bSPierre Pronchery ctx.qc->incoming_stream_aec = aec;
3555*e7be843bSPierre Pronchery break;
3556*e7be843bSPierre Pronchery
3557*e7be843bSPierre Pronchery default:
3558*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3559*e7be843bSPierre Pronchery ret = 0;
3560*e7be843bSPierre Pronchery break;
3561*e7be843bSPierre Pronchery }
3562*e7be843bSPierre Pronchery
3563*e7be843bSPierre Pronchery qc_update_reject_policy(ctx.qc);
3564*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3565*e7be843bSPierre Pronchery return ret;
3566*e7be843bSPierre Pronchery }
3567*e7be843bSPierre Pronchery
3568*e7be843bSPierre Pronchery /*
3569*e7be843bSPierre Pronchery * SSL_get_value, SSL_set_value
3570*e7be843bSPierre Pronchery * ----------------------------
3571*e7be843bSPierre Pronchery */
3572*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
qc_getset_idle_timeout(QCTX * ctx,uint32_t class_,uint64_t * p_value_out,uint64_t * p_value_in)3573*e7be843bSPierre Pronchery static int qc_getset_idle_timeout(QCTX *ctx, uint32_t class_,
3574*e7be843bSPierre Pronchery uint64_t *p_value_out, uint64_t *p_value_in)
3575*e7be843bSPierre Pronchery {
3576*e7be843bSPierre Pronchery int ret = 0;
3577*e7be843bSPierre Pronchery uint64_t value_out = 0, value_in;
3578*e7be843bSPierre Pronchery
3579*e7be843bSPierre Pronchery qctx_lock(ctx);
3580*e7be843bSPierre Pronchery
3581*e7be843bSPierre Pronchery switch (class_) {
3582*e7be843bSPierre Pronchery case SSL_VALUE_CLASS_FEATURE_REQUEST:
3583*e7be843bSPierre Pronchery value_out = ossl_quic_channel_get_max_idle_timeout_request(ctx->qc->ch);
3584*e7be843bSPierre Pronchery
3585*e7be843bSPierre Pronchery if (p_value_in != NULL) {
3586*e7be843bSPierre Pronchery value_in = *p_value_in;
3587*e7be843bSPierre Pronchery if (value_in > OSSL_QUIC_VLINT_MAX) {
3588*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3589*e7be843bSPierre Pronchery NULL);
3590*e7be843bSPierre Pronchery goto err;
3591*e7be843bSPierre Pronchery }
3592*e7be843bSPierre Pronchery
3593*e7be843bSPierre Pronchery if (ossl_quic_channel_have_generated_transport_params(ctx->qc->ch)) {
3594*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_FEATURE_NOT_RENEGOTIABLE,
3595*e7be843bSPierre Pronchery NULL);
3596*e7be843bSPierre Pronchery goto err;
3597*e7be843bSPierre Pronchery }
3598*e7be843bSPierre Pronchery
3599*e7be843bSPierre Pronchery ossl_quic_channel_set_max_idle_timeout_request(ctx->qc->ch, value_in);
3600*e7be843bSPierre Pronchery }
3601*e7be843bSPierre Pronchery break;
3602*e7be843bSPierre Pronchery
3603*e7be843bSPierre Pronchery case SSL_VALUE_CLASS_FEATURE_PEER_REQUEST:
3604*e7be843bSPierre Pronchery case SSL_VALUE_CLASS_FEATURE_NEGOTIATED:
3605*e7be843bSPierre Pronchery if (p_value_in != NULL) {
3606*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_OP,
3607*e7be843bSPierre Pronchery NULL);
3608*e7be843bSPierre Pronchery goto err;
3609*e7be843bSPierre Pronchery }
3610*e7be843bSPierre Pronchery
3611*e7be843bSPierre Pronchery if (!ossl_quic_channel_is_handshake_complete(ctx->qc->ch)) {
3612*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_FEATURE_NEGOTIATION_NOT_COMPLETE,
3613*e7be843bSPierre Pronchery NULL);
3614*e7be843bSPierre Pronchery goto err;
3615*e7be843bSPierre Pronchery }
3616*e7be843bSPierre Pronchery
3617*e7be843bSPierre Pronchery value_out = (class_ == SSL_VALUE_CLASS_FEATURE_NEGOTIATED)
3618*e7be843bSPierre Pronchery ? ossl_quic_channel_get_max_idle_timeout_actual(ctx->qc->ch)
3619*e7be843bSPierre Pronchery : ossl_quic_channel_get_max_idle_timeout_peer_request(ctx->qc->ch);
3620*e7be843bSPierre Pronchery break;
3621*e7be843bSPierre Pronchery
3622*e7be843bSPierre Pronchery default:
3623*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3624*e7be843bSPierre Pronchery NULL);
3625*e7be843bSPierre Pronchery goto err;
3626*e7be843bSPierre Pronchery }
3627*e7be843bSPierre Pronchery
3628*e7be843bSPierre Pronchery ret = 1;
3629*e7be843bSPierre Pronchery err:
3630*e7be843bSPierre Pronchery qctx_unlock(ctx);
3631*e7be843bSPierre Pronchery if (ret && p_value_out != NULL)
3632*e7be843bSPierre Pronchery *p_value_out = value_out;
3633*e7be843bSPierre Pronchery
3634*e7be843bSPierre Pronchery return ret;
3635*e7be843bSPierre Pronchery }
3636*e7be843bSPierre Pronchery
3637*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
qc_get_stream_avail(QCTX * ctx,uint32_t class_,int is_uni,int is_remote,uint64_t * value)3638*e7be843bSPierre Pronchery static int qc_get_stream_avail(QCTX *ctx, uint32_t class_,
3639*e7be843bSPierre Pronchery int is_uni, int is_remote,
3640*e7be843bSPierre Pronchery uint64_t *value)
3641*e7be843bSPierre Pronchery {
3642*e7be843bSPierre Pronchery int ret = 0;
3643*e7be843bSPierre Pronchery
3644*e7be843bSPierre Pronchery if (class_ != SSL_VALUE_CLASS_GENERIC) {
3645*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3646*e7be843bSPierre Pronchery NULL);
3647*e7be843bSPierre Pronchery return 0;
3648*e7be843bSPierre Pronchery }
3649*e7be843bSPierre Pronchery
3650*e7be843bSPierre Pronchery qctx_lock(ctx);
3651*e7be843bSPierre Pronchery
3652*e7be843bSPierre Pronchery *value = is_remote
3653*e7be843bSPierre Pronchery ? ossl_quic_channel_get_remote_stream_count_avail(ctx->qc->ch, is_uni)
3654*e7be843bSPierre Pronchery : ossl_quic_channel_get_local_stream_count_avail(ctx->qc->ch, is_uni);
3655*e7be843bSPierre Pronchery
3656*e7be843bSPierre Pronchery ret = 1;
3657*e7be843bSPierre Pronchery qctx_unlock(ctx);
3658*e7be843bSPierre Pronchery return ret;
3659*e7be843bSPierre Pronchery }
3660*e7be843bSPierre Pronchery
3661*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qctx_should_autotick(QCTX * ctx)3662*e7be843bSPierre Pronchery static int qctx_should_autotick(QCTX *ctx)
3663*e7be843bSPierre Pronchery {
3664*e7be843bSPierre Pronchery int event_handling_mode;
3665*e7be843bSPierre Pronchery QUIC_OBJ *obj = ctx->obj;
3666*e7be843bSPierre Pronchery
3667*e7be843bSPierre Pronchery for (; (event_handling_mode = obj->event_handling_mode) == SSL_VALUE_EVENT_HANDLING_MODE_INHERIT
3668*e7be843bSPierre Pronchery && obj->parent_obj != NULL; obj = obj->parent_obj);
3669*e7be843bSPierre Pronchery
3670*e7be843bSPierre Pronchery return event_handling_mode != SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT;
3671*e7be843bSPierre Pronchery }
3672*e7be843bSPierre Pronchery
3673*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
qctx_maybe_autotick(QCTX * ctx)3674*e7be843bSPierre Pronchery static void qctx_maybe_autotick(QCTX *ctx)
3675*e7be843bSPierre Pronchery {
3676*e7be843bSPierre Pronchery if (!qctx_should_autotick(ctx))
3677*e7be843bSPierre Pronchery return;
3678*e7be843bSPierre Pronchery
3679*e7be843bSPierre Pronchery ossl_quic_reactor_tick(ossl_quic_obj_get0_reactor(ctx->obj), 0);
3680*e7be843bSPierre Pronchery }
3681*e7be843bSPierre Pronchery
3682*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
qc_getset_event_handling(QCTX * ctx,uint32_t class_,uint64_t * p_value_out,uint64_t * p_value_in)3683*e7be843bSPierre Pronchery static int qc_getset_event_handling(QCTX *ctx, uint32_t class_,
3684*e7be843bSPierre Pronchery uint64_t *p_value_out,
3685*e7be843bSPierre Pronchery uint64_t *p_value_in)
3686*e7be843bSPierre Pronchery {
3687*e7be843bSPierre Pronchery int ret = 0;
3688*e7be843bSPierre Pronchery uint64_t value_out = 0;
3689*e7be843bSPierre Pronchery
3690*e7be843bSPierre Pronchery qctx_lock(ctx);
3691*e7be843bSPierre Pronchery
3692*e7be843bSPierre Pronchery if (class_ != SSL_VALUE_CLASS_GENERIC) {
3693*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3694*e7be843bSPierre Pronchery NULL);
3695*e7be843bSPierre Pronchery goto err;
3696*e7be843bSPierre Pronchery }
3697*e7be843bSPierre Pronchery
3698*e7be843bSPierre Pronchery if (p_value_in != NULL) {
3699*e7be843bSPierre Pronchery switch (*p_value_in) {
3700*e7be843bSPierre Pronchery case SSL_VALUE_EVENT_HANDLING_MODE_INHERIT:
3701*e7be843bSPierre Pronchery case SSL_VALUE_EVENT_HANDLING_MODE_IMPLICIT:
3702*e7be843bSPierre Pronchery case SSL_VALUE_EVENT_HANDLING_MODE_EXPLICIT:
3703*e7be843bSPierre Pronchery break;
3704*e7be843bSPierre Pronchery default:
3705*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, ERR_R_PASSED_INVALID_ARGUMENT,
3706*e7be843bSPierre Pronchery NULL);
3707*e7be843bSPierre Pronchery goto err;
3708*e7be843bSPierre Pronchery }
3709*e7be843bSPierre Pronchery
3710*e7be843bSPierre Pronchery value_out = *p_value_in;
3711*e7be843bSPierre Pronchery ctx->obj->event_handling_mode = (int)value_out;
3712*e7be843bSPierre Pronchery } else {
3713*e7be843bSPierre Pronchery value_out = ctx->obj->event_handling_mode;
3714*e7be843bSPierre Pronchery }
3715*e7be843bSPierre Pronchery
3716*e7be843bSPierre Pronchery ret = 1;
3717*e7be843bSPierre Pronchery err:
3718*e7be843bSPierre Pronchery qctx_unlock(ctx);
3719*e7be843bSPierre Pronchery if (ret && p_value_out != NULL)
3720*e7be843bSPierre Pronchery *p_value_out = value_out;
3721*e7be843bSPierre Pronchery
3722*e7be843bSPierre Pronchery return ret;
3723*e7be843bSPierre Pronchery }
3724*e7be843bSPierre Pronchery
3725*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
qc_get_stream_write_buf_stat(QCTX * ctx,uint32_t class_,uint64_t * p_value_out,size_t (* getter)(QUIC_SSTREAM * sstream))3726*e7be843bSPierre Pronchery static int qc_get_stream_write_buf_stat(QCTX *ctx, uint32_t class_,
3727*e7be843bSPierre Pronchery uint64_t *p_value_out,
3728*e7be843bSPierre Pronchery size_t (*getter)(QUIC_SSTREAM *sstream))
3729*e7be843bSPierre Pronchery {
3730*e7be843bSPierre Pronchery int ret = 0;
3731*e7be843bSPierre Pronchery size_t value = 0;
3732*e7be843bSPierre Pronchery
3733*e7be843bSPierre Pronchery qctx_lock(ctx);
3734*e7be843bSPierre Pronchery
3735*e7be843bSPierre Pronchery if (class_ != SSL_VALUE_CLASS_GENERIC) {
3736*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_UNSUPPORTED_CONFIG_VALUE_CLASS,
3737*e7be843bSPierre Pronchery NULL);
3738*e7be843bSPierre Pronchery goto err;
3739*e7be843bSPierre Pronchery }
3740*e7be843bSPierre Pronchery
3741*e7be843bSPierre Pronchery if (ctx->xso == NULL) {
3742*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_NO_STREAM, NULL);
3743*e7be843bSPierre Pronchery goto err;
3744*e7be843bSPierre Pronchery }
3745*e7be843bSPierre Pronchery
3746*e7be843bSPierre Pronchery if (!ossl_quic_stream_has_send(ctx->xso->stream)) {
3747*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(ctx, SSL_R_STREAM_RECV_ONLY, NULL);
3748*e7be843bSPierre Pronchery goto err;
3749*e7be843bSPierre Pronchery }
3750*e7be843bSPierre Pronchery
3751*e7be843bSPierre Pronchery if (ossl_quic_stream_has_send_buffer(ctx->xso->stream))
3752*e7be843bSPierre Pronchery value = getter(ctx->xso->stream->sstream);
3753*e7be843bSPierre Pronchery
3754*e7be843bSPierre Pronchery ret = 1;
3755*e7be843bSPierre Pronchery err:
3756*e7be843bSPierre Pronchery qctx_unlock(ctx);
3757*e7be843bSPierre Pronchery *p_value_out = (uint64_t)value;
3758*e7be843bSPierre Pronchery return ret;
3759*e7be843bSPierre Pronchery }
3760*e7be843bSPierre Pronchery
3761*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
expect_quic_for_value(SSL * s,QCTX * ctx,uint32_t id)3762*e7be843bSPierre Pronchery static int expect_quic_for_value(SSL *s, QCTX *ctx, uint32_t id)
3763*e7be843bSPierre Pronchery {
3764*e7be843bSPierre Pronchery switch (id) {
3765*e7be843bSPierre Pronchery case SSL_VALUE_EVENT_HANDLING_MODE:
3766*e7be843bSPierre Pronchery case SSL_VALUE_STREAM_WRITE_BUF_SIZE:
3767*e7be843bSPierre Pronchery case SSL_VALUE_STREAM_WRITE_BUF_USED:
3768*e7be843bSPierre Pronchery case SSL_VALUE_STREAM_WRITE_BUF_AVAIL:
3769*e7be843bSPierre Pronchery return expect_quic_cs(s, ctx);
3770*e7be843bSPierre Pronchery default:
3771*e7be843bSPierre Pronchery return expect_quic_conn_only(s, ctx);
3772*e7be843bSPierre Pronchery }
3773*e7be843bSPierre Pronchery }
3774*e7be843bSPierre Pronchery
3775*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_get_value_uint(SSL * s,uint32_t class_,uint32_t id,uint64_t * value)3776*e7be843bSPierre Pronchery int ossl_quic_get_value_uint(SSL *s, uint32_t class_, uint32_t id,
3777*e7be843bSPierre Pronchery uint64_t *value)
3778*e7be843bSPierre Pronchery {
3779*e7be843bSPierre Pronchery QCTX ctx;
3780*e7be843bSPierre Pronchery
3781*e7be843bSPierre Pronchery if (!expect_quic_for_value(s, &ctx, id))
3782*e7be843bSPierre Pronchery return 0;
3783*e7be843bSPierre Pronchery
3784*e7be843bSPierre Pronchery if (value == NULL)
3785*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3786*e7be843bSPierre Pronchery ERR_R_PASSED_INVALID_ARGUMENT, NULL);
3787*e7be843bSPierre Pronchery
3788*e7be843bSPierre Pronchery switch (id) {
3789*e7be843bSPierre Pronchery case SSL_VALUE_QUIC_IDLE_TIMEOUT:
3790*e7be843bSPierre Pronchery return qc_getset_idle_timeout(&ctx, class_, value, NULL);
3791*e7be843bSPierre Pronchery
3792*e7be843bSPierre Pronchery case SSL_VALUE_QUIC_STREAM_BIDI_LOCAL_AVAIL:
3793*e7be843bSPierre Pronchery return qc_get_stream_avail(&ctx, class_, /*uni=*/0, /*remote=*/0, value);
3794*e7be843bSPierre Pronchery case SSL_VALUE_QUIC_STREAM_BIDI_REMOTE_AVAIL:
3795*e7be843bSPierre Pronchery return qc_get_stream_avail(&ctx, class_, /*uni=*/0, /*remote=*/1, value);
3796*e7be843bSPierre Pronchery case SSL_VALUE_QUIC_STREAM_UNI_LOCAL_AVAIL:
3797*e7be843bSPierre Pronchery return qc_get_stream_avail(&ctx, class_, /*uni=*/1, /*remote=*/0, value);
3798*e7be843bSPierre Pronchery case SSL_VALUE_QUIC_STREAM_UNI_REMOTE_AVAIL:
3799*e7be843bSPierre Pronchery return qc_get_stream_avail(&ctx, class_, /*uni=*/1, /*remote=*/1, value);
3800*e7be843bSPierre Pronchery
3801*e7be843bSPierre Pronchery case SSL_VALUE_EVENT_HANDLING_MODE:
3802*e7be843bSPierre Pronchery return qc_getset_event_handling(&ctx, class_, value, NULL);
3803*e7be843bSPierre Pronchery
3804*e7be843bSPierre Pronchery case SSL_VALUE_STREAM_WRITE_BUF_SIZE:
3805*e7be843bSPierre Pronchery return qc_get_stream_write_buf_stat(&ctx, class_, value,
3806*e7be843bSPierre Pronchery ossl_quic_sstream_get_buffer_size);
3807*e7be843bSPierre Pronchery case SSL_VALUE_STREAM_WRITE_BUF_USED:
3808*e7be843bSPierre Pronchery return qc_get_stream_write_buf_stat(&ctx, class_, value,
3809*e7be843bSPierre Pronchery ossl_quic_sstream_get_buffer_used);
3810*e7be843bSPierre Pronchery case SSL_VALUE_STREAM_WRITE_BUF_AVAIL:
3811*e7be843bSPierre Pronchery return qc_get_stream_write_buf_stat(&ctx, class_, value,
3812*e7be843bSPierre Pronchery ossl_quic_sstream_get_buffer_avail);
3813*e7be843bSPierre Pronchery
3814*e7be843bSPierre Pronchery default:
3815*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3816*e7be843bSPierre Pronchery SSL_R_UNSUPPORTED_CONFIG_VALUE, NULL);
3817*e7be843bSPierre Pronchery }
3818*e7be843bSPierre Pronchery
3819*e7be843bSPierre Pronchery return 1;
3820*e7be843bSPierre Pronchery }
3821*e7be843bSPierre Pronchery
3822*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_set_value_uint(SSL * s,uint32_t class_,uint32_t id,uint64_t value)3823*e7be843bSPierre Pronchery int ossl_quic_set_value_uint(SSL *s, uint32_t class_, uint32_t id,
3824*e7be843bSPierre Pronchery uint64_t value)
3825*e7be843bSPierre Pronchery {
3826*e7be843bSPierre Pronchery QCTX ctx;
3827*e7be843bSPierre Pronchery
3828*e7be843bSPierre Pronchery if (!expect_quic_for_value(s, &ctx, id))
3829*e7be843bSPierre Pronchery return 0;
3830*e7be843bSPierre Pronchery
3831*e7be843bSPierre Pronchery switch (id) {
3832*e7be843bSPierre Pronchery case SSL_VALUE_QUIC_IDLE_TIMEOUT:
3833*e7be843bSPierre Pronchery return qc_getset_idle_timeout(&ctx, class_, NULL, &value);
3834*e7be843bSPierre Pronchery
3835*e7be843bSPierre Pronchery case SSL_VALUE_EVENT_HANDLING_MODE:
3836*e7be843bSPierre Pronchery return qc_getset_event_handling(&ctx, class_, NULL, &value);
3837*e7be843bSPierre Pronchery
3838*e7be843bSPierre Pronchery default:
3839*e7be843bSPierre Pronchery return QUIC_RAISE_NON_NORMAL_ERROR(&ctx,
3840*e7be843bSPierre Pronchery SSL_R_UNSUPPORTED_CONFIG_VALUE, NULL);
3841*e7be843bSPierre Pronchery }
3842*e7be843bSPierre Pronchery
3843*e7be843bSPierre Pronchery return 1;
3844*e7be843bSPierre Pronchery }
3845*e7be843bSPierre Pronchery
3846*e7be843bSPierre Pronchery /*
3847*e7be843bSPierre Pronchery * SSL_accept_stream
3848*e7be843bSPierre Pronchery * -----------------
3849*e7be843bSPierre Pronchery */
3850*e7be843bSPierre Pronchery struct wait_for_incoming_stream_args {
3851*e7be843bSPierre Pronchery QCTX *ctx;
3852*e7be843bSPierre Pronchery QUIC_STREAM *qs;
3853*e7be843bSPierre Pronchery };
3854*e7be843bSPierre Pronchery
3855*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
wait_for_incoming_stream(void * arg)3856*e7be843bSPierre Pronchery static int wait_for_incoming_stream(void *arg)
3857*e7be843bSPierre Pronchery {
3858*e7be843bSPierre Pronchery struct wait_for_incoming_stream_args *args = arg;
3859*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = args->ctx->qc;
3860*e7be843bSPierre Pronchery QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(qc->ch);
3861*e7be843bSPierre Pronchery
3862*e7be843bSPierre Pronchery if (!quic_mutation_allowed(qc, /*req_active=*/1)) {
3863*e7be843bSPierre Pronchery /* If connection is torn down due to an error while blocking, stop. */
3864*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(args->ctx, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
3865*e7be843bSPierre Pronchery return -1;
3866*e7be843bSPierre Pronchery }
3867*e7be843bSPierre Pronchery
3868*e7be843bSPierre Pronchery args->qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3869*e7be843bSPierre Pronchery if (args->qs != NULL)
3870*e7be843bSPierre Pronchery return 1; /* got a stream */
3871*e7be843bSPierre Pronchery
3872*e7be843bSPierre Pronchery return 0; /* did not get a stream, keep trying */
3873*e7be843bSPierre Pronchery }
3874*e7be843bSPierre Pronchery
3875*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_accept_stream(SSL * s,uint64_t flags)3876*e7be843bSPierre Pronchery SSL *ossl_quic_accept_stream(SSL *s, uint64_t flags)
3877*e7be843bSPierre Pronchery {
3878*e7be843bSPierre Pronchery QCTX ctx;
3879*e7be843bSPierre Pronchery int ret;
3880*e7be843bSPierre Pronchery SSL *new_s = NULL;
3881*e7be843bSPierre Pronchery QUIC_STREAM_MAP *qsm;
3882*e7be843bSPierre Pronchery QUIC_STREAM *qs;
3883*e7be843bSPierre Pronchery QUIC_XSO *xso;
3884*e7be843bSPierre Pronchery OSSL_RTT_INFO rtt_info;
3885*e7be843bSPierre Pronchery
3886*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
3887*e7be843bSPierre Pronchery return NULL;
3888*e7be843bSPierre Pronchery
3889*e7be843bSPierre Pronchery qctx_lock(&ctx);
3890*e7be843bSPierre Pronchery
3891*e7be843bSPierre Pronchery if (qc_get_effective_incoming_stream_policy(ctx.qc)
3892*e7be843bSPierre Pronchery == SSL_INCOMING_STREAM_POLICY_REJECT) {
3893*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
3894*e7be843bSPierre Pronchery goto out;
3895*e7be843bSPierre Pronchery }
3896*e7be843bSPierre Pronchery
3897*e7be843bSPierre Pronchery qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
3898*e7be843bSPierre Pronchery
3899*e7be843bSPierre Pronchery qs = ossl_quic_stream_map_peek_accept_queue(qsm);
3900*e7be843bSPierre Pronchery if (qs == NULL) {
3901*e7be843bSPierre Pronchery if (qctx_blocking(&ctx)
3902*e7be843bSPierre Pronchery && (flags & SSL_ACCEPT_STREAM_NO_BLOCK) == 0) {
3903*e7be843bSPierre Pronchery struct wait_for_incoming_stream_args args;
3904*e7be843bSPierre Pronchery
3905*e7be843bSPierre Pronchery args.ctx = &ctx;
3906*e7be843bSPierre Pronchery args.qs = NULL;
3907*e7be843bSPierre Pronchery
3908*e7be843bSPierre Pronchery ret = block_until_pred(&ctx, wait_for_incoming_stream, &args, 0);
3909*e7be843bSPierre Pronchery if (ret == 0) {
3910*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
3911*e7be843bSPierre Pronchery goto out;
3912*e7be843bSPierre Pronchery } else if (ret < 0 || args.qs == NULL) {
3913*e7be843bSPierre Pronchery goto out;
3914*e7be843bSPierre Pronchery }
3915*e7be843bSPierre Pronchery
3916*e7be843bSPierre Pronchery qs = args.qs;
3917*e7be843bSPierre Pronchery } else {
3918*e7be843bSPierre Pronchery goto out;
3919*e7be843bSPierre Pronchery }
3920*e7be843bSPierre Pronchery }
3921*e7be843bSPierre Pronchery
3922*e7be843bSPierre Pronchery xso = create_xso_from_stream(ctx.qc, qs);
3923*e7be843bSPierre Pronchery if (xso == NULL)
3924*e7be843bSPierre Pronchery goto out;
3925*e7be843bSPierre Pronchery
3926*e7be843bSPierre Pronchery ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ctx.qc->ch), &rtt_info);
3927*e7be843bSPierre Pronchery ossl_quic_stream_map_remove_from_accept_queue(qsm, qs,
3928*e7be843bSPierre Pronchery rtt_info.smoothed_rtt);
3929*e7be843bSPierre Pronchery new_s = &xso->obj.ssl;
3930*e7be843bSPierre Pronchery
3931*e7be843bSPierre Pronchery /* Calling this function inhibits default XSO autocreation. */
3932*e7be843bSPierre Pronchery qc_touch_default_xso(ctx.qc); /* inhibits default XSO */
3933*e7be843bSPierre Pronchery
3934*e7be843bSPierre Pronchery out:
3935*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3936*e7be843bSPierre Pronchery return new_s;
3937*e7be843bSPierre Pronchery }
3938*e7be843bSPierre Pronchery
3939*e7be843bSPierre Pronchery /*
3940*e7be843bSPierre Pronchery * SSL_get_accept_stream_queue_len
3941*e7be843bSPierre Pronchery * -------------------------------
3942*e7be843bSPierre Pronchery */
3943*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_get_accept_stream_queue_len(SSL * s)3944*e7be843bSPierre Pronchery size_t ossl_quic_get_accept_stream_queue_len(SSL *s)
3945*e7be843bSPierre Pronchery {
3946*e7be843bSPierre Pronchery QCTX ctx;
3947*e7be843bSPierre Pronchery size_t v;
3948*e7be843bSPierre Pronchery
3949*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
3950*e7be843bSPierre Pronchery return 0;
3951*e7be843bSPierre Pronchery
3952*e7be843bSPierre Pronchery qctx_lock(&ctx);
3953*e7be843bSPierre Pronchery
3954*e7be843bSPierre Pronchery v = ossl_quic_stream_map_get_total_accept_queue_len(ossl_quic_channel_get_qsm(ctx.qc->ch));
3955*e7be843bSPierre Pronchery
3956*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3957*e7be843bSPierre Pronchery return v;
3958*e7be843bSPierre Pronchery }
3959*e7be843bSPierre Pronchery
3960*e7be843bSPierre Pronchery /*
3961*e7be843bSPierre Pronchery * SSL_stream_reset
3962*e7be843bSPierre Pronchery * ----------------
3963*e7be843bSPierre Pronchery */
ossl_quic_stream_reset(SSL * ssl,const SSL_STREAM_RESET_ARGS * args,size_t args_len)3964*e7be843bSPierre Pronchery int ossl_quic_stream_reset(SSL *ssl,
3965*e7be843bSPierre Pronchery const SSL_STREAM_RESET_ARGS *args,
3966*e7be843bSPierre Pronchery size_t args_len)
3967*e7be843bSPierre Pronchery {
3968*e7be843bSPierre Pronchery QCTX ctx;
3969*e7be843bSPierre Pronchery QUIC_STREAM_MAP *qsm;
3970*e7be843bSPierre Pronchery QUIC_STREAM *qs;
3971*e7be843bSPierre Pronchery uint64_t error_code;
3972*e7be843bSPierre Pronchery int ok, err;
3973*e7be843bSPierre Pronchery
3974*e7be843bSPierre Pronchery if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/0, /*io=*/0, &ctx))
3975*e7be843bSPierre Pronchery return 0;
3976*e7be843bSPierre Pronchery
3977*e7be843bSPierre Pronchery qsm = ossl_quic_channel_get_qsm(ctx.qc->ch);
3978*e7be843bSPierre Pronchery qs = ctx.xso->stream;
3979*e7be843bSPierre Pronchery error_code = (args != NULL ? args->quic_error_code : 0);
3980*e7be843bSPierre Pronchery
3981*e7be843bSPierre Pronchery if (!quic_validate_for_write(ctx.xso, &err)) {
3982*e7be843bSPierre Pronchery ok = QUIC_RAISE_NON_NORMAL_ERROR(&ctx, err, NULL);
3983*e7be843bSPierre Pronchery goto err;
3984*e7be843bSPierre Pronchery }
3985*e7be843bSPierre Pronchery
3986*e7be843bSPierre Pronchery ok = ossl_quic_stream_map_reset_stream_send_part(qsm, qs, error_code);
3987*e7be843bSPierre Pronchery if (ok)
3988*e7be843bSPierre Pronchery ctx.xso->requested_reset = 1;
3989*e7be843bSPierre Pronchery
3990*e7be843bSPierre Pronchery err:
3991*e7be843bSPierre Pronchery qctx_unlock(&ctx);
3992*e7be843bSPierre Pronchery return ok;
3993*e7be843bSPierre Pronchery }
3994*e7be843bSPierre Pronchery
3995*e7be843bSPierre Pronchery /*
3996*e7be843bSPierre Pronchery * SSL_get_stream_read_state
3997*e7be843bSPierre Pronchery * -------------------------
3998*e7be843bSPierre Pronchery */
quic_classify_stream(QUIC_CONNECTION * qc,QUIC_STREAM * qs,int is_write,int * state,uint64_t * app_error_code)3999*e7be843bSPierre Pronchery static void quic_classify_stream(QUIC_CONNECTION *qc,
4000*e7be843bSPierre Pronchery QUIC_STREAM *qs,
4001*e7be843bSPierre Pronchery int is_write,
4002*e7be843bSPierre Pronchery int *state,
4003*e7be843bSPierre Pronchery uint64_t *app_error_code)
4004*e7be843bSPierre Pronchery {
4005*e7be843bSPierre Pronchery int local_init;
4006*e7be843bSPierre Pronchery uint64_t final_size;
4007*e7be843bSPierre Pronchery
4008*e7be843bSPierre Pronchery local_init = (ossl_quic_stream_is_server_init(qs) == qc->as_server);
4009*e7be843bSPierre Pronchery
4010*e7be843bSPierre Pronchery if (app_error_code != NULL)
4011*e7be843bSPierre Pronchery *app_error_code = UINT64_MAX;
4012*e7be843bSPierre Pronchery else
4013*e7be843bSPierre Pronchery app_error_code = &final_size; /* throw away value */
4014*e7be843bSPierre Pronchery
4015*e7be843bSPierre Pronchery if (!ossl_quic_stream_is_bidi(qs) && local_init != is_write) {
4016*e7be843bSPierre Pronchery /*
4017*e7be843bSPierre Pronchery * Unidirectional stream and this direction of transmission doesn't
4018*e7be843bSPierre Pronchery * exist.
4019*e7be843bSPierre Pronchery */
4020*e7be843bSPierre Pronchery *state = SSL_STREAM_STATE_WRONG_DIR;
4021*e7be843bSPierre Pronchery } else if (ossl_quic_channel_is_term_any(qc->ch)) {
4022*e7be843bSPierre Pronchery /* Connection already closed. */
4023*e7be843bSPierre Pronchery *state = SSL_STREAM_STATE_CONN_CLOSED;
4024*e7be843bSPierre Pronchery } else if (!is_write && qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ) {
4025*e7be843bSPierre Pronchery /* Application has read a FIN. */
4026*e7be843bSPierre Pronchery *state = SSL_STREAM_STATE_FINISHED;
4027*e7be843bSPierre Pronchery } else if ((!is_write && qs->stop_sending)
4028*e7be843bSPierre Pronchery || (is_write && ossl_quic_stream_send_is_reset(qs))) {
4029*e7be843bSPierre Pronchery /*
4030*e7be843bSPierre Pronchery * Stream has been reset locally. FIN takes precedence over this for the
4031*e7be843bSPierre Pronchery * read case as the application need not care if the stream is reset
4032*e7be843bSPierre Pronchery * after a FIN has been successfully processed.
4033*e7be843bSPierre Pronchery */
4034*e7be843bSPierre Pronchery *state = SSL_STREAM_STATE_RESET_LOCAL;
4035*e7be843bSPierre Pronchery *app_error_code = !is_write
4036*e7be843bSPierre Pronchery ? qs->stop_sending_aec
4037*e7be843bSPierre Pronchery : qs->reset_stream_aec;
4038*e7be843bSPierre Pronchery } else if ((!is_write && ossl_quic_stream_recv_is_reset(qs))
4039*e7be843bSPierre Pronchery || (is_write && qs->peer_stop_sending)) {
4040*e7be843bSPierre Pronchery /*
4041*e7be843bSPierre Pronchery * Stream has been reset remotely. */
4042*e7be843bSPierre Pronchery *state = SSL_STREAM_STATE_RESET_REMOTE;
4043*e7be843bSPierre Pronchery *app_error_code = !is_write
4044*e7be843bSPierre Pronchery ? qs->peer_reset_stream_aec
4045*e7be843bSPierre Pronchery : qs->peer_stop_sending_aec;
4046*e7be843bSPierre Pronchery } else if (is_write && ossl_quic_sstream_get_final_size(qs->sstream,
4047*e7be843bSPierre Pronchery &final_size)) {
4048*e7be843bSPierre Pronchery /*
4049*e7be843bSPierre Pronchery * Stream has been finished. Stream reset takes precedence over this for
4050*e7be843bSPierre Pronchery * the write case as peer may not have received all data.
4051*e7be843bSPierre Pronchery */
4052*e7be843bSPierre Pronchery *state = SSL_STREAM_STATE_FINISHED;
4053*e7be843bSPierre Pronchery } else {
4054*e7be843bSPierre Pronchery /* Stream still healthy. */
4055*e7be843bSPierre Pronchery *state = SSL_STREAM_STATE_OK;
4056*e7be843bSPierre Pronchery }
4057*e7be843bSPierre Pronchery }
4058*e7be843bSPierre Pronchery
quic_get_stream_state(SSL * ssl,int is_write)4059*e7be843bSPierre Pronchery static int quic_get_stream_state(SSL *ssl, int is_write)
4060*e7be843bSPierre Pronchery {
4061*e7be843bSPierre Pronchery QCTX ctx;
4062*e7be843bSPierre Pronchery int state;
4063*e7be843bSPierre Pronchery
4064*e7be843bSPierre Pronchery if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4065*e7be843bSPierre Pronchery return SSL_STREAM_STATE_NONE;
4066*e7be843bSPierre Pronchery
4067*e7be843bSPierre Pronchery quic_classify_stream(ctx.qc, ctx.xso->stream, is_write, &state, NULL);
4068*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4069*e7be843bSPierre Pronchery return state;
4070*e7be843bSPierre Pronchery }
4071*e7be843bSPierre Pronchery
ossl_quic_get_stream_read_state(SSL * ssl)4072*e7be843bSPierre Pronchery int ossl_quic_get_stream_read_state(SSL *ssl)
4073*e7be843bSPierre Pronchery {
4074*e7be843bSPierre Pronchery return quic_get_stream_state(ssl, /*is_write=*/0);
4075*e7be843bSPierre Pronchery }
4076*e7be843bSPierre Pronchery
4077*e7be843bSPierre Pronchery /*
4078*e7be843bSPierre Pronchery * SSL_get_stream_write_state
4079*e7be843bSPierre Pronchery * --------------------------
4080*e7be843bSPierre Pronchery */
ossl_quic_get_stream_write_state(SSL * ssl)4081*e7be843bSPierre Pronchery int ossl_quic_get_stream_write_state(SSL *ssl)
4082*e7be843bSPierre Pronchery {
4083*e7be843bSPierre Pronchery return quic_get_stream_state(ssl, /*is_write=*/1);
4084*e7be843bSPierre Pronchery }
4085*e7be843bSPierre Pronchery
4086*e7be843bSPierre Pronchery /*
4087*e7be843bSPierre Pronchery * SSL_get_stream_read_error_code
4088*e7be843bSPierre Pronchery * ------------------------------
4089*e7be843bSPierre Pronchery */
quic_get_stream_error_code(SSL * ssl,int is_write,uint64_t * app_error_code)4090*e7be843bSPierre Pronchery static int quic_get_stream_error_code(SSL *ssl, int is_write,
4091*e7be843bSPierre Pronchery uint64_t *app_error_code)
4092*e7be843bSPierre Pronchery {
4093*e7be843bSPierre Pronchery QCTX ctx;
4094*e7be843bSPierre Pronchery int state;
4095*e7be843bSPierre Pronchery
4096*e7be843bSPierre Pronchery if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4097*e7be843bSPierre Pronchery return -1;
4098*e7be843bSPierre Pronchery
4099*e7be843bSPierre Pronchery quic_classify_stream(ctx.qc, ctx.xso->stream, /*is_write=*/0,
4100*e7be843bSPierre Pronchery &state, app_error_code);
4101*e7be843bSPierre Pronchery
4102*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4103*e7be843bSPierre Pronchery switch (state) {
4104*e7be843bSPierre Pronchery case SSL_STREAM_STATE_FINISHED:
4105*e7be843bSPierre Pronchery return 0;
4106*e7be843bSPierre Pronchery case SSL_STREAM_STATE_RESET_LOCAL:
4107*e7be843bSPierre Pronchery case SSL_STREAM_STATE_RESET_REMOTE:
4108*e7be843bSPierre Pronchery return 1;
4109*e7be843bSPierre Pronchery default:
4110*e7be843bSPierre Pronchery return -1;
4111*e7be843bSPierre Pronchery }
4112*e7be843bSPierre Pronchery }
4113*e7be843bSPierre Pronchery
ossl_quic_get_stream_read_error_code(SSL * ssl,uint64_t * app_error_code)4114*e7be843bSPierre Pronchery int ossl_quic_get_stream_read_error_code(SSL *ssl, uint64_t *app_error_code)
4115*e7be843bSPierre Pronchery {
4116*e7be843bSPierre Pronchery return quic_get_stream_error_code(ssl, /*is_write=*/0, app_error_code);
4117*e7be843bSPierre Pronchery }
4118*e7be843bSPierre Pronchery
4119*e7be843bSPierre Pronchery /*
4120*e7be843bSPierre Pronchery * SSL_get_stream_write_error_code
4121*e7be843bSPierre Pronchery * -------------------------------
4122*e7be843bSPierre Pronchery */
ossl_quic_get_stream_write_error_code(SSL * ssl,uint64_t * app_error_code)4123*e7be843bSPierre Pronchery int ossl_quic_get_stream_write_error_code(SSL *ssl, uint64_t *app_error_code)
4124*e7be843bSPierre Pronchery {
4125*e7be843bSPierre Pronchery return quic_get_stream_error_code(ssl, /*is_write=*/1, app_error_code);
4126*e7be843bSPierre Pronchery }
4127*e7be843bSPierre Pronchery
4128*e7be843bSPierre Pronchery /*
4129*e7be843bSPierre Pronchery * Write buffer size mutation
4130*e7be843bSPierre Pronchery * --------------------------
4131*e7be843bSPierre Pronchery */
ossl_quic_set_write_buffer_size(SSL * ssl,size_t size)4132*e7be843bSPierre Pronchery int ossl_quic_set_write_buffer_size(SSL *ssl, size_t size)
4133*e7be843bSPierre Pronchery {
4134*e7be843bSPierre Pronchery int ret = 0;
4135*e7be843bSPierre Pronchery QCTX ctx;
4136*e7be843bSPierre Pronchery
4137*e7be843bSPierre Pronchery if (!expect_quic_with_stream_lock(ssl, /*remote_init=*/-1, /*io=*/0, &ctx))
4138*e7be843bSPierre Pronchery return 0;
4139*e7be843bSPierre Pronchery
4140*e7be843bSPierre Pronchery if (!ossl_quic_stream_has_send(ctx.xso->stream)) {
4141*e7be843bSPierre Pronchery /* Called on a unidirectional receive-only stream - error. */
4142*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, NULL);
4143*e7be843bSPierre Pronchery goto out;
4144*e7be843bSPierre Pronchery }
4145*e7be843bSPierre Pronchery
4146*e7be843bSPierre Pronchery if (!ossl_quic_stream_has_send_buffer(ctx.xso->stream)) {
4147*e7be843bSPierre Pronchery /*
4148*e7be843bSPierre Pronchery * If the stream has a send part but we have disposed of it because we
4149*e7be843bSPierre Pronchery * no longer need it, this is a no-op.
4150*e7be843bSPierre Pronchery */
4151*e7be843bSPierre Pronchery ret = 1;
4152*e7be843bSPierre Pronchery goto out;
4153*e7be843bSPierre Pronchery }
4154*e7be843bSPierre Pronchery
4155*e7be843bSPierre Pronchery if (!ossl_quic_sstream_set_buffer_size(ctx.xso->stream->sstream, size)) {
4156*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_INTERNAL_ERROR, NULL);
4157*e7be843bSPierre Pronchery goto out;
4158*e7be843bSPierre Pronchery }
4159*e7be843bSPierre Pronchery
4160*e7be843bSPierre Pronchery ret = 1;
4161*e7be843bSPierre Pronchery
4162*e7be843bSPierre Pronchery out:
4163*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4164*e7be843bSPierre Pronchery return ret;
4165*e7be843bSPierre Pronchery }
4166*e7be843bSPierre Pronchery
4167*e7be843bSPierre Pronchery /*
4168*e7be843bSPierre Pronchery * SSL_get_conn_close_info
4169*e7be843bSPierre Pronchery * -----------------------
4170*e7be843bSPierre Pronchery */
ossl_quic_get_conn_close_info(SSL * ssl,SSL_CONN_CLOSE_INFO * info,size_t info_len)4171*e7be843bSPierre Pronchery int ossl_quic_get_conn_close_info(SSL *ssl,
4172*e7be843bSPierre Pronchery SSL_CONN_CLOSE_INFO *info,
4173*e7be843bSPierre Pronchery size_t info_len)
4174*e7be843bSPierre Pronchery {
4175*e7be843bSPierre Pronchery QCTX ctx;
4176*e7be843bSPierre Pronchery const QUIC_TERMINATE_CAUSE *tc;
4177*e7be843bSPierre Pronchery
4178*e7be843bSPierre Pronchery if (!expect_quic_conn_only(ssl, &ctx))
4179*e7be843bSPierre Pronchery return -1;
4180*e7be843bSPierre Pronchery
4181*e7be843bSPierre Pronchery tc = ossl_quic_channel_get_terminate_cause(ctx.qc->ch);
4182*e7be843bSPierre Pronchery if (tc == NULL)
4183*e7be843bSPierre Pronchery return 0;
4184*e7be843bSPierre Pronchery
4185*e7be843bSPierre Pronchery info->error_code = tc->error_code;
4186*e7be843bSPierre Pronchery info->frame_type = tc->frame_type;
4187*e7be843bSPierre Pronchery info->reason = tc->reason;
4188*e7be843bSPierre Pronchery info->reason_len = tc->reason_len;
4189*e7be843bSPierre Pronchery info->flags = 0;
4190*e7be843bSPierre Pronchery if (!tc->remote)
4191*e7be843bSPierre Pronchery info->flags |= SSL_CONN_CLOSE_FLAG_LOCAL;
4192*e7be843bSPierre Pronchery if (!tc->app)
4193*e7be843bSPierre Pronchery info->flags |= SSL_CONN_CLOSE_FLAG_TRANSPORT;
4194*e7be843bSPierre Pronchery return 1;
4195*e7be843bSPierre Pronchery }
4196*e7be843bSPierre Pronchery
4197*e7be843bSPierre Pronchery /*
4198*e7be843bSPierre Pronchery * SSL_key_update
4199*e7be843bSPierre Pronchery * --------------
4200*e7be843bSPierre Pronchery */
ossl_quic_key_update(SSL * ssl,int update_type)4201*e7be843bSPierre Pronchery int ossl_quic_key_update(SSL *ssl, int update_type)
4202*e7be843bSPierre Pronchery {
4203*e7be843bSPierre Pronchery QCTX ctx;
4204*e7be843bSPierre Pronchery
4205*e7be843bSPierre Pronchery if (!expect_quic_conn_only(ssl, &ctx))
4206*e7be843bSPierre Pronchery return 0;
4207*e7be843bSPierre Pronchery
4208*e7be843bSPierre Pronchery switch (update_type) {
4209*e7be843bSPierre Pronchery case SSL_KEY_UPDATE_NOT_REQUESTED:
4210*e7be843bSPierre Pronchery /*
4211*e7be843bSPierre Pronchery * QUIC signals peer key update implicily by triggering a local
4212*e7be843bSPierre Pronchery * spontaneous TXKU. Silently upgrade this to SSL_KEY_UPDATE_REQUESTED.
4213*e7be843bSPierre Pronchery */
4214*e7be843bSPierre Pronchery case SSL_KEY_UPDATE_REQUESTED:
4215*e7be843bSPierre Pronchery break;
4216*e7be843bSPierre Pronchery
4217*e7be843bSPierre Pronchery default:
4218*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
4219*e7be843bSPierre Pronchery return 0;
4220*e7be843bSPierre Pronchery }
4221*e7be843bSPierre Pronchery
4222*e7be843bSPierre Pronchery qctx_lock(&ctx);
4223*e7be843bSPierre Pronchery
4224*e7be843bSPierre Pronchery /* Attempt to perform a TXKU. */
4225*e7be843bSPierre Pronchery if (!ossl_quic_channel_trigger_txku(ctx.qc->ch)) {
4226*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(&ctx, SSL_R_TOO_MANY_KEY_UPDATES, NULL);
4227*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4228*e7be843bSPierre Pronchery return 0;
4229*e7be843bSPierre Pronchery }
4230*e7be843bSPierre Pronchery
4231*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4232*e7be843bSPierre Pronchery return 1;
4233*e7be843bSPierre Pronchery }
4234*e7be843bSPierre Pronchery
4235*e7be843bSPierre Pronchery /*
4236*e7be843bSPierre Pronchery * SSL_get_key_update_type
4237*e7be843bSPierre Pronchery * -----------------------
4238*e7be843bSPierre Pronchery */
ossl_quic_get_key_update_type(const SSL * s)4239*e7be843bSPierre Pronchery int ossl_quic_get_key_update_type(const SSL *s)
4240*e7be843bSPierre Pronchery {
4241*e7be843bSPierre Pronchery /*
4242*e7be843bSPierre Pronchery * We always handle key updates immediately so a key update is never
4243*e7be843bSPierre Pronchery * pending.
4244*e7be843bSPierre Pronchery */
4245*e7be843bSPierre Pronchery return SSL_KEY_UPDATE_NONE;
4246*e7be843bSPierre Pronchery }
4247*e7be843bSPierre Pronchery
4248*e7be843bSPierre Pronchery /**
4249*e7be843bSPierre Pronchery * @brief Allocates an SSL object for a user from a QUIC channel.
4250*e7be843bSPierre Pronchery *
4251*e7be843bSPierre Pronchery * This function creates a new QUIC_CONNECTION object based on an incoming
4252*e7be843bSPierre Pronchery * connection associated with the provided QUIC_LISTENER. If the connection
4253*e7be843bSPierre Pronchery * creation fails, the function returns NULL. Otherwise, it returns a pointer
4254*e7be843bSPierre Pronchery * to the SSL object associated with the newly created connection.
4255*e7be843bSPierre Pronchery *
4256*e7be843bSPierre Pronchery * Note: This function is a registered port callback made from
4257*e7be843bSPierre Pronchery * ossl_quic_new_listener and ossl_quic_new_listener_from, and allows for
4258*e7be843bSPierre Pronchery * pre-allocation of the user_ssl object when a channel is created, rather than
4259*e7be843bSPierre Pronchery * when it is accepted
4260*e7be843bSPierre Pronchery *
4261*e7be843bSPierre Pronchery * @param ch Pointer to the QUIC_CHANNEL representing the incoming connection.
4262*e7be843bSPierre Pronchery * @param arg Pointer to a QUIC_LISTENER used to create the connection.
4263*e7be843bSPierre Pronchery *
4264*e7be843bSPierre Pronchery * @return Pointer to the SSL object on success, or NULL on failure.
4265*e7be843bSPierre Pronchery */
alloc_port_user_ssl(QUIC_CHANNEL * ch,void * arg)4266*e7be843bSPierre Pronchery static SSL *alloc_port_user_ssl(QUIC_CHANNEL *ch, void *arg)
4267*e7be843bSPierre Pronchery {
4268*e7be843bSPierre Pronchery QUIC_LISTENER *ql = arg;
4269*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = create_qc_from_incoming_conn(ql, ch);
4270*e7be843bSPierre Pronchery
4271*e7be843bSPierre Pronchery return (qc == NULL) ? NULL : &qc->obj.ssl;
4272*e7be843bSPierre Pronchery }
4273*e7be843bSPierre Pronchery
4274*e7be843bSPierre Pronchery /*
4275*e7be843bSPierre Pronchery * QUIC Front-End I/O API: Listeners
4276*e7be843bSPierre Pronchery * =================================
4277*e7be843bSPierre Pronchery */
4278*e7be843bSPierre Pronchery
4279*e7be843bSPierre Pronchery /*
4280*e7be843bSPierre Pronchery * SSL_new_listener
4281*e7be843bSPierre Pronchery * ----------------
4282*e7be843bSPierre Pronchery */
ossl_quic_new_listener(SSL_CTX * ctx,uint64_t flags)4283*e7be843bSPierre Pronchery SSL *ossl_quic_new_listener(SSL_CTX *ctx, uint64_t flags)
4284*e7be843bSPierre Pronchery {
4285*e7be843bSPierre Pronchery QUIC_LISTENER *ql = NULL;
4286*e7be843bSPierre Pronchery QUIC_ENGINE_ARGS engine_args = {0};
4287*e7be843bSPierre Pronchery QUIC_PORT_ARGS port_args = {0};
4288*e7be843bSPierre Pronchery
4289*e7be843bSPierre Pronchery if ((ql = OPENSSL_zalloc(sizeof(*ql))) == NULL) {
4290*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4291*e7be843bSPierre Pronchery goto err;
4292*e7be843bSPierre Pronchery }
4293*e7be843bSPierre Pronchery
4294*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4295*e7be843bSPierre Pronchery if ((ql->mutex = ossl_crypto_mutex_new()) == NULL) {
4296*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4297*e7be843bSPierre Pronchery goto err;
4298*e7be843bSPierre Pronchery }
4299*e7be843bSPierre Pronchery #endif
4300*e7be843bSPierre Pronchery
4301*e7be843bSPierre Pronchery engine_args.libctx = ctx->libctx;
4302*e7be843bSPierre Pronchery engine_args.propq = ctx->propq;
4303*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4304*e7be843bSPierre Pronchery engine_args.mutex = ql->mutex;
4305*e7be843bSPierre Pronchery #endif
4306*e7be843bSPierre Pronchery
4307*e7be843bSPierre Pronchery if (need_notifier_for_domain_flags(ctx->domain_flags))
4308*e7be843bSPierre Pronchery engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
4309*e7be843bSPierre Pronchery
4310*e7be843bSPierre Pronchery if ((ql->engine = ossl_quic_engine_new(&engine_args)) == NULL) {
4311*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4312*e7be843bSPierre Pronchery goto err;
4313*e7be843bSPierre Pronchery }
4314*e7be843bSPierre Pronchery
4315*e7be843bSPierre Pronchery port_args.channel_ctx = ctx;
4316*e7be843bSPierre Pronchery port_args.is_multi_conn = 1;
4317*e7be843bSPierre Pronchery port_args.get_conn_user_ssl = alloc_port_user_ssl;
4318*e7be843bSPierre Pronchery port_args.user_ssl_arg = ql;
4319*e7be843bSPierre Pronchery if ((flags & SSL_LISTENER_FLAG_NO_VALIDATE) == 0)
4320*e7be843bSPierre Pronchery port_args.do_addr_validation = 1;
4321*e7be843bSPierre Pronchery ql->port = ossl_quic_engine_create_port(ql->engine, &port_args);
4322*e7be843bSPierre Pronchery if (ql->port == NULL) {
4323*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4324*e7be843bSPierre Pronchery goto err;
4325*e7be843bSPierre Pronchery }
4326*e7be843bSPierre Pronchery
4327*e7be843bSPierre Pronchery /* TODO(QUIC FUTURE): Implement SSL_LISTENER_FLAG_NO_ACCEPT */
4328*e7be843bSPierre Pronchery
4329*e7be843bSPierre Pronchery ossl_quic_port_set_allow_incoming(ql->port, 1);
4330*e7be843bSPierre Pronchery
4331*e7be843bSPierre Pronchery /* Initialise the QUIC_LISTENER's object header. */
4332*e7be843bSPierre Pronchery if (!ossl_quic_obj_init(&ql->obj, ctx, SSL_TYPE_QUIC_LISTENER, NULL,
4333*e7be843bSPierre Pronchery ql->engine, ql->port))
4334*e7be843bSPierre Pronchery goto err;
4335*e7be843bSPierre Pronchery
4336*e7be843bSPierre Pronchery return &ql->obj.ssl;
4337*e7be843bSPierre Pronchery
4338*e7be843bSPierre Pronchery err:
4339*e7be843bSPierre Pronchery if (ql != NULL)
4340*e7be843bSPierre Pronchery ossl_quic_engine_free(ql->engine);
4341*e7be843bSPierre Pronchery
4342*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4343*e7be843bSPierre Pronchery ossl_crypto_mutex_free(&ql->mutex);
4344*e7be843bSPierre Pronchery #endif
4345*e7be843bSPierre Pronchery OPENSSL_free(ql);
4346*e7be843bSPierre Pronchery return NULL;
4347*e7be843bSPierre Pronchery }
4348*e7be843bSPierre Pronchery
4349*e7be843bSPierre Pronchery /*
4350*e7be843bSPierre Pronchery * SSL_new_listener_from
4351*e7be843bSPierre Pronchery * ---------------------
4352*e7be843bSPierre Pronchery */
ossl_quic_new_listener_from(SSL * ssl,uint64_t flags)4353*e7be843bSPierre Pronchery SSL *ossl_quic_new_listener_from(SSL *ssl, uint64_t flags)
4354*e7be843bSPierre Pronchery {
4355*e7be843bSPierre Pronchery QCTX ctx;
4356*e7be843bSPierre Pronchery QUIC_LISTENER *ql = NULL;
4357*e7be843bSPierre Pronchery QUIC_PORT_ARGS port_args = {0};
4358*e7be843bSPierre Pronchery
4359*e7be843bSPierre Pronchery if (!expect_quic_domain(ssl, &ctx))
4360*e7be843bSPierre Pronchery return NULL;
4361*e7be843bSPierre Pronchery
4362*e7be843bSPierre Pronchery if (!SSL_up_ref(&ctx.qd->obj.ssl))
4363*e7be843bSPierre Pronchery return NULL;
4364*e7be843bSPierre Pronchery
4365*e7be843bSPierre Pronchery qctx_lock(&ctx);
4366*e7be843bSPierre Pronchery
4367*e7be843bSPierre Pronchery if ((ql = OPENSSL_zalloc(sizeof(*ql))) == NULL) {
4368*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4369*e7be843bSPierre Pronchery goto err;
4370*e7be843bSPierre Pronchery }
4371*e7be843bSPierre Pronchery
4372*e7be843bSPierre Pronchery port_args.channel_ctx = ssl->ctx;
4373*e7be843bSPierre Pronchery port_args.is_multi_conn = 1;
4374*e7be843bSPierre Pronchery port_args.get_conn_user_ssl = alloc_port_user_ssl;
4375*e7be843bSPierre Pronchery port_args.user_ssl_arg = ql;
4376*e7be843bSPierre Pronchery if ((flags & SSL_LISTENER_FLAG_NO_VALIDATE) == 0)
4377*e7be843bSPierre Pronchery port_args.do_addr_validation = 1;
4378*e7be843bSPierre Pronchery ql->port = ossl_quic_engine_create_port(ctx.qd->engine, &port_args);
4379*e7be843bSPierre Pronchery if (ql->port == NULL) {
4380*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4381*e7be843bSPierre Pronchery goto err;
4382*e7be843bSPierre Pronchery }
4383*e7be843bSPierre Pronchery
4384*e7be843bSPierre Pronchery ql->domain = ctx.qd;
4385*e7be843bSPierre Pronchery ql->engine = ctx.qd->engine;
4386*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4387*e7be843bSPierre Pronchery ql->mutex = ctx.qd->mutex;
4388*e7be843bSPierre Pronchery #endif
4389*e7be843bSPierre Pronchery
4390*e7be843bSPierre Pronchery /*
4391*e7be843bSPierre Pronchery * TODO(QUIC FUTURE): Implement SSL_LISTENER_FLAG_NO_ACCEPT
4392*e7be843bSPierre Pronchery * Given that we have apis to create client SSL objects from
4393*e7be843bSPierre Pronchery * server SSL objects (see SSL_new_from_listener), we have aspirations
4394*e7be843bSPierre Pronchery * to enable a flag that allows for the creation of the latter, but not
4395*e7be843bSPierre Pronchery * be used to do accept any connections. This is a placeholder for the
4396*e7be843bSPierre Pronchery * implementation of that flag
4397*e7be843bSPierre Pronchery */
4398*e7be843bSPierre Pronchery
4399*e7be843bSPierre Pronchery ossl_quic_port_set_allow_incoming(ql->port, 1);
4400*e7be843bSPierre Pronchery
4401*e7be843bSPierre Pronchery /* Initialise the QUIC_LISTENER's object header. */
4402*e7be843bSPierre Pronchery if (!ossl_quic_obj_init(&ql->obj, ssl->ctx, SSL_TYPE_QUIC_LISTENER,
4403*e7be843bSPierre Pronchery &ctx.qd->obj.ssl, NULL, ql->port))
4404*e7be843bSPierre Pronchery goto err;
4405*e7be843bSPierre Pronchery
4406*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4407*e7be843bSPierre Pronchery return &ql->obj.ssl;
4408*e7be843bSPierre Pronchery
4409*e7be843bSPierre Pronchery err:
4410*e7be843bSPierre Pronchery if (ql != NULL)
4411*e7be843bSPierre Pronchery ossl_quic_port_free(ql->port);
4412*e7be843bSPierre Pronchery
4413*e7be843bSPierre Pronchery OPENSSL_free(ql);
4414*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4415*e7be843bSPierre Pronchery SSL_free(&ctx.qd->obj.ssl);
4416*e7be843bSPierre Pronchery
4417*e7be843bSPierre Pronchery return NULL;
4418*e7be843bSPierre Pronchery }
4419*e7be843bSPierre Pronchery
4420*e7be843bSPierre Pronchery /*
4421*e7be843bSPierre Pronchery * SSL_new_from_listener
4422*e7be843bSPierre Pronchery * ---------------------
4423*e7be843bSPierre Pronchery * code here is derived from ossl_quic_new(). The `ssl` argument is
4424*e7be843bSPierre Pronchery * a listener object which already comes with QUIC port/engine. The newly
4425*e7be843bSPierre Pronchery * created QUIC connection object (QCSO) is going to share the port/engine
4426*e7be843bSPierre Pronchery * with listener (`ssl`). The `ssl` also becomes a parent of QCSO created
4427*e7be843bSPierre Pronchery * by this function. The caller uses QCSO instance to connect to
4428*e7be843bSPierre Pronchery * remote QUIC server.
4429*e7be843bSPierre Pronchery *
4430*e7be843bSPierre Pronchery * The QCSO created here requires us to also create a channel so we
4431*e7be843bSPierre Pronchery * can connect to remote server.
4432*e7be843bSPierre Pronchery */
ossl_quic_new_from_listener(SSL * ssl,uint64_t flags)4433*e7be843bSPierre Pronchery SSL *ossl_quic_new_from_listener(SSL *ssl, uint64_t flags)
4434*e7be843bSPierre Pronchery {
4435*e7be843bSPierre Pronchery QCTX ctx;
4436*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = NULL;
4437*e7be843bSPierre Pronchery QUIC_LISTENER *ql;
4438*e7be843bSPierre Pronchery SSL_CONNECTION *sc = NULL;
4439*e7be843bSPierre Pronchery
4440*e7be843bSPierre Pronchery if (flags != 0)
4441*e7be843bSPierre Pronchery return NULL;
4442*e7be843bSPierre Pronchery
4443*e7be843bSPierre Pronchery if (!expect_quic_listener(ssl, &ctx))
4444*e7be843bSPierre Pronchery return NULL;
4445*e7be843bSPierre Pronchery
4446*e7be843bSPierre Pronchery if (!SSL_up_ref(&ctx.ql->obj.ssl))
4447*e7be843bSPierre Pronchery return NULL;
4448*e7be843bSPierre Pronchery
4449*e7be843bSPierre Pronchery qctx_lock(&ctx);
4450*e7be843bSPierre Pronchery
4451*e7be843bSPierre Pronchery ql = ctx.ql;
4452*e7be843bSPierre Pronchery
4453*e7be843bSPierre Pronchery /*
4454*e7be843bSPierre Pronchery * listeners (server) contexts don't typically
4455*e7be843bSPierre Pronchery * allocate a token cache because they don't need
4456*e7be843bSPierre Pronchery * to store them, but here we are using a server side
4457*e7be843bSPierre Pronchery * ctx as a client, so we should allocate one now
4458*e7be843bSPierre Pronchery */
4459*e7be843bSPierre Pronchery if (ssl->ctx->tokencache == NULL)
4460*e7be843bSPierre Pronchery if ((ssl->ctx->tokencache = ossl_quic_new_token_store()) == NULL)
4461*e7be843bSPierre Pronchery goto err;
4462*e7be843bSPierre Pronchery
4463*e7be843bSPierre Pronchery if ((qc = OPENSSL_zalloc(sizeof(*qc))) == NULL) {
4464*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4465*e7be843bSPierre Pronchery goto err;
4466*e7be843bSPierre Pronchery }
4467*e7be843bSPierre Pronchery
4468*e7be843bSPierre Pronchery /*
4469*e7be843bSPierre Pronchery * NOTE: setting a listener here is needed so `qc_cleanup()` does the right
4470*e7be843bSPierre Pronchery * thing. Setting listener to ql avoids premature destruction of port in
4471*e7be843bSPierre Pronchery * qc_cleanup()
4472*e7be843bSPierre Pronchery */
4473*e7be843bSPierre Pronchery qc->listener = ql;
4474*e7be843bSPierre Pronchery qc->engine = ql->engine;
4475*e7be843bSPierre Pronchery qc->port = ql->port;
4476*e7be843bSPierre Pronchery /* create channel */
4477*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4478*e7be843bSPierre Pronchery /* this is the engine mutex */
4479*e7be843bSPierre Pronchery qc->mutex = ql->mutex;
4480*e7be843bSPierre Pronchery #endif
4481*e7be843bSPierre Pronchery #if !defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
4482*e7be843bSPierre Pronchery qc->is_thread_assisted
4483*e7be843bSPierre Pronchery = ((ql->obj.domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0);
4484*e7be843bSPierre Pronchery #endif
4485*e7be843bSPierre Pronchery
4486*e7be843bSPierre Pronchery /* Create the handshake layer. */
4487*e7be843bSPierre Pronchery qc->tls = ossl_ssl_connection_new_int(ql->obj.ssl.ctx, NULL, TLS_method());
4488*e7be843bSPierre Pronchery if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) {
4489*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4490*e7be843bSPierre Pronchery goto err;
4491*e7be843bSPierre Pronchery }
4492*e7be843bSPierre Pronchery sc->s3.flags |= TLS1_FLAGS_QUIC | TLS1_FLAGS_QUIC_INTERNAL;
4493*e7be843bSPierre Pronchery
4494*e7be843bSPierre Pronchery qc->default_ssl_options = OSSL_QUIC_PERMITTED_OPTIONS;
4495*e7be843bSPierre Pronchery qc->last_error = SSL_ERROR_NONE;
4496*e7be843bSPierre Pronchery
4497*e7be843bSPierre Pronchery /*
4498*e7be843bSPierre Pronchery * This is QCSO, we don't expect to accept connections
4499*e7be843bSPierre Pronchery * on success the channel assumes ownership of tls, we need
4500*e7be843bSPierre Pronchery * to grab reference for qc.
4501*e7be843bSPierre Pronchery */
4502*e7be843bSPierre Pronchery qc->ch = ossl_quic_port_create_outgoing(qc->port, qc->tls);
4503*e7be843bSPierre Pronchery
4504*e7be843bSPierre Pronchery ossl_quic_channel_set_msg_callback(qc->ch, ql->obj.ssl.ctx->msg_callback, &qc->obj.ssl);
4505*e7be843bSPierre Pronchery ossl_quic_channel_set_msg_callback_arg(qc->ch, ql->obj.ssl.ctx->msg_callback_arg);
4506*e7be843bSPierre Pronchery
4507*e7be843bSPierre Pronchery /*
4508*e7be843bSPierre Pronchery * We deliberately pass NULL for engine and port, because we don't want to
4509*e7be843bSPierre Pronchery * to turn QCSO we create here into an event leader, nor port leader.
4510*e7be843bSPierre Pronchery * Both those roles are occupied already by listener (`ssl`) we use
4511*e7be843bSPierre Pronchery * to create a new QCSO here.
4512*e7be843bSPierre Pronchery */
4513*e7be843bSPierre Pronchery if (!ossl_quic_obj_init(&qc->obj, ql->obj.ssl.ctx,
4514*e7be843bSPierre Pronchery SSL_TYPE_QUIC_CONNECTION,
4515*e7be843bSPierre Pronchery &ql->obj.ssl, NULL, NULL)) {
4516*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4517*e7be843bSPierre Pronchery goto err;
4518*e7be843bSPierre Pronchery }
4519*e7be843bSPierre Pronchery
4520*e7be843bSPierre Pronchery /* Initialise libssl APL-related state. */
4521*e7be843bSPierre Pronchery qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
4522*e7be843bSPierre Pronchery qc->default_ssl_mode = qc->obj.ssl.ctx->mode;
4523*e7be843bSPierre Pronchery qc->default_ssl_options = qc->obj.ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
4524*e7be843bSPierre Pronchery qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
4525*e7be843bSPierre Pronchery qc->last_error = SSL_ERROR_NONE;
4526*e7be843bSPierre Pronchery
4527*e7be843bSPierre Pronchery qc_update_reject_policy(qc);
4528*e7be843bSPierre Pronchery
4529*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4530*e7be843bSPierre Pronchery
4531*e7be843bSPierre Pronchery return &qc->obj.ssl;
4532*e7be843bSPierre Pronchery
4533*e7be843bSPierre Pronchery err:
4534*e7be843bSPierre Pronchery if (qc != NULL) {
4535*e7be843bSPierre Pronchery qc_cleanup(qc, /* have_lock= */ 0);
4536*e7be843bSPierre Pronchery OPENSSL_free(qc);
4537*e7be843bSPierre Pronchery }
4538*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4539*e7be843bSPierre Pronchery SSL_free(&ctx.ql->obj.ssl);
4540*e7be843bSPierre Pronchery
4541*e7be843bSPierre Pronchery return NULL;
4542*e7be843bSPierre Pronchery }
4543*e7be843bSPierre Pronchery
4544*e7be843bSPierre Pronchery /*
4545*e7be843bSPierre Pronchery * SSL_listen
4546*e7be843bSPierre Pronchery * ----------
4547*e7be843bSPierre Pronchery */
4548*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
ql_listen(QUIC_LISTENER * ql)4549*e7be843bSPierre Pronchery static int ql_listen(QUIC_LISTENER *ql)
4550*e7be843bSPierre Pronchery {
4551*e7be843bSPierre Pronchery if (ql->listening)
4552*e7be843bSPierre Pronchery return 1;
4553*e7be843bSPierre Pronchery
4554*e7be843bSPierre Pronchery ossl_quic_port_set_allow_incoming(ql->port, 1);
4555*e7be843bSPierre Pronchery ql->listening = 1;
4556*e7be843bSPierre Pronchery return 1;
4557*e7be843bSPierre Pronchery }
4558*e7be843bSPierre Pronchery
4559*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_listen(SSL * ssl)4560*e7be843bSPierre Pronchery int ossl_quic_listen(SSL *ssl)
4561*e7be843bSPierre Pronchery {
4562*e7be843bSPierre Pronchery QCTX ctx;
4563*e7be843bSPierre Pronchery int ret;
4564*e7be843bSPierre Pronchery
4565*e7be843bSPierre Pronchery if (!expect_quic_listener(ssl, &ctx))
4566*e7be843bSPierre Pronchery return 0;
4567*e7be843bSPierre Pronchery
4568*e7be843bSPierre Pronchery qctx_lock_for_io(&ctx);
4569*e7be843bSPierre Pronchery
4570*e7be843bSPierre Pronchery ret = ql_listen(ctx.ql);
4571*e7be843bSPierre Pronchery
4572*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4573*e7be843bSPierre Pronchery return ret;
4574*e7be843bSPierre Pronchery }
4575*e7be843bSPierre Pronchery
4576*e7be843bSPierre Pronchery /*
4577*e7be843bSPierre Pronchery * SSL_accept_connection
4578*e7be843bSPierre Pronchery * ---------------------
4579*e7be843bSPierre Pronchery */
quic_accept_connection_wait(void * arg)4580*e7be843bSPierre Pronchery static int quic_accept_connection_wait(void *arg)
4581*e7be843bSPierre Pronchery {
4582*e7be843bSPierre Pronchery QUIC_PORT *port = arg;
4583*e7be843bSPierre Pronchery
4584*e7be843bSPierre Pronchery if (!ossl_quic_port_is_running(port))
4585*e7be843bSPierre Pronchery return -1;
4586*e7be843bSPierre Pronchery
4587*e7be843bSPierre Pronchery if (ossl_quic_port_have_incoming(port))
4588*e7be843bSPierre Pronchery return 1;
4589*e7be843bSPierre Pronchery
4590*e7be843bSPierre Pronchery return 0;
4591*e7be843bSPierre Pronchery }
4592*e7be843bSPierre Pronchery
4593*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_accept_connection(SSL * ssl,uint64_t flags)4594*e7be843bSPierre Pronchery SSL *ossl_quic_accept_connection(SSL *ssl, uint64_t flags)
4595*e7be843bSPierre Pronchery {
4596*e7be843bSPierre Pronchery int ret;
4597*e7be843bSPierre Pronchery QCTX ctx;
4598*e7be843bSPierre Pronchery SSL *conn_ssl = NULL;
4599*e7be843bSPierre Pronchery SSL_CONNECTION *conn = NULL;
4600*e7be843bSPierre Pronchery QUIC_CHANNEL *new_ch = NULL;
4601*e7be843bSPierre Pronchery QUIC_CONNECTION *qc;
4602*e7be843bSPierre Pronchery int no_block = ((flags & SSL_ACCEPT_CONNECTION_NO_BLOCK) != 0);
4603*e7be843bSPierre Pronchery
4604*e7be843bSPierre Pronchery if (!expect_quic_listener(ssl, &ctx))
4605*e7be843bSPierre Pronchery return NULL;
4606*e7be843bSPierre Pronchery
4607*e7be843bSPierre Pronchery qctx_lock_for_io(&ctx);
4608*e7be843bSPierre Pronchery
4609*e7be843bSPierre Pronchery if (!ql_listen(ctx.ql))
4610*e7be843bSPierre Pronchery goto out;
4611*e7be843bSPierre Pronchery
4612*e7be843bSPierre Pronchery /* Wait for an incoming connection if needed. */
4613*e7be843bSPierre Pronchery new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4614*e7be843bSPierre Pronchery if (new_ch == NULL && ossl_quic_port_is_running(ctx.ql->port)) {
4615*e7be843bSPierre Pronchery if (!no_block && qctx_blocking(&ctx)) {
4616*e7be843bSPierre Pronchery ret = block_until_pred(&ctx, quic_accept_connection_wait,
4617*e7be843bSPierre Pronchery ctx.ql->port, 0);
4618*e7be843bSPierre Pronchery if (ret < 1)
4619*e7be843bSPierre Pronchery goto out;
4620*e7be843bSPierre Pronchery } else {
4621*e7be843bSPierre Pronchery qctx_maybe_autotick(&ctx);
4622*e7be843bSPierre Pronchery }
4623*e7be843bSPierre Pronchery
4624*e7be843bSPierre Pronchery if (!ossl_quic_port_is_running(ctx.ql->port))
4625*e7be843bSPierre Pronchery goto out;
4626*e7be843bSPierre Pronchery
4627*e7be843bSPierre Pronchery new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4628*e7be843bSPierre Pronchery }
4629*e7be843bSPierre Pronchery
4630*e7be843bSPierre Pronchery if (new_ch == NULL && ossl_quic_port_is_running(ctx.ql->port)) {
4631*e7be843bSPierre Pronchery /* No connections already queued. */
4632*e7be843bSPierre Pronchery ossl_quic_reactor_tick(ossl_quic_engine_get0_reactor(ctx.ql->engine), 0);
4633*e7be843bSPierre Pronchery
4634*e7be843bSPierre Pronchery new_ch = ossl_quic_port_pop_incoming(ctx.ql->port);
4635*e7be843bSPierre Pronchery }
4636*e7be843bSPierre Pronchery
4637*e7be843bSPierre Pronchery /*
4638*e7be843bSPierre Pronchery * port_make_channel pre-allocates our user_ssl for us for each newly
4639*e7be843bSPierre Pronchery * created channel, so once we pop the new channel from the port above
4640*e7be843bSPierre Pronchery * we just need to extract it
4641*e7be843bSPierre Pronchery */
4642*e7be843bSPierre Pronchery if (new_ch == NULL
4643*e7be843bSPierre Pronchery || (conn_ssl = ossl_quic_channel_get0_tls(new_ch)) == NULL
4644*e7be843bSPierre Pronchery || (conn = SSL_CONNECTION_FROM_SSL(conn_ssl)) == NULL
4645*e7be843bSPierre Pronchery || (conn_ssl = SSL_CONNECTION_GET_USER_SSL(conn)) == NULL)
4646*e7be843bSPierre Pronchery goto out;
4647*e7be843bSPierre Pronchery qc = (QUIC_CONNECTION *)conn_ssl;
4648*e7be843bSPierre Pronchery qc->listener = ctx.ql;
4649*e7be843bSPierre Pronchery qc->pending = 0;
4650*e7be843bSPierre Pronchery if (!SSL_up_ref(&ctx.ql->obj.ssl)) {
4651*e7be843bSPierre Pronchery SSL_free(conn_ssl);
4652*e7be843bSPierre Pronchery SSL_free(ossl_quic_channel_get0_tls(new_ch));
4653*e7be843bSPierre Pronchery conn_ssl = NULL;
4654*e7be843bSPierre Pronchery }
4655*e7be843bSPierre Pronchery
4656*e7be843bSPierre Pronchery out:
4657*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4658*e7be843bSPierre Pronchery return conn_ssl;
4659*e7be843bSPierre Pronchery }
4660*e7be843bSPierre Pronchery
create_qc_from_incoming_conn(QUIC_LISTENER * ql,QUIC_CHANNEL * ch)4661*e7be843bSPierre Pronchery static QUIC_CONNECTION *create_qc_from_incoming_conn(QUIC_LISTENER *ql, QUIC_CHANNEL *ch)
4662*e7be843bSPierre Pronchery {
4663*e7be843bSPierre Pronchery QUIC_CONNECTION *qc = NULL;
4664*e7be843bSPierre Pronchery
4665*e7be843bSPierre Pronchery if ((qc = OPENSSL_zalloc(sizeof(*qc))) == NULL) {
4666*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4667*e7be843bSPierre Pronchery goto err;
4668*e7be843bSPierre Pronchery }
4669*e7be843bSPierre Pronchery
4670*e7be843bSPierre Pronchery if (!ossl_quic_obj_init(&qc->obj, ql->obj.ssl.ctx,
4671*e7be843bSPierre Pronchery SSL_TYPE_QUIC_CONNECTION,
4672*e7be843bSPierre Pronchery &ql->obj.ssl, NULL, NULL)) {
4673*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4674*e7be843bSPierre Pronchery goto err;
4675*e7be843bSPierre Pronchery }
4676*e7be843bSPierre Pronchery
4677*e7be843bSPierre Pronchery ossl_quic_channel_get_peer_addr(ch, &qc->init_peer_addr); /* best effort */
4678*e7be843bSPierre Pronchery qc->pending = 1;
4679*e7be843bSPierre Pronchery qc->engine = ql->engine;
4680*e7be843bSPierre Pronchery qc->port = ql->port;
4681*e7be843bSPierre Pronchery qc->ch = ch;
4682*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4683*e7be843bSPierre Pronchery qc->mutex = ql->mutex;
4684*e7be843bSPierre Pronchery #endif
4685*e7be843bSPierre Pronchery qc->tls = ossl_quic_channel_get0_tls(ch);
4686*e7be843bSPierre Pronchery qc->started = 1;
4687*e7be843bSPierre Pronchery qc->as_server = 1;
4688*e7be843bSPierre Pronchery qc->as_server_state = 1;
4689*e7be843bSPierre Pronchery qc->default_stream_mode = SSL_DEFAULT_STREAM_MODE_AUTO_BIDI;
4690*e7be843bSPierre Pronchery qc->default_ssl_options = ql->obj.ssl.ctx->options & OSSL_QUIC_PERMITTED_OPTIONS;
4691*e7be843bSPierre Pronchery qc->incoming_stream_policy = SSL_INCOMING_STREAM_POLICY_AUTO;
4692*e7be843bSPierre Pronchery qc->last_error = SSL_ERROR_NONE;
4693*e7be843bSPierre Pronchery qc_update_reject_policy(qc);
4694*e7be843bSPierre Pronchery return qc;
4695*e7be843bSPierre Pronchery
4696*e7be843bSPierre Pronchery err:
4697*e7be843bSPierre Pronchery OPENSSL_free(qc);
4698*e7be843bSPierre Pronchery return NULL;
4699*e7be843bSPierre Pronchery }
4700*e7be843bSPierre Pronchery
4701*e7be843bSPierre Pronchery DEFINE_LHASH_OF_EX(QUIC_TOKEN);
4702*e7be843bSPierre Pronchery
4703*e7be843bSPierre Pronchery struct ssl_token_store_st {
4704*e7be843bSPierre Pronchery LHASH_OF(QUIC_TOKEN) *cache;
4705*e7be843bSPierre Pronchery CRYPTO_REF_COUNT references;
4706*e7be843bSPierre Pronchery CRYPTO_MUTEX *mutex;
4707*e7be843bSPierre Pronchery };
4708*e7be843bSPierre Pronchery
quic_token_hash(const QUIC_TOKEN * item)4709*e7be843bSPierre Pronchery static unsigned long quic_token_hash(const QUIC_TOKEN *item)
4710*e7be843bSPierre Pronchery {
4711*e7be843bSPierre Pronchery return (unsigned long)ossl_fnv1a_hash(item->hashkey, item->hashkey_len);
4712*e7be843bSPierre Pronchery }
4713*e7be843bSPierre Pronchery
quic_token_cmp(const QUIC_TOKEN * a,const QUIC_TOKEN * b)4714*e7be843bSPierre Pronchery static int quic_token_cmp(const QUIC_TOKEN *a, const QUIC_TOKEN *b)
4715*e7be843bSPierre Pronchery {
4716*e7be843bSPierre Pronchery if (a->hashkey_len != b->hashkey_len)
4717*e7be843bSPierre Pronchery return 1;
4718*e7be843bSPierre Pronchery return memcmp(a->hashkey, b->hashkey, a->hashkey_len);
4719*e7be843bSPierre Pronchery }
4720*e7be843bSPierre Pronchery
ossl_quic_new_token_store(void)4721*e7be843bSPierre Pronchery SSL_TOKEN_STORE *ossl_quic_new_token_store(void)
4722*e7be843bSPierre Pronchery {
4723*e7be843bSPierre Pronchery int ok = 0;
4724*e7be843bSPierre Pronchery SSL_TOKEN_STORE *newcache = OPENSSL_zalloc(sizeof(SSL_TOKEN_STORE));
4725*e7be843bSPierre Pronchery
4726*e7be843bSPierre Pronchery if (newcache == NULL)
4727*e7be843bSPierre Pronchery goto out;
4728*e7be843bSPierre Pronchery
4729*e7be843bSPierre Pronchery newcache->cache = lh_QUIC_TOKEN_new(quic_token_hash, quic_token_cmp);
4730*e7be843bSPierre Pronchery if (newcache->cache == NULL)
4731*e7be843bSPierre Pronchery goto out;
4732*e7be843bSPierre Pronchery
4733*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4734*e7be843bSPierre Pronchery if ((newcache->mutex = ossl_crypto_mutex_new()) == NULL)
4735*e7be843bSPierre Pronchery goto out;
4736*e7be843bSPierre Pronchery #endif
4737*e7be843bSPierre Pronchery
4738*e7be843bSPierre Pronchery if (!CRYPTO_NEW_REF(&newcache->references, 1))
4739*e7be843bSPierre Pronchery goto out;
4740*e7be843bSPierre Pronchery
4741*e7be843bSPierre Pronchery ok = 1;
4742*e7be843bSPierre Pronchery out:
4743*e7be843bSPierre Pronchery if (!ok) {
4744*e7be843bSPierre Pronchery ossl_quic_free_token_store(newcache);
4745*e7be843bSPierre Pronchery newcache = NULL;
4746*e7be843bSPierre Pronchery }
4747*e7be843bSPierre Pronchery return newcache;
4748*e7be843bSPierre Pronchery }
4749*e7be843bSPierre Pronchery
free_this_token(QUIC_TOKEN * tok)4750*e7be843bSPierre Pronchery static void free_this_token(QUIC_TOKEN *tok)
4751*e7be843bSPierre Pronchery {
4752*e7be843bSPierre Pronchery ossl_quic_free_peer_token(tok);
4753*e7be843bSPierre Pronchery }
4754*e7be843bSPierre Pronchery
ossl_quic_free_token_store(SSL_TOKEN_STORE * hdl)4755*e7be843bSPierre Pronchery void ossl_quic_free_token_store(SSL_TOKEN_STORE *hdl)
4756*e7be843bSPierre Pronchery {
4757*e7be843bSPierre Pronchery int refs;
4758*e7be843bSPierre Pronchery
4759*e7be843bSPierre Pronchery if (hdl == NULL)
4760*e7be843bSPierre Pronchery return;
4761*e7be843bSPierre Pronchery
4762*e7be843bSPierre Pronchery if (!CRYPTO_DOWN_REF(&hdl->references, &refs))
4763*e7be843bSPierre Pronchery return;
4764*e7be843bSPierre Pronchery
4765*e7be843bSPierre Pronchery if (refs > 0)
4766*e7be843bSPierre Pronchery return;
4767*e7be843bSPierre Pronchery
4768*e7be843bSPierre Pronchery /* last reference, we can clean up */
4769*e7be843bSPierre Pronchery ossl_crypto_mutex_free(&hdl->mutex);
4770*e7be843bSPierre Pronchery lh_QUIC_TOKEN_doall(hdl->cache, free_this_token);
4771*e7be843bSPierre Pronchery lh_QUIC_TOKEN_free(hdl->cache);
4772*e7be843bSPierre Pronchery OPENSSL_free(hdl);
4773*e7be843bSPierre Pronchery return;
4774*e7be843bSPierre Pronchery }
4775*e7be843bSPierre Pronchery
4776*e7be843bSPierre Pronchery /**
4777*e7be843bSPierre Pronchery * @brief build a new QUIC_TOKEN
4778*e7be843bSPierre Pronchery *
4779*e7be843bSPierre Pronchery * This function creates a new token storage structure for saving in our
4780*e7be843bSPierre Pronchery * tokencache
4781*e7be843bSPierre Pronchery *
4782*e7be843bSPierre Pronchery * In an effort to make allocation and freeing of these tokens a bit faster
4783*e7be843bSPierre Pronchery * We do them in a single allocation in this format
4784*e7be843bSPierre Pronchery * +---------------+ --\
4785*e7be843bSPierre Pronchery * | hashkey * |---| |
4786*e7be843bSPierre Pronchery * | hashkey_len | | | QUIC_TOKEN
4787*e7be843bSPierre Pronchery * | token * |---|--| |
4788*e7be843bSPierre Pronchery * | token_len | | | |
4789*e7be843bSPierre Pronchery * +---------------+<--| | --/
4790*e7be843bSPierre Pronchery * | hashkey buf | |
4791*e7be843bSPierre Pronchery * | | |
4792*e7be843bSPierre Pronchery * |---------------|<-----|
4793*e7be843bSPierre Pronchery * | token buf |
4794*e7be843bSPierre Pronchery * | |
4795*e7be843bSPierre Pronchery * +---------------+
4796*e7be843bSPierre Pronchery *
4797*e7be843bSPierre Pronchery * @param peer - the peer address that sent the token
4798*e7be843bSPierre Pronchery * @param token - the buffer holding the token
4799*e7be843bSPierre Pronchery * @param token_len - the size of token
4800*e7be843bSPierre Pronchery *
4801*e7be843bSPierre Pronchery * @returns a QUIC_TOKEN pointer or NULL on error
4802*e7be843bSPierre Pronchery */
ossl_quic_build_new_token(BIO_ADDR * peer,uint8_t * token,size_t token_len)4803*e7be843bSPierre Pronchery static QUIC_TOKEN *ossl_quic_build_new_token(BIO_ADDR *peer, uint8_t *token,
4804*e7be843bSPierre Pronchery size_t token_len)
4805*e7be843bSPierre Pronchery {
4806*e7be843bSPierre Pronchery QUIC_TOKEN *new_token;
4807*e7be843bSPierre Pronchery size_t hashkey_len = 0;
4808*e7be843bSPierre Pronchery size_t addr_len = 0;
4809*e7be843bSPierre Pronchery int family;
4810*e7be843bSPierre Pronchery unsigned short port;
4811*e7be843bSPierre Pronchery int *famptr;
4812*e7be843bSPierre Pronchery unsigned short *portptr;
4813*e7be843bSPierre Pronchery uint8_t *addrptr;
4814*e7be843bSPierre Pronchery
4815*e7be843bSPierre Pronchery if ((token != NULL && token_len == 0) || (token == NULL && token_len != 0))
4816*e7be843bSPierre Pronchery return NULL;
4817*e7be843bSPierre Pronchery
4818*e7be843bSPierre Pronchery if (!BIO_ADDR_rawaddress(peer, NULL, &addr_len))
4819*e7be843bSPierre Pronchery return NULL;
4820*e7be843bSPierre Pronchery family = BIO_ADDR_family(peer);
4821*e7be843bSPierre Pronchery port = BIO_ADDR_rawport(peer);
4822*e7be843bSPierre Pronchery
4823*e7be843bSPierre Pronchery hashkey_len += sizeof(int); /* hashkey(family) */
4824*e7be843bSPierre Pronchery hashkey_len += sizeof(unsigned short); /* hashkey(port) */
4825*e7be843bSPierre Pronchery hashkey_len += addr_len; /* hashkey(address) */
4826*e7be843bSPierre Pronchery
4827*e7be843bSPierre Pronchery new_token = OPENSSL_zalloc(sizeof(QUIC_TOKEN) + hashkey_len + token_len);
4828*e7be843bSPierre Pronchery if (new_token == NULL)
4829*e7be843bSPierre Pronchery return NULL;
4830*e7be843bSPierre Pronchery
4831*e7be843bSPierre Pronchery if (!CRYPTO_NEW_REF(&new_token->references, 1)) {
4832*e7be843bSPierre Pronchery OPENSSL_free(new_token);
4833*e7be843bSPierre Pronchery return NULL;
4834*e7be843bSPierre Pronchery }
4835*e7be843bSPierre Pronchery
4836*e7be843bSPierre Pronchery new_token->hashkey_len = hashkey_len;
4837*e7be843bSPierre Pronchery /* hashkey is allocated inline, immediately after the QUIC_TOKEN struct */
4838*e7be843bSPierre Pronchery new_token->hashkey = (uint8_t *)(new_token + 1);
4839*e7be843bSPierre Pronchery /* token buffer follows the hashkey in the inline allocation */
4840*e7be843bSPierre Pronchery new_token->token = new_token->hashkey + hashkey_len;
4841*e7be843bSPierre Pronchery new_token->token_len = token_len;
4842*e7be843bSPierre Pronchery famptr = (int *)new_token->hashkey;
4843*e7be843bSPierre Pronchery portptr = (unsigned short *)(famptr + 1);
4844*e7be843bSPierre Pronchery addrptr = (uint8_t *)(portptr + 1);
4845*e7be843bSPierre Pronchery *famptr = family;
4846*e7be843bSPierre Pronchery *portptr = port;
4847*e7be843bSPierre Pronchery if (!BIO_ADDR_rawaddress(peer, addrptr, NULL)) {
4848*e7be843bSPierre Pronchery ossl_quic_free_peer_token(new_token);
4849*e7be843bSPierre Pronchery return NULL;
4850*e7be843bSPierre Pronchery }
4851*e7be843bSPierre Pronchery if (token != NULL)
4852*e7be843bSPierre Pronchery memcpy(new_token->token, token, token_len);
4853*e7be843bSPierre Pronchery return new_token;
4854*e7be843bSPierre Pronchery }
4855*e7be843bSPierre Pronchery
ossl_quic_set_peer_token(SSL_CTX * ctx,BIO_ADDR * peer,const uint8_t * token,size_t token_len)4856*e7be843bSPierre Pronchery int ossl_quic_set_peer_token(SSL_CTX *ctx, BIO_ADDR *peer,
4857*e7be843bSPierre Pronchery const uint8_t *token, size_t token_len)
4858*e7be843bSPierre Pronchery {
4859*e7be843bSPierre Pronchery SSL_TOKEN_STORE *c = ctx->tokencache;
4860*e7be843bSPierre Pronchery QUIC_TOKEN *tok, *old = NULL;
4861*e7be843bSPierre Pronchery
4862*e7be843bSPierre Pronchery if (ctx->tokencache == NULL)
4863*e7be843bSPierre Pronchery return 0;
4864*e7be843bSPierre Pronchery
4865*e7be843bSPierre Pronchery tok = ossl_quic_build_new_token(peer, (uint8_t *)token, token_len);
4866*e7be843bSPierre Pronchery if (tok == NULL)
4867*e7be843bSPierre Pronchery return 0;
4868*e7be843bSPierre Pronchery
4869*e7be843bSPierre Pronchery /* we might be sharing this cache, lock it */
4870*e7be843bSPierre Pronchery ossl_crypto_mutex_lock(c->mutex);
4871*e7be843bSPierre Pronchery
4872*e7be843bSPierre Pronchery old = lh_QUIC_TOKEN_retrieve(c->cache, tok);
4873*e7be843bSPierre Pronchery if (old != NULL) {
4874*e7be843bSPierre Pronchery lh_QUIC_TOKEN_delete(c->cache, old);
4875*e7be843bSPierre Pronchery ossl_quic_free_peer_token(old);
4876*e7be843bSPierre Pronchery }
4877*e7be843bSPierre Pronchery lh_QUIC_TOKEN_insert(c->cache, tok);
4878*e7be843bSPierre Pronchery
4879*e7be843bSPierre Pronchery ossl_crypto_mutex_unlock(c->mutex);
4880*e7be843bSPierre Pronchery return 1;
4881*e7be843bSPierre Pronchery }
4882*e7be843bSPierre Pronchery
ossl_quic_get_peer_token(SSL_CTX * ctx,BIO_ADDR * peer,QUIC_TOKEN ** token)4883*e7be843bSPierre Pronchery int ossl_quic_get_peer_token(SSL_CTX *ctx, BIO_ADDR *peer,
4884*e7be843bSPierre Pronchery QUIC_TOKEN **token)
4885*e7be843bSPierre Pronchery {
4886*e7be843bSPierre Pronchery SSL_TOKEN_STORE *c = ctx->tokencache;
4887*e7be843bSPierre Pronchery QUIC_TOKEN *key = NULL;
4888*e7be843bSPierre Pronchery QUIC_TOKEN *tok = NULL;
4889*e7be843bSPierre Pronchery int ret;
4890*e7be843bSPierre Pronchery int rc = 0;
4891*e7be843bSPierre Pronchery
4892*e7be843bSPierre Pronchery if (c == NULL)
4893*e7be843bSPierre Pronchery return 0;
4894*e7be843bSPierre Pronchery
4895*e7be843bSPierre Pronchery key = ossl_quic_build_new_token(peer, NULL, 0);
4896*e7be843bSPierre Pronchery if (key == NULL)
4897*e7be843bSPierre Pronchery return 0;
4898*e7be843bSPierre Pronchery
4899*e7be843bSPierre Pronchery ossl_crypto_mutex_lock(c->mutex);
4900*e7be843bSPierre Pronchery tok = lh_QUIC_TOKEN_retrieve(c->cache, key);
4901*e7be843bSPierre Pronchery if (tok != NULL) {
4902*e7be843bSPierre Pronchery *token = tok;
4903*e7be843bSPierre Pronchery CRYPTO_UP_REF(&tok->references, &ret);
4904*e7be843bSPierre Pronchery rc = 1;
4905*e7be843bSPierre Pronchery }
4906*e7be843bSPierre Pronchery
4907*e7be843bSPierre Pronchery ossl_crypto_mutex_unlock(c->mutex);
4908*e7be843bSPierre Pronchery ossl_quic_free_peer_token(key);
4909*e7be843bSPierre Pronchery return rc;
4910*e7be843bSPierre Pronchery }
4911*e7be843bSPierre Pronchery
ossl_quic_free_peer_token(QUIC_TOKEN * token)4912*e7be843bSPierre Pronchery void ossl_quic_free_peer_token(QUIC_TOKEN *token)
4913*e7be843bSPierre Pronchery {
4914*e7be843bSPierre Pronchery int refs = 0;
4915*e7be843bSPierre Pronchery
4916*e7be843bSPierre Pronchery if (!CRYPTO_DOWN_REF(&token->references, &refs))
4917*e7be843bSPierre Pronchery return;
4918*e7be843bSPierre Pronchery
4919*e7be843bSPierre Pronchery if (refs > 0)
4920*e7be843bSPierre Pronchery return;
4921*e7be843bSPierre Pronchery
4922*e7be843bSPierre Pronchery CRYPTO_FREE_REF(&token->references);
4923*e7be843bSPierre Pronchery OPENSSL_free(token);
4924*e7be843bSPierre Pronchery }
4925*e7be843bSPierre Pronchery
4926*e7be843bSPierre Pronchery /*
4927*e7be843bSPierre Pronchery * SSL_get_accept_connection_queue_len
4928*e7be843bSPierre Pronchery * -----------------------------------
4929*e7be843bSPierre Pronchery */
4930*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_get_accept_connection_queue_len(SSL * ssl)4931*e7be843bSPierre Pronchery size_t ossl_quic_get_accept_connection_queue_len(SSL *ssl)
4932*e7be843bSPierre Pronchery {
4933*e7be843bSPierre Pronchery QCTX ctx;
4934*e7be843bSPierre Pronchery int ret;
4935*e7be843bSPierre Pronchery
4936*e7be843bSPierre Pronchery if (!expect_quic_listener(ssl, &ctx))
4937*e7be843bSPierre Pronchery return 0;
4938*e7be843bSPierre Pronchery
4939*e7be843bSPierre Pronchery qctx_lock(&ctx);
4940*e7be843bSPierre Pronchery
4941*e7be843bSPierre Pronchery ret = ossl_quic_port_get_num_incoming_channels(ctx.ql->port);
4942*e7be843bSPierre Pronchery
4943*e7be843bSPierre Pronchery qctx_unlock(&ctx);
4944*e7be843bSPierre Pronchery return ret;
4945*e7be843bSPierre Pronchery }
4946*e7be843bSPierre Pronchery
4947*e7be843bSPierre Pronchery /*
4948*e7be843bSPierre Pronchery * QUIC Front-End I/O API: Domains
4949*e7be843bSPierre Pronchery * ===============================
4950*e7be843bSPierre Pronchery */
4951*e7be843bSPierre Pronchery
4952*e7be843bSPierre Pronchery /*
4953*e7be843bSPierre Pronchery * SSL_new_domain
4954*e7be843bSPierre Pronchery * --------------
4955*e7be843bSPierre Pronchery */
ossl_quic_new_domain(SSL_CTX * ctx,uint64_t flags)4956*e7be843bSPierre Pronchery SSL *ossl_quic_new_domain(SSL_CTX *ctx, uint64_t flags)
4957*e7be843bSPierre Pronchery {
4958*e7be843bSPierre Pronchery QUIC_DOMAIN *qd = NULL;
4959*e7be843bSPierre Pronchery QUIC_ENGINE_ARGS engine_args = {0};
4960*e7be843bSPierre Pronchery uint64_t domain_flags;
4961*e7be843bSPierre Pronchery
4962*e7be843bSPierre Pronchery domain_flags = ctx->domain_flags;
4963*e7be843bSPierre Pronchery if ((flags & (SSL_DOMAIN_FLAG_SINGLE_THREAD
4964*e7be843bSPierre Pronchery | SSL_DOMAIN_FLAG_MULTI_THREAD
4965*e7be843bSPierre Pronchery | SSL_DOMAIN_FLAG_THREAD_ASSISTED)) != 0)
4966*e7be843bSPierre Pronchery domain_flags = flags;
4967*e7be843bSPierre Pronchery else
4968*e7be843bSPierre Pronchery domain_flags = ctx->domain_flags | flags;
4969*e7be843bSPierre Pronchery
4970*e7be843bSPierre Pronchery if (!ossl_adjust_domain_flags(domain_flags, &domain_flags))
4971*e7be843bSPierre Pronchery return NULL;
4972*e7be843bSPierre Pronchery
4973*e7be843bSPierre Pronchery if ((qd = OPENSSL_zalloc(sizeof(*qd))) == NULL) {
4974*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4975*e7be843bSPierre Pronchery return NULL;
4976*e7be843bSPierre Pronchery }
4977*e7be843bSPierre Pronchery
4978*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4979*e7be843bSPierre Pronchery if ((qd->mutex = ossl_crypto_mutex_new()) == NULL) {
4980*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_CRYPTO_LIB, NULL);
4981*e7be843bSPierre Pronchery goto err;
4982*e7be843bSPierre Pronchery }
4983*e7be843bSPierre Pronchery #endif
4984*e7be843bSPierre Pronchery
4985*e7be843bSPierre Pronchery engine_args.libctx = ctx->libctx;
4986*e7be843bSPierre Pronchery engine_args.propq = ctx->propq;
4987*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
4988*e7be843bSPierre Pronchery engine_args.mutex = qd->mutex;
4989*e7be843bSPierre Pronchery #endif
4990*e7be843bSPierre Pronchery
4991*e7be843bSPierre Pronchery if (need_notifier_for_domain_flags(domain_flags))
4992*e7be843bSPierre Pronchery engine_args.reactor_flags |= QUIC_REACTOR_FLAG_USE_NOTIFIER;
4993*e7be843bSPierre Pronchery
4994*e7be843bSPierre Pronchery if ((qd->engine = ossl_quic_engine_new(&engine_args)) == NULL) {
4995*e7be843bSPierre Pronchery QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
4996*e7be843bSPierre Pronchery goto err;
4997*e7be843bSPierre Pronchery }
4998*e7be843bSPierre Pronchery
4999*e7be843bSPierre Pronchery /* Initialise the QUIC_DOMAIN's object header. */
5000*e7be843bSPierre Pronchery if (!ossl_quic_obj_init(&qd->obj, ctx, SSL_TYPE_QUIC_DOMAIN, NULL,
5001*e7be843bSPierre Pronchery qd->engine, NULL))
5002*e7be843bSPierre Pronchery goto err;
5003*e7be843bSPierre Pronchery
5004*e7be843bSPierre Pronchery ossl_quic_obj_set_domain_flags(&qd->obj, domain_flags);
5005*e7be843bSPierre Pronchery return &qd->obj.ssl;
5006*e7be843bSPierre Pronchery
5007*e7be843bSPierre Pronchery err:
5008*e7be843bSPierre Pronchery ossl_quic_engine_free(qd->engine);
5009*e7be843bSPierre Pronchery #if defined(OPENSSL_THREADS)
5010*e7be843bSPierre Pronchery ossl_crypto_mutex_free(&qd->mutex);
5011*e7be843bSPierre Pronchery #endif
5012*e7be843bSPierre Pronchery OPENSSL_free(qd);
5013*e7be843bSPierre Pronchery return NULL;
5014*e7be843bSPierre Pronchery }
5015*e7be843bSPierre Pronchery
5016*e7be843bSPierre Pronchery /*
5017*e7be843bSPierre Pronchery * QUIC Front-End I/O API: SSL_CTX Management
5018*e7be843bSPierre Pronchery * ==========================================
5019*e7be843bSPierre Pronchery */
5020*e7be843bSPierre Pronchery
ossl_quic_ctx_ctrl(SSL_CTX * ctx,int cmd,long larg,void * parg)5021*e7be843bSPierre Pronchery long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
5022*e7be843bSPierre Pronchery {
5023*e7be843bSPierre Pronchery switch (cmd) {
5024*e7be843bSPierre Pronchery default:
5025*e7be843bSPierre Pronchery return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
5026*e7be843bSPierre Pronchery }
5027*e7be843bSPierre Pronchery }
5028*e7be843bSPierre Pronchery
ossl_quic_callback_ctrl(SSL * s,int cmd,void (* fp)(void))5029*e7be843bSPierre Pronchery long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
5030*e7be843bSPierre Pronchery {
5031*e7be843bSPierre Pronchery QCTX ctx;
5032*e7be843bSPierre Pronchery
5033*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
5034*e7be843bSPierre Pronchery return 0;
5035*e7be843bSPierre Pronchery
5036*e7be843bSPierre Pronchery switch (cmd) {
5037*e7be843bSPierre Pronchery case SSL_CTRL_SET_MSG_CALLBACK:
5038*e7be843bSPierre Pronchery ossl_quic_channel_set_msg_callback(ctx.qc->ch, (ossl_msg_cb)fp,
5039*e7be843bSPierre Pronchery &ctx.qc->obj.ssl);
5040*e7be843bSPierre Pronchery /* This callback also needs to be set on the internal SSL object */
5041*e7be843bSPierre Pronchery return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);;
5042*e7be843bSPierre Pronchery
5043*e7be843bSPierre Pronchery default:
5044*e7be843bSPierre Pronchery /* Probably a TLS related ctrl. Defer to our internal SSL object */
5045*e7be843bSPierre Pronchery return ssl3_callback_ctrl(ctx.qc->tls, cmd, fp);
5046*e7be843bSPierre Pronchery }
5047*e7be843bSPierre Pronchery }
5048*e7be843bSPierre Pronchery
ossl_quic_ctx_callback_ctrl(SSL_CTX * ctx,int cmd,void (* fp)(void))5049*e7be843bSPierre Pronchery long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
5050*e7be843bSPierre Pronchery {
5051*e7be843bSPierre Pronchery return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
5052*e7be843bSPierre Pronchery }
5053*e7be843bSPierre Pronchery
ossl_quic_renegotiate_check(SSL * ssl,int initok)5054*e7be843bSPierre Pronchery int ossl_quic_renegotiate_check(SSL *ssl, int initok)
5055*e7be843bSPierre Pronchery {
5056*e7be843bSPierre Pronchery /* We never do renegotiation. */
5057*e7be843bSPierre Pronchery return 0;
5058*e7be843bSPierre Pronchery }
5059*e7be843bSPierre Pronchery
ossl_quic_get_cipher_by_char(const unsigned char * p)5060*e7be843bSPierre Pronchery const SSL_CIPHER *ossl_quic_get_cipher_by_char(const unsigned char *p)
5061*e7be843bSPierre Pronchery {
5062*e7be843bSPierre Pronchery const SSL_CIPHER *ciph = ssl3_get_cipher_by_char(p);
5063*e7be843bSPierre Pronchery
5064*e7be843bSPierre Pronchery if ((ciph->algorithm2 & SSL_QUIC) == 0)
5065*e7be843bSPierre Pronchery return NULL;
5066*e7be843bSPierre Pronchery
5067*e7be843bSPierre Pronchery return ciph;
5068*e7be843bSPierre Pronchery }
5069*e7be843bSPierre Pronchery
5070*e7be843bSPierre Pronchery /*
5071*e7be843bSPierre Pronchery * These functions define the TLSv1.2 (and below) ciphers that are supported by
5072*e7be843bSPierre Pronchery * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
5073*e7be843bSPierre Pronchery */
5074*e7be843bSPierre Pronchery
ossl_quic_num_ciphers(void)5075*e7be843bSPierre Pronchery int ossl_quic_num_ciphers(void)
5076*e7be843bSPierre Pronchery {
5077*e7be843bSPierre Pronchery return 0;
5078*e7be843bSPierre Pronchery }
5079*e7be843bSPierre Pronchery
ossl_quic_get_cipher(unsigned int u)5080*e7be843bSPierre Pronchery const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
5081*e7be843bSPierre Pronchery {
5082*e7be843bSPierre Pronchery return NULL;
5083*e7be843bSPierre Pronchery }
5084*e7be843bSPierre Pronchery
5085*e7be843bSPierre Pronchery /*
5086*e7be843bSPierre Pronchery * SSL_get_shutdown()
5087*e7be843bSPierre Pronchery * ------------------
5088*e7be843bSPierre Pronchery */
ossl_quic_get_shutdown(const SSL * s)5089*e7be843bSPierre Pronchery int ossl_quic_get_shutdown(const SSL *s)
5090*e7be843bSPierre Pronchery {
5091*e7be843bSPierre Pronchery QCTX ctx;
5092*e7be843bSPierre Pronchery int shut = 0;
5093*e7be843bSPierre Pronchery
5094*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
5095*e7be843bSPierre Pronchery return 0;
5096*e7be843bSPierre Pronchery
5097*e7be843bSPierre Pronchery if (ossl_quic_channel_is_term_any(ctx.qc->ch)) {
5098*e7be843bSPierre Pronchery shut |= SSL_SENT_SHUTDOWN;
5099*e7be843bSPierre Pronchery if (!ossl_quic_channel_is_closing(ctx.qc->ch))
5100*e7be843bSPierre Pronchery shut |= SSL_RECEIVED_SHUTDOWN;
5101*e7be843bSPierre Pronchery }
5102*e7be843bSPierre Pronchery
5103*e7be843bSPierre Pronchery return shut;
5104*e7be843bSPierre Pronchery }
5105*e7be843bSPierre Pronchery
5106*e7be843bSPierre Pronchery /*
5107*e7be843bSPierre Pronchery * QUIC Polling Support APIs
5108*e7be843bSPierre Pronchery * =========================
5109*e7be843bSPierre Pronchery */
5110*e7be843bSPierre Pronchery
5111*e7be843bSPierre Pronchery /* Do we have the R (read) condition? */
5112*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_r(QUIC_XSO * xso)5113*e7be843bSPierre Pronchery static int test_poll_event_r(QUIC_XSO *xso)
5114*e7be843bSPierre Pronchery {
5115*e7be843bSPierre Pronchery int fin = 0;
5116*e7be843bSPierre Pronchery size_t avail = 0;
5117*e7be843bSPierre Pronchery
5118*e7be843bSPierre Pronchery /*
5119*e7be843bSPierre Pronchery * If a stream has had the fin bit set on the last packet
5120*e7be843bSPierre Pronchery * received, then we need to return a 1 here to raise
5121*e7be843bSPierre Pronchery * SSL_POLL_EVENT_R, so that the stream can have its completion
5122*e7be843bSPierre Pronchery * detected and closed gracefully by an application.
5123*e7be843bSPierre Pronchery * However, if the client reads the data via SSL_read[_ex], that api
5124*e7be843bSPierre Pronchery * provides no stream status, and as a result the stream state moves to
5125*e7be843bSPierre Pronchery * QUIC_RSTREAM_STATE_DATA_READ, and the receive buffer is freed, which
5126*e7be843bSPierre Pronchery * stored the fin state, so its not directly know-able here. Instead
5127*e7be843bSPierre Pronchery * check for the stream state being QUIC_RSTREAM_STATE_DATA_READ, which
5128*e7be843bSPierre Pronchery * is only set if the last stream frame received had the fin bit set, and
5129*e7be843bSPierre Pronchery * the client read the data. This catches our poll/read/poll case
5130*e7be843bSPierre Pronchery */
5131*e7be843bSPierre Pronchery if (xso->stream->recv_state == QUIC_RSTREAM_STATE_DATA_READ)
5132*e7be843bSPierre Pronchery return 1;
5133*e7be843bSPierre Pronchery
5134*e7be843bSPierre Pronchery return ossl_quic_stream_has_recv_buffer(xso->stream)
5135*e7be843bSPierre Pronchery && ossl_quic_rstream_available(xso->stream->rstream, &avail, &fin)
5136*e7be843bSPierre Pronchery && (avail > 0 || (fin && !xso->retired_fin));
5137*e7be843bSPierre Pronchery }
5138*e7be843bSPierre Pronchery
5139*e7be843bSPierre Pronchery /* Do we have the ER (exception: read) condition? */
5140*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_er(QUIC_XSO * xso)5141*e7be843bSPierre Pronchery static int test_poll_event_er(QUIC_XSO *xso)
5142*e7be843bSPierre Pronchery {
5143*e7be843bSPierre Pronchery return ossl_quic_stream_has_recv(xso->stream)
5144*e7be843bSPierre Pronchery && ossl_quic_stream_recv_is_reset(xso->stream)
5145*e7be843bSPierre Pronchery && !xso->retired_fin;
5146*e7be843bSPierre Pronchery }
5147*e7be843bSPierre Pronchery
5148*e7be843bSPierre Pronchery /* Do we have the W (write) condition? */
5149*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_w(QUIC_XSO * xso)5150*e7be843bSPierre Pronchery static int test_poll_event_w(QUIC_XSO *xso)
5151*e7be843bSPierre Pronchery {
5152*e7be843bSPierre Pronchery return !xso->conn->shutting_down
5153*e7be843bSPierre Pronchery && ossl_quic_stream_has_send_buffer(xso->stream)
5154*e7be843bSPierre Pronchery && ossl_quic_sstream_get_buffer_avail(xso->stream->sstream)
5155*e7be843bSPierre Pronchery && !ossl_quic_sstream_get_final_size(xso->stream->sstream, NULL)
5156*e7be843bSPierre Pronchery && ossl_quic_txfc_get_cwm(&xso->stream->txfc)
5157*e7be843bSPierre Pronchery > ossl_quic_sstream_get_cur_size(xso->stream->sstream)
5158*e7be843bSPierre Pronchery && quic_mutation_allowed(xso->conn, /*req_active=*/1);
5159*e7be843bSPierre Pronchery }
5160*e7be843bSPierre Pronchery
5161*e7be843bSPierre Pronchery /* Do we have the EW (exception: write) condition? */
5162*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_ew(QUIC_XSO * xso)5163*e7be843bSPierre Pronchery static int test_poll_event_ew(QUIC_XSO *xso)
5164*e7be843bSPierre Pronchery {
5165*e7be843bSPierre Pronchery return ossl_quic_stream_has_send(xso->stream)
5166*e7be843bSPierre Pronchery && xso->stream->peer_stop_sending
5167*e7be843bSPierre Pronchery && !xso->requested_reset
5168*e7be843bSPierre Pronchery && !xso->conn->shutting_down;
5169*e7be843bSPierre Pronchery }
5170*e7be843bSPierre Pronchery
5171*e7be843bSPierre Pronchery /* Do we have the EC (exception: connection) condition? */
5172*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_ec(QUIC_CONNECTION * qc)5173*e7be843bSPierre Pronchery static int test_poll_event_ec(QUIC_CONNECTION *qc)
5174*e7be843bSPierre Pronchery {
5175*e7be843bSPierre Pronchery return ossl_quic_channel_is_term_any(qc->ch);
5176*e7be843bSPierre Pronchery }
5177*e7be843bSPierre Pronchery
5178*e7be843bSPierre Pronchery /* Do we have the ECD (exception: connection drained) condition? */
5179*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_ecd(QUIC_CONNECTION * qc)5180*e7be843bSPierre Pronchery static int test_poll_event_ecd(QUIC_CONNECTION *qc)
5181*e7be843bSPierre Pronchery {
5182*e7be843bSPierre Pronchery return ossl_quic_channel_is_terminated(qc->ch);
5183*e7be843bSPierre Pronchery }
5184*e7be843bSPierre Pronchery
5185*e7be843bSPierre Pronchery /* Do we have the IS (incoming: stream) condition? */
5186*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_is(QUIC_CONNECTION * qc,int is_uni)5187*e7be843bSPierre Pronchery static int test_poll_event_is(QUIC_CONNECTION *qc, int is_uni)
5188*e7be843bSPierre Pronchery {
5189*e7be843bSPierre Pronchery return ossl_quic_stream_map_get_accept_queue_len(ossl_quic_channel_get_qsm(qc->ch),
5190*e7be843bSPierre Pronchery is_uni);
5191*e7be843bSPierre Pronchery }
5192*e7be843bSPierre Pronchery
5193*e7be843bSPierre Pronchery /* Do we have the OS (outgoing: stream) condition? */
5194*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_os(QUIC_CONNECTION * qc,int is_uni)5195*e7be843bSPierre Pronchery static int test_poll_event_os(QUIC_CONNECTION *qc, int is_uni)
5196*e7be843bSPierre Pronchery {
5197*e7be843bSPierre Pronchery /* Is it currently possible for us to make an outgoing stream? */
5198*e7be843bSPierre Pronchery return quic_mutation_allowed(qc, /*req_active=*/1)
5199*e7be843bSPierre Pronchery && ossl_quic_channel_get_local_stream_count_avail(qc->ch, is_uni) > 0;
5200*e7be843bSPierre Pronchery }
5201*e7be843bSPierre Pronchery
5202*e7be843bSPierre Pronchery /* Do we have the EL (exception: listener) condition? */
5203*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_el(QUIC_LISTENER * ql)5204*e7be843bSPierre Pronchery static int test_poll_event_el(QUIC_LISTENER *ql)
5205*e7be843bSPierre Pronchery {
5206*e7be843bSPierre Pronchery return !ossl_quic_port_is_running(ql->port);
5207*e7be843bSPierre Pronchery }
5208*e7be843bSPierre Pronchery
5209*e7be843bSPierre Pronchery /* Do we have the IC (incoming: connection) condition? */
5210*e7be843bSPierre Pronchery QUIC_NEEDS_LOCK
test_poll_event_ic(QUIC_LISTENER * ql)5211*e7be843bSPierre Pronchery static int test_poll_event_ic(QUIC_LISTENER *ql)
5212*e7be843bSPierre Pronchery {
5213*e7be843bSPierre Pronchery return ossl_quic_port_get_num_incoming_channels(ql->port) > 0;
5214*e7be843bSPierre Pronchery }
5215*e7be843bSPierre Pronchery
5216*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_conn_poll_events(SSL * ssl,uint64_t events,int do_tick,uint64_t * p_revents)5217*e7be843bSPierre Pronchery int ossl_quic_conn_poll_events(SSL *ssl, uint64_t events, int do_tick,
5218*e7be843bSPierre Pronchery uint64_t *p_revents)
5219*e7be843bSPierre Pronchery {
5220*e7be843bSPierre Pronchery QCTX ctx;
5221*e7be843bSPierre Pronchery uint64_t revents = 0;
5222*e7be843bSPierre Pronchery
5223*e7be843bSPierre Pronchery if (!expect_quic_csl(ssl, &ctx))
5224*e7be843bSPierre Pronchery return 0;
5225*e7be843bSPierre Pronchery
5226*e7be843bSPierre Pronchery qctx_lock(&ctx);
5227*e7be843bSPierre Pronchery
5228*e7be843bSPierre Pronchery if (ctx.qc != NULL && !ctx.qc->started) {
5229*e7be843bSPierre Pronchery /* We can only try to write on non-started connection. */
5230*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_W) != 0)
5231*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_W;
5232*e7be843bSPierre Pronchery goto end;
5233*e7be843bSPierre Pronchery }
5234*e7be843bSPierre Pronchery
5235*e7be843bSPierre Pronchery if (do_tick)
5236*e7be843bSPierre Pronchery ossl_quic_reactor_tick(ossl_quic_obj_get0_reactor(ctx.obj), 0);
5237*e7be843bSPierre Pronchery
5238*e7be843bSPierre Pronchery if (ctx.xso != NULL) {
5239*e7be843bSPierre Pronchery /* SSL object has a stream component. */
5240*e7be843bSPierre Pronchery
5241*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_R) != 0
5242*e7be843bSPierre Pronchery && test_poll_event_r(ctx.xso))
5243*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_R;
5244*e7be843bSPierre Pronchery
5245*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_ER) != 0
5246*e7be843bSPierre Pronchery && test_poll_event_er(ctx.xso))
5247*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_ER;
5248*e7be843bSPierre Pronchery
5249*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_W) != 0
5250*e7be843bSPierre Pronchery && test_poll_event_w(ctx.xso))
5251*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_W;
5252*e7be843bSPierre Pronchery
5253*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_EW) != 0
5254*e7be843bSPierre Pronchery && test_poll_event_ew(ctx.xso))
5255*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_EW;
5256*e7be843bSPierre Pronchery }
5257*e7be843bSPierre Pronchery
5258*e7be843bSPierre Pronchery if (ctx.qc != NULL && !ctx.is_stream) {
5259*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_EC) != 0
5260*e7be843bSPierre Pronchery && test_poll_event_ec(ctx.qc))
5261*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_EC;
5262*e7be843bSPierre Pronchery
5263*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_ECD) != 0
5264*e7be843bSPierre Pronchery && test_poll_event_ecd(ctx.qc))
5265*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_ECD;
5266*e7be843bSPierre Pronchery
5267*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_ISB) != 0
5268*e7be843bSPierre Pronchery && test_poll_event_is(ctx.qc, /*uni=*/0))
5269*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_ISB;
5270*e7be843bSPierre Pronchery
5271*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_ISU) != 0
5272*e7be843bSPierre Pronchery && test_poll_event_is(ctx.qc, /*uni=*/1))
5273*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_ISU;
5274*e7be843bSPierre Pronchery
5275*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_OSB) != 0
5276*e7be843bSPierre Pronchery && test_poll_event_os(ctx.qc, /*uni=*/0))
5277*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_OSB;
5278*e7be843bSPierre Pronchery
5279*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_OSU) != 0
5280*e7be843bSPierre Pronchery && test_poll_event_os(ctx.qc, /*uni=*/1))
5281*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_OSU;
5282*e7be843bSPierre Pronchery }
5283*e7be843bSPierre Pronchery
5284*e7be843bSPierre Pronchery if (ctx.is_listener) {
5285*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_EL) != 0
5286*e7be843bSPierre Pronchery && test_poll_event_el(ctx.ql))
5287*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_EL;
5288*e7be843bSPierre Pronchery
5289*e7be843bSPierre Pronchery if ((events & SSL_POLL_EVENT_IC) != 0
5290*e7be843bSPierre Pronchery && test_poll_event_ic(ctx.ql))
5291*e7be843bSPierre Pronchery revents |= SSL_POLL_EVENT_IC;
5292*e7be843bSPierre Pronchery }
5293*e7be843bSPierre Pronchery
5294*e7be843bSPierre Pronchery end:
5295*e7be843bSPierre Pronchery qctx_unlock(&ctx);
5296*e7be843bSPierre Pronchery *p_revents = revents;
5297*e7be843bSPierre Pronchery return 1;
5298*e7be843bSPierre Pronchery }
5299*e7be843bSPierre Pronchery
5300*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_get_notifier_fd(SSL * ssl)5301*e7be843bSPierre Pronchery int ossl_quic_get_notifier_fd(SSL *ssl)
5302*e7be843bSPierre Pronchery {
5303*e7be843bSPierre Pronchery QCTX ctx;
5304*e7be843bSPierre Pronchery QUIC_REACTOR *rtor;
5305*e7be843bSPierre Pronchery RIO_NOTIFIER *nfy;
5306*e7be843bSPierre Pronchery int nfd = -1;
5307*e7be843bSPierre Pronchery
5308*e7be843bSPierre Pronchery if (!expect_quic_any(ssl, &ctx))
5309*e7be843bSPierre Pronchery return -1;
5310*e7be843bSPierre Pronchery
5311*e7be843bSPierre Pronchery qctx_lock(&ctx);
5312*e7be843bSPierre Pronchery rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5313*e7be843bSPierre Pronchery nfy = ossl_quic_reactor_get0_notifier(rtor);
5314*e7be843bSPierre Pronchery if (nfy == NULL)
5315*e7be843bSPierre Pronchery goto end;
5316*e7be843bSPierre Pronchery nfd = ossl_rio_notifier_as_fd(nfy);
5317*e7be843bSPierre Pronchery
5318*e7be843bSPierre Pronchery end:
5319*e7be843bSPierre Pronchery qctx_unlock(&ctx);
5320*e7be843bSPierre Pronchery return nfd;
5321*e7be843bSPierre Pronchery }
5322*e7be843bSPierre Pronchery
5323*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_enter_blocking_section(SSL * ssl,QUIC_REACTOR_WAIT_CTX * wctx)5324*e7be843bSPierre Pronchery void ossl_quic_enter_blocking_section(SSL *ssl, QUIC_REACTOR_WAIT_CTX *wctx)
5325*e7be843bSPierre Pronchery {
5326*e7be843bSPierre Pronchery QCTX ctx;
5327*e7be843bSPierre Pronchery QUIC_REACTOR *rtor;
5328*e7be843bSPierre Pronchery
5329*e7be843bSPierre Pronchery if (!expect_quic_any(ssl, &ctx))
5330*e7be843bSPierre Pronchery return;
5331*e7be843bSPierre Pronchery
5332*e7be843bSPierre Pronchery qctx_lock(&ctx);
5333*e7be843bSPierre Pronchery rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5334*e7be843bSPierre Pronchery ossl_quic_reactor_wait_ctx_enter(wctx, rtor);
5335*e7be843bSPierre Pronchery qctx_unlock(&ctx);
5336*e7be843bSPierre Pronchery }
5337*e7be843bSPierre Pronchery
5338*e7be843bSPierre Pronchery QUIC_TAKES_LOCK
ossl_quic_leave_blocking_section(SSL * ssl,QUIC_REACTOR_WAIT_CTX * wctx)5339*e7be843bSPierre Pronchery void ossl_quic_leave_blocking_section(SSL *ssl, QUIC_REACTOR_WAIT_CTX *wctx)
5340*e7be843bSPierre Pronchery {
5341*e7be843bSPierre Pronchery QCTX ctx;
5342*e7be843bSPierre Pronchery QUIC_REACTOR *rtor;
5343*e7be843bSPierre Pronchery
5344*e7be843bSPierre Pronchery if (!expect_quic_any(ssl, &ctx))
5345*e7be843bSPierre Pronchery return;
5346*e7be843bSPierre Pronchery
5347*e7be843bSPierre Pronchery qctx_lock(&ctx);
5348*e7be843bSPierre Pronchery rtor = ossl_quic_obj_get0_reactor(ctx.obj);
5349*e7be843bSPierre Pronchery ossl_quic_reactor_wait_ctx_leave(wctx, rtor);
5350*e7be843bSPierre Pronchery qctx_unlock(&ctx);
5351*e7be843bSPierre Pronchery }
5352*e7be843bSPierre Pronchery
5353*e7be843bSPierre Pronchery /*
5354*e7be843bSPierre Pronchery * Internal Testing APIs
5355*e7be843bSPierre Pronchery * =====================
5356*e7be843bSPierre Pronchery */
5357*e7be843bSPierre Pronchery
ossl_quic_conn_get_channel(SSL * s)5358*e7be843bSPierre Pronchery QUIC_CHANNEL *ossl_quic_conn_get_channel(SSL *s)
5359*e7be843bSPierre Pronchery {
5360*e7be843bSPierre Pronchery QCTX ctx;
5361*e7be843bSPierre Pronchery
5362*e7be843bSPierre Pronchery if (!expect_quic_conn_only(s, &ctx))
5363*e7be843bSPierre Pronchery return NULL;
5364*e7be843bSPierre Pronchery
5365*e7be843bSPierre Pronchery return ctx.qc->ch;
5366*e7be843bSPierre Pronchery }
5367*e7be843bSPierre Pronchery
ossl_quic_set_diag_title(SSL_CTX * ctx,const char * title)5368*e7be843bSPierre Pronchery int ossl_quic_set_diag_title(SSL_CTX *ctx, const char *title)
5369*e7be843bSPierre Pronchery {
5370*e7be843bSPierre Pronchery #ifndef OPENSSL_NO_QLOG
5371*e7be843bSPierre Pronchery OPENSSL_free(ctx->qlog_title);
5372*e7be843bSPierre Pronchery ctx->qlog_title = NULL;
5373*e7be843bSPierre Pronchery
5374*e7be843bSPierre Pronchery if (title == NULL)
5375*e7be843bSPierre Pronchery return 1;
5376*e7be843bSPierre Pronchery
5377*e7be843bSPierre Pronchery if ((ctx->qlog_title = OPENSSL_strdup(title)) == NULL)
5378*e7be843bSPierre Pronchery return 0;
5379*e7be843bSPierre Pronchery #endif
5380*e7be843bSPierre Pronchery
5381*e7be843bSPierre Pronchery return 1;
5382*e7be843bSPierre Pronchery }
5383