blob: 9a17488d2b0b4186a6f80c8687bade5e3d90936d [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 Rodgman16799db2023-11-02 19:47:20 +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
Bence Szépkútic662b362021-05-27 11:25:03 +020013#include "mbedtls/build_info.h"
Rich Evans00ab4702015-02-06 13:43:58 +000014
Jaeden Amero6609aef2019-07-04 20:01:14 +010015#include "mbedtls/ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#if defined(MBEDTLS_ECP_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010018#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010019#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000022
Gilles Peskine449bd832023-01-11 14:50:10 +010023#define MBEDTLS_DEBUG_STRIP_PARENS(...) __VA_ARGS__
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020024
Gilles Peskine449bd832023-01-11 14:50:10 +010025#define MBEDTLS_SSL_DEBUG_MSG(level, args) \
26 mbedtls_debug_print_msg(ssl, level, __FILE__, __LINE__, \
27 MBEDTLS_DEBUG_STRIP_PARENS args)
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Gilles Peskine449bd832023-01-11 14:50:10 +010029#define MBEDTLS_SSL_DEBUG_RET(level, text, ret) \
30 mbedtls_debug_print_ret(ssl, level, __FILE__, __LINE__, text, ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Gilles Peskine449bd832023-01-11 14:50:10 +010032#define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) \
33 mbedtls_debug_print_buf(ssl, level, __FILE__, __LINE__, text, buf, len)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010036#define MBEDTLS_SSL_DEBUG_MPI(level, text, X) \
37 mbedtls_debug_print_mpi(ssl, level, __FILE__, __LINE__, text, X)
Paul Bakkered27a042013-04-18 22:46:23 +020038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010041#define MBEDTLS_SSL_DEBUG_ECP(level, text, X) \
42 mbedtls_debug_print_ecp(ssl, level, __FILE__, __LINE__, text, X)
Paul Bakkered27a042013-04-18 22:46:23 +020043#endif
Paul Bakker41c83d32013-03-20 14:39:14 +010044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker612a2f12020-10-09 09:19:39 +010046#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +010047#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
48 mbedtls_debug_print_crt(ssl, level, __FILE__, __LINE__, text, crt)
Peter Kolbus9a969b62018-12-11 13:55:56 -060049#else
Gilles Peskine449bd832023-01-11 14:50:10 +010050#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) do { } while (0)
Hanno Becker612a2f12020-10-09 09:19:39 +010051#endif /* MBEDTLS_X509_REMOVE_INFO */
Peter Kolbus9a969b62018-12-11 13:55:56 -060052#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Janos Follath948f4be2018-08-22 01:37:55 +010054#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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)
eugenee42a50d2021-05-12 12:33: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 Peskine449bd832023-01-11 14:50:10 +010087 __attribute__((__format__(gnu_printf, string_index, first_to_check)))
eugenee42a50d2021-05-12 12:33: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 */
eugenee42a50d2021-05-12 12:33:36 -0400111#if (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800)
Paul Elliott48438c72021-01-08 17:05:25 +0000112 #include <inttypes.h>
113 #define MBEDTLS_PRINTF_SIZET PRIuPTR
114 #define MBEDTLS_PRINTF_LONGLONG "I64d"
Gilles Peskine449bd832023-01-11 14:50:10 +0100115#else \
116 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000117 #define MBEDTLS_PRINTF_SIZET "zu"
118 #define MBEDTLS_PRINTF_LONGLONG "lld"
Gilles Peskine449bd832023-01-11 14:50:10 +0100119#endif \
120 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000121
Jerry Yueba0ab52022-12-15 17:41:41 +0800122#if !defined(MBEDTLS_PRINTF_MS_TIME)
Jerry Yu03511b02023-02-27 10:48:41 +0800123#include <inttypes.h>
124#if !defined(PRId64)
125#define MBEDTLS_PRINTF_MS_TIME MBEDTLS_PRINTF_LONGLONG
126#else
Jerry Yu041c8c12023-02-03 13:15:09 +0800127#define MBEDTLS_PRINTF_MS_TIME PRId64
Jerry Yu03511b02023-02-27 10:48:41 +0800128#endif
Jerry Yueba0ab52022-12-15 17:41:41 +0800129#endif /* MBEDTLS_PRINTF_MS_TIME */
130
Paul Bakker5121ce52009-01-03 21:22:43 +0000131#ifdef __cplusplus
132extern "C" {
133#endif
134
Paul Bakkerc73079a2014-04-25 16:34:30 +0200135/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000136 * \brief Set the threshold error level to handle globally all debug output.
137 * Debug messages that have a level over the threshold value are
138 * discarded.
139 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200140 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800141 * \param threshold threshold level of messages to filter on. Messages at a
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000142 * higher level will be discarded.
143 * - Debug levels
144 * - 0 No debug
145 * - 1 Error
146 * - 2 State change
147 * - 3 Informational
148 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150void mbedtls_debug_set_threshold(int threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200151
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000152/**
Janos Follathd75b7822016-03-18 16:28:20 +0000153 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000154 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
155 * context, file and line number parameters.
156 *
157 * \param ssl SSL context
158 * \param level error level of the debug message
159 * \param file file the message has occurred in
160 * \param line line number the message has occurred at
161 * \param format format specifier, in printf format
162 * \param ... variables used by the format specifier
163 *
164 * \attention This function is intended for INTERNAL usage within the
165 * library only.
166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
168 const char *file, int line,
169 const char *format, ...) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200170
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000171/**
172 * \brief Print the return value of a function to the debug output. This
173 * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
174 * which supplies the ssl context, file and line number parameters.
175 *
176 * \param ssl SSL context
177 * \param level error level of the debug message
178 * \param file file the error has occurred in
179 * \param line line number the error has occurred in
180 * \param text the name of the function that returned the error
181 * \param ret the return code value
182 *
183 * \attention This function is intended for INTERNAL usage within the
184 * library only.
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
187 const char *file, int line,
188 const char *text, int ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000190/**
191 * \brief Output a buffer of size len bytes to the debug output. This function
192 * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
193 * which supplies the ssl context, file and line number parameters.
194 *
195 * \param ssl SSL context
196 * \param level error level of the debug message
197 * \param file file the error has occurred in
198 * \param line line number the error has occurred in
199 * \param text a name or label for the buffer being dumped. Normally the
200 * variable or buffer name
201 * \param buf the buffer to be outputted
202 * \param len length of the buffer
203 *
204 * \attention This function is intended for INTERNAL usage within the
205 * library only.
206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
208 const char *file, int line, const char *text,
209 const unsigned char *buf, size_t len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000212/**
213 * \brief Print a MPI variable to the debug output. This function is always
214 * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
215 * ssl context, file and line number parameters.
216 *
217 * \param ssl SSL context
218 * \param level error level of the debug message
219 * \param file file the error has occurred in
220 * \param line line number the error has occurred in
221 * \param text a name or label for the MPI being output. Normally the
222 * variable name
223 * \param X the MPI variable
224 *
225 * \attention This function is intended for INTERNAL usage within the
226 * library only.
227 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
229 const char *file, int line,
230 const char *text, const mbedtls_mpi *X);
Paul Bakkered27a042013-04-18 22:46:23 +0200231#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000234/**
235 * \brief Print an ECP point to the debug output. This function is always
236 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
237 * ssl context, file and line number parameters.
238 *
239 * \param ssl SSL context
240 * \param level error level of the debug message
241 * \param file file the error has occurred in
242 * \param line line number the error has occurred in
243 * \param text a name or label for the ECP point being output. Normally the
244 * variable name
245 * \param X the ECP point
246 *
247 * \attention This function is intended for INTERNAL usage within the
248 * library only.
249 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
251 const char *file, int line,
252 const char *text, const mbedtls_ecp_point *X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100253#endif
254
Hanno Becker612a2f12020-10-09 09:19:39 +0100255#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000256/**
257 * \brief Print a X.509 certificate structure to the debug output. This
258 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
259 * which supplies the ssl context, file and line number parameters.
260 *
261 * \param ssl SSL context
262 * \param level error level of the debug message
263 * \param file file the error has occurred in
264 * \param line line number the error has occurred in
265 * \param text a name or label for the certificate being output
266 * \param crt X.509 certificate structure
267 *
268 * \attention This function is intended for INTERNAL usage within the
269 * library only.
270 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
272 const char *file, int line,
273 const char *text, const mbedtls_x509_crt *crt);
Paul Bakkered27a042013-04-18 22:46:23 +0200274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Valerio Settid0371b02023-07-25 10:57:01 +0200276/* Note: the MBEDTLS_ECDH_C guard here is mandatory because this debug function
277 only works for the built-in implementation. */
278#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) && \
279 defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280typedef enum {
Janos Follath948f4be2018-08-22 01:37:55 +0100281 MBEDTLS_DEBUG_ECDH_Q,
282 MBEDTLS_DEBUG_ECDH_QP,
283 MBEDTLS_DEBUG_ECDH_Z,
284} mbedtls_debug_ecdh_attr;
285
286/**
287 * \brief Print a field of the ECDH structure in the SSL context to the debug
288 * output. This function is always used through the
289 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
290 * and line number parameters.
291 *
292 * \param ssl SSL context
293 * \param level error level of the debug message
294 * \param file file the error has occurred in
295 * \param line line number the error has occurred in
296 * \param ecdh the ECDH context
297 * \param attr the identifier of the attribute being output
298 *
299 * \attention This function is intended for INTERNAL usage within the
300 * library only.
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
303 const char *file, int line,
304 const mbedtls_ecdh_context *ecdh,
305 mbedtls_debug_ecdh_attr attr);
Valerio Settid0371b02023-07-25 10:57:01 +0200306#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED &&
307 MBEDTLS_ECDH_C */
Janos Follath948f4be2018-08-22 01:37:55 +0100308
Paul Bakker5121ce52009-01-03 21:22:43 +0000309#ifdef __cplusplus
310}
311#endif
312
313#endif /* debug.h */