debug.h 8.6 KB

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