blob: acc85ded740490ff6699dfaf0dfd2a10956c0980 [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)
62#define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020063 mbedtls_debug_print_crt( ssl, level, __FILE__, __LINE__, text, crt )
Paul Bakkered27a042013-04-18 22:46:23 +020064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Janos Follath948f4be2018-08-22 01:37:55 +010066#if defined(MBEDTLS_ECDH_C)
67#define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr ) \
68 mbedtls_debug_printf_ecdh( ssl, level, __FILE__, __LINE__, ecdh, attr )
69#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#else /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#define MBEDTLS_SSL_DEBUG_MSG( level, args ) do { } while( 0 )
74#define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) do { } while( 0 )
75#define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) do { } while( 0 )
76#define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) do { } while( 0 )
77#define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) do { } while( 0 )
78#define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) do { } while( 0 )
Janos Follath948f4be2018-08-22 01:37:55 +010079#define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr ) do { } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Elliott4e589702020-12-09 14:38:01 +000083/**
84 * \def MBEDTLS_PRINTF_ATTRIBUTE
85 *
86 * Mark a function as having printf attributes, and thus enable checking
87 * via -wFormat and other flags. This does nothing in windows builds as the
88 * windows compiler does not support it.
89 *
90 * Module: library/debug.c
91 * Caller:
92 *
93 * This module provides debugging functions.
94 */
Paul Elliottf8d733e2020-12-17 18:23:31 +000095#if defined(__GNUC__)
Paul Elliott4e589702020-12-09 14:38:01 +000096#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
97 __attribute__((format (printf, string_index, first_to_check)))
Paul Elliottabb3af72020-12-18 16:43:22 +000098#else
99#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
Paul Elliott4e589702020-12-09 14:38:01 +0000100#endif
101
Paul Elliottd48d5c62021-01-07 14:47:05 +0000102/**
103 * \def MBEDTLS_PRINTF_SIZET
104 *
105 * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
106 * and MinGW we need to define the printf specifier for size_t
107 * and long long per platform.
108 *
109 * Module: library/debug.c
110 * Caller:
111 *
112 * This module provides debugging functions.
113 */
114#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1800)
Paul Elliott48438c72021-01-08 17:05:25 +0000115 #include <inttypes.h>
116 #define MBEDTLS_PRINTF_SIZET PRIuPTR
117 #define MBEDTLS_PRINTF_LONGLONG "I64d"
Paul Elliottd48d5c62021-01-07 14:47:05 +0000118#else
119 #define MBEDTLS_PRINTF_SIZET "zu"
120 #define MBEDTLS_PRINTF_LONGLONG "lld"
121#endif
122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123#ifdef __cplusplus
124extern "C" {
125#endif
126
Paul Bakkerc73079a2014-04-25 16:34:30 +0200127/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000128 * \brief Set the threshold error level to handle globally all debug output.
129 * Debug messages that have a level over the threshold value are
130 * discarded.
131 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200132 *
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000133 * \param threshold theshold level of messages to filter on. Messages at a
134 * higher level will be discarded.
135 * - Debug levels
136 * - 0 No debug
137 * - 1 Error
138 * - 2 State change
139 * - 3 Informational
140 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142void mbedtls_debug_set_threshold( int threshold );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200143
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000144/**
Janos Follathd75b7822016-03-18 16:28:20 +0000145 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000146 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
147 * context, file and line number parameters.
148 *
149 * \param ssl SSL context
150 * \param level error level of the debug message
151 * \param file file the message has occurred in
152 * \param line line number the message has occurred at
153 * \param format format specifier, in printf format
154 * \param ... variables used by the format specifier
155 *
156 * \attention This function is intended for INTERNAL usage within the
157 * library only.
158 */
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +0200159void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200160 const char *file, int line,
Paul Elliott4e589702020-12-09 14:38:01 +0000161 const char *format, ... ) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200162
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000163/**
164 * \brief Print the return value of a function to the debug output. This
165 * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
166 * which supplies the ssl 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 error has occurred in
171 * \param line line number the error has occurred in
172 * \param text the name of the function that returned the error
173 * \param ret the return code value
174 *
175 * \attention This function is intended for INTERNAL usage within the
176 * library only.
177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000179 const char *file, int line,
180 const char *text, int ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000182/**
183 * \brief Output a buffer of size len bytes to the debug output. This function
184 * is always used through the MBEDTLS_SSL_DEBUG_BUF() 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 a name or label for the buffer being dumped. Normally the
192 * variable or buffer name
193 * \param buf the buffer to be outputted
194 * \param len length of the buffer
195 *
196 * \attention This function is intended for INTERNAL usage within the
197 * library only.
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000200 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000201 const unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000204/**
205 * \brief Print a MPI variable to the debug output. This function is always
206 * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
207 * ssl context, file and line number parameters.
208 *
209 * \param ssl SSL context
210 * \param level error level of the debug message
211 * \param file file the error has occurred in
212 * \param line line number the error has occurred in
213 * \param text a name or label for the MPI being output. Normally the
214 * variable name
215 * \param X the MPI variable
216 *
217 * \attention This function is intended for INTERNAL usage within the
218 * library only.
219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000221 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 const char *text, const mbedtls_mpi *X );
Paul Bakkered27a042013-04-18 22:46:23 +0200223#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000226/**
227 * \brief Print an ECP point to the debug output. This function is always
228 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
229 * ssl context, file and line number parameters.
230 *
231 * \param ssl SSL context
232 * \param level error level of the debug message
233 * \param file file the error has occurred in
234 * \param line line number the error has occurred in
235 * \param text a name or label for the ECP point being output. Normally the
236 * variable name
237 * \param X the ECP point
238 *
239 * \attention This function is intended for INTERNAL usage within the
240 * library only.
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100243 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 const char *text, const mbedtls_ecp_point *X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100245#endif
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_X509_CRT_PARSE_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000248/**
249 * \brief Print a X.509 certificate structure to the debug output. This
250 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
251 * which supplies the ssl context, file and line number parameters.
252 *
253 * \param ssl SSL context
254 * \param level error level of the debug message
255 * \param file file the error has occurred in
256 * \param line line number the error has occurred in
257 * \param text a name or label for the certificate being output
258 * \param crt X.509 certificate structure
259 *
260 * \attention This function is intended for INTERNAL usage within the
261 * library only.
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000264 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 const char *text, const mbedtls_x509_crt *crt );
Paul Bakkered27a042013-04-18 22:46:23 +0200266#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Janos Follath948f4be2018-08-22 01:37:55 +0100268#if defined(MBEDTLS_ECDH_C)
269typedef enum
270{
271 MBEDTLS_DEBUG_ECDH_Q,
272 MBEDTLS_DEBUG_ECDH_QP,
273 MBEDTLS_DEBUG_ECDH_Z,
274} mbedtls_debug_ecdh_attr;
275
276/**
277 * \brief Print a field of the ECDH structure in the SSL context to the debug
278 * output. This function is always used through the
279 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
280 * and line number parameters.
281 *
282 * \param ssl SSL context
283 * \param level error level of the debug message
284 * \param file file the error has occurred in
285 * \param line line number the error has occurred in
286 * \param ecdh the ECDH context
287 * \param attr the identifier of the attribute being output
288 *
289 * \attention This function is intended for INTERNAL usage within the
290 * library only.
291 */
292void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
293 const char *file, int line,
294 const mbedtls_ecdh_context *ecdh,
295 mbedtls_debug_ecdh_attr attr );
296#endif
297
Paul Bakker5121ce52009-01-03 21:22:43 +0000298#ifdef __cplusplus
299}
300#endif
301
302#endif /* debug.h */