This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 363e1a846a3a4a75a4d1968d6e18abca5765965b
Author: makejian <[email protected]>
AuthorDate: Tue Nov 24 17:34:11 2026 +0800

    crypto/openssl_mbedtls_wrapper: route allocations through mbedtls_calloc
    
    Replace malloc()/zalloc()/free() in the wrapper with
    mbedtls_calloc()/mbedtls_free() so that every allocation goes
    through the mbed TLS platform allocator.  This lets the wrapper run
    entirely on a static buffer pool when
    MBEDTLS_MEMORY_BUFFER_ALLOC_C is configured, and fixes the
    implicit-declaration warnings for free() in bio_lib.c and
    bss_mem.c.
    
    mbed TLS has no realloc() wrapper, so the BUF_MEM grow path
    allocates the new buffer, copies the old contents and frees the
    old buffer instead.
    
    Signed-off-by: makejian <[email protected]>
---
 crypto/openssl_mbedtls_wrapper/mbedtls/aes.c      |  8 ++++++--
 crypto/openssl_mbedtls_wrapper/mbedtls/bio_lib.c  |  7 ++++---
 crypto/openssl_mbedtls_wrapper/mbedtls/bss_mem.c  | 15 +++++++++++----
 crypto/openssl_mbedtls_wrapper/mbedtls/digest.c   |  6 ++++--
 crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c     |  6 ++++--
 crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c   |  4 ++--
 crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.c |  9 +--------
 crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.h |  5 +++--
 8 files changed, 35 insertions(+), 25 deletions(-)

diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/aes.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/aes.c
index 53ff0e259..57dd51427 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/aes.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/aes.c
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 #include <openssl/evp.h>
 #include <mbedtls/cipher.h>
+#include <mbedtls/platform.h>
 
 /****************************************************************************
  * Public Functions
@@ -199,7 +200,10 @@ int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx,
 
 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
 {
-  return (EVP_CIPHER_CTX *)(malloc(sizeof(mbedtls_cipher_context_t)));
+  mbedtls_cipher_context_t *ctx;
+
+  ctx = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_t));
+  return (EVP_CIPHER_CTX *)ctx;
 }
 
 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
@@ -207,5 +211,5 @@ void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
   if (ctx == NULL)
     return;
 
-  free(ctx);
+  mbedtls_free(ctx);
 }
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/bio_lib.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/bio_lib.c
index 36d08403a..865ce304d 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/bio_lib.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/bio_lib.c
@@ -25,6 +25,7 @@
 #include <stdio.h>
 
 #include <openssl/bio.h>
+#include <mbedtls/platform.h>
 
 /****************************************************************************
  * Public Functions
@@ -32,7 +33,7 @@
 
 BIO *BIO_new(const BIO_METHOD *method)
 {
-  BIO *bio = zalloc(sizeof(*bio));
+  BIO *bio = mbedtls_calloc(1, sizeof(*bio));
 
   if (bio == NULL)
     {
@@ -43,7 +44,7 @@ BIO *BIO_new(const BIO_METHOD *method)
 
   if (method->create != NULL && !method->create(bio))
     {
-      free(bio);
+      mbedtls_free(bio);
       return NULL;
     }
 
@@ -121,7 +122,7 @@ int BIO_free(BIO *a)
       a->method->destroy(a);
     }
 
-  free(a);
+  mbedtls_free(a);
   return 1;
 }
 
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/bss_mem.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/bss_mem.c
index 40f884135..5f1945154 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/bss_mem.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/bss_mem.c
@@ -28,6 +28,7 @@
 #include <sys/param.h>
 
 #include <openssl/bio.h>
+#include <mbedtls/platform.h>
 
 /****************************************************************************
  * Pre-processor definitions
@@ -128,12 +129,18 @@ static size_t BUF_MEM_grow_clean(BIO_BUF_MEM *bbm, size_t 
len)
     }
 
   n = MAX((len + 3) / 3 * 4, bbm->max);
-  ret = realloc(bbm->data, n);
+  ret = mbedtls_calloc(1, n);
   if (ret == NULL)
     {
       return 0;
     }
 
+  if (bbm->data != NULL)
+    {
+      memcpy(ret, bbm->data, bbm->length);
+      mbedtls_free(bbm->data);
+    }
+
   bbm->data = ret;
   bbm->max = n;
   bbm->length = len;
@@ -196,7 +203,7 @@ static int mem_read(BIO *b, char *out, int outl)
 
 static int mem_new(BIO *bi)
 {
-  BIO_BUF_MEM *bbm = zalloc(sizeof(*bbm));
+  BIO_BUF_MEM *bbm = mbedtls_calloc(1, sizeof(*bbm));
 
   if (bbm == NULL)
     {
@@ -219,10 +226,10 @@ static int mem_free(BIO *a)
   bbm = (BIO_BUF_MEM *)a->ptr;
   if (bbm->data)
     {
-      free(bbm->data);
+      mbedtls_free(bbm->data);
     }
 
-  free(bbm);
+  mbedtls_free(bbm);
   return 1;
 }
 
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/digest.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/digest.c
index 5dbf6d3f6..6b39c6fe0 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/digest.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/digest.c
@@ -25,6 +25,7 @@
  ****************************************************************************/
 
 #include <mbedtls/md.h>
