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