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