+#include <mbedtls/platform.h>
 #include <openssl/digest.h>
 #include <stdlib.h>
 
@@ -35,7 +36,8 @@
 EVP_MD_CTX *EVP_MD_CTX_new(void)
 {
   mbedtls_md_context_t *ctx =
-      (mbedtls_md_context_t *)malloc(sizeof(mbedtls_md_context_t));
+      (mbedtls_md_context_t *)mbedtls_calloc(1,
+                                            sizeof(mbedtls_md_context_t));
   EVP_MD_CTX_init((EVP_MD_CTX *)ctx);
   return (EVP_MD_CTX *)ctx;
 }
@@ -45,7 +47,7 @@ void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
   if (ctx != NULL)
     {
       EVP_MD_CTX_cleanup(ctx);
-      free(ctx);
+      mbedtls_free(ctx);
     }
 }
 
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c
index 325018e97..00d7e13c0 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/hmac.c
@@ -28,6 +28,7 @@
 #include <openssl/hmac.h>
 #include <mbedtls/hmac_drbg.h>
 #include <mbedtls/md.h>
+#include <mbedtls/platform.h>
 #include <stdlib.h>
 
 /****************************************************************************
@@ -57,7 +58,8 @@ uint8_t *HMAC(const EVP_MD *evp_md, const void *key,
 HMAC_CTX *HMAC_CTX_new(void)
 {
   mbedtls_md_context_t *ctx =
-      (mbedtls_md_context_t *)malloc(sizeof(mbedtls_md_context_t));
+      (mbedtls_md_context_t *)mbedtls_calloc(1,
+                                            sizeof(mbedtls_md_context_t));
   if (ctx != NULL)
     {
       mbedtls_md_init(ctx);
@@ -126,7 +128,7 @@ void HMAC_CTX_cleanup(HMAC_CTX *ctx)
 void HMAC_CTX_free(HMAC_CTX *ctx)
 {
   mbedtls_md_free((mbedtls_md_context_t *)ctx);
-  free(ctx);
+  mbedtls_free(ctx);
 }
 
 size_t EVP_MD_size(const EVP_MD *md)
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
index fb709a39b..9d7f5b5cb 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
@@ -932,7 +932,7 @@ int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
 
   if (namelen)
     {
-      name_cstr = malloc(namelen + 1);
+      name_cstr = mbedtls_calloc(1, namelen + 1);
       if (!name_cstr)
         {
           return 0;
@@ -947,7 +947,7 @@ int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
 
   if (namelen)
     {
-      free(name_cstr);
+      mbedtls_free(name_cstr);
     }
 
   return 1;
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.c
index b21d4fb3a..c353ada43 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.c
@@ -29,12 +29,5 @@
 
 void *ssl_mem_zalloc(size_t size)
 {
-  void *p = malloc(size);
-
-  if (p)
-    {
-      memset(p, 0, size);
-    }
-
-  return p;
+  return mbedtls_calloc(1, size);
 }
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.h 
b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.h
index 90bec6a6a..da9a6ae4c 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.h
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_port.h
@@ -26,6 +26,7 @@
 
 #include "string.h"
 #include "stdlib.h"
+#include "mbedtls/platform.h"
 #if defined(LWS_HAVE_MALLOC_H)
 #include "malloc.h"
 #endif
@@ -37,8 +38,8 @@ extern "C"
 
 void *ssl_mem_zalloc(size_t size);
 
-#define ssl_mem_malloc malloc
-#define ssl_mem_free   free
+#define ssl_mem_malloc(size) mbedtls_calloc(1, (size))
+#define ssl_mem_free   mbedtls_free
 
 #define ssl_memcpy     memcpy
 #define ssl_strlen     strlen

Reply via email to