quic_obj_local.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright 2024-2025 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_OBJ_LOCAL_H
  10. # define OSSL_QUIC_OBJ_LOCAL_H
  11. # include <openssl/ssl.h>
  12. # include "internal/quic_predef.h"
  13. # include "internal/quic_engine.h"
  14. # include "../ssl_local.h"
  15. # ifndef OPENSSL_NO_QUIC
  16. /*
  17. * QUIC Object Structure.
  18. *
  19. * In the libssl APL, we have QLSOs, QCSOs and QSSOs, and in the future might
  20. * choose to introduce QDSOs. There are also roles such as Port Leader and Event
  21. * Leader which can be assumed by these different types under different
  22. * circumstances — in other words, whether an APL object is a Port or Event
  23. * Leader is not a static function of its type and these roles can 'float'
  24. * dynamically depending on the circumstances under which an APL object was
  25. * created.
  26. *
  27. * The QUIC_OBJ is a base type for QUIC APL objects which provides functionality
  28. * common to all QUIC objects and which supports having different APL objects
  29. * dynamically assume leader roles. It can therefore be seen as an extension of
  30. * the SSL base class and extends the SSL object for QUIC APL objects. This
  31. * avoids duplication of functionality for different types of QUIC object and
  32. * allows access to common responsibilities of different types of APL object
  33. * without regard to the kind of APL object we are dealing with.
  34. *
  35. * The "inheritance" hierarchy is as follows:
  36. *
  37. * SSL
  38. * SSL_CONNECTION
  39. * QUIC_OBJ
  40. * QUIC_DOMAIN (QDSO) -> QUIC_ENGINE *E
  41. * QUIC_LISTENER (QLSO) -> QUIC_PORT eP
  42. * QUIC_CONNECTION (QCSO) -> QUIC_CHANNEL epCs
  43. * QUIC_XSO (QSSO) -> QUIC_STREAM S
  44. *
  45. * Legend:
  46. *
  47. * *: Not currently modelled in the APL, though QUIC_ENGINE exists internally.
  48. *
  49. * E: Always an event leader if it exists.
  50. * e: Potentially an event leader (namely if it is the root APL object in a
  51. * hierarchy).
  52. *
  53. * P: Always a port leader if it exists.
  54. * p: Potentially a port leader (namely if there is no port leader above it).
  55. *
  56. * C: Always a connection leader.
  57. *
  58. * s: Potentially usable as a stream (if it has a default stream attached).
  59. * S: Always has the stream role if it exists.
  60. *
  61. * This structure must come at the start of a QUIC object structure definition.
  62. *
  63. * ssl->type still determines the actual object type. An SSL object
  64. * pointer s can be safely cast to (QUIC_OBJ *) iff IS_QUIC(s) is true.
  65. */
  66. struct quic_obj_st {
  67. /* SSL object common header. */
  68. struct ssl_st ssl;
  69. /*
  70. * Pointer to a parent APL object in a QUIC APL object hierarchy, or NULL if
  71. * this is the root object.
  72. */
  73. QUIC_OBJ *parent_obj;
  74. /* invariant: != NULL */
  75. QUIC_OBJ *cached_event_leader;
  76. /* invariant: != NULL iff this is a port leader or subsidiary object */
  77. QUIC_OBJ *cached_port_leader;
  78. /*
  79. * Points to the QUIC_ENGINE instance. Always equals
  80. * cached_event_leader->engine. The containing_obj APL object owns this
  81. * instance iff is_event_leader is set, otherwise it is an additional
  82. * reference cached for convenience. Unlike port this is never NULL because
  83. * a QUIC domain is always rooted in an event leader.
  84. */
  85. QUIC_ENGINE *engine;
  86. /*
  87. * Points to the QUIC_PORT instance applicable to the containing_obj APL
  88. * object, or NULL if we are not at or below a port leader. Always equals
  89. * cached_port_leader->port. The containing_obj APL object owns this
  90. * instance iff is_port_leader is set, otherwise it is an additional
  91. * reference cached for convenience.
  92. */
  93. QUIC_PORT *port;
  94. /* SSL_DOMAIN_FLAG values taken from SSL_CTX at construction time. */
  95. uint64_t domain_flags;
  96. unsigned int init_done : 1;
  97. unsigned int is_event_leader : 1;
  98. unsigned int is_port_leader : 1;
  99. /*
  100. * Blocking mode configuration is handled generically through QUIC_OBJ as it
  101. * by default inherits from the parent SSL object.
  102. */
  103. unsigned int req_blocking_mode : 2; /* QUIC_BLOCKING_MODE */
  104. /* Event handling mode. One of SSL_QUIC_VALUE_EVENT_HANDLING. */
  105. unsigned int event_handling_mode : 2;
  106. };
  107. enum {
  108. QUIC_BLOCKING_MODE_INHERIT,
  109. QUIC_BLOCKING_MODE_NONBLOCKING,
  110. QUIC_BLOCKING_MODE_BLOCKING
  111. };
  112. /*
  113. * Core Functions and Inlines
  114. * ==========================
  115. */
  116. /*
  117. * Initialises a QUIC_OBJ structure with zero or more roles active. Returns 1
  118. * on success or 0 on failure.
  119. *
  120. * ctx: A SSL_CTX used to initialise the SSL base object structure.
  121. *
  122. * type: A SSL_TYPE_* value designating the SSL object type.
  123. *
  124. * parent_obj: NULL if this is the root APL object in a new hierarchy, or a
  125. * pointer to the parent APL object otherwise.
  126. *
  127. * engine: If non-NULL, this object becomes the Event Leader. parent_obj must be
  128. * NULL iff this is non-NULL as currently the Event Leader is always the root in
  129. * an APL object hierarchy. If NULL, the contextually applicable engine is
  130. * determined by using parent_obj and ancestors to find the Event Leader.
  131. *
  132. * port: If non-NULL, this object becomes a Port Leader. If NULL, the
  133. * contextually applicable port (if any) is determined by using parent_obj and
  134. * ancestors to find the Port Leader.
  135. */
  136. int ossl_quic_obj_init(QUIC_OBJ *obj,
  137. SSL_CTX *ctx,
  138. int type,
  139. SSL *parent_obj,
  140. QUIC_ENGINE *engine,
  141. QUIC_PORT *port);
  142. /*
  143. * Returns a pointer to the handshake layer object which should be accessible on
  144. * obj for purposes of handshake API autoforwarding, if any.
  145. *
  146. * This returns NULL if a handshake layer SSL object is available but should not
  147. * be used for autoforwarding purposes, for example on a QSSO.
  148. */
  149. SSL_CONNECTION *ossl_quic_obj_get0_handshake_layer(QUIC_OBJ *obj);
  150. /*
  151. * Returns a pointer to the SSL base object structure. Returns NULL if obj is
  152. * NULL. If obj is non-NULL, it must be initialised.
  153. */
  154. static ossl_inline ossl_unused SSL *
  155. ossl_quic_obj_get0_ssl(QUIC_OBJ *obj)
  156. {
  157. /*
  158. * ->ssl is guaranteed to have an offset of 0 but the NULL check here makes
  159. * ubsan happy.
  160. */
  161. if (!ossl_assert(obj != NULL))
  162. return NULL;
  163. return &obj->ssl;
  164. }
  165. /*
  166. * Determines the applicable engine and return a pointer to it. Never returns
  167. * NULL.
  168. */
  169. static ossl_inline ossl_unused QUIC_ENGINE *
  170. ossl_quic_obj_get0_engine(const QUIC_OBJ *obj)
  171. {
  172. assert(obj->init_done);
  173. assert(obj->engine != NULL);
  174. return obj->engine;
  175. }
  176. /* Determines the applicable port (if any) and returns a pointer to it. */
  177. static ossl_inline ossl_unused QUIC_PORT *
  178. ossl_quic_obj_get0_port(const QUIC_OBJ *obj)
  179. {
  180. assert(obj->init_done);
  181. return obj->port;
  182. }
  183. /* Returns 1 iff this leader structure represents an event leader. */
  184. static ossl_inline ossl_unused int
  185. ossl_quic_obj_is_event_leader(const QUIC_OBJ *obj)
  186. {
  187. return obj->is_event_leader;
  188. }
  189. /*
  190. * Similar to ossl_quic_obj_get0_engine, but only returns a non-NULL value if
  191. * the obj object itself is an event leader, rather than one of its ancestors.
  192. */
  193. static ossl_inline ossl_unused QUIC_ENGINE *
  194. ossl_quic_obj_get0_engine_local(const QUIC_OBJ *obj)
  195. {
  196. return ossl_quic_obj_is_event_leader(obj)
  197. ? ossl_quic_obj_get0_engine(obj) : NULL;
  198. }
  199. /* Returns 1 iff this leader structure represents a port leader. */
  200. static ossl_inline ossl_unused int
  201. ossl_quic_obj_is_port_leader(const QUIC_OBJ *obj)
  202. {
  203. return obj->is_port_leader;
  204. }
  205. /*
  206. * Similar to ossl_quic_obj_get0_port, but only returns a non-NULL value if
  207. * the obj object itself is a port leader, rather than one of its ancestors.
  208. */
  209. static ossl_inline ossl_unused QUIC_PORT *
  210. ossl_quic_obj_get0_port_local(const QUIC_OBJ *obj)
  211. {
  212. return ossl_quic_obj_is_port_leader(obj)
  213. ? ossl_quic_obj_get0_port(obj) : NULL;
  214. }
  215. /*
  216. * Return 1 if we are currently capable of supporting blocking mode (regardless
  217. * of whether it is actually turned on).
  218. */
  219. int ossl_quic_obj_can_support_blocking(const QUIC_OBJ *obj);
  220. /*
  221. * Returns 1 if we *desire* to do blocking I/O, regardless of whether it will
  222. * actually be used (e.g. because it cannot currently be supported).
  223. */
  224. int ossl_quic_obj_desires_blocking(const QUIC_OBJ *obj);
  225. /*
  226. * Return 1 if an API call directly to the given object should use blocking mode
  227. * and 0 otherwise.
  228. */
  229. int ossl_quic_obj_blocking(const QUIC_OBJ *obj);
  230. /*
  231. * Set the (requested) blocking mode, which might or might not be honoured
  232. * depending on whether the BIO configuration can support it. Argument is a
  233. * QUIC_BLOCKING_MODE value. If the top-level object in a QSO hierarchy is set
  234. * to QUIC_BLOCKING_MODE_INHERIT, defaults to blocking mode.
  235. */
  236. void ossl_quic_obj_set_blocking_mode(QUIC_OBJ *obj, unsigned int mode);
  237. /*
  238. * Convenience Inlines
  239. * ===================
  240. *
  241. * These inlines are expressed in terms of the core functions and inlines above.
  242. */
  243. /* Get a pointer to the QUIC domain mutex. Always returns non-NULL. */
  244. static ossl_inline ossl_unused CRYPTO_MUTEX *
  245. ossl_quic_obj_get0_mutex(const QUIC_OBJ *obj)
  246. {
  247. return ossl_quic_engine_get0_mutex(ossl_quic_obj_get0_engine(obj));
  248. }
  249. /*
  250. * Get a reference to the reactor applicable to a leader. Always returns
  251. * non-NULL.
  252. */
  253. static ossl_inline ossl_unused QUIC_REACTOR *
  254. ossl_quic_obj_get0_reactor(const QUIC_OBJ *obj)
  255. {
  256. return ossl_quic_engine_get0_reactor(ossl_quic_obj_get0_engine(obj));
  257. }
  258. /* Get a reference to the OSSL_LIB_CTX pointer applicable to a leader. */
  259. static ossl_inline ossl_unused OSSL_LIB_CTX *
  260. ossl_quic_obj_get0_libctx(const QUIC_OBJ *obj)
  261. {
  262. return ossl_quic_engine_get0_libctx(ossl_quic_obj_get0_engine(obj));
  263. }
  264. /* Get a reference to the propq pointer applicable to a leader. */
  265. static ossl_inline ossl_unused const char *
  266. ossl_quic_obj_get0_propq(const QUIC_OBJ *obj)
  267. {
  268. return ossl_quic_engine_get0_propq(ossl_quic_obj_get0_engine(obj));
  269. }
  270. /*
  271. * Returns the APL object pointer to the event leader in a hierarchy. Always
  272. * returns non-NULL.
  273. */
  274. static ossl_inline ossl_unused SSL *
  275. ossl_quic_obj_get0_event_leader(const QUIC_OBJ *obj)
  276. {
  277. assert(obj->init_done);
  278. return obj->cached_event_leader != NULL
  279. ? &obj->cached_event_leader->ssl
  280. : NULL;
  281. }
  282. /*
  283. * Returns the APL object pointer to the port leader in a hierarchy (if any).
  284. * Always returns non-NULL.
  285. */
  286. static ossl_inline ossl_unused SSL *
  287. ossl_quic_obj_get0_port_leader(const QUIC_OBJ *obj)
  288. {
  289. assert(obj->init_done);
  290. return obj->cached_port_leader != NULL
  291. ? &obj->cached_port_leader->ssl
  292. : NULL;
  293. }
  294. /*
  295. * Change the domain flags. Should only be called immediately after
  296. * ossl_quic_obj_init().
  297. */
  298. static ossl_inline ossl_unused void
  299. ossl_quic_obj_set_domain_flags(QUIC_OBJ *obj, uint64_t domain_flags)
  300. {
  301. obj->domain_flags = domain_flags;
  302. }
  303. # endif
  304. #endif