libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

commit ddbab9dd6a873c14d62bf3cad5f7476763d414a3
parent 92f63e8bf9987e2089b7a2ecc7a403d8f37d63c4
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sun, 23 Nov 2025 17:24:58 +0100

add md5 for mbedtls/openssl, simplify logic a bit

Diffstat:
Msrc/mhd2/md5_ext.h | 11++++++-----
Asrc/mhd2/md5_ext_mbedtls.c | 139+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/mhd2/md5_ext_openssl.c | 150+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/mhd2/sha256_ext_mbedtls.c | 21+++++++++------------
Msrc/mhd2/sha256_ext_openssl.c | 29++++++++++++++---------------
5 files changed, 318 insertions(+), 32 deletions(-)

diff --git a/src/mhd2/md5_ext.h b/src/mhd2/md5_ext.h @@ -53,8 +53,9 @@ */ #define mhd_MD5_DIGEST_SIZE (16) -/* Actual declaration is in GnuTLS lib header */ -struct hash_hd_st; +#ifndef MHD_MD5_Context +#define MHD_MD5_Context void +#endif /** * Indicates that struct mhd_Md5CtxExt has 'ext_error' @@ -66,7 +67,7 @@ struct hash_hd_st; */ struct mhd_Md5CtxExt { - struct hash_hd_st *handle; /**< Hash calculation handle */ + MHD_MD5_Context *handle; /**< Hash calculation handle */ int ext_error; /**< Non-zero if external error occurs during init or hashing */ }; @@ -90,7 +91,7 @@ mhd_MD5_init_one_time (struct mhd_Md5CtxExt *ctx); * MD5 process portion of bytes. * * @param ctx the calculation context - * @param size number of bytes in @a data + * @param size number of bytes in @a data, must not be 0 * @param data bytes to add to hash */ void @@ -122,7 +123,7 @@ mhd_MD5_finish_reset (struct mhd_Md5CtxExt *restrict ctx, /** * Free allocated resources. * - * @param ctx the calculation context + * @param[in] ctx the calculation context */ void mhd_MD5_deinit (struct mhd_Md5CtxExt *ctx); diff --git a/src/mhd2/md5_ext_mbedtls.c b/src/mhd2/md5_ext_mbedtls.c @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ +/* + This file is part of GNU libmicrohttpd. + Copyright (C) 2025 Christian Grothoff + + 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. + + Alternatively, you can redistribute GNU libmicrohttpd and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version, together + with the eCos exception, as follows: + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile this + file and link it with other works to produce a work based on this + file, this file does not by itself cause the resulting work to be + covered by the GNU General Public License. However the source code + for this file must still be made available in accordance with + section (3) of the GNU General Public License v2. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + + You should have received copies of the GNU Lesser General Public + License and the GNU General Public License along with this library; + if not, see <https://www.gnu.org/licenses/>. +*/ + +/** + * @file microhttpd/md5_ext_mbedtls.c + * @brief Wrapper for MD5 calculation performed by mbedTLS library + * @author Christian Grothoff + */ + +#include <mbedtls/md5.h> +#define MHD_MD5_Context mbedtls_md5_context +#include "md5_ext.h" +#include "mhd_assert.h" + + +/** + * Initialise structure for MD5 calculation, allocate resources. + * + * This function must not be called more than one time for @a ctx. + * + * @param ctx the calculation context + */ +void +mhd_MD5_init_one_time (struct mhd_Md5CtxExt *ctx) +{ + ctx->ext_error = 0; + ctx->handle = (mbedtls_md5_context *) malloc ( + sizeof (mbedtls_md5_context)); + if (NULL == ctx->handle) + { + ctx->ext_error = 1; /* Allocation failure */ + return; + } + mbedtls_md5_init (ctx->handle); + ctx->ext_error = mbedtls_md5_starts_ret (ctx->handle); + if (0 != ctx->ext_error) + { + mhd_MD5_deinit (ctx->handle); + } + + /* If handle is NULL, the error must be set */ + mhd_assert ((NULL != ctx->handle) || (0 != ctx->ext_error)); + /* If error is set, the handle must be NULL */ + mhd_assert ((0 == ctx->ext_error) || (NULL == ctx->handle)); +} + + +/** + * Process portion of bytes. + * + * @param ctx the calculation context + * @param data bytes to add to hash + * @param length number of bytes in @a data + */ +void +mhd_MD5_update (struct mhd_Md5CtxExt *ctx, + size_t size, + const uint8_t *data) +{ + mhd_assert (0 != size); + + if (0 == ctx->ext_error) + ctx->ext_error = mbedtls_md5_update_ret (ctx->handle, + data, + size); +} + + +/** + * Finalise MD5 calculation, return digest, reset hash calculation. + * + * @param ctx the calculation context + * @param[out] digest set to the hash, must be #mhd_MD5_DIGEST_SIZE bytes + */ +void +mhd_MD5_finish_reset (struct mhd_Md5CtxExt *ctx, + uint8_t digest[mhd_MD5_DIGEST_SIZE]) +{ + if (0 != ctx->ext_error) + return; + ctx->ext_error = mbedtls_md5_finish_ret (ctx->handle, + digest); + if (0 != ctx->ext_error) + return; + /* Reset for potential reuse */ + ctx->ext_error = mbedtls_md5_starts_ret (ctx->handle); +} + + +/** + * Free allocated resources. + * + * @param ctx the calculation context + */ +void +mhd_MD5_deinit (struct mhd_Md5CtxExt *ctx) +{ + if (NULL != ctx->handle) + { + mbedtls_md5_free (ctx->handle); + free (ctx->handle); + ctx->handle = NULL; + } +} diff --git a/src/mhd2/md5_ext_openssl.c b/src/mhd2/md5_ext_openssl.c @@ -0,0 +1,150 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ +/* + This file is part of GNU libmicrohttpd. + Copyright (C) 2025 Christian Grothoff + + 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. + + Alternatively, you can redistribute GNU libmicrohttpd and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version, together + with the eCos exception, as follows: + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile this + file and link it with other works to produce a work based on this + file, this file does not by itself cause the resulting work to be + covered by the GNU General Public License. However the source code + for this file must still be made available in accordance with + section (3) of the GNU General Public License v2. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + + You should have received copies of the GNU Lesser General Public + License and the GNU General Public License along with this library; + if not, see <https://www.gnu.org/licenses/>. +*/ + +/** + * @file microhttpd/md5_ext_openssl.c + * @brief Wrapper for MD5 calculation performed by OpenSSL library + * @author Christian grothoff + */ + +#include <openssl/evp.h> +#define MHD_MD5_Context struct hash_hd_st +#include "md5_ext.h" +#include "mhd_assert.h" + + +/** + * Initialise structure for MD5 calculation, allocate resources. + * + * This function must not be called more than one time for @a ctx. + * + * @param ctx the calculation context + */ +void +mhd_MD5_init_one_time (struct mhd_Md5CtxExt *ctx) +{ + ctx->ext_error = 0; + ctx->handle = EVP_MD_CTX_new (); + if (NULL == ctx->handle) + { + ctx->ext_error = 1; /* Allocation failure */ + return; + } + if (1 != EVP_DigestInit_ex (ctx->handle, + EVP_md5 (), + NULL)) + { + ctx->ext_error = 1; /* Initialization failure */ + mhd_MD5_deinit (ctx); + } + + /* If handle is NULL, the error must be set */ + mhd_assert ((NULL != ctx->handle) || (0 != ctx->ext_error)); + /* If error is set, the handle must be NULL */ + mhd_assert ((0 == ctx->ext_error) || (NULL == ctx->handle)); +} + + +/** + * Process portion of bytes. + * + * @param ctx the calculation context + * @param size number of bytes in @a data, must not be 0 + * @param data bytes to add to hash + */ +void +mhd_MD5_update (struct mhd_Md5CtxExt *ctx, + size_t size, + const uint8_t *data) +{ + mhd_assert (0 != size); + + if (0 == ctx->ext_error) + { + if (1 != EVP_DigestUpdate (ctx->handle, + data, + size)) + ctx->ext_error = 1; + } +} + + +/** + * Finalise MD5 calculation, return digest, reset hash calculation. + * + * @param ctx the calculation context + * @param[out] digest set to the hash, must be #mhd_MD5_DIGEST_SIZE bytes + */ +void +mhd_MD5_finish_reset (struct mhd_Md5CtxExt *ctx, + uint8_t digest[mhd_MD5_DIGEST_SIZE]) +{ + unsigned int len; + + if (0 != ctx->ext_error) + return; + if (1 != EVP_DigestFinal_ex (ctx->handle, + digest, + &len)) + { + ctx->ext_error = 1; + return; + } + mhd_assert (mhd_MD5_DIGEST_SIZE == len); + /* Reset for potential reuse */ + if (1 != EVP_DigestInit_ex (ctx->handle, + EVP_md5 (), + NULL)) + ctx->ext_error = 1; +} + + +/** + * Free allocated resources. + * + * @param[in] ctx the calculation context + */ +void +mhd_MD5_deinit (struct mhd_Md5CtxExt *ctx) +{ + if (NULL != ctx->handle) + { + EVP_MD_CTX_free (ctx->handle); + ctx->handle = NULL; + } +} diff --git a/src/mhd2/sha256_ext_mbedtls.c b/src/mhd2/sha256_ext_mbedtls.c @@ -113,18 +113,15 @@ void mhd_SHA256_finish_reset (struct mhd_Sha256CtxExt *ctx, uint8_t digest[mhd_SHA256_DIGEST_SIZE]) { - if (0 == ctx->ext_error) - { - ctx->ext_error = mbedtls_sha256_finish_ret (ctx->handle, - digest); - - if (0 == ctx->ext_error) - { - /* Reset for potential reuse */ - ctx->ext_error = mbedtls_sha256_starts_ret (ctx->handle, - 0 /* ! is224 */); - } - } + if (0 != ctx->ext_error) + return; + ctx->ext_error = mbedtls_sha256_finish_ret (ctx->handle, + digest); + if (0 != ctx->ext_error) + return; + /* Reset for potential reuse */ + ctx->ext_error = mbedtls_sha256_starts_ret (ctx->handle, + 0 /* ! is224 */); } diff --git a/src/mhd2/sha256_ext_openssl.c b/src/mhd2/sha256_ext_openssl.c @@ -116,23 +116,22 @@ mhd_SHA256_finish_reset (struct mhd_Sha256CtxExt *ctx, { unsigned int len; - if (0 == ctx->ext_error) + if (0 != ctx->ext_error) + return; + if (1 != EVP_DigestFinal_ex (ctx->handle, + digest, + &len)) + { + ctx->ext_error = 1; + } + else { - if (1 != EVP_DigestFinal_ex (ctx->handle, - digest, - &len)) - { + mhd_assert (mhd_SHA256_DIGEST_SIZE == len); + /* Reset for potential reuse */ + if (1 != EVP_DigestInit_ex (ctx->handle, + EVP_sha256 (), + NULL)) ctx->ext_error = 1; - } - else - { - mhd_assert (mhd_SHA256_DIGEST_SIZE == len); - /* Reset for potential reuse */ - if (1 != EVP_DigestInit_ex (ctx->handle, - EVP_sha256 (), - NULL)) - ctx->ext_error = 1; - } } }