debug.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #if defined(MBEDTLS_ECDH_C)
  57. #define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr ) \
  58. mbedtls_debug_printf_ecdh( ssl, level, __FILE__, __LINE__, ecdh, attr )
  59. #endif
  60. #else /* MBEDTLS_DEBUG_C */
  61. #define MBEDTLS_SSL_DEBUG_MSG( level, args ) do { } while( 0 )
  62. #define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) do { } while( 0 )
  63. #define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) do { } while( 0 )
  64. #define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) do { } while( 0 )
  65. #define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) do { } while( 0 )
  66. #define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) do { } while( 0 )
  67. #define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr ) do { } while( 0 )
  68. #endif /* MBEDTLS_DEBUG_C */
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. /**
  73. * \brief Set the threshold error level to handle globally all debug output.
  74. * Debug messages that have a level over the threshold value are
  75. * discarded.
  76. * (Default value: 0 = No debug )
  77. *
  78. * \param threshold theshold level of messages to filter on. Messages at a
  79. * higher level will be discarded.
  80. * - Debug levels
  81. * - 0 No debug
  82. * - 1 Error
  83. * - 2 State change
  84. * - 3 Informational
  85. * - 4 Verbose
  86. */
  87. void mbedtls_debug_set_threshold( int threshold );
  88. /**
  89. * \brief Print a message to the debug output. This function is always used
  90. * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
  91. * context, file and line number parameters.
  92. *
  93. * \param ssl SSL context
  94. * \param level error level of the debug message
  95. * \param file file the message has occurred in
  96. * \param line line number the message has occurred at
  97. * \param format format specifier, in printf format
  98. * \param ... variables used by the format specifier
  99. *
  100. * \attention This function is intended for INTERNAL usage within the
  101. * library only.
  102. */
  103. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  104. const char *file, int line,
  105. const char *format, ... );
  106. /**
  107. * \brief Print the return value of a function to the debug output. This
  108. * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
  109. * which supplies the ssl context, file and line number parameters.
  110. *
  111. * \param ssl SSL context
  112. * \param level error level of the debug message
  113. * \param file file the error has occurred in
  114. * \param line line number the error has occurred in
  115. * \param text the name of the function that returned the error
  116. * \param ret the return code value
  117. *
  118. * \attention This function is intended for INTERNAL usage within the
  119. * library only.
  120. */
  121. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  122. const char *file, int line,
  123. const char *text, int ret );
  124. /**
  125. * \brief Output a buffer of size len bytes to the debug output. This function
  126. * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
  127. * which supplies the ssl context, file and line number parameters.
  128. *
  129. * \param ssl SSL context
  130. * \param level error level of the debug message
  131. * \param file file the error has occurred in
  132. * \param line line number the error has occurred in
  133. * \param text a name or label for the buffer being dumped. Normally the
  134. * variable or buffer name
  135. * \param buf the buffer to be outputted
  136. * \param len length of the buffer
  137. *
  138. * \attention This function is intended for INTERNAL usage within the
  139. * library only.
  140. */
  141. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  142. const char *file, int line, const char *text,
  143. const unsigned char *buf, size_t len );
  144. #if defined(MBEDTLS_BIGNUM_C)
  145. /**
  146. * \brief Print a MPI variable to the debug output. This function is always
  147. * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
  148. * ssl context, file and line number parameters.
  149. *
  150. * \param ssl SSL context
  151. * \param level error level of the debug message
  152. * \param file file the error has occurred in
  153. * \param line line number the error has occurred in
  154. * \param text a name or label for the MPI being output. Normally the
  155. * variable name
  156. * \param X the MPI variable
  157. *
  158. * \attention This function is intended for INTERNAL usage within the
  159. * library only.
  160. */
  161. void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  162. const char *file, int line,
  163. const char *text, const mbedtls_mpi *X );
  164. #endif
  165. #if defined(MBEDTLS_ECP_C)
  166. /**
  167. * \brief Print an ECP point to the debug output. This function is always
  168. * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
  169. * ssl context, file and line number parameters.
  170. *
  171. * \param ssl SSL context
  172. * \param level error level of the debug message
  173. * \param file file the error has occurred in
  174. * \param line line number the error has occurred in
  175. * \param text a name or label for the ECP point being output. Normally the
  176. * variable name
  177. * \param X the ECP point
  178. *
  179. * \attention This function is intended for INTERNAL usage within the
  180. * library only.
  181. */
  182. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  183. const char *file, int line,
  184. const char *text, const mbedtls_ecp_point *X );
  185. #endif
  186. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  187. /**
  188. * \brief Print a X.509 certificate structure to the debug output. This
  189. * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
  190. * which supplies the ssl context, file and line number parameters.
  191. *
  192. * \param ssl SSL context
  193. * \param level error level of the debug message
  194. * \param file file the error has occurred in
  195. * \param line line number the error has occurred in
  196. * \param text a name or label for the certificate being output
  197. * \param crt X.509 certificate structure
  198. *
  199. * \attention This function is intended for INTERNAL usage within the
  200. * library only.
  201. */
  202. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  203. const char *file, int line,
  204. const char *text, const mbedtls_x509_crt *crt );
  205. #endif
  206. #if defined(MBEDTLS_ECDH_C)
  207. typedef enum
  208. {
  209. MBEDTLS_DEBUG_ECDH_Q,
  210. MBEDTLS_DEBUG_ECDH_QP,
  211. MBEDTLS_DEBUG_ECDH_Z,
  212. } mbedtls_debug_ecdh_attr;
  213. /**
  214. * \brief Print a field of the ECDH structure in the SSL context to the debug
  215. * output. This function is always used through the
  216. * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
  217. * and line number parameters.
  218. *
  219. * \param ssl SSL context
  220. * \param level error level of the debug message
  221. * \param file file the error has occurred in
  222. * \param line line number the error has occurred in
  223. * \param ecdh the ECDH context
  224. * \param attr the identifier of the attribute being output
  225. *
  226. * \attention This function is intended for INTERNAL usage within the
  227. * library only.
  228. */
  229. void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
  230. const char *file, int line,
  231. const mbedtls_ecdh_context *ecdh,
  232. mbedtls_debug_ecdh_attr attr );
  233. #endif
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif /* debug.h */