2 * Copyright 2022-2023 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 #ifndef OSSL_QUIC_TSERVER_H
11 # define OSSL_QUIC_TSERVER_H
13 # include <openssl/ssl.h>
14 # include <openssl/bio.h>
15 # include "internal/quic_stream.h"
16 # include "internal/quic_channel.h"
17 # include "internal/statem.h"
18 # include "internal/time.h"
20 # ifndef OPENSSL_NO_QUIC
23 * QUIC Test Server Module
24 * =======================
26 * This implements a QUIC test server. Since full QUIC server support is not yet
27 * implemented this server is limited in features and scope. It exists to
28 * provide a target for our QUIC client to talk to for testing purposes.
30 * A given QUIC test server instance supports only one client at a time.
32 * Note that this test server is not suitable for production use because it does
33 * not implement address verification, anti-amplification or retry logic.
35 typedef struct quic_tserver_st QUIC_TSERVER;
37 typedef struct quic_tserver_args_st {
41 BIO *net_rbio, *net_wbio;
42 OSSL_TIME (*now_cb)(void *arg);
44 const unsigned char *alpn;
48 QUIC_TSERVER *ossl_quic_tserver_new(const QUIC_TSERVER_ARGS *args,
49 const char *certfile, const char *keyfile);
51 void ossl_quic_tserver_free(QUIC_TSERVER *srv);
53 /* Set mutator callbacks for test framework support */
54 int ossl_quic_tserver_set_plain_packet_mutator(QUIC_TSERVER *srv,
55 ossl_mutate_packet_cb mutatecb,
56 ossl_finish_mutate_cb finishmutatecb,
59 int ossl_quic_tserver_set_handshake_mutator(QUIC_TSERVER *srv,
60 ossl_statem_mutate_handshake_cb mutate_handshake_cb,
61 ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb,
64 /* Advances the state machine. */
65 int ossl_quic_tserver_tick(QUIC_TSERVER *srv);
67 /* Returns 1 if we have a (non-terminated) client. */
68 int ossl_quic_tserver_is_connected(QUIC_TSERVER *srv);
71 * Returns 1 if we have finished the TLS handshake
73 int ossl_quic_tserver_is_handshake_confirmed(const QUIC_TSERVER *srv);
75 /* Returns 1 if the server is in any terminating or terminated state */
76 int ossl_quic_tserver_is_term_any(const QUIC_TSERVER *srv);
78 const QUIC_TERMINATE_CAUSE *
79 ossl_quic_tserver_get_terminate_cause(const QUIC_TSERVER *srv);
81 /* Returns 1 if the server is in a terminated state */
82 int ossl_quic_tserver_is_terminated(const QUIC_TSERVER *srv);
85 * Attempts to read from stream 0. Writes the number of bytes read to
86 * *bytes_read and returns 1 on success. If no bytes are available, 0 is written
87 * to *bytes_read and 1 is returned (this is considered a success case).
89 * Returns 0 if connection is not currently active. If the receive part of
90 * the stream has reached the end of stream condition, returns 0; call
91 * ossl_quic_tserver_has_read_ended() to identify this condition.
93 int ossl_quic_tserver_read(QUIC_TSERVER *srv,
100 * Returns 1 if the read part of the stream has ended normally.
102 int ossl_quic_tserver_has_read_ended(QUIC_TSERVER *srv, uint64_t stream_id);
105 * Attempts to write to the given stream. Writes the number of bytes consumed to
106 * *bytes_written and returns 1 on success. If there is no space currently
107 * available to write any bytes, 0 is written to *consumed and 1 is returned
108 * (this is considered a success case).
110 * Note that unlike libssl public APIs, this API always works in a 'partial
113 * Returns 0 if connection is not currently active.
115 int ossl_quic_tserver_write(QUIC_TSERVER *srv,
117 const unsigned char *buf,
119 size_t *bytes_written);
122 * Signals normal end of the stream.
124 int ossl_quic_tserver_conclude(QUIC_TSERVER *srv, uint64_t stream_id);
127 * Create a server-initiated stream. The stream ID of the newly
128 * created stream is written to *stream_id.
130 int ossl_quic_tserver_stream_new(QUIC_TSERVER *srv,
132 uint64_t *stream_id);
134 BIO *ossl_quic_tserver_get0_rbio(QUIC_TSERVER *srv);
136 SSL_CTX *ossl_quic_tserver_get0_ssl_ctx(QUIC_TSERVER *srv);
139 * Returns 1 if the peer has sent a STOP_SENDING frame for a stream.
140 * app_error_code is written if this returns 1.
142 int ossl_quic_tserver_stream_has_peer_stop_sending(QUIC_TSERVER *srv,
144 uint64_t *app_error_code);
147 * Returns 1 if the peer has sent a RESET_STREAM frame for a stream.
148 * app_error_code is written if this returns 1.
150 int ossl_quic_tserver_stream_has_peer_reset_stream(QUIC_TSERVER *srv,
152 uint64_t *app_error_code);
155 * Replaces existing local connection ID in the underlying QUIC_CHANNEL.
157 int ossl_quic_tserver_set_new_local_cid(QUIC_TSERVER *srv,
158 const QUIC_CONN_ID *conn_id);
161 * Returns the stream ID of the next incoming stream, or UINT64_MAX if there
164 uint64_t ossl_quic_tserver_pop_incoming_stream(QUIC_TSERVER *srv);
167 * Returns 1 if all data sent on the given stream_id has been acked by the peer.
169 int ossl_quic_tserver_is_stream_totally_acked(QUIC_TSERVER *srv,
172 /* Returns 1 if we are currently interested in reading data from the network */
173 int ossl_quic_tserver_get_net_read_desired(QUIC_TSERVER *srv);
175 /* Returns 1 if we are currently interested in writing data to the network */
176 int ossl_quic_tserver_get_net_write_desired(QUIC_TSERVER *srv);
178 /* Returns the next event deadline */
179 OSSL_TIME ossl_quic_tserver_get_deadline(QUIC_TSERVER *srv);
182 * Shutdown the QUIC connection. Returns 1 if the connection is terminated and
185 int ossl_quic_tserver_shutdown(QUIC_TSERVER *srv, uint64_t app_error_code);
187 /* Force generation of an ACK-eliciting packet. */
188 int ossl_quic_tserver_ping(QUIC_TSERVER *srv);
190 /* Set tracing callback on channel. */
191 void ossl_quic_tserver_set_msg_callback(QUIC_TSERVER *srv,
192 void (*f)(int write_p, int version,
194 const void *buf, size_t len,
195 SSL *ssl, void *arg),
199 * This is similar to ossl_quic_conn_get_channel; it should be used for test
200 * instrumentation only and not to bypass QUIC_TSERVER for 'normal' operations.
202 QUIC_CHANNEL *ossl_quic_tserver_get_channel(QUIC_TSERVER *srv);
204 /* Send a TLS new session ticket */
205 int ossl_quic_tserver_new_ticket(QUIC_TSERVER *srv);
208 * Set the max_early_data value to be sent in NewSessionTickets. Only the
209 * values 0 and 0xffffffff are valid for use in QUIC.
211 int ossl_quic_tserver_set_max_early_data(QUIC_TSERVER *srv,
212 uint32_t max_early_data);
214 /* Set the find session callback for getting a server PSK */
215 void ossl_quic_tserver_set_psk_find_session_cb(QUIC_TSERVER *srv,
216 SSL_psk_find_session_cb_func cb);