platform_util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * \file platform_util.h
  3. *
  4. * \brief Common and shared functions used by multiple modules in the Mbed TLS
  5. * library.
  6. */
  7. /*
  8. * Copyright The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12. * not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #ifndef MBEDTLS_PLATFORM_UTIL_H
  24. #define MBEDTLS_PLATFORM_UTIL_H
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "mbedtls/config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #include <stddef.h>
  31. #if defined(MBEDTLS_HAVE_TIME_DATE)
  32. #include "mbedtls/platform_time.h"
  33. #include <time.h>
  34. #endif /* MBEDTLS_HAVE_TIME_DATE */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #if defined(MBEDTLS_CHECK_PARAMS)
  39. #if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
  40. /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
  41. * (which is what our config.h suggests). */
  42. #include <assert.h>
  43. #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
  44. #if defined(MBEDTLS_PARAM_FAILED)
  45. /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
  46. *
  47. * This flag can be used to check whether it is safe to assume that
  48. * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
  49. */
  50. #define MBEDTLS_PARAM_FAILED_ALT
  51. #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
  52. #define MBEDTLS_PARAM_FAILED(cond) assert(cond)
  53. #define MBEDTLS_PARAM_FAILED_ALT
  54. #else /* MBEDTLS_PARAM_FAILED */
  55. #define MBEDTLS_PARAM_FAILED(cond) \
  56. mbedtls_param_failed( #cond, __FILE__, __LINE__)
  57. /**
  58. * \brief User supplied callback function for parameter validation failure.
  59. * See #MBEDTLS_CHECK_PARAMS for context.
  60. *
  61. * This function will be called unless an alternative treatment
  62. * is defined through the #MBEDTLS_PARAM_FAILED macro.
  63. *
  64. * This function can return, and the operation will be aborted, or
  65. * alternatively, through use of setjmp()/longjmp() can resume
  66. * execution in the application code.
  67. *
  68. * \param failure_condition The assertion that didn't hold.
  69. * \param file The file where the assertion failed.
  70. * \param line The line in the file where the assertion failed.
  71. */
  72. void mbedtls_param_failed(const char *failure_condition,
  73. const char *file,
  74. int line);
  75. #endif /* MBEDTLS_PARAM_FAILED */
  76. /* Internal macro meant to be called only from within the library. */
  77. #define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) \
  78. do { \
  79. if (!(cond)) \
  80. { \
  81. MBEDTLS_PARAM_FAILED(cond); \
  82. return ret; \
  83. } \
  84. } while (0)
  85. /* Internal macro meant to be called only from within the library. */
  86. #define MBEDTLS_INTERNAL_VALIDATE(cond) \
  87. do { \
  88. if (!(cond)) \
  89. { \
  90. MBEDTLS_PARAM_FAILED(cond); \
  91. return; \
  92. } \
  93. } while (0)
  94. #else /* MBEDTLS_CHECK_PARAMS */
  95. /* Internal macros meant to be called only from within the library. */
  96. #define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) do { } while (0)
  97. #define MBEDTLS_INTERNAL_VALIDATE(cond) do { } while (0)
  98. #endif /* MBEDTLS_CHECK_PARAMS */
  99. /* Internal helper macros for deprecating API constants. */
  100. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  101. #if defined(MBEDTLS_DEPRECATED_WARNING)
  102. /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
  103. * to avoid conflict with other headers which define and use
  104. * it, too. We might want to move all these definitions here at
  105. * some point for uniformity. */
  106. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  107. MBEDTLS_DEPRECATED typedef char const *mbedtls_deprecated_string_constant_t;
  108. #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) \
  109. ((mbedtls_deprecated_string_constant_t) (VAL))
  110. MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
  111. #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) \
  112. ((mbedtls_deprecated_numeric_constant_t) (VAL))
  113. #undef MBEDTLS_DEPRECATED
  114. #else /* MBEDTLS_DEPRECATED_WARNING */
  115. #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) VAL
  116. #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) VAL
  117. #endif /* MBEDTLS_DEPRECATED_WARNING */
  118. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  119. /* Implementation of the check-return facility.
  120. * See the user documentation in config.h.
  121. *
  122. * Do not use this macro directly to annotate function: instead,
  123. * use one of MBEDTLS_CHECK_RETURN_CRITICAL or MBEDTLS_CHECK_RETURN_TYPICAL
  124. * depending on how important it is to check the return value.
  125. */
  126. #if !defined(MBEDTLS_CHECK_RETURN)
  127. #if defined(__GNUC__)
  128. #define MBEDTLS_CHECK_RETURN __attribute__((__warn_unused_result__))
  129. #elif defined(_MSC_VER) && _MSC_VER >= 1700
  130. #include <sal.h>
  131. #define MBEDTLS_CHECK_RETURN _Check_return_
  132. #else
  133. #define MBEDTLS_CHECK_RETURN
  134. #endif
  135. #endif
  136. /** Critical-failure function
  137. *
  138. * This macro appearing at the beginning of the declaration of a function
  139. * indicates that its return value should be checked in all applications.
  140. * Omitting the check is very likely to indicate a bug in the application
  141. * and will result in a compile-time warning if #MBEDTLS_CHECK_RETURN
  142. * is implemented for the compiler in use.
  143. *
  144. * \note The use of this macro is a work in progress.
  145. * This macro may be added to more functions in the future.
  146. * Such an extension is not considered an API break, provided that
  147. * there are near-unavoidable circumstances under which the function
  148. * can fail. For example, signature/MAC/AEAD verification functions,
  149. * and functions that require a random generator, are considered
  150. * return-check-critical.
  151. */
  152. #define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN
  153. /** Ordinary-failure function
  154. *
  155. * This macro appearing at the beginning of the declaration of a function
  156. * indicates that its return value should be generally be checked in portable
  157. * applications. Omitting the check will result in a compile-time warning if
  158. * #MBEDTLS_CHECK_RETURN is implemented for the compiler in use and
  159. * #MBEDTLS_CHECK_RETURN_WARNING is enabled in the compile-time configuration.
  160. *
  161. * You can use #MBEDTLS_IGNORE_RETURN to explicitly ignore the return value
  162. * of a function that is annotated with #MBEDTLS_CHECK_RETURN.
  163. *
  164. * \note The use of this macro is a work in progress.
  165. * This macro will be added to more functions in the future.
  166. * Eventually this should appear before most functions returning
  167. * an error code (as \c int in the \c mbedtls_xxx API or
  168. * as ::psa_status_t in the \c psa_xxx API).
  169. */
  170. #if defined(MBEDTLS_CHECK_RETURN_WARNING)
  171. #define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN
  172. #else
  173. #define MBEDTLS_CHECK_RETURN_TYPICAL
  174. #endif
  175. /** Benign-failure function
  176. *
  177. * This macro appearing at the beginning of the declaration of a function
  178. * indicates that it is rarely useful to check its return value.
  179. *
  180. * This macro has an empty expansion. It exists for documentation purposes:
  181. * a #MBEDTLS_CHECK_RETURN_OPTIONAL annotation indicates that the function
  182. * has been analyzed for return-check usefulness, whereas the lack of
  183. * an annotation indicates that the function has not been analyzed and its
  184. * return-check usefulness is unknown.
  185. */
  186. #define MBEDTLS_CHECK_RETURN_OPTIONAL
  187. /** \def MBEDTLS_IGNORE_RETURN
  188. *
  189. * Call this macro with one argument, a function call, to suppress a warning
  190. * from #MBEDTLS_CHECK_RETURN due to that function call.
  191. */
  192. #if !defined(MBEDTLS_IGNORE_RETURN)
  193. /* GCC doesn't silence the warning with just (void)(result).
  194. * (void)!(result) is known to work up at least up to GCC 10, as well
  195. * as with Clang and MSVC.
  196. *
  197. * https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Non_002dbugs.html
  198. * https://stackoverflow.com/questions/40576003/ignoring-warning-wunused-result
  199. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
  200. */
  201. #define MBEDTLS_IGNORE_RETURN(result) ((void) !(result))
  202. #endif
  203. /**
  204. * \brief Securely zeroize a buffer
  205. *
  206. * The function is meant to wipe the data contained in a buffer so
  207. * that it can no longer be recovered even if the program memory
  208. * is later compromised. Call this function on sensitive data
  209. * stored on the stack before returning from a function, and on
  210. * sensitive data stored on the heap before freeing the heap
  211. * object.
  212. *
  213. * It is extremely difficult to guarantee that calls to
  214. * mbedtls_platform_zeroize() are not removed by aggressive
  215. * compiler optimizations in a portable way. For this reason, Mbed
  216. * TLS provides the configuration option
  217. * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
  218. * mbedtls_platform_zeroize() to use a suitable implementation for
  219. * their platform and needs
  220. *
  221. * \param buf Buffer to be zeroized
  222. * \param len Length of the buffer in bytes
  223. *
  224. */
  225. void mbedtls_platform_zeroize(void *buf, size_t len);
  226. #if defined(MBEDTLS_HAVE_TIME_DATE)
  227. /**
  228. * \brief Platform-specific implementation of gmtime_r()
  229. *
  230. * The function is a thread-safe abstraction that behaves
  231. * similarly to the gmtime_r() function from Unix/POSIX.
  232. *
  233. * Mbed TLS will try to identify the underlying platform and
  234. * make use of an appropriate underlying implementation (e.g.
  235. * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
  236. * not possible, then gmtime() will be used. In this case, calls
  237. * from the library to gmtime() will be guarded by the mutex
  238. * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
  239. * enabled. It is recommended that calls from outside the library
  240. * are also guarded by this mutex.
  241. *
  242. * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
  243. * unconditionally use the alternative implementation for
  244. * mbedtls_platform_gmtime_r() supplied by the user at compile time.
  245. *
  246. * \param tt Pointer to an object containing time (in seconds) since the
  247. * epoch to be converted
  248. * \param tm_buf Pointer to an object where the results will be stored
  249. *
  250. * \return Pointer to an object of type struct tm on success, otherwise
  251. * NULL
  252. */
  253. struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
  254. struct tm *tm_buf);
  255. #endif /* MBEDTLS_HAVE_TIME_DATE */
  256. #ifdef __cplusplus
  257. }
  258. #endif
  259. #endif /* MBEDTLS_PLATFORM_UTIL_H */