blob: bcc640c6112a5d4ceabd41ecbf7917cf10808a58 [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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039#define MBEDTLS_DEBUG_STRIP_PARENS(...) __VA_ARGS__
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041#define MBEDTLS_SSL_DEBUG_MSG(level, args) \
42 mbedtls_debug_print_msg(ssl, level, __FILE__, __LINE__, \
43 MBEDTLS_DEBUG_STRIP_PARENS args)
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045#define MBEDTLS_SSL_DEBUG_RET(level, text, ret) \
46 mbedtls_debug_print_ret(ssl, level, __FILE__, __LINE__, text, ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048#define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) \
49 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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052#define MBEDTLS_SSL_DEBUG_MPI(level, text, X) \
53 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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057#define MBEDTLS_SSL_DEBUG_ECP(level, text, X) \
58 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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
63 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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#else /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073#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)
79#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
Paul Elliottaa5e1322021-03-05 15:52:25 +000087 * via -wFormat and other flags. This does nothing on builds with compilers
88 * that do not support the format attribute
Paul Elliott4e589702020-12-09 14:38:01 +000089 *
90 * Module: library/debug.c
91 * Caller:
92 *
93 * This module provides debugging functions.
94 */
Paul Elliottaa5e1322021-03-05 15:52:25 +000095#if defined(__has_attribute)
96#if __has_attribute(format)
eugene7f5cd472021-05-12 08:50:36 -040097#if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
Paul Elliott4e589702020-12-09 14:38:01 +000098#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 __attribute__((__format__(gnu_printf, string_index, first_to_check)))
eugene7f5cd472021-05-12 08:50:36 -0400100#else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
101#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
102 __attribute__((format(printf, string_index, first_to_check)))
103#endif
Paul Elliottaa5e1322021-03-05 15:52:25 +0000104#else /* __has_attribute(format) */
105#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
106#endif /* __has_attribute(format) */
107#else /* defined(__has_attribute) */
Paul Elliottabb3af72020-12-18 16:43:22 +0000108#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
Paul Elliott4e589702020-12-09 14:38:01 +0000109#endif
110
Paul Elliottd48d5c62021-01-07 14:47:05 +0000111/**
112 * \def MBEDTLS_PRINTF_SIZET
113 *
114 * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
115 * and MinGW we need to define the printf specifier for size_t
116 * and long long per platform.
117 *
118 * Module: library/debug.c
119 * Caller:
120 *
121 * This module provides debugging functions.
122 */
eugene7f5cd472021-05-12 08:50:36 -0400123#if (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800)
Paul Elliott48438c72021-01-08 17:05:25 +0000124 #include <inttypes.h>
125 #define MBEDTLS_PRINTF_SIZET PRIuPTR
126 #define MBEDTLS_PRINTF_LONGLONG "I64d"
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127#else \
128 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000129 #define MBEDTLS_PRINTF_SIZET "zu"
130 #define MBEDTLS_PRINTF_LONGLONG "lld"
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131#endif \
132 /* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134#ifdef __cplusplus
135extern "C" {
136#endif
137
Paul Bakkerc73079a2014-04-25 16:34:30 +0200138/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000139 * \brief Set the threshold error level to handle globally all debug output.
140 * Debug messages that have a level over the threshold value are
141 * discarded.
142 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200143 *
Shaun Case0e7791f2021-12-20 21:14:10 -0800144 * \param threshold threshold level of messages to filter on. Messages at a
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000145 * higher level will be discarded.
146 * - Debug levels
147 * - 0 No debug
148 * - 1 Error
149 * - 2 State change
150 * - 3 Informational
151 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200152 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153void mbedtls_debug_set_threshold(int threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200154
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000155/**
Janos Follathd75b7822016-03-18 16:28:20 +0000156 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000157 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
158 * context, file and line number parameters.
159 *
160 * \param ssl SSL context
161 * \param level error level of the debug message
162 * \param file file the message has occurred in
163 * \param line line number the message has occurred at
164 * \param format format specifier, in printf format
165 * \param ... variables used by the format specifier
166 *
167 * \attention This function is intended for INTERNAL usage within the
168 * library only.
169 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
171 const char *file, int line,
172 const char *format, ...) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200173
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000174/**
175 * \brief Print the return value of a function to the debug output. This
176 * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
177 * which supplies the ssl context, file and line number parameters.
178 *
179 * \param ssl SSL context
180 * \param level error level of the debug message
181 * \param file file the error has occurred in
182 * \param line line number the error has occurred in
183 * \param text the name of the function that returned the error
184 * \param ret the return code value
185 *
186 * \attention This function is intended for INTERNAL usage within the
187 * library only.
188 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
190 const char *file, int line,
191 const char *text, int ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000193/**
194 * \brief Output a buffer of size len bytes to the debug output. This function
195 * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
196 * which supplies the ssl context, file and line number parameters.
197 *
198 * \param ssl SSL context
199 * \param level error level of the debug message
200 * \param file file the error has occurred in
201 * \param line line number the error has occurred in
202 * \param text a name or label for the buffer being dumped. Normally the
203 * variable or buffer name
204 * \param buf the buffer to be outputted
205 * \param len length of the buffer
206 *
207 * \attention This function is intended for INTERNAL usage within the
208 * library only.
209 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
211 const char *file, int line, const char *text,
212 const unsigned char *buf, size_t len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000215/**
216 * \brief Print a MPI variable to the debug output. This function is always
217 * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
218 * ssl context, file and line number parameters.
219 *
220 * \param ssl SSL context
221 * \param level error level of the debug message
222 * \param file file the error has occurred in
223 * \param line line number the error has occurred in
224 * \param text a name or label for the MPI being output. Normally the
225 * variable name
226 * \param X the MPI variable
227 *
228 * \attention This function is intended for INTERNAL usage within the
229 * library only.
230 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
232 const char *file, int line,
233 const char *text, const mbedtls_mpi *X);
Paul Bakkered27a042013-04-18 22:46:23 +0200234#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000237/**
238 * \brief Print an ECP point to the debug output. This function is always
239 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
240 * ssl context, file and line number parameters.
241 *
242 * \param ssl SSL context
243 * \param level error level of the debug message
244 * \param file file the error has occurred in
245 * \param line line number the error has occurred in
246 * \param text a name or label for the ECP point being output. Normally the
247 * variable name
248 * \param X the ECP point
249 *
250 * \attention This function is intended for INTERNAL usage within the
251 * library only.
252 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
254 const char *file, int line,
255 const char *text, const mbedtls_ecp_point *X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100256#endif
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_X509_CRT_PARSE_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000259/**
260 * \brief Print a X.509 certificate structure to the debug output. This
261 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
262 * which supplies the ssl context, file and line number parameters.
263 *
264 * \param ssl SSL context
265 * \param level error level of the debug message
266 * \param file file the error has occurred in
267 * \param line line number the error has occurred in
268 * \param text a name or label for the certificate being output
269 * \param crt X.509 certificate structure
270 *
271 * \attention This function is intended for INTERNAL usage within the
272 * library only.
273 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
275 const char *file, int line,
276 const char *text, const mbedtls_x509_crt *crt);
Paul Bakkered27a042013-04-18 22:46:23 +0200277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000278
Janos Follath948f4be2018-08-22 01:37:55 +0100279#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280typedef enum {
Janos Follath948f4be2018-08-22 01:37:55 +0100281 MBEDTLS_DEBUG_ECDH_Q,
282 MBEDTLS_DEBUG_ECDH_QP,
283 MBEDTLS_DEBUG_ECDH_Z,
284} mbedtls_debug_ecdh_attr;
285
286/**
287 * \brief Print a field of the ECDH structure in the SSL context to the debug
288 * output. This function is always used through the
289 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
290 * and line number parameters.
291 *
292 * \param ssl SSL context
293 * \param level error level of the debug message
294 * \param file file the error has occurred in
295 * \param line line number the error has occurred in
296 * \param ecdh the ECDH context
297 * \param attr the identifier of the attribute being output
298 *
299 * \attention This function is intended for INTERNAL usage within the
300 * library only.
301 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
303 const char *file, int line,
304 const mbedtls_ecdh_context *ecdh,
305 mbedtls_debug_ecdh_attr attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100306#endif
307
Paul Bakker5121ce52009-01-03 21:22:43 +0000308#ifdef __cplusplus
309}
310#endif
311
312#endif /* debug.h */