blob: 3ca88408349b285552611b221a6d7954970324d4 [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 Lv5b8dcd22022-11-17 15:11:39 +080085#define MBEDTLS_SSL_DEBUG_TICKET_FLAGS(level, flag) do { } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Paul Elliott4e589702020-12-09 14:38:01 +000089/**
90 * \def MBEDTLS_PRINTF_ATTRIBUTE
91 *
92 * Mark a function as having printf attributes, and thus enable checking
Paul Elliottaa5e1322021-03-05 15:52:25 +000093 * via -wFormat and other flags. This does nothing on builds with compilers
94 * that do not support the format attribute
Paul Elliott4e589702020-12-09 14:38:01 +000095 *
96 * Module: library/debug.c
97 * Caller:
98 *
99 * This module provides debugging functions.
100 */
Paul Elliottaa5e1322021-03-05 15:52:25 +0000101#if defined(__has_attribute)
102#if __has_attribute(format)
eugenee42a50d2021-05-12 12:33:36 -0400103#if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
Paul Elliott4e589702020-12-09 14:38:01 +0000104#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 __attribute__((__format__(gnu_printf, string_index, first_to_check)))
eugenee42a50d2021-05-12 12:33:36 -0400106#else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
107#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
108 __attribute__((format(printf, string_index, first_to_check)))
109#endif
Paul Elliottaa5e1322021-03-05 15:52:25 +0000110#else /* __has_attribute(format) */
111#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
112#endif /* __has_attribute(format) */
113#else /* defined(__has_attribute) */
Paul Elliottabb3af72020-12-18 16:43:22 +0000114#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
Paul Elliott4e589702020-12-09 14:38:01 +0000115#endif
116
Paul Elliottd48d5c62021-01-07 14:47:05 +0000117/**
118 * \def MBEDTLS_PRINTF_SIZET
119 *
120 * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
121 * and MinGW we need to define the printf specifier for size_t
122 * and long long per platform.
123 *
124 * Module: library/debug.c
125 * Caller:
126 *
127 * This module provides debugging functions.
128 */
eugenee42a50d2021-05-12 12:33:36 -0400129#if (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800)
Paul Elliott48438c72021-01-08 17:05:25 +0000130 #include <inttypes.h>
131 #define MBEDTLS_PRINTF_SIZET PRIuPTR
132 #define MBEDTLS_PRINTF_LONGLONG "I64d"
Gilles Peskine449bd832023-01-11 14:50:10 +0100133#else \
134 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000135 #define MBEDTLS_PRINTF_SIZET "zu"
136 #define MBEDTLS_PRINTF_LONGLONG "lld"
Gilles Peskine449bd832023-01-11 14:50:10 +0100137#endif \
138 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140#ifdef __cplusplus
141extern "C" {
142#endif
143
Paul Bakkerc73079a2014-04-25 16:34:30 +0200144/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000145 * \brief Set the threshold error level to handle globally all debug output.
146 * Debug messages that have a level over the threshold value are
147 * discarded.
148 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200149 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800150 * \param threshold threshold level of messages to filter on. Messages at a
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000151 * higher level will be discarded.
152 * - Debug levels
153 * - 0 No debug
154 * - 1 Error
155 * - 2 State change
156 * - 3 Informational
157 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200158 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159void mbedtls_debug_set_threshold(int threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200160
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000161/**
Janos Follathd75b7822016-03-18 16:28:20 +0000162 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000163 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
164 * context, file and line number parameters.
165 *
166 * \param ssl SSL context
167 * \param level error level of the debug message
168 * \param file file the message has occurred in
169 * \param line line number the message has occurred at
170 * \param format format specifier, in printf format
171 * \param ... variables used by the format specifier
172 *
173 * \attention This function is intended for INTERNAL usage within the
174 * library only.
175 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100176void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
177 const char *file, int line,
178 const char *format, ...) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200179
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000180/**
181 * \brief Print the return value of a function to the debug output. This
182 * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
183 * which supplies the ssl context, file and line number parameters.
184 *
185 * \param ssl SSL context
186 * \param level error level of the debug message
187 * \param file file the error has occurred in
188 * \param line line number the error has occurred in
189 * \param text the name of the function that returned the error
190 * \param ret the return code value
191 *
192 * \attention This function is intended for INTERNAL usage within the
193 * library only.
194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
196 const char *file, int line,
197 const char *text, int ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000199/**
200 * \brief Output a buffer of size len bytes to the debug output. This function
201 * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
202 * which supplies the ssl context, file and line number parameters.
203 *
204 * \param ssl SSL context
205 * \param level error level of the debug message
206 * \param file file the error has occurred in
207 * \param line line number the error has occurred in
208 * \param text a name or label for the buffer being dumped. Normally the
209 * variable or buffer name
210 * \param buf the buffer to be outputted
211 * \param len length of the buffer
212 *
213 * \attention This function is intended for INTERNAL usage within the
214 * library only.
215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
217 const char *file, int line, const char *text,
218 const unsigned char *buf, size_t len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000221/**
222 * \brief Print a MPI variable to the debug output. This function is always
223 * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
224 * ssl context, file and line number parameters.
225 *
226 * \param ssl SSL context
227 * \param level error level of the debug message
228 * \param file file the error has occurred in
229 * \param line line number the error has occurred in
230 * \param text a name or label for the MPI being output. Normally the
231 * variable name
232 * \param X the MPI variable
233 *
234 * \attention This function is intended for INTERNAL usage within the
235 * library only.
236 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
238 const char *file, int line,
239 const char *text, const mbedtls_mpi *X);
Paul Bakkered27a042013-04-18 22:46:23 +0200240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000243/**
244 * \brief Print an ECP point to the debug output. This function is always
245 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
246 * ssl context, file and line number parameters.
247 *
248 * \param ssl SSL context
249 * \param level error level of the debug message
250 * \param file file the error has occurred in
251 * \param line line number the error has occurred in
252 * \param text a name or label for the ECP point being output. Normally the
253 * variable name
254 * \param X the ECP point
255 *
256 * \attention This function is intended for INTERNAL usage within the
257 * library only.
258 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
260 const char *file, int line,
261 const char *text, const mbedtls_ecp_point *X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100262#endif
263
Hanno Becker612a2f12020-10-09 09:19:39 +0100264#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000265/**
266 * \brief Print a X.509 certificate structure to the debug output. This
267 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
268 * which supplies the ssl context, file and line number parameters.
269 *
270 * \param ssl SSL context
271 * \param level error level of the debug message
272 * \param file file the error has occurred in
273 * \param line line number the error has occurred in
274 * \param text a name or label for the certificate being output
275 * \param crt X.509 certificate structure
276 *
277 * \attention This function is intended for INTERNAL usage within the
278 * library only.
279 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
281 const char *file, int line,
282 const char *text, const mbedtls_x509_crt *crt);
Paul Bakkered27a042013-04-18 22:46:23 +0200283#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
Janos Follath948f4be2018-08-22 01:37:55 +0100285#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100286typedef enum {
Janos Follath948f4be2018-08-22 01:37:55 +0100287 MBEDTLS_DEBUG_ECDH_Q,
288 MBEDTLS_DEBUG_ECDH_QP,
289 MBEDTLS_DEBUG_ECDH_Z,
290} mbedtls_debug_ecdh_attr;
291
292/**
293 * \brief Print a field of the ECDH structure in the SSL context to the debug
294 * output. This function is always used through the
295 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
296 * and line number parameters.
297 *
298 * \param ssl SSL context
299 * \param level error level of the debug message
300 * \param file file the error has occurred in
301 * \param line line number the error has occurred in
302 * \param ecdh the ECDH context
303 * \param attr the identifier of the attribute being output
304 *
305 * \attention This function is intended for INTERNAL usage within the
306 * library only.
307 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
309 const char *file, int line,
310 const mbedtls_ecdh_context *ecdh,
311 mbedtls_debug_ecdh_attr attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100312#endif
313
Pengyu Lv5b8dcd22022-11-17 15:11:39 +0800314#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
315void mbedtls_debug_print_ticket_flags(
316 const mbedtls_ssl_context *ssl, int level,
317 const char *file, int line,
318 mbedtls_ssl_tls13_ticket_flags flag);
319#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
320
Paul Bakker5121ce52009-01-03 21:22:43 +0000321#ifdef __cplusplus
322}
323#endif
324
325#endif /* debug.h */