platform.c 10 KB

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