2
0

quictestlib.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #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. QTEST_FAULT *qtest_create_injector(QUIC_TSERVER *ts);
  46. BIO_METHOD *qtest_get_bio_method(void);
  47. /*
  48. * Free up a Fault Injector instance
  49. */
  50. void qtest_fault_free(QTEST_FAULT *fault);
  51. /* Returns 1 if the quictestlib supports blocking tests */
  52. int qtest_supports_blocking(void);
  53. /*
  54. * Run the TLS handshake to create a QUIC connection between the client and
  55. * server.
  56. */
  57. int qtest_create_quic_connection(QUIC_TSERVER *qtserv, SSL *clientssl);
  58. /*
  59. * Check if both client and server have no data to read and are waiting on a
  60. * timeout. If so, wait until the timeout has expired.
  61. */
  62. int qtest_wait_for_timeout(SSL *s, QUIC_TSERVER *qtserv);
  63. /*
  64. * Same as qtest_create_quic_connection but will stop (successfully) if the
  65. * clientssl indicates SSL_ERROR_WANT_XXX as specified by |wanterr|
  66. */
  67. int qtest_create_quic_connection_ex(QUIC_TSERVER *qtserv, SSL *clientssl,
  68. int wanterr);
  69. /*
  70. * Shutdown the client SSL object gracefully
  71. */
  72. int qtest_shutdown(QUIC_TSERVER *qtserv, SSL *clientssl);
  73. /*
  74. * Confirm that the server has received the given transport error code.
  75. */
  76. int qtest_check_server_transport_err(QUIC_TSERVER *qtserv, uint64_t code);
  77. /*
  78. * Confirm the server has received a protocol error. Equivalent to calling
  79. * qtest_check_server_transport_err with a code of QUIC_ERR_PROTOCOL_VIOLATION
  80. */
  81. int qtest_check_server_protocol_err(QUIC_TSERVER *qtserv);
  82. /*
  83. * Confirm the server has received a frame encoding error. Equivalent to calling
  84. * qtest_check_server_transport_err with a code of QUIC_ERR_FRAME_ENCODING_ERROR
  85. */
  86. int qtest_check_server_frame_encoding_err(QUIC_TSERVER *qtserv);
  87. /*
  88. * Enable tests to listen for pre-encryption QUIC packets being sent
  89. */
  90. typedef int (*qtest_fault_on_packet_plain_cb)(QTEST_FAULT *fault,
  91. QUIC_PKT_HDR *hdr,
  92. unsigned char *buf,
  93. size_t len,
  94. void *cbarg);
  95. int qtest_fault_set_packet_plain_listener(QTEST_FAULT *fault,
  96. qtest_fault_on_packet_plain_cb pplaincb,
  97. void *pplaincbarg);
  98. /*
  99. * Helper function to be called from a packet_plain_listener callback if it
  100. * wants to resize the packet (either to add new data to it, or to truncate it).
  101. * The buf provided to packet_plain_listener is over allocated, so this just
  102. * changes the logical size and never changes the actual address of the buf.
  103. * This will fail if a large resize is attempted that exceeds the over
  104. * allocation.
  105. */
  106. int qtest_fault_resize_plain_packet(QTEST_FAULT *fault, size_t newlen);
  107. /*
  108. * Prepend frame data into a packet. To be called from a packet_plain_listener
  109. * callback
  110. */
  111. int qtest_fault_prepend_frame(QTEST_FAULT *fault, const unsigned char *frame,
  112. size_t frame_len);
  113. /*
  114. * The general handshake message listener is sent the entire handshake message
  115. * data block, including the handshake header itself
  116. */
  117. typedef int (*qtest_fault_on_handshake_cb)(QTEST_FAULT *fault,
  118. unsigned char *msg,
  119. size_t msglen,
  120. void *handshakecbarg);
  121. int qtest_fault_set_handshake_listener(QTEST_FAULT *fault,
  122. qtest_fault_on_handshake_cb handshakecb,
  123. void *handshakecbarg);
  124. /*
  125. * Helper function to be called from a handshake_listener callback if it wants
  126. * to resize the handshake message (either to add new data to it, or to truncate
  127. * it). newlen must include the length of the handshake message header. The
  128. * handshake message buffer is over allocated, so this just changes the logical
  129. * size and never changes the actual address of the buf.
  130. * This will fail if a large resize is attempted that exceeds the over
  131. * allocation.
  132. */
  133. int qtest_fault_resize_handshake(QTEST_FAULT *fault, size_t newlen);
  134. /*
  135. * Add listeners for specific types of frame here. E.g. we might
  136. * expect to see an "ACK" frame listener which will be passed pre-parsed ack
  137. * data that can be modified as required.
  138. */
  139. /*
  140. * Handshake message specific listeners. Unlike the general handshake message
  141. * listener these messages are pre-parsed and supplied with message specific
  142. * data and exclude the handshake header
  143. */
  144. typedef int (*qtest_fault_on_enc_ext_cb)(QTEST_FAULT *fault,
  145. QTEST_ENCRYPTED_EXTENSIONS *ee,
  146. size_t eelen,
  147. void *encextcbarg);
  148. int qtest_fault_set_hand_enc_ext_listener(QTEST_FAULT *fault,
  149. qtest_fault_on_enc_ext_cb encextcb,
  150. void *encextcbarg);
  151. /* Add listeners for other types of handshake message here */
  152. /*
  153. * Helper function to be called from message specific listener callbacks. newlen
  154. * is the new length of the specific message excluding the handshake message
  155. * header. The buffers provided to the message specific listeners are over
  156. * allocated, so this just changes the logical size and never changes the actual
  157. * address of the buffer. This will fail if a large resize is attempted that
  158. * exceeds the over allocation.
  159. */
  160. int qtest_fault_resize_message(QTEST_FAULT *fault, size_t newlen);
  161. /*
  162. * Helper function to delete an extension from an extension block. |exttype| is
  163. * the type of the extension to be deleted. |ext| points to the extension block.
  164. * On entry |*extlen| contains the length of the extension block. It is updated
  165. * with the new length on exit. If old_ext is non-NULL, the deleted extension
  166. * is appended to the given BUF_MEM.
  167. */
  168. int qtest_fault_delete_extension(QTEST_FAULT *fault,
  169. unsigned int exttype, unsigned char *ext,
  170. size_t *extlen,
  171. BUF_MEM *old_ext);
  172. /*
  173. * Add additional helper functions for querying extensions here (e.g.
  174. * finding or adding them). We could also provide a "listener" API for listening
  175. * for specific extension types
  176. */
  177. /*
  178. * Enable tests to listen for post-encryption QUIC packets being sent
  179. */
  180. typedef int (*qtest_fault_on_packet_cipher_cb)(QTEST_FAULT *fault,
  181. /* The parsed packet header */
  182. QUIC_PKT_HDR *hdr,
  183. /* The packet payload data */
  184. unsigned char *buf,
  185. /* Length of the payload */
  186. size_t len,
  187. void *cbarg);
  188. int qtest_fault_set_packet_cipher_listener(QTEST_FAULT *fault,
  189. qtest_fault_on_packet_cipher_cb pciphercb,
  190. void *picphercbarg);
  191. /*
  192. * Enable tests to listen for datagrams being sent
  193. */
  194. typedef int (*qtest_fault_on_datagram_cb)(QTEST_FAULT *fault,
  195. BIO_MSG *m,
  196. size_t stride,
  197. void *cbarg);
  198. int qtest_fault_set_datagram_listener(QTEST_FAULT *fault,
  199. qtest_fault_on_datagram_cb datagramcb,
  200. void *datagramcbarg);
  201. /*
  202. * To be called from a datagram_listener callback. The datagram buffer is over
  203. * allocated, so this just changes the logical size and never changes the actual
  204. * address of the buffer. This will fail if a large resize is attempted that
  205. * exceeds the over allocation.
  206. */
  207. int qtest_fault_resize_datagram(QTEST_FAULT *fault, size_t newlen);
  208. /* Copy a BIO_MSG */
  209. int bio_msg_copy(BIO_MSG *dst, BIO_MSG *src);
  210. #define BIO_CTRL_NOISE_BACK_OFF 1001
  211. /* BIO filter for simulating a noisy UDP socket */
  212. const BIO_METHOD *bio_f_noisy_dgram_filter(void);
  213. /* Free the BIO filter method object */
  214. void bio_f_noisy_dgram_filter_free(void);
  215. /*
  216. * BIO filter for splitting QUIC datagrams containing multiple packets into
  217. * individual datagrams.
  218. */
  219. const BIO_METHOD *bio_f_pkt_split_dgram_filter(void);
  220. /* Free the BIO filter method object */
  221. void bio_f_pkt_split_dgram_filter_free(void);