tls_mbed_tls_lib.h (7823B)
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 #ifndef __cplusplus 55 # define mhd_MBETLS_C_HDR_START /* Empty */ 56 # define mhd_MBETLS_C_HDR_FINISH /* Empty */ 57 #else /* __cplusplus */ 58 /* *INDENT-OFF* */ 59 # define mhd_MBETLS_C_HDR_START extern "C" { 60 # define mhd_MBETLS_C_HDR_FINISH } 61 /* *INDENT-ON* */ 62 #endif /* __cplusplus */ 63 64 #include "microhttpd2_portability.h" 65 66 #if defined(MHD_WARN_IGNORE_STYLE_GCC) 67 MHD_WARN_PUSH_ 68 # ifdef HAVE_WZERO_AS_NULL_POINTER_CONSTANT 69 MHD_WARN_IGNORE_ ("-Wzero-as-null-pointer-constant") 70 # endif /* HAVE_WZERO_AS_NULL_POINTER_CONSTANT */ 71 # ifdef HAVE_WREDUNDANT_DECLS 72 MHD_WARN_IGNORE_ ("-Wredundant-decls") 73 # endif /* HAVE_WREDUNDANT_DECLS */ 74 # ifdef HAVE_WSWITCH_ENUM 75 MHD_WARN_IGNORE_ ("-Wswitch-enum") 76 # endif /* HAVE_WSWITCH_ENUM */ 77 # ifdef HAVE_WDOCUMENTATION_DEPRECATED_SYNC 78 MHD_WARN_IGNORE_ ("-Wdocumentation-deprecated-sync") 79 # endif /* HAVE_WDOCUMENTATION_DEPRECATED_SYNC */ 80 # ifdef HAVE_WDOCUMENTATION_UNKNOWN_COMMAND 81 MHD_WARN_IGNORE_ ("-Wdocumentation-unknown-command") 82 # endif /* HAVE_WDOCUMENTATION_UNKNOWN_COMMAND */ 83 # ifdef HAVE_WDOCUMENTATION_PEDANTIC 84 MHD_WARN_IGNORE_ ("-Wdocumentation-pedantic") 85 # endif /* HAVE_WDOCUMENTATION_PEDANTIC */ 86 # define mhd_RESTORE_MBEDTLS_MUTED_WARNS MHD_WARN_POP_ 87 #else /* ! MHD_WARN_IGNORE_STYLE_GCC */ 88 # define mhd_RESTORE_MBEDTLS_MUTED_WARNS /* empty */ 89 #endif /* ! MHD_WARN_IGNORE_STYLE_GCC */ 90 91 #include <mbedtls/build_info.h> 92 #include <mbedtls/platform.h> 93 #ifdef MBEDTLS_VERSION_C 94 # include <mbedtls/version.h> 95 #endif /* MBEDTLS_VERSION_C */ 96 97 #if ((MBEDTLS_VERSION_MAJOR + 0) < 3) 98 #error MbedTLS version 3.0 or later is required 99 #endif 100 #if ((MBEDTLS_VERSION_NUMBER + 0) < 0x03000000) 101 #error MbedTLS version 3.0 or later is required 102 #endif 103 104 /* #mhd_TLS_MBED_USE_PSA_FREE is MHD build-time user-definable macro */ 105 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) 106 # define mhd_TLS_MBED_USE_PSA 1 107 # ifdef MHD_TLS_MBED_USE_PSA_FREE 108 /* The application must not use MbedTLS directly */ 109 # define mhd_TLS_MBED_USE_PSA_FREE 1 110 # endif 111 #endif 112 113 #ifdef mhd_TLS_MBED_USE_PSA 114 # include <psa/crypto.h> 115 #endif /* mhd_TLS_MBED_USE_PSA */ 116 117 #ifdef MBEDTLS_MD_C 118 /* Actually MD must be available if TLS is enabled */ 119 # include <mbedtls/md.h> 120 #endif 121 122 #if ((MBEDTLS_VERSION_NUMBER + 0) >= 0x03050000) 123 # define mhd_TLS_MBED_HAS_SHA3_IDS 1 124 #endif 125 126 #ifdef MBEDTLS_ENTROPY_C 127 # include <mbedtls/entropy.h> 128 #endif /* MBEDTLS_ENTROPY_C */ 129 130 #ifdef mhd_TLS_MBED_USE_PSA 131 mhd_MBETLS_C_HDR_START 132 # include <mbedtls/psa_util.h> 133 mhd_MBETLS_C_HDR_FINISH 134 # define mhd_TLS_MBED_HAS_RNG_PSA 1 135 #elif defined(MHD_TLS_MBED_PREF_RNG_PSA) 136 # undef MHD_TLS_MBED_PREF_RNG_PSA 137 #endif 138 139 #ifdef MBEDTLS_HMAC_DRBG_C 140 # include <mbedtls/hmac_drbg.h> 141 # define mhd_TLS_MBED_HAS_RNG_HMAC 1 142 #elif defined(MHD_TLS_MBED_PREF_RNG_HMAC) 143 # undef MHD_TLS_MBED_PREF_RNG_HMAC 144 #endif /* MBEDTLS_HMAC_DRBG_C */ 145 146 #ifdef MBEDTLS_CTR_DRBG_C 147 # include <mbedtls/ctr_drbg.h> 148 # define mhd_TLS_MBED_HAS_RNG_CTR 1 149 #elif defined(MHD_TLS_MBED_PREF_RNG_CTR) 150 # undef MHD_TLS_MBED_PREF_RNG_CTR 151 #endif /* MBEDTLS_CTR_DRBG_C */ 152 153 #if ! defined(MHD_TLS_MBED_PREF_RNG_PSA) && \ 154 ! defined(MHD_TLS_MBED_PREF_RNG_HMAC) && \ 155 ! defined(MHD_TLS_MBED_PREF_RNG_CTR) 156 # if defined(mhd_TLS_MBED_HAS_RNG_PSA) 157 # define MHD_TLS_MBED_PREF_RNG_PSA 1 158 # elif defined(mhd_TLS_MBED_HAS_RNG_HMAC) && \ 159 defined(MBEDTLS_MD_C) 160 # define MHD_TLS_MBED_PREF_RNG_HMAC 1 161 # define mhd_TLS_MBED_RNG_PREF_NEEDS_ENTROPY 1 162 # elif defined(mhd_TLS_MBED_HAS_RNG_CTR) 163 # define MHD_TLS_MBED_PREF_RNG_CTR 1 164 # define mhd_TLS_MBED_RNG_PREF_NEEDS_ENTROPY 1 165 # endif 166 #endif 167 168 #if defined(mhd_TLS_MBED_RNG_PREF_NEEDS_ENTROPY) && \ 169 defined(MBEDTLS_ENTROPY_C) 170 # define mhd_TLS_MBED_USE_LIB_ENTROPY 1 171 #endif 172 173 #if ((MBEDTLS_VERSION_NUMBER + 0) < 0x04000000) 174 /** 175 * TLS initialisation requires random generator 176 */ 177 # define mhd_TLS_MBED_INIT_TLS_REQ_RNG 1 178 #endif 179 180 #include <mbedtls/x509_crt.h> 181 182 #if ! defined(MBEDTLS_X509_CRT_PARSE_C) 183 #error X.509 certificate parsing functions are required 184 #endif /* ! MBEDTLS_X509_CRT_PARSE_C */ 185 186 #include <mbedtls/pk.h> 187 188 #if ! defined(MBEDTLS_PK_PARSE_C) 189 #error Public key parser is required 190 #endif /* ! MBEDTLS_PK_PARSE_C */ 191 192 #if ! defined(MBEDTLS_PEM_PARSE_C) 193 #error PEM parser is required 194 #endif /* ! MBEDTLS_PEM_PARSE_C */ 195 196 /* Required header, checked in 'configure' */ 197 #include <mbedtls/ssl.h> 198 199 /* #MHD_TLS_MBED_SKIP_PLATFORM_SETUP and #MHD_TLS_MBED_USE_PLATFORM_TEARDOWN 200 are MHD build-time user-definable macros */ 201 /* User may set #MHD_TLS_MBED_SKIP_PLATFORM_SETUP and/or 202 #MHD_TLS_MBED_USE_PLATFORM_TEARDOWN when building MHD to control 203 automatic platform setup / teardown */ 204 #if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) && \ 205 ! defined(MHD_TLS_MBED_SKIP_PLATFORM_SETUP) 206 # define mhd_TLS_MBED_HAS_PLATFORM_SETUP 1 207 # ifdef MHD_TLS_MBED_USE_PLATFORM_TEARDOWN 208 /* The application must not use MbedTLS directly */ 209 # define mhd_TLS_MBED_USE_PLATFORM_TEARDOWN 1 210 # endif 211 #endif 212 213 #ifdef MBEDTLS_NET_C 214 /* Actually, the header should be available unconditionally, but could be 215 accidently excluded if module is disabled. */ 216 # include <mbedtls/net_sockets.h> 217 #endif 218 219 #ifndef MBEDTLS_ERR_NET_RECV_FAILED 220 /* Unknown error when receiving the data */ 221 # define MBEDTLS_ERR_NET_RECV_FAILED (-0x004C) 222 #endif 223 #ifndef MBEDTLS_ERR_NET_SEND_FAILED 224 /* Unknown error when sending the data */ 225 # define MBEDTLS_ERR_NET_SEND_FAILED (-0x004E) 226 #endif 227 #ifndef MBEDTLS_ERR_NET_CONN_RESET 228 /* The network connection is broken */ 229 # define MBEDTLS_ERR_NET_CONN_RESET (-0x0050) 230 #endif 231 232 #ifdef MBEDTLS_DEBUG_C 233 # include <mbedtls/debug.h> 234 #endif 235 236 mhd_RESTORE_MBEDTLS_MUTED_WARNS 237 238 #endif /* ! MHD_TLS_MBED_TLS_LIB_H */