blob: a0f19c0ac46234c3d247899d0961075952e0094d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file debug.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Simon Butcher7ef5cf32016-02-13 22:20:04 +00004 * \brief Functions for controlling and providing debug output from the library.
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_DEBUG_H
11#define MBEDTLS_DEBUG_H
Paul Bakker5121ce52009-01-03 21:22:43 +000012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020015#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020017#endif
Rich Evans00ab4702015-02-06 13:43:58 +000018
Jaeden Amero6609aef2019-07-04 20:01:14 +010019#include "mbedtls/ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#if defined(MBEDTLS_ECP_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010022#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010027#define MBEDTLS_DEBUG_STRIP_PARENS(...) __VA_ARGS__
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010029#define MBEDTLS_SSL_DEBUG_MSG(level, args) \
30 mbedtls_debug_print_msg(ssl, level, __FILE__, __LINE__, \
31 MBEDTLS_DEBUG_STRIP_PARENS args)
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033#define MBEDTLS_SSL_DEBUG_RET(level, text, ret) \
34 mbedtls_debug_print_ret(ssl, level, __FILE__, __LINE__, text, ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036#define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) \
37 mbedtls_debug_print_buf(ssl, level, __FILE__, __LINE__, text, buf, len)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040#define MBEDTLS_SSL_DEBUG_MPI(level, text, X) \
41 mbedtls_debug_print_mpi(ssl, level, __FILE__, __LINE__, text, X)
Paul Bakkered27a042013-04-18 22:46:23 +020042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045#define MBEDTLS_SSL_DEBUG_ECP(level, text, X) \
46 mbedtls_debug_print_ecp(ssl, level, __FILE__, __LINE__, text, X)
Paul Bakkered27a042013-04-18 22:46:23 +020047#endif
Paul Bakker41c83d32013-03-20 14:39:14 +010048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
51 mbedtls_debug_print_crt(ssl, level, __FILE__, __LINE__, text, crt)
Paul Bakkered27a042013-04-18 22:46:23 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Janos Follath948f4be2018-08-22 01:37:55 +010054#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055#define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr) \
56 mbedtls_debug_printf_ecdh(ssl, level, __FILE__, __LINE__, ecdh, attr)
Janos Follath948f4be2018-08-22 01:37:55 +010057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#else /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061#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)
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Elliott4e589702020-12-09 14:38:01 +000071/**
72 * \def MBEDTLS_PRINTF_ATTRIBUTE
73 *
74 * Mark a function as having printf attributes, and thus enable checking
Paul Elliottaa5e1322021-03-05 15:52:25 +000075 * via -wFormat and other flags. This does nothing on builds with compilers
76 * that do not support the format attribute
Paul Elliott4e589702020-12-09 14:38:01 +000077 *
78 * Module: library/debug.c
79 * Caller:
80 *
81 * This module provides debugging functions.
82 */
Paul Elliottaa5e1322021-03-05 15:52:25 +000083#if defined(__has_attribute)
84#if __has_attribute(format)
eugene7f5cd472021-05-12 08:50:36 -040085#if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
Paul Elliott4e589702020-12-09 14:38:01 +000086#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 __attribute__((__format__(gnu_printf, string_index, first_to_check)))
eugene7f5cd472021-05-12 08:50:36 -040088#else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
89#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
90 __attribute__((format(printf, string_index, first_to_check)))
91#endif
Paul Elliottaa5e1322021-03-05 15:52:25 +000092#else /* __has_attribute(format) */
93#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
94#endif /* __has_attribute(format) */
95#else /* defined(__has_attribute) */
Paul Elliottabb3af72020-12-18 16:43:22 +000096#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
Paul Elliott4e589702020-12-09 14:38:01 +000097#endif
98
Paul Elliottd48d5c62021-01-07 14:47:05 +000099/**
100 * \def MBEDTLS_PRINTF_SIZET
101 *
102 * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
103 * and MinGW we need to define the printf specifier for size_t
104 * and long long per platform.
105 *
106 * Module: library/debug.c
107 * Caller:
108 *
109 * This module provides debugging functions.
110 */
Bence Szépkútib4f25122025-03-07 17:22:40 +0100111#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900)
Paul Elliott48438c72021-01-08 17:05:25 +0000112 #include <inttypes.h>
113 #define MBEDTLS_PRINTF_SIZET PRIuPTR
114 #define MBEDTLS_PRINTF_LONGLONG "I64d"
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115#else \
Bence Szépkútie7ee9022025-02-28 22:39:09 +0100116 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1900) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000117 #define MBEDTLS_PRINTF_SIZET "zu"
118 #define MBEDTLS_PRINTF_LONGLONG "lld"
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119#endif \
Bence Szépkútie7ee9022025-02-28 22:39:09 +0100120 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1900) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122#ifdef __cplusplus
123extern "C" {
124#endif
125
Paul Bakkerc73079a2014-04-25 16:34:30 +0200126/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000127 * \brief Set the threshold error level to handle globally all debug output.
128 * Debug messages that have a level over the threshold value are
129 * discarded.
130 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200131 *
Shaun Case0e7791f2021-12-20 21:14:10 -0800132 * \param threshold threshold level of messages to filter on. Messages at a
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000133 * higher level will be discarded.
134 * - Debug levels
135 * - 0 No debug
136 * - 1 Error
137 * - 2 State change
138 * - 3 Informational
139 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200140 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141void mbedtls_debug_set_threshold(int threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200142
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000143/**
Janos Follathd75b7822016-03-18 16:28:20 +0000144 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000145 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
146 * context, file and line number parameters.
147 *
148 * \param ssl SSL context
149 * \param level error level of the debug message
150 * \param file file the message has occurred in
151 * \param line line number the message has occurred at
152 * \param format format specifier, in printf format
153 * \param ... variables used by the format specifier
154 *
155 * \attention This function is intended for INTERNAL usage within the
156 * library only.
157 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
159 const char *file, int line,
160 const char *format, ...) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200161
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000162/**
163 * \brief Print the return value of a function to the debug output. This
164 * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
165 * which supplies the 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 the name of the function that returned the error
172 * \param ret the return code value
173 *
174 * \attention This function is intended for INTERNAL usage within the
175 * library only.
176 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
178 const char *file, int line,
179 const char *text, int ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000181/**
182 * \brief Output a buffer of size len bytes to the debug output. This function
183 * is always used through the MBEDTLS_SSL_DEBUG_BUF() 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 buffer being dumped. Normally the
191 * variable or buffer name
192 * \param buf the buffer to be outputted
193 * \param len length of the buffer
194 *
195 * \attention This function is intended for INTERNAL usage within the
196 * library only.
197 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
199 const char *file, int line, const char *text,
200 const unsigned char *buf, size_t len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000203/**
204 * \brief Print a MPI variable to the debug output. This function is always
205 * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
206 * ssl context, file and line number parameters.
207 *
208 * \param ssl SSL context
209 * \param level error level of the debug message
210 * \param file file the error has occurred in
211 * \param line line number the error has occurred in
212 * \param text a name or label for the MPI being output. Normally the
213 * variable name
214 * \param X the MPI variable
215 *
216 * \attention This function is intended for INTERNAL usage within the
217 * library only.
218 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
220 const char *file, int line,
221 const char *text, const mbedtls_mpi *X);
Paul Bakkered27a042013-04-18 22:46:23 +0200222#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000225/**
226 * \brief Print an ECP point to the debug output. This function is always
227 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
228 * ssl context, file and line number parameters.
229 *
230 * \param ssl SSL context
231 * \param level error level of the debug message
232 * \param file file the error has occurred in
233 * \param line line number the error has occurred in
234 * \param text a name or label for the ECP point being output. Normally the
235 * variable name
236 * \param X the ECP point
237 *
238 * \attention This function is intended for INTERNAL usage within the
239 * library only.
240 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
242 const char *file, int line,
243 const char *text, const mbedtls_ecp_point *X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000247/**
248 * \brief Print a X.509 certificate structure to the debug output. This
249 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
250 * which supplies the ssl context, file and line number parameters.
251 *
252 * \param ssl SSL context
253 * \param level error level of the debug message
254 * \param file file the error has occurred in
255 * \param line line number the error has occurred in
256 * \param text a name or label for the certificate being output
257 * \param crt X.509 certificate structure
258 *
259 * \attention This function is intended for INTERNAL usage within the
260 * library only.
261 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
263 const char *file, int line,
264 const char *text, const mbedtls_x509_crt *crt);
Paul Bakkered27a042013-04-18 22:46:23 +0200265#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
Janos Follath948f4be2018-08-22 01:37:55 +0100267#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268typedef enum {
Janos Follath948f4be2018-08-22 01:37:55 +0100269 MBEDTLS_DEBUG_ECDH_Q,
270 MBEDTLS_DEBUG_ECDH_QP,
271 MBEDTLS_DEBUG_ECDH_Z,
272} mbedtls_debug_ecdh_attr;
273
274/**
275 * \brief Print a field of the ECDH structure in the SSL context to the debug
276 * output. This function is always used through the
277 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
278 * and line number parameters.
279 *
280 * \param ssl SSL context
281 * \param level error level of the debug message
282 * \param file file the error has occurred in
283 * \param line line number the error has occurred in
284 * \param ecdh the ECDH context
285 * \param attr the identifier of the attribute being output
286 *
287 * \attention This function is intended for INTERNAL usage within the
288 * library only.
289 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
291 const char *file, int line,
292 const mbedtls_ecdh_context *ecdh,
293 mbedtls_debug_ecdh_attr attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100294#endif
295
Paul Bakker5121ce52009-01-03 21:22:43 +0000296#ifdef __cplusplus
297}
298#endif
299
300#endif /* debug.h */