From 9cccdd11ebe775b4e4f92f383841cddb7e9dd218 Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Thu, 21 Nov 2024 20:43:02 +0300 Subject: [PATCH] TLS backends: improved readability, renames, corrected comments --- src/mhd2/daemon_start.c | 4 +-- src/mhd2/mhd_daemon.h | 10 +++++-- src/mhd2/mhd_tls_choice.h | 22 +++++++++++---- src/mhd2/mhd_tls_funcs.h | 31 ++++++++++----------- src/mhd2/tls_gnu_conn_data.h | 2 +- src/mhd2/tls_gnu_daemon_data.h | 2 +- src/mhd2/tls_gnu_funcs.c | 51 ++++++++++++++++++---------------- src/mhd2/tls_gnu_funcs.h | 20 ++++++------- 8 files changed, 79 insertions(+), 63 deletions(-) diff --git a/src/mhd2/daemon_start.c b/src/mhd2/daemon_start.c index bf4c1f5d..308cb510 100644 --- a/src/mhd2/daemon_start.c +++ b/src/mhd2/daemon_start.c @@ -1498,8 +1498,8 @@ daemon_init_tls (struct MHD_Daemon *restrict d, } } ret = mhd_tls_daemon_init (d, - &(d->tls), - s); + s, + &(d->tls)); mhd_assert ((MHD_SC_OK == ret) || (NULL == d->tls)); mhd_assert ((MHD_SC_OK != ret) || (NULL != d->tls)); #ifndef NDEBUG diff --git a/src/mhd2/mhd_daemon.h b/src/mhd2/mhd_daemon.h index 028f100d..cf7e22dc 100644 --- a/src/mhd2/mhd_daemon.h +++ b/src/mhd2/mhd_daemon.h @@ -924,7 +924,7 @@ struct MHD_Daemon * If set to non-NULL then HTTPS protocol is used, if set to NULL then * plain HTTP protocol used. */ - struct mhd_DaemonTlsData *tls; + struct mhd_TlsDaemonData *tls; #endif #ifdef MHD_USE_THREADS @@ -1017,8 +1017,14 @@ struct MHD_Daemon (mhd_WM_INT_EXTERNAL_EVENTS_EDGE ==((d)->wmode_int))) #ifdef MHD_ENABLE_HTTPS -# define mhd_D_HAS_TLS(d) ((d->tls) ? (! 0) : (0)) +/** + * Returns non-zero if daemon has TLS enabled or zero otherwise + */ +# define mhd_D_HAS_TLS(d) (((d)->tls) ? (! 0) : (0)) #else +/** + * Returns non-zero if daemon has TLS enabled or zero otherwise + */ # define mhd_D_HAS_TLS(d) (0) #endif diff --git a/src/mhd2/mhd_tls_choice.h b/src/mhd2/mhd_tls_choice.h index 27ec4964..90adf9e5 100644 --- a/src/mhd2/mhd_tls_choice.h +++ b/src/mhd2/mhd_tls_choice.h @@ -113,26 +113,36 @@ # define mhd_TLS_MACRO_NAME_ID GNU #endif +/** + * Form function name specific for the selected TLS backend + */ +#define mhd_TLS_DATA(name_suffix) \ + mhd_MACRO_CONCAT3 (mhd_Tls,mhd_TLS_DATA_NAME_ID,name_suffix) + +/** + * Form name of the data specific for the selected TLS backend + */ +#define mhd_TLS_FUNC(name_suffix) \ + mhd_MACRO_CONCAT3 (mhd_tls_,mhd_TLS_FUNC_NAME_ID,name_suffix) + /** * The name of the structure that holds daemon-specific TLS data */ -#define mhd_DaemonTlsData \ - mhd_MACRO_CONCAT3 (mhd_DaemonTls,mhd_TLS_DATA_NAME_ID,Data) +#define mhd_TlsDaemonData mhd_TLS_DATA (DaemonData) /** * The name of the structure that holds connection-specific TLS data */ -#define mhd_ConnTlsData \ - mhd_MACRO_CONCAT3 (mhd_ConnTls,mhd_TLS_DATA_NAME_ID,Data) +#define mhd_TlsConnData mhd_TLS_DATA (ConnData) /** * The structure with daemon-specific TLS data */ -struct mhd_DaemonTlsData; /* Forward declaration */ +struct mhd_TlsDaemonData; /* Forward declaration */ /** * The structure with connection-specific TLS data */ -struct mhd_ConnTlsData; /* Forward declaration */ +struct mhd_TlsConnData; /* Forward declaration */ #endif /* ! MHD_TLS_CHOICE_H */ diff --git a/src/mhd2/mhd_tls_funcs.h b/src/mhd2/mhd_tls_funcs.h index 2bf30733..46486aaf 100644 --- a/src/mhd2/mhd_tls_funcs.h +++ b/src/mhd2/mhd_tls_funcs.h @@ -37,7 +37,9 @@ #error This header should be used only if HTTPS is enabled #endif -#ifdef MHD_USE_GNUTLS +#if defined(MHD_USE_MULTITLS) +// TODO: Multi-TLS implementation +#elif defined(MHD_USE_GNUTLS) # include "tls_gnu_funcs.h" #endif @@ -45,40 +47,36 @@ # define mhd_tls_gnu_is_inited_fine() (0) #endif -/* ** Global initialisation ** */ +/* ** Global initialisation / de-initialisation ** */ /** * Perform one-time global initialisation of TLS backend */ -#define mhd_tls_global_init_once() \ - mhd_MACRO_CONCAT3 (mhd_tls_,mhd_TLS_FUNC_NAME_ID,_global_init_once)() +#define mhd_tls_global_init_once() mhd_TLS_FUNC (_global_init_once)() /** * Perform de-initialisation of TLS backend */ -#define mhd_tls_global_deinit() \ - mhd_MACRO_CONCAT3 (mhd_tls_,mhd_TLS_FUNC_NAME_ID,_global_deinit)() +#define mhd_tls_global_deinit() mhd_TLS_FUNC (_global_deinit)() /** * Perform re-initialisation of TLS backend */ -#define mhd_tls_global_re_init() \ - mhd_MACRO_CONCAT3 (mhd_tls_,mhd_TLS_FUNC_NAME_ID,_global_re_init)() +#define mhd_tls_global_re_init() mhd_TLS_FUNC (_global_re_init)() -/* ** Daemon initialisation ** */ +/* ** Daemon initialisation / de-initialisation ** */ /** - * Set daemon TLS parameters + * Allocate and initialise daemon TLS parameters * @param d the daemon handle + * @param s the daemon settings * @param p_d_tls the pointer to variable to set the pointer to * the daemon's TLS settings (allocated by this function) - * @param s the daemon settings * @return #MHD_SC_OK on success (p_d_tls set to the allocated settings), * error code otherwise */ -#define mhd_tls_daemon_init(d,p_d_tls,s) \ - mhd_MACRO_CONCAT3 (mhd_tls_,mhd_TLS_FUNC_NAME_ID,_daemon_init)( \ - (d),(p_d_tls),(s)) +#define mhd_tls_daemon_init(d,s,p_d_tls) \ + mhd_TLS_FUNC (_daemon_init)((d),(s),(p_d_tls)) /** * De-initialise daemon TLS parameters (and free memory allocated for TLS @@ -86,12 +84,11 @@ * @param d_tls the pointer to the daemon's TLS settings */ #define mhd_tls_daemon_deinit(d_tls) \ - mhd_MACRO_CONCAT3 (mhd_tls_,mhd_TLS_FUNC_NAME_ID,_daemon_deinit)( \ - (d_tls)) + mhd_TLS_FUNC (_daemon_deinit)((d_tls)) /** - * Result of TLS backend availablility check + * Result of TLS backend availability check */ enum mhd_TlsBackendAvailable { diff --git a/src/mhd2/tls_gnu_conn_data.h b/src/mhd2/tls_gnu_conn_data.h index a2db40c7..7f3145de 100644 --- a/src/mhd2/tls_gnu_conn_data.h +++ b/src/mhd2/tls_gnu_conn_data.h @@ -38,7 +38,7 @@ /** * The structure with connection-specific GnuTLS data */ -struct mhd_ConnTlsGnuData +struct mhd_TlsGnuConnData { /** * GnuTLS session data diff --git a/src/mhd2/tls_gnu_daemon_data.h b/src/mhd2/tls_gnu_daemon_data.h index 2db67d55..eec4e85b 100644 --- a/src/mhd2/tls_gnu_daemon_data.h +++ b/src/mhd2/tls_gnu_daemon_data.h @@ -38,7 +38,7 @@ /** * The structure with daemon-specific GnuTLS data */ -struct mhd_DaemonTlsGnuData +struct mhd_TlsGnuDaemonData { /** * The credentials diff --git a/src/mhd2/tls_gnu_funcs.c b/src/mhd2/tls_gnu_funcs.c index d641342a..9707d6fd 100644 --- a/src/mhd2/tls_gnu_funcs.c +++ b/src/mhd2/tls_gnu_funcs.c @@ -53,9 +53,9 @@ # include "tls_dh_params.h" #endif -struct mhd_DaemonTlsGnuData; /* Forward declaration */ +struct mhd_TlsGnuDaemonData; /* Forward declaration */ -struct mhd_ConnTlsGnuData; /* Forward declaration */ +struct mhd_TlsGnuConnData; /* Forward declaration */ /* ** Global initialisation ** */ @@ -132,7 +132,7 @@ check_app_tls_sessings (struct MHD_Daemon *restrict d, * 'false' if failed */ static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_MUST_CHECK_RESULT_ bool -daemon_init_dh_data (struct mhd_DaemonTlsGnuData *restrict d_tls) +daemon_init_dh_data (struct mhd_TlsGnuDaemonData *restrict d_tls) { #if defined(mhd_TLS_GNU_DH_PARAMS_USE_KNOWN) /* Rely on reasonable TLS defaults set in the TLS library. @@ -175,7 +175,7 @@ daemon_init_dh_data (struct mhd_DaemonTlsGnuData *restrict d_tls) * @param d_tls the daemon TLS data */ static MHD_FN_PAR_NONNULL_ALL_ void -daemon_deinit_dh_data (struct mhd_DaemonTlsGnuData *restrict d_tls) +daemon_deinit_dh_data (struct mhd_TlsGnuDaemonData *restrict d_tls) { #if defined(mhd_TLS_GNU_DH_PARAMS_NEEDS_PKCS3) mhd_assert (NULL != d_tls->dh_params); @@ -197,10 +197,10 @@ daemon_deinit_dh_data (struct mhd_DaemonTlsGnuData *restrict d_tls) */ static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_MUST_CHECK_RESULT_ enum MHD_StatusCode daemon_init_credentials (struct MHD_Daemon *restrict d, - struct mhd_DaemonTlsGnuData *restrict d_tls, + struct mhd_TlsGnuDaemonData *restrict d_tls, struct DaemonOptions *restrict s) { - enum MHD_StatusCode res; + enum MHD_StatusCode ret; size_t cert_len; size_t key_len; @@ -213,12 +213,15 @@ daemon_init_credentials (struct MHD_Daemon *restrict d, } // TODO: Support multiple certificates - cert_len = strlen (s->tls_cert_key.v_mem_cert) + 1; // TODO: Reuse calculated length - key_len = strlen (s->tls_cert_key.v_mem_key) + 1; // TODO: Reuse calculated length + cert_len = strlen (s->tls_cert_key.v_mem_cert); // TODO: Reuse calculated length + key_len = strlen (s->tls_cert_key.v_mem_key); // TODO: Reuse calculated length + + mhd_assert (0 != cert_len); + mhd_assert (0 != key_len); if (((unsigned int) cert_len != cert_len) || ((unsigned int) key_len != key_len)) - res = MHD_SC_TLS_CONF_BAD_CERT; /* Very unlikely, do not waste space on special message */ + ret = MHD_SC_TLS_CONF_BAD_CERT; /* Very unlikely, do not waste space on special message */ else { gnutls_datum_t cert_data; @@ -238,7 +241,7 @@ daemon_init_credentials (struct MHD_Daemon *restrict d, { mhd_LOG_MSG (d, MHD_SC_TLS_CONF_BAD_CERT, \ "Failed to set the provided TLS certificate"); - res = MHD_SC_TLS_CONF_BAD_CERT; + ret = MHD_SC_TLS_CONF_BAD_CERT; } else { @@ -247,7 +250,7 @@ daemon_init_credentials (struct MHD_Daemon *restrict d, mhd_LOG_MSG (d, MHD_SC_DAEMON_TLS_INIT_FAILED, \ "Failed to initialise Diffie-Hellman parameters " \ "for the daemon"); - res = MHD_SC_DAEMON_TLS_INIT_FAILED; + ret = MHD_SC_DAEMON_TLS_INIT_FAILED; } else return MHD_SC_OK; @@ -255,8 +258,8 @@ daemon_init_credentials (struct MHD_Daemon *restrict d, } gnutls_certificate_free_credentials (d_tls->cred); - mhd_assert (MHD_SC_OK != res); - return res; /* Failure exit point */ + mhd_assert (MHD_SC_OK != ret); + return ret; /* Failure exit point */ } @@ -265,7 +268,7 @@ daemon_init_credentials (struct MHD_Daemon *restrict d, * @param d_tls the daemon TLS settings */ static MHD_FN_PAR_NONNULL_ALL_ void -daemon_deinit_credentials (struct mhd_DaemonTlsGnuData *restrict d_tls) +daemon_deinit_credentials (struct mhd_TlsGnuDaemonData *restrict d_tls) { mhd_assert (NULL != d_tls->cred); /* To avoid dangling pointer to DH data in the credentials, @@ -301,7 +304,7 @@ static const struct MHD_StringNullable tlsgnulib_base_priorities[] = { */ static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_MUST_CHECK_RESULT_ enum MHD_StatusCode daemon_init_priorities_cache (struct MHD_Daemon *restrict d, - struct mhd_DaemonTlsGnuData *restrict d_tls, + struct mhd_TlsGnuDaemonData *restrict d_tls, struct DaemonOptions *restrict s) { size_t i; @@ -345,7 +348,7 @@ daemon_init_priorities_cache (struct MHD_Daemon *restrict d, * @param d_tls the daemon TLS settings */ static MHD_FN_PAR_NONNULL_ALL_ void -daemon_deinit_priorities_cache (struct mhd_DaemonTlsGnuData *restrict d_tls) +daemon_deinit_priorities_cache (struct mhd_TlsGnuDaemonData *restrict d_tls) { #if ! defined(mhd_TLS_GNU_NULL_PRIO_CACHE_MEANS_DEF_PRIORITY) mhd_assert (NULL != d_tls->pri_cache); @@ -356,21 +359,21 @@ daemon_deinit_priorities_cache (struct mhd_DaemonTlsGnuData *restrict d_tls) } -MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ -MHD_FN_PAR_OUT_ (2) mhd_StatusCodeInt +MHD_INTERNAL MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ +MHD_FN_PAR_OUT_ (3) mhd_StatusCodeInt mhd_tls_gnu_daemon_init (struct MHD_Daemon *restrict d, - struct mhd_DaemonTlsGnuData **restrict p_d_tls, - struct DaemonOptions *restrict s) + struct DaemonOptions *restrict s, + struct mhd_TlsGnuDaemonData **restrict p_d_tls) { mhd_StatusCodeInt res; - struct mhd_DaemonTlsGnuData *restrict d_tls; + struct mhd_TlsGnuDaemonData *restrict d_tls; res = check_app_tls_sessings (d, s); if (MHD_SC_OK != res) return res; - d_tls = (struct mhd_DaemonTlsGnuData *) - mhd_calloc (1, sizeof (struct mhd_DaemonTlsGnuData)); + d_tls = (struct mhd_TlsGnuDaemonData *) + mhd_calloc (1, sizeof (struct mhd_TlsGnuDaemonData)); *p_d_tls = d_tls; if (NULL == d_tls) return MHD_SC_DAEMON_MALLOC_FAILURE; @@ -399,7 +402,7 @@ mhd_tls_gnu_daemon_init (struct MHD_Daemon *restrict d, MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) void -mhd_tls_gnu_daemon_deinit (struct mhd_DaemonTlsGnuData *restrict d_tls) +mhd_tls_gnu_daemon_deinit (struct mhd_TlsGnuDaemonData *restrict d_tls) { mhd_assert (NULL != d_tls); daemon_deinit_priorities_cache (d_tls); diff --git a/src/mhd2/tls_gnu_funcs.h b/src/mhd2/tls_gnu_funcs.h index 49c5b00e..5d8cab3c 100644 --- a/src/mhd2/tls_gnu_funcs.h +++ b/src/mhd2/tls_gnu_funcs.h @@ -38,15 +38,15 @@ /** * The structure with daemon-specific GnuTLS data */ -struct mhd_DaemonTlsGnuData; /* Forward declaration */ +struct mhd_TlsGnuDaemonData; /* Forward declaration */ /** * The structure with connection-specific GnuTLS data */ -struct mhd_ConnTlsGnuData; /* Forward declaration */ +struct mhd_TlsGnuConnData; /* Forward declaration */ -/* ** Global initialisation ** */ +/* ** Global initialisation / de-initialisation ** */ /** * Globally initialise GnuTLS backend @@ -73,25 +73,25 @@ MHD_INTERNAL bool mhd_tls_gnu_is_inited_fine (void); -/* ** Daemon initialisation ** */ +/* ** Daemon initialisation / de-initialisation ** */ struct MHD_Daemon; /* Forward declaration */ struct DaemonOptions; /* Forward declaration */ /** - * Set daemon TLS parameters + * Allocate and initialise daemon TLS parameters * @param d the daemon handle + * @param s the daemon settings * @param p_d_tls the pointer to variable to set the pointer to * the daemon's TLS settings (allocated by this function) - * @param s the daemon settings * @return #MHD_SC_OK on success (p_d_tls set to the allocated settings), * error code otherwise */ MHD_INTERNAL mhd_StatusCodeInt mhd_tls_gnu_daemon_init (struct MHD_Daemon *restrict d, - struct mhd_DaemonTlsGnuData **restrict p_d_tls, - struct DaemonOptions *restrict s) -MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (2); + struct DaemonOptions *restrict s, + struct mhd_TlsGnuDaemonData **restrict p_d_tls) +MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (3); /** * De-initialise daemon TLS parameters (and free memory allocated for TLS @@ -99,7 +99,7 @@ MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (2); * @param d_tls the pointer to the daemon's TLS settings */ MHD_INTERNAL void -mhd_tls_gnu_daemon_deinit (struct mhd_DaemonTlsGnuData *restrict d_tls) +mhd_tls_gnu_daemon_deinit (struct mhd_TlsGnuDaemonData *restrict d_tls) MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1); #endif /* ! MHD_TLS_GNU_FUNCS_H */