blob: ea6d35c5162c5c9c3130caf323349afdb5b1cd3f [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)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020030# 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035# define MBEDTLS_DEBUG_STRIP_PARENS(...) __VA_ARGS__
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020036
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020037# 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020041# 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047# if defined(MBEDTLS_BIGNUM_C)
48# define MBEDTLS_SSL_DEBUG_MPI(level, text, X) \
49 mbedtls_debug_print_mpi(ssl, level, __FILE__, __LINE__, text, X)
50# endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020052# if defined(MBEDTLS_ECP_C)
53# define MBEDTLS_SSL_DEBUG_ECP(level, text, X) \
54 mbedtls_debug_print_ecp(ssl, level, __FILE__, __LINE__, text, X)
55# endif
Paul Bakker41c83d32013-03-20 14:39:14 +010056
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057# if defined(MBEDTLS_X509_CRT_PARSE_C)
58# if !defined(MBEDTLS_X509_REMOVE_INFO)
59# define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
60 mbedtls_debug_print_crt(ssl, level, __FILE__, __LINE__, text, \
61 crt)
62# else
63# define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
64 do { \
65 } while (0)
66# endif /* MBEDTLS_X509_REMOVE_INFO */
67# endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069# if defined(MBEDTLS_ECDH_C)
70# define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr) \
71 mbedtls_debug_printf_ecdh(ssl, level, __FILE__, __LINE__, ecdh, \
72 attr)
73# endif
Janos Follath948f4be2018-08-22 01:37:55 +010074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#else /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077# define MBEDTLS_SSL_DEBUG_MSG(level, args) \
78 do { \
79 } while (0)
80# define MBEDTLS_SSL_DEBUG_RET(level, text, ret) \
81 do { \
82 } while (0)
83# define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) \
84 do { \
85 } while (0)
86# define MBEDTLS_SSL_DEBUG_MPI(level, text, X) \
87 do { \
88 } while (0)
89# define MBEDTLS_SSL_DEBUG_ECP(level, text, X) \
90 do { \
91 } while (0)
92# define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
93 do { \
94 } while (0)
95# define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr) \
96 do { \
97 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Elliott4e589702020-12-09 14:38:01 +0000101/**
102 * \def MBEDTLS_PRINTF_ATTRIBUTE
103 *
104 * Mark a function as having printf attributes, and thus enable checking
Paul Elliottaa5e1322021-03-05 15:52:25 +0000105 * via -wFormat and other flags. This does nothing on builds with compilers
106 * that do not support the format attribute
Paul Elliott4e589702020-12-09 14:38:01 +0000107 *
108 * Module: library/debug.c
109 * Caller:
110 *
111 * This module provides debugging functions.
112 */
Paul Elliottaa5e1322021-03-05 15:52:25 +0000113#if defined(__has_attribute)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200114# if __has_attribute(format)
115# if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
116# define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
117 __attribute__(( \
118 __format__(gnu_printf, string_index, first_to_check)))
119# else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
120# define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
121 __attribute__((format(printf, string_index, first_to_check)))
122# endif
123# else /* __has_attribute(format) */
124# define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
125# endif /* __has_attribute(format) */
Paul Elliottaa5e1322021-03-05 15:52:25 +0000126#else /* defined(__has_attribute) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127# define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
Paul Elliott4e589702020-12-09 14:38:01 +0000128#endif
129
Paul Elliottd48d5c62021-01-07 14:47:05 +0000130/**
131 * \def MBEDTLS_PRINTF_SIZET
132 *
133 * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
134 * and MinGW we need to define the printf specifier for size_t
135 * and long long per platform.
136 *
137 * Module: library/debug.c
138 * Caller:
139 *
140 * This module provides debugging functions.
141 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200142#if (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || \
143 (defined(_MSC_VER) && _MSC_VER < 1800)
144# include <inttypes.h>
145# define MBEDTLS_PRINTF_SIZET PRIuPTR
146# define MBEDTLS_PRINTF_LONGLONG "I64d"
147#else /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || \
148 (defined(_MSC_VER) && _MSC_VER < 1800) */
149# define MBEDTLS_PRINTF_SIZET "zu"
150# define MBEDTLS_PRINTF_LONGLONG "lld"
151#endif /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || \
152 (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154#ifdef __cplusplus
155extern "C" {
156#endif
157
Paul Bakkerc73079a2014-04-25 16:34:30 +0200158/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000159 * \brief Set the threshold error level to handle globally all debug output.
160 * Debug messages that have a level over the threshold value are
161 * discarded.
162 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200163 *
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000164 * \param threshold theshold level of messages to filter on. Messages at a
165 * higher level will be discarded.
166 * - Debug levels
167 * - 0 No debug
168 * - 1 Error
169 * - 2 State change
170 * - 3 Informational
171 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200172 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173void mbedtls_debug_set_threshold(int threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200174
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000175/**
Janos Follathd75b7822016-03-18 16:28:20 +0000176 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000177 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
178 * context, file and line number parameters.
179 *
180 * \param ssl SSL context
181 * \param level error level of the debug message
182 * \param file file the message has occurred in
183 * \param line line number the message has occurred at
184 * \param format format specifier, in printf format
185 * \param ... variables used by the format specifier
186 *
187 * \attention This function is intended for INTERNAL usage within the
188 * library only.
189 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl,
191 int level,
192 const char *file,
193 int line,
194 const char *format,
195 ...) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200196
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000197/**
198 * \brief Print the return value of a function to the debug output. This
199 * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
200 * which supplies the ssl context, file and line number parameters.
201 *
202 * \param ssl SSL context
203 * \param level error level of the debug message
204 * \param file file the error has occurred in
205 * \param line line number the error has occurred in
206 * \param text the name of the function that returned the error
207 * \param ret the return code value
208 *
209 * \attention This function is intended for INTERNAL usage within the
210 * library only.
211 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl,
213 int level,
214 const char *file,
215 int line,
216 const char *text,
217 int ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000219/**
220 * \brief Output a buffer of size len bytes to the debug output. This function
221 * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
222 * which supplies the ssl context, file and line number parameters.
223 *
224 * \param ssl SSL context
225 * \param level error level of the debug message
226 * \param file file the error has occurred in
227 * \param line line number the error has occurred in
228 * \param text a name or label for the buffer being dumped. Normally the
229 * variable or buffer name
230 * \param buf the buffer to be outputted
231 * \param len length of the buffer
232 *
233 * \attention This function is intended for INTERNAL usage within the
234 * library only.
235 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl,
237 int level,
238 const char *file,
239 int line,
240 const char *text,
241 const unsigned char *buf,
242 size_t len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000245/**
246 * \brief Print a MPI variable to the debug output. This function is always
247 * used through the MBEDTLS_SSL_DEBUG_MPI() 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 MPI being output. Normally the
255 * variable name
256 * \param X the MPI variable
257 *
258 * \attention This function is intended for INTERNAL usage within the
259 * library only.
260 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200261void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl,
262 int level,
263 const char *file,
264 int line,
265 const char *text,
266 const mbedtls_mpi *X);
Paul Bakkered27a042013-04-18 22:46:23 +0200267#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000270/**
271 * \brief Print an ECP point to the debug output. This function is always
272 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
273 * ssl context, file and line number parameters.
274 *
275 * \param ssl SSL context
276 * \param level error level of the debug message
277 * \param file file the error has occurred in
278 * \param line line number the error has occurred in
279 * \param text a name or label for the ECP point being output. Normally the
280 * variable name
281 * \param X the ECP point
282 *
283 * \attention This function is intended for INTERNAL usage within the
284 * library only.
285 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200286void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl,
287 int level,
288 const char *file,
289 int line,
290 const char *text,
291 const mbedtls_ecp_point *X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100292#endif
293
Hanno Becker612a2f12020-10-09 09:19:39 +0100294#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000295/**
296 * \brief Print a X.509 certificate structure to the debug output. This
297 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
298 * which supplies the ssl context, file 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 text a name or label for the certificate being output
305 * \param crt X.509 certificate structure
306 *
307 * \attention This function is intended for INTERNAL usage within the
308 * library only.
309 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200310void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl,
311 int level,
312 const char *file,
313 int line,
314 const char *text,
315 const mbedtls_x509_crt *crt);
Paul Bakkered27a042013-04-18 22:46:23 +0200316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
Janos Follath948f4be2018-08-22 01:37:55 +0100318#if defined(MBEDTLS_ECDH_C)
319typedef enum
320{
321 MBEDTLS_DEBUG_ECDH_Q,
322 MBEDTLS_DEBUG_ECDH_QP,
323 MBEDTLS_DEBUG_ECDH_Z,
324} mbedtls_debug_ecdh_attr;
325
326/**
327 * \brief Print a field of the ECDH structure in the SSL context to the debug
328 * output. This function is always used through the
329 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
330 * and line number parameters.
331 *
332 * \param ssl SSL context
333 * \param level error level of the debug message
334 * \param file file the error has occurred in
335 * \param line line number the error has occurred in
336 * \param ecdh the ECDH context
337 * \param attr the identifier of the attribute being output
338 *
339 * \attention This function is intended for INTERNAL usage within the
340 * library only.
341 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200342void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl,
343 int level,
344 const char *file,
345 int line,
346 const mbedtls_ecdh_context *ecdh,
347 mbedtls_debug_ecdh_attr attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100348#endif
349
Paul Bakker5121ce52009-01-03 21:22:43 +0000350#ifdef __cplusplus
351}
352#endif
353
354#endif /* debug.h */