quic_local.h 13 KB

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