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