debug.h 8.6 KB

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