Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers altcp_tls_mbedtls_mem.c Source File

altcp_tls_mbedtls_mem.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Application layered TCP connection API (to be used from TCPIP thread)
00004  *
00005  * This file contains memory management functions for a TLS layer using mbedTLS.
00006  *
00007  * ATTENTION: For production usage, you might want to override this file with
00008  *            your own implementation since this implementation simply uses the
00009  *            lwIP heap without caring for fragmentation or leaving heap for
00010  *            other parts of lwIP!
00011  */
00012 
00013 /*
00014  * Copyright (c) 2017 Simon Goldschmidt
00015  * All rights reserved.
00016  *
00017  * Redistribution and use in source and binary forms, with or without modification,
00018  * are permitted provided that the following conditions are met:
00019  *
00020  * 1. Redistributions of source code must retain the above copyright notice,
00021  *    this list of conditions and the following disclaimer.
00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
00023  *    this list of conditions and the following disclaimer in the documentation
00024  *    and/or other materials provided with the distribution.
00025  * 3. The name of the author may not be used to endorse or promote products
00026  *    derived from this software without specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00029  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00030  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00031  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00033  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00036  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00037  * OF SUCH DAMAGE.
00038  *
00039  * This file is part of the lwIP TCP/IP stack.
00040  *
00041  * Author: Simon Goldschmidt <goldsimon@gmx.de>
00042  *
00043  * Missing things / @todo:
00044  * - RX data is acknowledged after receiving (tcp_recved is called when enqueueing
00045  *   the pbuf for mbedTLS receive, not when processed by mbedTLS or the inner
00046  *   connection; altcp_recved() from inner connection does nothing)
00047  * - TX data is marked as 'sent' (i.e. acknowledged; sent callback is called) right
00048  *   after enqueueing for transmission, not when actually ACKed be the remote host.
00049  */
00050 
00051 #include "lwip/opt.h"
00052 
00053 #if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */
00054 
00055 #include "lwip/apps/altcp_tls_mbedtls_opts.h"
00056 
00057 #if LWIP_ALTCP_TLS && LWIP_ALTCP_TLS_MBEDTLS
00058 
00059 #include "altcp_tls_mbedtls_mem.h"
00060 #include "altcp_tls_mbedtls_structs.h"
00061 #include "lwip/mem.h"
00062 
00063 #include "mbedtls/platform.h"
00064 
00065 #include <string.h>
00066 
00067 #ifndef ALTCP_MBEDTLS_MEM_DEBUG
00068 #define ALTCP_MBEDTLS_MEM_DEBUG   LWIP_DBG_OFF
00069 #endif
00070 
00071 #if defined(MBEDTLS_PLATFORM_MEMORY) && \
00072    (!defined(MBEDTLS_PLATFORM_FREE_MACRO) || \
00073     defined(MBEDTLS_PLATFORM_CALLOC_MACRO))
00074 #define ALTCP_MBEDTLS_PLATFORM_ALLOC 1
00075 #else
00076 #define ALTCP_MBEDTLS_PLATFORM_ALLOC 0
00077 #endif
00078 
00079 #if ALTCP_MBEDTLS_PLATFORM_ALLOC
00080 
00081 #ifndef ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
00082 #define ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS 0
00083 #endif
00084 
00085 /* This is an example/debug implementation of alloc/free functions only */
00086 typedef struct altcp_mbedtls_malloc_helper_s {
00087   size_t c;
00088   size_t len;
00089 } altcp_mbedtls_malloc_helper_t;
00090 
00091 #if ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
00092 typedef struct altcp_mbedtls_malloc_stats_s {
00093   size_t allocedBytes;
00094   size_t allocCnt;
00095   size_t maxBytes;
00096   size_t totalBytes;
00097 } altcp_mbedtls_malloc_stats_t;
00098 altcp_mbedtls_malloc_stats_t altcp_mbedtls_malloc_stats;
00099 volatile int altcp_mbedtls_malloc_clear_stats;
00100 #endif
00101 
00102 static void *
00103 tls_malloc(size_t c, size_t len)
00104 {
00105   altcp_mbedtls_malloc_helper_t *hlpr;
00106   void *ret;
00107   size_t alloc_size;
00108 #if ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
00109   if (altcp_mbedtls_malloc_clear_stats) {
00110     altcp_mbedtls_malloc_clear_stats = 0;
00111     memset(&altcp_mbedtls_malloc_stats, 0, sizeof(altcp_mbedtls_malloc_stats));
00112   }
00113 #endif
00114   alloc_size = sizeof(altcp_mbedtls_malloc_helper_t) + (c * len);
00115   /* check for maximum allocation size, mainly to prevent mem_size_t overflow */
00116   if (alloc_size > MEM_SIZE) {
00117     LWIP_DEBUGF(ALTCP_MBEDTLS_MEM_DEBUG, ("mbedtls allocation too big: %c * %d bytes vs MEM_SIZE=%d",
00118                                           (int)c, (int)len, (int)MEM_SIZE));
00119     return NULL;
00120   }
00121   hlpr = (altcp_mbedtls_malloc_helper_t *)mem_malloc((mem_size_t)alloc_size);
00122   if (hlpr == NULL) {
00123     LWIP_DEBUGF(ALTCP_MBEDTLS_MEM_DEBUG, ("mbedtls alloc callback failed for %c * %d bytes", (int)c, (int)len));
00124     return NULL;
00125   }
00126 #if ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
00127   altcp_mbedtls_malloc_stats.allocCnt++;
00128   altcp_mbedtls_malloc_stats.allocedBytes += c * len;
00129   if (altcp_mbedtls_malloc_stats.allocedBytes > altcp_mbedtls_malloc_stats.maxBytes) {
00130     altcp_mbedtls_malloc_stats.maxBytes = altcp_mbedtls_malloc_stats.allocedBytes;
00131   }
00132   altcp_mbedtls_malloc_stats.totalBytes += c * len;
00133 #endif
00134   hlpr->c = c;
00135   hlpr->len = len;
00136   ret = hlpr + 1;
00137   /* zeroing the allocated chunk is required by mbedTLS! */
00138   memset(ret, 0, c * len);
00139   return ret;
00140 }
00141 
00142 static void
00143 tls_free(void *ptr)
00144 {
00145   altcp_mbedtls_malloc_helper_t *hlpr;
00146   if (ptr == NULL) {
00147     /* this obviously happened in mbedtls... */
00148     return;
00149   }
00150   hlpr = ((altcp_mbedtls_malloc_helper_t *)ptr) - 1;
00151 #if ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
00152   if (!altcp_mbedtls_malloc_clear_stats) {
00153     altcp_mbedtls_malloc_stats.allocedBytes -= hlpr->c * hlpr->len;
00154   }
00155 #endif
00156   mem_free(hlpr);
00157 }
00158 #endif /* ALTCP_MBEDTLS_PLATFORM_ALLOC*/
00159 
00160 void
00161 altcp_mbedtls_mem_init(void)
00162 {
00163   /* not much to do here when using the heap */
00164 
00165 #if ALTCP_MBEDTLS_PLATFORM_ALLOC
00166   /* set mbedtls allocation methods */
00167   mbedtls_platform_set_calloc_free(&tls_malloc, &tls_free);
00168 #endif
00169 }
00170 
00171 altcp_mbedtls_state_t *
00172 altcp_mbedtls_alloc(void *conf)
00173 {
00174   altcp_mbedtls_state_t *ret = (altcp_mbedtls_state_t *)mem_calloc(1, sizeof(altcp_mbedtls_state_t));
00175   if (ret != NULL) {
00176     ret->conf = conf;
00177   }
00178   return ret;
00179 }
00180 
00181 void
00182 altcp_mbedtls_free(void *conf, altcp_mbedtls_state_t *state)
00183 {
00184   LWIP_UNUSED_ARG(conf);
00185   LWIP_ASSERT("state != NULL", state != NULL);
00186   mem_free(state);
00187 }
00188 
00189 void *
00190 altcp_mbedtls_alloc_config(size_t size)
00191 {
00192   void *ret;
00193   size_t checked_size = (mem_size_t)size;
00194   if (size != checked_size) {
00195     /* allocation too big (mem_size_t overflow) */
00196     return NULL;
00197   }
00198   ret = (altcp_mbedtls_state_t *)mem_calloc(1, (mem_size_t)size);
00199   return ret;
00200 }
00201 
00202 void
00203 altcp_mbedtls_free_config(void *item)
00204 {
00205   LWIP_ASSERT("item != NULL", item != NULL);
00206   mem_free(item);
00207 }
00208 
00209 #endif /* LWIP_ALTCP_TLS && LWIP_ALTCP_TLS_MBEDTLS */
00210 #endif /* LWIP_ALTCP */