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