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