quic_tserver.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2022-2023 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. /*
  69. * Attempts to read from stream 0. Writes the number of bytes read to
  70. * *bytes_read and returns 1 on success. If no bytes are available, 0 is written
  71. * to *bytes_read and 1 is returned (this is considered a success case).
  72. *
  73. * Returns 0 if connection is not currently active. If the receive part of
  74. * the stream has reached the end of stream condition, returns 0; call
  75. * ossl_quic_tserver_has_read_ended() to identify this condition.
  76. */
  77. int ossl_quic_tserver_read(QUIC_TSERVER *srv,
  78. uint64_t stream_id,
  79. unsigned char *buf,
  80. size_t buf_len,
  81. size_t *bytes_read);
  82. /*
  83. * Returns 1 if the read part of the stream has ended normally.
  84. */
  85. int ossl_quic_tserver_has_read_ended(QUIC_TSERVER *srv, uint64_t stream_id);
  86. /*
  87. * Attempts to write to the given stream. Writes the number of bytes consumed to
  88. * *bytes_written and returns 1 on success. If there is no space currently
  89. * available to write any bytes, 0 is written to *consumed and 1 is returned
  90. * (this is considered a success case).
  91. *
  92. * Note that unlike libssl public APIs, this API always works in a 'partial
  93. * write' mode.
  94. *
  95. * Returns 0 if connection is not currently active.
  96. */
  97. int ossl_quic_tserver_write(QUIC_TSERVER *srv,
  98. uint64_t stream_id,
  99. const unsigned char *buf,
  100. size_t buf_len,
  101. size_t *bytes_written);
  102. /*
  103. * Signals normal end of the stream.
  104. */
  105. int ossl_quic_tserver_conclude(QUIC_TSERVER *srv, uint64_t stream_id);
  106. /*
  107. * Create a server-initiated stream. The stream ID of the newly
  108. * created stream is written to *stream_id.
  109. */
  110. int ossl_quic_tserver_stream_new(QUIC_TSERVER *srv,
  111. int is_uni,
  112. uint64_t *stream_id);
  113. BIO *ossl_quic_tserver_get0_rbio(QUIC_TSERVER *srv);
  114. SSL_CTX *ossl_quic_tserver_get0_ssl_ctx(QUIC_TSERVER *srv);
  115. /*
  116. * Returns 1 if the peer has sent a STOP_SENDING frame for a stream.
  117. * app_error_code is written if this returns 1.
  118. */
  119. int ossl_quic_tserver_stream_has_peer_stop_sending(QUIC_TSERVER *srv,
  120. uint64_t stream_id,
  121. uint64_t *app_error_code);
  122. /*
  123. * Returns 1 if the peer has sent a RESET_STREAM frame for a stream.
  124. * app_error_code is written if this returns 1.
  125. */
  126. int ossl_quic_tserver_stream_has_peer_reset_stream(QUIC_TSERVER *srv,
  127. uint64_t stream_id,
  128. uint64_t *app_error_code);
  129. /*
  130. * Replaces existing local connection ID in the underlying QUIC_CHANNEL.
  131. */
  132. int ossl_quic_tserver_set_new_local_cid(QUIC_TSERVER *srv,
  133. const QUIC_CONN_ID *conn_id);
  134. /*
  135. * Returns the stream ID of the next incoming stream, or UINT64_MAX if there
  136. * currently is none.
  137. */
  138. uint64_t ossl_quic_tserver_pop_incoming_stream(QUIC_TSERVER *srv);
  139. /*
  140. * Returns 1 if all data sent on the given stream_id has been acked by the peer.
  141. */
  142. int ossl_quic_tserver_is_stream_totally_acked(QUIC_TSERVER *srv,
  143. uint64_t stream_id);
  144. /* Returns 1 if we are currently interested in reading data from the network */
  145. int ossl_quic_tserver_get_net_read_desired(QUIC_TSERVER *srv);
  146. /* Returns 1 if we are currently interested in writing data to the network */
  147. int ossl_quic_tserver_get_net_write_desired(QUIC_TSERVER *srv);
  148. /* Returns the next event deadline */
  149. OSSL_TIME ossl_quic_tserver_get_deadline(QUIC_TSERVER *srv);
  150. /*
  151. * Shutdown the QUIC connection. Returns 1 if the connection is terminated and
  152. * 0 otherwise.
  153. */
  154. int ossl_quic_tserver_shutdown(QUIC_TSERVER *srv, uint64_t app_error_code);
  155. /* Force generation of an ACK-eliciting packet. */
  156. int ossl_quic_tserver_ping(QUIC_TSERVER *srv);
  157. /* Set tracing callback on channel. */
  158. void ossl_quic_tserver_set_msg_callback(QUIC_TSERVER *srv,
  159. void (*f)(int write_p, int version,
  160. int content_type,
  161. const void *buf, size_t len,
  162. SSL *ssl, void *arg),
  163. void *arg);
  164. /*
  165. * This is similar to ossl_quic_conn_get_channel; it should be used for test
  166. * instrumentation only and not to bypass QUIC_TSERVER for 'normal' operations.
  167. */
  168. QUIC_CHANNEL *ossl_quic_tserver_get_channel(QUIC_TSERVER *srv);
  169. /* Send a TLS new session ticket */
  170. int ossl_quic_tserver_new_ticket(QUIC_TSERVER *srv);
  171. /*
  172. * Set the max_early_data value to be sent in NewSessionTickets. Only the
  173. * values 0 and 0xffffffff are valid for use in QUIC.
  174. */
  175. int ossl_quic_tserver_set_max_early_data(QUIC_TSERVER *srv,
  176. uint32_t max_early_data);
  177. /* Set the find session callback for getting a server PSK */
  178. void ossl_quic_tserver_set_psk_find_session_cb(QUIC_TSERVER *srv,
  179. SSL_psk_find_session_cb_func cb);
  180. # endif
  181. #endif