2
0

quictestlib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright 2022-2024 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. #include <openssl/ssl.h>
  10. #include <internal/quic_tserver.h>
  11. /* Type to represent the Fault Injector */
  12. typedef struct qtest_fault QTEST_FAULT;
  13. /*
  14. * Structure representing a parsed EncryptedExtension message. Listeners can
  15. * make changes to the contents of structure objects as required and the fault
  16. * injector will reconstruct the message to be sent on
  17. */
  18. typedef struct qtest_fault_encrypted_extensions {
  19. /* EncryptedExtension messages just have an extensions block */
  20. unsigned char *extensions;
  21. size_t extensionslen;
  22. } QTEST_ENCRYPTED_EXTENSIONS;
  23. /* Flags for use with qtest_create_quic_objects() */
  24. /* Indicates whether we are using blocking mode or not */
  25. #define QTEST_FLAG_BLOCK (1 << 0)
  26. /* Use fake time rather than real time */
  27. #define QTEST_FLAG_FAKE_TIME (1 << 1)
  28. /* Introduce noise in the BIO */
  29. #define QTEST_FLAG_NOISE (1 << 2)
  30. /* Split datagrams such that each datagram contains one packet */
  31. #define QTEST_FLAG_PACKET_SPLIT (1 << 3)
  32. /* Turn on client side tracing */
  33. #define QTEST_FLAG_CLIENT_TRACE (1 << 4)
  34. /*
  35. * Given an SSL_CTX for the client and filenames for the server certificate and
  36. * keyfile, create a server and client instances as well as a fault injector
  37. * instance. |flags| is the logical or of flags defined above, or 0 if none.
  38. */
  39. int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
  40. SSL_CTX *serverctx, char *certfile, char *keyfile,
  41. int flags, QUIC_TSERVER **qtserv, SSL **cssl,
  42. QTEST_FAULT **fault, BIO **tracebio);
  43. /* Where QTEST_FLAG_FAKE_TIME is used, add millis to the current time */
  44. void qtest_add_time(uint64_t millis);
  45. /* Starts time measurement */
  46. void qtest_start_stopwatch(void);
  47. /* Returns the duration from the start in millis */
  48. uint64_t qtest_get_stopwatch_time(void);
  49. QTEST_FAULT *qtest_create_injector(QUIC_TSERVER *ts);
  50. BIO_METHOD *qtest_get_bio_method(void);
  51. /*
  52. * Free up a Fault Injector instance
  53. */
  54. void qtest_fault_free(QTEST_FAULT *fault);
  55. /* Returns 1 if the quictestlib supports blocking tests */
  56. int qtest_supports_blocking(void);
  57. /*
  58. * Run the TLS handshake to create a QUIC connection between the client and
  59. * server.
  60. */
  61. int qtest_create_quic_connection(QUIC_TSERVER *qtserv, SSL *clientssl);
  62. /*
  63. * Check if both client and server have no data to read and are waiting on a
  64. * timeout. If so, wait until the timeout has expired.
  65. */
  66. int qtest_wait_for_timeout(SSL *s, QUIC_TSERVER *qtserv);
  67. /*
  68. * Same as qtest_create_quic_connection but will stop (successfully) if the
  69. * clientssl indicates SSL_ERROR_WANT_XXX as specified by |wanterr|
  70. */
  71. int qtest_create_quic_connection_ex(QUIC_TSERVER *qtserv, SSL *clientssl,
  72. int wanterr);
  73. /*
  74. * Shutdown the client SSL object gracefully
  75. */
  76. int qtest_shutdown(QUIC_TSERVER *qtserv, SSL *clientssl);
  77. /*
  78. * Confirm that the server has received the given transport error code.
  79. */
  80. int qtest_check_server_transport_err(QUIC_TSERVER *qtserv, uint64_t code);
  81. /*
  82. * Confirm the server has received a protocol error. Equivalent to calling
  83. * qtest_check_server_transport_err with a code of QUIC_ERR_PROTOCOL_VIOLATION
  84. */
  85. int qtest_check_server_protocol_err(QUIC_TSERVER *qtserv);
  86. /*
  87. * Confirm the server has received a frame encoding error. Equivalent to calling
  88. * qtest_check_server_transport_err with a code of QUIC_ERR_FRAME_ENCODING_ERROR
  89. */
  90. int qtest_check_server_frame_encoding_err(QUIC_TSERVER *qtserv);
  91. /*
  92. * Enable tests to listen for pre-encryption QUIC packets being sent
  93. */
  94. typedef int (*qtest_fault_on_packet_plain_cb)(QTEST_FAULT *fault,
  95. QUIC_PKT_HDR *hdr,
  96. unsigned char *buf,
  97. size_t len,
  98. void *cbarg);
  99. int qtest_fault_set_packet_plain_listener(QTEST_FAULT *fault,
  100. qtest_fault_on_packet_plain_cb pplaincb,
  101. void *pplaincbarg);
  102. /*
  103. * Helper function to be called from a packet_plain_listener callback if it
  104. * wants to resize the packet (either to add new data to it, or to truncate it).
  105. * The buf provided to packet_plain_listener is over allocated, so this just
  106. * changes the logical size and never changes the actual address of the buf.
  107. * This will fail if a large resize is attempted that exceeds the over
  108. * allocation.
  109. */
  110. int qtest_fault_resize_plain_packet(QTEST_FAULT *fault, size_t newlen);
  111. /*
  112. * Prepend frame data into a packet. To be called from a packet_plain_listener
  113. * callback
  114. */
  115. int qtest_fault_prepend_frame(QTEST_FAULT *fault, const unsigned char *frame,
  116. size_t frame_len);
  117. /*
  118. * The general handshake message listener is sent the entire handshake message
  119. * data block, including the handshake header itself
  120. */
  121. typedef int (*qtest_fault_on_handshake_cb)(QTEST_FAULT *fault,
  122. unsigned char *msg,
  123. size_t msglen,
  124. void *handshakecbarg);
  125. int qtest_fault_set_handshake_listener(QTEST_FAULT *fault,
  126. qtest_fault_on_handshake_cb handshakecb,
  127. void *handshakecbarg);
  128. /*
  129. * Helper function to be called from a handshake_listener callback if it wants
  130. * to resize the handshake message (either to add new data to it, or to truncate
  131. * it). newlen must include the length of the handshake message header. The
  132. * handshake message buffer is over allocated, so this just changes the logical
  133. * size and never changes the actual address of the buf.
  134. * This will fail if a large resize is attempted that exceeds the over
  135. * allocation.
  136. */
  137. int qtest_fault_resize_handshake(QTEST_FAULT *fault, size_t newlen);
  138. /*
  139. * Add listeners for specific types of frame here. E.g. we might
  140. * expect to see an "ACK" frame listener which will be passed pre-parsed ack
  141. * data that can be modified as required.
  142. */
  143. /*
  144. * Handshake message specific listeners. Unlike the general handshake message
  145. * listener these messages are pre-parsed and supplied with message specific
  146. * data and exclude the handshake header
  147. */
  148. typedef int (*qtest_fault_on_enc_ext_cb)(QTEST_FAULT *fault,
  149. QTEST_ENCRYPTED_EXTENSIONS *ee,
  150. size_t eelen,
  151. void *encextcbarg);
  152. int qtest_fault_set_hand_enc_ext_listener(QTEST_FAULT *fault,
  153. qtest_fault_on_enc_ext_cb encextcb,
  154. void *encextcbarg);
  155. /* Add listeners for other types of handshake message here */
  156. /*
  157. * Helper function to be called from message specific listener callbacks. newlen
  158. * is the new length of the specific message excluding the handshake message
  159. * header. The buffers provided to the message specific listeners are over
  160. * allocated, so this just changes the logical size and never changes the actual
  161. * address of the buffer. This will fail if a large resize is attempted that
  162. * exceeds the over allocation.
  163. */
  164. int qtest_fault_resize_message(QTEST_FAULT *fault, size_t newlen);
  165. /*
  166. * Helper function to delete an extension from an extension block. |exttype| is
  167. * the type of the extension to be deleted. |ext| points to the extension block.
  168. * On entry |*extlen| contains the length of the extension block. It is updated
  169. * with the new length on exit. If old_ext is non-NULL, the deleted extension
  170. * is appended to the given BUF_MEM.
  171. */
  172. int qtest_fault_delete_extension(QTEST_FAULT *fault,
  173. unsigned int exttype, unsigned char *ext,
  174. size_t *extlen,
  175. BUF_MEM *old_ext);
  176. /*
  177. * Add additional helper functions for querying extensions here (e.g.
  178. * finding or adding them). We could also provide a "listener" API for listening
  179. * for specific extension types
  180. */
  181. /*
  182. * Enable tests to listen for post-encryption QUIC packets being sent
  183. */
  184. typedef int (*qtest_fault_on_packet_cipher_cb)(QTEST_FAULT *fault,
  185. /* The parsed packet header */
  186. QUIC_PKT_HDR *hdr,
  187. /* The packet payload data */
  188. unsigned char *buf,
  189. /* Length of the payload */
  190. size_t len,
  191. void *cbarg);
  192. int qtest_fault_set_packet_cipher_listener(QTEST_FAULT *fault,
  193. qtest_fault_on_packet_cipher_cb pciphercb,
  194. void *picphercbarg);
  195. /*
  196. * Enable tests to listen for datagrams being sent
  197. */
  198. typedef int (*qtest_fault_on_datagram_cb)(QTEST_FAULT *fault,
  199. BIO_MSG *m,
  200. size_t stride,
  201. void *cbarg);
  202. int qtest_fault_set_datagram_listener(QTEST_FAULT *fault,
  203. qtest_fault_on_datagram_cb datagramcb,
  204. void *datagramcbarg);
  205. /*
  206. * To be called from a datagram_listener callback. The datagram buffer is over
  207. * allocated, so this just changes the logical size and never changes the actual
  208. * address of the buffer. This will fail if a large resize is attempted that
  209. * exceeds the over allocation.
  210. */
  211. int qtest_fault_resize_datagram(QTEST_FAULT *fault, size_t newlen);
  212. /*
  213. * Set bandwidth and noise rate on noisy dgram filter.
  214. * Arguments with values of 0 mean no limit/no noise.
  215. */
  216. int qtest_fault_set_bw_limit(QTEST_FAULT *fault,
  217. size_t ctos_bw, size_t stoc_bw,
  218. int noise_rate);
  219. /* Copy a BIO_MSG */
  220. int bio_msg_copy(BIO_MSG *dst, BIO_MSG *src);
  221. #define BIO_CTRL_NOISE_BACK_OFF 1001
  222. #define BIO_CTRL_NOISE_RATE 1002
  223. #define BIO_CTRL_NOISE_RECV_BANDWIDTH 1003
  224. #define BIO_CTRL_NOISE_SEND_BANDWIDTH 1004
  225. #define BIO_CTRL_NOISE_SET_NOW_CB 1005
  226. struct bio_noise_now_cb_st {
  227. OSSL_TIME (*now_cb)(void *);
  228. void *now_cb_arg;
  229. };
  230. /* BIO filter for simulating a noisy UDP socket */
  231. const BIO_METHOD *bio_f_noisy_dgram_filter(void);
  232. /* Free the BIO filter method object */
  233. void bio_f_noisy_dgram_filter_free(void);
  234. /*
  235. * BIO filter for splitting QUIC datagrams containing multiple packets into
  236. * individual datagrams.
  237. */
  238. const BIO_METHOD *bio_f_pkt_split_dgram_filter(void);
  239. /* Free the BIO filter method object */
  240. void bio_f_pkt_split_dgram_filter_free(void);