Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/ssl/quic/quic_tserver.c

raw
1 /*
2 * Copyright 2022-2025 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 "internal/quic_tserver.h"
11 #include "internal/quic_channel.h"
12 #include "internal/quic_statm.h"
13 #include "internal/quic_port.h"
14 #include "internal/quic_engine.h"
15 #include "internal/common.h"
16 #include "internal/time.h"
17 #include "quic_local.h"
18
19 /*
20 * QUIC Test Server Module
21 * =======================
22 */
23 struct quic_tserver_st {
24 QUIC_TSERVER_ARGS args;
25
26 /* Dummy SSL object for this QUIC connection for use by msg_callback */
27 SSL *ssl;
28
29 /*
30 * The QUIC engine, port and channel providing the core QUIC connection
31 * implementation.
32 */
33 QUIC_ENGINE *engine;
34 QUIC_PORT *port;
35 QUIC_CHANNEL *ch;
36
37 /* The mutex we give to the QUIC channel. */
38 CRYPTO_MUTEX *mutex;
39
40 /* SSL_CTX for creating the underlying TLS connection */
41 SSL_CTX *ctx;
42
43 /* SSL for the underlying TLS connection */
44 SSL *tls;
45
46 /* Are we connected to a peer? */
47 unsigned int connected : 1;
48 };
49
50 static int alpn_select_cb(SSL *ssl, const unsigned char **out,
51 unsigned char *outlen, const unsigned char *in,
52 unsigned int inlen, void *arg)
53 {
54 QUIC_TSERVER *srv = arg;
55 static const unsigned char alpndeflt[] = {
56 8, 'o', 's', 's', 'l', 't', 'e', 's', 't'
57 };
58 const unsigned char *alpn;
59 size_t alpnlen;
60
61 if (srv->args.alpn == NULL) {
62 alpn = alpndeflt;
63 alpnlen = sizeof(alpndeflt);
64 } else {
65 alpn = srv->args.alpn;
66 alpnlen = srv->args.alpnlen;
67 }
68
69 if (SSL_select_next_proto((unsigned char **)out, outlen, alpn,
70 (unsigned int)alpnlen,
71 in, inlen)
72 != OPENSSL_NPN_NEGOTIATED)
73 return SSL_TLSEXT_ERR_ALERT_FATAL;
74
75 return SSL_TLSEXT_ERR_OK;
76 }
77
78 QUIC_TSERVER *ossl_quic_tserver_new(const QUIC_TSERVER_ARGS *args,
79 const char *certfile, const char *keyfile)
80 {
81 QUIC_TSERVER *srv = NULL;
82 QUIC_ENGINE_ARGS engine_args = { 0 };
83 QUIC_PORT_ARGS port_args = { 0 };
84 QUIC_CONNECTION *qc = NULL;
85
86 if (args->net_rbio == NULL || args->net_wbio == NULL)
87 goto err;
88
89 if ((srv = OPENSSL_zalloc(sizeof(*srv))) == NULL)
90 goto err;
91
92 srv->args = *args;
93
94 #if defined(OPENSSL_THREADS)
95 if ((srv->mutex = ossl_crypto_mutex_new()) == NULL)
96 goto err;
97 #endif
98
99 if (args->ctx != NULL)
100 srv->ctx = args->ctx;
101 else
102 srv->ctx = SSL_CTX_new_ex(srv->args.libctx, srv->args.propq,
103 TLS_method());
104 if (srv->ctx == NULL)
105 goto err;
106
107 if (certfile != NULL
108 && SSL_CTX_use_certificate_file(srv->ctx, certfile, SSL_FILETYPE_PEM) <= 0)
109 goto err;
110
111 if (keyfile != NULL
112 && SSL_CTX_use_PrivateKey_file(srv->ctx, keyfile, SSL_FILETYPE_PEM) <= 0)
113 goto err;
114
115 SSL_CTX_set_alpn_select_cb(srv->ctx, alpn_select_cb, srv);
116
117 srv->tls = SSL_new(srv->ctx);
118 if (srv->tls == NULL)
119 goto err;
120
121 engine_args.libctx = srv->args.libctx;
122 engine_args.propq = srv->args.propq;
123 engine_args.mutex = srv->mutex;
124
125 if ((srv->engine = ossl_quic_engine_new(&engine_args)) == NULL)
126 goto err;
127
128 ossl_quic_engine_set_time_cb(srv->engine, srv->args.now_cb,
129 srv->args.now_cb_arg);
130
131 port_args.channel_ctx = srv->ctx;
132 port_args.is_multi_conn = 1;
133 port_args.do_addr_validation = 1;
134 if ((srv->port = ossl_quic_engine_create_port(srv->engine, &port_args)) == NULL)
135 goto err;
136
137 if ((srv->ch = ossl_quic_port_create_incoming(srv->port, srv->tls)) == NULL)
138 goto err;
139
140 if (!ossl_quic_port_set_net_rbio(srv->port, srv->args.net_rbio)
141 || !ossl_quic_port_set_net_wbio(srv->port, srv->args.net_wbio))
142 goto err;
143
144 qc = OPENSSL_zalloc(sizeof(*qc));
145 if (qc == NULL)
146 goto err;
147 srv->ssl = (SSL *)qc;
148 qc->ch = srv->ch;
149 srv->ssl->type = SSL_TYPE_QUIC_CONNECTION;
150
151 return srv;
152
153 err:
154 if (srv != NULL) {
155 if (args->ctx == NULL)
156 SSL_CTX_free(srv->ctx);
157 SSL_free(srv->tls);
158 ossl_quic_channel_free(srv->ch);
159 ossl_quic_port_free(srv->port);
160 ossl_quic_engine_free(srv->engine);
161 #if defined(OPENSSL_THREADS)
162 ossl_crypto_mutex_free(&srv->mutex);
163 #endif
164 OPENSSL_free(qc);
165 }
166
167 OPENSSL_free(srv);
168 return NULL;
169 }
170
171 void ossl_quic_tserver_free(QUIC_TSERVER *srv)
172 {
173 if (srv == NULL)
174 return;
175
176 SSL_free(srv->tls);
177 ossl_quic_channel_free(srv->ch);
178 ossl_quic_port_free(srv->port);
179 ossl_quic_engine_free(srv->engine);
180 BIO_free_all(srv->args.net_rbio);
181 BIO_free_all(srv->args.net_wbio);
182 OPENSSL_free(srv->ssl);
183 SSL_CTX_free(srv->ctx);
184 #if defined(OPENSSL_THREADS)
185 ossl_crypto_mutex_free(&srv->mutex);
186 #endif
187 OPENSSL_free(srv);
188 }
189
190 /* Set mutator callbacks for test framework support */
191 int ossl_quic_tserver_set_plain_packet_mutator(QUIC_TSERVER *srv,
192 ossl_mutate_packet_cb mutatecb,
193 ossl_finish_mutate_cb finishmutatecb,
194 void *mutatearg)
195 {
196 return ossl_quic_channel_set_mutator(srv->ch, mutatecb, finishmutatecb,
197 mutatearg);
198 }
199
200 int ossl_quic_tserver_set_handshake_mutator(QUIC_TSERVER *srv,
201 ossl_statem_mutate_handshake_cb mutate_handshake_cb,
202 ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb,
203 void *mutatearg)
204 {
205 return ossl_statem_set_mutator(ossl_quic_channel_get0_ssl(srv->ch),
206 mutate_handshake_cb,
207 finish_mutate_handshake_cb,
208 mutatearg);
209 }
210
211 int ossl_quic_tserver_tick(QUIC_TSERVER *srv)
212 {
213 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv->ch), 0);
214
215 if (ossl_quic_channel_is_active(srv->ch))
216 srv->connected = 1;
217
218 return 1;
219 }
220
221 int ossl_quic_tserver_is_connected(QUIC_TSERVER *srv)
222 {
223 return ossl_quic_channel_is_active(srv->ch);
224 }
225
226 /* Returns 1 if the server is in any terminating or terminated state */
227 int ossl_quic_tserver_is_term_any(const QUIC_TSERVER *srv)
228 {
229 return ossl_quic_channel_is_term_any(srv->ch);
230 }
231
232 const QUIC_TERMINATE_CAUSE *
233 ossl_quic_tserver_get_terminate_cause(const QUIC_TSERVER *srv)
234 {
235 return ossl_quic_channel_get_terminate_cause(srv->ch);
236 }
237
238 /* Returns 1 if the server is in a terminated state */
239 int ossl_quic_tserver_is_terminated(const QUIC_TSERVER *srv)
240 {
241 return ossl_quic_channel_is_terminated(srv->ch);
242 }
243
244 size_t ossl_quic_tserver_get_short_header_conn_id_len(const QUIC_TSERVER *srv)
245 {
246 return ossl_quic_channel_get_short_header_conn_id_len(srv->ch);
247 }
248
249 int ossl_quic_tserver_is_handshake_confirmed(const QUIC_TSERVER *srv)
250 {
251 return ossl_quic_channel_is_handshake_confirmed(srv->ch);
252 }
253
254 int ossl_quic_tserver_read(QUIC_TSERVER *srv,
255 uint64_t stream_id,
256 unsigned char *buf,
257 size_t buf_len,
258 size_t *bytes_read)
259 {
260 int is_fin = 0;
261 QUIC_STREAM *qs;
262
263 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
264 stream_id);
265 if (qs == NULL) {
266 int is_client_init
267 = ((stream_id & QUIC_STREAM_INITIATOR_MASK)
268 == QUIC_STREAM_INITIATOR_CLIENT);
269
270 /*
271 * A client-initiated stream might spontaneously come into existence, so
272 * allow trying to read on a client-initiated stream before it exists,
273 * assuming the connection is still active.
274 * Otherwise, fail.
275 */
276 if (!is_client_init || !ossl_quic_channel_is_active(srv->ch))
277 return 0;
278
279 *bytes_read = 0;
280 return 1;
281 }
282
283 if (qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ
284 || !ossl_quic_stream_has_recv_buffer(qs))
285 return 0;
286
287 if (!ossl_quic_rstream_read(qs->rstream, buf, buf_len,
288 bytes_read, &is_fin))
289 return 0;
290
291 if (*bytes_read > 0) {
292 /*
293 * We have read at least one byte from the stream. Inform stream-level
294 * RXFC of the retirement of controlled bytes. Update the active stream
295 * status (the RXFC may now want to emit a frame granting more credit to
296 * the peer).
297 */
298 OSSL_RTT_INFO rtt_info;
299
300 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(srv->ch), &rtt_info);
301
302 if (!ossl_quic_rxfc_on_retire(&qs->rxfc, *bytes_read,
303 rtt_info.smoothed_rtt))
304 return 0;
305 }
306
307 if (is_fin)
308 ossl_quic_stream_map_notify_totally_read(ossl_quic_channel_get_qsm(srv->ch),
309 qs);
310
311 if (*bytes_read > 0)
312 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv->ch), qs);
313
314 return 1;
315 }
316
317 int ossl_quic_tserver_has_read_ended(QUIC_TSERVER *srv, uint64_t stream_id)
318 {
319 QUIC_STREAM *qs;
320 unsigned char buf[1];
321 size_t bytes_read = 0;
322 int is_fin = 0;
323
324 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
325 stream_id);
326
327 if (qs == NULL)
328 return 0;
329
330 if (qs->recv_state == QUIC_RSTREAM_STATE_DATA_READ)
331 return 1;
332
333 if (!ossl_quic_stream_has_recv_buffer(qs))
334 return 0;
335
336 /*
337 * If we do not have the DATA_READ, it is possible we should still return 1
338 * if there is a lone FIN (but no more data) remaining to be retired from
339 * the RSTREAM, for example because ossl_quic_tserver_read() has not been
340 * called since the FIN was received.
341 */
342 if (!ossl_quic_rstream_peek(qs->rstream, buf, sizeof(buf),
343 &bytes_read, &is_fin))
344 return 0;
345
346 if (is_fin && bytes_read == 0) {
347 /* If we have a FIN awaiting retirement and no data before it... */
348 /* Let RSTREAM know we've consumed this FIN. */
349 if (!ossl_quic_rstream_read(qs->rstream, buf, sizeof(buf),
350 &bytes_read, &is_fin))
351 return 0;
352
353 assert(is_fin && bytes_read == 0);
354 assert(qs->recv_state == QUIC_RSTREAM_STATE_DATA_RECVD);
355
356 ossl_quic_stream_map_notify_totally_read(ossl_quic_channel_get_qsm(srv->ch),
357 qs);
358 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv->ch), qs);
359 return 1;
360 }
361
362 return 0;
363 }
364
365 int ossl_quic_tserver_write(QUIC_TSERVER *srv,
366 uint64_t stream_id,
367 const unsigned char *buf,
368 size_t buf_len,
369 size_t *bytes_written)
370 {
371 QUIC_STREAM *qs;
372
373 if (!ossl_quic_channel_is_active(srv->ch))
374 return 0;
375
376 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
377 stream_id);
378 if (qs == NULL || !ossl_quic_stream_has_send_buffer(qs))
379 return 0;
380
381 if (!ossl_quic_sstream_append(qs->sstream,
382 buf, buf_len, bytes_written))
383 return 0;
384
385 if (*bytes_written > 0)
386 /*
387 * We have appended at least one byte to the stream. Potentially mark
388 * the stream as active, depending on FC.
389 */
390 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv->ch), qs);
391
392 /* Try and send. */
393 ossl_quic_tserver_tick(srv);
394 return 1;
395 }
396
397 int ossl_quic_tserver_conclude(QUIC_TSERVER *srv, uint64_t stream_id)
398 {
399 QUIC_STREAM *qs;
400
401 if (!ossl_quic_channel_is_active(srv->ch))
402 return 0;
403
404 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
405 stream_id);
406 if (qs == NULL || !ossl_quic_stream_has_send_buffer(qs))
407 return 0;
408
409 if (!ossl_quic_sstream_get_final_size(qs->sstream, NULL)) {
410 ossl_quic_sstream_fin(qs->sstream);
411 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv->ch), qs);
412 }
413
414 ossl_quic_tserver_tick(srv);
415 return 1;
416 }
417
418 int ossl_quic_tserver_stream_new(QUIC_TSERVER *srv,
419 int is_uni,
420 uint64_t *stream_id)
421 {
422 QUIC_STREAM *qs;
423
424 if (!ossl_quic_channel_is_active(srv->ch))
425 return 0;
426
427 if ((qs = ossl_quic_channel_new_stream_local(srv->ch, is_uni)) == NULL)
428 return 0;
429
430 *stream_id = qs->id;
431 return 1;
432 }
433
434 BIO *ossl_quic_tserver_get0_rbio(QUIC_TSERVER *srv)
435 {
436 return srv->args.net_rbio;
437 }
438
439 SSL_CTX *ossl_quic_tserver_get0_ssl_ctx(QUIC_TSERVER *srv)
440 {
441 return srv->ctx;
442 }
443
444 int ossl_quic_tserver_stream_has_peer_stop_sending(QUIC_TSERVER *srv,
445 uint64_t stream_id,
446 uint64_t *app_error_code)
447 {
448 QUIC_STREAM *qs;
449
450 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
451 stream_id);
452 if (qs == NULL)
453 return 0;
454
455 if (qs->peer_stop_sending && app_error_code != NULL)
456 *app_error_code = qs->peer_stop_sending_aec;
457
458 return qs->peer_stop_sending;
459 }
460
461 int ossl_quic_tserver_stream_has_peer_reset_stream(QUIC_TSERVER *srv,
462 uint64_t stream_id,
463 uint64_t *app_error_code)
464 {
465 QUIC_STREAM *qs;
466
467 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
468 stream_id);
469 if (qs == NULL)
470 return 0;
471
472 if (ossl_quic_stream_recv_is_reset(qs) && app_error_code != NULL)
473 *app_error_code = qs->peer_reset_stream_aec;
474
475 return ossl_quic_stream_recv_is_reset(qs);
476 }
477
478 int ossl_quic_tserver_set_new_local_cid(QUIC_TSERVER *srv,
479 const QUIC_CONN_ID *conn_id)
480 {
481 /* Replace existing local connection ID in the QUIC_CHANNEL */
482 return ossl_quic_channel_replace_local_cid(srv->ch, conn_id);
483 }
484
485 uint64_t ossl_quic_tserver_pop_incoming_stream(QUIC_TSERVER *srv)
486 {
487 QUIC_STREAM_MAP *qsm = ossl_quic_channel_get_qsm(srv->ch);
488 QUIC_STREAM *qs = ossl_quic_stream_map_peek_accept_queue(qsm);
489
490 if (qs == NULL)
491 return UINT64_MAX;
492
493 ossl_quic_stream_map_remove_from_accept_queue(qsm, qs, ossl_time_zero());
494
495 return qs->id;
496 }
497
498 int ossl_quic_tserver_is_stream_totally_acked(QUIC_TSERVER *srv,
499 uint64_t stream_id)
500 {
501 QUIC_STREAM *qs;
502
503 qs = ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv->ch),
504 stream_id);
505 if (qs == NULL)
506 return 1;
507
508 return ossl_quic_sstream_is_totally_acked(qs->sstream);
509 }
510
511 int ossl_quic_tserver_get_net_read_desired(QUIC_TSERVER *srv)
512 {
513 return ossl_quic_reactor_net_read_desired(
514 ossl_quic_channel_get_reactor(srv->ch));
515 }
516
517 int ossl_quic_tserver_get_net_write_desired(QUIC_TSERVER *srv)
518 {
519 return ossl_quic_reactor_net_write_desired(
520 ossl_quic_channel_get_reactor(srv->ch));
521 }
522
523 OSSL_TIME ossl_quic_tserver_get_deadline(QUIC_TSERVER *srv)
524 {
525 return ossl_quic_reactor_get_tick_deadline(
526 ossl_quic_channel_get_reactor(srv->ch));
527 }
528
529 int ossl_quic_tserver_shutdown(QUIC_TSERVER *srv, uint64_t app_error_code)
530 {
531 ossl_quic_channel_local_close(srv->ch, app_error_code, NULL);
532
533 if (ossl_quic_channel_is_terminated(srv->ch))
534 return 1;
535
536 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv->ch), 0);
537
538 return ossl_quic_channel_is_terminated(srv->ch);
539 }
540
541 int ossl_quic_tserver_ping(QUIC_TSERVER *srv)
542 {
543 if (ossl_quic_channel_is_terminated(srv->ch))
544 return 0;
545
546 if (!ossl_quic_channel_ping(srv->ch))
547 return 0;
548
549 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv->ch), 0);
550 return 1;
551 }
552
553 QUIC_CHANNEL *ossl_quic_tserver_get_channel(QUIC_TSERVER *srv)
554 {
555 return srv->ch;
556 }
557
558 void ossl_quic_tserver_set_msg_callback(QUIC_TSERVER *srv,
559 void (*f)(int write_p, int version,
560 int content_type,
561 const void *buf, size_t len,
562 SSL *ssl, void *arg),
563 void *arg)
564 {
565 ossl_quic_channel_set_msg_callback(srv->ch, f, srv->ssl);
566 ossl_quic_channel_set_msg_callback_arg(srv->ch, arg);
567 SSL_set_msg_callback(srv->tls, f);
568 SSL_set_msg_callback_arg(srv->tls, arg);
569 }
570
571 int ossl_quic_tserver_new_ticket(QUIC_TSERVER *srv)
572 {
573 return SSL_new_session_ticket(srv->tls);
574 }
575
576 int ossl_quic_tserver_set_max_early_data(QUIC_TSERVER *srv,
577 uint32_t max_early_data)
578 {
579 return SSL_set_max_early_data(srv->tls, max_early_data);
580 }
581
582 void ossl_quic_tserver_set_psk_find_session_cb(QUIC_TSERVER *srv,
583 SSL_psk_find_session_cb_func cb)
584 {
585 SSL_set_psk_find_session_callback(srv->tls, cb);
586 }