Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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