blob: abc2d4f07ca04f252214ab8d300052bf0677f83c [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útia2947ac2020-08-19 16:37:36 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000027 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_DEBUG_H
50#define MBEDTLS_DEBUG_H
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker314052f2011-08-15 09:07:52 +000053#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Rich Evans00ab4702015-02-06 13:43:58 +000057
Paul Bakker314052f2011-08-15 09:07:52 +000058#include "ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_ECP_C)
Paul Bakker41c83d32013-03-20 14:39:14 +010061#include "ecp.h"
62#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020066#define MBEDTLS_DEBUG_STRIP_PARENS( ... ) __VA_ARGS__
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#define MBEDTLS_SSL_DEBUG_MSG( level, args ) \
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020069 mbedtls_debug_print_msg( ssl, level, __FILE__, __LINE__, \
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020070 MBEDTLS_DEBUG_STRIP_PARENS args )
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020073 mbedtls_debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020076 mbedtls_debug_print_buf( ssl, level, __FILE__, __LINE__, text, buf, len )
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_BIGNUM_C)
79#define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020080 mbedtls_debug_print_mpi( ssl, level, __FILE__, __LINE__, text, X )
Paul Bakkered27a042013-04-18 22:46:23 +020081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_ECP_C)
84#define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020085 mbedtls_debug_print_ecp( ssl, level, __FILE__, __LINE__, text, X )
Paul Bakkered27a042013-04-18 22:46:23 +020086#endif
Paul Bakker41c83d32013-03-20 14:39:14 +010087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_X509_CRT_PARSE_C)
89#define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) \
Manuel Pégourié-Gonnard2ff873c2015-05-19 14:38:09 +020090 mbedtls_debug_print_crt( ssl, level, __FILE__, __LINE__, text, crt )
Paul Bakkered27a042013-04-18 22:46:23 +020091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Janos Follath948f4be2018-08-22 01:37:55 +010093#if defined(MBEDTLS_ECDH_C)
94#define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr ) \
95 mbedtls_debug_printf_ecdh( ssl, level, __FILE__, __LINE__, ecdh, attr )
96#endif
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#else /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define MBEDTLS_SSL_DEBUG_MSG( level, args ) do { } while( 0 )
101#define MBEDTLS_SSL_DEBUG_RET( level, text, ret ) do { } while( 0 )
102#define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len ) do { } while( 0 )
103#define MBEDTLS_SSL_DEBUG_MPI( level, text, X ) do { } while( 0 )
104#define MBEDTLS_SSL_DEBUG_ECP( level, text, X ) do { } while( 0 )
105#define MBEDTLS_SSL_DEBUG_CRT( level, text, crt ) do { } while( 0 )
Janos Follath948f4be2018-08-22 01:37:55 +0100106#define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr ) do { } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110#ifdef __cplusplus
111extern "C" {
112#endif
113
Paul Bakkerc73079a2014-04-25 16:34:30 +0200114/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000115 * \brief Set the threshold error level to handle globally all debug output.
116 * Debug messages that have a level over the threshold value are
117 * discarded.
118 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200119 *
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000120 * \param threshold theshold level of messages to filter on. Messages at a
121 * higher level will be discarded.
122 * - Debug levels
123 * - 0 No debug
124 * - 1 Error
125 * - 2 State change
126 * - 3 Informational
127 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129void mbedtls_debug_set_threshold( int threshold );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200130
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000131/**
Janos Follathd75b7822016-03-18 16:28:20 +0000132 * \brief Print a message to the debug output. This function is always used
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000133 * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
134 * context, file and line number parameters.
135 *
136 * \param ssl SSL context
137 * \param level error level of the debug message
138 * \param file file the message has occurred in
139 * \param line line number the message has occurred at
140 * \param format format specifier, in printf format
141 * \param ... variables used by the format specifier
142 *
143 * \attention This function is intended for INTERNAL usage within the
144 * library only.
145 */
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +0200146void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200147 const char *file, int line,
148 const char *format, ... );
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200149
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000150/**
151 * \brief Print the return value of a function to the debug output. This
152 * function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
153 * which supplies the ssl context, file and line number parameters.
154 *
155 * \param ssl SSL context
156 * \param level error level of the debug message
157 * \param file file the error has occurred in
158 * \param line line number the error has occurred in
159 * \param text the name of the function that returned the error
160 * \param ret the return code value
161 *
162 * \attention This function is intended for INTERNAL usage within the
163 * library only.
164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000166 const char *file, int line,
167 const char *text, int ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000169/**
170 * \brief Output a buffer of size len bytes to the debug output. This function
171 * is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
172 * which supplies the ssl context, file and line number parameters.
173 *
174 * \param ssl SSL context
175 * \param level error level of the debug message
176 * \param file file the error has occurred in
177 * \param line line number the error has occurred in
178 * \param text a name or label for the buffer being dumped. Normally the
179 * variable or buffer name
180 * \param buf the buffer to be outputted
181 * \param len length of the buffer
182 *
183 * \attention This function is intended for INTERNAL usage within the
184 * library only.
185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000187 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000188 const unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_BIGNUM_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000191/**
192 * \brief Print a MPI variable to the debug output. This function is always
193 * used through the MBEDTLS_SSL_DEBUG_MPI() macro, which supplies the
194 * ssl context, file and line number parameters.
195 *
196 * \param ssl SSL context
197 * \param level error level of the debug message
198 * \param file file the error has occurred in
199 * \param line line number the error has occurred in
200 * \param text a name or label for the MPI being output. Normally the
201 * variable name
202 * \param X the MPI variable
203 *
204 * \attention This function is intended for INTERNAL usage within the
205 * library only.
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000208 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 const char *text, const mbedtls_mpi *X );
Paul Bakkered27a042013-04-18 22:46:23 +0200210#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_ECP_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000213/**
214 * \brief Print an ECP point to the debug output. This function is always
215 * used through the MBEDTLS_SSL_DEBUG_ECP() macro, which supplies the
216 * ssl context, file and line number parameters.
217 *
218 * \param ssl SSL context
219 * \param level error level of the debug message
220 * \param file file the error has occurred in
221 * \param line line number the error has occurred in
222 * \param text a name or label for the ECP point being output. Normally the
223 * variable name
224 * \param X the ECP point
225 *
226 * \attention This function is intended for INTERNAL usage within the
227 * library only.
228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100230 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 const char *text, const mbedtls_ecp_point *X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100232#endif
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_X509_CRT_PARSE_C)
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000235/**
236 * \brief Print a X.509 certificate structure to the debug output. This
237 * function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
238 * which supplies the ssl context, file and line number parameters.
239 *
240 * \param ssl SSL context
241 * \param level error level of the debug message
242 * \param file file the error has occurred in
243 * \param line line number the error has occurred in
244 * \param text a name or label for the certificate being output
245 * \param crt X.509 certificate structure
246 *
247 * \attention This function is intended for INTERNAL usage within the
248 * library only.
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000251 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 const char *text, const mbedtls_x509_crt *crt );
Paul Bakkered27a042013-04-18 22:46:23 +0200253#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
Janos Follath948f4be2018-08-22 01:37:55 +0100255#if defined(MBEDTLS_ECDH_C)
256typedef enum
257{
258 MBEDTLS_DEBUG_ECDH_Q,
259 MBEDTLS_DEBUG_ECDH_QP,
260 MBEDTLS_DEBUG_ECDH_Z,
261} mbedtls_debug_ecdh_attr;
262
263/**
264 * \brief Print a field of the ECDH structure in the SSL context to the debug
265 * output. This function is always used through the
266 * MBEDTLS_SSL_DEBUG_ECDH() macro, which supplies the ssl context, file
267 * and line number parameters.
268 *
269 * \param ssl SSL context
270 * \param level error level of the debug message
271 * \param file file the error has occurred in
272 * \param line line number the error has occurred in
273 * \param ecdh the ECDH context
274 * \param attr the identifier of the attribute being output
275 *
276 * \attention This function is intended for INTERNAL usage within the
277 * library only.
278 */
279void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
280 const char *file, int line,
281 const mbedtls_ecdh_context *ecdh,
282 mbedtls_debug_ecdh_attr attr );
283#endif
284
Paul Bakker5121ce52009-01-03 21:22:43 +0000285#ifdef __cplusplus
286}
287#endif
288
289#endif /* debug.h */
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000290