]>
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" |
22d53c88 HL |
15 | #include "internal/quic_dummy_handshake.h" |
16 | #include "internal/quic_rx_depack.h" | |
17 | #include "internal/quic_error.h" | |
18 | #include "internal/time.h" | |
99e1cc7b | 19 | |
22d53c88 HL |
20 | static void aon_write_finish(QUIC_CONNECTION *qc); |
21 | ||
22 | /* | |
23 | * QUIC Front-End I/O API: Common Utilities | |
24 | * ======================================== | |
25 | */ | |
26 | ||
27 | /* | |
28 | * Block until a predicate is met. | |
29 | * | |
30 | * Precondition: Must have a channel. | |
31 | */ | |
32 | static int block_until_pred(QUIC_CONNECTION *qc, | |
33 | int (*pred)(void *arg), void *pred_arg, | |
34 | uint32_t flags) | |
35 | { | |
36 | QUIC_REACTOR *rtor; | |
37 | ||
38 | assert(qc->ch != NULL); | |
39 | ||
40 | rtor = ossl_quic_channel_get_reactor(qc->ch); | |
41 | return ossl_quic_reactor_block_until_pred(rtor, pred, pred_arg, flags); | |
42 | } | |
43 | ||
44 | /* | |
45 | * Raise a 'normal' error, meaning one that can be reported via SSL_get_error() | |
46 | * rather than via ERR. | |
47 | */ | |
48 | static int quic_raise_normal_error(QUIC_CONNECTION *qc, | |
49 | int err) | |
50 | { | |
51 | qc->last_error = err; | |
52 | return 0; | |
53 | } | |
54 | ||
55 | /* | |
56 | * Raise a 'non-normal' error, meaning any error that is not reported via | |
57 | * SSL_get_error() and must be reported via ERR. | |
58 | */ | |
59 | static int quic_raise_non_normal_error(QUIC_CONNECTION *qc, | |
60 | const char *file, | |
61 | int line, | |
62 | const char *func, | |
63 | int reason, | |
64 | const char *fmt, | |
65 | ...) | |
66 | { | |
67 | va_list args; | |
68 | ||
69 | ERR_new(); | |
70 | ERR_set_debug(file, line, func); | |
71 | ||
72 | va_start(args, fmt); | |
73 | ERR_vset_error(ERR_LIB_SSL, reason, fmt, args); | |
74 | va_end(args); | |
75 | ||
76 | qc->last_error = SSL_ERROR_SSL; | |
77 | return 0; | |
78 | } | |
79 | ||
80 | #define QUIC_RAISE_NORMAL_ERROR(qc, err) \ | |
81 | quic_raise_normal_error((qc), (err)) | |
82 | ||
83 | #define QUIC_RAISE_NON_NORMAL_ERROR(qc, reason, msg) \ | |
84 | quic_raise_non_normal_error((qc), \ | |
85 | OPENSSL_FILE, OPENSSL_LINE, \ | |
86 | OPENSSL_FUNC, \ | |
87 | (reason), \ | |
88 | (msg)) | |
89 | ||
90 | /* | |
91 | * Should be called at entry of every public function to confirm we have a valid | |
92 | * QUIC_CONNECTION. | |
93 | */ | |
94 | static ossl_inline int expect_quic_conn(const QUIC_CONNECTION *qc) | |
95 | { | |
96 | if (!ossl_assert(qc != NULL)) | |
97 | return QUIC_RAISE_NON_NORMAL_ERROR(NULL, ERR_R_INTERNAL_ERROR, NULL); | |
98 | ||
99 | return 1; | |
100 | ||
101 | } | |
102 | ||
103 | /* | |
104 | * QUIC Front-End I/O API: Initialization | |
105 | * ====================================== | |
106 | * | |
107 | * SSL_new => ossl_quic_new | |
108 | * ossl_quic_init | |
109 | * SSL_reset => ossl_quic_reset | |
110 | * SSL_clear => ossl_quic_clear | |
111 | * ossl_quic_deinit | |
112 | * SSL_free => ossl_quic_free | |
113 | * | |
114 | */ | |
115 | ||
116 | /* SSL_new */ | |
38b051a1 TM |
117 | SSL *ossl_quic_new(SSL_CTX *ctx) |
118 | { | |
22d53c88 HL |
119 | QUIC_CONNECTION *qc = NULL; |
120 | SSL *ssl_base = NULL; | |
a7f41885 | 121 | SSL_CONNECTION *sc = NULL; |
38b051a1 TM |
122 | |
123 | qc = OPENSSL_zalloc(sizeof(*qc)); | |
124 | if (qc == NULL) | |
125 | goto err; | |
126 | ||
22d53c88 HL |
127 | /* Initialise the QUIC_CONNECTION's stub header. */ |
128 | ssl_base = &qc->ssl; | |
a7f41885 | 129 | if (!ossl_ssl_init(ssl_base, ctx, ctx->method, SSL_TYPE_QUIC_CONNECTION)) { |
22d53c88 | 130 | ssl_base = NULL; |
38b051a1 TM |
131 | goto err; |
132 | } | |
38b051a1 | 133 | |
a7f41885 MC |
134 | qc->tls = ossl_ssl_connection_new_int(ctx, TLS_client_method()); |
135 | if (qc->tls == NULL || (sc = SSL_CONNECTION_FROM_SSL(qc->tls)) == NULL) | |
136 | goto err; | |
137 | /* override the user_ssl of the inner connection */ | |
138 | sc->user_ssl = ssl_base; | |
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 */ | |
e44795bd | 491 | int ossl_quic_shutdown(SSL *s) |
99e1cc7b | 492 | { |
22d53c88 HL |
493 | QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s); |
494 | ||
495 | if (!expect_quic_conn(qc)) | |
496 | return 0; | |
497 | ||
498 | if (qc->ch != NULL) | |
499 | ossl_quic_channel_local_close(qc->ch); | |
500 | ||
99e1cc7b TM |
501 | return 1; |
502 | } | |
503 | ||
22d53c88 | 504 | /* SSL_ctrl */ |
e44795bd | 505 | long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg) |
99e1cc7b | 506 | { |
22d53c88 | 507 | QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s); |
38b051a1 | 508 | |
22d53c88 | 509 | if (!expect_quic_conn(qc)) |
38b051a1 TM |
510 | return 0; |
511 | ||
22d53c88 HL |
512 | switch (cmd) { |
513 | case SSL_CTRL_MODE: | |
dfc227bd HL |
514 | /* Cannot enable EPW while AON write in progress. */ |
515 | if (qc->aon_write_in_progress) | |
516 | larg &= ~SSL_MODE_ENABLE_PARTIAL_WRITE; | |
517 | ||
22d53c88 | 518 | qc->ssl_mode |= (uint32_t)larg; |
22d53c88 HL |
519 | return qc->ssl_mode; |
520 | case SSL_CTRL_CLEAR_MODE: | |
521 | qc->ssl_mode &= ~(uint32_t)larg; | |
22d53c88 HL |
522 | return qc->ssl_mode; |
523 | default: | |
524 | return 0; | |
08e49012 | 525 | } |
22d53c88 HL |
526 | } |
527 | ||
528 | /* SSL_set_connect_state */ | |
529 | void ossl_quic_set_connect_state(QUIC_CONNECTION *qc) | |
530 | { | |
531 | /* Cannot be changed after handshake started */ | |
532 | if (qc->started) | |
533 | return; | |
534 | ||
535 | qc->as_server = 0; | |
536 | } | |
537 | ||
538 | /* SSL_set_accept_state */ | |
539 | void ossl_quic_set_accept_state(QUIC_CONNECTION *qc) | |
540 | { | |
541 | /* Cannot be changed after handshake started */ | |
542 | if (qc->started) | |
543 | return; | |
544 | ||
545 | qc->as_server = 1; | |
546 | } | |
547 | ||
548 | /* SSL_do_handshake */ | |
549 | struct quic_handshake_wait_args { | |
550 | QUIC_CONNECTION *qc; | |
551 | }; | |
552 | ||
553 | static int quic_handshake_wait(void *arg) | |
554 | { | |
555 | struct quic_handshake_wait_args *args = arg; | |
556 | ||
557 | if (!ossl_quic_channel_is_active(args->qc->ch)) | |
558 | return -1; | |
559 | ||
560 | if (ossl_quic_channel_is_handshake_complete(args->qc->ch)) | |
561 | return 1; | |
562 | ||
99e1cc7b TM |
563 | return 0; |
564 | } | |
565 | ||
22d53c88 | 566 | static int configure_channel(QUIC_CONNECTION *qc) |
99e1cc7b | 567 | { |
22d53c88 | 568 | assert(qc->ch != NULL); |
08e49012 | 569 | |
d1ac77b1 HL |
570 | if (!ossl_quic_channel_set_net_rbio(qc->ch, qc->net_rbio) |
571 | || !ossl_quic_channel_set_net_wbio(qc->ch, qc->net_wbio) | |
22d53c88 HL |
572 | || !ossl_quic_channel_set_peer_addr(qc->ch, &qc->init_peer_addr)) |
573 | return 0; | |
574 | ||
575 | return 1; | |
576 | } | |
577 | ||
578 | /* | |
579 | * Creates a channel and configures it with the information we have accumulated | |
580 | * via calls made to us from the application prior to starting a handshake | |
581 | * attempt. | |
582 | */ | |
583 | static int ensure_channel_and_start(QUIC_CONNECTION *qc) | |
584 | { | |
585 | QUIC_CHANNEL_ARGS args = {0}; | |
586 | ||
587 | if (qc->ch != NULL) | |
08e49012 | 588 | return 1; |
22d53c88 HL |
589 | |
590 | args.libctx = qc->ssl.ctx->libctx; | |
591 | args.propq = qc->ssl.ctx->propq; | |
592 | args.is_server = 0; | |
593 | ||
594 | qc->ch = ossl_quic_channel_new(&args); | |
595 | if (qc->ch == NULL) | |
596 | return 0; | |
597 | ||
598 | if (!configure_channel(qc) | |
599 | || !ossl_quic_channel_start(qc->ch)) { | |
600 | ossl_quic_channel_free(qc->ch); | |
601 | qc->ch = NULL; | |
602 | return 0; | |
08e49012 | 603 | } |
22d53c88 HL |
604 | |
605 | qc->stream0 = ossl_quic_channel_get_stream_by_id(qc->ch, 0); | |
606 | if (qc->stream0 == NULL) { | |
607 | ossl_quic_channel_free(qc->ch); | |
608 | qc->ch = NULL; | |
609 | return 0; | |
610 | } | |
611 | ||
612 | qc->started = 1; | |
613 | return 1; | |
99e1cc7b TM |
614 | } |
615 | ||
22d53c88 | 616 | int ossl_quic_do_handshake(QUIC_CONNECTION *qc) |
99e1cc7b | 617 | { |
22d53c88 HL |
618 | int ret; |
619 | ||
ca41f6b7 HL |
620 | if (qc->ch != NULL && ossl_quic_channel_is_handshake_complete(qc->ch)) |
621 | /* Handshake already completed. */ | |
622 | return 1; | |
623 | ||
22d53c88 HL |
624 | if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch)) |
625 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
626 | ||
ca41f6b7 | 627 | if (BIO_ADDR_family(&qc->init_peer_addr) == AF_UNSPEC) { |
22d53c88 | 628 | /* Peer address must have been set. */ |
ca41f6b7 HL |
629 | QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL); |
630 | return -1; /* Non-protocol error */ | |
631 | } | |
22d53c88 | 632 | |
ca41f6b7 | 633 | if (qc->as_server) { |
22d53c88 | 634 | /* TODO(QUIC): Server mode not currently supported */ |
ca41f6b7 HL |
635 | QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL); |
636 | return -1; /* Non-protocol error */ | |
637 | } | |
22d53c88 | 638 | |
ca41f6b7 | 639 | if (qc->net_rbio == NULL || qc->net_wbio == NULL) { |
22d53c88 | 640 | /* Need read and write BIOs. */ |
ca41f6b7 HL |
641 | QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_PASSED_INVALID_ARGUMENT, NULL); |
642 | return -1; /* Non-protocol error */ | |
643 | } | |
22d53c88 HL |
644 | |
645 | /* | |
646 | * Start connection process. Note we may come here multiple times in | |
647 | * non-blocking mode, which is fine. | |
648 | */ | |
ca41f6b7 HL |
649 | if (!ensure_channel_and_start(qc)) { |
650 | QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
651 | return -1; /* Non-protocol error */ | |
652 | } | |
22d53c88 HL |
653 | |
654 | if (ossl_quic_channel_is_handshake_complete(qc->ch)) | |
655 | /* The handshake is now done. */ | |
656 | return 1; | |
657 | ||
658 | if (blocking_mode(qc)) { | |
659 | /* In blocking mode, wait for the handshake to complete. */ | |
660 | struct quic_handshake_wait_args args; | |
661 | ||
662 | args.qc = qc; | |
663 | ||
664 | ret = block_until_pred(qc, quic_handshake_wait, &args, 0); | |
ca41f6b7 HL |
665 | if (!ossl_quic_channel_is_active(qc->ch)) { |
666 | QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
667 | return 0; /* Shutdown before completion */ | |
668 | } else if (ret <= 0) { | |
669 | QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
670 | return -1; /* Non-protocol error */ | |
671 | } | |
22d53c88 HL |
672 | |
673 | assert(ossl_quic_channel_is_handshake_complete(qc->ch)); | |
674 | return 1; | |
675 | } else { | |
ca41f6b7 HL |
676 | /* Try to advance the reactor. */ |
677 | ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch)); | |
678 | ||
679 | if (ossl_quic_channel_is_handshake_complete(qc->ch)) | |
680 | /* The handshake is now done. */ | |
681 | return 1; | |
682 | ||
22d53c88 | 683 | /* Otherwise, indicate that the handshake isn't done yet. */ |
ca41f6b7 HL |
684 | QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ); |
685 | return -1; /* Non-protocol error */ | |
22d53c88 | 686 | } |
99e1cc7b TM |
687 | } |
688 | ||
22d53c88 HL |
689 | /* SSL_connect */ |
690 | int ossl_quic_connect(SSL *s) | |
99e1cc7b | 691 | { |
22d53c88 HL |
692 | QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s); |
693 | ||
694 | if (!expect_quic_conn(qc)) | |
695 | return 0; | |
696 | ||
697 | /* Ensure we are in connect state (no-op if non-idle). */ | |
698 | ossl_quic_set_connect_state(qc); | |
699 | ||
700 | /* Begin or continue the handshake */ | |
701 | return ossl_quic_do_handshake(qc); | |
99e1cc7b TM |
702 | } |
703 | ||
22d53c88 HL |
704 | /* SSL_accept */ |
705 | int ossl_quic_accept(SSL *s) | |
99e1cc7b | 706 | { |
22d53c88 HL |
707 | QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s); |
708 | ||
709 | if (!expect_quic_conn(qc)) | |
710 | return 0; | |
711 | ||
712 | /* Ensure we are in accept state (no-op if non-idle). */ | |
713 | ossl_quic_set_accept_state(qc); | |
714 | ||
715 | /* Begin or continue the handshake */ | |
716 | return ossl_quic_do_handshake(qc); | |
99e1cc7b | 717 | } |
e44795bd | 718 | |
22d53c88 HL |
719 | /* |
720 | * QUIC Front-End I/O API: Steady-State Operations | |
721 | * =============================================== | |
722 | * | |
723 | * Here we dispatch calls to the steady-state front-end I/O API functions; that | |
724 | * is, the functions used during the established phase of a QUIC connection | |
725 | * (e.g. SSL_read, SSL_write). | |
726 | * | |
727 | * Each function must handle both blocking and non-blocking modes. As discussed | |
728 | * above, all QUIC I/O is implemented using non-blocking mode internally. | |
729 | * | |
730 | * SSL_get_error => partially implemented by ossl_quic_get_error | |
731 | * (BIO/)SSL_read => ossl_quic_read | |
732 | * (BIO/)SSL_write => ossl_quic_write | |
733 | * SSL_pending => ossl_quic_pending | |
734 | */ | |
735 | ||
736 | /* SSL_get_error */ | |
737 | int ossl_quic_get_error(const QUIC_CONNECTION *qc, int i) | |
e44795bd | 738 | { |
22d53c88 | 739 | return qc->last_error; |
e44795bd TM |
740 | } |
741 | ||
22d53c88 HL |
742 | /* |
743 | * SSL_write | |
744 | * --------- | |
745 | * | |
746 | * The set of functions below provide the implementation of the public SSL_write | |
747 | * function. We must handle: | |
748 | * | |
749 | * - both blocking and non-blocking operation at the application level, | |
750 | * depending on how we are configured; | |
751 | * | |
752 | * - SSL_MODE_ENABLE_PARTIAL_WRITE being on or off; | |
753 | * | |
754 | * - SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER. | |
755 | * | |
756 | */ | |
757 | static void quic_post_write(QUIC_CONNECTION *qc, int did_append, int do_tick) | |
758 | { | |
759 | /* | |
760 | * We have appended at least one byte to the stream. | |
761 | * Potentially mark stream as active, depending on FC. | |
762 | */ | |
763 | if (did_append) | |
764 | ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch), | |
765 | qc->stream0); | |
766 | ||
767 | /* | |
768 | * Try and send. | |
769 | * | |
770 | * TODO(QUIC): It is probably inefficient to try and do this immediately, | |
771 | * plus we should eventually consider Nagle's algorithm. | |
772 | */ | |
773 | if (do_tick) | |
774 | ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch)); | |
775 | } | |
776 | ||
777 | struct quic_write_again_args { | |
778 | QUIC_CONNECTION *qc; | |
779 | const unsigned char *buf; | |
780 | size_t len; | |
781 | size_t total_written; | |
782 | }; | |
783 | ||
784 | static int quic_write_again(void *arg) | |
785 | { | |
786 | struct quic_write_again_args *args = arg; | |
787 | size_t actual_written = 0; | |
788 | ||
789 | if (!ossl_quic_channel_is_active(args->qc->ch)) | |
790 | /* If connection is torn down due to an error while blocking, stop. */ | |
791 | return -2; | |
792 | ||
793 | if (!ossl_quic_sstream_append(args->qc->stream0->sstream, | |
794 | args->buf, args->len, &actual_written)) | |
795 | return -2; | |
796 | ||
797 | quic_post_write(args->qc, actual_written > 0, 0); | |
798 | ||
799 | args->buf += actual_written; | |
800 | args->len -= actual_written; | |
801 | args->total_written += actual_written; | |
802 | ||
3f0c310b | 803 | if (args->len == 0) |
22d53c88 HL |
804 | /* Written everything, done. */ |
805 | return 1; | |
806 | ||
807 | /* Not written everything yet, keep trying. */ | |
808 | return 0; | |
809 | } | |
810 | ||
811 | static int quic_write_blocking(QUIC_CONNECTION *qc, const void *buf, size_t len, | |
812 | size_t *written) | |
e44795bd | 813 | { |
22d53c88 HL |
814 | int res; |
815 | struct quic_write_again_args args; | |
816 | size_t actual_written = 0; | |
817 | ||
818 | /* First make a best effort to append as much of the data as possible. */ | |
819 | if (!ossl_quic_sstream_append(qc->stream0->sstream, buf, len, | |
820 | &actual_written)) { | |
821 | /* Stream already finished or allocation error. */ | |
822 | *written = 0; | |
823 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
824 | } | |
825 | ||
826 | quic_post_write(qc, actual_written > 0, 1); | |
827 | ||
828 | if (actual_written == len) { | |
829 | /* Managed to append everything on the first try. */ | |
830 | *written = actual_written; | |
831 | return 1; | |
832 | } | |
833 | ||
834 | /* | |
835 | * We did not manage to append all of the data immediately, so the stream | |
836 | * buffer has probably filled up. This means we need to block until some of | |
837 | * it is freed up. | |
838 | */ | |
839 | args.qc = qc; | |
840 | args.buf = (const unsigned char *)buf + actual_written; | |
841 | args.len = len - actual_written; | |
842 | args.total_written = 0; | |
843 | ||
844 | res = block_until_pred(qc, quic_write_again, &args, 0); | |
845 | if (res <= 0) { | |
846 | if (!ossl_quic_channel_is_active(qc->ch)) | |
847 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
848 | else | |
849 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
850 | } | |
851 | ||
852 | *written = args.total_written; | |
e44795bd TM |
853 | return 1; |
854 | } | |
855 | ||
ca41f6b7 HL |
856 | /* |
857 | * Functions to manage All-or-Nothing (AON) (that is, non-ENABLE_PARTIAL_WRITE) | |
858 | * write semantics. | |
859 | */ | |
22d53c88 HL |
860 | static void aon_write_begin(QUIC_CONNECTION *qc, const unsigned char *buf, |
861 | size_t buf_len, size_t already_sent) | |
862 | { | |
863 | assert(!qc->aon_write_in_progress); | |
864 | ||
865 | qc->aon_write_in_progress = 1; | |
866 | qc->aon_buf_base = buf; | |
867 | qc->aon_buf_pos = already_sent; | |
868 | qc->aon_buf_len = buf_len; | |
869 | } | |
870 | ||
871 | static void aon_write_finish(QUIC_CONNECTION *qc) | |
872 | { | |
873 | qc->aon_write_in_progress = 0; | |
874 | qc->aon_buf_base = NULL; | |
875 | qc->aon_buf_pos = 0; | |
876 | qc->aon_buf_len = 0; | |
877 | } | |
878 | ||
879 | static int quic_write_nonblocking_aon(QUIC_CONNECTION *qc, const void *buf, | |
880 | size_t len, size_t *written) | |
e44795bd | 881 | { |
22d53c88 HL |
882 | const void *actual_buf; |
883 | size_t actual_len, actual_written = 0; | |
884 | int accept_moving_buffer | |
885 | = ((qc->ssl_mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) != 0); | |
886 | ||
887 | if (qc->aon_write_in_progress) { | |
888 | /* | |
889 | * We are in the middle of an AON write (i.e., a previous write did not | |
75b2920a HL |
890 | * manage to append all data to the SSTREAM and we have Enable Partial |
891 | * Write (EPW) mode disabled.) | |
22d53c88 HL |
892 | */ |
893 | if ((!accept_moving_buffer && qc->aon_buf_base != buf) | |
894 | || len != qc->aon_buf_len) | |
895 | /* | |
896 | * Pointer must not have changed if we are not in accept moving | |
897 | * buffer mode. Length must never change. | |
898 | */ | |
899 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_BAD_WRITE_RETRY, NULL); | |
900 | ||
901 | actual_buf = (unsigned char *)buf + qc->aon_buf_pos; | |
902 | actual_len = len - qc->aon_buf_pos; | |
903 | assert(actual_len > 0); | |
904 | } else { | |
905 | actual_buf = buf; | |
906 | actual_len = len; | |
907 | } | |
908 | ||
909 | /* First make a best effort to append as much of the data as possible. */ | |
910 | if (!ossl_quic_sstream_append(qc->stream0->sstream, actual_buf, actual_len, | |
911 | &actual_written)) { | |
912 | /* Stream already finished or allocation error. */ | |
913 | *written = 0; | |
914 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
915 | } | |
916 | ||
917 | quic_post_write(qc, actual_written > 0, 1); | |
918 | ||
919 | if (actual_written == actual_len) { | |
920 | /* We have sent everything. */ | |
921 | if (qc->aon_write_in_progress) { | |
922 | /* | |
923 | * We have sent everything, and we were in the middle of an AON | |
924 | * write. The output write length is the total length of the AON | |
925 | * buffer, not however many bytes we managed to write to the stream | |
926 | * in this call. | |
927 | */ | |
928 | *written = qc->aon_buf_len; | |
929 | aon_write_finish(qc); | |
930 | } else { | |
931 | *written = actual_written; | |
932 | } | |
933 | ||
934 | return 1; | |
935 | } | |
936 | ||
937 | if (qc->aon_write_in_progress) { | |
938 | /* | |
939 | * AON write is in progress but we have not written everything yet. We | |
940 | * may have managed to send zero bytes, or some number of bytes less | |
941 | * than the total remaining which need to be appended during this | |
942 | * AON operation. | |
943 | */ | |
944 | qc->aon_buf_pos += actual_written; | |
945 | assert(qc->aon_buf_pos < qc->aon_buf_len); | |
946 | return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_WRITE); | |
947 | } | |
948 | ||
08e49012 | 949 | /* |
22d53c88 HL |
950 | * Not in an existing AON operation but partial write is not enabled, so we |
951 | * need to begin a new AON operation. However we needn't bother if we didn't | |
952 | * actually append anything. | |
08e49012 | 953 | */ |
22d53c88 HL |
954 | if (actual_written > 0) |
955 | aon_write_begin(qc, buf, len, actual_written); | |
e44795bd | 956 | |
22d53c88 HL |
957 | /* |
958 | * AON - We do not publicly admit to having appended anything until AON | |
959 | * completes. | |
960 | */ | |
961 | *written = 0; | |
962 | return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_WRITE); | |
e44795bd TM |
963 | } |
964 | ||
22d53c88 HL |
965 | static int quic_write_nonblocking_epw(QUIC_CONNECTION *qc, const void *buf, size_t len, |
966 | size_t *written) | |
e44795bd | 967 | { |
22d53c88 HL |
968 | /* Simple best effort operation. */ |
969 | if (!ossl_quic_sstream_append(qc->stream0->sstream, buf, len, written)) { | |
970 | /* Stream already finished or allocation error. */ | |
971 | *written = 0; | |
972 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
973 | } | |
974 | ||
975 | quic_post_write(qc, *written > 0, 1); | |
e44795bd TM |
976 | return 1; |
977 | } | |
d5ab48a1 | 978 | |
22d53c88 | 979 | int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written) |
d5ab48a1 | 980 | { |
22d53c88 HL |
981 | QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s); |
982 | int partial_write = ((qc->ssl_mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0); | |
983 | ||
984 | *written = 0; | |
985 | ||
986 | if (!expect_quic_conn(qc)) | |
987 | return 0; | |
988 | ||
22d53c88 HL |
989 | if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch)) |
990 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
991 | ||
ca41f6b7 HL |
992 | /* |
993 | * If we haven't finished the handshake, try to advance it. | |
994 | * We don't accept writes until the handshake is completed. | |
995 | */ | |
996 | if (ossl_quic_do_handshake(qc) < 1) | |
997 | return 0; | |
998 | ||
22d53c88 HL |
999 | if (qc->stream0 == NULL || qc->stream0->sstream == NULL) |
1000 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1001 | ||
1002 | if (blocking_mode(qc)) | |
1003 | return quic_write_blocking(qc, buf, len, written); | |
1004 | else if (partial_write) | |
1005 | return quic_write_nonblocking_epw(qc, buf, len, written); | |
1006 | else | |
1007 | return quic_write_nonblocking_aon(qc, buf, len, written); | |
d5ab48a1 RL |
1008 | } |
1009 | ||
1010 | /* | |
22d53c88 HL |
1011 | * SSL_read |
1012 | * -------- | |
d5ab48a1 | 1013 | */ |
22d53c88 HL |
1014 | struct quic_read_again_args { |
1015 | QUIC_CONNECTION *qc; | |
1016 | QUIC_STREAM *stream; | |
1017 | void *buf; | |
1018 | size_t len; | |
1019 | size_t *bytes_read; | |
1020 | int peek; | |
1021 | }; | |
1022 | ||
1023 | static int quic_read_actual(QUIC_CONNECTION *qc, | |
1024 | QUIC_STREAM *stream, | |
1025 | void *buf, size_t buf_len, | |
1026 | size_t *bytes_read, | |
1027 | int peek) | |
d5ab48a1 | 1028 | { |
22d53c88 HL |
1029 | int is_fin = 0; |
1030 | ||
1031 | if (stream->rstream == NULL) | |
1032 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1033 | ||
1034 | if (peek) { | |
1035 | if (!ossl_quic_rstream_peek(stream->rstream, buf, buf_len, | |
1036 | bytes_read, &is_fin)) | |
1037 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1038 | ||
1039 | } else { | |
1040 | if (!ossl_quic_rstream_read(stream->rstream, buf, buf_len, | |
1041 | bytes_read, &is_fin)) | |
1042 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1043 | } | |
1044 | ||
1045 | if (!peek) { | |
1046 | if (*bytes_read > 0) { | |
1047 | /* | |
1048 | * We have read at least one byte from the stream. Inform stream-level | |
1049 | * RXFC of the retirement of controlled bytes. Update the active stream | |
1050 | * status (the RXFC may now want to emit a frame granting more credit to | |
1051 | * the peer). | |
1052 | */ | |
1053 | OSSL_RTT_INFO rtt_info; | |
d50e750e | 1054 | |
22d53c88 HL |
1055 | ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(qc->ch), &rtt_info); |
1056 | ||
1057 | if (!ossl_quic_rxfc_on_retire(&qc->stream0->rxfc, *bytes_read, | |
1058 | rtt_info.smoothed_rtt)) | |
1059 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1060 | } | |
1061 | ||
1062 | if (is_fin) | |
1063 | stream->recv_fin_retired = 1; | |
1064 | ||
1065 | if (*bytes_read > 0) | |
1066 | ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(qc->ch), | |
1067 | qc->stream0); | |
1068 | } | |
1069 | ||
d5ab48a1 RL |
1070 | return 1; |
1071 | } | |
1072 | ||
22d53c88 | 1073 | static int quic_read_again(void *arg) |
d5ab48a1 | 1074 | { |
22d53c88 HL |
1075 | struct quic_read_again_args *args = arg; |
1076 | ||
1077 | if (!ossl_quic_channel_is_active(args->qc->ch)) | |
1078 | /* If connection is torn down due to an error while blocking, stop. */ | |
ca41f6b7 | 1079 | return -1; |
22d53c88 HL |
1080 | |
1081 | if (!quic_read_actual(args->qc, args->stream, | |
1082 | args->buf, args->len, args->bytes_read, | |
1083 | args->peek)) | |
1084 | return -1; | |
1085 | ||
1086 | if (*args->bytes_read > 0) | |
1087 | /* got at least one byte, the SSL_read op can finish now */ | |
1088 | return 1; | |
1089 | ||
81b6b43c | 1090 | return 0; /* did not read anything, keep trying */ |
d5ab48a1 RL |
1091 | } |
1092 | ||
22d53c88 | 1093 | static int quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read, int peek) |
d5ab48a1 | 1094 | { |
22d53c88 HL |
1095 | int res; |
1096 | QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_SSL(s); | |
1097 | struct quic_read_again_args args; | |
1098 | ||
1099 | *bytes_read = 0; | |
1100 | ||
1101 | if (!expect_quic_conn(qc)) | |
1102 | return 0; | |
1103 | ||
1104 | if (qc->ch != NULL && ossl_quic_channel_is_term_any(qc->ch)) | |
1105 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
1106 | ||
ca41f6b7 HL |
1107 | /* If we haven't finished the handshake, try to advance it.*/ |
1108 | if (ossl_quic_do_handshake(qc) < 1) | |
22d53c88 HL |
1109 | return 0; |
1110 | ||
1111 | if (qc->stream0 == NULL) | |
1112 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1113 | ||
1114 | if (!quic_read_actual(qc, qc->stream0, buf, len, bytes_read, peek)) | |
d5ab48a1 | 1115 | return 0; |
22d53c88 HL |
1116 | |
1117 | if (*bytes_read > 0) { | |
1118 | /* | |
1119 | * Even though we succeeded, tick the reactor here to ensure we are | |
1120 | * handling other aspects of the QUIC connection. | |
1121 | */ | |
1122 | ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(qc->ch)); | |
1123 | return 1; | |
1124 | } else if (blocking_mode(qc)) { | |
1125 | /* | |
1126 | * We were not able to read anything immediately, so our stream | |
1127 | * buffer is empty. This means we need to block until we get | |
1128 | * at least one byte. | |
1129 | */ | |
1130 | args.qc = qc; | |
1131 | args.stream = qc->stream0; | |
1132 | args.buf = buf; | |
1133 | args.len = len; | |
1134 | args.bytes_read = bytes_read; | |
1135 | args.peek = peek; | |
1136 | ||
1137 | res = block_until_pred(qc, quic_read_again, &args, 0); | |
1138 | if (res <= 0) { | |
1139 | if (!ossl_quic_channel_is_active(qc->ch)) | |
1140 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, SSL_R_PROTOCOL_IS_SHUTDOWN, NULL); | |
1141 | else | |
1142 | return QUIC_RAISE_NON_NORMAL_ERROR(qc, ERR_R_INTERNAL_ERROR, NULL); | |
1143 | } | |
22d53c88 | 1144 | |
af8b52cf HL |
1145 | return 1; |
1146 | } else { | |
1147 | /* We did not get any bytes and are not in blocking mode. */ | |
1148 | return QUIC_RAISE_NORMAL_ERROR(qc, SSL_ERROR_WANT_READ); | |
1149 | } | |
d5ab48a1 RL |
1150 | } |
1151 | ||
22d53c88 HL |
1152 | int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *bytes_read) |
1153 | { | |
1154 | return quic_read(s, buf, len, bytes_read, 0); | |
1155 | } | |
1156 | ||
1157 | int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *bytes_read) | |
1158 | { | |
1159 | return quic_read(s, buf, len, bytes_read, 1); | |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * SSL_pending | |
1164 | * ----------- | |
1165 | */ | |
1166 | size_t ossl_quic_pending(const SSL *s) | |
1167 | { | |
1168 | const QUIC_CONNECTION *qc = QUIC_CONNECTION_FROM_CONST_SSL(s); | |
1169 | size_t avail = 0; | |
1170 | int fin = 0; | |
1171 | ||
1172 | if (!expect_quic_conn(qc)) | |
1173 | return 0; | |
1174 | ||
1175 | if (qc->stream0 == NULL || qc->stream0->rstream == NULL) | |
1176 | /* Cannot raise errors here because we are const, just fail. */ | |
1177 | return 0; | |
1178 | ||
1179 | if (!ossl_quic_rstream_available(qc->stream0->rstream, &avail, &fin)) | |
1180 | return 0; | |
1181 | ||
1182 | return avail; | |
1183 | } | |
1184 | ||
1185 | /* | |
1186 | * QUIC Front-End I/O API: SSL_CTX Management | |
1187 | * ========================================== | |
1188 | */ | |
1189 | ||
1190 | long ossl_quic_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) | |
1191 | { | |
1192 | switch (cmd) { | |
1193 | default: | |
8a1a6d6d | 1194 | return ssl3_ctx_ctrl(ctx, cmd, larg, parg); |
22d53c88 HL |
1195 | } |
1196 | } | |
1197 | ||
1198 | long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void)) | |
1199 | { | |
8a1a6d6d | 1200 | return ssl3_callback_ctrl(s, cmd, fp); |
22d53c88 HL |
1201 | } |
1202 | ||
1203 | long ossl_quic_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void)) | |
1204 | { | |
8a1a6d6d | 1205 | return ssl3_ctx_callback_ctrl(ctx, cmd, fp); |
22d53c88 HL |
1206 | } |
1207 | ||
1208 | int ossl_quic_renegotiate_check(SSL *ssl, int initok) | |
1209 | { | |
1210 | /* We never do renegotiation. */ | |
1211 | return 0; | |
1212 | } | |
1213 | ||
1214 | /* | |
1215 | * This is the subset of TLS1.3 ciphers which can be used with QUIC and which we | |
1216 | * actually support. | |
c41c7ee9 HL |
1217 | * |
1218 | * TODO(QUIC): CCM support | |
22d53c88 HL |
1219 | */ |
1220 | static SSL_CIPHER tls13_quic_ciphers[] = { | |
1221 | { | |
1222 | 1, | |
1223 | TLS1_3_RFC_AES_128_GCM_SHA256, | |
1224 | TLS1_3_RFC_AES_128_GCM_SHA256, | |
1225 | TLS1_3_CK_AES_128_GCM_SHA256, | |
1226 | SSL_kANY, | |
1227 | SSL_aANY, | |
1228 | SSL_AES128GCM, | |
1229 | SSL_AEAD, | |
1230 | TLS1_3_VERSION, TLS1_3_VERSION, | |
1231 | 0, 0, | |
1232 | SSL_HIGH, | |
1233 | SSL_HANDSHAKE_MAC_SHA256, | |
1234 | 128, | |
1235 | 128, | |
1236 | }, { | |
1237 | 1, | |
1238 | TLS1_3_RFC_AES_256_GCM_SHA384, | |
1239 | TLS1_3_RFC_AES_256_GCM_SHA384, | |
1240 | TLS1_3_CK_AES_256_GCM_SHA384, | |
1241 | SSL_kANY, | |
1242 | SSL_aANY, | |
1243 | SSL_AES256GCM, | |
1244 | SSL_AEAD, | |
1245 | TLS1_3_VERSION, TLS1_3_VERSION, | |
1246 | 0, 0, | |
1247 | SSL_HIGH, | |
1248 | SSL_HANDSHAKE_MAC_SHA384, | |
1249 | 256, | |
1250 | 256, | |
1251 | }, | |
1252 | { | |
1253 | 1, | |
1254 | TLS1_3_RFC_CHACHA20_POLY1305_SHA256, | |
1255 | TLS1_3_RFC_CHACHA20_POLY1305_SHA256, | |
1256 | TLS1_3_CK_CHACHA20_POLY1305_SHA256, | |
1257 | SSL_kANY, | |
1258 | SSL_aANY, | |
1259 | SSL_CHACHA20POLY1305, | |
1260 | SSL_AEAD, | |
1261 | TLS1_3_VERSION, TLS1_3_VERSION, | |
1262 | 0, 0, | |
1263 | SSL_HIGH, | |
1264 | SSL_HANDSHAKE_MAC_SHA256, | |
1265 | 256, | |
1266 | 256, | |
1267 | } | |
1268 | }; | |
1269 | ||
1270 | int ossl_quic_num_ciphers(void) | |
1271 | { | |
1272 | return OSSL_NELEM(tls13_quic_ciphers); | |
1273 | } | |
1274 | ||
1275 | const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u) | |
1276 | { | |
1277 | if (u >= OSSL_NELEM(tls13_quic_ciphers)) | |
1278 | return NULL; | |
1279 | ||
1280 | return &tls13_quic_ciphers[u]; | |
d5ab48a1 | 1281 | } |