quic_tserver.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifndef OSSL_QUIC_TSERVER_H
  10. # define OSSL_QUIC_TSERVER_H
  11. # include <openssl/ssl.h>
  12. # include <openssl/bio.h>
  13. # include "internal/quic_stream.h"
  14. # include "internal/quic_channel.h"
  15. # include "internal/statem.h"
  16. # include "internal/time.h"
  17. # ifndef OPENSSL_NO_QUIC
  18. /*
  19. * QUIC Test Server Module
  20. * =======================
  21. *
  22. * This implements a QUIC test server. Since full QUIC server support is not yet
  23. * implemented this server is limited in features and scope. It exists to
  24. * provide a target for our QUIC client to talk to for testing purposes.
  25. *
  26. * A given QUIC test server instance supports only one client at a time.
  27. *
  28. * Note that this test server is not suitable for production use because it does
  29. * not implement address verification, anti-amplification or retry logic.
  30. */
  31. typedef struct quic_tserver_st QUIC_TSERVER;
  32. typedef struct quic_tserver_args_st {
  33. OSSL_LIB_CTX *libctx;
  34. const char *propq;
  35. SSL_CTX *ctx;
  36. BIO *net_rbio, *net_wbio;
  37. OSSL_TIME (*now_cb)(void *arg);
  38. void *now_cb_arg;
  39. const unsigned char *alpn;
  40. size_t alpnlen;
  41. } QUIC_TSERVER_ARGS;
  42. QUIC_TSERVER *ossl_quic_tserver_new(const QUIC_TSERVER_ARGS *args,
  43. const char *certfile, const char *keyfile);
  44. void ossl_quic_tserver_free(QUIC_TSERVER *srv);
  45. /* Set mutator callbacks for test framework support */
  46. int ossl_quic_tserver_set_plain_packet_mutator(QUIC_TSERVER *srv,
  47. ossl_mutate_packet_cb mutatecb,
  48. ossl_finish_mutate_cb finishmutatecb,
  49. void *mutatearg);
  50. int ossl_quic_tserver_set_handshake_mutator(QUIC_TSERVER *srv,
  51. ossl_statem_mutate_handshake_cb mutate_handshake_cb,
  52. ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb,
  53. void *mutatearg);
  54. /* Advances the state machine. */
  55. int ossl_quic_tserver_tick(QUIC_TSERVER *srv);
  56. /* Returns 1 if we have a (non-terminated) client. */
  57. int ossl_quic_tserver_is_connected(QUIC_TSERVER *srv);
  58. /*
  59. * Returns 1 if we have finished the TLS handshake
  60. */
  61. int ossl_quic_tserver_is_handshake_confirmed(const QUIC_TSERVER *srv);
  62. /* Returns 1 if the server is in any terminating or terminated state */
  63. int ossl_quic_tserver_is_term_any(const QUIC_TSERVER *srv);
  64. const QUIC_TERMINATE_CAUSE *
  65. ossl_quic_tserver_get_terminate_cause(const QUIC_TSERVER *srv);
  66. /* Returns 1 if the server is in a terminated state */
  67. int ossl_quic_tserver_is_terminated(const QUIC_TSERVER *srv);
  68. /* Get out short header conn id length */
  69. size_t ossl_quic_tserver_get_short_header_conn_id_len(const QUIC_TSERVER *srv);
  70. /*
  71. * Attempts to read from stream 0. Writes the number of bytes read to
  72. * *bytes_read and returns 1 on success. If no bytes are available, 0 is written
  73. * to *bytes_read and 1 is returned (this is considered a success case).
  74. *
  75. * Returns 0 if connection is not currently active. If the receive part of
  76. * the stream has reached the end of stream condition, returns 0; call
  77. * ossl_quic_tserver_has_read_ended() to identify this condition.
  78. */
  79. int ossl_quic_tserver_read(QUIC_TSERVER *srv,
  80. uint64_t stream_id,
  81. unsigned char *buf,
  82. size_t buf_len,
  83. size_t *bytes_read);
  84. /*
  85. * Returns 1 if the read part of the stream has ended normally.
  86. */
  87. int ossl_quic_tserver_has_read_ended(QUIC_TSERVER *srv, uint64_t stream_id);
  88. /*
  89. * Attempts to write to the given stream. Writes the number of bytes consumed to
  90. * *bytes_written and returns 1 on success. If there is no space currently
  91. * available to write any bytes, 0 is written to *consumed and 1 is returned
  92. * (this is considered a success case).
  93. *
  94. * Note that unlike libssl public APIs, this API always works in a 'partial
  95. * write' mode.
  96. *
  97. * Returns 0 if connection is not currently active.
  98. */
  99. int ossl_quic_tserver_write(QUIC_TSERVER *srv,
  100. uint64_t stream_id,
  101. const unsigned char *buf,
  102. size_t buf_len,
  103. size_t *bytes_written);
  104. /*
  105. * Signals normal end of the stream.
  106. */
  107. int ossl_quic_tserver_conclude(QUIC_TSERVER *srv, uint64_t stream_id);
  108. /*
  109. * Create a server-initiated stream. The stream ID of the newly
  110. * created stream is written to *stream_id.
  111. */
  112. int ossl_quic_tserver_stream_new(QUIC_TSERVER *srv,
  113. int is_uni,
  114. uint64_t *stream_id);
  115. BIO *ossl_quic_tserver_get0_rbio(QUIC_TSERVER *srv);
  116. SSL_CTX *ossl_quic_tserver_get0_ssl_ctx(QUIC_TSERVER *srv);
  117. /*
  118. * Returns 1 if the peer has sent a STOP_SENDING frame for a stream.
  119. * app_error_code is written if this returns 1.
  120. */
  121. int ossl_quic_tserver_stream_has_peer_stop_sending(QUIC_TSERVER *srv,
  122. uint64_t stream_id,
  123. uint64_t *app_error_code);
  124. /*
  125. * Returns 1 if the peer has sent a RESET_STREAM frame for a stream.
  126. * app_error_code is written if this returns 1.
  127. */
  128. int ossl_quic_tserver_stream_has_peer_reset_stream(QUIC_TSERVER *srv,
  129. uint64_t stream_id,
  130. uint64_t *app_error_code);
  131. /*
  132. * Replaces existing local connection ID in the underlying QUIC_CHANNEL.
  133. */
  134. int ossl_quic_tserver_set_new_local_cid(QUIC_TSERVER *srv,
  135. const QUIC_CONN_ID *conn_id);
  136. /*
  137. * Returns the stream ID of the next incoming stream, or UINT64_MAX if there
  138. * currently is none.
  139. */
  140. uint64_t ossl_quic_tserver_pop_incoming_stream(QUIC_TSERVER *srv);
  141. /*
  142. * Returns 1 if all data sent on the given stream_id has been acked by the peer.
  143. */
  144. int ossl_quic_tserver_is_stream_totally_acked(QUIC_TSERVER *srv,
  145. uint64_t stream_id);
  146. /* Returns 1 if we are currently interested in reading data from the network */
  147. int ossl_quic_tserver_get_net_read_desired(QUIC_TSERVER *srv);
  148. /* Returns 1 if we are currently interested in writing data to the network */
  149. int ossl_quic_tserver_get_net_write_desired(QUIC_TSERVER *srv);
  150. /* Returns the next event deadline */
  151. OSSL_TIME ossl_quic_tserver_get_deadline(QUIC_TSERVER *srv);
  152. /*
  153. * Shutdown the QUIC connection. Returns 1 if the connection is terminated and
  154. * 0 otherwise.
  155. */
  156. int ossl_quic_tserver_shutdown(QUIC_TSERVER *srv, uint64_t app_error_code);
  157. /* Force generation of an ACK-eliciting packet. */
  158. int ossl_quic_tserver_ping(QUIC_TSERVER *srv);
  159. /* Set tracing callback on channel. */
  160. void ossl_quic_tserver_set_msg_callback(QUIC_TSERVER *srv,
  161. void (*f)(int write_p, int version,
  162. int content_type,
  163. const void *buf, size_t len,
  164. SSL *ssl, void *arg),
  165. void *arg);
  166. /*
  167. * This is similar to ossl_quic_conn_get_channel; it should be used for test
  168. * instrumentation only and not to bypass QUIC_TSERVER for 'normal' operations.
  169. */
  170. QUIC_CHANNEL *ossl_quic_tserver_get_channel(QUIC_TSERVER *srv);
  171. /* Send a TLS new session ticket */
  172. int ossl_quic_tserver_new_ticket(QUIC_TSERVER *srv);
  173. /*
  174. * Set the max_early_data value to be sent in NewSessionTickets. Only the
  175. * values 0 and 0xffffffff are valid for use in QUIC.
  176. */
  177. int ossl_quic_tserver_set_max_early_data(QUIC_TSERVER *srv,
  178. uint32_t max_early_data);
  179. /* Set the find session callback for getting a server PSK */
  180. void ossl_quic_tserver_set_psk_find_session_cb(QUIC_TSERVER *srv,
  181. SSL_psk_find_session_cb_func cb);
  182. # endif
  183. #endif