From 77e6c70f3e0b9c486322ebdf9965709698f55ebf Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Tue, 12 Nov 2024 11:20:43 +0300 Subject: [PATCH] Added basis for TLS infrastructure, implemented GnuTLS global initialisation --- src/mhd2/Makefile.am | 16 +++++ src/mhd2/mhd_lib_init.c | 4 ++ src/mhd2/mhd_tls_choice.h | 138 ++++++++++++++++++++++++++++++++++++++ src/mhd2/mhd_tls_funcs.h | 61 +++++++++++++++++ src/mhd2/tls_gnu_funcs.c | 64 ++++++++++++++++++ src/mhd2/tls_gnu_funcs.h | 67 ++++++++++++++++++ 6 files changed, 350 insertions(+) create mode 100644 src/mhd2/mhd_tls_choice.h create mode 100644 src/mhd2/mhd_tls_funcs.h create mode 100644 src/mhd2/tls_gnu_funcs.c create mode 100644 src/mhd2/tls_gnu_funcs.h diff --git a/src/mhd2/Makefile.am b/src/mhd2/Makefile.am index 2575bf09..0214b45a 100644 --- a/src/mhd2/Makefile.am +++ b/src/mhd2/Makefile.am @@ -106,6 +106,14 @@ upgrade_files = \ upgrade_proc.c upgrade_proc.h \ upgraded_net.c +tls_common_files = \ + mhd_tls_choice.h \ + mhd_tls_funcs.h \ + tls_dh_params.h + +tls_gnu_files = \ + tls_gnu_funcs.c tls_gnu_funcs.h + if HAVE_POST_PARSER libmicrohttpd2_la_SOURCES += $(post_parser_files) endif @@ -114,6 +122,14 @@ if MHD_UPGRADE_SUPPORT libmicrohttpd2_la_SOURCES += $(upgrade_files) endif +if MHD_ENABLE_HTTPS + libmicrohttpd2_la_SOURCES += $(tls_common_files) + +if MHD_USE_GNUTLS + libmicrohttpd2_la_SOURCES += $(tls_gnu_files) +endif +endif + libmicrohttpd2_la_CPPFLAGS = \ $(AM_CPPFLAGS) $(MHD_LIB_CPPFLAGS) $(MHD_TLS_LIB_CPPFLAGS) \ -DBUILDING_MHD_LIB=1 diff --git a/src/mhd2/mhd_lib_init.c b/src/mhd2/mhd_lib_init.c index c96b23fb..0ab7b657 100644 --- a/src/mhd2/mhd_lib_init.c +++ b/src/mhd2/mhd_lib_init.c @@ -46,6 +46,7 @@ #include "mhd_mono_clock.h" #include "mhd_send.h" +#include "mhd_tls_funcs.h" #include "mhd_lib_init.h" #include "mhd_lib_init_auto.h" @@ -353,6 +354,7 @@ mhd_lib_global_full_init_once (void) return false; mhd_mclock_init_once (); mhd_send_init_once (); + mhd_tls_global_init_once (); mhd_lib_fully_inited_once = true; @@ -367,6 +369,7 @@ mhd_lib_global_full_init_once (void) static void mhd_lib_global_full_deinit (void) { + mhd_tls_global_deinit (); mhd_mclock_deinit (); mhd_lib_sockets_deinit_w32 (); } @@ -386,6 +389,7 @@ mhd_lib_global_full_re_init (void) if (! mhd_lib_sockets_init_w32 ()) return false; mhd_mclock_re_init (); + mhd_tls_global_re_init (); return true; } diff --git a/src/mhd2/mhd_tls_choice.h b/src/mhd2/mhd_tls_choice.h new file mode 100644 index 00000000..27ec4964 --- /dev/null +++ b/src/mhd2/mhd_tls_choice.h @@ -0,0 +1,138 @@ +/* + This file is part of GNU libmicrohttpd + Copyright (C) 2024 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + GNU libmicrohttpd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +/** + * @file src/mhd2/mhd_tls_choice.h + * @brief The TLS backend compile-time selection header + * @author Karlson2k (Evgeny Grin) + */ + +#ifndef MHD_TLS_CHOICE_H +#define MHD_TLS_CHOICE_H 1 + +#include "mhd_sys_options.h" + +#ifndef MHD_ENABLE_HTTPS +#error This header should be used only if HTTPS is enabled +#endif + +/** + * Concatenate three arguments literally + */ +#define mhd_MACRO_CONCAT3_(a,b,c) a ## b ## c +/** + * Concatenate three arguments after expansion + */ +#define mhd_MACRO_CONCAT3(a,b,c) mhd_MACRO_CONCAT3_ (a,b,c) + + +#ifdef MHD_USE_GNUTLS +/** + * Defined to one if GnuTLS is enabled at build time or to zero if not enabled + */ +# define mhd_TLS_GNU_ENABLED (1) +#else +/** + * Defined to one if GnuTLS is enabled at build time or to zero if not enabled + */ +# define mhd_TLS_GNU_ENABLED (0) +#endif + +/** + * Return non-zero if GnuTLS is supported + */ +#define mhd_TLS_GNU_IS_SUPPORTED() (! ! mhd_TLS_GNU_ENABLED) + +/** + * Defined to the number of enabled TLS backends + */ +#define mhd_TLS_NUM_BACKENDS \ + (mhd_TLS_GNU_ENABLED /* + mhd_TLS_SOME_ENABLED */) + +#if mhd_TLS_NUM_BACKENDS == 0 +#error At least one TLS backend must be enabled is this header is included +#endif + +#if mhd_TLS_NUM_BACKENDS > 1 +/** + * Defined to '1' if Multi-TLS should be used + */ +# define MHD_USE_MULTITLS +#endif + +#ifdef MHD_USE_MULTITLS +/** + * Defined to one if Multi-TLS is enabled at build time or + * to zero if not enabled + */ +# define mhd_TLS_MULTI_ENABLED (1) +#else +/** + * Defined to one if Multi-TLS is enabled at build time or + * to zero if not enabled + */ +# define mhd_TLS_MULTI_ENABLED (0) +#endif +/** + * Return non-zero if Multi-TLS is supported + */ +#define mhd_TLS_MULTI_IS_SUPPORTED() (! ! mhd_TLS_MULTI_ENABLED) + + +#if defined(MHD_USE_MULTITLS) +// TODO: Multi-TLS implementation +#elif defined(MHD_USE_GNUTLS) +/** + * The TLS back-end identifier for function names + */ +# define mhd_TLS_FUNC_NAME_ID gnu +/** + * The TLS back-end identifier for data names + */ +# define mhd_TLS_DATA_NAME_ID Gnu +/** + * The TLS back-end identifier for macro names + */ +# define mhd_TLS_MACRO_NAME_ID GNU +#endif + +/** + * 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) +/** + * 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) + +/** + * The structure with daemon-specific TLS data + */ +struct mhd_DaemonTlsData; /* Forward declaration */ + +/** + * The structure with connection-specific TLS data + */ +struct mhd_ConnTlsData; /* Forward declaration */ + + +#endif /* ! MHD_TLS_CHOICE_H */ diff --git a/src/mhd2/mhd_tls_funcs.h b/src/mhd2/mhd_tls_funcs.h new file mode 100644 index 00000000..a3ef55ed --- /dev/null +++ b/src/mhd2/mhd_tls_funcs.h @@ -0,0 +1,61 @@ +/* + This file is part of GNU libmicrohttpd + Copyright (C) 2024 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + GNU libmicrohttpd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +/** + * @file src/mhd2/mhd_tls_funcs.h + * @brief The TLS backend functions generic declaration, mapped to specific TLS + * backend at compile-time + * @author Karlson2k (Evgeny Grin) + */ + +#ifndef MHD_TLS_FUNCS_H +#define MHD_TLS_FUNCS_H 1 + +#include "mhd_sys_options.h" + +#include "mhd_tls_choice.h" +#ifndef MHD_ENABLE_HTTPS +#error This header should be used only if HTTPS is enabled +#endif + +#ifdef MHD_USE_GNUTLS +# include "tls_gnu_funcs.h" +#endif + +/** + * 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)() + +/** + * Perform de-initialisation of TLS backend + */ +#define mhd_tls_global_deinit() \ + mhd_MACRO_CONCAT3 (mhd_tls_,mhd_TLS_FUNC_NAME_ID,_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)() + + +#endif /* ! MHD_TLS_FUNCS_H */ diff --git a/src/mhd2/tls_gnu_funcs.c b/src/mhd2/tls_gnu_funcs.c new file mode 100644 index 00000000..f9cf3773 --- /dev/null +++ b/src/mhd2/tls_gnu_funcs.c @@ -0,0 +1,64 @@ +/* + This file is part of GNU libmicrohttpd + Copyright (C) 2024 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + GNU libmicrohttpd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +/** + * @file src/mhd2/tls_gnu_funcs.c + * @brief The implementation of GnuTLS wrapper functions + * @author Karlson2k (Evgeny Grin) + */ + +#include "mhd_sys_options.h" + +#include "sys_bool_type.h" +#include "sys_base_types.h" + +#include + +#include "tls_gnu_funcs.h" + +struct mhd_DaemonTlsGnuData; /* Forward declaration */ + +struct mhd_ConnTlsGnuData; /* Forward declaration */ + + +/* ** Global initialisation ** */ + +static bool gnutls_lib_inited = false; + +MHD_INTERNAL void +mhd_tls_gnu_global_init (void) +{ +#ifdef GNUTLS_VERSION + /* Make sure that used shared GnuTLS library has least the same version as + MHD was configured for. Fail if the version is earlier. */ + gnutls_lib_inited = (NULL != gnutls_check_version (GNUTLS_VERSION)); +#endif + gnutls_lib_inited = + gnutls_lib_inited && (GNUTLS_E_SUCCESS == gnutls_global_init ()); +} + + +MHD_INTERNAL void +mhd_tls_gnu_global_deinit (void) +{ + if (gnutls_lib_inited) + gnutls_global_deinit (); + gnutls_lib_inited = false; +} diff --git a/src/mhd2/tls_gnu_funcs.h b/src/mhd2/tls_gnu_funcs.h new file mode 100644 index 00000000..59a84649 --- /dev/null +++ b/src/mhd2/tls_gnu_funcs.h @@ -0,0 +1,67 @@ +/* + This file is part of GNU libmicrohttpd + Copyright (C) 2024 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + GNU libmicrohttpd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +/** + * @file src/mhd2/tls_gnu_funcs.h + * @brief The declarations of GnuTLS wrapper functions + * @author Karlson2k (Evgeny Grin) + */ + +#ifndef MHD_TLS_GNU_FUNCS_H +#define MHD_TLS_GNU_FUNCS_H 1 + +#include "mhd_sys_options.h" + +#ifndef MHD_USE_GNUTLS +#error This header can be used only if GnuTLS is enabled +#endif + +/** + * The structure with daemon-specific GnuTLS data + */ +struct mhd_DaemonTlsGnuData; /* Forward declaration */ + +/** + * The structure with connection-specific GnuTLS data + */ +struct mhd_ConnTlsGnuData; /* Forward declaration */ + + +/* ** Global initialisation ** */ + +/** + * Globally initialise GnuTLS backend + */ +MHD_INTERNAL void +mhd_tls_gnu_global_init (void); + +/* An alias for mhd_tls_gnu_global_init() */ +#define mhd_tls_gnu_global_init_once() mhd_tls_gnu_global_init () + +/* An alias for mhd_tls_gnu_global_init() */ +#define mhd_tls_gnu_global_re_init() mhd_tls_gnu_global_init () + +/** + * Globally de-initialise GnuTLS backend + */ +MHD_INTERNAL void +mhd_tls_gnu_global_deinit (void); + +#endif /* ! MHD_TLS_GNU_FUNCS_H */