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