debug.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * \file debug.h
  3. *
  4. * \brief Functions for controlling and providing debug output from the library.
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_DEBUG_H
  26. #define MBEDTLS_DEBUG_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include "ssl.h"
  33. #if defined(MBEDTLS_ECP_C)
  34. #include "ecp.h"
  35. #endif
  36. #if defined(MBEDTLS_DEBUG_C)
  37. #define MBEDTLS_DEBUG_STRIP_PARENS( ... ) __VA_ARGS__
  38. #define MBEDTLS_SSL_DEBUG_MSG( level, args ) \
  39. mbedtls_debug_print_msg( ssl, level, __FILE__, __LINE__, \
  40. MBEDTLS_DEBUG_STRIP_PARENS args )
  41. #define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) \
  42. mbedtls_debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret )
  43. #define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) \
  44. mbedtls_debug_print_buf( ssl, level, __FILE__, __LINE__, text, buf, len )
  45. #if defined(MBEDTLS_BIGNUM_C)
  46. #define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) \
  47. mbedtls_debug_print_mpi( ssl, level, __FILE__, __LINE__, text, X )
  48. #endif
  49. #if defined(MBEDTLS_ECP_C)
  50. #define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) \
  51. mbedtls_debug_print_ecp( ssl, level, __FILE__, __LINE__, text, X )
  52. #endif
  53. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  54. #define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) \
  55. mbedtls_debug_print_crt( ssl, level, __FILE__, __LINE__, text, crt )
  56. #endif
  57. #else /* MBEDTLS_DEBUG_C */
  58. #define MBEDTLS_SSL_DEBUG_MSG( level, args ) do { } while( 0 )
  59. #define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) do { } while( 0 )
  60. #define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) do { } while( 0 )
  61. #define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) do { } while( 0 )
  62. #define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) do { } while( 0 )
  63. #define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) do { } while( 0 )
  64. #endif /* MBEDTLS_DEBUG_C */
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. /**
  69. * \brief Set the threshold error level to handle globally all debug output.
  70. * Debug messages that have a level over the threshold value are
  71. * discarded.
  72. * (Default value: 0 = No debug )
  73. *
  74. * \param threshold theshold level of messages to filter on. Messages at a
  75. * higher level will be discarded.
  76. * - Debug levels
  77. * - 0 No debug
  78. * - 1 Error
  79. * - 2 State change
  80. * - 3 Informational
  81. * - 4 Verbose
  82. */
  83. void mbedtls_debug_set_threshold( int threshold );
  84. /**
  85. * \brief Print a message to the debug output. This function is always used
  86. * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
  87. * context, file and line number parameters.
  88. *
  89. * \param ssl SSL context
  90. * \param level error level of the debug message
  91. * \param file file the message has occurred in
  92. * \param line line number the message has occurred at
  93. * \param format format specifier, in printf format
  94. * \param ... variables used by the format specifier
  95. *
  96. * \attention This function is intended for INTERNAL usage within the
  97. * library only.
  98. */
  99. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  100. const char *file, int line,
  101. const char *format, ... );
  102. /**
  103. * \brief Print the return value of a function to the debug output. This
  104. * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
  105. * which supplies the ssl context, file and line number parameters.
  106. *
  107. * \param ssl SSL context
  108. * \param level error level of the debug message
  109. * \param file file the error has occurred in
  110. * \param line line number the error has occurred in
  111. * \param text the name of the function that returned the error
  112. * \param ret the return code value
  113. *
  114. * \attention This function is intended for INTERNAL usage within the
  115. * library only.
  116. */
  117. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  118. const char *file, int line,
  119. const char *text, int ret );
  120. /**
  121. * \brief Output a buffer of size len bytes to the debug output. This function
  122. * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
  123. * which supplies the ssl context, file and line number parameters.
  124. *
  125. * \param ssl SSL context
  126. * \param level error level of the debug message
  127. * \param file file the error has occurred in
  128. * \param line line number the error has occurred in
  129. * \param text a name or label for the buffer being dumped. Normally the
  130. * variable or buffer name
  131. * \param buf the buffer to be outputted
  132. * \param len length of the buffer
  133. *
  134. * \attention This function is intended for INTERNAL usage within the
  135. * library only.
  136. */
  137. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  138. const char *file, int line, const char *text,
  139. const unsigned char *buf, size_t len );
  140. #if defined(MBEDTLS_BIGNUM_C)
  141. /**
  142. * \brief Print a MPI variable to the debug output. This function is always
  143. * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
  144. * 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 a name or label for the MPI being output. Normally the
  151. * variable name
  152. * \param X the MPI variable
  153. *
  154. * \attention This function is intended for INTERNAL usage within the
  155. * library only.
  156. */
  157. void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  158. const char *file, int line,
  159. const char *text, const mbedtls_mpi *X );
  160. #endif
  161. #if defined(MBEDTLS_ECP_C)
  162. /**
  163. * \brief Print an ECP point to the debug output. This function is always
  164. * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
  165. * ssl context, file and line number parameters.
  166. *
  167. * \param ssl SSL context
  168. * \param level error level of the debug message
  169. * \param file file the error has occurred in
  170. * \param line line number the error has occurred in
  171. * \param text a name or label for the ECP point being output. Normally the
  172. * variable name
  173. * \param X the ECP point
  174. *
  175. * \attention This function is intended for INTERNAL usage within the
  176. * library only.
  177. */
  178. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  179. const char *file, int line,
  180. const char *text, const mbedtls_ecp_point *X );
  181. #endif
  182. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  183. /**
  184. * \brief Print a X.509 certificate structure to the debug output. This
  185. * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
  186. * which supplies the ssl context, file and line number parameters.
  187. *
  188. * \param ssl SSL context
  189. * \param level error level of the debug message
  190. * \param file file the error has occurred in
  191. * \param line line number the error has occurred in
  192. * \param text a name or label for the certificate being output
  193. * \param crt X.509 certificate structure
  194. *
  195. * \attention This function is intended for INTERNAL usage within the
  196. * library only.
  197. */
  198. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  199. const char *file, int line,
  200. const char *text, const mbedtls_x509_crt *crt );
  201. #endif
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif /* debug.h */