Path: blob/main/crypto/openssl/ssl/quic/quic_obj_local.h
48261 views
/*1* Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#ifndef OSSL_QUIC_OBJ_LOCAL_H10# define OSSL_QUIC_OBJ_LOCAL_H1112# include <openssl/ssl.h>13# include "internal/quic_predef.h"14# include "internal/quic_engine.h"15# include "../ssl_local.h"1617# ifndef OPENSSL_NO_QUIC1819/*20* QUIC Object Structure.21*22* In the libssl APL, we have QLSOs, QCSOs and QSSOs, and in the future might23* choose to introduce QDSOs. There are also roles such as Port Leader and Event24* Leader which can be assumed by these different types under different25* circumstances — in other words, whether an APL object is a Port or Event26* Leader is not a static function of its type and these roles can 'float'27* dynamically depending on the circumstances under which an APL object was28* created.29*30* The QUIC_OBJ is a base type for QUIC APL objects which provides functionality31* common to all QUIC objects and which supports having different APL objects32* dynamically assume leader roles. It can therefore be seen as an extension of33* the SSL base class and extends the SSL object for QUIC APL objects. This34* avoids duplication of functionality for different types of QUIC object and35* allows access to common responsibilities of different types of APL object36* without regard to the kind of APL object we are dealing with.37*38* The "inheritance" hierarchy is as follows:39*40* SSL41* SSL_CONNECTION42* QUIC_OBJ43* QUIC_DOMAIN (QDSO) -> QUIC_ENGINE *E44* QUIC_LISTENER (QLSO) -> QUIC_PORT eP45* QUIC_CONNECTION (QCSO) -> QUIC_CHANNEL epCs46* QUIC_XSO (QSSO) -> QUIC_STREAM S47*48* Legend:49*50* *: Not currently modelled in the APL, though QUIC_ENGINE exists internally.51*52* E: Always an event leader if it exists.53* e: Potentially an event leader (namely if it is the root APL object in a54* hierarchy).55*56* P: Always a port leader if it exists.57* p: Potentially a port leader (namely if there is no port leader above it).58*59* C: Always a connection leader.60*61* s: Potentially usable as a stream (if it has a default stream attached).62* S: Always has the stream role if it exists.63*64* This structure must come at the start of a QUIC object structure definition.65*66* ssl->type still determines the actual object type. An SSL object67* pointer s can be safely cast to (QUIC_OBJ *) iff IS_QUIC(s) is true.68*/69struct quic_obj_st {70/* SSL object common header. */71struct ssl_st ssl;7273/*74* Pointer to a parent APL object in a QUIC APL object hierarchy, or NULL if75* this is the root object.76*/77QUIC_OBJ *parent_obj;7879/* invariant: != NULL */80QUIC_OBJ *cached_event_leader;81/* invariant: != NULL iff this is a port leader or subsidiary object */82QUIC_OBJ *cached_port_leader;8384/*85* Points to the QUIC_ENGINE instance. Always equals86* cached_event_leader->engine. The containing_obj APL object owns this87* instance iff is_event_leader is set, otherwise it is an additional88* reference cached for convenience. Unlike port this is never NULL because89* a QUIC domain is always rooted in an event leader.90*/91QUIC_ENGINE *engine;9293/*94* Points to the QUIC_PORT instance applicable to the containing_obj APL95* object, or NULL if we are not at or below a port leader. Always equals96* cached_port_leader->port. The containing_obj APL object owns this97* instance iff is_port_leader is set, otherwise it is an additional98* reference cached for convenience.99*/100QUIC_PORT *port;101102/* SSL_DOMAIN_FLAG values taken from SSL_CTX at construction time. */103uint64_t domain_flags;104105unsigned int init_done : 1;106unsigned int is_event_leader : 1;107unsigned int is_port_leader : 1;108109/*110* Blocking mode configuration is handled generically through QUIC_OBJ as it111* by default inherits from the parent SSL object.112*/113unsigned int req_blocking_mode : 2; /* QUIC_BLOCKING_MODE */114115/* Event handling mode. One of SSL_QUIC_VALUE_EVENT_HANDLING. */116unsigned int event_handling_mode : 2;117};118119enum {120QUIC_BLOCKING_MODE_INHERIT,121QUIC_BLOCKING_MODE_NONBLOCKING,122QUIC_BLOCKING_MODE_BLOCKING123};124125/*126* Core Functions and Inlines127* ==========================128*/129130/*131* Initialises a QUIC_OBJ structure with zero or more roles active. Returns 1132* on success or 0 on failure.133*134* ctx: A SSL_CTX used to initialise the SSL base object structure.135*136* type: A SSL_TYPE_* value designating the SSL object type.137*138* parent_obj: NULL if this is the root APL object in a new hierarchy, or a139* pointer to the parent APL object otherwise.140*141* engine: If non-NULL, this object becomes the Event Leader. parent_obj must be142* NULL iff this is non-NULL as currently the Event Leader is always the root in143* an APL object hierarchy. If NULL, the contextually applicable engine is144* determined by using parent_obj and ancestors to find the Event Leader.145*146* port: If non-NULL, this object becomes a Port Leader. If NULL, the147* contextually applicable port (if any) is determined by using parent_obj and148* ancestors to find the Port Leader.149*/150int ossl_quic_obj_init(QUIC_OBJ *obj,151SSL_CTX *ctx,152int type,153SSL *parent_obj,154QUIC_ENGINE *engine,155QUIC_PORT *port);156157/*158* Returns a pointer to the handshake layer object which should be accessible on159* obj for purposes of handshake API autoforwarding, if any.160*161* This returns NULL if a handshake layer SSL object is available but should not162* be used for autoforwarding purposes, for example on a QSSO.163*/164SSL_CONNECTION *ossl_quic_obj_get0_handshake_layer(QUIC_OBJ *obj);165166/*167* Returns a pointer to the SSL base object structure. Returns NULL if obj is168* NULL. If obj is non-NULL, it must be initialised.169*/170static ossl_inline ossl_unused SSL *171ossl_quic_obj_get0_ssl(QUIC_OBJ *obj)172{173/*174* ->ssl is guaranteed to have an offset of 0 but the NULL check here makes175* ubsan happy.176*/177if (!ossl_assert(obj != NULL))178return NULL;179180return &obj->ssl;181}182183/*184* Determines the applicable engine and return a pointer to it. Never returns185* NULL.186*/187static ossl_inline ossl_unused QUIC_ENGINE *188ossl_quic_obj_get0_engine(const QUIC_OBJ *obj)189{190assert(obj->init_done);191assert(obj->engine != NULL);192return obj->engine;193}194195/* Determines the applicable port (if any) and returns a pointer to it. */196static ossl_inline ossl_unused QUIC_PORT *197ossl_quic_obj_get0_port(const QUIC_OBJ *obj)198{199assert(obj->init_done);200return obj->port;201}202203/* Returns 1 iff this leader structure represents an event leader. */204static ossl_inline ossl_unused int205ossl_quic_obj_is_event_leader(const QUIC_OBJ *obj)206{207return obj->is_event_leader;208}209210/*211* Similar to ossl_quic_obj_get0_engine, but only returns a non-NULL value if212* the obj object itself is an event leader, rather than one of its ancestors.213*/214static ossl_inline ossl_unused QUIC_ENGINE *215ossl_quic_obj_get0_engine_local(const QUIC_OBJ *obj)216{217return ossl_quic_obj_is_event_leader(obj)218? ossl_quic_obj_get0_engine(obj) : NULL;219}220221/* Returns 1 iff this leader structure represents a port leader. */222static ossl_inline ossl_unused int223ossl_quic_obj_is_port_leader(const QUIC_OBJ *obj)224{225return obj->is_port_leader;226}227228/*229* Similar to ossl_quic_obj_get0_port, but only returns a non-NULL value if230* the obj object itself is a port leader, rather than one of its ancestors.231*/232static ossl_inline ossl_unused QUIC_PORT *233ossl_quic_obj_get0_port_local(const QUIC_OBJ *obj)234{235return ossl_quic_obj_is_port_leader(obj)236? ossl_quic_obj_get0_port(obj) : NULL;237}238239/*240* Return 1 if we are currently capable of supporting blocking mode (regardless241* of whether it is actually turned on).242*/243int ossl_quic_obj_can_support_blocking(const QUIC_OBJ *obj);244245/*246* Returns 1 if we *desire* to do blocking I/O, regardless of whether it will247* actually be used (e.g. because it cannot currently be supported).248*/249int ossl_quic_obj_desires_blocking(const QUIC_OBJ *obj);250251/*252* Return 1 if an API call directly to the given object should use blocking mode253* and 0 otherwise.254*/255int ossl_quic_obj_blocking(const QUIC_OBJ *obj);256257/*258* Set the (requested) blocking mode, which might or might not be honoured259* depending on whether the BIO configuration can support it. Argument is a260* QUIC_BLOCKING_MODE value. If the top-level object in a QSO hierarchy is set261* to QUIC_BLOCKING_MODE_INHERIT, defaults to blocking mode.262*/263void ossl_quic_obj_set_blocking_mode(QUIC_OBJ *obj, unsigned int mode);264265/*266* Convenience Inlines267* ===================268*269* These inlines are expressed in terms of the core functions and inlines above.270*/271272/* Get a pointer to the QUIC domain mutex. Always returns non-NULL. */273static ossl_inline ossl_unused CRYPTO_MUTEX *274ossl_quic_obj_get0_mutex(const QUIC_OBJ *obj)275{276return ossl_quic_engine_get0_mutex(ossl_quic_obj_get0_engine(obj));277}278279/*280* Get a reference to the reactor applicable to a leader. Always returns281* non-NULL.282*/283static ossl_inline ossl_unused QUIC_REACTOR *284ossl_quic_obj_get0_reactor(const QUIC_OBJ *obj)285{286return ossl_quic_engine_get0_reactor(ossl_quic_obj_get0_engine(obj));287}288289/* Get a reference to the OSSL_LIB_CTX pointer applicable to a leader. */290static ossl_inline ossl_unused OSSL_LIB_CTX *291ossl_quic_obj_get0_libctx(const QUIC_OBJ *obj)292{293return ossl_quic_engine_get0_libctx(ossl_quic_obj_get0_engine(obj));294}295296/* Get a reference to the propq pointer applicable to a leader. */297static ossl_inline ossl_unused const char *298ossl_quic_obj_get0_propq(const QUIC_OBJ *obj)299{300return ossl_quic_engine_get0_propq(ossl_quic_obj_get0_engine(obj));301}302303/*304* Returns the APL object pointer to the event leader in a hierarchy. Always305* returns non-NULL.306*/307static ossl_inline ossl_unused SSL *308ossl_quic_obj_get0_event_leader(const QUIC_OBJ *obj)309{310assert(obj->init_done);311return obj->cached_event_leader != NULL312? &obj->cached_event_leader->ssl313: NULL;314}315316/*317* Returns the APL object pointer to the port leader in a hierarchy (if any).318* Always returns non-NULL.319*/320static ossl_inline ossl_unused SSL *321ossl_quic_obj_get0_port_leader(const QUIC_OBJ *obj)322{323assert(obj->init_done);324return obj->cached_port_leader != NULL325? &obj->cached_port_leader->ssl326: NULL;327}328329/*330* Change the domain flags. Should only be called immediately after331* ossl_quic_obj_init().332*/333static ossl_inline ossl_unused void334ossl_quic_obj_set_domain_flags(QUIC_OBJ *obj, uint64_t domain_flags)335{336obj->domain_flags = domain_flags;337}338339# endif340#endif341342343