tls_mbed_tls_lib.h (7469B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2025 Evgeny Grin (Karlson2k) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file src/mhd2/tls_mbed_tls_lib.h 41 * @brief The wrapper for MbedTLS headers 42 * @author Karlson2k (Evgeny Grin) 43 */ 44 45 #ifndef MHD_TLS_MBED_TLS_LIB_H 46 #define MHD_TLS_MBED_TLS_LIB_H 1 47 48 #include "mhd_sys_options.h" 49 50 #ifndef MHD_SUPPORT_MBEDTLS 51 #error This header can be used only if MbedTLS is enabled 52 #endif 53 54 #include "microhttpd2_portability.h" 55 56 #if defined(MHD_WARN_IGNORE_STYLE_GCC) 57 MHD_WARN_PUSH_ 58 # ifdef HAVE_WZERO_AS_NULL_POINTER_CONSTANT 59 MHD_WARN_IGNORE_ ("-Wzero-as-null-pointer-constant") 60 # endif /* HAVE_WZERO_AS_NULL_POINTER_CONSTANT */ 61 # ifdef HAVE_WREDUNDANT_DECLS 62 MHD_WARN_IGNORE_ ("-Wredundant-decls") 63 # endif /* HAVE_WREDUNDANT_DECLS */ 64 # ifdef HAVE_WSWITCH_ENUM 65 MHD_WARN_IGNORE_ ("-Wswitch-enum") 66 # endif /* HAVE_WSWITCH_ENUM */ 67 # ifdef HAVE_WDOCUMENTATION_DEPRECATED_SYNC 68 MHD_WARN_IGNORE_ ("-Wdocumentation-deprecated-sync") 69 # endif /* HAVE_WDOCUMENTATION_DEPRECATED_SYNC */ 70 # ifdef HAVE_WDOCUMENTATION_UNKNOWN_COMMAND 71 MHD_WARN_IGNORE_ ("-Wdocumentation-unknown-command") 72 # endif /* HAVE_WDOCUMENTATION_UNKNOWN_COMMAND */ 73 # ifdef HAVE_WDOCUMENTATION_PEDANTIC 74 MHD_WARN_IGNORE_ ("-Wdocumentation-pedantic") 75 # endif /* HAVE_WDOCUMENTATION_PEDANTIC */ 76 # define mhd_RESTORE_MBEDTLS_MUTED_WARNS MHD_WARN_POP_ 77 #else /* ! MHD_WARN_IGNORE_STYLE_GCC */ 78 # define mhd_RESTORE_MBEDTLS_MUTED_WARNS /* empty */ 79 #endif /* ! MHD_WARN_IGNORE_STYLE_GCC */ 80 81 #include <mbedtls/build_info.h> 82 #include <mbedtls/platform.h> 83 #ifdef MBEDTLS_VERSION_C 84 # include <mbedtls/version.h> 85 #endif /* MBEDTLS_VERSION_C */ 86 87 #if ((MBEDTLS_VERSION_MAJOR + 0) < 3) 88 #error MbedTLS version 3.0 or later is required 89 #endif 90 #if ((MBEDTLS_VERSION_NUMBER + 0) < 0x03000000) 91 #error MbedTLS version 3.0 or later is required 92 #endif 93 94 /* #mhd_TLS_MBED_USE_PSA_FREE is MHD build-time user-definable macro */ 95 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) 96 # define mhd_TLS_MBED_USE_PSA 1 97 # ifdef MHD_TLS_MBED_USE_PSA_FREE 98 /* The application must not use MbedTLS directly */ 99 # define mhd_TLS_MBED_USE_PSA_FREE 1 100 # endif 101 #endif 102 103 #ifdef mhd_TLS_MBED_USE_PSA 104 # include <psa/crypto.h> 105 #endif /* mhd_TLS_MBED_USE_PSA */ 106 107 #ifdef MBEDTLS_MD_C 108 /* Actually MD must be available if TLS is enabled */ 109 # include <mbedtls/md.h> 110 #endif 111 112 #if ((MBEDTLS_VERSION_NUMBER + 0) >= 0x03050000) 113 # define mhd_TLS_MBED_HAS_SHA3_IDS 1 114 #endif 115 116 #ifdef MBEDTLS_ENTROPY_C 117 # include <mbedtls/entropy.h> 118 #endif /* MBEDTLS_ENTROPY_C */ 119 120 #ifdef mhd_TLS_MBED_USE_PSA 121 # include <mbedtls/psa_util.h> 122 # define mhd_TLS_MBED_HAS_RNG_PSA 1 123 #elif defined(MHD_TLS_MBED_PREF_RNG_PSA) 124 # undef MHD_TLS_MBED_PREF_RNG_PSA 125 #endif 126 127 #ifdef MBEDTLS_HMAC_DRBG_C 128 # include <mbedtls/hmac_drbg.h> 129 # define mhd_TLS_MBED_HAS_RNG_HMAC 1 130 #elif defined(MHD_TLS_MBED_PREF_RNG_HMAC) 131 # undef MHD_TLS_MBED_PREF_RNG_HMAC 132 #endif /* MBEDTLS_HMAC_DRBG_C */ 133 134 #ifdef MBEDTLS_CTR_DRBG_C 135 # include <mbedtls/ctr_drbg.h> 136 # define mhd_TLS_MBED_HAS_RNG_CTR 1 137 #elif defined(MHD_TLS_MBED_PREF_RNG_CTR) 138 # undef MHD_TLS_MBED_PREF_RNG_CTR 139 #endif /* MBEDTLS_CTR_DRBG_C */ 140 141 #if ! defined(MHD_TLS_MBED_PREF_RNG_PSA) && \ 142 ! defined(MHD_TLS_MBED_PREF_RNG_HMAC) && \ 143 ! defined(MHD_TLS_MBED_PREF_RNG_CTR) 144 # if defined(mhd_TLS_MBED_HAS_RNG_PSA) 145 # define MHD_TLS_MBED_PREF_RNG_PSA 1 146 # elif defined(mhd_TLS_MBED_HAS_RNG_HMAC) && \ 147 defined(MBEDTLS_MD_C) 148 # define MHD_TLS_MBED_PREF_RNG_HMAC 1 149 # define mhd_TLS_MBED_RNG_PREF_NEEDS_ENTROPY 1 150 # elif defined(mhd_TLS_MBED_HAS_RNG_CTR) 151 # define MHD_TLS_MBED_PREF_RNG_CTR 1 152 # define mhd_TLS_MBED_RNG_PREF_NEEDS_ENTROPY 1 153 # endif 154 #endif 155 156 #if defined(mhd_TLS_MBED_RNG_PREF_NEEDS_ENTROPY) && \ 157 defined(MBEDTLS_ENTROPY_C) 158 # define mhd_TLS_MBED_USE_LIB_ENTROPY 1 159 #endif 160 161 #if ((MBEDTLS_VERSION_NUMBER + 0) < 0x04000000) 162 /** 163 * TLS initialisation requires random generator 164 */ 165 # define mhd_TLS_MBED_INIT_TLS_REQ_RNG 1 166 #endif 167 168 #include <mbedtls/x509_crt.h> 169 170 #if ! defined(MBEDTLS_X509_CRT_PARSE_C) 171 #error X.509 certificate parsing functions are required 172 #endif /* ! MBEDTLS_X509_CRT_PARSE_C */ 173 174 #include <mbedtls/pk.h> 175 176 #if ! defined(MBEDTLS_PK_PARSE_C) 177 #error Public key parser is required 178 #endif /* ! MBEDTLS_PK_PARSE_C */ 179 180 #if ! defined(MBEDTLS_PEM_PARSE_C) 181 #error PEM parser is required 182 #endif /* ! MBEDTLS_PEM_PARSE_C */ 183 184 /* Required header, checked in 'configure' */ 185 #include <mbedtls/ssl.h> 186 187 /* #MHD_TLS_MBED_SKIP_PLATFORM_SETUP and #MHD_TLS_MBED_USE_PLATFORM_TEARDOWN 188 are MHD build-time user-definable macros */ 189 /* User may set #MHD_TLS_MBED_SKIP_PLATFORM_SETUP and/or 190 #MHD_TLS_MBED_USE_PLATFORM_TEARDOWN when building MHD to control 191 automatic platform setup / teardown */ 192 #if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) && \ 193 ! defined(MHD_TLS_MBED_SKIP_PLATFORM_SETUP) 194 # define mhd_TLS_MBED_HAS_PLATFORM_SETUP 1 195 # ifdef MHD_TLS_MBED_USE_PLATFORM_TEARDOWN 196 /* The application must not use MbedTLS directly */ 197 # define mhd_TLS_MBED_USE_PLATFORM_TEARDOWN 1 198 # endif 199 #endif 200 201 #ifdef MBEDTLS_NET_C 202 /* Actually, the header should be available unconditionally, but could be 203 accidently excluded if module is disabled. */ 204 # include <mbedtls/net_sockets.h> 205 #endif 206 207 #ifndef MBEDTLS_ERR_NET_RECV_FAILED 208 /* Unknown error when receiving the data */ 209 # define MBEDTLS_ERR_NET_RECV_FAILED (-0x004C) 210 #endif 211 #ifndef MBEDTLS_ERR_NET_SEND_FAILED 212 /* Unknown error when sending the data */ 213 # define MBEDTLS_ERR_NET_SEND_FAILED (-0x004E) 214 #endif 215 #ifndef MBEDTLS_ERR_NET_CONN_RESET 216 /* The network connection is broken */ 217 # define MBEDTLS_ERR_NET_CONN_RESET (-0x0050) 218 #endif 219 220 #ifdef MBEDTLS_DEBUG_C 221 # include <mbedtls/debug.h> 222 #endif 223 224 mhd_RESTORE_MBEDTLS_MUTED_WARNS 225 226 #endif /* ! MHD_TLS_MBED_TLS_LIB_H */