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