2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
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"
17 * QUIC Test Server Module
18 * =======================
20 struct quic_tserver_st
{
21 QUIC_TSERVER_ARGS args
;
24 * The QUIC channel providing the core QUIC connection implementation.
28 /* The mutex we give to the QUIC channel. */
31 /* SSL_CTX for creating the underlying TLS connection */
34 /* SSL for the underlying TLS connection */
37 /* The current peer L4 address. AF_UNSPEC if we do not have a peer yet. */
38 BIO_ADDR cur_peer_addr
;
40 /* Are we connected to a peer? */
41 unsigned int connected
: 1;
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
)
48 QUIC_TSERVER
*srv
= arg
;
49 static const unsigned char alpndeflt
[] = {
50 8, 'o', 's', 's', 'l', 't', 'e', 's', 't'
52 static const unsigned char *alpn
;
55 if (srv
->args
.alpn
== NULL
) {
57 alpnlen
= sizeof(alpn
);
59 alpn
= srv
->args
.alpn
;
60 alpnlen
= srv
->args
.alpnlen
;
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
;
67 return SSL_TLSEXT_ERR_OK
;
70 QUIC_TSERVER
*ossl_quic_tserver_new(const QUIC_TSERVER_ARGS
*args
,
71 const char *certfile
, const char *keyfile
)
73 QUIC_TSERVER
*srv
= NULL
;
74 QUIC_CHANNEL_ARGS ch_args
= {0};
76 if (args
->net_rbio
== NULL
|| args
->net_wbio
== NULL
)
79 if ((srv
= OPENSSL_zalloc(sizeof(*srv
))) == NULL
)
84 #if defined(OPENSSL_THREADS)
85 if ((srv
->mutex
= ossl_crypto_mutex_new()) == NULL
)
89 if (args
->ctx
!= NULL
)
92 srv
->ctx
= SSL_CTX_new_ex(srv
->args
.libctx
, srv
->args
.propq
,
97 if (SSL_CTX_use_certificate_file(srv
->ctx
, certfile
, SSL_FILETYPE_PEM
) <= 0)
100 if (SSL_CTX_use_PrivateKey_file(srv
->ctx
, keyfile
, SSL_FILETYPE_PEM
) <= 0)
103 SSL_CTX_set_alpn_select_cb(srv
->ctx
, alpn_select_cb
, srv
);
105 srv
->tls
= SSL_new(srv
->ctx
);
106 if (srv
->tls
== NULL
)
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
;
117 if ((srv
->ch
= ossl_quic_channel_new(&ch_args
)) == NULL
)
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
))
128 if (args
->ctx
== NULL
)
129 SSL_CTX_free(srv
->ctx
);
131 ossl_quic_channel_free(srv
->ch
);
132 #if defined(OPENSSL_THREADS)
133 ossl_crypto_mutex_free(&srv
->mutex
);
141 void ossl_quic_tserver_free(QUIC_TSERVER
*srv
)
146 ossl_quic_channel_free(srv
->ch
);
147 BIO_free(srv
->args
.net_rbio
);
148 BIO_free(srv
->args
.net_wbio
);
150 SSL_CTX_free(srv
->ctx
);
151 #if defined(OPENSSL_THREADS)
152 ossl_crypto_mutex_free(&srv
->mutex
);
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
,
163 return ossl_quic_channel_set_mutator(srv
->ch
, mutatecb
, finishmutatecb
,
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
,
172 return ossl_statem_set_mutator(ossl_quic_channel_get0_ssl(srv
->ch
),
174 finish_mutate_handshake_cb
,
178 int ossl_quic_tserver_tick(QUIC_TSERVER
*srv
)
180 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv
->ch
), 0);
182 if (ossl_quic_channel_is_active(srv
->ch
))
188 int ossl_quic_tserver_is_connected(QUIC_TSERVER
*srv
)
190 return ossl_quic_channel_is_active(srv
->ch
);
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
)
196 return ossl_quic_channel_is_term_any(srv
->ch
);
199 const QUIC_TERMINATE_CAUSE
*
200 ossl_quic_tserver_get_terminate_cause(const QUIC_TSERVER
*srv
)
202 return ossl_quic_channel_get_terminate_cause(srv
->ch
);
205 /* Returns 1 if the server is in a terminated state */
206 int ossl_quic_tserver_is_terminated(const QUIC_TSERVER
*srv
)
208 return ossl_quic_channel_is_terminated(srv
->ch
);
211 int ossl_quic_tserver_is_handshake_confirmed(const QUIC_TSERVER
*srv
)
213 return ossl_quic_channel_is_handshake_confirmed(srv
->ch
);
216 int ossl_quic_tserver_read(QUIC_TSERVER
*srv
,
225 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv
->ch
),
229 = ((stream_id
& QUIC_STREAM_INITIATOR_MASK
)
230 == QUIC_STREAM_INITIATOR_CLIENT
);
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.
238 if (!is_client_init
|| !ossl_quic_channel_is_active(srv
->ch
))
245 if (qs
->recv_state
== QUIC_RSTREAM_STATE_DATA_READ
246 || !ossl_quic_stream_has_recv_buffer(qs
))
249 if (!ossl_quic_rstream_read(qs
->rstream
, buf
, buf_len
,
250 bytes_read
, &is_fin
))
253 if (*bytes_read
> 0) {
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
260 OSSL_RTT_INFO rtt_info
;
262 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(srv
->ch
), &rtt_info
);
264 if (!ossl_quic_rxfc_on_retire(&qs
->rxfc
, *bytes_read
,
265 rtt_info
.smoothed_rtt
))
270 ossl_quic_stream_map_notify_totally_read(ossl_quic_channel_get_qsm(srv
->ch
),
274 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv
->ch
), qs
);
279 int ossl_quic_tserver_has_read_ended(QUIC_TSERVER
*srv
, uint64_t stream_id
)
282 unsigned char buf
[1];
283 size_t bytes_read
= 0;
286 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv
->ch
),
292 if (qs
->recv_state
== QUIC_RSTREAM_STATE_DATA_READ
)
295 if (!ossl_quic_stream_has_recv_buffer(qs
))
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.
304 if (!ossl_quic_rstream_peek(qs
->rstream
, buf
, sizeof(buf
),
305 &bytes_read
, &is_fin
))
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
);
316 ossl_quic_stream_map_notify_totally_read(ossl_quic_channel_get_qsm(srv
->ch
),
318 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv
->ch
), qs
);
325 int ossl_quic_tserver_write(QUIC_TSERVER
*srv
,
327 const unsigned char *buf
,
329 size_t *bytes_written
)
333 if (!ossl_quic_channel_is_active(srv
->ch
))
336 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv
->ch
),
338 if (qs
== NULL
|| !ossl_quic_stream_has_send_buffer(qs
))
341 if (!ossl_quic_sstream_append(qs
->sstream
,
342 buf
, buf_len
, bytes_written
))
345 if (*bytes_written
> 0)
347 * We have appended at least one byte to the stream. Potentially mark
348 * the stream as active, depending on FC.
350 ossl_quic_stream_map_update_state(ossl_quic_channel_get_qsm(srv
->ch
), qs
);
353 ossl_quic_tserver_tick(srv
);
357 int ossl_quic_tserver_conclude(QUIC_TSERVER
*srv
, uint64_t stream_id
)
361 if (!ossl_quic_channel_is_active(srv
->ch
))
364 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv
->ch
),
366 if (qs
== NULL
|| !ossl_quic_stream_has_send_buffer(qs
))
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
);
374 ossl_quic_tserver_tick(srv
);
378 int ossl_quic_tserver_stream_new(QUIC_TSERVER
*srv
,
384 if (!ossl_quic_channel_is_active(srv
->ch
))
387 if ((qs
= ossl_quic_channel_new_stream_local(srv
->ch
, is_uni
)) == NULL
)
394 BIO
*ossl_quic_tserver_get0_rbio(QUIC_TSERVER
*srv
)
396 return srv
->args
.net_rbio
;
399 SSL_CTX
*ossl_quic_tserver_get0_ssl_ctx(QUIC_TSERVER
*srv
)
404 int ossl_quic_tserver_stream_has_peer_stop_sending(QUIC_TSERVER
*srv
,
406 uint64_t *app_error_code
)
410 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv
->ch
),
415 if (qs
->peer_stop_sending
&& app_error_code
!= NULL
)
416 *app_error_code
= qs
->peer_stop_sending_aec
;
418 return qs
->peer_stop_sending
;
421 int ossl_quic_tserver_stream_has_peer_reset_stream(QUIC_TSERVER
*srv
,
423 uint64_t *app_error_code
)
427 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv
->ch
),
432 if (ossl_quic_stream_recv_is_reset(qs
) && app_error_code
!= NULL
)
433 *app_error_code
= qs
->peer_reset_stream_aec
;
435 return ossl_quic_stream_recv_is_reset(qs
);
438 int ossl_quic_tserver_set_new_local_cid(QUIC_TSERVER
*srv
,
439 const QUIC_CONN_ID
*conn_id
)
441 /* Replace existing local connection ID in the QUIC_CHANNEL */
442 return ossl_quic_channel_replace_local_cid(srv
->ch
, conn_id
);
445 uint64_t ossl_quic_tserver_pop_incoming_stream(QUIC_TSERVER
*srv
)
447 QUIC_STREAM_MAP
*qsm
= ossl_quic_channel_get_qsm(srv
->ch
);
448 QUIC_STREAM
*qs
= ossl_quic_stream_map_peek_accept_queue(qsm
);
453 ossl_quic_stream_map_remove_from_accept_queue(qsm
, qs
, ossl_time_zero());
458 int ossl_quic_tserver_is_stream_totally_acked(QUIC_TSERVER
*srv
,
463 qs
= ossl_quic_stream_map_get_by_id(ossl_quic_channel_get_qsm(srv
->ch
),
468 return ossl_quic_sstream_is_totally_acked(qs
->sstream
);
471 int ossl_quic_tserver_get_net_read_desired(QUIC_TSERVER
*srv
)
473 return ossl_quic_reactor_net_read_desired(
474 ossl_quic_channel_get_reactor(srv
->ch
));
477 int ossl_quic_tserver_get_net_write_desired(QUIC_TSERVER
*srv
)
479 return ossl_quic_reactor_net_write_desired(
480 ossl_quic_channel_get_reactor(srv
->ch
));
483 OSSL_TIME
ossl_quic_tserver_get_deadline(QUIC_TSERVER
*srv
)
485 return ossl_quic_reactor_get_tick_deadline(
486 ossl_quic_channel_get_reactor(srv
->ch
));
489 int ossl_quic_tserver_shutdown(QUIC_TSERVER
*srv
)
491 ossl_quic_channel_local_close(srv
->ch
, 0);
493 /* TODO(QUIC): !SSL_SHUTDOWN_FLAG_NO_STREAM_FLUSH */
495 if (ossl_quic_channel_is_terminated(srv
->ch
))
498 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv
->ch
), 0);
500 return ossl_quic_channel_is_terminated(srv
->ch
);
503 int ossl_quic_tserver_ping(QUIC_TSERVER
*srv
)
505 if (ossl_quic_channel_is_terminated(srv
->ch
))
508 if (!ossl_quic_channel_ping(srv
->ch
))
511 ossl_quic_reactor_tick(ossl_quic_channel_get_reactor(srv
->ch
), 0);