platform.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Platform abstraction layer
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #include "mbedtls/platform_util.h"
  54. /* The compile time configuration of memory allocation via the macros
  55. * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
  56. * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
  57. * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
  58. #if defined(MBEDTLS_PLATFORM_MEMORY) && \
  59. !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
  60. defined(MBEDTLS_PLATFORM_FREE_MACRO) )
  61. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  62. static void *platform_calloc_uninit( size_t n, size_t size )
  63. {
  64. ((void) n);
  65. ((void) size);
  66. return( NULL );
  67. }
  68. #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
  69. #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
  70. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  71. static void platform_free_uninit( void *ptr )
  72. {
  73. ((void) ptr);
  74. }
  75. #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
  76. #endif /* !MBEDTLS_PLATFORM_STD_FREE */
  77. static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
  78. static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
  79. void * mbedtls_calloc( size_t nmemb, size_t size )
  80. {
  81. return (*mbedtls_calloc_func)( nmemb, size );
  82. }
  83. void mbedtls_free( void * ptr )
  84. {
  85. (*mbedtls_free_func)( ptr );
  86. }
  87. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  88. void (*free_func)( void * ) )
  89. {
  90. mbedtls_calloc_func = calloc_func;
  91. mbedtls_free_func = free_func;
  92. return( 0 );
  93. }
  94. #endif /* MBEDTLS_PLATFORM_MEMORY &&
  95. !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
  96. defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
  97. #if defined(_WIN32)
  98. #include <stdarg.h>
  99. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
  100. {
  101. int ret;
  102. va_list argp;
  103. /* Avoid calling the invalid parameter handler by checking ourselves */
  104. if( s == NULL || n == 0 || fmt == NULL )
  105. return( -1 );
  106. va_start( argp, fmt );
  107. #if defined(_TRUNCATE) && !defined(__MINGW32__)
  108. ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
  109. #else
  110. ret = _vsnprintf( s, n, fmt, argp );
  111. if( ret < 0 || (size_t) ret == n )
  112. {
  113. s[n-1] = '\0';
  114. ret = -1;
  115. }
  116. #endif
  117. va_end( argp );
  118. return( ret );
  119. }
  120. #endif
  121. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  122. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  123. /*
  124. * Make dummy function to prevent NULL pointer dereferences
  125. */
  126. static int platform_snprintf_uninit( char * s, size_t n,
  127. const char * format, ... )
  128. {
  129. ((void) s);
  130. ((void) n);
  131. ((void) format);
  132. return( 0 );
  133. }
  134. #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
  135. #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
  136. int (*mbedtls_snprintf)( char * s, size_t n,
  137. const char * format,
  138. ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
  139. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  140. const char * format,
  141. ... ) )
  142. {
  143. mbedtls_snprintf = snprintf_func;
  144. return( 0 );
  145. }
  146. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  147. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  148. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  149. /*
  150. * Make dummy function to prevent NULL pointer dereferences
  151. */
  152. static int platform_printf_uninit( const char *format, ... )
  153. {
  154. ((void) format);
  155. return( 0 );
  156. }
  157. #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
  158. #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
  159. int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
  160. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
  161. {
  162. mbedtls_printf = printf_func;
  163. return( 0 );
  164. }
  165. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  166. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  167. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  168. /*
  169. * Make dummy function to prevent NULL pointer dereferences
  170. */
  171. static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
  172. {
  173. ((void) stream);
  174. ((void) format);
  175. return( 0 );
  176. }
  177. #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
  178. #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
  179. int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
  180. MBEDTLS_PLATFORM_STD_FPRINTF;
  181. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
  182. {
  183. mbedtls_fprintf = fprintf_func;
  184. return( 0 );
  185. }
  186. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  187. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  188. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  189. /*
  190. * Make dummy function to prevent NULL pointer dereferences
  191. */
  192. static void platform_exit_uninit( int status )
  193. {
  194. ((void) status);
  195. }
  196. #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
  197. #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
  198. void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
  199. int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
  200. {
  201. mbedtls_exit = exit_func;
  202. return( 0 );
  203. }
  204. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  205. #if defined(MBEDTLS_HAVE_TIME)
  206. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  207. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  208. /*
  209. * Make dummy function to prevent NULL pointer dereferences
  210. */
  211. static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
  212. {
  213. ((void) timer);
  214. return( 0 );
  215. }
  216. #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
  217. #endif /* !MBEDTLS_PLATFORM_STD_TIME */
  218. mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
  219. int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
  220. {
  221. mbedtls_time = time_func;
  222. return( 0 );
  223. }
  224. #endif /* MBEDTLS_PLATFORM_TIME_ALT */
  225. #endif /* MBEDTLS_HAVE_TIME */
  226. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  227. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  228. /* Default implementations for the platform independent seed functions use
  229. * standard libc file functions to read from and write to a pre-defined filename
  230. */
  231. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
  232. {
  233. FILE *file;
  234. size_t n;
  235. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
  236. return( -1 );
  237. if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
  238. {
  239. fclose( file );
  240. mbedtls_platform_zeroize( buf, buf_len );
  241. return( -1 );
  242. }
  243. fclose( file );
  244. return( (int)n );
  245. }
  246. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
  247. {
  248. FILE *file;
  249. size_t n;
  250. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
  251. return -1;
  252. if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
  253. {
  254. fclose( file );
  255. return -1;
  256. }
  257. fclose( file );
  258. return( (int)n );
  259. }
  260. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  261. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  262. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  263. /*
  264. * Make dummy function to prevent NULL pointer dereferences
  265. */
  266. static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
  267. {
  268. ((void) buf);
  269. ((void) buf_len);
  270. return( -1 );
  271. }
  272. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
  273. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
  274. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  275. /*
  276. * Make dummy function to prevent NULL pointer dereferences
  277. */
  278. static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
  279. {
  280. ((void) buf);
  281. ((void) buf_len);
  282. return( -1 );
  283. }
  284. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
  285. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
  286. int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
  287. MBEDTLS_PLATFORM_STD_NV_SEED_READ;
  288. int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
  289. MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
  290. int mbedtls_platform_set_nv_seed(
  291. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  292. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
  293. {
  294. mbedtls_nv_seed_read = nv_seed_read_func;
  295. mbedtls_nv_seed_write = nv_seed_write_func;
  296. return( 0 );
  297. }
  298. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  299. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  300. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  301. /*
  302. * Placeholder platform setup that does nothing by default
  303. */
  304. int mbedtls_platform_setup( mbedtls_platform_context *ctx )
  305. {
  306. (void)ctx;
  307. return( 0 );
  308. }
  309. /*
  310. * Placeholder platform teardown that does nothing by default
  311. */
  312. void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
  313. {
  314. (void)ctx;
  315. }
  316. #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  317. #endif /* MBEDTLS_PLATFORM_C */