blob: 7f813a3cf88df770757b1ef869f7c2d36eb7b450 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_DEBUG_H
23#define MBEDTLS_DEBUG_H
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Bence Szépkútic662b362021-05-27 11:25:03 +020025#include "mbedtls/build_info.h"
Rich Evans00ab4702015-02-06 13:43:58 +000026
Jaeden Amero6609aef2019-07-04 20:01:14 +010027#include "mbedtls/ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ECP_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010030#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Gilles Peskine449bd832023-01-11 14:50:10 +010035#define MBEDTLS_DEBUG_STRIP_PARENS(...) __VA_ARGS__
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020036
Gilles Peskine449bd832023-01-11 14:50:10 +010037#define MBEDTLS_SSL_DEBUG_MSG(level, args) \
38 mbedtls_debug_print_msg(ssl, level, __FILE__, __LINE__, \
39 MBEDTLS_DEBUG_STRIP_PARENS args)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Gilles Peskine449bd832023-01-11 14:50:10 +010041#define MBEDTLS_SSL_DEBUG_RET(level, text, ret) \
42 mbedtls_debug_print_ret(ssl, level, __FILE__, __LINE__, text, ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Gilles Peskine449bd832023-01-11 14:50:10 +010044#define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) \
45 mbedtls_debug_print_buf(ssl, level, __FILE__, __LINE__, text, buf, len)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010048#define MBEDTLS_SSL_DEBUG_MPI(level, text, X) \
49 mbedtls_debug_print_mpi(ssl, level, __FILE__, __LINE__, text, X)
Paul Bakkered27a042013-04-18 22:46:23 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010053#define MBEDTLS_SSL_DEBUG_ECP(level, text, X) \
54 mbedtls_debug_print_ecp(ssl, level, __FILE__, __LINE__, text, X)
Paul Bakkered27a042013-04-18 22:46:23 +020055#endif
Paul Bakker41c83d32013-03-20 14:39:14 +010056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker612a2f12020-10-09 09:19:39 +010058#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +010059#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
60 mbedtls_debug_print_crt(ssl, level, __FILE__, __LINE__, text, crt)
Peter Kolbus9a969b62018-12-11 13:55:56 -060061#else
Gilles Peskine449bd832023-01-11 14:50:10 +010062#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) do { } while (0)
Hanno Becker612a2f12020-10-09 09:19:39 +010063#endif /* MBEDTLS_X509_REMOVE_INFO */
Peter Kolbus9a969b62018-12-11 13:55:56 -060064#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Janos Follath948f4be2018-08-22 01:37:55 +010066#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010067#define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr) \
68 mbedtls_debug_printf_ecdh(ssl, level, __FILE__, __LINE__, ecdh, attr)
Janos Follath948f4be2018-08-22 01:37:55 +010069#endif
70
Pengyu Lv5b8dcd22022-11-17 15:11:39 +080071#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
72#define MBEDTLS_SSL_DEBUG_TICKET_FLAGS(level, flag) \
73 mbedtls_debug_print_ticket_flags(ssl, level, __FILE__, __LINE__, flag)
74#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#else /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Gilles Peskine449bd832023-01-11 14:50:10 +010078#define MBEDTLS_SSL_DEBUG_MSG(level, args) do { } while (0)
79#define MBEDTLS_SSL_DEBUG_RET(level, text, ret) do { } while (0)
80#define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) do { } while (0)
81#define MBEDTLS_SSL_DEBUG_MPI(level, text, X) do { } while (0)
82#define MBEDTLS_SSL_DEBUG_ECP(level, text, X) do { } while (0)
83#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) do { } while (0)
84#define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr) do { } while (0)
Pengyu Lva1aa31b2022-12-13 13:49:59 +080085#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv5b8dcd22022-11-17 15:11:39 +080086#define MBEDTLS_SSL_DEBUG_TICKET_FLAGS(level, flag) do { } while (0)
Pengyu Lva1aa31b2022-12-13 13:49:59 +080087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Elliott4e589702020-12-09 14:38:01 +000091/**
92 * \def MBEDTLS_PRINTF_ATTRIBUTE
93 *
94 * Mark a function as having printf attributes, and thus enable checking
Paul Elliottaa5e1322021-03-05 15:52:25 +000095 * via -wFormat and other flags. This does nothing on builds with compilers
96 * that do not support the format attribute
Paul Elliott4e589702020-12-09 14:38:01 +000097 *
98 * Module: library/debug.c
99 * Caller:
100 *
101 * This module provides debugging functions.
102 */
Paul Elliottaa5e1322021-03-05 15:52:25 +0000103#if defined(__has_attribute)
104#if __has_attribute(format)
eugenee42a50d2021-05-12 12:33:36 -0400105#if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
Paul Elliott4e589702020-12-09 14:38:01 +0000106#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 __attribute__((__format__(gnu_printf, string_index, first_to_check)))
eugenee42a50d2021-05-12 12:33:36 -0400108#else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
109#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
110 __attribute__((format(printf, string_index, first_to_check)))
111#endif
Paul Elliottaa5e1322021-03-05 15:52:25 +0000112#else /* __has_attribute(format) */
113#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
114#endif /* __has_attribute(format) */
115#else /* defined(__has_attribute) */
Paul Elliottabb3af72020-12-18 16:43:22 +0000116#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
Paul Elliott4e589702020-12-09 14:38:01 +0000117#endif
118
Paul Elliottd48d5c62021-01-07 14:47:05 +0000119/**
120 * \def MBEDTLS_PRINTF_SIZET
121 *
122 * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
123 * and MinGW we need to define the printf specifier for size_t
124 * and long long per platform.
125 *
126 * Module: library/debug.c
127 * Caller:
128 *
129 * This module provides debugging functions.
130 */
eugenee42a50d2021-05-12 12:33:36 -0400131#if (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800)
Paul Elliott48438c72021-01-08 17:05:25 +0000132 #include <inttypes.h>
133 #define MBEDTLS_PRINTF_SIZET PRIuPTR
134 #define MBEDTLS_PRINTF_LONGLONG "I64d"
Gilles Peskine449bd832023-01-11 14:50:10 +0100135#else \
136 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000137 #define MBEDTLS_PRINTF_SIZET "zu"
138 #define MBEDTLS_PRINTF_LONGLONG "lld"
Gilles Peskine449bd832023-01-11 14:50:10 +0100139#endif \
140 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000141
Paul Bakker5121ce52009-01-03 21:22:43 +0000142#ifdef __cplusplus
143extern "C" {
144#endif
145
Paul Bakkerc73079a2014-04-25 16:34:30 +0200146/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000147 * \brief Set the threshold error level to handle globally all debug output.
148 * Debug messages that have a level over the threshold value are
149 * discarded.
150 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200151 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800152 * \param threshold threshold level of messages to filter on. Messages at a
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000153 * higher level will be discarded.
154 * - Debug levels
155 * - 0 No debug
156 * - 1 Error
157 * - 2 State change
158 * - 3 Informational
159 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200160 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161void mbedtls_debug_set_threshold(int threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200162
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000163/**
Janos Follathd75b7822016-03-18 16:28:20 +0000164 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000165 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
166 * context, file and line number parameters.
167 *
168 * \param ssl SSL context
169 * \param level error level of the debug message
170 * \param file file the message has occurred in
171 * \param line line number the message has occurred at
172 * \param format format specifier, in printf format
173 * \param ... variables used by the format specifier
174 *
175 * \attention This function is intended for INTERNAL usage within the
176 * library only.
177 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
179 const char *file, int line,
180 const char *format, ...) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200181
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000182/**
183 * \brief Print the return value of a function to the debug output. This
184 * function is always used through the MBEDTLS_SSL_DEBUG_RET() 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 the name of the function that returned the error
192 * \param ret the return code value
193 *
194 * \attention This function is intended for INTERNAL usage within the
195 * library only.
196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
198 const char *file, int line,
199 const char *text, int ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000201/**
202 * \brief Output a buffer of size len bytes to the debug output. This function
203 * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
204 * which supplies the ssl context, file and line number parameters.
205 *
206 * \param ssl SSL context
207 * \param level error level of the debug message
208 * \param file file the error has occurred in
209 * \param line line number the error has occurred in
210 * \param text a name or label for the buffer being dumped. Normally the
211 * variable or buffer name
212 * \param buf the buffer to be outputted
213 * \param len length of the buffer
214 *
215 * \attention This function is intended for INTERNAL usage within the
216 * library only.
217 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
219 const char *file, int line, const char *text,
220 const unsigned char *buf, size_t len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000223/**
224 * \brief Print a MPI variable to the debug output. This function is always
225 * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
226 * ssl context, file and line number parameters.
227 *
228 * \param ssl SSL context
229 * \param level error level of the debug message
230 * \param file file the error has occurred in
231 * \param line line number the error has occurred in
232 * \param text a name or label for the MPI being output. Normally the
233 * variable name
234 * \param X the MPI variable
235 *
236 * \attention This function is intended for INTERNAL usage within the
237 * library only.
238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
240 const char *file, int line,
241 const char *text, const mbedtls_mpi *X);
Paul Bakkered27a042013-04-18 22:46:23 +0200242#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000245/**
246 * \brief Print an ECP point to the debug output. This function is always
247 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
248 * ssl context, file and line number parameters.
249 *
250 * \param ssl SSL context
251 * \param level error level of the debug message
252 * \param file file the error has occurred in
253 * \param line line number the error has occurred in
254 * \param text a name or label for the ECP point being output. Normally the
255 * variable name
256 * \param X the ECP point
257 *
258 * \attention This function is intended for INTERNAL usage within the
259 * library only.
260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
262 const char *file, int line,
263 const char *text, const mbedtls_ecp_point *X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100264#endif
265
Hanno Becker612a2f12020-10-09 09:19:39 +0100266#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000267/**
268 * \brief Print a X.509 certificate structure to the debug output. This
269 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
270 * which supplies the ssl context, file and line number parameters.
271 *
272 * \param ssl SSL context
273 * \param level error level of the debug message
274 * \param file file the error has occurred in
275 * \param line line number the error has occurred in
276 * \param text a name or label for the certificate being output
277 * \param crt X.509 certificate structure
278 *
279 * \attention This function is intended for INTERNAL usage within the
280 * library only.
281 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
283 const char *file, int line,
284 const char *text, const mbedtls_x509_crt *crt);
Paul Bakkered27a042013-04-18 22:46:23 +0200285#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Janos Follath948f4be2018-08-22 01:37:55 +0100287#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100288typedef enum {
Janos Follath948f4be2018-08-22 01:37:55 +0100289 MBEDTLS_DEBUG_ECDH_Q,
290 MBEDTLS_DEBUG_ECDH_QP,
291 MBEDTLS_DEBUG_ECDH_Z,
292} mbedtls_debug_ecdh_attr;
293
294/**
295 * \brief Print a field of the ECDH structure in the SSL context to the debug
296 * output. This function is always used through the
297 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
298 * and line number parameters.
299 *
300 * \param ssl SSL context
301 * \param level error level of the debug message
302 * \param file file the error has occurred in
303 * \param line line number the error has occurred in
304 * \param ecdh the ECDH context
305 * \param attr the identifier of the attribute being output
306 *
307 * \attention This function is intended for INTERNAL usage within the
308 * library only.
309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
311 const char *file, int line,
312 const mbedtls_ecdh_context *ecdh,
313 mbedtls_debug_ecdh_attr attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100314#endif
315
Pengyu Lv5b8dcd22022-11-17 15:11:39 +0800316#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
317void mbedtls_debug_print_ticket_flags(
318 const mbedtls_ssl_context *ssl, int level,
319 const char *file, int line,
320 mbedtls_ssl_tls13_ticket_flags flag);
321#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
322
Paul Bakker5121ce52009-01-03 21:22:43 +0000323#ifdef __cplusplus
324}
325#endif
326
327#endif /* debug.h */