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