quic_local.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #ifndef OSSL_QUIC_LOCAL_H
  10. # define OSSL_QUIC_LOCAL_H
  11. # include <openssl/ssl.h>
  12. # include "internal/quic_ssl.h" /* QUIC_CONNECTION */
  13. # include "internal/quic_txp.h"
  14. # include "internal/quic_statm.h"
  15. # include "internal/quic_demux.h"
  16. # include "internal/quic_record_rx.h"
  17. # include "internal/quic_tls.h"
  18. # include "internal/quic_fc.h"
  19. # include "internal/quic_stream.h"
  20. # include "internal/quic_channel.h"
  21. # include "internal/quic_reactor.h"
  22. # include "internal/quic_thread_assist.h"
  23. # include "../ssl_local.h"
  24. # ifndef OPENSSL_NO_QUIC
  25. /*
  26. * QUIC stream SSL object (QSSO) type. This implements the API personality layer
  27. * for QSSO objects, wrapping the QUIC-native QUIC_STREAM object and tracking
  28. * state required by the libssl API personality.
  29. */
  30. struct quic_xso_st {
  31. /* SSL object common header. */
  32. struct ssl_st ssl;
  33. /* The connection this stream is associated with. Always non-NULL. */
  34. QUIC_CONNECTION *conn;
  35. /* The stream object. Always non-NULL for as long as the XSO exists. */
  36. QUIC_STREAM *stream;
  37. /*
  38. * Has this stream been logically configured into blocking mode? Only
  39. * meaningful if desires_blocking_set is 1. Ignored if blocking is not
  40. * currently possible given QUIC_CONNECTION configuration.
  41. */
  42. unsigned int desires_blocking : 1;
  43. /*
  44. * Has SSL_set_blocking_mode been called on this stream? If not set, we
  45. * inherit from the QUIC_CONNECTION blocking state.
  46. */
  47. unsigned int desires_blocking_set : 1;
  48. /* The application has retired a FIN (i.e. SSL_ERROR_ZERO_RETURN). */
  49. unsigned int retired_fin : 1;
  50. /*
  51. * The application has requested a reset. Not set for reflexive
  52. * STREAM_RESETs caused by peer STOP_SENDING.
  53. */
  54. unsigned int requested_reset : 1;
  55. /*
  56. * This state tracks SSL_write all-or-nothing (AON) write semantics
  57. * emulation.
  58. *
  59. * Example chronology:
  60. *
  61. * t=0: aon_write_in_progress=0
  62. * t=1: SSL_write(ssl, b1, l1) called;
  63. * too big to enqueue into sstream at once, SSL_ERROR_WANT_WRITE;
  64. * aon_write_in_progress=1; aon_buf_base=b1; aon_buf_len=l1;
  65. * aon_buf_pos < l1 (depends on how much room was in sstream);
  66. * t=2: SSL_write(ssl, b2, l2);
  67. * b2 must equal b1 (validated unless ACCEPT_MOVING_WRITE_BUFFER)
  68. * l2 must equal l1 (always validated)
  69. * append into sstream from [b2 + aon_buf_pos, b2 + aon_buf_len)
  70. * if done, aon_write_in_progress=0
  71. *
  72. */
  73. /* Is an AON write in progress? */
  74. unsigned int aon_write_in_progress : 1;
  75. /* Event handling mode. One of SSL_QUIC_VALUE_EVENT_HANDLING. */
  76. unsigned int event_handling_mode : 2;
  77. /*
  78. * The base buffer pointer the caller passed us for the initial AON write
  79. * call. We use this for validation purposes unless
  80. * ACCEPT_MOVING_WRITE_BUFFER is enabled.
  81. *
  82. * NOTE: We never dereference this, as the caller might pass a different
  83. * (but identical) buffer if using ACCEPT_MOVING_WRITE_BUFFER. It is for
  84. * validation by pointer comparison only.
  85. */
  86. const unsigned char *aon_buf_base;
  87. /* The total length of the AON buffer being sent, in bytes. */
  88. size_t aon_buf_len;
  89. /*
  90. * The position in the AON buffer up to which we have successfully sent data
  91. * so far.
  92. */
  93. size_t aon_buf_pos;
  94. /* SSL_set_mode */
  95. uint32_t ssl_mode;
  96. /* SSL_set_options */
  97. uint64_t ssl_options;
  98. /*
  99. * Last 'normal' error during an app-level I/O operation, used by
  100. * SSL_get_error(); used to track data-path errors like SSL_ERROR_WANT_READ
  101. * and SSL_ERROR_WANT_WRITE.
  102. */
  103. int last_error;
  104. };
  105. struct quic_conn_st {
  106. /*
  107. * ssl_st is a common header for ordinary SSL objects, QUIC connection
  108. * objects and QUIC stream objects, allowing objects of these different
  109. * types to be disambiguated at runtime and providing some common fields.
  110. *
  111. * Note: This must come first in the QUIC_CONNECTION structure.
  112. */
  113. struct ssl_st ssl;
  114. SSL *tls;
  115. /* The QUIC engine representing the QUIC event domain. */
  116. QUIC_ENGINE *engine;
  117. /* The QUIC port representing the QUIC listener and socket. */
  118. QUIC_PORT *port;
  119. /*
  120. * The QUIC channel providing the core QUIC connection implementation. Note
  121. * that this is not instantiated until we actually start trying to do the
  122. * handshake. This is to allow us to gather information like whether we are
  123. * going to be in client or server mode before committing to instantiating
  124. * the channel, since we want to determine the channel arguments based on
  125. * that.
  126. *
  127. * The channel remains available after connection termination until the SSL
  128. * object is freed, thus (ch != NULL) iff (started == 1).
  129. */
  130. QUIC_CHANNEL *ch;
  131. /*
  132. * The mutex used to synchronise access to the QUIC_CHANNEL. We own this but
  133. * provide it to the channel.
  134. */
  135. CRYPTO_MUTEX *mutex;
  136. /*
  137. * If we have a default stream attached, this is the internal XSO
  138. * object. If there is no default stream, this is NULL.
  139. */
  140. QUIC_XSO *default_xso;
  141. /* The network read and write BIOs. */
  142. BIO *net_rbio, *net_wbio;
  143. /* Initial peer L4 address. */
  144. BIO_ADDR init_peer_addr;
  145. # ifndef OPENSSL_NO_QUIC_THREAD_ASSIST
  146. /* Manages thread for QUIC thread assisted mode. */
  147. QUIC_THREAD_ASSIST thread_assist;
  148. # endif
  149. /* If non-NULL, used instead of ossl_time_now(). Used for testing. */
  150. OSSL_TIME (*override_now_cb)(void *arg);
  151. void *override_now_cb_arg;
  152. /* Number of XSOs allocated. Includes the default XSO, if any. */
  153. size_t num_xso;
  154. /* Have we started? */
  155. unsigned int started : 1;
  156. /*
  157. * This is 1 if we were instantiated using a QUIC server method
  158. * (for future use).
  159. */
  160. unsigned int as_server : 1;
  161. /*
  162. * Has the application called SSL_set_accept_state? We require this to be
  163. * congruent with the value of as_server.
  164. */
  165. unsigned int as_server_state : 1;
  166. /* Are we using thread assisted mode? Never changes after init. */
  167. unsigned int is_thread_assisted : 1;
  168. /* Do connection-level operations (e.g. handshakes) run in blocking mode? */
  169. unsigned int blocking : 1;
  170. /* Does the application want blocking mode? */
  171. unsigned int desires_blocking : 1;
  172. /* Have we created a default XSO yet? */
  173. unsigned int default_xso_created : 1;
  174. /*
  175. * Pre-TERMINATING shutdown phase in which we are flushing streams.
  176. * Monotonically transitions to 1.
  177. * New streams cannot be created in this state.
  178. */
  179. unsigned int shutting_down : 1;
  180. /* Have we probed the BIOs for addressing support? */
  181. unsigned int addressing_probe_done : 1;
  182. /* Are we using addressed mode (BIO_sendmmsg with non-NULL peer)? */
  183. unsigned int addressed_mode_w : 1;
  184. unsigned int addressed_mode_r : 1;
  185. /* Event handling mode. One of SSL_QUIC_VALUE_EVENT_HANDLING. */
  186. unsigned int event_handling_mode : 2;
  187. /* Default stream type. Defaults to SSL_DEFAULT_STREAM_MODE_AUTO_BIDI. */
  188. uint32_t default_stream_mode;
  189. /* SSL_set_mode. This is not used directly but inherited by new XSOs. */
  190. uint32_t default_ssl_mode;
  191. /* SSL_set_options. This is not used directly but inherited by new XSOs. */
  192. uint64_t default_ssl_options;
  193. /* SSL_set_incoming_stream_policy. */
  194. int incoming_stream_policy;
  195. uint64_t incoming_stream_aec;
  196. /*
  197. * Last 'normal' error during an app-level I/O operation, used by
  198. * SSL_get_error(); used to track data-path errors like SSL_ERROR_WANT_READ
  199. * and SSL_ERROR_WANT_WRITE.
  200. */
  201. int last_error;
  202. };
  203. /* Internal calls to the QUIC CSM which come from various places. */
  204. int ossl_quic_conn_on_handshake_confirmed(QUIC_CONNECTION *qc);
  205. /*
  206. * To be called when a protocol violation occurs. The connection is torn down
  207. * with the given error code, which should be a OSSL_QUIC_ERR_* value. Reason
  208. * string is optional and copied if provided. frame_type should be 0 if not
  209. * applicable.
  210. */
  211. void ossl_quic_conn_raise_protocol_error(QUIC_CONNECTION *qc,
  212. uint64_t error_code,
  213. uint64_t frame_type,
  214. const char *reason);
  215. void ossl_quic_conn_on_remote_conn_close(QUIC_CONNECTION *qc,
  216. OSSL_QUIC_FRAME_CONN_CLOSE *f);
  217. int ossl_quic_trace(int write_p, int version, int content_type,
  218. const void *buf, size_t msglen, SSL *ssl, void *arg);
  219. # define OSSL_QUIC_ANY_VERSION 0xFFFFF
  220. # define IS_QUIC_METHOD(m) \
  221. ((m) == OSSL_QUIC_client_method() || \
  222. (m) == OSSL_QUIC_client_thread_method())
  223. # define IS_QUIC_CTX(ctx) IS_QUIC_METHOD((ctx)->method)
  224. # define QUIC_CONNECTION_FROM_SSL_int(ssl, c) \
  225. ((ssl) == NULL ? NULL \
  226. : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
  227. ? (c QUIC_CONNECTION *)(ssl) \
  228. : NULL))
  229. # define QUIC_XSO_FROM_SSL_int(ssl, c) \
  230. ((ssl) == NULL \
  231. ? NULL \
  232. : (((ssl)->type == SSL_TYPE_QUIC_XSO \
  233. ? (c QUIC_XSO *)(ssl) \
  234. : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
  235. ? (c QUIC_XSO *)((QUIC_CONNECTION *)(ssl))->default_xso \
  236. : NULL))))
  237. # define SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, c) \
  238. ((ssl) == NULL ? NULL \
  239. : ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
  240. ? (c SSL_CONNECTION *)((c QUIC_CONNECTION *)(ssl))->tls \
  241. : NULL))
  242. # define IS_QUIC(ssl) ((ssl) != NULL \
  243. && ((ssl)->type == SSL_TYPE_QUIC_CONNECTION \
  244. || (ssl)->type == SSL_TYPE_QUIC_XSO))
  245. # else
  246. # define QUIC_CONNECTION_FROM_SSL_int(ssl, c) NULL
  247. # define QUIC_XSO_FROM_SSL_int(ssl, c) NULL
  248. # define SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, c) NULL
  249. # define IS_QUIC(ssl) 0
  250. # define IS_QUIC_CTX(ctx) 0
  251. # define IS_QUIC_METHOD(m) 0
  252. # endif
  253. # define QUIC_CONNECTION_FROM_SSL(ssl) \
  254. QUIC_CONNECTION_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
  255. # define QUIC_CONNECTION_FROM_CONST_SSL(ssl) \
  256. QUIC_CONNECTION_FROM_SSL_int(ssl, const)
  257. # define QUIC_XSO_FROM_SSL(ssl) \
  258. QUIC_XSO_FROM_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
  259. # define QUIC_XSO_FROM_CONST_SSL(ssl) \
  260. QUIC_XSO_FROM_SSL_int(ssl, const)
  261. # define SSL_CONNECTION_FROM_QUIC_SSL(ssl) \
  262. SSL_CONNECTION_FROM_QUIC_SSL_int(ssl, SSL_CONNECTION_NO_CONST)
  263. # define SSL_CONNECTION_FROM_CONST_QUIC_SSL(ssl) \
  264. SSL_CONNECTION_FROM_CONST_QUIC_SSL_int(ssl, const)
  265. # define IMPLEMENT_quic_meth_func(version, func_name, q_accept, \
  266. q_connect, enc_data) \
  267. const SSL_METHOD *func_name(void) \
  268. { \
  269. static const SSL_METHOD func_name##_data= { \
  270. version, \
  271. 0, \
  272. 0, \
  273. ossl_quic_new, \
  274. ossl_quic_free, \
  275. ossl_quic_reset, \
  276. ossl_quic_init, \
  277. NULL /* clear */, \
  278. ossl_quic_deinit, \
  279. q_accept, \
  280. q_connect, \
  281. ossl_quic_read, \
  282. ossl_quic_peek, \
  283. ossl_quic_write, \
  284. NULL /* shutdown */, \
  285. NULL /* renegotiate */, \
  286. ossl_quic_renegotiate_check, \
  287. NULL /* read_bytes */, \
  288. NULL /* write_bytes */, \
  289. NULL /* dispatch_alert */, \
  290. ossl_quic_ctrl, \
  291. ossl_quic_ctx_ctrl, \
  292. ossl_quic_get_cipher_by_char, \
  293. NULL /* put_cipher_by_char */, \
  294. ossl_quic_pending, \
  295. ossl_quic_num_ciphers, \
  296. ossl_quic_get_cipher, \
  297. tls1_default_timeout, \
  298. &enc_data, \
  299. ssl_undefined_void_function, \
  300. ossl_quic_callback_ctrl, \
  301. ossl_quic_ctx_callback_ctrl, \
  302. }; \
  303. return &func_name##_data; \
  304. }
  305. #endif