blob: 71b08295d6c86b127b1ac46fb1b4ca36aab8757b [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010026#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#endif
Rich Evans00ab4702015-02-06 13:43:58 +000030
Jaeden Amero6609aef2019-07-04 20:01:14 +010031#include "mbedtls/ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_ECP_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010034#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010035#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020039#define MBEDTLS_DEBUG_STRIP_PARENS( ... ) __VA_ARGS__
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define MBEDTLS_SSL_DEBUG_MSG( level, args ) \
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020042 mbedtls_debug_print_msg( ssl, level, __FILE__, __LINE__, \
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020043 MBEDTLS_DEBUG_STRIP_PARENS args )
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020046 mbedtls_debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020049 mbedtls_debug_print_buf( ssl, level, __FILE__, __LINE__, text, buf, len )
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_BIGNUM_C)
52#define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020053 mbedtls_debug_print_mpi( ssl, level, __FILE__, __LINE__, text, X )
Paul Bakkered27a042013-04-18 22:46:23 +020054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_ECP_C)
57#define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020058 mbedtls_debug_print_ecp( ssl, level, __FILE__, __LINE__, text, X )
Paul Bakkered27a042013-04-18 22:46:23 +020059#endif
Paul Bakker41c83d32013-03-20 14:39:14 +010060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker612a2f12020-10-09 09:19:39 +010062#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020064 mbedtls_debug_print_crt( ssl, level, __FILE__, __LINE__, text, crt )
Peter Kolbus9a969b62018-12-11 13:55:56 -060065#else
66#define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) do { } while( 0 )
Hanno Becker612a2f12020-10-09 09:19:39 +010067#endif /* MBEDTLS_X509_REMOVE_INFO */
Peter Kolbus9a969b62018-12-11 13:55:56 -060068#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Janos Follath948f4be2018-08-22 01:37:55 +010070#if defined(MBEDTLS_ECDH_C)
71#define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr ) \
72 mbedtls_debug_printf_ecdh( ssl, level, __FILE__, __LINE__, ecdh, attr )
73#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#else /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#define MBEDTLS_SSL_DEBUG_MSG( level, args ) do { } while( 0 )
78#define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) do { } while( 0 )
79#define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) do { } while( 0 )
80#define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) do { } while( 0 )
81#define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) do { } while( 0 )
82#define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) do { } while( 0 )
Janos Follath948f4be2018-08-22 01:37:55 +010083#define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr ) do { } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Paul Elliott4e589702020-12-09 14:38:01 +000087/**
88 * \def MBEDTLS_PRINTF_ATTRIBUTE
89 *
90 * Mark a function as having printf attributes, and thus enable checking
Paul Elliottaa5e1322021-03-05 15:52:25 +000091 * via -wFormat and other flags. This does nothing on builds with compilers
92 * that do not support the format attribute
Paul Elliott4e589702020-12-09 14:38:01 +000093 *
94 * Module: library/debug.c
95 * Caller:
96 *
97 * This module provides debugging functions.
98 */
Paul Elliottaa5e1322021-03-05 15:52:25 +000099#if defined(__has_attribute)
100#if __has_attribute(format)
eugenee42a50d2021-05-12 12:33:36 -0400101#if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
Paul Elliott4e589702020-12-09 14:38:01 +0000102#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
eugenee42a50d2021-05-12 12:33:36 -0400103 __attribute__((__format__ (gnu_printf, string_index, first_to_check)))
104#else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
105#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
106 __attribute__((format(printf, string_index, first_to_check)))
107#endif
Paul Elliottaa5e1322021-03-05 15:52:25 +0000108#else /* __has_attribute(format) */
109#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
110#endif /* __has_attribute(format) */
111#else /* defined(__has_attribute) */
Paul Elliottabb3af72020-12-18 16:43:22 +0000112#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
Paul Elliott4e589702020-12-09 14:38:01 +0000113#endif
114
Paul Elliottd48d5c62021-01-07 14:47:05 +0000115/**
116 * \def MBEDTLS_PRINTF_SIZET
117 *
118 * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
119 * and MinGW we need to define the printf specifier for size_t
120 * and long long per platform.
121 *
122 * Module: library/debug.c
123 * Caller:
124 *
125 * This module provides debugging functions.
126 */
eugenee42a50d2021-05-12 12:33:36 -0400127#if (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800)
Paul Elliott48438c72021-01-08 17:05:25 +0000128 #include <inttypes.h>
129 #define MBEDTLS_PRINTF_SIZET PRIuPTR
130 #define MBEDTLS_PRINTF_LONGLONG "I64d"
eugenee42a50d2021-05-12 12:33:36 -0400131#else /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000132 #define MBEDTLS_PRINTF_SIZET "zu"
133 #define MBEDTLS_PRINTF_LONGLONG "lld"
eugenee42a50d2021-05-12 12:33:36 -0400134#endif /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000135
Paul Bakker5121ce52009-01-03 21:22:43 +0000136#ifdef __cplusplus
137extern "C" {
138#endif
139
Paul Bakkerc73079a2014-04-25 16:34:30 +0200140/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000141 * \brief Set the threshold error level to handle globally all debug output.
142 * Debug messages that have a level over the threshold value are
143 * discarded.
144 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200145 *
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000146 * \param threshold theshold level of messages to filter on. Messages at a
147 * higher level will be discarded.
148 * - Debug levels
149 * - 0 No debug
150 * - 1 Error
151 * - 2 State change
152 * - 3 Informational
153 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155void mbedtls_debug_set_threshold( int threshold );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200156
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000157/**
Janos Follathd75b7822016-03-18 16:28:20 +0000158 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000159 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
160 * context, file and line number parameters.
161 *
162 * \param ssl SSL context
163 * \param level error level of the debug message
164 * \param file file the message has occurred in
165 * \param line line number the message has occurred at
166 * \param format format specifier, in printf format
167 * \param ... variables used by the format specifier
168 *
169 * \attention This function is intended for INTERNAL usage within the
170 * library only.
171 */
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +0200172void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200173 const char *file, int line,
Paul Elliott4e589702020-12-09 14:38:01 +0000174 const char *format, ... ) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200175
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000176/**
177 * \brief Print the return value of a function to the debug output. This
178 * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
179 * which supplies the ssl context, file and line number parameters.
180 *
181 * \param ssl SSL context
182 * \param level error level of the debug message
183 * \param file file the error has occurred in
184 * \param line line number the error has occurred in
185 * \param text the name of the function that returned the error
186 * \param ret the return code value
187 *
188 * \attention This function is intended for INTERNAL usage within the
189 * library only.
190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000192 const char *file, int line,
193 const char *text, int ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000195/**
196 * \brief Output a buffer of size len bytes to the debug output. This function
197 * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
198 * which supplies the ssl context, file and line number parameters.
199 *
200 * \param ssl SSL context
201 * \param level error level of the debug message
202 * \param file file the error has occurred in
203 * \param line line number the error has occurred in
204 * \param text a name or label for the buffer being dumped. Normally the
205 * variable or buffer name
206 * \param buf the buffer to be outputted
207 * \param len length of the buffer
208 *
209 * \attention This function is intended for INTERNAL usage within the
210 * library only.
211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000213 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000214 const unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000217/**
218 * \brief Print a MPI variable to the debug output. This function is always
219 * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
220 * ssl context, file and line number parameters.
221 *
222 * \param ssl SSL context
223 * \param level error level of the debug message
224 * \param file file the error has occurred in
225 * \param line line number the error has occurred in
226 * \param text a name or label for the MPI being output. Normally the
227 * variable name
228 * \param X the MPI variable
229 *
230 * \attention This function is intended for INTERNAL usage within the
231 * library only.
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000234 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 const char *text, const mbedtls_mpi *X );
Paul Bakkered27a042013-04-18 22:46:23 +0200236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000239/**
240 * \brief Print an ECP point to the debug output. This function is always
241 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
242 * ssl context, file and line number parameters.
243 *
244 * \param ssl SSL context
245 * \param level error level of the debug message
246 * \param file file the error has occurred in
247 * \param line line number the error has occurred in
248 * \param text a name or label for the ECP point being output. Normally the
249 * variable name
250 * \param X the ECP point
251 *
252 * \attention This function is intended for INTERNAL usage within the
253 * library only.
254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100256 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 const char *text, const mbedtls_ecp_point *X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100258#endif
259
Hanno Becker612a2f12020-10-09 09:19:39 +0100260#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000261/**
262 * \brief Print a X.509 certificate structure to the debug output. This
263 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
264 * which supplies the ssl context, file and line number parameters.
265 *
266 * \param ssl SSL context
267 * \param level error level of the debug message
268 * \param file file the error has occurred in
269 * \param line line number the error has occurred in
270 * \param text a name or label for the certificate being output
271 * \param crt X.509 certificate structure
272 *
273 * \attention This function is intended for INTERNAL usage within the
274 * library only.
275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000277 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 const char *text, const mbedtls_x509_crt *crt );
Paul Bakkered27a042013-04-18 22:46:23 +0200279#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
Janos Follath948f4be2018-08-22 01:37:55 +0100281#if defined(MBEDTLS_ECDH_C)
282typedef enum
283{
284 MBEDTLS_DEBUG_ECDH_Q,
285 MBEDTLS_DEBUG_ECDH_QP,
286 MBEDTLS_DEBUG_ECDH_Z,
287} mbedtls_debug_ecdh_attr;
288
289/**
290 * \brief Print a field of the ECDH structure in the SSL context to the debug
291 * output. This function is always used through the
292 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
293 * and line number parameters.
294 *
295 * \param ssl SSL context
296 * \param level error level of the debug message
297 * \param file file the error has occurred in
298 * \param line line number the error has occurred in
299 * \param ecdh the ECDH context
300 * \param attr the identifier of the attribute being output
301 *
302 * \attention This function is intended for INTERNAL usage within the
303 * library only.
304 */
305void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
306 const char *file, int line,
307 const mbedtls_ecdh_context *ecdh,
308 mbedtls_debug_ecdh_attr attr );
309#endif
310
Paul Bakker5121ce52009-01-03 21:22:43 +0000311#ifdef __cplusplus
312}
313#endif
314
315#endif /* debug.h */