]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/quic/quic_impl.c
threading: Add ossl_crypto_condvar_wait_timeout
[thirdparty/openssl.git] / ssl / quic / quic_impl.c
CommitLineData
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
22d53c88 20static void aon_write_finish(QUIC_CONNECTION *qc);
e8043229 21static int ensure_channel(QUIC_CONNECTION *qc);
22d53c88
HL
22
23/*
24 * QUIC Front-End I/O API: Common Utilities
25 * ========================================
26 */
27
28/*
29 * Block until a predicate is met.
30 *
31 * Precondition: Must have a channel.
32 */
33static int block_until_pred(QUIC_CONNECTION *qc,
34 int (*pred)(void *arg), void *pred_arg,
35 uint32_t flags)
36{
37 QUIC_REACTOR *rtor;
38
39 assert(qc->ch != NULL);
40
41 rtor = ossl_quic_channel_get_reactor(qc->ch);
42 return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags);
43}
44
45/*
46 * Raise a 'normal' error, meaning one that can be reported via SSL_get_error()
47 * rather than via ERR.
48 */
49static int quic_raise_normal_error(QUIC_CONNECTION *qc,
50 int err)
51{
52 qc->last_error = err;
53 return 0;
54}
55
56/*
57 * Raise a 'non-normal' error, meaning any error that is not reported via
58 * SSL_get_error() and must be reported via ERR.
59 */
60static int quic_raise_non_normal_error(QUIC_CONNECTION *qc,
61 const char *file,
62 int line,
63 const char *func,
64 int reason,
65 const char *fmt,
66 ...)
67{
68 va_list args;
69
70 ERR_new();
71 ERR_set_debug(file, line, func);
72
73 va_start(args, fmt);
74 ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
75 va_end(args);
76
77 qc->last_error = SSL_ERROR_SSL;
78 return 0;
79}
80
81#define QUIC_RAISE_NORMAL_ERROR(qc, err) \
82 quic_raise_normal_error((qc), (err))
83
84#define QUIC_RAISE_NON_NORMAL_ERROR(qc, reason, msg) \
85 quic_raise_non_normal_error((qc), \
86 OPENSSL_FILE, OPENSSL_LINE, \
87 OPENSSL_FUNC, \
88 (reason), \
89 (msg))
90
91/*
92 * Should be called at entry of every public function to confirm we have a valid
93 * QUIC_CONNECTION.
94 */
95static ossl_inline int expect_quic_conn(const QUIC_CONNECTION *qc)
96{
97 if (!ossl_assert(qc != NULL))
98 return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL);
99
100 return 1;
101
102}
103
104/*
105 * QUIC Front-End I/O API: Initialization
106 * ======================================
107 *
108 * SSL_new => ossl_quic_new
109 * ossl_quic_init
110 * SSL_reset => ossl_quic_reset
111 * SSL_clear => ossl_quic_clear
112 * ossl_quic_deinit
113 * SSL_free => ossl_quic_free
114 *
115 */
116
117/* SSL_new */
38b051a1
TM
118SSL *ossl_quic_new(SSL_CTX *ctx)
119{
22d53c88
HL
120 QUIC_CONNECTION *qc = NULL;
121 SSL *ssl_base = NULL;
a7f41885 122 SSL_CONNECTION *sc = NULL;
38b051a1
TM
123
124 qc = OPENSSL_zalloc(sizeof(*qc));
125 if (qc == NULL)
126 goto err;
127
22d53c88
HL
128 /* Initialise the QUIC_CONNECTION's stub header. */
129 ssl_base = &qc->ssl;
a7f41885 130 if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) {
22d53c88 131 ssl_base = NULL;
38b051a1
TM
132 goto err;
133 }
38b051a1 134
4e3a55fd 135 qc->tls = ossl_ssl_connection_new_int(ctx, TLS_method());
a7f41885
MC
136 if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL)
137 goto err;
a7f41885 138
22d53c88
HL
139 /* Channel is not created yet. */
140 qc->ssl_mode = qc->ssl.ctx->mode;
141 qc->last_error = SSL_ERROR_NONE;
142 qc->blocking = 1;
a7f41885 143
22d53c88
HL
144 return ssl_base;
145
38b051a1 146err:
22d53c88 147 OPENSSL_free(qc);
38b051a1
TM
148 return NULL;
149}
150
22d53c88
HL
151/* SSL_free */
152void ossl_quic_free(SSL *s)
153{
154 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
155
156 /* We should never be called on anything but a QUIC_CONNECTION. */
157 if (!expect_quic_conn(qc))
158 return;
159
160 ossl_quic_channel_free(qc->ch);
161
d1ac77b1
HL
162 BIO_free(qc->net_rbio);
163 BIO_free(qc->net_wbio);
164
22d53c88 165 /* Note: SSL_free calls OPENSSL_free(qc) for us */
a7f41885
MC
166
167 SSL_free(qc->tls);
22d53c88
HL
168}
169
170/* SSL method init */
38b051a1 171int ossl_quic_init(SSL *s)
99e1cc7b 172{
22d53c88
HL
173 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
174
175 if (!expect_quic_conn(qc))
176 return 0;
177
178 /* Same op as SSL_clear, forward the call. */
179 return ossl_quic_clear(s);
99e1cc7b
TM
180}
181
22d53c88 182/* SSL method deinit */
38b051a1
TM
183void ossl_quic_deinit(SSL *s)
184{
22d53c88 185 /* No-op. */
38b051a1
TM
186}
187
22d53c88
HL
188/* SSL_reset */
189int ossl_quic_reset(SSL *s)
190{
191 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
192
193 if (!expect_quic_conn(qc))
194 return 0;
195
c8b3fdc2 196 /* TODO(QUIC); Currently a no-op. */
22d53c88
HL
197 return 1;
198}
199
200/* SSL_clear */
201int ossl_quic_clear(SSL *s)
99e1cc7b 202{
38b051a1
TM
203 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
204
22d53c88
HL
205 if (!expect_quic_conn(qc))
206 return 0;
207
c8b3fdc2 208 /* TODO(QUIC): Currently a no-op. */
22d53c88
HL
209 return 1;
210}
38b051a1 211
22d53c88
HL
212/*
213 * QUIC Front-End I/O API: Network BIO Configuration
214 * =================================================
215 *
216 * Handling the different BIOs is difficult:
217 *
218 * - It is more or less a requirement that we use non-blocking network I/O;
219 * we need to be able to have timeouts on recv() calls, and make best effort
220 * (non blocking) send() and recv() calls.
221 *
222 * The only sensible way to do this is to configure the socket into
223 * non-blocking mode. We could try to do select() before calling send() or
224 * recv() to get a guarantee that the call will not block, but this will
225 * probably run into issues with buggy OSes which generate spurious socket
226 * readiness events. In any case, relying on this to work reliably does not
227 * seem sane.
228 *
229 * Timeouts could be handled via setsockopt() socket timeout options, but
230 * this depends on OS support and adds another syscall to every network I/O
231 * operation. It also has obvious thread safety concerns if we want to move
232 * to concurrent use of a single socket at some later date.
233 *
234 * Some OSes support a MSG_DONTWAIT flag which allows a single I/O option to
235 * be made non-blocking. However some OSes (e.g. Windows) do not support
236 * this, so we cannot rely on this.
237 *
238 * As such, we need to configure any FD in non-blocking mode. This may
239 * confound users who pass a blocking socket to libssl. However, in practice
240 * it would be extremely strange for a user of QUIC to pass an FD to us,
241 * then also try and send receive traffic on the same socket(!). Thus the
242 * impact of this should be limited, and can be documented.
243 *
244 * - We support both blocking and non-blocking operation in terms of the API
245 * presented to the user. One prospect is to set the blocking mode based on
246 * whether the socket passed to us was already in blocking mode. However,
247 * Windows has no API for determining if a socket is in blocking mode (!),
248 * therefore this cannot be done portably. Currently therefore we expose an
249 * explicit API call to set this, and default to blocking mode.
250 *
251 * - We need to determine our initial destination UDP address. The "natural"
252 * way for a user to do this is to set the peer variable on a BIO_dgram.
253 * However, this has problems because BIO_dgram's peer variable is used for
254 * both transmission and reception. This means it can be constantly being
255 * changed to a malicious value (e.g. if some random unrelated entity on the
256 * network starts sending traffic to us) on every read call. This is not a
257 * direct issue because we use the 'stateless' BIO_sendmmsg and BIO_recvmmsg
258 * calls only, which do not use this variable. However, we do need to let
259 * the user specify the peer in a 'normal' manner. The compromise here is
260 * that we grab the current peer value set at the time the write BIO is set
261 * and do not read the value again.
262 *
263 * - We also need to support memory BIOs (e.g. BIO_dgram_pair) or custom BIOs.
264 * Currently we do this by only supporting non-blocking mode.
265 *
266 */
267
268/*
269 * Determines what initial destination UDP address we should use, if possible.
270 * If this fails the client must set the destination address manually, or use a
271 * BIO which does not need a destination address.
272 */
273static int csm_analyse_init_peer_addr(BIO *net_wbio, BIO_ADDR *peer)
274{
75b2920a 275 if (BIO_dgram_get_peer(net_wbio, peer) <= 0)
22d53c88
HL
276 return 0;
277
278 return 1;
279}
280
281void ossl_quic_conn_set0_net_rbio(QUIC_CONNECTION *qc, BIO *net_rbio)
282{
283 if (qc->net_rbio == net_rbio)
38b051a1 284 return;
38b051a1 285
d1ac77b1 286 if (qc->ch != NULL && !ossl_quic_channel_set_net_rbio(qc->ch, net_rbio))
22d53c88
HL
287 return;
288
d1ac77b1 289 BIO_free(qc->net_rbio);
22d53c88
HL
290 qc->net_rbio = net_rbio;
291
292 /*
293 * If what we have is not pollable (e.g. a BIO_dgram_pair) disable blocking
294 * mode as we do not support it for non-pollable BIOs.
295 */
296 if (net_rbio != NULL) {
297 BIO_POLL_DESCRIPTOR d = {0};
298
299 if (!BIO_get_rpoll_descriptor(net_rbio, &d)
300 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
301 qc->blocking = 0;
302 qc->can_poll_net_rbio = 0;
303 } else {
304 qc->can_poll_net_rbio = 1;
305 }
306 }
99e1cc7b
TM
307}
308
22d53c88 309void ossl_quic_conn_set0_net_wbio(QUIC_CONNECTION *qc, BIO *net_wbio)
38b051a1 310{
22d53c88
HL
311 if (qc->net_wbio == net_wbio)
312 return;
313
d1ac77b1 314 if (qc->ch != NULL && !ossl_quic_channel_set_net_wbio(qc->ch, net_wbio))
22d53c88
HL
315 return;
316
d1ac77b1 317 BIO_free(qc->net_wbio);
22d53c88
HL
318 qc->net_wbio = net_wbio;
319
320 if (net_wbio != NULL) {
321 BIO_POLL_DESCRIPTOR d = {0};
38b051a1 322
22d53c88
HL
323 if (!BIO_get_wpoll_descriptor(net_wbio, &d)
324 || d.type != BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD) {
325 qc->blocking = 0;
326 qc->can_poll_net_wbio = 0;
327 } else {
328 qc->can_poll_net_wbio = 1;
329 }
38b051a1 330
22d53c88
HL
331 /*
332 * If we do not have a peer address yet, and we have not started trying
333 * to connect yet, try to autodetect one.
334 */
335 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC
336 && !qc->started) {
337 if (!csm_analyse_init_peer_addr(net_wbio, &qc->init_peer_addr))
338 /* best effort */
339 BIO_ADDR_clear(&qc->init_peer_addr);
340
341 if (qc->ch != NULL)
342 ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr);
343 }
38b051a1 344 }
22d53c88 345}
38b051a1 346
22d53c88
HL
347BIO *ossl_quic_conn_get_net_rbio(const QUIC_CONNECTION *qc)
348{
349 return qc->net_rbio;
38b051a1
TM
350}
351
22d53c88
HL
352BIO *ossl_quic_conn_get_net_wbio(const QUIC_CONNECTION *qc)
353{
354 return qc->net_wbio;
355}
356
357int ossl_quic_conn_get_blocking_mode(const QUIC_CONNECTION *qc)
99e1cc7b 358{
22d53c88
HL
359 return qc->blocking;
360}
361
362int ossl_quic_conn_set_blocking_mode(QUIC_CONNECTION *qc, int blocking)
363{
364 /* Cannot enable blocking mode if we do not have pollable FDs. */
365 if (blocking != 0 &&
366 (!qc->can_poll_net_rbio || !qc->can_poll_net_wbio))
367 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_UNSUPPORTED, NULL);
368
369 qc->blocking = (blocking != 0);
99e1cc7b
TM
370 return 1;
371}
372
22d53c88
HL
373int ossl_quic_conn_set_initial_peer_addr(QUIC_CONNECTION *qc,
374 const BIO_ADDR *peer_addr)
99e1cc7b 375{
22d53c88
HL
376 if (qc->started)
377 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
378 NULL);
38b051a1 379
22d53c88
HL
380 if (peer_addr == NULL) {
381 BIO_ADDR_clear(&qc->init_peer_addr);
382 return 1;
383 }
38b051a1 384
22d53c88 385 qc->init_peer_addr = *peer_addr;
99e1cc7b
TM
386 return 1;
387}
388
22d53c88
HL
389/*
390 * QUIC Front-End I/O API: Asynchronous I/O Management
391 * ===================================================
392 *
393 * (BIO/)SSL_tick => ossl_quic_tick
394 * (BIO/)SSL_get_tick_timeout => ossl_quic_get_tick_timeout
395 * (BIO/)SSL_get_poll_fd => ossl_quic_get_poll_fd
396 *
397 */
398
399/* Returns 1 if the connection is being used in blocking mode. */
400static int blocking_mode(const QUIC_CONNECTION *qc)
99e1cc7b 401{
22d53c88
HL
402 return qc->blocking;
403}
38b051a1 404
22d53c88
HL
405/* SSL_tick; ticks the reactor. */
406int ossl_quic_tick(QUIC_CONNECTION *qc)
407{
408 if (qc->ch == NULL)
409 return 1;
38b051a1 410
22d53c88 411 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch));
99e1cc7b
TM
412 return 1;
413}
414
22d53c88
HL
415/*
416 * SSL_get_tick_timeout. Get the time in milliseconds until the SSL object
417 * should be ticked by the application by calling SSL_tick(). tv is set to 0 if
418 * the object should be ticked immediately and tv->tv_sec is set to -1 if no
419 * timeout is currently active.
420 */
421int ossl_quic_get_tick_timeout(QUIC_CONNECTION *qc, struct timeval *tv)
99e1cc7b 422{
a1660c94 423 OSSL_TIME deadline = ossl_time_infinite();
e44795bd 424
22d53c88
HL
425 if (qc->ch != NULL)
426 deadline
427 = ossl_quic_reactor_get_tick_deadline(ossl_quic_channel_get_reactor(qc->ch));
428
429 if (ossl_time_is_infinite(deadline)) {
430 tv->tv_sec = -1;
431 tv->tv_usec = 0;
432 return 1;
433 }
434
a1660c94 435 *tv = ossl_time_to_timeval(ossl_time_subtract(deadline, ossl_time_now()));
22d53c88
HL
436 return 1;
437}
438
439/* SSL_get_rpoll_descriptor */
440int ossl_quic_get_rpoll_descriptor(QUIC_CONNECTION *qc, BIO_POLL_DESCRIPTOR *desc)
441{
442 if (desc == NULL || qc->net_rbio == NULL)
e44795bd
TM
443 return 0;
444
22d53c88 445 return BIO_get_rpoll_descriptor(qc->net_rbio, desc);
99e1cc7b
TM
446}
447
22d53c88
HL
448/* SSL_get_wpoll_descriptor */
449int ossl_quic_get_wpoll_descriptor(QUIC_CONNECTION *qc, BIO_POLL_DESCRIPTOR *desc)
99e1cc7b 450{
22d53c88
HL
451 if (desc == NULL || qc->net_wbio == NULL)
452 return 0;
453
454 return BIO_get_wpoll_descriptor(qc->net_wbio, desc);
99e1cc7b
TM
455}
456
b639475a
HL
457/* SSL_net_read_desired */
458int ossl_quic_get_net_read_desired(QUIC_CONNECTION *qc)
99e1cc7b 459{
22d53c88
HL
460 if (qc->ch == NULL)
461 return 0;
462
b639475a 463 return ossl_quic_reactor_net_read_desired(ossl_quic_channel_get_reactor(qc->ch));
22d53c88 464}
e44795bd 465
b639475a
HL
466/* SSL_net_write_desired */
467int ossl_quic_get_net_write_desired(QUIC_CONNECTION *qc)
22d53c88
HL
468{
469 if (qc->ch == NULL)
e44795bd
TM
470 return 0;
471
b639475a 472 return ossl_quic_reactor_net_write_desired(ossl_quic_channel_get_reactor(qc->ch));
99e1cc7b
TM
473}
474
22d53c88
HL
475/*
476 * QUIC Front-End I/O API: Connection Lifecycle Operations
477 * =======================================================
478 *
479 * SSL_do_handshake => ossl_quic_do_handshake
480 * SSL_set_connect_state => ossl_quic_set_connect_state
481 * SSL_set_accept_state => ossl_quic_set_accept_state
482 * SSL_shutdown => ossl_quic_shutdown
483 * SSL_ctrl => ossl_quic_ctrl
484 * (BIO/)SSL_connect => ossl_quic_connect
485 * (BIO/)SSL_accept => ossl_quic_accept
486 *
487 */
488
489/* SSL_shutdown */
e8043229 490static int quic_shutdown_wait(void *arg)
99e1cc7b 491{
e8043229 492 QUIC_CONNECTION *qc = arg;
22d53c88 493
e8043229
HL
494 return qc->ch == NULL || ossl_quic_channel_is_terminated(qc->ch);
495}
22d53c88 496
e8043229
HL
497int ossl_quic_conn_shutdown(QUIC_CONNECTION *qc, uint64_t flags,
498 const SSL_SHUTDOWN_EX_ARGS *args,
499 size_t args_len)
500{
501 if (!ensure_channel(qc))
502 return -1;
22d53c88 503
e8043229
HL
504 ossl_quic_channel_local_close(qc->ch,
505 args != NULL ? args->quic_error_code : 0);
506
1d40b151 507 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
e8043229
HL
508
509 if (ossl_quic_channel_is_terminated(qc->ch))
510 return 1;
511
512 if (blocking_mode(qc) && (flags & SSL_SHUTDOWN_FLAG_RAPID) == 0)
4e15b448 513 block_until_pred(qc, quic_shutdown_wait, qc, 0);
e8043229
HL
514 else
515 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch));
516
517 return ossl_quic_channel_is_terminated(qc->ch);
99e1cc7b
TM
518}
519
22d53c88 520/* SSL_ctrl */
e44795bd 521long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
99e1cc7b 522{
22d53c88 523 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
38b051a1 524
22d53c88 525 if (!expect_quic_conn(qc))
38b051a1
TM
526 return 0;
527
22d53c88
HL
528 switch (cmd) {
529 case SSL_CTRL_MODE:
dfc227bd
HL
530 /* Cannot enable EPW while AON write in progress. */
531 if (qc->aon_write_in_progress)
532 larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
533
22d53c88 534 qc->ssl_mode |= (uint32_t)larg;
22d53c88
HL
535 return qc->ssl_mode;
536 case SSL_CTRL_CLEAR_MODE:
537 qc->ssl_mode &= ~(uint32_t)larg;
22d53c88
HL
538 return qc->ssl_mode;
539 default:
f8ffab0d
MC
540 /* Probably a TLS related ctrl. Defer to our internal SSL object */
541 return SSL_ctrl(qc->tls, cmd, larg, parg);
08e49012 542 }
22d53c88
HL
543}
544
545/* SSL_set_connect_state */
546void ossl_quic_set_connect_state(QUIC_CONNECTION *qc)
547{
548 /* Cannot be changed after handshake started */
549 if (qc->started)
550 return;
551
552 qc->as_server = 0;
553}
554
555/* SSL_set_accept_state */
556void ossl_quic_set_accept_state(QUIC_CONNECTION *qc)
557{
558 /* Cannot be changed after handshake started */
559 if (qc->started)
560 return;
561
562 qc->as_server = 1;
563}
564
565/* SSL_do_handshake */
566struct quic_handshake_wait_args {
567 QUIC_CONNECTION *qc;
568};
569
570static int quic_handshake_wait(void *arg)
571{
572 struct quic_handshake_wait_args *args = arg;
573
574 if (!ossl_quic_channel_is_active(args->qc->ch))
575 return -1;
576
577 if (ossl_quic_channel_is_handshake_complete(args->qc->ch))
578 return 1;
579
99e1cc7b
TM
580 return 0;
581}
582
22d53c88 583static int configure_channel(QUIC_CONNECTION *qc)
99e1cc7b 584{
22d53c88 585 assert(qc->ch != NULL);
08e49012 586
d1ac77b1
HL
587 if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio)
588 || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio)
22d53c88
HL
589 || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr))
590 return 0;
591
592 return 1;
593}
594
e8043229 595static int ensure_channel(QUIC_CONNECTION *qc)
22d53c88
HL
596{
597 QUIC_CHANNEL_ARGS args = {0};
598
599 if (qc->ch != NULL)
08e49012 600 return 1;
22d53c88
HL
601
602 args.libctx = qc->ssl.ctx->libctx;
603 args.propq = qc->ssl.ctx->propq;
604 args.is_server = 0;
2723d705 605 args.tls = qc->tls;
22d53c88
HL
606
607 qc->ch = ossl_quic_channel_new(&args);
608 if (qc->ch == NULL)
609 return 0;
610
e8043229
HL
611 return 1;
612}
613
614/*
615 * Creates a channel and configures it with the information we have accumulated
616 * via calls made to us from the application prior to starting a handshake
617 * attempt.
618 */
619static int ensure_channel_and_start(QUIC_CONNECTION *qc)
620{
621 if (!ensure_channel(qc))
622 return 0;
623
22d53c88
HL
624 if (!configure_channel(qc)
625 || !ossl_quic_channel_start(qc->ch)) {
626 ossl_quic_channel_free(qc->ch);
627 qc->ch = NULL;
628 return 0;
08e49012 629 }
22d53c88
HL
630
631 qc->stream0 = ossl_quic_channel_get_stream_by_id(qc->ch, 0);
632 if (qc->stream0 == NULL) {
633 ossl_quic_channel_free(qc->ch);
634 qc->ch = NULL;
635 return 0;
636 }
637
638 qc->started = 1;
639 return 1;
99e1cc7b
TM
640}
641
22d53c88 642int ossl_quic_do_handshake(QUIC_CONNECTION *qc)
99e1cc7b 643{
22d53c88
HL
644 int ret;
645
ca41f6b7
HL
646 if (qc->ch != NULL && ossl_quic_channel_is_handshake_complete(qc->ch))
647 /* Handshake already completed. */
648 return 1;
649
c12e1113 650 if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch))
22d53c88
HL
651 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
652
ca41f6b7 653 if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) {
22d53c88 654 /* Peer address must have been set. */
44a1ac5d 655 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_REMOTE_PEER_ADDRESS_NOT_SET, NULL);
ca41f6b7
HL
656 return -1; /* Non-protocol error */
657 }
22d53c88 658
ca41f6b7 659 if (qc->as_server) {
22d53c88 660 /* TODO(QUIC): Server mode not currently supported */
ca41f6b7
HL
661 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL);
662 return -1; /* Non-protocol error */
663 }
22d53c88 664
ca41f6b7 665 if (qc->net_rbio == NULL || qc->net_wbio == NULL) {
22d53c88 666 /* Need read and write BIOs. */
44a1ac5d 667 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BIO_NOT_SET, NULL);
ca41f6b7
HL
668 return -1; /* Non-protocol error */
669 }
22d53c88
HL
670
671 /*
672 * Start connection process. Note we may come here multiple times in
673 * non-blocking mode, which is fine.
674 */
ca41f6b7
HL
675 if (!ensure_channel_and_start(qc)) {
676 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
677 return -1; /* Non-protocol error */
678 }
22d53c88
HL
679
680 if (ossl_quic_channel_is_handshake_complete(qc->ch))
681 /* The handshake is now done. */
682 return 1;
683
684 if (blocking_mode(qc)) {
685 /* In blocking mode, wait for the handshake to complete. */
686 struct quic_handshake_wait_args args;
687
688 args.qc = qc;
689
690 ret = block_until_pred(qc, quic_handshake_wait, &args, 0);
ca41f6b7
HL
691 if (!ossl_quic_channel_is_active(qc->ch)) {
692 QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
693 return 0; /* Shutdown before completion */
694 } else if (ret <= 0) {
695 QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
696 return -1; /* Non-protocol error */
697 }
22d53c88
HL
698
699 assert(ossl_quic_channel_is_handshake_complete(qc->ch));
700 return 1;
701 } else {
ca41f6b7
HL
702 /* Try to advance the reactor. */
703 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch));
704
705 if (ossl_quic_channel_is_handshake_complete(qc->ch))
706 /* The handshake is now done. */
707 return 1;
708
22d53c88 709 /* Otherwise, indicate that the handshake isn't done yet. */
ca41f6b7
HL
710 QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
711 return -1; /* Non-protocol error */
22d53c88 712 }
99e1cc7b
TM
713}
714
22d53c88
HL
715/* SSL_connect */
716int ossl_quic_connect(SSL *s)
99e1cc7b 717{
22d53c88
HL
718 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
719
720 if (!expect_quic_conn(qc))
721 return 0;
722
723 /* Ensure we are in connect state (no-op if non-idle). */
724 ossl_quic_set_connect_state(qc);
725
726 /* Begin or continue the handshake */
727 return ossl_quic_do_handshake(qc);
99e1cc7b
TM
728}
729
22d53c88
HL
730/* SSL_accept */
731int ossl_quic_accept(SSL *s)
99e1cc7b 732{
22d53c88
HL
733 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
734
735 if (!expect_quic_conn(qc))
736 return 0;
737
738 /* Ensure we are in accept state (no-op if non-idle). */
739 ossl_quic_set_accept_state(qc);
740
741 /* Begin or continue the handshake */
742 return ossl_quic_do_handshake(qc);
99e1cc7b 743}
e44795bd 744
22d53c88
HL
745/*
746 * QUIC Front-End I/O API: Steady-State Operations
747 * ===============================================
748 *
749 * Here we dispatch calls to the steady-state front-end I/O API functions; that
750 * is, the functions used during the established phase of a QUIC connection
751 * (e.g. SSL_read, SSL_write).
752 *
753 * Each function must handle both blocking and non-blocking modes. As discussed
754 * above, all QUIC I/O is implemented using non-blocking mode internally.
755 *
756 * SSL_get_error => partially implemented by ossl_quic_get_error
757 * (BIO/)SSL_read => ossl_quic_read
758 * (BIO/)SSL_write => ossl_quic_write
759 * SSL_pending => ossl_quic_pending
a9979965 760 * SSL_stream_conclude => ossl_quic_conn_stream_conclude
22d53c88
HL
761 */
762
763/* SSL_get_error */
764int ossl_quic_get_error(const QUIC_CONNECTION *qc, int i)
e44795bd 765{
22d53c88 766 return qc->last_error;
e44795bd
TM
767}
768
22d53c88
HL
769/*
770 * SSL_write
771 * ---------
772 *
773 * The set of functions below provide the implementation of the public SSL_write
774 * function. We must handle:
775 *
776 * - both blocking and non-blocking operation at the application level,
777 * depending on how we are configured;
778 *
779 * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off;
780 *
781 * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER.
782 *
783 */
784static void quic_post_write(QUIC_CONNECTION *qc, int did_append, int do_tick)
785{
786 /*
787 * We have appended at least one byte to the stream.
788 * Potentially mark stream as active, depending on FC.
789 */
790 if (did_append)
791 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
792 qc->stream0);
793
794 /*
795 * Try and send.
796 *
797 * TODO(QUIC): It is probably inefficient to try and do this immediately,
798 * plus we should eventually consider Nagle's algorithm.
799 */
800 if (do_tick)
801 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch));
802}
803
804struct quic_write_again_args {
805 QUIC_CONNECTION *qc;
806 const unsigned char *buf;
807 size_t len;
808 size_t total_written;
809};
810
811static int quic_write_again(void *arg)
812{
813 struct quic_write_again_args *args = arg;
814 size_t actual_written = 0;
815
816 if (!ossl_quic_channel_is_active(args->qc->ch))
817 /* If connection is torn down due to an error while blocking, stop. */
818 return -2;
819
820 if (!ossl_quic_sstream_append(args->qc->stream0->sstream,
821 args->buf, args->len, &actual_written))
822 return -2;
823
824 quic_post_write(args->qc, actual_written > 0, 0);
825
826 args->buf += actual_written;
827 args->len -= actual_written;
828 args->total_written += actual_written;
829
3f0c310b 830 if (args->len == 0)
22d53c88
HL
831 /* Written everything, done. */
832 return 1;
833
834 /* Not written everything yet, keep trying. */
835 return 0;
836}
837
838static int quic_write_blocking(QUIC_CONNECTION *qc, const void *buf, size_t len,
839 size_t *written)
e44795bd 840{
22d53c88
HL
841 int res;
842 struct quic_write_again_args args;
843 size_t actual_written = 0;
844
845 /* First make a best effort to append as much of the data as possible. */
846 if (!ossl_quic_sstream_append(qc->stream0->sstream, buf, len,
847 &actual_written)) {
848 /* Stream already finished or allocation error. */
849 *written = 0;
850 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
851 }
852
853 quic_post_write(qc, actual_written > 0, 1);
854
855 if (actual_written == len) {
856 /* Managed to append everything on the first try. */
857 *written = actual_written;
858 return 1;
859 }
860
861 /*
862 * We did not manage to append all of the data immediately, so the stream
863 * buffer has probably filled up. This means we need to block until some of
864 * it is freed up.
865 */
866 args.qc = qc;
867 args.buf = (const unsigned char *)buf + actual_written;
868 args.len = len - actual_written;
869 args.total_written = 0;
870
871 res = block_until_pred(qc, quic_write_again, &args, 0);
872 if (res <= 0) {
873 if (!ossl_quic_channel_is_active(qc->ch))
874 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
875 else
876 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
877 }
878
879 *written = args.total_written;
e44795bd
TM
880 return 1;
881}
882
ca41f6b7
HL
883/*
884 * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE)
885 * write semantics.
886 */
22d53c88
HL
887static void aon_write_begin(QUIC_CONNECTION *qc, const unsigned char *buf,
888 size_t buf_len, size_t already_sent)
889{
890 assert(!qc->aon_write_in_progress);
891
892 qc->aon_write_in_progress = 1;
893 qc->aon_buf_base = buf;
894 qc->aon_buf_pos = already_sent;
895 qc->aon_buf_len = buf_len;
896}
897
898static void aon_write_finish(QUIC_CONNECTION *qc)
899{
900 qc->aon_write_in_progress = 0;
901 qc->aon_buf_base = NULL;
902 qc->aon_buf_pos = 0;
903 qc->aon_buf_len = 0;
904}
905
906static int quic_write_nonblocking_aon(QUIC_CONNECTION *qc, const void *buf,
907 size_t len, size_t *written)
e44795bd 908{
22d53c88
HL
909 const void *actual_buf;
910 size_t actual_len, actual_written = 0;
911 int accept_moving_buffer
912 = ((qc->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0);
913
914 if (qc->aon_write_in_progress) {
915 /*
916 * We are in the middle of an AON write (i.e., a previous write did not
75b2920a
HL
917 * manage to append all data to the SSTREAM and we have Enable Partial
918 * Write (EPW) mode disabled.)
22d53c88
HL
919 */
920 if ((!accept_moving_buffer && qc->aon_buf_base != buf)
921 || len != qc->aon_buf_len)
922 /*
923 * Pointer must not have changed if we are not in accept moving
924 * buffer mode. Length must never change.
925 */
926 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BAD_WRITE_RETRY, NULL);
927
928 actual_buf = (unsigned char *)buf + qc->aon_buf_pos;
929 actual_len = len - qc->aon_buf_pos;
930 assert(actual_len > 0);
931 } else {
932 actual_buf = buf;
933 actual_len = len;
934 }
935
936 /* First make a best effort to append as much of the data as possible. */
937 if (!ossl_quic_sstream_append(qc->stream0->sstream, actual_buf, actual_len,
938 &actual_written)) {
939 /* Stream already finished or allocation error. */
940 *written = 0;
941 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
942 }
943
944 quic_post_write(qc, actual_written > 0, 1);
945
946 if (actual_written == actual_len) {
947 /* We have sent everything. */
948 if (qc->aon_write_in_progress) {
949 /*
950 * We have sent everything, and we were in the middle of an AON
951 * write. The output write length is the total length of the AON
952 * buffer, not however many bytes we managed to write to the stream
953 * in this call.
954 */
955 *written = qc->aon_buf_len;
956 aon_write_finish(qc);
957 } else {
958 *written = actual_written;
959 }
960
961 return 1;
962 }
963
964 if (qc->aon_write_in_progress) {
965 /*
966 * AON write is in progress but we have not written everything yet. We
967 * may have managed to send zero bytes, or some number of bytes less
968 * than the total remaining which need to be appended during this
969 * AON operation.
970 */
971 qc->aon_buf_pos += actual_written;
972 assert(qc->aon_buf_pos < qc->aon_buf_len);
973 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_WRITE);
974 }
975
08e49012 976 /*
22d53c88
HL
977 * Not in an existing AON operation but partial write is not enabled, so we
978 * need to begin a new AON operation. However we needn't bother if we didn't
979 * actually append anything.
08e49012 980 */
22d53c88
HL
981 if (actual_written > 0)
982 aon_write_begin(qc, buf, len, actual_written);
e44795bd 983
22d53c88
HL
984 /*
985 * AON - We do not publicly admit to having appended anything until AON
986 * completes.
987 */
988 *written = 0;
989 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_WRITE);
e44795bd
TM
990}
991
22d53c88
HL
992static int quic_write_nonblocking_epw(QUIC_CONNECTION *qc, const void *buf, size_t len,
993 size_t *written)
e44795bd 994{
22d53c88
HL
995 /* Simple best effort operation. */
996 if (!ossl_quic_sstream_append(qc->stream0->sstream, buf, len, written)) {
997 /* Stream already finished or allocation error. */
998 *written = 0;
999 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1000 }
1001
1002 quic_post_write(qc, *written > 0, 1);
e44795bd
TM
1003 return 1;
1004}
d5ab48a1 1005
22d53c88 1006int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
d5ab48a1 1007{
22d53c88
HL
1008 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
1009 int partial_write = ((qc->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0);
1010
1011 *written = 0;
1012
1013 if (!expect_quic_conn(qc))
1014 return 0;
1015
c12e1113 1016 if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch))
22d53c88
HL
1017 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1018
ca41f6b7
HL
1019 /*
1020 * If we haven't finished the handshake, try to advance it.
1021 * We don't accept writes until the handshake is completed.
1022 */
1023 if (ossl_quic_do_handshake(qc) < 1)
1024 return 0;
1025
22d53c88
HL
1026 if (qc->stream0 == NULL || qc->stream0->sstream == NULL)
1027 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1028
1029 if (blocking_mode(qc))
1030 return quic_write_blocking(qc, buf, len, written);
1031 else if (partial_write)
1032 return quic_write_nonblocking_epw(qc, buf, len, written);
1033 else
1034 return quic_write_nonblocking_aon(qc, buf, len, written);
d5ab48a1
RL
1035}
1036
1037/*
22d53c88
HL
1038 * SSL_read
1039 * --------
d5ab48a1 1040 */
22d53c88
HL
1041struct quic_read_again_args {
1042 QUIC_CONNECTION *qc;
1043 QUIC_STREAM *stream;
1044 void *buf;
1045 size_t len;
1046 size_t *bytes_read;
1047 int peek;
1048};
1049
1050static int quic_read_actual(QUIC_CONNECTION *qc,
1051 QUIC_STREAM *stream,
1052 void *buf, size_t buf_len,
1053 size_t *bytes_read,
1054 int peek)
d5ab48a1 1055{
22d53c88
HL
1056 int is_fin = 0;
1057
a9979965
HL
1058 /* If the receive part of the stream is over, issue EOF. */
1059 if (stream->recv_fin_retired)
1060 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_ZERO_RETURN);
1061
22d53c88
HL
1062 if (stream->rstream == NULL)
1063 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1064
1065 if (peek) {
1066 if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len,
1067 bytes_read, &is_fin))
1068 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1069
1070 } else {
1071 if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len,
1072 bytes_read, &is_fin))
1073 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1074 }
1075
1076 if (!peek) {
1077 if (*bytes_read > 0) {
1078 /*
1079 * We have read at least one byte from the stream. Inform stream-level
1080 * RXFC of the retirement of controlled bytes. Update the active stream
1081 * status (the RXFC may now want to emit a frame granting more credit to
1082 * the peer).
1083 */
1084 OSSL_RTT_INFO rtt_info;
d50e750e 1085
22d53c88
HL
1086 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info);
1087
1088 if (!ossl_quic_rxfc_on_retire(&qc->stream0->rxfc, *bytes_read,
1089 rtt_info.smoothed_rtt))
1090 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1091 }
1092
1093 if (is_fin)
1094 stream->recv_fin_retired = 1;
1095
1096 if (*bytes_read > 0)
1097 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch),
1098 qc->stream0);
1099 }
1100
d5ab48a1
RL
1101 return 1;
1102}
1103
22d53c88 1104static int quic_read_again(void *arg)
d5ab48a1 1105{
22d53c88
HL
1106 struct quic_read_again_args *args = arg;
1107
a9979965 1108 if (!ossl_quic_channel_is_active(args->qc->ch)) {
22d53c88 1109 /* If connection is torn down due to an error while blocking, stop. */
a9979965 1110 QUIC_RAISE_NON_NORMAL_ERROR(args->qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
ca41f6b7 1111 return -1;
a9979965 1112 }
22d53c88
HL
1113
1114 if (!quic_read_actual(args->qc, args->stream,
1115 args->buf, args->len, args->bytes_read,
1116 args->peek))
1117 return -1;
1118
1119 if (*args->bytes_read > 0)
1120 /* got at least one byte, the SSL_read op can finish now */
1121 return 1;
1122
81b6b43c 1123 return 0; /* did not read anything, keep trying */
d5ab48a1
RL
1124}
1125
22d53c88 1126static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek)
d5ab48a1 1127{
22d53c88
HL
1128 int res;
1129 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
1130 struct quic_read_again_args args;
1131
1132 *bytes_read = 0;
1133
1134 if (!expect_quic_conn(qc))
1135 return 0;
1136
c12e1113 1137 if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch))
22d53c88
HL
1138 return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL);
1139
a9979965 1140 /* If we haven't finished the handshake, try to advance it. */
ca41f6b7 1141 if (ossl_quic_do_handshake(qc) < 1)
a9979965 1142 return 0; /* ossl_quic_do_handshake raised error here */
22d53c88
HL
1143
1144 if (qc->stream0 == NULL)
1145 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1146
1147 if (!quic_read_actual(qc, qc->stream0, buf, len, bytes_read, peek))
a9979965 1148 return 0; /* quic_read_actual raised error here */
22d53c88
HL
1149
1150 if (*bytes_read > 0) {
1151 /*
1152 * Even though we succeeded, tick the reactor here to ensure we are
1153 * handling other aspects of the QUIC connection.
1154 */
1155 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch));
1156 return 1;
1157 } else if (blocking_mode(qc)) {
1158 /*
1159 * We were not able to read anything immediately, so our stream
1160 * buffer is empty. This means we need to block until we get
1161 * at least one byte.
1162 */
1163 args.qc = qc;
1164 args.stream = qc->stream0;
1165 args.buf = buf;
1166 args.len = len;
1167 args.bytes_read = bytes_read;
1168 args.peek = peek;
1169
1170 res = block_until_pred(qc, quic_read_again, &args, 0);
a9979965
HL
1171 if (res == 0)
1172 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL);
1173 else if (res < 0)
1174 return 0; /* quic_read_again raised error here */
22d53c88 1175
af8b52cf
HL
1176 return 1;
1177 } else {
1178 /* We did not get any bytes and are not in blocking mode. */
1179 return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ);
1180 }
d5ab48a1
RL
1181}
1182
22d53c88
HL
1183int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read)
1184{
1185 return quic_read(s, buf, len, bytes_read, 0);
1186}
1187
1188int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read)
1189{
1190 return quic_read(s, buf, len, bytes_read, 1);
1191}
1192
1193/*
1194 * SSL_pending
1195 * -----------
1196 */
560470b5 1197static size_t ossl_quic_pending_int(const QUIC_CONNECTION *qc)
22d53c88 1198{
22d53c88
HL
1199 size_t avail = 0;
1200 int fin = 0;
1201
1202 if (!expect_quic_conn(qc))
1203 return 0;
1204
1205 if (qc->stream0 == NULL || qc->stream0->rstream == NULL)
1206 /* Cannot raise errors here because we are const, just fail. */
1207 return 0;
1208
1209 if (!ossl_quic_rstream_available(qc->stream0->rstream, &avail, &fin))
1210 return 0;
1211
1212 return avail;
1213}
1214
560470b5
MC
1215size_t ossl_quic_pending(const SSL *s)
1216{
1217 const QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_CONST_SSL(s);
1218
1219 return ossl_quic_pending_int(qc);
1220}
1221
1222int ossl_quic_has_pending(const QUIC_CONNECTION *qc)
1223{
1224 return ossl_quic_pending_int(qc) > 0;
1225}
1226
a9979965
HL
1227/*
1228 * SSL_stream_conclude
1229 * -------------------
1230 */
1231int ossl_quic_conn_stream_conclude(QUIC_CONNECTION *qc)
1232{
1233 QUIC_STREAM *qs = qc->stream0;
1234
1235 if (qs == NULL || qs->sstream == NULL)
1236 return 0;
1237
1238 if (!ossl_quic_channel_is_active(qc->ch)
1239 || ossl_quic_sstream_get_final_size(qs->sstream, NULL))
1240 return 1;
1241
1242 ossl_quic_sstream_fin(qs->sstream);
1243 quic_post_write(qc, 1, 1);
1244 return 1;
1245}
1246
553a4e00
HL
1247/*
1248 * SSL_inject_net_dgram
1249 * --------------------
1250 */
1251int SSL_inject_net_dgram(SSL *s, const unsigned char *buf,
1252 size_t buf_len,
1253 const BIO_ADDR *peer,
1254 const BIO_ADDR *local)
1255{
1256 QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s);
1257 QUIC_DEMUX *demux;
1258
1259 if (!expect_quic_conn(qc))
1260 return 0;
1261
1262 if (qc->ch == NULL)
1263 return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
1264 NULL);
1265
1266 demux = ossl_quic_channel_get0_demux(qc->ch);
1267 return ossl_quic_demux_inject(demux, buf, buf_len, peer, local);
1268}
1269
22d53c88
HL
1270/*
1271 * QUIC Front-End I/O API: SSL_CTX Management
1272 * ==========================================
1273 */
1274
1275long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1276{
1277 switch (cmd) {
1278 default:
8a1a6d6d 1279 return ssl3_ctx_ctrl(ctx, cmd, larg, parg);
22d53c88
HL
1280 }
1281}
1282
1283long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
1284{
8a1a6d6d 1285 return ssl3_callback_ctrl(s, cmd, fp);
22d53c88
HL
1286}
1287
1288long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
1289{
8a1a6d6d 1290 return ssl3_ctx_callback_ctrl(ctx, cmd, fp);
22d53c88
HL
1291}
1292
1293int ossl_quic_renegotiate_check(SSL *ssl, int initok)
1294{
1295 /* We never do renegotiation. */
1296 return 0;
1297}
1298
1299/*
d518854c
MC
1300 * These functions define the TLSv1.2 (and below) ciphers that are supported by
1301 * the SSL_METHOD. Since QUIC only supports TLSv1.3 we don't support any.
22d53c88 1302 */
22d53c88
HL
1303
1304int ossl_quic_num_ciphers(void)
1305{
d518854c 1306 return 0;
22d53c88
HL
1307}
1308
1309const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
1310{
d518854c 1311 return NULL;
d5ab48a1 1312}