xref: /reactos/dll/3rdparty/mbedtls/platform.c (revision 7e069ccd)
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 
34 #if defined(MBEDTLS_ENTROPY_NV_SEED) && \
35     !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
36 /* Implementation that should never be optimized out by the compiler */
37 static void mbedtls_zeroize( void *v, size_t n ) {
38     volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
39 }
40 #endif
41 
42 /* The compile time configuration of memory allocation via the macros
43  * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
44  * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
45  * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
46 #if defined(MBEDTLS_PLATFORM_MEMORY) &&                 \
47     !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&        \
48        defined(MBEDTLS_PLATFORM_FREE_MACRO) )
49 
50 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
51 static void *platform_calloc_uninit( size_t n, size_t size )
52 {
53     ((void) n);
54     ((void) size);
55     return( NULL );
56 }
57 
58 #define MBEDTLS_PLATFORM_STD_CALLOC   platform_calloc_uninit
59 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
60 
61 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
62 static void platform_free_uninit( void *ptr )
63 {
64     ((void) ptr);
65 }
66 
67 #define MBEDTLS_PLATFORM_STD_FREE     platform_free_uninit
68 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
69 
70 void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
71 void (*mbedtls_free)( void * )     = MBEDTLS_PLATFORM_STD_FREE;
72 
73 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
74                               void (*free_func)( void * ) )
75 {
76     mbedtls_calloc = calloc_func;
77     mbedtls_free = free_func;
78     return( 0 );
79 }
80 #endif /* MBEDTLS_PLATFORM_MEMORY &&
81           !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
82              defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
83 
84 #if defined(_WIN32)
85 #include <stdarg.h>
86 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
87 {
88     int ret;
89     va_list argp;
90 
91     /* Avoid calling the invalid parameter handler by checking ourselves */
92     if( s == NULL || n == 0 || fmt == NULL )
93         return( -1 );
94 
95     va_start( argp, fmt );
96 #if defined(_TRUNCATE) && !defined(__MINGW32__)
97     ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
98 #else
99     ret = _vsnprintf( s, n, fmt, argp );
100     if( ret < 0 || (size_t) ret == n )
101     {
102         s[n-1] = '\0';
103         ret = -1;
104     }
105 #endif
106     va_end( argp );
107 
108     return( ret );
109 }
110 #endif
111 
112 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
113 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
114 /*
115  * Make dummy function to prevent NULL pointer dereferences
116  */
117 static int platform_snprintf_uninit( char * s, size_t n,
118                                      const char * format, ... )
119 {
120     ((void) s);
121     ((void) n);
122     ((void) format);
123     return( 0 );
124 }
125 
126 #define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
127 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
128 
129 int (*mbedtls_snprintf)( char * s, size_t n,
130                           const char * format,
131                           ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
132 
133 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
134                                                  const char * format,
135                                                  ... ) )
136 {
137     mbedtls_snprintf = snprintf_func;
138     return( 0 );
139 }
140 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
141 
142 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
143 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
144 /*
145  * Make dummy function to prevent NULL pointer dereferences
146  */
147 static int platform_printf_uninit( const char *format, ... )
148 {
149     ((void) format);
150     return( 0 );
151 }
152 
153 #define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
154 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
155 
156 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
157 
158 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
159 {
160     mbedtls_printf = printf_func;
161     return( 0 );
162 }
163 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
164 
165 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
166 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
167 /*
168  * Make dummy function to prevent NULL pointer dereferences
169  */
170 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
171 {
172     ((void) stream);
173     ((void) format);
174     return( 0 );
175 }
176 
177 #define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
178 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
179 
180 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
181                                         MBEDTLS_PLATFORM_STD_FPRINTF;
182 
183 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
184 {
185     mbedtls_fprintf = fprintf_func;
186     return( 0 );
187 }
188 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
189 
190 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
191 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
192 /*
193  * Make dummy function to prevent NULL pointer dereferences
194  */
195 static void platform_exit_uninit( int status )
196 {
197     ((void) status);
198 }
199 
200 #define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
201 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
202 
203 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
204 
205 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
206 {
207     mbedtls_exit = exit_func;
208     return( 0 );
209 }
210 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
211 
212 #if defined(MBEDTLS_HAVE_TIME)
213 
214 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
215 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
216 /*
217  * Make dummy function to prevent NULL pointer dereferences
218  */
219 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
220 {
221     ((void) timer);
222     return( 0 );
223 }
224 
225 #define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
226 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
227 
228 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
229 
230 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
231 {
232     mbedtls_time = time_func;
233     return( 0 );
234 }
235 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
236 
237 #endif /* MBEDTLS_HAVE_TIME */
238 
239 #if defined(MBEDTLS_ENTROPY_NV_SEED)
240 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
241 /* Default implementations for the platform independent seed functions use
242  * standard libc file functions to read from and write to a pre-defined filename
243  */
244 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
245 {
246     FILE *file;
247     size_t n;
248 
249     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
250         return( -1 );
251 
252     if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
253     {
254         fclose( file );
255         mbedtls_zeroize( buf, buf_len );
256         return( -1 );
257     }
258 
259     fclose( file );
260     return( (int)n );
261 }
262 
263 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
264 {
265     FILE *file;
266     size_t n;
267 
268     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
269         return -1;
270 
271     if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
272     {
273         fclose( file );
274         return -1;
275     }
276 
277     fclose( file );
278     return( (int)n );
279 }
280 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
281 
282 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
283 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
284 /*
285  * Make dummy function to prevent NULL pointer dereferences
286  */
287 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
288 {
289     ((void) buf);
290     ((void) buf_len);
291     return( -1 );
292 }
293 
294 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
295 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
296 
297 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
298 /*
299  * Make dummy function to prevent NULL pointer dereferences
300  */
301 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
302 {
303     ((void) buf);
304     ((void) buf_len);
305     return( -1 );
306 }
307 
308 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
309 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
310 
311 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
312             MBEDTLS_PLATFORM_STD_NV_SEED_READ;
313 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
314             MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
315 
316 int mbedtls_platform_set_nv_seed(
317         int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
318         int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
319 {
320     mbedtls_nv_seed_read = nv_seed_read_func;
321     mbedtls_nv_seed_write = nv_seed_write_func;
322     return( 0 );
323 }
324 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
325 #endif /* MBEDTLS_ENTROPY_NV_SEED */
326 
327 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
328 /*
329  * Placeholder platform setup that does nothing by default
330  */
331 int mbedtls_platform_setup( mbedtls_platform_context *ctx )
332 {
333     (void)ctx;
334 
335     return( 0 );
336 }
337 
338 /*
339  * Placeholder platform teardown that does nothing by default
340  */
341 void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
342 {
343     (void)ctx;
344 }
345 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
346 
347 #endif /* MBEDTLS_PLATFORM_C */
348