debug.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * \file debug.h
  3. *
  4. * \brief Functions for controlling and providing debug output from the library.
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_DEBUG_H
  11. #define MBEDTLS_DEBUG_H
  12. #if !defined(MBEDTLS_CONFIG_FILE)
  13. #include "mbedtls/config.h"
  14. #else
  15. #include MBEDTLS_CONFIG_FILE
  16. #endif
  17. #include "mbedtls/ssl.h"
  18. #if defined(MBEDTLS_ECP_C)
  19. #include "mbedtls/ecp.h"
  20. #endif
  21. #if defined(MBEDTLS_DEBUG_C)
  22. #define MBEDTLS_DEBUG_STRIP_PARENS(...) __VA_ARGS__
  23. #define MBEDTLS_SSL_DEBUG_MSG(level, args) \
  24. mbedtls_debug_print_msg(ssl, level, __FILE__, __LINE__, \
  25. MBEDTLS_DEBUG_STRIP_PARENS args)
  26. #define MBEDTLS_SSL_DEBUG_RET(level, text, ret) \
  27. mbedtls_debug_print_ret(ssl, level, __FILE__, __LINE__, text, ret)
  28. #define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) \
  29. mbedtls_debug_print_buf(ssl, level, __FILE__, __LINE__, text, buf, len)
  30. #if defined(MBEDTLS_BIGNUM_C)
  31. #define MBEDTLS_SSL_DEBUG_MPI(level, text, X) \
  32. mbedtls_debug_print_mpi(ssl, level, __FILE__, __LINE__, text, X)
  33. #endif
  34. #if defined(MBEDTLS_ECP_C)
  35. #define MBEDTLS_SSL_DEBUG_ECP(level, text, X) \
  36. mbedtls_debug_print_ecp(ssl, level, __FILE__, __LINE__, text, X)
  37. #endif
  38. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  39. #define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
  40. mbedtls_debug_print_crt(ssl, level, __FILE__, __LINE__, text, crt)
  41. #endif
  42. #if defined(MBEDTLS_ECDH_C)
  43. #define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr) \
  44. mbedtls_debug_printf_ecdh(ssl, level, __FILE__, __LINE__, ecdh, attr)
  45. #endif
  46. #else /* MBEDTLS_DEBUG_C */
  47. #define MBEDTLS_SSL_DEBUG_MSG(level, args) do { } while (0)
  48. #define MBEDTLS_SSL_DEBUG_RET(level, text, ret) do { } while (0)
  49. #define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) do { } while (0)
  50. #define MBEDTLS_SSL_DEBUG_MPI(level, text, X) do { } while (0)
  51. #define MBEDTLS_SSL_DEBUG_ECP(level, text, X) do { } while (0)
  52. #define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) do { } while (0)
  53. #define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr) do { } while (0)
  54. #endif /* MBEDTLS_DEBUG_C */
  55. /**
  56. * \def MBEDTLS_PRINTF_ATTRIBUTE
  57. *
  58. * Mark a function as having printf attributes, and thus enable checking
  59. * via -wFormat and other flags. This does nothing on builds with compilers
  60. * that do not support the format attribute
  61. *
  62. * Module: library/debug.c
  63. * Caller:
  64. *
  65. * This module provides debugging functions.
  66. */
  67. #if defined(__has_attribute)
  68. #if __has_attribute(format)
  69. #if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
  70. #define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
  71. __attribute__((__format__(gnu_printf, string_index, first_to_check)))
  72. #else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
  73. #define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
  74. __attribute__((format(printf, string_index, first_to_check)))
  75. #endif
  76. #else /* __has_attribute(format) */
  77. #define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
  78. #endif /* __has_attribute(format) */
  79. #else /* defined(__has_attribute) */
  80. #define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
  81. #endif
  82. /**
  83. * \def MBEDTLS_PRINTF_SIZET
  84. *
  85. * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
  86. * and MinGW we need to define the printf specifier for size_t
  87. * and long long per platform.
  88. *
  89. * Module: library/debug.c
  90. * Caller:
  91. *
  92. * This module provides debugging functions.
  93. */
  94. #if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900)
  95. #include <inttypes.h>
  96. #define MBEDTLS_PRINTF_SIZET PRIuPTR
  97. #define MBEDTLS_PRINTF_LONGLONG "I64d"
  98. #else \
  99. /* defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900) */
  100. #define MBEDTLS_PRINTF_SIZET "zu"
  101. #define MBEDTLS_PRINTF_LONGLONG "lld"
  102. #endif \
  103. /* defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900) */
  104. #ifdef __cplusplus
  105. extern "C" {
  106. #endif
  107. /**
  108. * \brief Set the threshold error level to handle globally all debug output.
  109. * Debug messages that have a level over the threshold value are
  110. * discarded.
  111. * (Default value: 0 = No debug )
  112. *
  113. * \param threshold threshold level of messages to filter on. Messages at a
  114. * higher level will be discarded.
  115. * - Debug levels
  116. * - 0 No debug
  117. * - 1 Error
  118. * - 2 State change
  119. * - 3 Informational
  120. * - 4 Verbose
  121. */
  122. void mbedtls_debug_set_threshold(int threshold);
  123. /**
  124. * \brief Print a message to the debug output. This function is always used
  125. * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
  126. * context, file and line number parameters.
  127. *
  128. * \param ssl SSL context
  129. * \param level error level of the debug message
  130. * \param file file the message has occurred in
  131. * \param line line number the message has occurred at
  132. * \param format format specifier, in printf format
  133. * \param ... variables used by the format specifier
  134. *
  135. * \attention This function is intended for INTERNAL usage within the
  136. * library only.
  137. */
  138. void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
  139. const char *file, int line,
  140. const char *format, ...) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
  141. /**
  142. * \brief Print the return value of a function to the debug output. This
  143. * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
  144. * which supplies the ssl context, file and line number parameters.
  145. *
  146. * \param ssl SSL context
  147. * \param level error level of the debug message
  148. * \param file file the error has occurred in
  149. * \param line line number the error has occurred in
  150. * \param text the name of the function that returned the error
  151. * \param ret the return code value
  152. *
  153. * \attention This function is intended for INTERNAL usage within the
  154. * library only.
  155. */
  156. void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
  157. const char *file, int line,
  158. const char *text, int ret);
  159. /**
  160. * \brief Output a buffer of size len bytes to the debug output. This function
  161. * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
  162. * which supplies the ssl context, file and line number parameters.
  163. *
  164. * \param ssl SSL context
  165. * \param level error level of the debug message
  166. * \param file file the error has occurred in
  167. * \param line line number the error has occurred in
  168. * \param text a name or label for the buffer being dumped. Normally the
  169. * variable or buffer name
  170. * \param buf the buffer to be outputted
  171. * \param len length of the buffer
  172. *
  173. * \attention This function is intended for INTERNAL usage within the
  174. * library only.
  175. */
  176. void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
  177. const char *file, int line, const char *text,
  178. const unsigned char *buf, size_t len);
  179. #if defined(MBEDTLS_BIGNUM_C)
  180. /**
  181. * \brief Print a MPI variable to the debug output. This function is always
  182. * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
  183. * ssl context, file and line number parameters.
  184. *
  185. * \param ssl SSL context
  186. * \param level error level of the debug message
  187. * \param file file the error has occurred in
  188. * \param line line number the error has occurred in
  189. * \param text a name or label for the MPI being output. Normally the
  190. * variable name
  191. * \param X the MPI variable
  192. *
  193. * \attention This function is intended for INTERNAL usage within the
  194. * library only.
  195. */
  196. void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
  197. const char *file, int line,
  198. const char *text, const mbedtls_mpi *X);
  199. #endif
  200. #if defined(MBEDTLS_ECP_C)
  201. /**
  202. * \brief Print an ECP point to the debug output. This function is always
  203. * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
  204. * ssl context, file and line number parameters.
  205. *
  206. * \param ssl SSL context
  207. * \param level error level of the debug message
  208. * \param file file the error has occurred in
  209. * \param line line number the error has occurred in
  210. * \param text a name or label for the ECP point being output. Normally the
  211. * variable name
  212. * \param X the ECP point
  213. *
  214. * \attention This function is intended for INTERNAL usage within the
  215. * library only.
  216. */
  217. void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
  218. const char *file, int line,
  219. const char *text, const mbedtls_ecp_point *X);
  220. #endif
  221. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  222. /**
  223. * \brief Print a X.509 certificate structure to the debug output. This
  224. * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
  225. * which supplies the ssl context, file and line number parameters.
  226. *
  227. * \param ssl SSL context
  228. * \param level error level of the debug message
  229. * \param file file the error has occurred in
  230. * \param line line number the error has occurred in
  231. * \param text a name or label for the certificate being output
  232. * \param crt X.509 certificate structure
  233. *
  234. * \attention This function is intended for INTERNAL usage within the
  235. * library only.
  236. */
  237. void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
  238. const char *file, int line,
  239. const char *text, const mbedtls_x509_crt *crt);
  240. #endif
  241. #if defined(MBEDTLS_ECDH_C)
  242. typedef enum {
  243. MBEDTLS_DEBUG_ECDH_Q,
  244. MBEDTLS_DEBUG_ECDH_QP,
  245. MBEDTLS_DEBUG_ECDH_Z,
  246. } mbedtls_debug_ecdh_attr;
  247. /**
  248. * \brief Print a field of the ECDH structure in the SSL context to the debug
  249. * output. This function is always used through the
  250. * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
  251. * and line number parameters.
  252. *
  253. * \param ssl SSL context
  254. * \param level error level of the debug message
  255. * \param file file the error has occurred in
  256. * \param line line number the error has occurred in
  257. * \param ecdh the ECDH context
  258. * \param attr the identifier of the attribute being output
  259. *
  260. * \attention This function is intended for INTERNAL usage within the
  261. * library only.
  262. */
  263. void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
  264. const char *file, int line,
  265. const mbedtls_ecdh_context *ecdh,
  266. mbedtls_debug_ecdh_attr attr);
  267. #endif
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* debug.h */