]>
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 | |
cb5c208b | 20 | static void aon_write_finish(QUIC_XSO *xso); |
23c04709 | 21 | static int create_channel(QUIC_CONNECTION *qc); |
21c80696 HL |
22 | static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs); |
23 | static int qc_try_create_default_xso(QUIC_CONNECTION *qc, int remote_init); | |
22d53c88 HL |
24 | |
25 | /* | |
26 | * QUIC Front-End I/O API: Common Utilities | |
27 | * ======================================== | |
28 | */ | |
29 | ||
30 | /* | |
31 | * Block until a predicate is met. | |
32 | * | |
33 | * Precondition: Must have a channel. | |
d7b1fadd | 34 | * Precondition: Must hold channel lock (unchecked). |
22d53c88 | 35 | */ |
d7b1fadd | 36 | QUIC_NEEDS_LOCK |
22d53c88 HL |
37 | static int block_until_pred(QUIC_CONNECTION *qc, |
38 | int (*pred)(void *arg), void *pred_arg, | |
39 | uint32_t flags) | |
40 | { | |
41 | QUIC_REACTOR *rtor; | |
42 | ||
43 | assert(qc->ch != NULL); | |
44 | ||
45 | rtor = ossl_quic_channel_get_reactor(qc->ch); | |
c019e1ef | 46 | return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags, |
4847599b | 47 | qc->mutex); |
22d53c88 HL |
48 | } |
49 | ||
50 | /* | |
51 | * Raise a 'normal' error, meaning one that can be reported via SSL_get_error() | |
52 | * rather than via ERR. | |
53 | */ | |
54 | static int quic_raise_normal_error(QUIC_CONNECTION *qc, | |
55 | int err) | |
56 | { | |
57 | qc->last_error = err; | |
58 | return 0; | |
59 | } | |
60 | ||
61 | /* | |
62 | * Raise a 'non-normal' error, meaning any error that is not reported via | |
63 | * SSL_get_error() and must be reported via ERR. | |
e88cdb8e HL |
64 | * |
65 | * qc should be provided if available. In exceptional circumstances when qc is | |
66 | * not known NULL may be passed. This should generally only happen when an | |
67 | * expect_...() function defined below fails, which generally indicates a | |
68 | * dispatch error or caller error. | |
22d53c88 HL |
69 | */ |
70 | static int quic_raise_non_normal_error(QUIC_CONNECTION *qc, | |
71 | const char *file, | |
72 | int line, | |
73 | const char *func, | |
74 | int reason, | |
75 | const char *fmt, | |
76 | ...) | |
77 | { | |
78 | va_list args; | |
79 | ||
80 | ERR_new(); | |
81 | ERR_set_debug(file, line, func); | |
82 | ||
83 | va_start(args, fmt); | |
84 | ERR_vset_error(ERR_LIB_SSL, reason, fmt, args); | |
85 | va_end(args); | |
86 | ||
e88cdb8e HL |
87 | if (qc != NULL) |
88 | qc->last_error = SSL_ERROR_SSL; | |
89 | ||
22d53c88 HL |
90 | return 0; |
91 | } | |
92 | ||
93 | #define QUIC_RAISE_NORMAL_ERROR(qc, err) \ | |
94 | quic_raise_normal_error((qc), (err)) | |
95 | ||
96 | #define QUIC_RAISE_NON_NORMAL_ERROR(qc, reason, msg) \ | |
97 | quic_raise_non_normal_error((qc), \ | |
98 | OPENSSL_FILE, OPENSSL_LINE, \ | |
99 | OPENSSL_FUNC, \ | |
100 | (reason), \ | |
101 | (msg)) | |
102 | ||
103 | /* | |
e88cdb8e HL |
104 | * QCTX is a utility structure which provides information we commonly wish to |
105 | * unwrap upon an API call being dispatched to us, namely: | |
106 | * | |
107 | * - a pointer to the QUIC_CONNECTION (regardless of whether a QCSO or QSSO | |
108 | * was passed); | |
109 | * - a pointer to any applicable QUIC_XSO (e.g. if a QSSO was passed, or if | |
110 | * a QCSO with a default stream was passed); | |
111 | * - whether a QSSO was passed (xso == NULL must not be used to determine this | |
112 | * because it may be non-NULL when a QCSO is passed if that QCSO has a | |
113 | * default stream). | |
114 | */ | |
115 | typedef struct qctx_st { | |
116 | QUIC_CONNECTION *qc; | |
117 | QUIC_XSO *xso; | |
118 | int is_stream; | |
119 | } QCTX; | |
120 | ||
121 | /* | |
122 | * Given a QCSO or QSSO, initialises a QCTX, determining the contextually | |
123 | * applicable QUIC_CONNECTION pointer and, if applicable, QUIC_XSO pointer. | |
124 | * | |
125 | * After this returns 1, all fields of the passed QCTX are initialised. | |
126 | * Returns 0 on failure. This function is intended to be used to provide API | |
127 | * semantics and as such, it invokes QUIC_RAISE_NON_NORMAL_ERROR() on failure. | |
22d53c88 | 128 | */ |
e88cdb8e | 129 | static int expect_quic(const SSL *s, QCTX *ctx) |
22d53c88 | 130 | { |
e88cdb8e HL |
131 | QUIC_CONNECTION *qc; |
132 | QUIC_XSO *xso; | |
133 | ||
134 | ctx->qc = NULL; | |
135 | ctx->xso = NULL; | |
136 | ctx->is_stream = 0; | |
137 | ||
138 | if (s == NULL) | |
22d53c88 HL |
139 | return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL); |
140 | ||
e88cdb8e HL |
141 | switch (s->type) { |
142 | case SSL_TYPE_QUIC_CONNECTION: | |
143 | qc = (QUIC_CONNECTION *)s; | |
144 | ctx->qc = qc; | |
cb5c208b | 145 | ctx->xso = qc->default_xso; |
e88cdb8e HL |
146 | ctx->is_stream = 0; |
147 | return 1; | |
148 | ||
149 | case SSL_TYPE_QUIC_XSO: | |
150 | xso = (QUIC_XSO *)s; | |
cb5c208b | 151 | ctx->qc = xso->conn; |
e88cdb8e HL |
152 | ctx->xso = xso; |
153 | ctx->is_stream = 1; | |
154 | return 1; | |
155 | ||
156 | default: | |
157 | return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL); | |
158 | } | |
159 | } | |
160 | ||
161 | /* | |
162 | * Like expect_quic(), but requires a QUIC_XSO be contextually available. In | |
163 | * other words, requires that the passed QSO be a QSSO or a QCSO with a default | |
164 | * stream. | |
21c80696 HL |
165 | * |
166 | * remote_init determines if we expect the default XSO to be remotely created or | |
167 | * not. If it is -1, do not instantiate a default XSO if one does not yet exist. | |
e88cdb8e | 168 | */ |
21c80696 HL |
169 | static int ossl_unused expect_quic_with_stream(const SSL *s, int remote_init, |
170 | QCTX *ctx) | |
e88cdb8e HL |
171 | { |
172 | if (!expect_quic(s, ctx)) | |
173 | return 0; | |
174 | ||
21c80696 HL |
175 | if (ctx->xso == NULL && remote_init >= 0) { |
176 | qc_try_create_default_xso(ctx->qc, remote_init); | |
177 | ctx->xso = ctx->qc->default_xso; | |
178 | } | |
179 | ||
e88cdb8e HL |
180 | if (ctx->xso == NULL) |
181 | return QUIC_RAISE_NON_NORMAL_ERROR(ctx->qc, ERR_R_INTERNAL_ERROR, NULL); | |
182 | ||
22d53c88 | 183 | return 1; |
e88cdb8e | 184 | } |
22d53c88 | 185 | |
e88cdb8e HL |
186 | /* |
187 | * Like expect_quic(), but fails if called on a QUIC_XSO. ctx->xso may still | |
188 | * be non-NULL if the QCSO has a default stream. | |
189 | */ | |
23c04709 | 190 | static int ossl_unused expect_quic_conn_only(const SSL *s, QCTX *ctx) |
e88cdb8e HL |
191 | { |
192 | if (!expect_quic(s, ctx)) | |
193 | return 0; | |
194 | ||
195 | if (ctx->is_stream) | |
196 | return QUIC_RAISE_NON_NORMAL_ERROR(ctx->qc, ERR_R_INTERNAL_ERROR, NULL); | |
197 | ||
198 | return 1; | |
22d53c88 HL |
199 | } |
200 | ||
4847599b HL |
201 | /* |
202 | * Ensures that the channel mutex is held for a method which touches channel | |
203 | * state. | |
204 | * | |
205 | * Precondition: Channel mutex is not held (unchecked) | |
206 | */ | |
20f45743 | 207 | static void quic_lock(QUIC_CONNECTION *qc) |
4847599b | 208 | { |
ffce2946 | 209 | ossl_crypto_mutex_lock(qc->mutex); |
4847599b HL |
210 | } |
211 | ||
212 | /* Precondition: Channel mutex is held (unchecked) */ | |
213 | QUIC_NEEDS_LOCK | |
214 | static void quic_unlock(QUIC_CONNECTION *qc) | |
215 | { | |
ffce2946 | 216 | ossl_crypto_mutex_unlock(qc->mutex); |
4847599b HL |
217 | } |
218 | ||
219 | ||
22d53c88 HL |
220 | /* |
221 | * QUIC Front-End I/O API: Initialization | |
222 | * ====================================== | |
223 | * | |
224 | * SSL_new => ossl_quic_new | |
225 | * ossl_quic_init | |
226 | * SSL_reset => ossl_quic_reset | |
227 | * SSL_clear => ossl_quic_clear | |
228 | * ossl_quic_deinit | |
229 | * SSL_free => ossl_quic_free | |
230 | * | |
231 | */ | |
232 | ||
233 | /* SSL_new */ | |
38b051a1 TM |
234 | SSL *ossl_quic_new(SSL_CTX *ctx) |
235 | { | |
22d53c88 HL |
236 | QUIC_CONNECTION *qc = NULL; |
237 | SSL *ssl_base = NULL; | |
a7f41885 | 238 | SSL_CONNECTION *sc = NULL; |
38b051a1 TM |
239 | |
240 | qc = OPENSSL_zalloc(sizeof(*qc)); | |
241 | if (qc == NULL) | |
242 | goto err; | |
243 | ||
22d53c88 HL |
244 | /* Initialise the QUIC_CONNECTION's stub header. */ |
245 | ssl_base = &qc->ssl; | |
a7f41885 | 246 | if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) { |
22d53c88 | 247 | ssl_base = NULL; |
38b051a1 TM |
248 | goto err; |
249 | } | |
38b051a1 | 250 | |
4e3a55fd | 251 | qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method()); |
a7f41885 MC |
252 | if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) |
253 | goto err; | |
a7f41885 | 254 | |
ffce2946 | 255 | if ((qc->mutex = ossl_crypto_mutex_new()) == NULL) |
4847599b HL |
256 | goto err; |
257 | ||
f2f7c4f1 HL |
258 | qc->is_thread_assisted |
259 | = (ssl_base->method == OSSL_QUIC_client_thread_method()); | |
260 | ||
dfb9ae14 HL |
261 | qc->as_server = 0; /* TODO(QUIC): server support */ |
262 | qc->as_server_state = qc->as_server; | |
263 | ||
21c80696 HL |
264 | qc->default_ssl_mode = qc->ssl.ctx->mode; |
265 | qc->default_blocking = 1; | |
266 | qc->last_error = SSL_ERROR_NONE; | |
a7f41885 | 267 | |
23c04709 HL |
268 | if (!create_channel(qc)) |
269 | goto err; | |
270 | ||
21c80696 HL |
271 | /* |
272 | * We do not create the default XSO yet. The reason for this is that the | |
273 | * stream ID of the default XSO will depend on whether the stream is client | |
274 | * or server-initiated, which depends on who transmits first. Since we do | |
275 | * not know whether the application will be using a client-transmits-first | |
276 | * or server-transmits-first protocol, we defer default XSO creation until | |
277 | * the client calls SSL_read() or SSL_write(). If it calls SSL_read() first, | |
278 | * we take that as a cue that the client is expecting a server-initiated | |
279 | * stream, and vice versa if SSL_write() is called first. | |
280 | */ | |
22d53c88 HL |
281 | return ssl_base; |
282 | ||
38b051a1 | 283 | err: |
2dbc39de HL |
284 | if (qc != NULL) { |
285 | ossl_quic_channel_free(qc->ch); | |
286 | SSL_free(qc->tls); | |
287 | } | |
22d53c88 | 288 | OPENSSL_free(qc); |
38b051a1 TM |
289 | return NULL; |
290 | } | |
291 | ||
22d53c88 | 292 | /* SSL_free */ |
a8489257 | 293 | QUIC_TAKES_LOCK |
22d53c88 HL |
294 | void ossl_quic_free(SSL *s) |
295 | { | |
072328dd | 296 | QCTX ctx; |
22d53c88 | 297 | |
072328dd HL |
298 | /* We should never be called on anything but a QSO. */ |
299 | if (!expect_quic(s, &ctx)) | |
22d53c88 HL |
300 | return; |
301 | ||
2dbc39de HL |
302 | if (ctx.is_stream) { |
303 | /* | |
304 | * When a QSSO is freed, the XSO is freed immediately, because the XSO | |
305 | * itself only contains API personality layer data. However the | |
306 | * underlying QUIC_STREAM is not freed immediately but is instead marked | |
307 | * as deleted for later collection. | |
308 | */ | |
309 | ||
310 | quic_lock(ctx.qc); | |
311 | ||
312 | assert(ctx.qc->num_xso > 0); | |
313 | --ctx.qc->num_xso; | |
314 | ||
315 | ctx.xso->stream->deleted = 1; | |
316 | ||
f20fdd16 HL |
317 | /* Auto-conclude stream. */ |
318 | /* TODO(QUIC): Do RESET_STREAM here instead of auto-conclude */ | |
319 | if (ctx.xso->stream->sstream != NULL) | |
320 | ossl_quic_sstream_fin(ctx.xso->stream->sstream); | |
321 | ||
322 | /* Update stream state. */ | |
323 | ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(ctx.xso->conn->ch), | |
324 | ctx.xso->stream); | |
325 | ||
2dbc39de HL |
326 | quic_unlock(ctx.qc); |
327 | ||
328 | /* Note: SSL_free calls OPENSSL_free(xso) for us */ | |
329 | return; | |
330 | } | |
331 | ||
072328dd | 332 | quic_lock(ctx.qc); |
f2f7c4f1 | 333 | |
2dbc39de HL |
334 | /* |
335 | * Free the default XSO, if any. The QUIC_STREAM is not deleted at this | |
336 | * stage, but is freed during the channel free when the whole QSM is freed. | |
337 | */ | |
21c80696 HL |
338 | if (ctx.qc->default_xso != NULL) { |
339 | QUIC_XSO *xso = ctx.qc->default_xso; | |
340 | ||
341 | quic_unlock(ctx.qc); | |
342 | SSL_free(&xso->ssl); | |
343 | quic_lock(ctx.qc); | |
344 | } | |
2dbc39de HL |
345 | |
346 | /* Ensure we have no remaining XSOs. */ | |
347 | assert(ctx.qc->num_xso == 0); | |
348 | ||
072328dd HL |
349 | if (ctx.qc->is_thread_assisted && ctx.qc->started) { |
350 | ossl_quic_thread_assist_wait_stopped(&ctx.qc->thread_assist); | |
351 | ossl_quic_thread_assist_cleanup(&ctx.qc->thread_assist); | |
dbe7b51a | 352 | } |
f2f7c4f1 | 353 | |
072328dd | 354 | ossl_quic_channel_free(ctx.qc->ch); |
22d53c88 | 355 | |
072328dd HL |
356 | BIO_free(ctx.qc->net_rbio); |
357 | BIO_free(ctx.qc->net_wbio); | |
d1ac77b1 | 358 | |
22d53c88 | 359 | /* Note: SSL_free calls OPENSSL_free(qc) for us */ |
a7f41885 | 360 | |
072328dd HL |
361 | SSL_free(ctx.qc->tls); |
362 | ossl_crypto_mutex_free(&ctx.qc->mutex); /* freed while still locked */ | |
22d53c88 HL |
363 | } |
364 | ||
365 | /* SSL method init */ | |
38b051a1 | 366 | int ossl_quic_init(SSL *s) |
99e1cc7b | 367 | { |
22d53c88 HL |
368 | /* Same op as SSL_clear, forward the call. */ |
369 | return ossl_quic_clear(s); | |
99e1cc7b TM |
370 | } |
371 | ||
22d53c88 | 372 | /* SSL method deinit */ |
38b051a1 TM |
373 | void ossl_quic_deinit(SSL *s) |
374 | { | |
22d53c88 | 375 | /* No-op. */ |
38b051a1 TM |
376 | } |
377 | ||
22d53c88 HL |
378 | /* SSL_reset */ |
379 | int ossl_quic_reset(SSL *s) | |
380 | { | |
072328dd | 381 | QCTX ctx; |
22d53c88 | 382 | |
072328dd | 383 | if (!expect_quic(s, &ctx)) |
22d53c88 HL |
384 | return 0; |
385 | ||
c8b3fdc2 | 386 | /* TODO(QUIC); Currently a no-op. */ |
22d53c88 HL |
387 | return 1; |
388 | } | |
389 | ||
390 | /* SSL_clear */ | |
391 | int ossl_quic_clear(SSL *s) | |
99e1cc7b | 392 | { |
072328dd | 393 | QCTX ctx; |
38b051a1 | 394 | |
072328dd | 395 | if (!expect_quic(s, &ctx)) |
22d53c88 HL |
396 | return 0; |
397 | ||
c8b3fdc2 | 398 | /* TODO(QUIC): Currently a no-op. */ |
22d53c88 HL |
399 | return 1; |
400 | } | |
38b051a1 | 401 | |
b212d554 HL |
402 | void ossl_quic_conn_set_override_now_cb(SSL *s, |
403 | OSSL_TIME (*now_cb)(void *arg), | |
404 | void *now_cb_arg) | |
405 | { | |
072328dd | 406 | QCTX ctx; |
b212d554 | 407 | |
072328dd HL |
408 | if (!expect_quic(s, &ctx)) |
409 | return; | |
410 | ||
411 | ctx.qc->override_now_cb = now_cb; | |
412 | ctx.qc->override_now_cb_arg = now_cb_arg; | |
b212d554 HL |
413 | } |
414 | ||
3b1ab5a3 HL |
415 | void ossl_quic_conn_force_assist_thread_wake(SSL *s) |
416 | { | |
072328dd HL |
417 | QCTX ctx; |
418 | ||
419 | if (!expect_quic(s, &ctx)) | |
420 | return; | |
3b1ab5a3 | 421 | |
072328dd HL |
422 | if (ctx.qc->is_thread_assisted && ctx.qc->started) |
423 | ossl_quic_thread_assist_notify_deadline_changed(&ctx.qc->thread_assist); | |
3b1ab5a3 HL |
424 | } |
425 | ||
22d53c88 HL |
426 | /* |
427 | * QUIC Front-End I/O API: Network BIO Configuration | |
428 | * ================================================= | |
429 | * | |
430 | * Handling the different BIOs is difficult: | |
431 | * | |
432 | * - It is more or less a requirement that we use non-blocking network I/O; | |
433 | * we need to be able to have timeouts on recv() calls, and make best effort | |
434 | * (non blocking) send() and recv() calls. | |
435 | * | |
436 | * The only sensible way to do this is to configure the socket into | |
437 | * non-blocking mode. We could try to do select() before calling send() or | |
438 | * recv() to get a guarantee that the call will not block, but this will | |
439 | * probably run into issues with buggy OSes which generate spurious socket | |
440 | * readiness events. In any case, relying on this to work reliably does not | |
441 | * seem sane. | |
442 | * | |
443 | * Timeouts could be handled via setsockopt() socket timeout options, but | |
444 | * this depends on OS support and adds another syscall to every network I/O | |
445 | * operation. It also has obvious thread safety concerns if we want to move | |
446 | * to concurrent use of a single socket at some later date. | |
447 | * | |
448 | * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to | |
449 | * be made non-blocking. However some OSes (e.g. Windows) do not support | |
450 | * this, so we cannot rely on this. | |
451 | * | |
452 | * As such, we need to configure any FD in non-blocking mode. This may | |
453 | * confound users who pass a blocking socket to libssl. However, in practice | |
454 | * it would be extremely strange for a user of QUIC to pass an FD to us, | |
455 | * then also try and send receive traffic on the same socket(!). Thus the | |
456 | * impact of this should be limited, and can be documented. | |
457 | * | |
458 | * - We support both blocking and non-blocking operation in terms of the API | |
459 | * presented to the user. One prospect is to set the blocking mode based on | |
460 | * whether the socket passed to us was already in blocking mode. However, | |
461 | * Windows has no API for determining if a socket is in blocking mode (!), | |
462 | * therefore this cannot be done portably. Currently therefore we expose an | |
463 | * explicit API call to set this, and default to blocking mode. | |
464 | * | |
465 | * - We need to determine our initial destination UDP address. The "natural" | |
466 | * way for a user to do this is to set the peer variable on a BIO_dgram. | |
467 | * However, this has problems because BIO_dgram's peer variable is used for | |
468 | * both transmission and reception. This means it can be constantly being | |
469 | * changed to a malicious value (e.g. if some random unrelated entity on the | |
470 | * network starts sending traffic to us) on every read call. This is not a | |
471 | * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg | |
472 | * calls only, which do not use this variable. However, we do need to let | |
473 | * the user specify the peer in a 'normal' manner. The compromise here is | |
474 | * that we grab the current peer value set at the time the write BIO is set | |
475 | * and do not read the value again. | |
476 | * | |
477 | * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs. | |
478 | * Currently we do this by only supporting non-blocking mode. | |
479 | * | |
480 | */ | |
481 | ||
482 | /* | |
483 | * Determines what initial destination UDP address we should use, if possible. | |
484 | * If this fails the client must set the destination address manually, or use a | |
485 | * BIO which does not need a destination address. | |
486 | */ | |
487 | static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer) | |
488 | { | |
75b2920a | 489 | if (BIO_dgram_get_peer(net_wbio, peer) <= 0) |
22d53c88 HL |
490 | return 0; |
491 | ||
492 | return 1; | |
493 | } | |
494 | ||
072328dd | 495 | void ossl_quic_conn_set0_net_rbio(SSL *s, BIO *net_rbio) |
22d53c88 | 496 | { |
072328dd HL |
497 | QCTX ctx; |
498 | ||
499 | if (!expect_quic(s, &ctx)) | |
500 | return; | |
501 | ||
502 | if (ctx.qc->net_rbio == net_rbio) | |
38b051a1 | 503 | return; |
38b051a1 | 504 | |
23c04709 | 505 | if (!ossl_quic_channel_set_net_rbio(ctx.qc->ch, net_rbio)) |
22d53c88 HL |
506 | return; |
507 | ||
072328dd HL |
508 | BIO_free(ctx.qc->net_rbio); |
509 | ctx.qc->net_rbio = net_rbio; | |
22d53c88 HL |
510 | |
511 | /* | |
512 | * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking | |
513 | * mode as we do not support it for non-pollable BIOs. | |
514 | */ | |
515 | if (net_rbio != NULL) { | |
516 | BIO_POLL_DESCRIPTOR d = {0}; | |
517 | ||
518 | if (!BIO_get_rpoll_descriptor(net_rbio, &d) | |
519 | || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) { | |
cb5c208b HL |
520 | ctx.qc->blocking = 0; |
521 | ctx.qc->default_blocking = 0; | |
072328dd | 522 | ctx.qc->can_poll_net_rbio = 0; |
22d53c88 | 523 | } else { |
072328dd | 524 | ctx.qc->can_poll_net_rbio = 1; |
22d53c88 HL |
525 | } |
526 | } | |
99e1cc7b TM |
527 | } |
528 | ||
072328dd | 529 | void ossl_quic_conn_set0_net_wbio(SSL *s, BIO *net_wbio) |
38b051a1 | 530 | { |
072328dd HL |
531 | QCTX ctx; |
532 | ||
533 | if (!expect_quic(s, &ctx)) | |
22d53c88 HL |
534 | return; |
535 | ||
072328dd | 536 | if (ctx.qc->net_wbio == net_wbio) |
22d53c88 HL |
537 | return; |
538 | ||
23c04709 | 539 | if (!ossl_quic_channel_set_net_wbio(ctx.qc->ch, net_wbio)) |
072328dd HL |
540 | return; |
541 | ||
542 | BIO_free(ctx.qc->net_wbio); | |
543 | ctx.qc->net_wbio = net_wbio; | |
22d53c88 HL |
544 | |
545 | if (net_wbio != NULL) { | |
546 | BIO_POLL_DESCRIPTOR d = {0}; | |
38b051a1 | 547 | |
22d53c88 HL |
548 | if (!BIO_get_wpoll_descriptor(net_wbio, &d) |
549 | || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) { | |
cb5c208b HL |
550 | ctx.qc->blocking = 0; |
551 | ctx.qc->default_blocking = 0; | |
072328dd | 552 | ctx.qc->can_poll_net_wbio = 0; |
22d53c88 | 553 | } else { |
072328dd | 554 | ctx.qc->can_poll_net_wbio = 1; |
22d53c88 | 555 | } |
38b051a1 | 556 | |
22d53c88 HL |
557 | /* |
558 | * If we do not have a peer address yet, and we have not started trying | |
559 | * to connect yet, try to autodetect one. | |
560 | */ | |
072328dd HL |
561 | if (BIO_ADDR_family(&ctx.qc->init_peer_addr) == AF_UNSPEC |
562 | && !ctx.qc->started) { | |
563 | if (!csm_analyse_init_peer_addr(net_wbio, &ctx.qc->init_peer_addr)) | |
22d53c88 | 564 | /* best effort */ |
072328dd | 565 | BIO_ADDR_clear(&ctx.qc->init_peer_addr); |
22d53c88 | 566 | |
23c04709 HL |
567 | ossl_quic_channel_set_peer_addr(ctx.qc->ch, |
568 | &ctx.qc->init_peer_addr); | |
22d53c88 | 569 | } |
38b051a1 | 570 | } |
22d53c88 | 571 | } |
38b051a1 | 572 | |
072328dd | 573 | BIO *ossl_quic_conn_get_net_rbio(const SSL *s) |
22d53c88 | 574 | { |
072328dd HL |
575 | QCTX ctx; |
576 | ||
577 | if (!expect_quic(s, &ctx)) | |
578 | return NULL; | |
579 | ||
580 | return ctx.qc->net_rbio; | |
38b051a1 TM |
581 | } |
582 | ||
072328dd | 583 | BIO *ossl_quic_conn_get_net_wbio(const SSL *s) |
22d53c88 | 584 | { |
072328dd HL |
585 | QCTX ctx; |
586 | ||
587 | if (!expect_quic(s, &ctx)) | |
588 | return NULL; | |
589 | ||
590 | return ctx.qc->net_wbio; | |
22d53c88 HL |
591 | } |
592 | ||
072328dd | 593 | int ossl_quic_conn_get_blocking_mode(const SSL *s) |
99e1cc7b | 594 | { |
072328dd HL |
595 | QCTX ctx; |
596 | ||
597 | if (!expect_quic(s, &ctx)) | |
598 | return 0; | |
599 | ||
cb5c208b HL |
600 | if (ctx.is_stream) |
601 | return ctx.xso->blocking; | |
602 | ||
072328dd | 603 | return ctx.qc->blocking; |
22d53c88 HL |
604 | } |
605 | ||
072328dd | 606 | int ossl_quic_conn_set_blocking_mode(SSL *s, int blocking) |
22d53c88 | 607 | { |
072328dd HL |
608 | QCTX ctx; |
609 | ||
610 | if (!expect_quic(s, &ctx)) | |
611 | return 0; | |
612 | ||
22d53c88 HL |
613 | /* Cannot enable blocking mode if we do not have pollable FDs. */ |
614 | if (blocking != 0 && | |
072328dd HL |
615 | (!ctx.qc->can_poll_net_rbio || !ctx.qc->can_poll_net_wbio)) |
616 | return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_UNSUPPORTED, NULL); | |
22d53c88 | 617 | |
cb5c208b HL |
618 | if (!ctx.is_stream) { |
619 | /* | |
620 | * If called on a QCSO, update default and connection-level blocking | |
621 | * modes. | |
622 | */ | |
623 | ctx.qc->blocking = (blocking != 0); | |
624 | ctx.qc->default_blocking = ctx.qc->blocking; | |
625 | } | |
626 | ||
627 | if (ctx.xso != NULL) | |
628 | /* | |
629 | * If called on a QSSO or QCSO with a default XSO, update blocking | |
630 | * mode. | |
631 | */ | |
632 | ctx.xso->blocking = (blocking != 0); | |
633 | ||
99e1cc7b TM |
634 | return 1; |
635 | } | |
636 | ||
072328dd | 637 | int ossl_quic_conn_set_initial_peer_addr(SSL *s, |
22d53c88 | 638 | const BIO_ADDR *peer_addr) |
99e1cc7b | 639 | { |
072328dd HL |
640 | QCTX ctx; |
641 | ||
642 | if (!expect_quic(s, &ctx)) | |
643 | return 0; | |
644 | ||
645 | if (ctx.qc->started) | |
646 | return QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, | |
22d53c88 | 647 | NULL); |
38b051a1 | 648 | |
22d53c88 | 649 | if (peer_addr == NULL) { |
072328dd | 650 | BIO_ADDR_clear(&ctx.qc->init_peer_addr); |
22d53c88 HL |
651 | return 1; |
652 | } | |
38b051a1 | 653 | |
072328dd | 654 | ctx.qc->init_peer_addr = *peer_addr; |
99e1cc7b TM |
655 | return 1; |
656 | } | |
657 | ||
22d53c88 HL |
658 | /* |
659 | * QUIC Front-End I/O API: Asynchronous I/O Management | |
660 | * =================================================== | |
661 | * | |
662 | * (BIO/)SSL_tick => ossl_quic_tick | |
663 | * (BIO/)SSL_get_tick_timeout => ossl_quic_get_tick_timeout | |
664 | * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd | |
665 | * | |
666 | */ | |
667 | ||
668 | /* Returns 1 if the connection is being used in blocking mode. */ | |
cb5c208b | 669 | static int qc_blocking_mode(const QUIC_CONNECTION *qc) |
99e1cc7b | 670 | { |
22d53c88 HL |
671 | return qc->blocking; |
672 | } | |
38b051a1 | 673 | |
cb5c208b HL |
674 | static int xso_blocking_mode(const QUIC_XSO *xso) |
675 | { | |
676 | return xso->blocking | |
677 | && xso->conn->can_poll_net_rbio | |
678 | && xso->conn->can_poll_net_wbio; | |
679 | } | |
680 | ||
22d53c88 | 681 | /* SSL_tick; ticks the reactor. */ |
a8489257 | 682 | QUIC_TAKES_LOCK |
072328dd | 683 | int ossl_quic_tick(SSL *s) |
22d53c88 | 684 | { |
072328dd HL |
685 | QCTX ctx; |
686 | ||
687 | if (!expect_quic(s, &ctx)) | |
688 | return 0; | |
689 | ||
690 | quic_lock(ctx.qc); | |
072328dd HL |
691 | ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0); |
692 | quic_unlock(ctx.qc); | |
99e1cc7b TM |
693 | return 1; |
694 | } | |
695 | ||
22d53c88 HL |
696 | /* |
697 | * SSL_get_tick_timeout. Get the time in milliseconds until the SSL object | |
698 | * should be ticked by the application by calling SSL_tick(). tv is set to 0 if | |
699 | * the object should be ticked immediately and tv->tv_sec is set to -1 if no | |
700 | * timeout is currently active. | |
701 | */ | |
a8489257 | 702 | QUIC_TAKES_LOCK |
072328dd | 703 | int ossl_quic_get_tick_timeout(SSL *s, struct timeval *tv) |
99e1cc7b | 704 | { |
072328dd | 705 | QCTX ctx; |
a1660c94 | 706 | OSSL_TIME deadline = ossl_time_infinite(); |
e44795bd | 707 | |
072328dd HL |
708 | if (!expect_quic(s, &ctx)) |
709 | return 0; | |
a8489257 | 710 | |
072328dd HL |
711 | quic_lock(ctx.qc); |
712 | ||
23c04709 HL |
713 | deadline |
714 | = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(ctx.qc->ch)); | |
22d53c88 HL |
715 | |
716 | if (ossl_time_is_infinite(deadline)) { | |
717 | tv->tv_sec = -1; | |
718 | tv->tv_usec = 0; | |
072328dd | 719 | quic_unlock(ctx.qc); |
22d53c88 HL |
720 | return 1; |
721 | } | |
722 | ||
a1660c94 | 723 | *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, ossl_time_now())); |
072328dd | 724 | quic_unlock(ctx.qc); |
22d53c88 HL |
725 | return 1; |
726 | } | |
727 | ||
728 | /* SSL_get_rpoll_descriptor */ | |
072328dd | 729 | int ossl_quic_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc) |
22d53c88 | 730 | { |
072328dd HL |
731 | QCTX ctx; |
732 | ||
733 | if (!expect_quic(s, &ctx)) | |
e44795bd TM |
734 | return 0; |
735 | ||
072328dd HL |
736 | if (desc == NULL || ctx.qc->net_rbio == NULL) |
737 | return 0; | |
738 | ||
739 | return BIO_get_rpoll_descriptor(ctx.qc->net_rbio, desc); | |
99e1cc7b TM |
740 | } |
741 | ||
22d53c88 | 742 | /* SSL_get_wpoll_descriptor */ |
072328dd | 743 | int ossl_quic_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc) |
99e1cc7b | 744 | { |
072328dd HL |
745 | QCTX ctx; |
746 | ||
747 | if (!expect_quic(s, &ctx)) | |
748 | return 0; | |
749 | ||
750 | if (desc == NULL || ctx.qc->net_wbio == NULL) | |
22d53c88 HL |
751 | return 0; |
752 | ||
072328dd | 753 | return BIO_get_wpoll_descriptor(ctx.qc->net_wbio, desc); |
99e1cc7b TM |
754 | } |
755 | ||
b639475a | 756 | /* SSL_net_read_desired */ |
a8489257 | 757 | QUIC_TAKES_LOCK |
072328dd | 758 | int ossl_quic_get_net_read_desired(SSL *s) |
99e1cc7b | 759 | { |
072328dd | 760 | QCTX ctx; |
a8489257 HL |
761 | int ret; |
762 | ||
072328dd HL |
763 | if (!expect_quic(s, &ctx)) |
764 | return 0; | |
765 | ||
766 | quic_lock(ctx.qc); | |
072328dd HL |
767 | ret = ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(ctx.qc->ch)); |
768 | quic_unlock(ctx.qc); | |
a8489257 | 769 | return ret; |
22d53c88 | 770 | } |
e44795bd | 771 | |
b639475a | 772 | /* SSL_net_write_desired */ |
a8489257 | 773 | QUIC_TAKES_LOCK |
072328dd | 774 | int ossl_quic_get_net_write_desired(SSL *s) |
22d53c88 | 775 | { |
a8489257 | 776 | int ret; |
072328dd HL |
777 | QCTX ctx; |
778 | ||
779 | if (!expect_quic(s, &ctx)) | |
780 | return 0; | |
a8489257 | 781 | |
072328dd | 782 | quic_lock(ctx.qc); |
072328dd HL |
783 | ret = ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(ctx.qc->ch)); |
784 | quic_unlock(ctx.qc); | |
a8489257 | 785 | return ret; |
99e1cc7b TM |
786 | } |
787 | ||
22d53c88 HL |
788 | /* |
789 | * QUIC Front-End I/O API: Connection Lifecycle Operations | |
790 | * ======================================================= | |
791 | * | |
792 | * SSL_do_handshake => ossl_quic_do_handshake | |
793 | * SSL_set_connect_state => ossl_quic_set_connect_state | |
794 | * SSL_set_accept_state => ossl_quic_set_accept_state | |
795 | * SSL_shutdown => ossl_quic_shutdown | |
796 | * SSL_ctrl => ossl_quic_ctrl | |
797 | * (BIO/)SSL_connect => ossl_quic_connect | |
798 | * (BIO/)SSL_accept => ossl_quic_accept | |
799 | * | |
800 | */ | |
801 | ||
802 | /* SSL_shutdown */ | |
e8043229 | 803 | static int quic_shutdown_wait(void *arg) |
99e1cc7b | 804 | { |
e8043229 | 805 | QUIC_CONNECTION *qc = arg; |
22d53c88 | 806 | |
23c04709 | 807 | return ossl_quic_channel_is_terminated(qc->ch); |
e8043229 | 808 | } |
22d53c88 | 809 | |
a8489257 | 810 | QUIC_TAKES_LOCK |
072328dd | 811 | int ossl_quic_conn_shutdown(SSL *s, uint64_t flags, |
e8043229 HL |
812 | const SSL_SHUTDOWN_EX_ARGS *args, |
813 | size_t args_len) | |
814 | { | |
a8489257 | 815 | int ret; |
072328dd | 816 | QCTX ctx; |
a8489257 | 817 | |
072328dd HL |
818 | if (!expect_quic(s, &ctx)) |
819 | return 0; | |
820 | ||
cb5c208b HL |
821 | if (ctx.is_stream) |
822 | /* TODO(QUIC): Semantics currently undefined for QSSOs */ | |
823 | return -1; | |
824 | ||
072328dd | 825 | quic_lock(ctx.qc); |
a8489257 | 826 | |
072328dd | 827 | ossl_quic_channel_local_close(ctx.qc->ch, |
e8043229 HL |
828 | args != NULL ? args->quic_error_code : 0); |
829 | ||
1d40b151 | 830 | /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */ |
e8043229 | 831 | |
072328dd HL |
832 | if (ossl_quic_channel_is_terminated(ctx.qc->ch)) { |
833 | quic_unlock(ctx.qc); | |
e8043229 | 834 | return 1; |
a8489257 | 835 | } |
e8043229 | 836 | |
cb5c208b | 837 | if (qc_blocking_mode(ctx.qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0) |
072328dd | 838 | block_until_pred(ctx.qc, quic_shutdown_wait, ctx.qc, 0); |
e8043229 | 839 | else |
072328dd | 840 | ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0); |
e8043229 | 841 | |
072328dd HL |
842 | ret = ossl_quic_channel_is_terminated(ctx.qc->ch); |
843 | quic_unlock(ctx.qc); | |
a8489257 | 844 | return ret; |
99e1cc7b TM |
845 | } |
846 | ||
22d53c88 | 847 | /* SSL_ctrl */ |
e44795bd | 848 | long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg) |
99e1cc7b | 849 | { |
072328dd | 850 | QCTX ctx; |
38b051a1 | 851 | |
072328dd | 852 | if (!expect_quic(s, &ctx)) |
38b051a1 TM |
853 | return 0; |
854 | ||
22d53c88 HL |
855 | switch (cmd) { |
856 | case SSL_CTRL_MODE: | |
cb5c208b HL |
857 | /* If called on a QCSO, update the default mode. */ |
858 | if (!ctx.is_stream) | |
859 | ctx.qc->default_ssl_mode |= (uint32_t)larg; | |
860 | ||
861 | /* | |
862 | * If we were called on a QSSO or have a default stream, we also update | |
863 | * that. | |
864 | */ | |
865 | if (ctx.xso != NULL) { | |
866 | /* Cannot enable EPW while AON write in progress. */ | |
867 | if (ctx.xso->aon_write_in_progress) | |
868 | larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE; | |
869 | ||
870 | ctx.xso->ssl_mode |= (uint32_t)larg; | |
871 | return ctx.xso->ssl_mode; | |
872 | } | |
dfc227bd | 873 | |
cb5c208b | 874 | return ctx.qc->default_ssl_mode; |
22d53c88 | 875 | case SSL_CTRL_CLEAR_MODE: |
cb5c208b HL |
876 | if (!ctx.is_stream) |
877 | ctx.qc->default_ssl_mode &= ~(uint32_t)larg; | |
878 | ||
879 | if (ctx.xso != NULL) { | |
880 | ctx.xso->ssl_mode &= ~(uint32_t)larg; | |
881 | return ctx.xso->ssl_mode; | |
882 | } | |
883 | ||
884 | return ctx.qc->default_ssl_mode; | |
22d53c88 | 885 | default: |
f8ffab0d | 886 | /* Probably a TLS related ctrl. Defer to our internal SSL object */ |
072328dd | 887 | return SSL_ctrl(ctx.qc->tls, cmd, larg, parg); |
08e49012 | 888 | } |
22d53c88 HL |
889 | } |
890 | ||
891 | /* SSL_set_connect_state */ | |
072328dd | 892 | void ossl_quic_set_connect_state(SSL *s) |
22d53c88 | 893 | { |
072328dd HL |
894 | QCTX ctx; |
895 | ||
896 | if (!expect_quic(s, &ctx)) | |
897 | return; | |
898 | ||
22d53c88 | 899 | /* Cannot be changed after handshake started */ |
cb5c208b | 900 | if (ctx.qc->started || ctx.is_stream) |
22d53c88 HL |
901 | return; |
902 | ||
dfb9ae14 | 903 | ctx.qc->as_server_state = 0; |
22d53c88 HL |
904 | } |
905 | ||
906 | /* SSL_set_accept_state */ | |
072328dd | 907 | void ossl_quic_set_accept_state(SSL *s) |
22d53c88 | 908 | { |
072328dd HL |
909 | QCTX ctx; |
910 | ||
911 | if (!expect_quic(s, &ctx)) | |
912 | return; | |
913 | ||
22d53c88 | 914 | /* Cannot be changed after handshake started */ |
cb5c208b | 915 | if (ctx.qc->started || ctx.is_stream) |
22d53c88 HL |
916 | return; |
917 | ||
dfb9ae14 | 918 | ctx.qc->as_server_state = 1; |
22d53c88 HL |
919 | } |
920 | ||
921 | /* SSL_do_handshake */ | |
922 | struct quic_handshake_wait_args { | |
923 | QUIC_CONNECTION *qc; | |
924 | }; | |
925 | ||
926 | static int quic_handshake_wait(void *arg) | |
927 | { | |
928 | struct quic_handshake_wait_args *args = arg; | |
929 | ||
930 | if (!ossl_quic_channel_is_active(args->qc->ch)) | |
931 | return -1; | |
932 | ||
933 | if (ossl_quic_channel_is_handshake_complete(args->qc->ch)) | |
934 | return 1; | |
935 | ||
99e1cc7b TM |
936 | return 0; |
937 | } | |
938 | ||
22d53c88 | 939 | static int configure_channel(QUIC_CONNECTION *qc) |
99e1cc7b | 940 | { |
22d53c88 | 941 | assert(qc->ch != NULL); |
08e49012 | 942 | |
d1ac77b1 HL |
943 | if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio) |
944 | || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio) | |
22d53c88 HL |
945 | || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr)) |
946 | return 0; | |
947 | ||
948 | return 1; | |
949 | } | |
950 | ||
4847599b | 951 | QUIC_NEEDS_LOCK |
23c04709 | 952 | static int create_channel(QUIC_CONNECTION *qc) |
22d53c88 HL |
953 | { |
954 | QUIC_CHANNEL_ARGS args = {0}; | |
955 | ||
22d53c88 HL |
956 | args.libctx = qc->ssl.ctx->libctx; |
957 | args.propq = qc->ssl.ctx->propq; | |
23c04709 | 958 | args.is_server = qc->as_server; |
2723d705 | 959 | args.tls = qc->tls; |
ffce2946 | 960 | args.mutex = qc->mutex; |
b212d554 HL |
961 | args.now_cb = qc->override_now_cb; |
962 | args.now_cb_arg = qc->override_now_cb_arg; | |
22d53c88 HL |
963 | |
964 | qc->ch = ossl_quic_channel_new(&args); | |
965 | if (qc->ch == NULL) | |
966 | return 0; | |
967 | ||
e8043229 HL |
968 | return 1; |
969 | } | |
970 | ||
971 | /* | |
972 | * Creates a channel and configures it with the information we have accumulated | |
973 | * via calls made to us from the application prior to starting a handshake | |
974 | * attempt. | |
975 | */ | |
4847599b | 976 | QUIC_NEEDS_LOCK |
23c04709 | 977 | static int ensure_channel_started(QUIC_CONNECTION *qc) |
e8043229 | 978 | { |
ffce2946 | 979 | if (!qc->started) { |
ffce2946 HL |
980 | if (!configure_channel(qc) |
981 | || !ossl_quic_channel_start(qc->ch)) | |
982 | goto err; | |
f2f7c4f1 | 983 | |
ffce2946 HL |
984 | if (qc->is_thread_assisted) |
985 | if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch)) | |
986 | goto err; | |
987 | } | |
988 | ||
22d53c88 HL |
989 | qc->started = 1; |
990 | return 1; | |
f2f7c4f1 HL |
991 | |
992 | err: | |
993 | ossl_quic_channel_free(qc->ch); | |
994 | qc->ch = NULL; | |
995 | return 0; | |
99e1cc7b TM |
996 | } |
997 | ||
4a530180 HL |
998 | QUIC_NEEDS_LOCK |
999 | static int quic_do_handshake(QUIC_CONNECTION *qc) | |
99e1cc7b | 1000 | { |
22d53c88 HL |
1001 | int ret; |
1002 | ||
23c04709 | 1003 | if (ossl_quic_channel_is_handshake_complete(qc->ch)) |
ca41f6b7 | 1004 | /* Handshake already completed. */ |
4a530180 | 1005 | return 1; |
ca41f6b7 | 1006 | |
23c04709 | 1007 | if (ossl_quic_channel_is_term_any(qc->ch)) |
4a530180 | 1008 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); |
22d53c88 | 1009 | |
ca41f6b7 | 1010 | if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) { |
22d53c88 | 1011 | /* Peer address must have been set. */ |
44a1ac5d | 1012 | QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL); |
4a530180 | 1013 | return -1; /* Non-protocol error */ |
ca41f6b7 | 1014 | } |
22d53c88 | 1015 | |
dfb9ae14 | 1016 | if (qc->as_server != qc->as_server_state) { |
23c04709 | 1017 | /* TODO(QUIC): Must match the method used to create the QCSO */ |
ca41f6b7 | 1018 | QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL); |
4a530180 | 1019 | return -1; /* Non-protocol error */ |
ca41f6b7 | 1020 | } |
22d53c88 | 1021 | |
ca41f6b7 | 1022 | if (qc->net_rbio == NULL || qc->net_wbio == NULL) { |
22d53c88 | 1023 | /* Need read and write BIOs. */ |
44a1ac5d | 1024 | QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BIO_NOT_SET, NULL); |
4a530180 | 1025 | return -1; /* Non-protocol error */ |
ca41f6b7 | 1026 | } |
22d53c88 HL |
1027 | |
1028 | /* | |
1029 | * Start connection process. Note we may come here multiple times in | |
1030 | * non-blocking mode, which is fine. | |
1031 | */ | |
23c04709 | 1032 | if (!ensure_channel_started(qc)) { |
ca41f6b7 | 1033 | QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); |
4a530180 | 1034 | return -1; /* Non-protocol error */ |
ca41f6b7 | 1035 | } |
22d53c88 | 1036 | |
4a530180 | 1037 | if (ossl_quic_channel_is_handshake_complete(qc->ch)) |
22d53c88 | 1038 | /* The handshake is now done. */ |
4a530180 | 1039 | return 1; |
22d53c88 | 1040 | |
cb5c208b | 1041 | if (qc_blocking_mode(qc)) { |
22d53c88 HL |
1042 | /* In blocking mode, wait for the handshake to complete. */ |
1043 | struct quic_handshake_wait_args args; | |
1044 | ||
1045 | args.qc = qc; | |
1046 | ||
1047 | ret = block_until_pred(qc, quic_handshake_wait, &args, 0); | |
ca41f6b7 HL |
1048 | if (!ossl_quic_channel_is_active(qc->ch)) { |
1049 | QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
4a530180 | 1050 | return 0; /* Shutdown before completion */ |
ca41f6b7 HL |
1051 | } else if (ret <= 0) { |
1052 | QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
4a530180 | 1053 | return -1; /* Non-protocol error */ |
ca41f6b7 | 1054 | } |
22d53c88 HL |
1055 | |
1056 | assert(ossl_quic_channel_is_handshake_complete(qc->ch)); | |
4a530180 | 1057 | return 1; |
22d53c88 | 1058 | } else { |
ca41f6b7 | 1059 | /* Try to advance the reactor. */ |
ccd31037 | 1060 | ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch), 0); |
ca41f6b7 | 1061 | |
4a530180 | 1062 | if (ossl_quic_channel_is_handshake_complete(qc->ch)) |
ca41f6b7 | 1063 | /* The handshake is now done. */ |
4a530180 | 1064 | return 1; |
ca41f6b7 | 1065 | |
22d53c88 | 1066 | /* Otherwise, indicate that the handshake isn't done yet. */ |
ca41f6b7 | 1067 | QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ); |
4a530180 | 1068 | return -1; /* Non-protocol error */ |
22d53c88 | 1069 | } |
4a530180 | 1070 | } |
a8489257 | 1071 | |
4a530180 | 1072 | QUIC_TAKES_LOCK |
072328dd | 1073 | int ossl_quic_do_handshake(SSL *s) |
4a530180 HL |
1074 | { |
1075 | int ret; | |
072328dd | 1076 | QCTX ctx; |
4a530180 | 1077 | |
072328dd HL |
1078 | if (!expect_quic(s, &ctx)) |
1079 | return 0; | |
1080 | ||
1081 | quic_lock(ctx.qc); | |
4a530180 | 1082 | |
072328dd HL |
1083 | ret = quic_do_handshake(ctx.qc); |
1084 | quic_unlock(ctx.qc); | |
a8489257 | 1085 | return ret; |
99e1cc7b TM |
1086 | } |
1087 | ||
22d53c88 HL |
1088 | /* SSL_connect */ |
1089 | int ossl_quic_connect(SSL *s) | |
99e1cc7b | 1090 | { |
22d53c88 | 1091 | /* Ensure we are in connect state (no-op if non-idle). */ |
072328dd | 1092 | ossl_quic_set_connect_state(s); |
22d53c88 HL |
1093 | |
1094 | /* Begin or continue the handshake */ | |
072328dd | 1095 | return ossl_quic_do_handshake(s); |
99e1cc7b TM |
1096 | } |
1097 | ||
22d53c88 HL |
1098 | /* SSL_accept */ |
1099 | int ossl_quic_accept(SSL *s) | |
99e1cc7b | 1100 | { |
22d53c88 | 1101 | /* Ensure we are in accept state (no-op if non-idle). */ |
072328dd | 1102 | ossl_quic_set_accept_state(s); |
22d53c88 HL |
1103 | |
1104 | /* Begin or continue the handshake */ | |
072328dd | 1105 | return ossl_quic_do_handshake(s); |
99e1cc7b | 1106 | } |
e44795bd | 1107 | |
cb5c208b HL |
1108 | /* |
1109 | * QUIC Front-End I/O API: Stream Lifecycle Operations | |
1110 | * =================================================== | |
1111 | * | |
1112 | * SSL_stream_new => ossl_quic_conn_stream_new | |
1113 | * | |
1114 | */ | |
21c80696 HL |
1115 | |
1116 | /* | |
1117 | * Try to create the default XSO if it doesn't already exist. Returns 1 if the | |
1118 | * default XSO was created. Returns 0 if it was not (e.g. because it already | |
1119 | * exists). Note that this is NOT an error condition. | |
1120 | */ | |
1121 | QUIC_NEEDS_LOCK | |
1122 | static int qc_try_create_default_xso(QUIC_CONNECTION *qc, int remote_init) | |
1123 | { | |
1124 | QUIC_STREAM *qs; | |
1125 | ||
1126 | if (qc->default_xso != NULL) | |
1127 | qc->default_xso_created = 1; | |
1128 | ||
1129 | if (qc->default_xso_created) | |
1130 | /* | |
1131 | * We only do this once. If the user detaches a previously created | |
1132 | * default XSO we don't auto-create another one. | |
1133 | */ | |
1134 | return 0; | |
1135 | ||
1136 | if (!remote_init) { | |
1137 | /* | |
1138 | * We are writing to the stream first, so this is a locally-initiated | |
1139 | * stream. | |
1140 | */ | |
1141 | qc->default_xso = (QUIC_XSO *)ossl_quic_conn_stream_new(&qc->ssl, 0); | |
1142 | if (qc->default_xso == NULL) | |
1143 | return 0; | |
1144 | } else { | |
1145 | /* | |
1146 | * Client is reading first. This means it is expecting to get data on a | |
1147 | * stream the peer writes to first, meaning this is a remotely-initiated | |
1148 | * stream. Ordinarily, we wait for the RXDP to handle a STREAM frame to | |
1149 | * create such a stream. But in this case, special case it and create | |
1150 | * the bookkeeping structures for the first peer-initiated bidirectional | |
1151 | * stream so the client can start to wait on it. | |
1152 | */ | |
1153 | qs = ossl_quic_channel_new_stream_remote(qc->ch, | |
1154 | QUIC_STREAM_INITIATOR_SERVER | |
1155 | | QUIC_STREAM_DIR_BIDI); | |
1156 | if (qs == NULL) | |
1157 | return 0; | |
1158 | ||
1159 | qc->default_xso = create_xso_from_stream(qc, qs); | |
1160 | if (qc->default_xso == NULL) { | |
1161 | ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(qc->ch), qs); | |
1162 | return 0; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | qc->default_xso_created = 1; | |
1167 | return 1; | |
1168 | } | |
1169 | ||
1170 | QUIC_NEEDS_LOCK | |
1171 | static QUIC_XSO *create_xso_from_stream(QUIC_CONNECTION *qc, QUIC_STREAM *qs) | |
1172 | { | |
1173 | QUIC_XSO *xso = NULL; | |
1174 | ||
1175 | if ((xso = OPENSSL_zalloc(sizeof(*xso))) == NULL) | |
1176 | goto err; | |
1177 | ||
1178 | if (!ossl_ssl_init(&xso->ssl, qc->ssl.ctx, qc->ssl.method, SSL_TYPE_QUIC_XSO)) | |
1179 | goto err; | |
1180 | ||
1181 | xso->conn = qc; | |
1182 | xso->blocking = qc->default_blocking; | |
1183 | xso->ssl_mode = qc->default_ssl_mode; | |
1184 | ||
1185 | xso->stream = qs; | |
1186 | ||
1187 | ++qc->num_xso; | |
1188 | return xso; | |
1189 | ||
1190 | err: | |
1191 | OPENSSL_free(xso); | |
1192 | return NULL; | |
1193 | } | |
1194 | ||
1195 | QUIC_TAKES_LOCK | |
cb5c208b HL |
1196 | SSL *ossl_quic_conn_stream_new(SSL *s, uint64_t flags) |
1197 | { | |
1198 | QCTX ctx; | |
1199 | QUIC_XSO *xso = NULL; | |
21c80696 | 1200 | QUIC_STREAM *qs = NULL; |
2dbc39de | 1201 | int is_uni = ((flags & SSL_STREAM_FLAG_UNI) != 0); |
cb5c208b HL |
1202 | |
1203 | if (!expect_quic_conn_only(s, &ctx)) | |
1204 | return NULL; | |
1205 | ||
2dbc39de HL |
1206 | quic_lock(ctx.qc); |
1207 | ||
1208 | if (ossl_quic_channel_is_term_any(ctx.qc->ch)) { | |
1209 | QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
1210 | goto err; | |
1211 | } | |
1212 | ||
21c80696 HL |
1213 | qs = ossl_quic_channel_new_stream_local(ctx.qc->ch, is_uni); |
1214 | if (qs == NULL) | |
2dbc39de | 1215 | goto err; |
cb5c208b | 1216 | |
21c80696 HL |
1217 | xso = create_xso_from_stream(ctx.qc, qs); |
1218 | if (xso == NULL) | |
cb5c208b HL |
1219 | goto err; |
1220 | ||
2dbc39de | 1221 | quic_unlock(ctx.qc); |
cb5c208b HL |
1222 | return &xso->ssl; |
1223 | ||
1224 | err: | |
1225 | OPENSSL_free(xso); | |
21c80696 | 1226 | ossl_quic_stream_map_release(ossl_quic_channel_get_qsm(ctx.qc->ch), qs); |
2dbc39de | 1227 | quic_unlock(ctx.qc); |
cb5c208b HL |
1228 | return NULL; |
1229 | } | |
1230 | ||
22d53c88 HL |
1231 | /* |
1232 | * QUIC Front-End I/O API: Steady-State Operations | |
1233 | * =============================================== | |
1234 | * | |
1235 | * Here we dispatch calls to the steady-state front-end I/O API functions; that | |
1236 | * is, the functions used during the established phase of a QUIC connection | |
1237 | * (e.g. SSL_read, SSL_write). | |
1238 | * | |
1239 | * Each function must handle both blocking and non-blocking modes. As discussed | |
1240 | * above, all QUIC I/O is implemented using non-blocking mode internally. | |
1241 | * | |
1242 | * SSL_get_error => partially implemented by ossl_quic_get_error | |
1243 | * (BIO/)SSL_read => ossl_quic_read | |
1244 | * (BIO/)SSL_write => ossl_quic_write | |
1245 | * SSL_pending => ossl_quic_pending | |
a9979965 | 1246 | * SSL_stream_conclude => ossl_quic_conn_stream_conclude |
22d53c88 HL |
1247 | */ |
1248 | ||
1249 | /* SSL_get_error */ | |
072328dd | 1250 | int ossl_quic_get_error(const SSL *s, int i) |
e44795bd | 1251 | { |
072328dd HL |
1252 | QCTX ctx; |
1253 | ||
1254 | if (!expect_quic(s, &ctx)) | |
1255 | return 0; | |
1256 | ||
1257 | return ctx.qc->last_error; | |
e44795bd TM |
1258 | } |
1259 | ||
22d53c88 HL |
1260 | /* |
1261 | * SSL_write | |
1262 | * --------- | |
1263 | * | |
1264 | * The set of functions below provide the implementation of the public SSL_write | |
1265 | * function. We must handle: | |
1266 | * | |
1267 | * - both blocking and non-blocking operation at the application level, | |
1268 | * depending on how we are configured; | |
1269 | * | |
1270 | * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off; | |
1271 | * | |
1272 | * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER. | |
1273 | * | |
1274 | */ | |
4847599b | 1275 | QUIC_NEEDS_LOCK |
cb5c208b | 1276 | static void quic_post_write(QUIC_XSO *xso, int did_append, int do_tick) |
22d53c88 HL |
1277 | { |
1278 | /* | |
1279 | * We have appended at least one byte to the stream. | |
1280 | * Potentially mark stream as active, depending on FC. | |
1281 | */ | |
1282 | if (did_append) | |
cb5c208b HL |
1283 | ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(xso->conn->ch), |
1284 | xso->stream); | |
22d53c88 HL |
1285 | |
1286 | /* | |
1287 | * Try and send. | |
1288 | * | |
1289 | * TODO(QUIC): It is probably inefficient to try and do this immediately, | |
1290 | * plus we should eventually consider Nagle's algorithm. | |
1291 | */ | |
1292 | if (do_tick) | |
cb5c208b | 1293 | ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(xso->conn->ch), 0); |
22d53c88 HL |
1294 | } |
1295 | ||
1296 | struct quic_write_again_args { | |
cb5c208b | 1297 | QUIC_XSO *xso; |
22d53c88 HL |
1298 | const unsigned char *buf; |
1299 | size_t len; | |
1300 | size_t total_written; | |
1301 | }; | |
1302 | ||
4847599b | 1303 | QUIC_NEEDS_LOCK |
22d53c88 HL |
1304 | static int quic_write_again(void *arg) |
1305 | { | |
1306 | struct quic_write_again_args *args = arg; | |
1307 | size_t actual_written = 0; | |
1308 | ||
cb5c208b | 1309 | if (!ossl_quic_channel_is_active(args->xso->conn->ch)) |
22d53c88 HL |
1310 | /* If connection is torn down due to an error while blocking, stop. */ |
1311 | return -2; | |
1312 | ||
cb5c208b | 1313 | if (!ossl_quic_sstream_append(args->xso->stream->sstream, |
22d53c88 HL |
1314 | args->buf, args->len, &actual_written)) |
1315 | return -2; | |
1316 | ||
cb5c208b | 1317 | quic_post_write(args->xso, actual_written > 0, 0); |
22d53c88 HL |
1318 | |
1319 | args->buf += actual_written; | |
1320 | args->len -= actual_written; | |
1321 | args->total_written += actual_written; | |
1322 | ||
3f0c310b | 1323 | if (args->len == 0) |
22d53c88 HL |
1324 | /* Written everything, done. */ |
1325 | return 1; | |
1326 | ||
1327 | /* Not written everything yet, keep trying. */ | |
1328 | return 0; | |
1329 | } | |
1330 | ||
4847599b | 1331 | QUIC_NEEDS_LOCK |
cb5c208b | 1332 | static int quic_write_blocking(QUIC_XSO *xso, const void *buf, size_t len, |
22d53c88 | 1333 | size_t *written) |
e44795bd | 1334 | { |
22d53c88 HL |
1335 | int res; |
1336 | struct quic_write_again_args args; | |
1337 | size_t actual_written = 0; | |
1338 | ||
1339 | /* First make a best effort to append as much of the data as possible. */ | |
cb5c208b | 1340 | if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, |
22d53c88 HL |
1341 | &actual_written)) { |
1342 | /* Stream already finished or allocation error. */ | |
1343 | *written = 0; | |
cb5c208b | 1344 | return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL); |
22d53c88 HL |
1345 | } |
1346 | ||
cb5c208b | 1347 | quic_post_write(xso, actual_written > 0, 1); |
22d53c88 HL |
1348 | |
1349 | if (actual_written == len) { | |
1350 | /* Managed to append everything on the first try. */ | |
1351 | *written = actual_written; | |
1352 | return 1; | |
1353 | } | |
1354 | ||
1355 | /* | |
1356 | * We did not manage to append all of the data immediately, so the stream | |
1357 | * buffer has probably filled up. This means we need to block until some of | |
1358 | * it is freed up. | |
1359 | */ | |
cb5c208b | 1360 | args.xso = xso; |
22d53c88 HL |
1361 | args.buf = (const unsigned char *)buf + actual_written; |
1362 | args.len = len - actual_written; | |
1363 | args.total_written = 0; | |
1364 | ||
cb5c208b | 1365 | res = block_until_pred(xso->conn, quic_write_again, &args, 0); |
22d53c88 | 1366 | if (res <= 0) { |
cb5c208b HL |
1367 | if (!ossl_quic_channel_is_active(xso->conn->ch)) |
1368 | return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
22d53c88 | 1369 | else |
cb5c208b | 1370 | return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL); |
22d53c88 HL |
1371 | } |
1372 | ||
1373 | *written = args.total_written; | |
e44795bd TM |
1374 | return 1; |
1375 | } | |
1376 | ||
ca41f6b7 HL |
1377 | /* |
1378 | * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE) | |
1379 | * write semantics. | |
1380 | */ | |
cb5c208b | 1381 | static void aon_write_begin(QUIC_XSO *xso, const unsigned char *buf, |
22d53c88 HL |
1382 | size_t buf_len, size_t already_sent) |
1383 | { | |
cb5c208b | 1384 | assert(!xso->aon_write_in_progress); |
22d53c88 | 1385 | |
cb5c208b HL |
1386 | xso->aon_write_in_progress = 1; |
1387 | xso->aon_buf_base = buf; | |
1388 | xso->aon_buf_pos = already_sent; | |
1389 | xso->aon_buf_len = buf_len; | |
22d53c88 HL |
1390 | } |
1391 | ||
cb5c208b | 1392 | static void aon_write_finish(QUIC_XSO *xso) |
22d53c88 | 1393 | { |
cb5c208b HL |
1394 | xso->aon_write_in_progress = 0; |
1395 | xso->aon_buf_base = NULL; | |
1396 | xso->aon_buf_pos = 0; | |
1397 | xso->aon_buf_len = 0; | |
22d53c88 HL |
1398 | } |
1399 | ||
4847599b | 1400 | QUIC_NEEDS_LOCK |
cb5c208b | 1401 | static int quic_write_nonblocking_aon(QUIC_XSO *xso, const void *buf, |
22d53c88 | 1402 | size_t len, size_t *written) |
e44795bd | 1403 | { |
22d53c88 HL |
1404 | const void *actual_buf; |
1405 | size_t actual_len, actual_written = 0; | |
1406 | int accept_moving_buffer | |
cb5c208b | 1407 | = ((xso->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0); |
22d53c88 | 1408 | |
cb5c208b | 1409 | if (xso->aon_write_in_progress) { |
22d53c88 HL |
1410 | /* |
1411 | * We are in the middle of an AON write (i.e., a previous write did not | |
75b2920a HL |
1412 | * manage to append all data to the SSTREAM and we have Enable Partial |
1413 | * Write (EPW) mode disabled.) | |
22d53c88 | 1414 | */ |
cb5c208b HL |
1415 | if ((!accept_moving_buffer && xso->aon_buf_base != buf) |
1416 | || len != xso->aon_buf_len) | |
22d53c88 HL |
1417 | /* |
1418 | * Pointer must not have changed if we are not in accept moving | |
1419 | * buffer mode. Length must never change. | |
1420 | */ | |
cb5c208b | 1421 | return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, SSL_R_BAD_WRITE_RETRY, NULL); |
22d53c88 | 1422 | |
cb5c208b HL |
1423 | actual_buf = (unsigned char *)buf + xso->aon_buf_pos; |
1424 | actual_len = len - xso->aon_buf_pos; | |
22d53c88 HL |
1425 | assert(actual_len > 0); |
1426 | } else { | |
1427 | actual_buf = buf; | |
1428 | actual_len = len; | |
1429 | } | |
1430 | ||
1431 | /* First make a best effort to append as much of the data as possible. */ | |
cb5c208b | 1432 | if (!ossl_quic_sstream_append(xso->stream->sstream, actual_buf, actual_len, |
22d53c88 HL |
1433 | &actual_written)) { |
1434 | /* Stream already finished or allocation error. */ | |
1435 | *written = 0; | |
cb5c208b | 1436 | return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL); |
22d53c88 HL |
1437 | } |
1438 | ||
cb5c208b | 1439 | quic_post_write(xso, actual_written > 0, 1); |
22d53c88 HL |
1440 | |
1441 | if (actual_written == actual_len) { | |
1442 | /* We have sent everything. */ | |
cb5c208b | 1443 | if (xso->aon_write_in_progress) { |
22d53c88 HL |
1444 | /* |
1445 | * We have sent everything, and we were in the middle of an AON | |
1446 | * write. The output write length is the total length of the AON | |
1447 | * buffer, not however many bytes we managed to write to the stream | |
1448 | * in this call. | |
1449 | */ | |
cb5c208b HL |
1450 | *written = xso->aon_buf_len; |
1451 | aon_write_finish(xso); | |
22d53c88 HL |
1452 | } else { |
1453 | *written = actual_written; | |
1454 | } | |
1455 | ||
1456 | return 1; | |
1457 | } | |
1458 | ||
cb5c208b | 1459 | if (xso->aon_write_in_progress) { |
22d53c88 HL |
1460 | /* |
1461 | * AON write is in progress but we have not written everything yet. We | |
1462 | * may have managed to send zero bytes, or some number of bytes less | |
1463 | * than the total remaining which need to be appended during this | |
1464 | * AON operation. | |
1465 | */ | |
cb5c208b HL |
1466 | xso->aon_buf_pos += actual_written; |
1467 | assert(xso->aon_buf_pos < xso->aon_buf_len); | |
1468 | return QUIC_RAISE_NORMAL_ERROR(xso->conn, SSL_ERROR_WANT_WRITE); | |
22d53c88 HL |
1469 | } |
1470 | ||
08e49012 | 1471 | /* |
22d53c88 HL |
1472 | * Not in an existing AON operation but partial write is not enabled, so we |
1473 | * need to begin a new AON operation. However we needn't bother if we didn't | |
1474 | * actually append anything. | |
08e49012 | 1475 | */ |
22d53c88 | 1476 | if (actual_written > 0) |
cb5c208b | 1477 | aon_write_begin(xso, buf, len, actual_written); |
e44795bd | 1478 | |
22d53c88 HL |
1479 | /* |
1480 | * AON - We do not publicly admit to having appended anything until AON | |
1481 | * completes. | |
1482 | */ | |
1483 | *written = 0; | |
cb5c208b | 1484 | return QUIC_RAISE_NORMAL_ERROR(xso->conn, SSL_ERROR_WANT_WRITE); |
e44795bd TM |
1485 | } |
1486 | ||
4847599b | 1487 | QUIC_NEEDS_LOCK |
cb5c208b | 1488 | static int quic_write_nonblocking_epw(QUIC_XSO *xso, const void *buf, size_t len, |
22d53c88 | 1489 | size_t *written) |
e44795bd | 1490 | { |
22d53c88 | 1491 | /* Simple best effort operation. */ |
cb5c208b | 1492 | if (!ossl_quic_sstream_append(xso->stream->sstream, buf, len, written)) { |
22d53c88 HL |
1493 | /* Stream already finished or allocation error. */ |
1494 | *written = 0; | |
cb5c208b | 1495 | return QUIC_RAISE_NON_NORMAL_ERROR(xso->conn, ERR_R_INTERNAL_ERROR, NULL); |
22d53c88 HL |
1496 | } |
1497 | ||
cb5c208b | 1498 | quic_post_write(xso, *written > 0, 1); |
e44795bd TM |
1499 | return 1; |
1500 | } | |
d5ab48a1 | 1501 | |
a8489257 | 1502 | QUIC_TAKES_LOCK |
22d53c88 | 1503 | int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written) |
d5ab48a1 | 1504 | { |
a8489257 | 1505 | int ret; |
072328dd HL |
1506 | QCTX ctx; |
1507 | int partial_write; | |
22d53c88 HL |
1508 | |
1509 | *written = 0; | |
1510 | ||
21c80696 | 1511 | if (!expect_quic_with_stream(s, /*remote_init=*/0, &ctx)) { |
22d53c88 | 1512 | return 0; |
21c80696 | 1513 | } |
22d53c88 | 1514 | |
072328dd | 1515 | quic_lock(ctx.qc); |
a8489257 | 1516 | |
cb5c208b | 1517 | partial_write = ((ctx.xso->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0); |
072328dd | 1518 | |
23c04709 | 1519 | if (ossl_quic_channel_is_term_any(ctx.qc->ch)) { |
072328dd | 1520 | ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); |
a8489257 HL |
1521 | goto out; |
1522 | } | |
22d53c88 | 1523 | |
ca41f6b7 HL |
1524 | /* |
1525 | * If we haven't finished the handshake, try to advance it. | |
1526 | * We don't accept writes until the handshake is completed. | |
1527 | */ | |
072328dd | 1528 | if (quic_do_handshake(ctx.qc) < 1) { |
a8489257 HL |
1529 | ret = 0; |
1530 | goto out; | |
1531 | } | |
ca41f6b7 | 1532 | |
cb5c208b | 1533 | if (ctx.xso->stream == NULL || ctx.xso->stream->sstream == NULL) { |
072328dd | 1534 | ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL); |
a8489257 HL |
1535 | goto out; |
1536 | } | |
22d53c88 | 1537 | |
cb5c208b HL |
1538 | if (xso_blocking_mode(ctx.xso)) |
1539 | ret = quic_write_blocking(ctx.xso, buf, len, written); | |
22d53c88 | 1540 | else if (partial_write) |
cb5c208b | 1541 | ret = quic_write_nonblocking_epw(ctx.xso, buf, len, written); |
22d53c88 | 1542 | else |
cb5c208b | 1543 | ret = quic_write_nonblocking_aon(ctx.xso, buf, len, written); |
a8489257 HL |
1544 | |
1545 | out: | |
072328dd | 1546 | quic_unlock(ctx.qc); |
a8489257 | 1547 | return ret; |
d5ab48a1 RL |
1548 | } |
1549 | ||
1550 | /* | |
22d53c88 HL |
1551 | * SSL_read |
1552 | * -------- | |
d5ab48a1 | 1553 | */ |
22d53c88 HL |
1554 | struct quic_read_again_args { |
1555 | QUIC_CONNECTION *qc; | |
1556 | QUIC_STREAM *stream; | |
1557 | void *buf; | |
1558 | size_t len; | |
1559 | size_t *bytes_read; | |
1560 | int peek; | |
1561 | }; | |
1562 | ||
4847599b | 1563 | QUIC_NEEDS_LOCK |
22d53c88 HL |
1564 | static int quic_read_actual(QUIC_CONNECTION *qc, |
1565 | QUIC_STREAM *stream, | |
1566 | void *buf, size_t buf_len, | |
1567 | size_t *bytes_read, | |
1568 | int peek) | |
d5ab48a1 | 1569 | { |
22d53c88 HL |
1570 | int is_fin = 0; |
1571 | ||
a9979965 HL |
1572 | /* If the receive part of the stream is over, issue EOF. */ |
1573 | if (stream->recv_fin_retired) | |
1574 | return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_ZERO_RETURN); | |
1575 | ||
22d53c88 HL |
1576 | if (stream->rstream == NULL) |
1577 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1578 | ||
1579 | if (peek) { | |
1580 | if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len, | |
1581 | bytes_read, &is_fin)) | |
1582 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1583 | ||
1584 | } else { | |
1585 | if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len, | |
1586 | bytes_read, &is_fin)) | |
1587 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1588 | } | |
1589 | ||
1590 | if (!peek) { | |
1591 | if (*bytes_read > 0) { | |
1592 | /* | |
1593 | * We have read at least one byte from the stream. Inform stream-level | |
1594 | * RXFC of the retirement of controlled bytes. Update the active stream | |
1595 | * status (the RXFC may now want to emit a frame granting more credit to | |
1596 | * the peer). | |
1597 | */ | |
1598 | OSSL_RTT_INFO rtt_info; | |
d50e750e | 1599 | |
22d53c88 HL |
1600 | ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info); |
1601 | ||
cb5c208b | 1602 | if (!ossl_quic_rxfc_on_retire(&stream->rxfc, *bytes_read, |
22d53c88 HL |
1603 | rtt_info.smoothed_rtt)) |
1604 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1605 | } | |
1606 | ||
1607 | if (is_fin) | |
1608 | stream->recv_fin_retired = 1; | |
1609 | ||
1610 | if (*bytes_read > 0) | |
1611 | ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch), | |
cb5c208b | 1612 | stream); |
22d53c88 HL |
1613 | } |
1614 | ||
d5ab48a1 RL |
1615 | return 1; |
1616 | } | |
1617 | ||
4847599b | 1618 | QUIC_NEEDS_LOCK |
22d53c88 | 1619 | static int quic_read_again(void *arg) |
d5ab48a1 | 1620 | { |
22d53c88 HL |
1621 | struct quic_read_again_args *args = arg; |
1622 | ||
a9979965 | 1623 | if (!ossl_quic_channel_is_active(args->qc->ch)) { |
22d53c88 | 1624 | /* If connection is torn down due to an error while blocking, stop. */ |
a9979965 | 1625 | QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); |
ca41f6b7 | 1626 | return -1; |
a9979965 | 1627 | } |
22d53c88 HL |
1628 | |
1629 | if (!quic_read_actual(args->qc, args->stream, | |
1630 | args->buf, args->len, args->bytes_read, | |
1631 | args->peek)) | |
1632 | return -1; | |
1633 | ||
1634 | if (*args->bytes_read > 0) | |
1635 | /* got at least one byte, the SSL_read op can finish now */ | |
1636 | return 1; | |
1637 | ||
81b6b43c | 1638 | return 0; /* did not read anything, keep trying */ |
d5ab48a1 RL |
1639 | } |
1640 | ||
a8489257 | 1641 | QUIC_TAKES_LOCK |
22d53c88 | 1642 | static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek) |
d5ab48a1 | 1643 | { |
a8489257 | 1644 | int ret, res; |
072328dd | 1645 | QCTX ctx; |
22d53c88 HL |
1646 | struct quic_read_again_args args; |
1647 | ||
1648 | *bytes_read = 0; | |
1649 | ||
21c80696 | 1650 | if (!expect_quic_with_stream(s, /*remote_init=*/0, &ctx)) |
22d53c88 HL |
1651 | return 0; |
1652 | ||
072328dd | 1653 | quic_lock(ctx.qc); |
a8489257 | 1654 | |
23c04709 | 1655 | if (ossl_quic_channel_is_term_any(ctx.qc->ch)) { |
072328dd | 1656 | ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); |
a8489257 HL |
1657 | goto out; |
1658 | } | |
22d53c88 | 1659 | |
a9979965 | 1660 | /* If we haven't finished the handshake, try to advance it. */ |
072328dd | 1661 | if (quic_do_handshake(ctx.qc) < 1) { |
a8489257 HL |
1662 | ret = 0; /* ossl_quic_do_handshake raised error here */ |
1663 | goto out; | |
1664 | } | |
22d53c88 | 1665 | |
cb5c208b | 1666 | if (ctx.xso->stream == NULL) { |
072328dd | 1667 | ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL); |
a8489257 HL |
1668 | goto out; |
1669 | } | |
22d53c88 | 1670 | |
cb5c208b | 1671 | if (!quic_read_actual(ctx.qc, ctx.xso->stream, buf, len, bytes_read, peek)) { |
a8489257 HL |
1672 | ret = 0; /* quic_read_actual raised error here */ |
1673 | goto out; | |
1674 | } | |
22d53c88 HL |
1675 | |
1676 | if (*bytes_read > 0) { | |
1677 | /* | |
1678 | * Even though we succeeded, tick the reactor here to ensure we are | |
1679 | * handling other aspects of the QUIC connection. | |
1680 | */ | |
072328dd | 1681 | ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(ctx.qc->ch), 0); |
a8489257 | 1682 | ret = 1; |
cb5c208b | 1683 | } else if (xso_blocking_mode(ctx.xso)) { |
22d53c88 HL |
1684 | /* |
1685 | * We were not able to read anything immediately, so our stream | |
1686 | * buffer is empty. This means we need to block until we get | |
1687 | * at least one byte. | |
1688 | */ | |
072328dd | 1689 | args.qc = ctx.qc; |
cb5c208b | 1690 | args.stream = ctx.xso->stream; |
22d53c88 HL |
1691 | args.buf = buf; |
1692 | args.len = len; | |
1693 | args.bytes_read = bytes_read; | |
1694 | args.peek = peek; | |
1695 | ||
072328dd | 1696 | res = block_until_pred(ctx.qc, quic_read_again, &args, 0); |
a8489257 | 1697 | if (res == 0) { |
072328dd | 1698 | ret = QUIC_RAISE_NON_NORMAL_ERROR(ctx.qc, ERR_R_INTERNAL_ERROR, NULL); |
a8489257 HL |
1699 | goto out; |
1700 | } else if (res < 0) { | |
1701 | ret = 0; /* quic_read_again raised error here */ | |
1702 | goto out; | |
1703 | } | |
22d53c88 | 1704 | |
a8489257 | 1705 | ret = 1; |
af8b52cf HL |
1706 | } else { |
1707 | /* We did not get any bytes and are not in blocking mode. */ | |
072328dd | 1708 | ret = QUIC_RAISE_NORMAL_ERROR(ctx.qc, SSL_ERROR_WANT_READ); |
af8b52cf | 1709 | } |
a8489257 HL |
1710 | |
1711 | out: | |
072328dd | 1712 | quic_unlock(ctx.qc); |
a8489257 | 1713 | return ret; |
d5ab48a1 RL |
1714 | } |
1715 | ||
22d53c88 HL |
1716 | int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read) |
1717 | { | |
1718 | return quic_read(s, buf, len, bytes_read, 0); | |
1719 | } | |
1720 | ||
1721 | int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read) | |
1722 | { | |
1723 | return quic_read(s, buf, len, bytes_read, 1); | |
1724 | } | |
1725 | ||
1726 | /* | |
1727 | * SSL_pending | |
1728 | * ----------- | |
1729 | */ | |
a8489257 | 1730 | QUIC_TAKES_LOCK |
072328dd | 1731 | static size_t ossl_quic_pending_int(const SSL *s) |
22d53c88 | 1732 | { |
072328dd | 1733 | QCTX ctx; |
22d53c88 HL |
1734 | size_t avail = 0; |
1735 | int fin = 0; | |
1736 | ||
21c80696 | 1737 | if (!expect_quic_with_stream(s, /*remote_init=*/-1, &ctx)) |
22d53c88 HL |
1738 | return 0; |
1739 | ||
072328dd | 1740 | quic_lock(ctx.qc); |
a8489257 | 1741 | |
cb5c208b | 1742 | if (ctx.xso->stream == NULL || ctx.xso->stream->rstream == NULL) |
22d53c88 | 1743 | /* Cannot raise errors here because we are const, just fail. */ |
a8489257 | 1744 | goto out; |
22d53c88 | 1745 | |
cb5c208b | 1746 | if (!ossl_quic_rstream_available(ctx.xso->stream->rstream, &avail, &fin)) |
a8489257 | 1747 | avail = 0; |
22d53c88 | 1748 | |
a8489257 | 1749 | out: |
072328dd | 1750 | quic_unlock(ctx.qc); |
22d53c88 HL |
1751 | return avail; |
1752 | } | |
1753 | ||
560470b5 MC |
1754 | size_t ossl_quic_pending(const SSL *s) |
1755 | { | |
072328dd | 1756 | return ossl_quic_pending_int(s); |
560470b5 MC |
1757 | } |
1758 | ||
072328dd | 1759 | int ossl_quic_has_pending(const SSL *s) |
560470b5 | 1760 | { |
072328dd | 1761 | return ossl_quic_pending_int(s) > 0; |
560470b5 MC |
1762 | } |
1763 | ||
a9979965 HL |
1764 | /* |
1765 | * SSL_stream_conclude | |
1766 | * ------------------- | |
1767 | */ | |
a8489257 | 1768 | QUIC_TAKES_LOCK |
072328dd | 1769 | int ossl_quic_conn_stream_conclude(SSL *s) |
a9979965 | 1770 | { |
072328dd HL |
1771 | QCTX ctx; |
1772 | QUIC_STREAM *qs; | |
1773 | ||
21c80696 | 1774 | if (!expect_quic_with_stream(s, /*remote_init=*/0, &ctx)) |
072328dd HL |
1775 | return 0; |
1776 | ||
1777 | quic_lock(ctx.qc); | |
a9979965 | 1778 | |
cb5c208b | 1779 | qs = ctx.xso->stream; |
a9979965 | 1780 | |
a8489257 | 1781 | if (qs == NULL || qs->sstream == NULL) { |
072328dd | 1782 | quic_unlock(ctx.qc); |
a8489257 HL |
1783 | return 0; |
1784 | } | |
1785 | ||
072328dd | 1786 | if (!ossl_quic_channel_is_active(ctx.qc->ch) |
a8489257 | 1787 | || ossl_quic_sstream_get_final_size(qs->sstream, NULL)) { |
072328dd | 1788 | quic_unlock(ctx.qc); |
a9979965 | 1789 | return 1; |
a8489257 | 1790 | } |
a9979965 HL |
1791 | |
1792 | ossl_quic_sstream_fin(qs->sstream); | |
cb5c208b | 1793 | quic_post_write(ctx.xso, 1, 1); |
072328dd | 1794 | quic_unlock(ctx.qc); |
a9979965 HL |
1795 | return 1; |
1796 | } | |
1797 | ||
553a4e00 HL |
1798 | /* |
1799 | * SSL_inject_net_dgram | |
1800 | * -------------------- | |
1801 | */ | |
5129e594 | 1802 | QUIC_TAKES_LOCK |
553a4e00 HL |
1803 | int SSL_inject_net_dgram(SSL *s, const unsigned char *buf, |
1804 | size_t buf_len, | |
1805 | const BIO_ADDR *peer, | |
1806 | const BIO_ADDR *local) | |
1807 | { | |
5129e594 | 1808 | int ret; |
072328dd | 1809 | QCTX ctx; |
553a4e00 HL |
1810 | QUIC_DEMUX *demux; |
1811 | ||
072328dd | 1812 | if (!expect_quic(s, &ctx)) |
553a4e00 HL |
1813 | return 0; |
1814 | ||
072328dd | 1815 | quic_lock(ctx.qc); |
5129e594 | 1816 | |
072328dd | 1817 | demux = ossl_quic_channel_get0_demux(ctx.qc->ch); |
5129e594 HL |
1818 | ret = ossl_quic_demux_inject(demux, buf, buf_len, peer, local); |
1819 | ||
072328dd | 1820 | quic_unlock(ctx.qc); |
5129e594 | 1821 | return ret; |
553a4e00 HL |
1822 | } |
1823 | ||
22d53c88 HL |
1824 | /* |
1825 | * QUIC Front-End I/O API: SSL_CTX Management | |
1826 | * ========================================== | |
1827 | */ | |
1828 | ||
1829 | long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) | |
1830 | { | |
1831 | switch (cmd) { | |
1832 | default: | |
8a1a6d6d | 1833 | return ssl3_ctx_ctrl(ctx, cmd, larg, parg); |
22d53c88 HL |
1834 | } |
1835 | } | |
1836 | ||
1837 | long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void)) | |
1838 | { | |
8a1a6d6d | 1839 | return ssl3_callback_ctrl(s, cmd, fp); |
22d53c88 HL |
1840 | } |
1841 | ||
1842 | long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void)) | |
1843 | { | |
8a1a6d6d | 1844 | return ssl3_ctx_callback_ctrl(ctx, cmd, fp); |
22d53c88 HL |
1845 | } |
1846 | ||
1847 | int ossl_quic_renegotiate_check(SSL *ssl, int initok) | |
1848 | { | |
1849 | /* We never do renegotiation. */ | |
1850 | return 0; | |
1851 | } | |
1852 | ||
1853 | /* | |
d518854c MC |
1854 | * These functions define the TLSv1.2 (and below) ciphers that are supported by |
1855 | * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any. | |
22d53c88 | 1856 | */ |
22d53c88 HL |
1857 | |
1858 | int ossl_quic_num_ciphers(void) | |
1859 | { | |
d518854c | 1860 | return 0; |
22d53c88 HL |
1861 | } |
1862 | ||
1863 | const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u) | |
1864 | { | |
d518854c | 1865 | return NULL; |
d5ab48a1 | 1866 | } |